1 | |
|
2 | |
|
3 | |
|
4 | |
|
5 | |
|
6 | |
|
7 | |
|
8 | |
|
9 | |
|
10 | |
|
11 | |
|
12 | |
|
13 | |
|
14 | |
|
15 | |
|
16 | |
|
17 | |
|
18 | |
package org.apache.any23.extractor.html; |
19 | |
|
20 | |
import org.apache.any23.extractor.ExtractionResult; |
21 | |
import org.apache.any23.extractor.ExtractorDescription; |
22 | |
import org.apache.any23.extractor.ExtractorFactory; |
23 | |
import org.apache.any23.extractor.SimpleExtractorFactory; |
24 | |
import org.apache.any23.extractor.TagSoupExtractionResult; |
25 | |
import org.apache.any23.rdf.PopularPrefixes; |
26 | |
import org.apache.any23.vocab.VCARD; |
27 | |
import org.openrdf.model.BNode; |
28 | |
import org.openrdf.model.vocabulary.RDF; |
29 | |
import org.w3c.dom.Node; |
30 | |
|
31 | |
import java.util.Arrays; |
32 | |
|
33 | |
|
34 | |
|
35 | |
|
36 | |
|
37 | |
|
38 | |
|
39 | |
|
40 | 0 | public class GeoExtractor extends EntityBasedMicroformatExtractor { |
41 | |
|
42 | 0 | private static final VCARD vVCARD = VCARD.getInstance(); |
43 | |
|
44 | 0 | public static final ExtractorFactory<GeoExtractor> factory = |
45 | |
SimpleExtractorFactory.create( |
46 | |
"html-mf-geo", |
47 | |
PopularPrefixes.createSubset("rdf", "vcard"), |
48 | |
Arrays.asList("text/html;q=0.1", "application/xhtml+xml;q=0.1"), |
49 | |
"example-mf-geo.html", |
50 | |
GeoExtractor.class |
51 | |
); |
52 | |
|
53 | |
public ExtractorDescription getDescription() { |
54 | 0 | return factory; |
55 | |
} |
56 | |
|
57 | |
protected String getBaseClassName() { |
58 | 0 | return "geo"; |
59 | |
} |
60 | |
|
61 | |
@Override |
62 | |
protected void resetExtractor() { |
63 | |
|
64 | 0 | } |
65 | |
|
66 | |
protected boolean extractEntity(Node node, ExtractionResult out) { |
67 | 0 | if (null == node) return false; |
68 | |
|
69 | 0 | final HTMLDocument document = new HTMLDocument(node); |
70 | 0 | HTMLDocument.TextField latNode = document.getSingularTextField("latitude" ); |
71 | 0 | HTMLDocument.TextField lonNode = document.getSingularTextField("longitude"); |
72 | 0 | String lat = latNode.value(); |
73 | 0 | String lon = lonNode.value(); |
74 | 0 | if ("".equals(lat) || "".equals(lon)) { |
75 | 0 | String[] both = document.getSingularUrlField("geo").value().split(";"); |
76 | 0 | if (both.length != 2) return false; |
77 | 0 | lat = both[0]; |
78 | 0 | lon = both[1]; |
79 | |
} |
80 | 0 | BNode geo = getBlankNodeFor(node); |
81 | 0 | out.writeTriple(geo, RDF.TYPE, vVCARD.Location); |
82 | 0 | final String extractorName = getDescription().getExtractorName(); |
83 | 0 | conditionallyAddStringProperty( |
84 | |
latNode.source(), |
85 | |
geo, vVCARD.latitude , lat |
86 | |
); |
87 | 0 | conditionallyAddStringProperty( |
88 | |
lonNode.source(), |
89 | |
geo, vVCARD.longitude, lon |
90 | |
); |
91 | |
|
92 | 0 | final TagSoupExtractionResult tser = (TagSoupExtractionResult) getCurrentExtractionResult(); |
93 | 0 | tser.addResourceRoot( document.getPathToLocalRoot(), geo, this.getClass() ); |
94 | |
|
95 | 0 | return true; |
96 | |
} |
97 | |
|
98 | |
} |