001: // DAVNode.java
002: // $Id: DAVNode.java,v 1.14 2004/11/23 13:17:05 ylafon Exp $
003: // (c) COPYRIGHT MIT, INRIA and Keio, 2000.
004: // Please first read the full copyright statement in file COPYRIGHT.html
005: package org.w3c.www.webdav.xml;
006:
007: import java.util.Vector;
008:
009: import org.w3c.dom.DOMException;
010: import org.w3c.dom.Document;
011: import org.w3c.dom.Element;
012: import org.w3c.dom.Node;
013:
014: import org.w3c.www.webdav.WEBDAV;
015:
016: /**
017: * @version $Revision: 1.14 $
018: * @author Benoît Mahé (bmahe@w3.org)
019: */
020: public class DAVNode {
021:
022: //
023: // XML Elements
024: //
025: public static final String ACTIVELOCK_NODE = "activelock";
026: public static final String LOCKSCOPE_NODE = "lockscope";
027: public static final String LOCKTYPE_NODE = "locktype";
028: public static final String DEPTH_NODE = "depth";
029: public static final String OWNER_NODE = "owner";
030: public static final String TIMEOUT_NODE = "timeout";
031: public static final String LOCKTOKEN_NODE = "locktoken";
032: public static final String LOCKENTRY_NODE = "lockentry";
033: public static final String LOCKINFO_NODE = "lockinfo";
034: public static final String WRITE_NODE = "write";
035: public static final String EXCLUSIVE_NODE = "exclusive";
036: public static final String SHARED_NODE = "shared";
037:
038: public static final String HREF_NODE = "href";
039:
040: public static final String LINK_NODE = "link";
041: public static final String SRC_NODE = "src";
042: public static final String DST_NODE = "dst";
043:
044: public static final String MULTISTATUS_NODE = "multistatus";
045: public static final String RESPONSE_NODE = "response";
046: public static final String PROPSTAT_NODE = "propstat";
047: public static final String RESPONSEDESC_NODE = "responsedescription";
048: public static final String STATUS_NODE = "status";
049:
050: public static final String PROP_NODE = "prop";
051:
052: public static final String PROPERTYBEHAVIOR_NODE = "propertybehavior";
053: public static final String OMIT_NODE = "omit";
054: public static final String KEEPALIVE_NODE = "keepalive";
055:
056: public static final String PROPERTYUPDATE_NODE = "propertyupdate";
057: public static final String REMOVE_NODE = "remove";
058: public static final String SET_NODE = "set";
059:
060: public static final String PROPFIND_NODE = "propfind";
061: public static final String ALLPROP_NODE = "allprop";
062: public static final String PROPNAME_NODE = "propname";
063:
064: public static final String COLLECTION_NODE = "collection";
065:
066: //
067: // Property Elements
068: //
069: public static final String CREATIONDATE_NODE = "creationdate";
070: public static final String DISPLAYNAME_NODE = "displayname";
071: public static final String GETCONTENTLANGUAGE_NODE = "getcontentlanguage";
072: public static final String GETCONTENTLENGTH_NODE = "getcontentlength";
073: public static final String GETCONTENTTYPE_NODE = "getcontenttype";
074: public static final String GETETAG_NODE = "getetag";
075: public static final String GETLASTMODIFIED_NODE = "getlastmodified";
076: public static final String LOCKDISCOVERY_NODE = "lockdiscovery";
077: public static final String RESOURCETYPE_NODE = "resourcetype";
078: public static final String SOURCE_NODE = "source";
079: public static final String SUPPORTEDLOCK_NODE = "supportedlock";
080: //
081: // Non std properties, per
082: // http://greenbytes.de/tech/webdav/draft-hopmann-collection-props-00.txt
083: public static final String ISCOLLECTION_NODE = "iscollection";
084: public static final String ISSHARED_NODE = "isshared";
085: public static final String ISHIDDEN_NODE = "ishidden";
086: public static final String ISFOLDER_NODE = "isfolder";
087:
088: //
089: // Our wrapped XML Element
090: //
091: protected Element element = null;
092:
093: //
094: // General methods
095: //
096:
097: /**
098: * Get the first node of the parent matchind the given tagname and
099: * namespace.
100: * @param parent the parent node
101: * @param tagname the tagname to find
102: * @param ns the namespace
103: * @return a Node instance of null
104: */
105: public static Node getNodeNS(Node parent, String tagname, String ns) {
106: Node current = parent.getFirstChild();
107: while (current != null) {
108: if ((current.getNodeType() == current.ELEMENT_NODE)
109: && (current.getLocalName().equals(tagname))) {
110: if (ns != null) {
111: String cns = current.getNamespaceURI();
112: if ((cns != null) && (cns.equals(ns))) {
113: return current;
114: }
115: } else {
116: return current;
117: }
118: }
119: current = current.getNextSibling();
120: }
121: return null;
122: }
123:
124: /**
125: * Add the given node to the children list of the parent node and
126: * Add the Namespace definition if needed (work arround of xmlserializer
127: * bug)
128: * @param parent the parent node
129: * @param child the new child
130: */
131: public static void addNodeNS(Node parent, Node child) {
132: Document doc = parent.getOwnerDocument();
133: String ns = child.getNamespaceURI();
134: String prefix = child.getPrefix();
135: if (ns != null) {
136: if (prefix != null) {
137: Element rootel = doc.getDocumentElement();
138: String arg = "xmlns:" + prefix;
139: if (rootel.getAttribute(arg).equals("")) {
140: rootel.setAttribute(arg, ns);
141: }
142: } else if (ns.equals(WEBDAV.NAMESPACE_URI)) {
143: setPrefix(child, WEBDAV.NAMESPACE_PREFIX, ns);
144: }
145: }
146: parent.appendChild(child);
147: }
148:
149: public static void exportChildren(Document newdoc, Node newparent,
150: Node node, boolean deep) throws DOMException {
151: Node current = node.getFirstChild();
152: while (current != null) {
153: if (current.getNodeType() == current.ELEMENT_NODE) {
154: Node newnode = newdoc.importNode(current, deep);
155: addNodeNS(newparent, newnode);
156: }
157: current = current.getNextSibling();
158: }
159: }
160:
161: public static Node importNode(Document newdoc, Element parent,
162: Node node, boolean deep) {
163: Element rootel = newdoc.getDocumentElement();
164: Node newnode = newdoc.importNode(node, deep);
165: String ns = newnode.getNamespaceURI();
166: if (ns != null) {
167: // check for declaration
168: String prefix = newnode.getPrefix();
169: if (prefix != null) {
170: String arg = "xmlns:" + prefix;
171: String ons = rootel.getAttribute(arg);
172: if (ons.equals("")) {
173: rootel.setAttribute(arg, ns);
174: } else if (!ons.equals(ns)) {
175: // change prefix
176: prefix = "D" + prefix;
177: arg = "xmlns:" + prefix;
178: while ((ons = rootel.getAttribute(arg)) != null) {
179: prefix = "D" + prefix;
180: arg = "xmlns:" + prefix;
181: }
182: rootel.setAttribute(arg, ns);
183: setPrefix(newnode, prefix, ns);
184: }
185: } else if (ns.equals(WEBDAV.NAMESPACE_URI)) {
186: // add DAV prefix
187: setPrefix(newnode, WEBDAV.NAMESPACE_PREFIX, ns);
188: }
189: }
190: parent.appendChild(newnode);
191: return newnode;
192: }
193:
194: private static void setPrefix(Node node, String prefix, String ns) {
195: String nns = node.getNamespaceURI();
196: if ((nns != null) && (nns.equals(ns))) {
197: node.setPrefix(prefix);
198: }
199: Node current = node.getFirstChild();
200: while (current != null) {
201: if (current.getNodeType() == current.ELEMENT_NODE) {
202: setPrefix(current, prefix, ns);
203: }
204: current = current.getNextSibling();
205: }
206: }
207:
208: /**
209: * Get our Node
210: * @return a Node instance
211: */
212: public Node getNode() {
213: return element;
214: }
215:
216: /**
217: * Get all the element that are chidren of the current node.
218: * @return a array of Element
219: */
220: public Element[] getChildrenElements() {
221: Vector v = new Vector();
222: Node current = element.getFirstChild();
223: while (current != null) {
224: if (current.getNodeType() == current.ELEMENT_NODE) {
225: v.addElement((Element) current);
226: }
227: current = current.getNextSibling();
228: }
229: Element elements[] = new Element[v.size()];
230: v.copyInto(elements);
231: return elements;
232: }
233:
234: /**
235: * Get the first node matching the given name and the given namespace
236: * @param tagname the node name
237: * @param ns the namespace
238: * @return a Node instance or null
239: */
240: public Node getNodeNS(String tagname, String ns) {
241: return getNodeNS(element, tagname, ns);
242: }
243:
244: /**
245: * Get the value of the fist child TEXT node.
246: * @return a String
247: */
248: public String getTextValue() {
249: return getTextChildValue(element);
250: }
251:
252: /**
253: * Get the value of the first child text node (if any)
254: * @param node the parent node (can be null)
255: * @return a String instance or null
256: */
257: public String getTextChildValue(Node node) {
258: if (node == null) {
259: return null;
260: }
261: Node current = node.getFirstChild();
262: while (current != null) {
263: if (current.getNodeType() == current.TEXT_NODE) {
264: return current.getNodeValue();
265: }
266: current = current.getNextSibling();
267: }
268: return null;
269: }
270:
271: /**
272: * Get the value of the first child text node (if any)
273: * @param tagname the parent node name
274: * @param ns the namespace to match
275: * @return a String instance or null
276: */
277: public String getTextChildValueNS(String tagname, String ns) {
278: return getTextChildValue(getNodeNS(tagname, ns));
279: }
280:
281: /**
282: * Add this node to our children
283: * @param node the new node
284: */
285: public void addNode(Node node) {
286: element.appendChild(node);
287: }
288:
289: /**
290: * Add the given node to our children list.
291: * Add the Namespace definition if needed (work arround of xmlserializer
292: * bug)
293: * @param parent the parent node
294: * @param child the new child
295: */
296: public void addNodeNS(Node node) {
297: addNodeNS(element, node);
298: }
299:
300: //
301: // DAV specific methods (ns="DAV:")
302: //
303:
304: /**
305: * Get the list of children element that match the given tagname
306: * (and the DAV namespace)
307: * @param node the parent node
308: * @param tagname the tagname to match
309: * @return a Vector instance
310: */
311: public static Vector getDAVElementsByTagName(Node node,
312: String tagname) {
313: Vector v = new Vector();
314: Node current = node.getFirstChild();
315: while (current != null) {
316: if ((current.getNodeType() == current.ELEMENT_NODE)
317: && (current.getLocalName().equals(tagname))
318: && (current.getNamespaceURI() != null)
319: && (current.getNamespaceURI()
320: .equals(WEBDAV.NAMESPACE_URI))) {
321: v.addElement(current);
322: }
323: current = current.getNextSibling();
324: }
325: return v;
326: }
327:
328: /**
329: * Get the first child element that match the given tagname
330: * (and the DAV namespace)
331: * @param node the parent node
332: * @param tagname the tagname to match
333: * @return a Vector instance
334: */
335: public static Node getDAVNode(Node node, String tagname) {
336: Node current = node.getFirstChild();
337: while (current != null) {
338: if ((current.getNodeType() == current.ELEMENT_NODE)
339: && (current.getLocalName().equals(tagname))
340: && (current.getNamespaceURI() != null)
341: && (current.getNamespaceURI()
342: .equals(WEBDAV.NAMESPACE_URI))) {
343: return current;
344: }
345: current = current.getNextSibling();
346: }
347: return null;
348: }
349:
350: /**
351: * Create a new node and add it to the parent children list.
352: * @param parent the parent node
353: * @param name the tagname of the new node
354: * @param textvalue the nodevalue of the TextNode (child of the new node)
355: * @return the newly added Element
356: */
357: public static Element addDAVNode(Node parent, String name,
358: String textvalue) throws DOMException {
359: Document doc = parent.getOwnerDocument();
360: Element el = doc.createElementNS(WEBDAV.NAMESPACE_URI,
361: WEBDAV.NAMESPACE_PREFIX + ":" + name);
362: if (textvalue != null) {
363: el.appendChild(doc.createTextNode(textvalue));
364: }
365: parent.appendChild(el);
366: return el;
367: }
368:
369: /**
370: * Get the text value of all our "DAV:" children matching the given
371: * tagname with a text value available.
372: * @param node the parent node
373: * @param tagname the tagname to search
374: * @return a String array
375: */
376: public static String[] getMultipleTextChildValue(Node node,
377: String tagname) {
378: Vector v = new Vector();
379: Node current = node.getFirstChild();
380: while (current != null) {
381: if ((current.getNodeType() == current.TEXT_NODE)
382: && (current.getLocalName().equals(tagname))
383: && (current.getNamespaceURI() != null)
384: && (current.getNamespaceURI()
385: .equals(WEBDAV.NAMESPACE_URI))) {
386: v.addElement(current.getNodeValue());
387: }
388: current = current.getNextSibling();
389: }
390: String array[] = new String[v.size()];
391: v.copyInto(array);
392: return array;
393: }
394:
395: /**
396: * Just for child, not the all tree.
397: * @param tagname the tagname
398: * @return a Vector of Node.
399: */
400: public Vector getDAVElementsByTagName(String tagname) {
401: return getDAVElementsByTagName(element, tagname);
402: }
403:
404: /**
405: * Get the first node matching the given name
406: * @param tagname the node name
407: * @return a Node instance or null
408: */
409: public Node getDAVNode(String tagname) {
410: return getDAVNode(element, tagname);
411: }
412:
413: /**
414: * Get the value of the first "DAV:" child text node (if any)
415: * @param tagname the parent node name
416: * @return a String instance or null
417: */
418: public String getTextChildValue(String tagname) {
419: return getTextChildValue(getDAVNode(tagname));
420: }
421:
422: /**
423: * Get the text value of all our "DAV:" children matching the given
424: * tagname with a text value available.
425: * @param tagname the tagname to search
426: * @return a String array
427: */
428: public String[] getMultipleTextChildValue(String tagname) {
429: return getMultipleTextChildValue(element, tagname);
430: }
431:
432: // cached value
433: String nodenames[] = null;
434:
435: /**
436: * Get the tagnames of all our DAV children
437: * @return a String array
438: */
439: public String[] getDAVNodeNames() {
440: if (nodenames == null) {
441: Node current = element.getFirstChild();
442: Vector v = new Vector();
443: while (current != null) {
444: if ((current.getNodeType() == current.ELEMENT_NODE)
445: && (current.getNamespaceURI() != null)
446: && (current.getNamespaceURI()
447: .equals(WEBDAV.NAMESPACE_URI))) {
448: v.addElement(current.getLocalName());
449: }
450: current = current.getNextSibling();
451: }
452: nodenames = new String[v.size()];
453: v.copyInto(nodenames);
454: }
455: return nodenames;
456: }
457:
458: //
459: // Creation
460: //
461:
462: /**
463: * Create a new node.
464: * @param name the tagname of the new node
465: * @param textvalue the nodevalue of the TextNode (child of the new node)
466: * @return the newly added Element
467: */
468: public Element addDAVNode(String name, String textvalue)
469: throws DOMException {
470: return addDAVNode(element, name, textvalue);
471: }
472:
473: /**
474: * Add the given node the our children list
475: * @param node the new child
476: */
477: public void addDAVNode(DAVNode node) {
478: element.appendChild(node.getNode());
479: }
480:
481: /**
482: * Add the given nodes the our children list
483: * @param nodes the new children
484: */
485: public void addDAVNodes(DAVNode nodes[]) {
486: if (nodes != null) {
487: int len = nodes.length;
488: for (int i = 0; i < len; i++) {
489: element.appendChild(nodes[i].getNode());
490: }
491: }
492: }
493:
494: public boolean equals(DAVNode node) {
495: return (element == node.getNode());
496: }
497:
498: /**
499: * Constructor
500: */
501: DAVNode(Element element) {
502: this.element = element;
503: }
504:
505: }
|