Example usage for org.w3c.dom Node getClass

List of usage examples for org.w3c.dom Node getClass

Introduction

In this page you can find the example usage for org.w3c.dom Node getClass.

Prototype

@HotSpotIntrinsicCandidate
public final native Class<?> getClass();

Source Link

Document

Returns the runtime class of this Object .

Usage

From source file:Main.java

/**
 * Try to normalize a document by removing nonsignificant whitespace.
 *
 * @see "#62006"//www.j  ava 2s.  c  o m
 */
private static Document normalize(Document orig) throws IOException {
    DocumentBuilder builder = null;
    DocumentBuilderFactory factory = getFactory(false, false);
    try {
        builder = factory.newDocumentBuilder();
    } catch (ParserConfigurationException e) {
        throw new IOException("Cannot create parser satisfying configuration parameters: " + e, e); //NOI18N
    }

    DocumentType doctype = null;
    NodeList nl = orig.getChildNodes();
    for (int i = 0; i < nl.getLength(); i++) {
        if (nl.item(i) instanceof DocumentType) {
            // We cannot import DocumentType's, so we need to manually copy it.
            doctype = (DocumentType) nl.item(i);
        }
    }
    Document doc;
    if (doctype != null) {
        doc = builder.getDOMImplementation().createDocument(orig.getDocumentElement().getNamespaceURI(),
                orig.getDocumentElement().getTagName(),
                builder.getDOMImplementation().createDocumentType(orig.getDoctype().getName(),
                        orig.getDoctype().getPublicId(), orig.getDoctype().getSystemId()));
        // XXX what about entity decls inside the DOCTYPE?
        doc.removeChild(doc.getDocumentElement());
    } else {
        doc = builder.newDocument();
    }
    for (int i = 0; i < nl.getLength(); i++) {
        Node node = nl.item(i);
        if (!(node instanceof DocumentType)) {
            try {
                doc.appendChild(doc.importNode(node, true));
            } catch (DOMException x) {
                // Thrown in NB-Core-Build #2896 & 2898 inside GeneratedFilesHelper.applyBuildExtensions
                throw new IOException("Could not import or append " + node + " of " + node.getClass(), x);
            }
        }
    }
    doc.normalize();
    nl = doc.getElementsByTagName("*"); // NOI18N
    for (int i = 0; i < nl.getLength(); i++) {
        Element e = (Element) nl.item(i);
        removeXmlBase(e);
        NodeList nl2 = e.getChildNodes();
        for (int j = 0; j < nl2.getLength(); j++) {
            Node n = nl2.item(j);
            if (n instanceof Text && ((Text) n).getNodeValue().trim().length() == 0) {
                e.removeChild(n);
                j--; // since list is dynamic
            }
        }
    }
    return doc;
}

From source file:DOMDump.java

private void dump(Node root, String prefix) {
    if (root instanceof Element) {
        System.out.println(prefix + ((Element) root).getTagName() + " / " + root.getClass().getName());
    } else if (root instanceof CharacterData) {
        String data = ((CharacterData) root).getData().trim();
        if (!data.equals("")) {
            System.out.println(prefix + "CharacterData: " + data);
        }//from w ww.j av a2s.  c  om
    } else {
        System.out.println(prefix + root.getClass().getName());
    }
    NamedNodeMap attrs = root.getAttributes();
    if (attrs != null) {
        int len = attrs.getLength();
        for (int i = 0; i < len; i++) {
            Node attr = attrs.item(i);
            System.out.print(prefix + HALF_INDENT + "attribute " + i + ": " + attr.getNodeName());
            if (attr instanceof Attr) {
                System.out.print(" = " + ((Attr) attr).getValue());
            }
            System.out.println();
        }
    }

    if (root.hasChildNodes()) {
        NodeList children = root.getChildNodes();
        if (children != null) {
            int len = children.getLength();
            for (int i = 0; i < len; i++) {
                dump(children.item(i), prefix + INDENT);
            }
        }
    }
}

From source file:fr.fastconnect.factory.tibco.bw.maven.compile.ArchiveBuilder.java

public void merge(ArchiveBuilder archiveBuilder) {
    ElementNSImpl mergedEnterpriseArchive = this.getEnterpriseArchive();
    ElementNSImpl enterpriseArchive = archiveBuilder.getEnterpriseArchive();

    if (mergedEnterpriseArchive == null) {
        this.repository.getAny().add(enterpriseArchive);
        return;/*  ww  w . ja va 2 s .  c  o m*/
    }

    @SuppressWarnings("unchecked")
    List<ElementNSImpl> elements = (List<ElementNSImpl>) (Object) archiveBuilder.repository.getAny(); // dirty

    for (ElementNSImpl e : elements) {
        if (e.getLocalName().equals("enterpriseArchive")) {
            // merged elements
            ElementNSImpl mergedProcessArchive = getElement(mergedEnterpriseArchive, "processArchive");
            ElementNSImpl mergedProcessProperty = getElement(mergedProcessArchive, "processProperty");

            // look for processArchive elements to merge
            ElementNSImpl nodes = (ElementNSImpl) e.getChildNodes();
            for (int i = 0; i < nodes.getLength(); i++) {
                Node o = nodes.item(i);
                if (!o.getClass().getName().equals("org.apache.xerces.dom.ElementNSImpl"))
                    continue;
                ElementNSImpl n = (ElementNSImpl) o;
                if (n != null && "processArchive".equals(n.getLocalName())) {
                    // current project elements
                    ElementNSImpl processArchive = (ElementNSImpl) n;
                    ElementNSImpl processProperty = (ElementNSImpl) processArchive
                            .getElementsByTagName("processProperty").item(0);

                    String newContent = processProperty.getTextContent();
                    String oldContent = mergedProcessProperty.getTextContent();
                    if (newContent != null && !newContent.isEmpty()) {
                        if (oldContent != null && !oldContent.isEmpty()) {
                            newContent = oldContent + "," + newContent;
                        }
                        mergedProcessProperty.setTextContent(newContent);
                    }
                }
            }
        }
    }
}

From source file:org.brekka.stillingar.spring.config.ConfigurationServiceBeanDefinitionParser.java

protected static List<Element> selectChildElements(Element element, String tagName) {
    NodeList children = element.getElementsByTagNameNS("*", tagName);
    List<Element> elementList = new ArrayList<Element>(children.getLength());
    for (int i = 0; i < children.getLength(); i++) {
        Node item = children.item(i);
        if (item instanceof Element) {
            elementList.add((Element) item);
        } else {// w w w .  j  a v a2 s.c  o m
            throw new IllegalArgumentException(String.format(
                    "The child node '%s' of element '%s' at index %d is not an instance of Element, "
                            + "it is instead '%s'",
                    tagName, element.getTagName(), i, item.getClass().getName()));
        }

    }
    return elementList;
}

From source file:org.brekka.stillingar.spring.config.ConfigurationServiceBeanDefinitionParser.java

/**
 * @param element/*www . j a v a  2  s  .  c  om*/
 * @param string
 * @return
 */
protected static Element selectSingleChildElement(Element element, String tagName, boolean optional) {
    Element singleChild = null;
    NodeList children = element.getElementsByTagNameNS("*", tagName);
    if (children.getLength() == 1) {
        Node node = children.item(0);
        if (node instanceof Element) {
            singleChild = (Element) node;
        } else {
            throw new IllegalArgumentException(
                    String.format(
                            "Expected child node '%s' of element '%s' to be itself an instance of Element, "
                                    + "it is instead '%s'",
                            tagName, element.getTagName(), node.getClass().getName()));
        }
    } else if (children.getLength() == 0) {
        if (!optional) {
            throw new IllegalArgumentException(
                    String.format("Failed to find a single child element named '%s' for parent element '%s'",
                            tagName, element.getTagName()));
        }
    } else {
        throw new IllegalArgumentException(String.format(
                "Expected element '%s' to have a single child element named '%s', found however %d elements",
                element.getTagName(), tagName, children.getLength()));
    }
    return singleChild;
}

From source file:com.amalto.core.history.accessor.AttributeAccessor.java

public String get() {
    Node namedItem = getAttribute();
    if (!(namedItem instanceof Attr)) {
        throw new IllegalStateException(
                "Expected a " + Attr.class.getName() + " instance but got a " + namedItem.getClass().getName()); //$NON-NLS-1$ //$NON-NLS-2$
    }/*from   w  w  w.ja  v a  2s  . com*/

    Attr attribute = (Attr) namedItem;
    return attribute.getValue();
}

From source file:com.amalto.core.history.accessor.AttributeAccessor.java

public void set(String value) {
    Node namedItem = getAttribute();
    if (namedItem == null) {
        throw new IllegalStateException("Attribute '" + attributeName + "' does not exist."); //$NON-NLS-1$ //$NON-NLS-2$
    }/*from w w  w  .  jav  a2 s .  c o m*/
    if (!(namedItem instanceof Attr)) {
        throw new IllegalStateException(
                "Expected a " + Attr.class.getName() + " instance but got a " + namedItem.getClass().getName()); //$NON-NLS-1$ //$NON-NLS-2$
    }

    Attr attribute = (Attr) namedItem;
    attribute.setValue(value);
}

From source file:edu.internet2.middleware.shibboleth.common.config.BaseSpringNamespaceHandler.java

/**
 * Locates the {@link BeanDefinitionParser} from the register implementations using the local name of the supplied
 * {@link org.w3c.dom.Node}. Supports both {@link org.w3c.dom.Element Elements} and {@link org.w3c.dom.Attr Attrs}.
 * /*from w w w .  java2s.  co m*/
 * @param node the node to locate the decorator for
 * 
 * @return the decorator for the given node
 */
protected BeanDefinitionDecorator findDecoratorForNode(Node node) {
    BeanDefinitionDecorator decorator = null;

    if (node instanceof Element) {
        decorator = decorators.get(XMLHelper.getXSIType((Element) node));
        if (decorator == null) {
            decorator = decorators.get(XMLHelper.getNodeQName(node));
        }
    } else if (node instanceof Attr) {
        decorator = attributeDecorators.get(node.getLocalName());
    } else {
        throw new IllegalArgumentException(
                "Cannot decorate based on Nodes of type [" + node.getClass().getName() + "]");
    }

    if (decorator == null) {
        throw new IllegalArgumentException(
                "Cannot locate BeanDefinitionDecorator for " + " [" + node.getLocalName() + "]");
    }

    return decorator;
}

From source file:DOMTreeTest.java

public Component getTreeCellRendererComponent(JTree tree, Object value, boolean selected, boolean expanded,
            boolean leaf, int row, boolean hasFocus) {
        Node node = (Node) value;
        if (node instanceof Element)
            return elementPanel((Element) node);

        super.getTreeCellRendererComponent(tree, value, selected, expanded, leaf, row, hasFocus);
        if (node instanceof CharacterData)
            setText(characterString((CharacterData) node));
        else//  ww  w  . ja v a2  s.c o m
            setText(node.getClass() + ": " + node.toString());
        return this;
    }

From source file:com.espertech.esper.event.EventAdapterServiceImpl.java

public EventBean adapterForDOM(Node node) {
    Node namedNode;//www.ja v a  2s .c  o m
    if (node instanceof Document) {
        namedNode = ((Document) node).getDocumentElement();
    } else if (node instanceof Element) {
        namedNode = node;
    } else {
        throw new EPException("Unexpected DOM node of type '" + node.getClass()
                + "' encountered, please supply a Document or Element node");
    }

    String rootElementName = namedNode.getLocalName();
    if (rootElementName == null) {
        rootElementName = namedNode.getNodeName();
    }

    EventType eventType = xmldomRootElementNames.get(rootElementName);
    if (eventType == null) {
        throw new EventAdapterException(
                "DOM event root element name '" + rootElementName + "' has not been configured");
    }

    return new XMLEventBean(namedNode, eventType);
}