Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(475)

Unified Diff: java/org/chromium/distiller/SchemaOrgParserAccessor.java

Issue 1705123002: Add support for Schema.org/Recipe Base URL: https://github.com/chromium/dom-distiller.git@master
Patch Set: wychen's comments addressed Created 4 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: java/org/chromium/distiller/SchemaOrgParserAccessor.java
diff --git a/java/org/chromium/distiller/SchemaOrgParserAccessor.java b/java/org/chromium/distiller/SchemaOrgParserAccessor.java
index 83779ecea65af6b358c1e9dc6d8d57c231a15df5..351b57cf8ed2cfc8bfaea1b7b3d2efd1a57bb3e4 100644
--- a/java/org/chromium/distiller/SchemaOrgParserAccessor.java
+++ b/java/org/chromium/distiller/SchemaOrgParserAccessor.java
@@ -19,9 +19,11 @@ import java.util.List;
*/
public class SchemaOrgParserAccessor implements MarkupParser.Accessor {
+ private static List<SchemaOrgParser.Type> supportedTypes;
wychen 2016/07/24 23:06:34 I suspect this still causes some slowness. Would l
private SchemaOrgParser mParser;
private final Element mRoot;
private final TimingInfo mTimingInfo;
+ private static final String ENGLISH_LANGUAGE = "en";
/**
* The object that instantiates SchemaOrgParser and implements its MarkupParser.Accessor
@@ -36,6 +38,11 @@ public class SchemaOrgParserAccessor implements MarkupParser.Accessor {
mTimingInfo = timingInfo;
}
+ static {
+ supportedTypes = new ArrayList<>();
+ supportedTypes.add(SchemaOrgParser.Type.RECIPE);
+ }
+
private void init() {
if (mParser == null) {
mParser = new SchemaOrgParser(mRoot, mTimingInfo);
@@ -184,6 +191,32 @@ public class SchemaOrgParserAccessor implements MarkupParser.Accessor {
}
/**
+ * Get schema.org parsed content.
+ *
+ * Content is generated when a main entity is identified or
+ * has only one entity item parsed and it is supported
+ *
+ * @return HTML output from the supported item.
+ */
+ @Override
+ public String getStructuredData() {
+ String output = "";
+ if (DomUtil.getLanguage(mRoot).startsWith(ENGLISH_LANGUAGE)) {
+ init();
+ SchemaOrgParser.ThingItem entity = mParser.getMainEntity();
+ List<SchemaOrgParser.ThingItem> topLevelEntities =
+ mParser.getTopLevelEntities();
+ if (entity == null && topLevelEntities.size() == 1) {
+ entity = topLevelEntities.get(0);
+ }
+ if (entity != null && supportedTypes.contains(entity.getType())) {
+ output = entity.generateOutput();
+ }
+ }
+ return output;
+ }
+
+ /**
* Sort a list of {@link SchemaOrgParser.ArticleItem}s by their area
* in descending order.
* @param articles List of {@link SchemaOrgParser.ArticleItem}s to

Powered by Google App Engine
This is Rietveld 408576698