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

Classes in this File | Line Coverage | Branch Coverage | Complexity
ICBMExtractor |
0% | 0/19
0% | 0/2
| 2.3333333333333335;2.333

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.ExtractionContext;
21 |  |
     import org.apache.any23.extractor.ExtractionException;
22 |  |
     import org.apache.any23.extractor.ExtractionParameters;
23 |  |
     import org.apache.any23.extractor.ExtractionResult;
24 |  |
     import org.apache.any23.extractor.ExtractorDescription;
25 |  |
     import org.apache.any23.extractor.ExtractorFactory;
26 |  |
     import org.apache.any23.extractor.SimpleExtractorFactory;
27 |  |
     import org.apache.any23.rdf.Any23ValueFactoryWrapper;
28 |  |
     import org.apache.any23.rdf.PopularPrefixes;
29 |  |
     import org.apache.any23.extractor.Extractor.TagSoupDOMExtractor;
30 |  |
     import org.openrdf.model.BNode;
31 |  |
     import org.openrdf.model.URI;
32 |  |
     import org.openrdf.model.ValueFactory;
33 |  |
     import org.openrdf.model.impl.ValueFactoryImpl;
34 |  |
     import org.w3c.dom.Document;
35 |  |
36 |  |
     import java.io.IOException;
37 |  |
     import java.util.Arrays;
38 |  |
39 |  |
     /**
40 |  |
      * Extractor for "ICBM coordinates" provided as META headers in the head
41 |  |
      * of an HTML page.
42 |  |
      *
43 |  |
      * @author Gabriele Renzi
44 |  |
      * @author Richard Cyganiak (richard@cyganiak.de)
45 |  |
      */
46 | 0 |
     public class ICBMExtractor implements TagSoupDOMExtractor {
47 |  |
48 | 0 |
         public final static ExtractorFactory<ICBMExtractor> factory =
49 |  |
                 SimpleExtractorFactory.create(
50 |  |
                         "html-head-icbm",
51 |  |
                         PopularPrefixes.createSubset("geo", "rdf"),
52 |  |
                         Arrays.asList("text/html;q=0.01", "application/xhtml+xml;q=0.01"),
53 |  |
                         "example-icbm.html",
54 |  |
                         ICBMExtractor.class
55 |  |
                 );
56 |  |
57 |  |
         public void run(
58 |  |
                 ExtractionParameters extractionParameters,
59 |  |
                 ExtractionContext extractionContext,
60 |  |
                 Document in,
61 |  |
                 ExtractionResult out
62 |  |
         ) throws IOException, ExtractionException {
63 |  |
64 |  |
             // ICBM is the preferred method, if two values are available it is meaningless to read both
65 | 0 |
             String props = DomUtils.find(in, "//META[@name=\"ICBM\" or @name=\"geo.position\"]/@content");
66 | 0 |
             if ("".equals(props)) return;
67 |  |
68 | 0 |
             String[] coords = props.split("[;,]");
69 |  |
             float lat, lon;
70 |  |
             try {
71 | 0 |
                 lat = Float.parseFloat(coords[0]);
72 | 0 |
                 lon = Float.parseFloat(coords[1]);
73 | 0 |
             } catch (NumberFormatException nfe) {
74 | 0 |
                 return;
75 | 0 |
             }
76 |  |
77 | 0 |
             final ValueFactory factory = new Any23ValueFactoryWrapper(ValueFactoryImpl.getInstance(), out);
78 | 0 |
             BNode point = factory.createBNode();
79 | 0 |
             out.writeTriple(extractionContext.getDocumentURI(), expand("dcterms:related"), point);
80 | 0 |
             out.writeTriple(point, expand("rdf:type"), expand("geo:Point"));
81 | 0 |
             out.writeTriple(point, expand("geo:lat"), factory.createLiteral(Float.toString(lat)));
82 | 0 |
             out.writeTriple(point, expand("geo:long"), factory.createLiteral(Float.toString(lon)));
83 | 0 |
         }
84 |  |
85 |  |
         private URI expand(String curie) {
86 | 0 |
             return factory.getPrefixes().expand(curie);
87 |  |
         }
88 |  |
89 |  |
         public ExtractorDescription getDescription() {
90 | 0 |
             return factory;
91 |  |
         }
92 |  |
93 |  |
     }