Search in sources :

Example 36 with Attr

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("/>");
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) SMILElement(org.w3c.dom.smil.SMILElement) Attr(org.w3c.dom.Attr)

Example 37 with Attr

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);
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) NodeList(org.w3c.dom.NodeList) Node(org.w3c.dom.Node) Attr(org.w3c.dom.Attr)

Example 38 with Attr

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();
    }
}
Also used : Node(org.w3c.dom.Node) Attr(org.w3c.dom.Attr)

Example 39 with Attr

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;
}
Also used : Messages.getString(org.mybatis.generator.internal.util.messages.Messages.getString) Attr(org.w3c.dom.Attr)

Example 40 with Attr

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()));
        }
    }
}
Also used : NamedNodeMap(org.w3c.dom.NamedNodeMap) Element(org.w3c.dom.Element) Attr(org.w3c.dom.Attr)

Aggregations

Attr (org.w3c.dom.Attr)461 Element (org.w3c.dom.Element)215 NamedNodeMap (org.w3c.dom.NamedNodeMap)194 Node (org.w3c.dom.Node)153 Document (org.w3c.dom.Document)147 NodeList (org.w3c.dom.NodeList)89 ArrayList (java.util.ArrayList)33 DOMException (org.w3c.dom.DOMException)32 QName (javax.xml.namespace.QName)22 HashMap (java.util.HashMap)18 DocumentBuilder (javax.xml.parsers.DocumentBuilder)17 Text (org.w3c.dom.Text)14 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)13 CanonicalizationException (com.sun.org.apache.xml.internal.security.c14n.CanonicalizationException)10 RubyString (org.jruby.RubyString)10 IOException (java.io.IOException)9 SAML2Exception (com.sun.identity.saml2.common.SAML2Exception)8 File (java.io.File)8 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)8 ParseException (java.text.ParseException)6