001: /*
002: * ChainBuilder ESB
003: * Visual Enterprise Integration
004: *
005: * Copyright (C) 2006 Bostech Corporation
006: *
007: * This program is free software; you can redistribute it and/or modify it
008: * under the terms of the GNU General Public License as published by the
009: * Free Software Foundation; either version 2 of the License, or (at your option)
010: * any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
014: * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
015: * for more details.
016: *
017: * You should have received a copy of the GNU General Public License along with
018: * this program; if not, write to the Free Software Foundation, Inc.,
019: * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020: *
021: *
022: * $Id: Dom.java 4950 2007-01-29 09:21:20Z lzheng $
023: */
024:
025: package com.bostechcorp.cbesb.console.help;
026:
027: import org.w3c.dom.NamedNodeMap;
028: import org.w3c.dom.Node;
029: import org.w3c.dom.NodeList;
030:
031: public class Dom {
032:
033: /**
034: * Search for a child with the given nodeName. If recursive, search in all
035: * the child of firdt level, then if not found, search in the 2nd level of
036: * the first child, ...
037: *
038: * @param parent
039: * parent node
040: * @param nodeName
041: * node name
042: * @param recursive
043: * boolean to know if we got through the xml tree
044: * @return a node
045: */
046: public static Node findChild(Node parent, String nodeName,
047: boolean recursive) {
048: parent.normalize();
049: Node result = null;
050: if (parent != null && nodeName != null) {
051: NodeList nl = parent.getChildNodes();
052: for (int i = 0; i < nl.getLength() && result == null; i++) {
053: if (nodeName.equals(nl.item(i).getNodeName())) {
054: result = nl.item(i);
055: }
056: }
057: // now, search recursively if required
058: if (result == null && recursive) {
059: for (int i = 0; i < nl.getLength() && result == null; i++) {
060: result = findChild(nl.item(i), nodeName, true);
061: }
062: }
063: }
064: return result;
065: }
066:
067: /**
068: * Search for a child with the given nodeName. If recursive, search in all
069: * the child of firdt level, then if not found, search in the 2nd level of
070: * the first child, ...
071: *
072: * @param parent
073: * parent node
074: * @param namespaceURI
075: * The namespaceURI of the node
076: * @param nodeName
077: * node name
078: * @param recursive
079: * boolean to know if we got through the xml tree
080: * @return a node
081: */
082: public static Node findChild(Node parent, String namespaceURI,
083: String nodeName, boolean recursive) {
084: parent.normalize();
085: String prefix = getPrefixForNamespaceURIRecursive(parent,
086: namespaceURI);
087: return (findChild(parent, prefix + nodeName, recursive));
088: }
089:
090: /**
091: * Return the first child of a node, regardless <i>text</i> node
092: *
093: * @param node
094: * @return
095: */
096: public static Node getFirstChild(Node node) {
097: node.normalize();
098: Node result = node.getFirstChild();
099: while (result.getNodeType() == Node.TEXT_NODE) {
100: result = result.getNextSibling();
101: }
102: return result;
103: }
104:
105: /**
106: * Return the next sibling of a node, regardless <i>text</i> node
107: *
108: * @param node
109: * @return
110: */
111: public static Node getNextSibling(Node node) {
112: node.normalize();
113: Node result = node.getNextSibling();
114: while (result.getNodeType() == Node.TEXT_NODE) {
115: result = result.getNextSibling();
116: }
117: return result;
118: }
119:
120: /**
121: * Search a document to see if a namespace is declared in it and if it is
122: * returns this namespace URI
123: * @param node
124: * @param namespaceURI
125: * @param deep
126: * @return
127: */
128: public static String getPrefixForNamespaceURI(Node node,
129: String namespaceURI) {
130: String result = "";
131:
132: // Search in root node attributes
133: NamedNodeMap attributes = node.getAttributes();
134: int i = 0;
135: if (attributes != null) {
136: while (i < attributes.getLength()) {
137: Node attr = attributes.item(i++);
138: if (namespaceURI.equals(attr.getNodeValue())) {
139: String nodeName = attr.getNodeName();
140: if (nodeName.startsWith("xmlns:")) {
141: result = nodeName.replaceFirst("xmlns:", "")
142: + ":";
143: return result;
144: } else if (nodeName.startsWith("xmlns")) {
145: return result;
146: }
147: }
148: }
149: }
150: // Search in child nodes attributes
151: i = 0;
152: NodeList nl = node.getChildNodes();
153: while (i < nl.getLength()) {
154: Node tmpNode = nl.item(i++);
155: String prefix = getPrefixForNamespaceURI(tmpNode,
156: namespaceURI);
157: if (prefix != null) {
158: return prefix;
159: }
160: }
161: return null;
162:
163: }
164:
165: public static String getPrefixForNamespaceURIRecursive(Node node,
166: String namespaceURI) {
167: String result = "";
168: if (namespaceURI != null && !"".equals(namespaceURI)) {
169: while (node.getParentNode() != null) {
170: node = node.getParentNode();
171: }
172: // Search in root node attributes
173: NamedNodeMap attributes = node.getAttributes();
174: int i = 0;
175: if (attributes != null) {
176: while (i < attributes.getLength()) {
177: Node attr = attributes.item(i++);
178: if (namespaceURI.equals(attr.getNodeValue())) {
179: String nodeName = attr.getNodeName();
180: if (nodeName.startsWith("xmlns:")) {
181: result = nodeName
182: .replaceFirst("xmlns:", "")
183: + ":";
184: return result;
185: } else if (nodeName.startsWith("xmlns")) {
186: return result;
187: }
188: }
189: }
190: }
191: // Search in child nodes attributes
192: i = 0;
193: NodeList nl = node.getChildNodes();
194: while (i < nl.getLength()) {
195: Node tmpNode = nl.item(i++);
196: String prefix = getPrefixForNamespaceURI(tmpNode,
197: namespaceURI);
198: if (prefix != null) {
199: return prefix;
200: }
201: }
202: }
203:
204: return result;
205: }
206:
207: }
|