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.ExtractionResult;
21  import org.apache.any23.extractor.ExtractorDescription;
22  import org.apache.any23.extractor.TagSoupExtractionResult;
23  import org.apache.any23.vocab.VCard;
24  import org.eclipse.rdf4j.model.BNode;
25  import org.eclipse.rdf4j.model.vocabulary.RDF;
26  import org.w3c.dom.Node;
27  
28  /**
29   * Extractor for the <a href="http://microformats.org/wiki/geo">Geo</a> microformat.
30   *
31   * @author Gabriele Renzi
32   */
33  public class GeoExtractor extends EntityBasedMicroformatExtractor {
34  
35      private static final VCard vVCARD = VCard.getInstance();
36  
37      @Override
38      public ExtractorDescription getDescription() {
39          return GeoExtractorFactory.getDescriptionInstance();
40      }
41  
42      protected String getBaseClassName() {
43          return "geo";
44      }
45  
46      @Override
47      protected void resetExtractor() {
48          // Empty.
49      }
50  
51      protected boolean extractEntity(Node node, ExtractionResult out) {
52          if (null == node)
53              return false;
54          // try lat & lon
55          final HTMLDocumentTMLDocument.html#HTMLDocument">HTMLDocument document = new HTMLDocument(node);
56          HTMLDocument.TextField latNode = document.getSingularTextField("latitude");
57          HTMLDocument.TextField lonNode = document.getSingularTextField("longitude");
58          String lat = latNode.value();
59          String lon = lonNode.value();
60          if ("".equals(lat) || "".equals(lon)) {
61              String[] both = document.getSingularUrlField("geo").value().split(";");
62              if (both.length != 2)
63                  return false;
64              lat = both[0];
65              lon = both[1];
66          }
67          BNode geo = getBlankNodeFor(node);
68          out.writeTriple(geo, RDF.TYPE, vVCARD.Location);
69          conditionallyAddStringProperty(latNode.source(), geo, vVCARD.latitude, lat);
70          conditionallyAddStringProperty(lonNode.source(), geo, vVCARD.longitude, lon);
71  
72          final TagSoupExtractionResultorg/apache/any23/extractor/TagSoupExtractionResult.html#TagSoupExtractionResult">TagSoupExtractionResult tser = (TagSoupExtractionResult) getCurrentExtractionResult();
73          tser.addResourceRoot(document.getPathToLocalRoot(), geo, this.getClass());
74  
75          return true;
76      }
77  
78  }