##### Coverage Report - org.apache.any23.extractor.html.GeoExtractor

Classes in this File | Line Coverage | Branch Coverage | Complexity
GeoExtractor |
0% | 0/25
0% | 0/8
| 2.5;2.5

1 |  |
     /*
2 |  |
      * Licensed to the Apache Software Foundation (ASF) under one or more
3 |  |
      * contributor license agreements.  See the NOTICE file distributed with
4 |  |
      * this work for additional information regarding copyright ownership.
5 |  |
      * The ASF licenses this file to You under the Apache License, Version 2.0
6 |  |
      * (the "License"); you may not use this file except in compliance with
7 |  |
      * the License.  You may obtain a copy of the License at
8 |  |
      *
9 |  |
      *  http://www.apache.org/licenses/LICENSE-2.0
10 |  |
      *
11 |  |
      * Unless required by applicable law or agreed to in writing, software
12 |  |
      * distributed under the License is distributed on an "AS IS" BASIS,
13 |  |
      * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 |  |
      * See the License for the specific language governing permissions and
15 |  |
      * limitations under the License.
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 |  |
      * Extractor for the <a href="http://microformats.org/wiki/geo">Geo</a>
36 |  |
      * microformat.
37 |  |
      *
38 |  |
      * @author Gabriele Renzi
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 |  |
             // Empty.
64 | 0 |
         }
65 |  |
66 |  |
         protected boolean extractEntity(Node node, ExtractionResult out) {
67 | 0 |
             if (null == node) return false;
68 |  |
             //try lat & lon
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 |  |
     }