use of org.w3c.dom.Text in project groovy by apache.
the class DOMCategory method appendNode.
public static Element appendNode(Element self, Object name, String value) {
Document doc = self.getOwnerDocument();
Element newChild;
if (name instanceof QName) {
QName qn = (QName) name;
newChild = doc.createElementNS(qn.getNamespaceURI(), qn.getQualifiedName());
} else {
newChild = doc.createElement(name.toString());
}
if (value != null) {
Text text = doc.createTextNode(value);
newChild.appendChild(text);
}
self.appendChild(newChild);
return newChild;
}
use of org.w3c.dom.Text in project hadoop by apache.
the class ConfigurationUtils method parseDocument.
// Canibalized from FileSystemAccess <code>Configuration.loadResource()</code>.
private static void parseDocument(Configuration conf, Document doc) throws IOException {
try {
Element root = doc.getDocumentElement();
if (!"configuration".equals(root.getTagName())) {
throw new IOException("bad conf file: top-level element not <configuration>");
}
NodeList props = root.getChildNodes();
for (int i = 0; i < props.getLength(); i++) {
Node propNode = props.item(i);
if (!(propNode instanceof Element)) {
continue;
}
Element prop = (Element) propNode;
if (!"property".equals(prop.getTagName())) {
throw new IOException("bad conf file: element not <property>");
}
NodeList fields = prop.getChildNodes();
String attr = null;
String value = null;
for (int j = 0; j < fields.getLength(); j++) {
Node fieldNode = fields.item(j);
if (!(fieldNode instanceof Element)) {
continue;
}
Element field = (Element) fieldNode;
if ("name".equals(field.getTagName()) && field.hasChildNodes()) {
attr = ((Text) field.getFirstChild()).getData().trim();
}
if ("value".equals(field.getTagName()) && field.hasChildNodes()) {
value = ((Text) field.getFirstChild()).getData();
}
}
if (attr != null && value != null) {
conf.set(attr, value);
}
}
} catch (DOMException e) {
throw new IOException(e);
}
}
use of org.w3c.dom.Text in project hadoop by apache.
the class JobConfigurationParser method parse.
/**
* Parse the job configuration file (as an input stream) and return a
* {@link Properties} collection. The input stream will not be closed after
* return from the call.
*
* @param input
* The input data.
* @return A {@link Properties} collection extracted from the job
* configuration xml.
* @throws IOException
*/
static Properties parse(InputStream input) throws IOException {
Properties result = new Properties();
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(input);
Element root = doc.getDocumentElement();
if (!"configuration".equals(root.getTagName())) {
System.out.print("root is not a configuration node");
return null;
}
NodeList props = root.getChildNodes();
for (int i = 0; i < props.getLength(); ++i) {
Node propNode = props.item(i);
if (!(propNode instanceof Element))
continue;
Element prop = (Element) propNode;
if (!"property".equals(prop.getTagName())) {
System.out.print("bad conf file: element not <property>");
}
NodeList fields = prop.getChildNodes();
String attr = null;
String value = null;
@SuppressWarnings("unused") boolean finalParameter = false;
for (int j = 0; j < fields.getLength(); j++) {
Node fieldNode = fields.item(j);
if (!(fieldNode instanceof Element)) {
continue;
}
Element field = (Element) fieldNode;
if ("name".equals(field.getTagName()) && field.hasChildNodes()) {
attr = ((Text) field.getFirstChild()).getData().trim();
}
if ("value".equals(field.getTagName()) && field.hasChildNodes()) {
value = ((Text) field.getFirstChild()).getData();
}
if ("final".equals(field.getTagName()) && field.hasChildNodes()) {
finalParameter = "true".equals(((Text) field.getFirstChild()).getData());
}
}
if (attr != null && value != null) {
result.put(attr, value);
}
}
} catch (ParserConfigurationException e) {
return null;
} catch (SAXException e) {
return null;
}
return result;
}
use of org.w3c.dom.Text in project robovm by robovm.
the class TextImpl method replaceWholeText.
public final Text replaceWholeText(String content) throws DOMException {
// TODO: support entity references. This code should expand and replace
// the child elements of entity references.
// http://code.google.com/p/android/issues/detail?id=6807
Node parent = getParentNode();
Text result = null;
// delete all nodes in the current run of text...
for (TextImpl n = firstTextNodeInCurrentRun(); n != null; ) {
// ...except the current node if we have content for it
if (n == this && content != null && content.length() > 0) {
setData(content);
result = this;
n = n.nextTextNode();
} else {
// because removeChild() detaches siblings
Node toRemove = n;
n = n.nextTextNode();
parent.removeChild(toRemove);
}
}
return result;
}
use of org.w3c.dom.Text in project robovm by robovm.
the class DocumentBuilderImpl method appendText.
/**
* @param token the XML pull parser token type, such as XmlPullParser.CDSECT
* or XmlPullParser.ENTITY_REF.
*/
private void appendText(DocumentImpl document, Node parent, int token, String text) {
// Ignore empty runs.
if (text.isEmpty()) {
return;
}
// Merge with any previous text node if possible.
if (coalescing || token != XmlPullParser.CDSECT) {
Node lastChild = parent.getLastChild();
if (lastChild != null && lastChild.getNodeType() == Node.TEXT_NODE) {
Text textNode = (Text) lastChild;
textNode.appendData(text);
return;
}
}
// Okay, we really do need a new text node
parent.appendChild(token == XmlPullParser.CDSECT ? new CDATASectionImpl(document, text) : new TextImpl(document, text));
}
Aggregations