View Javadoc

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  public class ICBMExtractor implements TagSoupDOMExtractor {
47  
48      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          String props = DomUtils.find(in, "//META[@name=\"ICBM\" or @name=\"geo.position\"]/@content");
66          if ("".equals(props)) return;
67  
68          String[] coords = props.split("[;,]");
69          float lat, lon;
70          try {
71              lat = Float.parseFloat(coords[0]);
72              lon = Float.parseFloat(coords[1]);
73          } catch (NumberFormatException nfe) {
74              return;
75          }
76  
77          final ValueFactory factory = new Any23ValueFactoryWrapper(ValueFactoryImpl.getInstance(), out);
78          BNode point = factory.createBNode();
79          out.writeTriple(extractionContext.getDocumentURI(), expand("dcterms:related"), point);
80          out.writeTriple(point, expand("rdf:type"), expand("geo:Point"));
81          out.writeTriple(point, expand("geo:lat"), factory.createLiteral(Float.toString(lat)));
82          out.writeTriple(point, expand("geo:long"), factory.createLiteral(Float.toString(lon)));
83      }
84  
85      private URI expand(String curie) {
86          return factory.getPrefixes().expand(curie);
87      }
88  
89      public ExtractorDescription getDescription() {
90          return factory;
91      }
92  
93  }