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/adr">adr</a> microformat.
30   *
31   * @author Gabriele Renzi
32   */
33  public class AdrExtractor extends EntityBasedMicroformatExtractor {
34  
35      private static final VCard vVCARD = VCard.getInstance();
36  
37      private static final String[] addressFields = { "post-office-box", "extended-address", "street-address", "locality",
38              "region", "country-name", "postal-code" };
39  
40      protected String getBaseClassName() {
41          return "adr";
42      }
43  
44      @Override
45      protected void resetExtractor() {
46          // Empty.
47      }
48  
49      protected boolean extractEntity(Node node, ExtractionResult out) {
50          if (null == node)
51              return false;
52          // try lat & lon
53          final HTMLDocumentTMLDocument.html#HTMLDocument">HTMLDocument document = new HTMLDocument(node);
54          BNode adr = getBlankNodeFor(node);
55          out.writeTriple(adr, RDF.TYPE, vVCARD.Address);
56          final String extractorName = getDescription().getExtractorName();
57          for (String field : addressFields) {
58              HTMLDocument.TextField[] values = document.getPluralTextField(field);
59              for (HTMLDocument.TextField val : values) {
60                  conditionallyAddStringProperty(val.source(), adr, vVCARD.getProperty(field), val.value());
61              }
62          }
63          HTMLDocument.TextField[] types = document.getPluralTextField("type");
64          for (HTMLDocument.TextField val : types) {
65              conditionallyAddStringProperty(val.source(), adr, vVCARD.addressType, val.value());
66          }
67  
68          final TagSoupExtractionResultorg/apache/any23/extractor/TagSoupExtractionResult.html#TagSoupExtractionResult">TagSoupExtractionResult tser = (TagSoupExtractionResult) getCurrentExtractionResult();
69          tser.addResourceRoot(document.getPathToLocalRoot(), adr, this.getClass());
70  
71          return true;
72      }
73  
74      @Override
75      public ExtractorDescription getDescription() {
76          return AdrExtractorFactory.getDescriptionInstance();
77      }
78  
79  }