View Javadoc

1   /*
2    * XPathTools.java
3    *
4    * Created on 22 décembre 2006, 14:15
5    *
6    */
7   
8   package org.softnetwork.xml.dom.xpath;
9   
10  import java.util.ArrayList;
11  import java.util.List;
12  import org.w3c.dom.Attr;
13  import org.w3c.dom.Document;
14  import org.w3c.dom.Element;
15  import org.w3c.dom.Node;
16  import org.w3c.dom.NodeList;
17  
18  /**
19   *
20   * @author stephane.manciot@ebiznext.com
21   */
22  public class XPathTools implements XPath {
23      
24      /** Creates a new instance of XPathTools */
25      private XPathTools() {
26      }
27      
28      public static void copy(Document source, Document out, String xpathFrom,
29              String xpathTo) throws XPathFormatException {
30          if (source == null)
31              throw new IllegalArgumentException("source document was null");
32          if (out == null)
33              throw new IllegalArgumentException("out document was null");
34          if (xpathFrom == null)
35              throw new IllegalArgumentException("xpath 'from' was null");
36          if (xpathTo == null)
37              throw new IllegalArgumentException("xpath 'to' was null");
38          Node[] nodes = LookupService.selectNodes(source, xpathFrom);
39          if (nodes != null && nodes.length > 0) {
40              Node[] selected = LookupService.selectNodes(out, xpathTo);
41              if (selected == null || selected.length != 1)
42                  return;
43              Node dest = selected[0];
44              final int len = nodes.length;
45              for (int i = 0; i < len; i++) {
46                  dest.appendChild(out.importNode(nodes[i], true));
47              }
48          }
49      }
50      
51      public static List getNamedElements(Element element, String name) {
52          List elements = new ArrayList();
53          boolean ALL = XPATH_ALL.equals(name);
54          NodeList list = element.getChildNodes();
55          int len = list.getLength();
56          for (int i = 0; i < len; i++) {
57              Node n = list.item(i);
58              if (n instanceof Element) {
59                  Element e = (Element) n;
60                  if (ALL || name.equals(e.getNodeName()))
61                      elements.add(e);
62              }
63          }
64          return elements;
65      }
66      
67      public static int getElementPosition(Element element) {
68          int pos = -1;
69          Node parent = element.getParentNode();
70          if (parent != null) {
71              if (parent instanceof Element) {
72                  List elements = getNamedElements(
73                          (Element) parent, element.getNodeName());
74                  if (elements.size() > 1) {
75                      pos = elements.indexOf(element) + 1;
76                  }
77              }
78          }
79          return pos;
80      }
81      
82      public final static String getXpath(Node node) {
83          if (node.getNodeType() == Node.DOCUMENT_NODE)
84              return "";
85          Node parent = node.getParentNode();
86          if (node instanceof Attr)
87              parent = ((Attr) node).getOwnerElement();
88          String xpath = "";
89          xpath = ((parent != null) ? getXpath(parent) : "") + XPATH_SEPARATOR;
90          switch (node.getNodeType()) {
91              case Node.ATTRIBUTE_NODE:
92                  return xpath + XPATH_ATTRIBUTE + node.getNodeName();
93              case Node.ELEMENT_NODE:
94                  if (node instanceof Element) {
95                      int pos = getElementPosition((Element) node);
96                      if (pos >= 0)
97                          return xpath + ((Element) node).getTagName()
98                          + XPATH_COND_BEGIN + pos + XPATH_COND_END;
99                  }
100             default:
101                 return xpath + node.getNodeName();
102         }
103     }
104     
105     public static String getParentXpath(String xpath) {
106         int last = xpath.lastIndexOf(XPATH_SEPARATOR);
107         String parentXpath = xpath.substring(0, (last > 0) ? last : xpath
108                 .length());
109         return parentXpath;
110     }
111     
112     public static String getCanonicalPath(String xpath) {
113         if (xpath == null)
114             return null;
115         int condBegin = -1;
116         while ((condBegin = xpath.indexOf(XPATH_COND_BEGIN)) >= 0) {
117             xpath = xpath.substring(0, condBegin)
118             + xpath.substring(xpath.indexOf(XPATH_COND_END) + 1);
119         }
120         return xpath;
121     }
122     
123     public static String getCanonicalPath(Node node) {
124         String xpath = (node.getNodeType() == Node.ATTRIBUTE_NODE) ? XPATH_ATTRIBUTE
125                 : "" + node.getNodeName();
126         Node current = node;
127         while ((current = current.getParentNode()) != null
128                 && !(current instanceof Document))
129             xpath = current.getNodeName() + XPATH_SEPARATOR + xpath;
130         return XPATH_SEPARATOR + XPATH_SEPARATOR + xpath;
131     }
132     
133     public static String getTagName(String xpath) {
134         int i = -1;
135         return xpath.substring(xpath.lastIndexOf(XPATH_SEPARATOR) + 1,
136                 ((i = xpath.indexOf(XPATH_COND_BEGIN)) >= 0) ? i : xpath
137                 .length());
138     }
139     
140 }