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