Coverage Report - org.apache.any23.validator.DefaultDOMDocument
 
Classes in this File Line Coverage Branch Coverage Complexity
DefaultDOMDocument
0%
0/24
0%
0/8
2.143
 
 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.validator;
 19  
 
 20  
 import org.apache.any23.extractor.html.DomUtils;
 21  
 import org.w3c.dom.Attr;
 22  
 import org.w3c.dom.Document;
 23  
 import org.w3c.dom.NamedNodeMap;
 24  
 import org.w3c.dom.Node;
 25  
 
 26  
 import java.net.URI;
 27  
 import java.util.List;
 28  
 
 29  
 /**
 30  
  * This class wraps the <i>DOM</i> document.
 31  
  *
 32  
  * @author Michele Mostarda (mostarda@fbk.eu)
 33  
  * @author Davide Palmisano (palmisano@fbk.eu)
 34  
  */
 35  
 public class DefaultDOMDocument implements DOMDocument {
 36  
 
 37  
     private URI documentURI;
 38  
 
 39  
     private Document document;
 40  
 
 41  0
     public DefaultDOMDocument(URI documentURI, Document document) {
 42  0
         if(documentURI == null) {
 43  0
             throw new NullPointerException("documentURI cannot be null.");
 44  
         }
 45  0
         if(document == null) {
 46  0
             throw new NullPointerException("document cannot be null.");
 47  
         }
 48  0
         this.documentURI = documentURI;
 49  0
         this.document = document;
 50  0
     }
 51  
 
 52  
     public URI getDocumentURI() {
 53  0
         return documentURI;
 54  
     }
 55  
 
 56  
     public Document getOriginalDocument() {
 57  0
         return document;
 58  
     }
 59  
 
 60  
     public List<Node> getNodes(String xPath) {
 61  0
         return DomUtils.findAll(document, xPath);
 62  
     }
 63  
 
 64  
     public Node getNode(String xPath) {
 65  0
         List<Node> nodes = DomUtils.findAll(document, xPath);
 66  0
         if(nodes.size() == 0) {
 67  0
             throw new IllegalArgumentException(
 68  
                     String.format("Cannot find node at XPath '%s'", xPath)
 69  
             );
 70  
         }
 71  0
         if(nodes.size() > 1) {
 72  0
             throw new IllegalArgumentException(
 73  
                     String.format("The given XPath '%s' corresponds to more than one node.", xPath)
 74  
             );
 75  
         }
 76  0
         return nodes.get(0);
 77  
     }
 78  
 
 79  
     public void addAttribute(String xPath, String attrName, String attrValue) {
 80  0
         Node node = getNode(xPath);
 81  0
         NamedNodeMap namedNodeMap =  node.getAttributes();
 82  0
         Attr attr = document.createAttribute(attrName);
 83  0
         attr.setNodeValue(attrValue);
 84  0
         namedNodeMap.setNamedItem(attr);
 85  0
     }
 86  
 
 87  
     public List<Node> getNodesWithAttribute(String attrName) {
 88  0
         return DomUtils.findAllByAttributeName(document, attrName);
 89  
     }
 90  
 
 91  
 }