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  
38  /**
39   * Extractor for the <a href="http://microformats.org/wiki/xfn">XFN</a>
40   * 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 =
50              new Any23ValueFactoryWrapper(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(
62              ExtractionParameters extractionParameters,
63              ExtractionContext extractionContext,
64              Document in,
65              ExtractionResult out
66      ) throws IOException, ExtractionException {
67          factoryWrapper.setIssueReport(out);
68          try {
69              document = new HTMLDocument(in);
70              this.out = out;
71  
72              BNode subject = factoryWrapper.createBNode();
73              boolean foundAnyXFN = false;
74              final IRI documentIRI = extractionContext.getDocumentIRI();
75              for (Node link : document.findAll("//A[@rel][@href]")) {
76                  foundAnyXFN |= extractLink(link, subject, documentIRI);
77              }
78              if (!foundAnyXFN) return;
79              out.writeTriple(subject, RDF.TYPE, vFOAF.Person);
80              out.writeTriple(subject, vXFN.mePage, documentIRI);
81          } finally {
82              factoryWrapper.setIssueReport(null);
83          }
84      }
85  
86      private boolean extractLink(Node firstLink, BNode subject, IRI documentIRI)
87      throws ExtractionException {
88          String href = firstLink.getAttributes().getNamedItem("href").getNodeValue();
89          String rel = firstLink.getAttributes().getNamedItem("rel").getNodeValue();
90  
91          String[] rels = rel.split("\\s+");
92          IRI link = document.resolveIRI(href);
93          if (containsRelMe(rels)) {
94              if (containsXFNRelExceptMe(rels)) {
95                  return false;    // "me" cannot be combined with any other XFN values
96              }
97              out.writeTriple(subject, vXFN.mePage, link);
98              out.writeTriple(documentIRI, vXFN.getExtendedProperty("me"), link);
99          } else {
100             BNode person2 = factoryWrapper.createBNode();
101             boolean foundAnyXFNRel = false;
102             for (String aRel : rels) {
103                 foundAnyXFNRel |= extractRel(aRel, subject, documentIRI, person2, link);
104             }
105             if (!foundAnyXFNRel) {
106                 return false;
107             }
108             out.writeTriple(person2, RDF.TYPE, vFOAF.Person);
109             out.writeTriple(person2, vXFN.mePage, link);
110         }
111         return true;
112     }
113 
114     private boolean containsRelMe(String[] rels) {
115         for (String rel : rels) {
116             if ("me".equals(rel.toLowerCase())) {
117                 return true;
118             }
119         }
120         return false;
121     }
122 
123     private boolean containsXFNRelExceptMe(String[] rels) {
124         for (String rel : rels) {
125             if (!"me".equals(rel.toLowerCase()) && vXFN.isXFNLocalName(rel)) {
126                 return true;
127             }
128         }
129         return false;
130     }
131 
132     private boolean extractRel(String rel, BNode person1, IRI uri1, BNode person2, IRI uri2) {
133         IRI peopleProp = vXFN.getPropertyByLocalName(rel);
134         IRI hyperlinkProp = vXFN.getExtendedProperty(rel);
135         if (peopleProp == null) {
136             return false;
137         }
138         out.writeTriple(person1, peopleProp, person2);
139         out.writeTriple(uri1, hyperlinkProp, uri2);
140         return true;
141     }
142 
143 }