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.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 documentIRI;
38  
39      private Document document;
40  
41      public DefaultDOMDocument(URI documentIRI, Document document) {
42          if(documentIRI == null) {
43              throw new NullPointerException("documentIRI cannot be null.");
44          }
45          if(document == null) {
46              throw new NullPointerException("document cannot be null.");
47          }
48          this.documentIRI = documentIRI;
49          this.document = document;
50      }
51  
52      public URI getDocumentIRI() {
53          return documentIRI;
54      }
55  
56      public Document getOriginalDocument() {
57          return document;
58      }
59  
60      public List<Node> getNodes(String xPath) {
61          return DomUtils.findAll(document, xPath);
62      }
63  
64      public Node getNode(String xPath) {
65          List<Node> nodes = DomUtils.findAll(document, xPath);
66          if(nodes.size() == 0) {
67              throw new IllegalArgumentException(
68                      String.format("Cannot find node at XPath '%s'", xPath)
69              );
70          }
71          if(nodes.size() > 1) {
72              throw new IllegalArgumentException(
73                      String.format("The given XPath '%s' corresponds to more than one node.", xPath)
74              );
75          }
76          return nodes.get(0);
77      }
78  
79      public void addAttribute(String xPath, String attrName, String attrValue) {
80          Node node = getNode(xPath);
81          NamedNodeMap namedNodeMap =  node.getAttributes();
82          Attr attr = document.createAttribute(attrName);
83          attr.setNodeValue(attrValue);
84          namedNodeMap.setNamedItem(attr);
85      }
86  
87      public List<Node> getNodesWithAttribute(String attrName) {
88          return DomUtils.findAllByAttributeName(document, attrName);
89      }
90  
91  }