001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.cocoon.components.source.helpers;
018:
019: import org.apache.cocoon.xml.XMLUtils;
020: import org.apache.cocoon.xml.dom.DOMBuilder;
021: import org.apache.cocoon.xml.dom.DOMStreamer;
022:
023: import org.apache.excalibur.xml.sax.XMLizable;
024: import org.w3c.dom.Document;
025: import org.w3c.dom.Element;
026: import org.w3c.dom.Node;
027: import org.w3c.dom.NodeList;
028: import org.xml.sax.ContentHandler;
029: import org.xml.sax.SAXException;
030: import org.xml.sax.helpers.AttributesImpl;
031:
032: /**
033: * The interface for a property of a source
034: *
035: * @author <a href="mailto:stephan@apache.org">Stephan Michels</a>
036: * @author <a href="mailto:holz@fiz-chemie.de">Martin Holz</a>
037: * @version $Id: SourceProperty.java 433543 2006-08-22 06:22:54Z crossley $
038: */
039: public class SourceProperty implements XMLizable {
040:
041: private static final String URI = "http://www.w3.org/2000/xmlns/";
042: private static final String NS_PREFIX = "property";
043: private static final String D_PREFIX = NS_PREFIX + ":";
044:
045: private String namespace;
046: private String name;
047: private Element value;
048:
049: /**
050: * Creates a new property for a source
051: *
052: * @param namespace The namespace of the property
053: * @param name The name of the property
054: */
055: public SourceProperty(String namespace, String name) {
056:
057: this .namespace = namespace;
058: this .name = name;
059:
060: try {
061: DOMBuilder builder = new DOMBuilder();
062: builder.startDocument();
063: builder.startPrefixMapping(NS_PREFIX, namespace);
064: AttributesImpl attrs = new AttributesImpl();
065: attrs.addAttribute(URI, NS_PREFIX, "xmlns:" + NS_PREFIX,
066: "NMTOKEN", namespace);
067: builder.startElement(namespace, name, D_PREFIX + name,
068: attrs);
069: builder.endElement(namespace, name, D_PREFIX + name);
070: builder.endPrefixMapping(NS_PREFIX);
071: Document doc = builder.getDocument();
072: this .value = doc.getDocumentElement();
073: } catch (SAXException se) {
074: // do nothing
075: }
076: }
077:
078: /**
079: * Creates a new property for a source
080: *
081: * @param namespace The namespace of the property
082: * @param name The name of the property
083: * @param value The value of the property
084: */
085: public SourceProperty(String namespace, String name, String value) {
086: this .namespace = namespace;
087: this .name = name;
088: setValue(value);
089: }
090:
091: /**
092: * Creates a new property for a source
093: *
094: * @param property The property in DOM representation
095: */
096: public SourceProperty(Element property) {
097: this .namespace = property.getNamespaceURI();
098: this .name = property.getLocalName();
099: this .value = property;
100: }
101:
102: /**
103: * Sets the namespace for this property
104: *
105: * @param namespace The namespace of the property
106: * @deprecated buggy
107: */
108: public void setNamespace(String namespace) {
109: this .namespace = namespace;
110: }
111:
112: /**
113: * Return the namespace of the property
114: *
115: * @return The namespace of the property
116: */
117: public String getNamespace() {
118: return this .namespace;
119: }
120:
121: /**
122: * Sets the name of the property
123: *
124: * @param name Name of the property
125: * @deprecated buggy
126: */
127: public void setName(String name) {
128: this .name = name;
129: }
130:
131: /**
132: * Return the name of the property
133: *
134: * @return Name of the property
135: */
136: public String getName() {
137: return this .name;
138: }
139:
140: /**
141: * Sets the value of the property
142: *
143: * @param value Value of the property
144: */
145: public void setValue(String value) {
146: try {
147: DOMBuilder builder = new DOMBuilder();
148: builder.startDocument();
149: builder.startPrefixMapping(NS_PREFIX, namespace);
150: AttributesImpl attrs = new AttributesImpl();
151: attrs.addAttribute(URI, NS_PREFIX, "xmlns:" + NS_PREFIX,
152: "NMTOKEN", namespace);
153: builder.startElement(namespace, name, D_PREFIX + name,
154: attrs);
155: builder.characters(value.toCharArray(), 0, value.length());
156: builder.endElement(namespace, name, D_PREFIX + name);
157: builder.endPrefixMapping(NS_PREFIX);
158: builder.endDocument();
159: Document doc = builder.getDocument();
160: this .value = doc.getDocumentElement();
161: } catch (SAXException se) {
162: // do nothing
163: }
164: }
165:
166: /**
167: * Returns the value of the property
168: *
169: * @return Value of the property
170: */
171: public String getValueAsString() {
172: NodeList nodeslist = this .value.getChildNodes();
173: StringBuffer buffer = new StringBuffer();
174: for (int i = 0; i < nodeslist.getLength(); i++) {
175: if ((nodeslist.item(i).getNodeType() == Node.TEXT_NODE)
176: || (nodeslist.item(i).getNodeType() == Node.CDATA_SECTION_NODE)) {
177:
178: buffer.append(nodeslist.item(i).getNodeValue());
179: }
180: }
181:
182: return buffer.toString();
183: }
184:
185: /**
186: * Sets the value of the property
187: *
188: * @param values
189: */
190: public void setValue(NodeList values) {
191: try {
192: DOMBuilder builder = new DOMBuilder();
193: builder.startDocument();
194: builder.startElement(namespace, name, name,
195: XMLUtils.EMPTY_ATTRIBUTES);
196: DOMStreamer stream = new DOMStreamer(builder);
197: for (int i = 0; i < values.getLength(); i++) {
198: stream.stream(values.item(i));
199: }
200: builder.endElement(namespace, name, name);
201: builder.endDocument();
202: Document doc = builder.getDocument();
203: this .value = doc.getDocumentElement();
204: } catch (SAXException se) {
205: // do nothing
206: }
207: }
208:
209: /**
210: * Get the property value as DOM Element.
211: */
212: public Element getValue() {
213: return this .value;
214: }
215:
216: /**
217: * Generates SAX events representing the object's state.<br/>
218: * <b>NOTE</b> : if the implementation can produce lexical events, care should be taken
219: * that <code>handler</code> can actually be a {@link org.apache.cocoon.xml.XMLConsumer} that accepts such
220: * events.
221: *
222: * @param handler
223: */
224: public void toSAX(ContentHandler handler) throws SAXException {
225: DOMStreamer stream = new DOMStreamer(handler);
226: stream.stream(this.value);
227: }
228: }
|