| Method from groovy.xml.DOMBuilder Detail: |
protected void appendNamespaceAttributes(Element element,
Map attributes) {
for (Map.Entry entry : attributes.entrySet()) {
Object key = entry.getKey();
Object value = entry.getValue();
if (value == null) {
throw new IllegalArgumentException("The value of key: " + key + " cannot be null");
}
if (key instanceof String) {
setStringNS(element, key, value);
} else if (key instanceof QName) {
QName qname = (QName) key;
element.setAttributeNS(qname.getNamespaceURI(), qname.getQualifiedName(), value.toString());
} else {
throw new IllegalArgumentException("The key: " + key + " should be an instanceof of " + QName.class);
}
}
}
|
protected Document createDocument() {
if (documentBuilder == null) {
throw new IllegalArgumentException("No Document or DOMImplementation available so cannot create Document");
} else {
return documentBuilder.newDocument();
}
}
|
protected Object createNode(Object name) {
if (document == null) {
document = createDocument();
}
if (name instanceof QName) {
QName qname = (QName) name;
return document.createElementNS(qname.getNamespaceURI(), qname.getQualifiedName());
} else {
return document.createElement(name.toString());
}
}
|
protected Object createNode(Object name,
Object value) {
Element element = (Element) createNode(name);
element.appendChild(document.createTextNode(value.toString()));
return element;
}
|
protected Object createNode(Object name,
Map attributes) {
Element element = (Element) createNode(name);
for (Iterator iter = attributes.entrySet().iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
String attrName = entry.getKey().toString();
Object value = entry.getValue();
if ("xmlns".equals(attrName)) {
if (value instanceof Map) {
appendNamespaceAttributes(element, (Map) value);
} else if (value instanceof String) {
setStringNS(element, "", value);
} else {
throw new IllegalArgumentException("The value of the xmlns attribute must be a Map of QNames to String URIs");
}
} else if (attrName.startsWith("xmlns:") && value instanceof String) {
setStringNS(element, attrName.substring(6), value);
} else {
String valueText = (value != null) ? value.toString() : "";
element.setAttribute(attrName, valueText);
}
}
return element;
}
|
protected Object createNode(Object name,
Map attributes,
Object value) {
Element element = (Element) createNode(name, attributes);
element.appendChild(document.createTextNode(value.toString()));
return element;
}
|
public static DOMBuilder newInstance() throws ParserConfigurationException {
return newInstance(false, true);
}
|
public static DOMBuilder newInstance(boolean validating,
boolean namespaceAware) throws ParserConfigurationException {
DocumentBuilderFactory factory = FactorySupport.createDocumentBuilderFactory();
factory.setNamespaceAware(namespaceAware);
factory.setValidating(validating);
return new DOMBuilder(factory.newDocumentBuilder());
}
|
public static Document parse(Reader reader) throws IOException, SAXException, ParserConfigurationException {
return parse(reader, false, true);
}
Creates a DocumentBuilder and uses it to parse the XML text read from the given reader.
A non-validating, namespace aware parser is used. |
public static Document parse(Reader reader,
boolean validating,
boolean namespaceAware) throws IOException, SAXException, ParserConfigurationException {
DocumentBuilderFactory factory = FactorySupport.createDocumentBuilderFactory();
factory.setNamespaceAware(namespaceAware);
factory.setValidating(validating);
DocumentBuilder documentBuilder = factory.newDocumentBuilder();
return documentBuilder.parse(new InputSource(reader));
}
Creates a DocumentBuilder and uses it to parse the XML text read from the given reader, allowing
parser validation and namespace awareness to be controlled. |
public Document parseText(String text) throws IOException, SAXException, ParserConfigurationException {
return parse(new StringReader(text));
}
A helper method to parse the given text as XML. |
protected void setParent(Object parent,
Object child) {
Node current = (Node) parent;
Node node = (Node) child;
current.appendChild(node);
}
|