KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > webdav > lib > methods > PropertyBodyHelper


1 /*
2  * $Header: /home/cvs/jakarta-slide/webdavclient/clientlib/src/java/org/apache/webdav/lib/methods/PropertyBodyHelper.java,v 1.4 2004/08/02 15:45:48 unico Exp $
3  * $Revision: 1.4 $
4  * $Date: 2004/08/02 15:45:48 $
5  *
6  * ====================================================================
7  *
8  * Copyright 1999-2002 The Apache Software Foundation
9  *
10  * Licensed under the Apache License, Version 2.0 (the "License");
11  * you may not use this file except in compliance with the License.
12  * You may obtain a copy of the License at
13  *
14  * http://www.apache.org/licenses/LICENSE-2.0
15  *
16  * Unless required by applicable law or agreed to in writing, software
17  * distributed under the License is distributed on an "AS IS" BASIS,
18  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
19  * See the License for the specific language governing permissions and
20  * limitations under the License.
21  *
22  */

23
24 package org.apache.webdav.lib.methods;
25
26 import java.util.Collection JavaDoc;
27 import java.util.Iterator JavaDoc;
28 import java.util.Vector JavaDoc;
29 import org.apache.webdav.lib.PropertyName;
30 import org.apache.webdav.lib.util.XMLPrinter;
31
32 /**
33  * This class manages an array of PropertyNames.
34  * It is used to generate a <prop> tag section
35  * in the body of a WebDAV method.
36  *
37  */

38 public class PropertyBodyHelper {
39
40     protected PropertyName[] propertyNames;
41
42     /**
43      * Property names setter.
44      * The enumeration may contain strings with or without a namespace prefix
45      * but the preferred way is to provide PropertyName objects.
46      *
47      * @param propertyNames List of the property names
48      */

49     protected void setPropertyNames(Collection JavaDoc propertyNames) {
50         Vector JavaDoc list = new Vector JavaDoc();
51         Iterator JavaDoc propertyIterator = propertyNames.iterator();
52         while (propertyIterator.hasNext()) {
53             Object JavaDoc item = propertyIterator.next();
54
55             if (item instanceof PropertyName) {
56                 list.add(item);
57
58             } else if (item instanceof String JavaDoc) {
59                 String JavaDoc propertyName = (String JavaDoc) item;
60                 int length = propertyName.length();
61                 boolean found = false;
62                 int i = 1;
63                 while (!found && (i <= length)) {
64                     char chr = propertyName.charAt(length - i);
65                     if (!Character.isUnicodeIdentifierPart(chr)
66                         && chr != '-'
67                         && chr != '_'
68                         && chr != '.') {
69                         found = true;
70                     } else {
71                         i++;
72                     }
73                 }
74                 if ((i == 1) || (i >= length)) {
75                     list.add(new PropertyName("DAV:", propertyName));
76                 } else {
77                     String JavaDoc namespace = propertyName.substring(0, length + 1 - i);
78                     String JavaDoc localName = propertyName.substring(length + 1 - i);
79                     list.add(new PropertyName(namespace, localName));
80                 }
81             } else {
82                 // unknown type
83
// ignore
84
}
85         }
86         this.propertyNames =
87             (PropertyName[]) list.toArray(new PropertyName[list.size()]);
88     }
89
90     /**
91      * Writes the <D:prop> element to a XMLPrinter.
92      * The element contains all properties from the
93      * propertyNames array. Result is:
94      * <D:prop>
95      * <D:displayname/>
96      * <D:creationdate/>
97      * ...
98      * </D:prop>
99      */

100     protected void wirtePropElement(XMLPrinter printer) {
101         if (propertyNames != null) {
102             printer.writeElement("D", "prop", XMLPrinter.OPENING);
103             for (int i = 0; i < propertyNames.length; i++) {
104                 String JavaDoc namespace = propertyNames[i].getNamespaceURI();
105                 String JavaDoc localname = propertyNames[i].getLocalName();
106                 if ("DAV:".equals(namespace)) {
107                     printer.writeElement("D", localname, XMLPrinter.NO_CONTENT);
108                 } else {
109                     printer.writeElement("ZZ", namespace, localname, XMLPrinter.NO_CONTENT);
110                 }
111             }
112             printer.writeElement("D", "prop", XMLPrinter.CLOSING);
113         }
114     }
115 }
116
Popular Tags