1 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 ; 24 import org.w3c.dom.Element ; 25 import org.w3c.dom.Node ; 26 import org.w3c.dom.NodeList ; 27 import org.xml.sax.ContentHandler ; 28 import org.xml.sax.SAXException ; 29 import org.xml.sax.helpers.AttributesImpl ; 30 31 38 public class SourceProperty implements XMLizable { 39 40 private static final String URI = "http://www.w3.org/2000/xmlns/"; 41 private static final String NS_PREFIX = "property"; 42 private static final String D_PREFIX = NS_PREFIX+":"; 43 44 private String namespace; 45 private String name; 46 private Element value; 47 48 54 public SourceProperty(String namespace, String 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 attrs = new AttributesImpl (); 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 doc = builder.getDocument(); 69 this.value = doc.getDocumentElement(); 70 } catch (SAXException se) { 71 } 73 } 74 75 82 public SourceProperty(String namespace, String name, String value) { 83 this.namespace = namespace; 84 this.name = name; 85 setValue(value); 86 } 87 88 93 public SourceProperty(Element property) { 94 this.namespace = property.getNamespaceURI(); 95 this.name = property.getLocalName(); 96 this.value = property; 97 } 98 99 105 public void setNamespace(String namespace) { 106 this.namespace = namespace; 107 } 108 109 114 public String getNamespace() { 115 return this.namespace; 116 } 117 118 124 public void setName(String name) { 125 this.name = name; 126 } 127 128 133 public String getName() { 134 return this.name; 135 } 136 137 142 public void setValue(String value) { 143 try { 144 DOMBuilder builder = new DOMBuilder(); 145 builder.startDocument(); 146 builder.startPrefixMapping(NS_PREFIX, namespace); 147 AttributesImpl attrs = new AttributesImpl (); 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 doc = builder.getDocument(); 155 this.value = doc.getDocumentElement(); 156 } catch (SAXException se) { 157 } 159 } 160 161 166 public String getValueAsString() { 167 NodeList nodeslist = this.value.getChildNodes(); 168 StringBuffer buffer = new StringBuffer (); 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 186 public void setValue(NodeList 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 doc = builder.getDocument(); 198 this.value = doc.getDocumentElement(); 199 } catch (SAXException se) { 200 } 202 } 203 204 207 public Element getValue() { 208 return this.value; 209 } 210 211 219 public void toSAX(ContentHandler handler) throws SAXException { 220 DOMStreamer stream = new DOMStreamer(handler); 221 stream.stream(this.value); 222 } 223 } 224 | Popular Tags |