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 Any23ValueFactoryWrapperAny23ValueFactoryWrapper">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          String href = firstLink.getAttributes().getNamedItem("href").getNodeValue();
85          String rel = firstLink.getAttributes().getNamedItem("rel").getNodeValue();
86  
87          String[] rels = rel.split("\\s+");
88          IRI link = document.resolveIRI(href);
89          if (containsRelMe(rels)) {
90              if (containsXFNRelExceptMe(rels)) {
91                  return false; // "me" cannot be combined with any other XFN values
92              }
93              out.writeTriple(subject, vXFN.mePage, link);
94              out.writeTriple(documentIRI, vXFN.getExtendedProperty("me"), link);
95          } else {
96              BNode person2 = factoryWrapper.createBNode();
97              boolean foundAnyXFNRel = false;
98              for (String aRel : rels) {
99                  foundAnyXFNRel |= extractRel(aRel, subject, documentIRI, person2, link);
100             }
101             if (!foundAnyXFNRel) {
102                 return false;
103             }
104             out.writeTriple(person2, RDF.TYPE, vFOAF.Person);
105             out.writeTriple(person2, vXFN.mePage, link);
106         }
107         return true;
108     }
109 
110     private boolean containsRelMe(String[] rels) {
111         for (String rel : rels) {
112             if ("me".equals(rel.toLowerCase(Locale.ROOT))) {
113                 return true;
114             }
115         }
116         return false;
117     }
118 
119     private boolean containsXFNRelExceptMe(String[] rels) {
120         for (String rel : rels) {
121             if (!"me".equals(rel.toLowerCase(Locale.ROOT)) && vXFN.isXFNLocalName(rel)) {
122                 return true;
123             }
124         }
125         return false;
126     }
127 
128     private boolean extractRel(String rel, BNode person1, IRI uri1, BNode person2, IRI uri2) {
129         IRI peopleProp = vXFN.getPropertyByLocalName(rel);
130         IRI hyperlinkProp = vXFN.getExtendedProperty(rel);
131         if (peopleProp == null) {
132             return false;
133         }
134         out.writeTriple(person1, peopleProp, person2);
135         out.writeTriple(uri1, hyperlinkProp, uri2);
136         return true;
137     }
138 
139 }