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.rdf.Any23ValueFactoryWrapper;
26  import org.apache.any23.vocab.FOAF;
27  import org.apache.any23.vocab.XFN;
28  import org.apache.any23.extractor.Extractor.TagSoupDOMExtractor;
29  import org.eclipse.rdf4j.model.BNode;
30  import org.eclipse.rdf4j.model.IRI;
31  import org.eclipse.rdf4j.model.impl.SimpleValueFactory;
32  import org.eclipse.rdf4j.model.vocabulary.RDF;
33  import org.w3c.dom.Document;
34  import org.w3c.dom.Node;
35  
36  import java.io.IOException;
37  import java.util.Locale;
38  
39  /**
40   * Extractor for the <a href="http://microformats.org/wiki/xfn">XFN</a> microformat.
41   *
42   * @author Richard Cyganiak (richard@cyganiak.de)
43   */
44  public class XFNExtractor implements TagSoupDOMExtractor {
45  
46      private static final FOAF vFOAF = FOAF.getInstance();
47      private static final XFN vXFN = XFN.getInstance();
48  
49      private final static Any23ValueFactoryWrapper factoryWrapper = new Any23ValueFactoryWrapper(
50              SimpleValueFactory.getInstance());
51  
52      private HTMLDocument document;
53      private ExtractionResult out;
54  
55      @Override
56      public ExtractorDescription getDescription() {
57          return XFNExtractorFactory.getDescriptionInstance();
58      }
59  
60      @Override
61      public void run(ExtractionParameters extractionParameters, ExtractionContext extractionContext, Document in,
62              ExtractionResult out) throws IOException, ExtractionException {
63          factoryWrapper.setIssueReport(out);
64          try {
65              document = new HTMLDocument(in);
66              this.out = out;
67  
68              BNode subject = factoryWrapper.createBNode();
69              boolean foundAnyXFN = false;
70              final IRI documentIRI = extractionContext.getDocumentIRI();
71              for (Node link : document.findAll("//A[@rel][@href]")) {
72                  foundAnyXFN |= extractLink(link, subject, documentIRI);
73              }
74              if (!foundAnyXFN)
75                  return;
76              out.writeTriple(subject, RDF.TYPE, vFOAF.Person);
77              out.writeTriple(subject, vXFN.mePage, documentIRI);
78          } finally {
79              factoryWrapper.setIssueReport(null);
80          }
81      }
82  
83      private boolean extractLink(Node firstLink, BNode subject, IRI documentIRI) throws ExtractionException {
84          Node hrefNodeItem = firstLink.getAttributes().getNamedItem("href");
85          Node relNodeItem = firstLink.getAttributes().getNamedItem("rel");
86          if (hrefNodeItem == null || relNodeItem == null) {
87              return false;
88          }
89          String href = hrefNodeItem.getNodeValue();
90          String rel = relNodeItem.getNodeValue();
91  
92          String[] rels = rel.split("\\s+");
93          IRI link = document.resolveIRI(href);
94          if (containsRelMe(rels)) {
95              if (containsXFNRelExceptMe(rels)) {
96                  return false; // "me" cannot be combined with any other XFN values
97              }
98              out.writeTriple(subject, vXFN.mePage, link);
99              out.writeTriple(documentIRI, vXFN.getExtendedProperty("me"), link);
100         } else {
101             BNode person2 = factoryWrapper.createBNode();
102             boolean foundAnyXFNRel = false;
103             for (String aRel : rels) {
104                 foundAnyXFNRel |= extractRel(aRel, subject, documentIRI, person2, link);
105             }
106             if (!foundAnyXFNRel) {
107                 return false;
108             }
109             out.writeTriple(person2, RDF.TYPE, vFOAF.Person);
110             out.writeTriple(person2, vXFN.mePage, link);
111         }
112         return true;
113     }
114 
115     private boolean containsRelMe(String[] rels) {
116         for (String rel : rels) {
117             if ("me".equals(rel.toLowerCase(Locale.ROOT))) {
118                 return true;
119             }
120         }
121         return false;
122     }
123 
124     private boolean containsXFNRelExceptMe(String[] rels) {
125         for (String rel : rels) {
126             if (!"me".equals(rel.toLowerCase(Locale.ROOT)) && vXFN.isXFNLocalName(rel)) {
127                 return true;
128             }
129         }
130         return false;
131     }
132 
133     private boolean extractRel(String rel, BNode person1, IRI uri1, BNode person2, IRI uri2) {
134         IRI peopleProp = vXFN.getPropertyByLocalName(rel);
135         IRI hyperlinkProp = vXFN.getExtendedProperty(rel);
136         if (peopleProp == null) {
137             return false;
138         }
139         out.writeTriple(person1, peopleProp, person2);
140         out.writeTriple(uri1, hyperlinkProp, uri2);
141         return true;
142     }
143 
144 }