Chromium Code Reviews| 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.Collections; | 12 import java.util.Collections; |
| 13 import java.util.Comparator; | 13 import java.util.Comparator; |
| 14 import java.util.List; | 14 import java.util.List; |
| 15 | 15 |
| 16 /** | 16 /** |
| 17 * This class instantiates SchemaOrgParser and implements MarkupParser.Accessor interface to provide | 17 * This class instantiates SchemaOrgParser and implements MarkupParser.Accessor interface to provide |
| 18 * access to properties that SchemaOrgParser has parsed. | 18 * access to properties that SchemaOrgParser has parsed. |
| 19 */ | 19 */ |
| 20 public class SchemaOrgParserAccessor implements MarkupParser.Accessor { | 20 public class SchemaOrgParserAccessor implements MarkupParser.Accessor { |
| 21 | 21 |
| 22 private static List<SchemaOrgParser.Type> supportedTypes; | |
| 22 private SchemaOrgParser mParser; | 23 private SchemaOrgParser mParser; |
| 23 private final Element mRoot; | 24 private final Element mRoot; |
| 24 private final TimingInfo mTimingInfo; | 25 private final TimingInfo mTimingInfo; |
| 26 private static final String ENGLISH_LANGUAGE = "en"; | |
| 25 | 27 |
| 26 /** | 28 /** |
| 27 * The object that instantiates SchemaOrgParser and implements its MarkupPar ser.Accessor | 29 * The object that instantiates SchemaOrgParser and implements its MarkupPar ser.Accessor |
| 28 * interface. | 30 * interface. |
| 29 */ | 31 */ |
| 30 public SchemaOrgParserAccessor(Element root) { | 32 public SchemaOrgParserAccessor(Element root) { |
| 31 this(root, (TimingInfo) null); | 33 this(root, (TimingInfo) null); |
| 32 } | 34 } |
| 33 | 35 |
| 34 public SchemaOrgParserAccessor(Element root, TimingInfo timingInfo) { | 36 public SchemaOrgParserAccessor(Element root, TimingInfo timingInfo) { |
| 35 mRoot = root; | 37 mRoot = root; |
| 36 mTimingInfo = timingInfo; | 38 mTimingInfo = timingInfo; |
| 37 } | 39 } |
| 38 | 40 |
| 41 static { | |
| 42 supportedTypes = new ArrayList<>(); | |
| 43 supportedTypes.add(SchemaOrgParser.Type.RECIPE); | |
| 44 } | |
| 45 | |
| 39 private void init() { | 46 private void init() { |
| 40 if (mParser == null) { | 47 if (mParser == null) { |
| 41 mParser = new SchemaOrgParser(mRoot, mTimingInfo); | 48 mParser = new SchemaOrgParser(mRoot, mTimingInfo); |
| 42 } | 49 } |
| 43 } | 50 } |
| 44 | 51 |
| 45 @Override | 52 @Override |
| 46 public String getTitle() { | 53 public String getTitle() { |
| 47 init(); | 54 init(); |
| 48 String title = ""; | 55 String title = ""; |
| (...skipping 128 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 177 List<SchemaOrgParser.ArticleItem> articles = mParser.getArticleItems(); | 184 List<SchemaOrgParser.ArticleItem> articles = mParser.getArticleItems(); |
| 178 return articles.isEmpty() ? null : articles.get(0).getArticle(); | 185 return articles.isEmpty() ? null : articles.get(0).getArticle(); |
| 179 } | 186 } |
| 180 | 187 |
| 181 @Override | 188 @Override |
| 182 public boolean optOut() { | 189 public boolean optOut() { |
| 183 return false; | 190 return false; |
| 184 } | 191 } |
| 185 | 192 |
| 186 /** | 193 /** |
| 194 * Get schema.org parsed content. | |
| 195 * | |
| 196 * Content is generated when a main entity is identified or | |
| 197 * has only one entity item parsed and it is supported | |
| 198 * | |
| 199 * @return HTML output from the supported item. | |
| 200 */ | |
| 201 @Override | |
| 202 public String getStructuredData() { | |
| 203 init(); | |
|
wychen
2016/05/31 21:56:34
Only init() when the page is in English.
dalmirsilva
2016/07/06 17:53:34
Done.
| |
| 204 String output = ""; | |
| 205 if (DomUtil.getLanguage(mRoot).contains(ENGLISH_LANGUAGE)) { | |
|
wychen
2016/05/31 21:56:34
Strictly speaking, the lang code should "start wit
dalmirsilva
2016/07/06 17:53:34
Done.
| |
| 206 SchemaOrgParser.ThingItem entity = mParser.getMainEntity(); | |
| 207 List<SchemaOrgParser.ThingItem> topLevelEntities = | |
| 208 mParser.getTopLevelEntities(); | |
| 209 if (entity == null && topLevelEntities.size() == 1) { | |
| 210 entity = topLevelEntities.get(0); | |
| 211 } | |
| 212 if (entity != null && supportedTypes.contains(entity.getType())) { | |
| 213 output = entity.generateOutput(); | |
| 214 } | |
| 215 } | |
| 216 return output; | |
| 217 } | |
| 218 | |
| 219 /** | |
| 187 * Sort a list of {@link SchemaOrgParser.ArticleItem}s by their area | 220 * Sort a list of {@link SchemaOrgParser.ArticleItem}s by their area |
| 188 * in descending order. | 221 * in descending order. |
| 189 * @param articles List of {@link SchemaOrgParser.ArticleItem}s to | 222 * @param articles List of {@link SchemaOrgParser.ArticleItem}s to |
| 190 * be sorted. | 223 * be sorted. |
| 191 * @return The sorted list. | 224 * @return The sorted list. |
| 192 */ | 225 */ |
| 193 private List<SchemaOrgParser.ArticleItem> sortArticlesByArea( | 226 private List<SchemaOrgParser.ArticleItem> sortArticlesByArea( |
| 194 List<SchemaOrgParser.ArticleItem> articles) { | 227 List<SchemaOrgParser.ArticleItem> articles) { |
| 195 List<SchemaOrgParser.ArticleItem> sortedArticles = | 228 List<SchemaOrgParser.ArticleItem> sortedArticles = |
| 196 new ArrayList<>(articles); | 229 new ArrayList<>(articles); |
| 197 Collections.sort(sortedArticles, | 230 Collections.sort(sortedArticles, |
| 198 new Comparator<SchemaOrgParser.ArticleItem>() { | 231 new Comparator<SchemaOrgParser.ArticleItem>() { |
| 199 @Override | 232 @Override |
| 200 public int compare(SchemaOrgParser.ArticleItem i1, | 233 public int compare(SchemaOrgParser.ArticleItem i1, |
| 201 SchemaOrgParser.ArticleItem i2) { | 234 SchemaOrgParser.ArticleItem i2) { |
| 202 int area1 = DomUtil.getArea(i1.getElement()); | 235 int area1 = DomUtil.getArea(i1.getElement()); |
| 203 int area2 = DomUtil.getArea(i2.getElement()); | 236 int area2 = DomUtil.getArea(i2.getElement()); |
| 204 if (area1 > area2) { | 237 if (area1 > area2) { |
| 205 return -1; | 238 return -1; |
| 206 } else if (area1 < area2) { | 239 } else if (area1 < area2) { |
| 207 return 1; | 240 return 1; |
| 208 } | 241 } |
| 209 return 0; | 242 return 0; |
| 210 } | 243 } |
| 211 }); | 244 }); |
| 212 return sortedArticles; | 245 return sortedArticles; |
| 213 } | 246 } |
| 214 } | 247 } |
| OLD | NEW |