1 17 18 19 20 package org.apache.lenya.xml; 21 22 import javax.xml.parsers.ParserConfigurationException ; 23 24 import org.w3c.dom.Document ; 25 import org.w3c.dom.Element ; 26 import org.w3c.dom.Text ; 27 28 29 35 public class NamespaceHelper { 36 private String namespaceUri; 37 private String prefix; 38 private Document document; 39 40 48 public NamespaceHelper(String namespaceUri, String prefix, Document document) { 49 this.namespaceUri = namespaceUri; 50 this.prefix = prefix; 51 this.document = document; 52 } 53 54 73 public NamespaceHelper(String namespaceUri, String prefix, String localName) 74 throws ParserConfigurationException { 75 this.namespaceUri = namespaceUri; 76 this.prefix = prefix; 77 setDocument(DocumentHelper.createDocument(getNamespaceURI(), getQualifiedName(localName), 78 null)); 79 } 80 81 86 protected void setDocument(Document document) { 87 this.document = document; 88 } 89 90 95 public Document getDocument() { 96 return document; 97 } 98 99 104 public String getNamespaceURI() { 105 return namespaceUri; 106 } 107 108 113 public String getPrefix() { 114 return prefix; 115 } 116 117 124 public String getQualifiedName(String localName) { 125 if (getPrefix().equals("")) { 126 return localName; 127 } else { 128 return getPrefix() + ":" + localName; 129 } 130 } 131 132 144 public Element createElement(String localName) { 145 return getDocument().createElementNS(getNamespaceURI(), getQualifiedName(localName)); 146 } 147 148 162 public Element createElement(String localName, String text) { 163 Element element = createElement(localName); 164 Text textNode = getDocument().createTextNode(text); 165 element.appendChild(textNode); 166 167 return element; 168 } 169 170 178 public Element [] getChildren(Element element) { 179 return DocumentHelper.getChildren(element, getNamespaceURI()); 180 } 181 182 191 public Element [] getChildren(Element element, String localName) { 192 return DocumentHelper.getChildren(element, getNamespaceURI(), localName); 193 } 194 195 204 public Element getFirstChild(Element element, String localName) { 205 return DocumentHelper.getFirstChild(element, getNamespaceURI(), localName); 206 } 207 208 217 public Element [] getNextSiblings(Element element, String localName) { 218 return DocumentHelper.getNextSiblings(element, getNamespaceURI(), localName); 219 } 220 } 221 | Popular Tags |