From e137277222e3f5696d4032d443429529e5fbdbea Mon Sep 17 00:00:00 2001 From: Kirill Meshkov Date: Tue, 21 Aug 2018 15:57:40 +0300 Subject: [PATCH] * MicrodataExtractor handles only properties only with methods, but some properties are stored in properties map * Some tags like does not have "a href" attribute, but just href --- .../pickaxe/SchemaToThingConverter.java | 26 +++++++++++++------ .../pickaxe/extractor/MicrodataExtractor.java | 4 +-- 2 files changed, 19 insertions(+), 11 deletions(-) diff --git a/src/main/java/com/github/mautini/pickaxe/SchemaToThingConverter.java b/src/main/java/com/github/mautini/pickaxe/SchemaToThingConverter.java index ec823b7..83a6d11 100644 --- a/src/main/java/com/github/mautini/pickaxe/SchemaToThingConverter.java +++ b/src/main/java/com/github/mautini/pickaxe/SchemaToThingConverter.java @@ -46,26 +46,36 @@ public static Optional convert(Schema schema) { } private static void setProperties(Thing.Builder builder, Schema schema, Class builderClass) - throws NoSuchMethodException, InvocationTargetException, IllegalAccessException { + throws InvocationTargetException, IllegalAccessException { // Set all the properties for (String propertyName : schema.getProperties().keySet()) { - String methodName = String.format("add%s", capitalize(propertyName)); - Method method = builderClass.getMethod(methodName, String.class); - method.invoke(builder, schema.getProperties().get(propertyName).get(0)); + try { + String methodName = String.format("add%s", capitalize(propertyName)); + Method method = builderClass.getMethod(methodName, String.class); + method.invoke(builder, schema.getProperties().get(propertyName).get(0)); + } + catch (NoSuchMethodException e){ + builder.addProperty(propertyName, schema.getProperties().get(propertyName).get(0)); + } } } private static void setChildren(Thing.Builder builder, Schema schema, Class builderClass) - throws NoSuchMethodException, InvocationTargetException, IllegalAccessException, ClassNotFoundException { + throws InvocationTargetException, IllegalAccessException, ClassNotFoundException { for (Schema child : schema.getChildren()) { if (!StringUtils.isEmpty(child.getPropertyName())) { Optional optionalThing = convert(child); if (optionalThing.isPresent()) { - String methodName = String.format("add%s", capitalize(child.getPropertyName())); - Method method = builderClass.getMethod(methodName, getInterfaceClass(getTypeName(child))); - method.invoke(builder, optionalThing.get()); + try { + String methodName = String.format("add%s", capitalize(child.getPropertyName())); + Method method = builderClass.getMethod(methodName, getInterfaceClass(getTypeName(child))); + method.invoke(builder, optionalThing.get()); + } + catch (NoSuchMethodException e){ + builder.addProperty(child.getPropertyName(), optionalThing.get()); + } } } } diff --git a/src/main/java/com/github/mautini/pickaxe/extractor/MicrodataExtractor.java b/src/main/java/com/github/mautini/pickaxe/extractor/MicrodataExtractor.java index 5ff162e..88dcebe 100644 --- a/src/main/java/com/github/mautini/pickaxe/extractor/MicrodataExtractor.java +++ b/src/main/java/com/github/mautini/pickaxe/extractor/MicrodataExtractor.java @@ -27,8 +27,6 @@ public class MicrodataExtractor implements Extractor { private static final String ITEM_PROP = "itemprop"; - private static final String HYPERLINK_TAG = "a"; - @Override public List getThings(Document document) { Elements elements = getElements(document); @@ -89,7 +87,7 @@ private Schema getTree(Element parent) { } private String getValue(Element element) { - if (HYPERLINK_TAG.equals(element.tagName()) && element.hasAttr("href")) { + if (element.hasAttr("href")) { return element.attr("href"); }