Example usage for org.w3c.dom DocumentFragment getChildNodes

List of usage examples for org.w3c.dom DocumentFragment getChildNodes

Introduction

In this page you can find the example usage for org.w3c.dom DocumentFragment getChildNodes.

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:Main.java

public static String getStringRepresentation(DocumentFragment df) {
    StringBuilder sb = new StringBuilder();
    NodeList nl = df.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        Node n = nl.item(i);//from  w w  w. j a v  a2  s .co  m
        sb.append(nodeToString(n));
    }
    return sb.toString();
}

From source file:Main.java

public static NodeList selectNodeList(Node context, Transformer t) throws Exception {
    DocumentFragment df = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
            .createDocumentFragment();//ww  w.  j  a  va 2 s . c om

    Result result = new DOMResult(df);

    t.transform(new DOMSource(context), result);

    return df.getChildNodes();
}

From source file:Main.java

public static NodeList selectNodeList(final Node context, Transformer t) throws Exception {
    // Create result document with top element "result"
    DocumentFragment df = DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument()
            .createDocumentFragment();//  www.j  a  v a 2 s  .  c  om

    Result result = new DOMResult(df);

    // Transform result into DocumentFragment
    t.transform(new DOMSource(context), result);

    // Return list of nodes in DocumentFragment
    return df.getChildNodes();
}

From source file:com.gargoylesoftware.htmlunit.html.impl.SimpleRange.java

private static void insertNodeOrDocFragment(final Node parent, final Node newNode, final Node refNode) {
    if (newNode instanceof DocumentFragment) {
        final DocumentFragment fragment = (DocumentFragment) newNode;

        final NodeList childNodes = fragment.getChildNodes();
        while (childNodes.getLength() > 0) {
            final Node item = childNodes.item(0);
            parent.insertBefore(item, refNode);
        }// w  w w . j  a v a 2s  .co m
    } else {
        parent.insertBefore(newNode, refNode);
    }
}

From source file:com.legstar.jaxb.gen.CobolJAXBCustomizer.java

/**
 * Inject a schema bindings element in the parent annotation node.
 * <p/>/*from  w w w .j  a va 2  s .c  o m*/
 * If the element is already present we update its attributes.
 * 
 * @param markupParent the parent annotation node (its a document fragment)
 * @param jaxbNamespace the JAXB namespace
 * @param jaxbNamespacePrefix the JAXB namespace prefix
 */
public void injectJaxbSchemaBindingsAnnotations(final DocumentFragment markupParent, final String jaxbNamespace,
        final String jaxbNamespacePrefix) {

    Element schemabindingsEl = null;
    NodeList nl = markupParent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i) instanceof Element && nl.item(i).getNamespaceURI().equals(jaxbNamespace)
                && nl.item(i).getLocalName().equals(JAXB_SCHEMABINDINGS)) {
            schemabindingsEl = (Element) nl.item(i);
            break;
        }
    }
    if (schemabindingsEl == null) {
        schemabindingsEl = markupParent.getOwnerDocument().createElementNS(jaxbNamespace,
                jaxbNamespacePrefix + ':' + JAXB_SCHEMABINDINGS);
        markupParent.appendChild(schemabindingsEl);
    }

    if (getJaxbGenModel().needXmlTransform()) {
        injectJaxbXmlTransformAnnotation(schemabindingsEl, jaxbNamespace, jaxbNamespacePrefix);
    }

}

From source file:com.legstar.jaxb.gen.CobolJAXBCustomizer.java

/**
 * Lookup a DOM element in the parent markup. If not found, an new DOM
 * element is created and added to the parent markup.
 * /*from   ww w .j av a2s. c o m*/
 * @param markupParent the parent markup
 * @param namespace the DOM namespace
 * @param namespacePrefix the DOM namespace prefix
 * @param elementLocalName the element local name
 * @return an existing or new DOM element
 */
protected Element getElement(final DocumentFragment markupParent, final String namespace,
        final String namespacePrefix, final String elementLocalName) {

    Element domElement = null;
    NodeList nl = markupParent.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i) instanceof Element && nl.item(i).getNamespaceURI().equals(namespace)
                && nl.item(i).getLocalName().equals(elementLocalName)) {
            domElement = (Element) nl.item(i);
            break;
        }
    }
    if (domElement == null) {
        domElement = markupParent.getOwnerDocument().createElementNS(namespace,
                namespacePrefix + ':' + elementLocalName);
        markupParent.appendChild(domElement);
    }
    return domElement;
}

From source file:com.legstar.jaxb.gen.CobolJAXBCustomizer.java

/**
 * Given an XML Schema, this will inject or replace custom JAXB bindings
 * annotations at the schema level./* w  ww.  j a  va2 s .c  o  m*/
 * <p/>
 * The generated schema holds JAXB annotations needed when, later on, the
 * schema is used to generate JAXB classes. The markup produced looks like
 * this:
 * 
 * <pre>
 * &lt;xsd:appinfo>
 *    &lt;jaxb:globalBindings generateIsSetMethod="true">
 *       &lt;jxb:serializable uid="1"/>
 *    &lt;/jaxb:globalBindings>
 *    &lt;jaxb:schemaBindings>
 *       &lt;jaxb:nameXmlTransform>
 *          &lt;jaxb:typeName prefix="Type" suffix="Type" />
 *          &lt;jaxb:elementName prefix="Type" suffix="Type" />
 *        &lt;/jaxb:nameXmlTransform>
 *    &lt;/jaxb:schemaBindings>
 * &lt;/xsd:appinfo>
 * </pre>
 * 
 * @param schema the XML Schema
 * @param jaxbNamespace the JAXB namespace
 * @param jaxbNamespacePrefix the JAXB namespace prefix
 */
public void injectJaxbSchemaAnnotations(final XmlSchema schema, final String jaxbNamespace,
        final String jaxbNamespacePrefix) {

    XmlSchemaAppInfo appInfo = getXmlSchemaAppInfo(schema);
    DocumentFragment markupParent = getMarkupParent(appInfo);

    injectJaxbGlobalBindingsAnnotations(markupParent, jaxbNamespace, jaxbNamespacePrefix);

    injectJaxbSchemaBindingsAnnotations(markupParent, jaxbNamespace, jaxbNamespacePrefix);

    appInfo.setMarkup(markupParent.getChildNodes());

}

From source file:com.legstar.jaxb.gen.CobolJAXBCustomizer.java

/**
 * Create a typesafeEnumClass markup. This marks the simple Type as eligible
 * to become a separate Enum class./*  w  w  w.  j a  v  a2  s  .  c o  m*/
 * 
 * @param jaxbNamespace the JAXB namespace
 * @param jaxbNamespacePrefix the JAXB namespace prefix
 * @param xsdSimpleType the simple type to annotate
 * @param elementName the name of the element whose simple type we are
 *            dealing with.
 */
protected void injectJaxbTypeSafeEnumClassAnnotation(final String jaxbNamespace,
        final String jaxbNamespacePrefix, final XmlSchemaSimpleType xsdSimpleType, final String elementName) {
    XmlSchemaAppInfo appInfo = getXmlSchemaAppInfo(xsdSimpleType);
    DocumentFragment markupParent = getMarkupParent(appInfo);

    Element typesafeEnumClassEl = getElement(markupParent, jaxbNamespace, jaxbNamespacePrefix,
            JAXB_TYPESAFEENUMCLASS);
    typesafeEnumClassEl.setAttribute(JAXB_TYPESAFEENUMCLASS_NAME, NameUtil.toClassName(elementName));

    appInfo.setMarkup(markupParent.getChildNodes());
}

From source file:com.legstar.jaxb.gen.CobolJAXBCustomizer.java

/**
 * Create a typesafeEnumMember markup. This provides a legal java identifier
 * for an enumeration value. Since these are constant values, we follow the
 * naming convention for static fields (all uppercase).
 * /*from  w  w  w . jav a  2 s.  co  m*/
 * @param jaxbNamespace the JAXB namespace
 * @param jaxbNamespacePrefix the JAXB namespace prefix
 * @param enumerationFacet the enumeration facet to annotate
 * @param value the enumeration value.
 */
protected void injectJaxbTypeSafeEnumMemberAnnotation(final String jaxbNamespace,
        final String jaxbNamespacePrefix, final XmlSchemaEnumerationFacet enumerationFacet,
        final String value) {
    XmlSchemaAppInfo appInfo = getXmlSchemaAppInfo(enumerationFacet);
    DocumentFragment markupParent = getMarkupParent(appInfo);

    Element typesafeEnumMemberEl = getElement(markupParent, jaxbNamespace, jaxbNamespacePrefix,
            JAXB_TYPESAFEENUMMEMBER);
    typesafeEnumMemberEl.setAttribute(JAXB_TYPESAFEENUMMEMBER_NAME,
            DEFAULT_ENUMERATION_MEMBER_PREFIX + NameUtil.toVariableName(value).toUpperCase());

    appInfo.setMarkup(markupParent.getChildNodes());
}

From source file:org.apache.cocoon.xml.dom.DOMUtil.java

/**
 * Create a string from a DOM document fragment.
 * Only the top level text nodes are chained together to build the text.
 *///  w  ww  .j a  v  a2 s  .c  o m
public static String createText(DocumentFragment fragment) {
    StringBuffer value = new StringBuffer();
    if (fragment != null) {
        NodeList childs = fragment.getChildNodes();
        if (childs != null) {
            Node current;

            for (int i = 0; i < childs.getLength(); i++) {
                current = childs.item(i);

                // only text nodes
                if (current.getNodeType() == Node.TEXT_NODE) {
                    if (value.length() > 0)
                        value.append(' ');
                    value.append(current.getNodeValue());
                }
            }
        }
    }
    return value.toString().trim();
}