KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > source > helpers > SourceProperty


1 /*
2  * Copyright 1999-2005 The Apache Software Foundation.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

16 package org.apache.cocoon.components.source.helpers;
17
18 import org.apache.cocoon.xml.XMLUtils;
19 import org.apache.cocoon.xml.dom.DOMBuilder;
20 import org.apache.cocoon.xml.dom.DOMStreamer;
21
22 import org.apache.excalibur.xml.sax.XMLizable;
23 import org.w3c.dom.Document JavaDoc;
24 import org.w3c.dom.Element JavaDoc;
25 import org.w3c.dom.Node JavaDoc;
26 import org.w3c.dom.NodeList JavaDoc;
27 import org.xml.sax.ContentHandler JavaDoc;
28 import org.xml.sax.SAXException JavaDoc;
29 import org.xml.sax.helpers.AttributesImpl JavaDoc;
30
31 /**
32  * The interface for a property of a source
33  *
34  * @author <a HREF="mailto:stephan@apache.org">Stephan Michels</a>
35  * @author <a HREF="mailto:holz@fiz-chemie.de">Martin Holz</a>
36  * @version $Id: SourceProperty.java 164808 2005-04-26 16:07:03Z vgritsenko $
37  */

38 public class SourceProperty implements XMLizable {
39
40     private static final String JavaDoc URI = "http://www.w3.org/2000/xmlns/";
41     private static final String JavaDoc NS_PREFIX = "property";
42     private static final String JavaDoc D_PREFIX = NS_PREFIX+":";
43
44     private String JavaDoc namespace;
45     private String JavaDoc name;
46     private Element JavaDoc value;
47
48     /**
49      * Creates a new property for a source
50      *
51      * @param namespace The namespace of the property
52      * @param name The name of the property
53      */

54     public SourceProperty(String JavaDoc namespace, String JavaDoc name) {
55
56         this.namespace = namespace;
57         this.name = name;
58
59         try {
60             DOMBuilder builder = new DOMBuilder();
61             builder.startDocument();
62             builder.startPrefixMapping(NS_PREFIX, namespace);
63             AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
64             attrs.addAttribute(URI, NS_PREFIX, "xmlns:"+NS_PREFIX, "NMTOKEN", namespace);
65             builder.startElement(namespace, name, D_PREFIX+name, attrs);
66             builder.endElement(namespace, name, D_PREFIX+name);
67             builder.endPrefixMapping(NS_PREFIX);
68             Document JavaDoc doc = builder.getDocument();
69             this.value = doc.getDocumentElement();
70         } catch (SAXException JavaDoc se) {
71             // do nothing
72
}
73     }
74
75     /**
76      * Creates a new property for a source
77      *
78      * @param namespace The namespace of the property
79      * @param name The name of the property
80      * @param value The value of the property
81      */

82     public SourceProperty(String JavaDoc namespace, String JavaDoc name, String JavaDoc value) {
83         this.namespace = namespace;
84         this.name = name;
85         setValue(value);
86     }
87
88     /**
89      * Creates a new property for a source
90      *
91      * @param property The property in DOM representation
92      */

93     public SourceProperty(Element JavaDoc property) {
94         this.namespace = property.getNamespaceURI();
95         this.name = property.getLocalName();
96         this.value = property;
97     }
98
99     /**
100      * Sets the namespace for this property
101      *
102      * @param namespace The namespace of the property
103      * @deprecated buggy
104      */

105     public void setNamespace(String JavaDoc namespace) {
106         this.namespace = namespace;
107     }
108
109     /**
110      * Return the namespace of the property
111      *
112      * @return The namespace of the property
113      */

114     public String JavaDoc getNamespace() {
115         return this.namespace;
116     }
117
118     /**
119      * Sets the name of the property
120      *
121      * @param name Name of the property
122      * @deprecated buggy
123      */

124     public void setName(String JavaDoc name) {
125         this.name = name;
126     }
127
128     /**
129      * Return the name of the property
130      *
131      * @return Name of the property
132      */

133     public String JavaDoc getName() {
134         return this.name;
135     }
136
137     /**
138      * Sets the value of the property
139      *
140      * @param value Value of the property
141      */

142     public void setValue(String JavaDoc value) {
143         try {
144             DOMBuilder builder = new DOMBuilder();
145             builder.startDocument();
146             builder.startPrefixMapping(NS_PREFIX, namespace);
147             AttributesImpl JavaDoc attrs = new AttributesImpl JavaDoc();
148             attrs.addAttribute(URI, NS_PREFIX, "xmlns:"+NS_PREFIX, "NMTOKEN", namespace);
149             builder.startElement(namespace, name, D_PREFIX+name, attrs);
150             builder.characters(value.toCharArray(), 0, value.length());
151             builder.endElement(namespace, name, D_PREFIX+name);
152             builder.endPrefixMapping(NS_PREFIX);
153             builder.endDocument();
154             Document JavaDoc doc = builder.getDocument();
155             this.value = doc.getDocumentElement();
156         } catch (SAXException JavaDoc se) {
157             // do nothing
158
}
159     }
160
161     /**
162      * Returns the value of the property
163      *
164      * @return Value of the property
165      */

166     public String JavaDoc getValueAsString() {
167         NodeList JavaDoc nodeslist = this.value.getChildNodes();
168         StringBuffer JavaDoc buffer = new StringBuffer JavaDoc();
169         for (int i = 0; i<nodeslist.getLength(); i++) {
170             if ((nodeslist.item(i).getNodeType()==Node.TEXT_NODE) ||
171                 (nodeslist.item(i).getNodeType()==Node.CDATA_SECTION_NODE))
172             {
173
174                 buffer.append(nodeslist.item(i).getNodeValue());
175             }
176         }
177
178         return buffer.toString();
179     }
180
181     /**
182      * Sets the value of the property
183      *
184      * @param values
185      */

186     public void setValue(NodeList JavaDoc values) {
187         try {
188             DOMBuilder builder = new DOMBuilder();
189             builder.startDocument();
190             builder.startElement(namespace, name, name, XMLUtils.EMPTY_ATTRIBUTES);
191             DOMStreamer stream = new DOMStreamer(builder);
192             for (int i = 0; i<values.getLength(); i++) {
193                 stream.stream(values.item(i));
194             }
195             builder.endElement(namespace, name, name);
196             builder.endDocument();
197             Document JavaDoc doc = builder.getDocument();
198             this.value = doc.getDocumentElement();
199         } catch (SAXException JavaDoc se) {
200             // do nothing
201
}
202     }
203
204     /**
205      * Get the property value as DOM Element.
206      */

207     public Element JavaDoc getValue() {
208         return this.value;
209     }
210
211     /**
212      * Generates SAX events representing the object's state.<br/>
213      * <b>NOTE</b> : if the implementation can produce lexical events, care should be taken
214      * that <code>handler</code> can actually be a {@link org.apache.cocoon.xml.XMLConsumer} that accepts such
215      * events.
216      *
217      * @param handler
218      */

219     public void toSAX(ContentHandler JavaDoc handler) throws SAXException JavaDoc {
220         DOMStreamer stream = new DOMStreamer(handler);
221         stream.stream(this.value);
222     }
223 }
224
Popular Tags