use of org.w3c.dom.Attr in project qksms by moezbhatti.
the class SmilXmlSerializer method writeElement.
private static void writeElement(Writer writer, Element element) throws IOException {
writer.write('<');
writer.write(element.getTagName());
if (element.hasAttributes()) {
NamedNodeMap attributes = element.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Attr attribute = (Attr) attributes.item(i);
writer.write(" " + attribute.getName());
writer.write("=\"" + attribute.getValue() + "\"");
}
}
// FIXME: Might throw ClassCastException
SMILElement childElement = (SMILElement) element.getFirstChild();
if (childElement != null) {
writer.write('>');
do {
writeElement(writer, childElement);
childElement = (SMILElement) childElement.getNextSibling();
} while (childElement != null);
writer.write("</");
writer.write(element.getTagName());
writer.write('>');
} else {
writer.write("/>");
}
}
use of org.w3c.dom.Attr in project SmartAndroidSource by jaychou2012.
the class XmlDom method serialize.
private void serialize(Element e, XmlSerializer s, int depth, String spaces) throws Exception {
String name = e.getTagName();
writeSpace(s, depth, spaces);
s.startTag("", name);
if (e.hasAttributes()) {
NamedNodeMap nm = e.getAttributes();
for (int i = 0; i < nm.getLength(); i++) {
Attr attr = (Attr) nm.item(i);
s.attribute("", attr.getName(), attr.getValue());
}
}
if (e.hasChildNodes()) {
NodeList nl = e.getChildNodes();
int elements = 0;
for (int i = 0; i < nl.getLength(); i++) {
Node n = nl.item(i);
short type = n.getNodeType();
switch(type) {
case Node.ELEMENT_NODE:
serialize((Element) n, s, depth + 1, spaces);
elements++;
break;
case Node.TEXT_NODE:
s.text(text(n));
break;
case Node.CDATA_SECTION_NODE:
s.cdsect(text(n));
break;
}
}
if (elements > 0) {
writeSpace(s, depth, spaces);
}
}
s.endTag("", name);
}
use of org.w3c.dom.Attr in project generator by mybatis.
the class DomWriter method write.
/**
* Write.
*
* @param node
* the node
* @throws ShellException
* the shell exception
*/
protected void write(Element node) throws ShellException {
printWriter.print('<');
printWriter.print(node.getNodeName());
Attr[] attrs = sortAttributes(node.getAttributes());
for (Attr attr : attrs) {
printWriter.print(' ');
printWriter.print(attr.getNodeName());
//$NON-NLS-1$
printWriter.print("=\"");
normalizeAndPrint(attr.getNodeValue(), true);
printWriter.print('"');
}
if (node.getChildNodes().getLength() == 0) {
//$NON-NLS-1$
printWriter.print(" />");
printWriter.flush();
} else {
printWriter.print('>');
printWriter.flush();
Node child = node.getFirstChild();
while (child != null) {
writeAnyNode(child);
child = child.getNextSibling();
}
//$NON-NLS-1$
printWriter.print("</");
printWriter.print(node.getNodeName());
printWriter.print('>');
printWriter.flush();
}
}
use of org.w3c.dom.Attr in project generator by mybatis.
the class DomWriter method sortAttributes.
/**
* Returns a sorted list of attributes.
*
* @param attrs
* the attrs
* @return the attr[]
*/
protected Attr[] sortAttributes(NamedNodeMap attrs) {
int len = (attrs != null) ? attrs.getLength() : 0;
Attr[] array = new Attr[len];
for (int i = 0; i < len; i++) {
array[i] = (Attr) attrs.item(i);
}
for (int i = 0; i < len - 1; i++) {
String name = array[i].getNodeName();
int index = i;
for (int j = i + 1; j < len; j++) {
String curName = array[j].getNodeName();
if (curName.compareTo(name) < 0) {
name = curName;
index = j;
}
}
if (index != i) {
Attr temp = array[i];
array[i] = array[index];
array[index] = temp;
}
}
return array;
}
use of org.w3c.dom.Attr in project hackpad by dropbox.
the class XmlNode method addNamespaces.
private void addNamespaces(Namespaces rv, Element element) {
if (element == null)
throw new RuntimeException("element must not be null");
String myDefaultNamespace = toUri(element.lookupNamespaceURI(null));
String parentDefaultNamespace = "";
if (element.getParentNode() != null) {
parentDefaultNamespace = toUri(element.getParentNode().lookupNamespaceURI(null));
}
if (!myDefaultNamespace.equals(parentDefaultNamespace) || !(element.getParentNode() instanceof Element)) {
rv.declare(Namespace.create("", myDefaultNamespace));
}
NamedNodeMap attributes = element.getAttributes();
for (int i = 0; i < attributes.getLength(); i++) {
Attr attr = (Attr) attributes.item(i);
if (attr.getPrefix() != null && attr.getPrefix().equals("xmlns")) {
rv.declare(Namespace.create(attr.getLocalName(), attr.getValue()));
}
}
}
Aggregations