OLD | NEW |
---|---|
1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.distiller; | 5 package org.chromium.distiller; |
6 | 6 |
7 import org.chromium.distiller.proto.DomDistillerProtos.TimingInfo; | 7 import org.chromium.distiller.proto.DomDistillerProtos.TimingInfo; |
8 | 8 |
9 import com.google.gwt.dom.client.Element; | 9 import com.google.gwt.dom.client.Element; |
10 | 10 |
11 import java.util.ArrayList; | 11 import java.util.ArrayList; |
12 import java.util.HashSet; | |
12 import java.util.List; | 13 import java.util.List; |
14 import java.util.Set; | |
13 | 15 |
14 /** | 16 /** |
15 * This class instantiates SchemaOrgParser and implements MarkupParser.Accessor interface to provide | 17 * This class instantiates SchemaOrgParser and implements MarkupParser.Accessor interface to provide |
16 * access to properties that SchemaOrgParser has parsed. | 18 * access to properties that SchemaOrgParser has parsed. |
17 */ | 19 */ |
18 public class SchemaOrgParserAccessor implements MarkupParser.Accessor { | 20 public class SchemaOrgParserAccessor implements MarkupParser.Accessor { |
21 | |
22 private static Set<SchemaOrgParser.Type> supportedTypes; | |
19 private final SchemaOrgParser parser; | 23 private final SchemaOrgParser parser; |
20 | 24 |
21 /** | 25 /** |
22 * The object that instantiates SchemaOrgParser and implements its MarkupPar ser.Accessor | 26 * The object that instantiates SchemaOrgParser and implements its MarkupPar ser.Accessor |
23 * interface. | 27 * interface. |
24 */ | 28 */ |
25 public SchemaOrgParserAccessor(Element root) { | 29 public SchemaOrgParserAccessor(Element root) { |
26 this(root, (TimingInfo) null); | 30 this(root, (TimingInfo) null); |
27 } | 31 } |
28 | 32 |
29 public SchemaOrgParserAccessor(Element root, TimingInfo timingInfo) { | 33 public SchemaOrgParserAccessor(Element root, TimingInfo timingInfo) { |
30 parser = new SchemaOrgParser(root, timingInfo); | 34 parser = new SchemaOrgParser(root, timingInfo); |
31 } | 35 } |
32 | 36 |
37 static { | |
wychen
2016/03/14 22:58:42
static sections tend to have performance issues in
| |
38 supportedTypes = new HashSet<>(); | |
39 supportedTypes.add(SchemaOrgParser.Type.RECIPE); | |
40 } | |
41 | |
33 @Override | 42 @Override |
34 public String getTitle() { | 43 public String getTitle() { |
35 String title = ""; | 44 String title = ""; |
36 List<SchemaOrgParser.ArticleItem> articles = parser.getArticleItems(); | 45 List<SchemaOrgParser.ArticleItem> articles = parser.getArticleItems(); |
37 | 46 |
38 // Get the "headline" property of the first article that has it. | 47 // Get the "headline" property of the first article that has it. |
39 for (int i = 0; i < articles.size() && title.isEmpty(); i++) { | 48 for (int i = 0; i < articles.size() && title.isEmpty(); i++) { |
40 title = articles.get(i).getStringProperty(SchemaOrgParser.HEADLINE_P ROP); | 49 title = articles.get(i).getStringProperty(SchemaOrgParser.HEADLINE_P ROP); |
41 } | 50 } |
42 | 51 |
(...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
155 @Override | 164 @Override |
156 public MarkupParser.Article getArticle() { | 165 public MarkupParser.Article getArticle() { |
157 List<SchemaOrgParser.ArticleItem> articles = parser.getArticleItems(); | 166 List<SchemaOrgParser.ArticleItem> articles = parser.getArticleItems(); |
158 return articles.isEmpty() ? null : articles.get(0).getArticle(); | 167 return articles.isEmpty() ? null : articles.get(0).getArticle(); |
159 } | 168 } |
160 | 169 |
161 @Override | 170 @Override |
162 public boolean optOut() { | 171 public boolean optOut() { |
163 return false; | 172 return false; |
164 } | 173 } |
174 | |
175 /** | |
176 * Get schema.org parsed content. | |
177 * | |
178 * Content is generated when a main entity is identified or | |
179 * has only one entity item parsed and it is supported | |
180 * | |
181 * @return HTML output from the supported item. | |
182 */ | |
183 @Override | |
184 public String getStructuredData() { | |
185 String output = ""; | |
186 SchemaOrgParser.ThingItem entity = parser.getMainEntity(); | |
187 List<SchemaOrgParser.ThingItem> topLevelEntities = | |
188 parser.getTopLevelEntities(); | |
189 if (entity == null && topLevelEntities.size() == 1) { | |
190 entity = topLevelEntities.get(0); | |
191 } | |
192 if (entity != null && supportedTypes.contains(entity.getType())) { | |
193 output = entity.generateOutput(); | |
194 } | |
195 return output; | |
196 } | |
165 } | 197 } |
OLD | NEW |