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