Example usage for org.w3c.dom Document getChildNodes

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

Introduction

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

Prototype

public NodeList getChildNodes();

Source Link

Document

A NodeList that contains all children of this node.

Usage

From source file:org.apache.openaz.xacml.std.jaxp.JaxpRequest.java

/**
 * Creates a new <code>JaxpRequest</code> by loading it from an XML <code>File</code>.
 *
 * @param fileXmlRequest the <code>File</code> containing the Request XML
 * @return a new <code>JaxpRequest</code> generated by parsing the given XML file
 * @throws javax.xml.parsers.ParserConfigurationException
 * @throws java.io.IOException//from  w  w w  .j  a  v a2  s .  c o  m
 * @throws org.xml.sax.SAXException
 * @throws javax.xml.bind.JAXBException
 * @throws DOMStructureException 
 */
public static JaxpRequest load(File fileXmlRequest)
        throws ParserConfigurationException, IOException, SAXException, JAXBException, DOMStructureException {
    if (fileXmlRequest == null) {
        throw new NullPointerException("Null File");
    }

    Document document = DOMUtil.loadDocument(fileXmlRequest);
    if (document == null) {
        logger.error("No Document returned parsing \"" + fileXmlRequest.getAbsolutePath() + "\"");
        return null;
    }

    NodeList nodeListRoot = document.getChildNodes();
    if (nodeListRoot == null || nodeListRoot.getLength() == 0) {
        logger.warn("No child elements of the XML document");
        return null;
    }
    Node nodeRoot = nodeListRoot.item(0);
    if (nodeRoot == null || nodeRoot.getNodeType() != Node.ELEMENT_NODE) {
        logger.warn("Root of the document is not an ELEMENT");
        return null;
    }

    JAXBContext context = JAXBContext.newInstance(RequestType.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    JAXBElement<RequestType> jaxbElementRequest = unmarshaller.unmarshal(nodeRoot, RequestType.class);
    if (jaxbElementRequest == null || jaxbElementRequest.getValue() == null) {
        logger.error("JAXB unmarshalling did not return a RequestType node");
        return null;
    }
    return JaxpRequest.newInstance(jaxbElementRequest.getValue());

}

From source file:org.apache.openaz.xacml.std.jaxp.JaxpResponse.java

/**
 * Creates a new <code>JaxpResponse</code> by loading it from an XML <code>File</code>.
 *
 * @param fileXmlResponse the <code>File</code> containing the Response XML
 * @return a new <code>JaxpResponse</code> generated by parsing the given XML file
 * @throws javax.xml.parsers.ParserConfigurationException
 * @throws java.io.IOException/*from w w  w.j ava 2  s . c o  m*/
 * @throws org.xml.sax.SAXException
 * @throws javax.xml.bind.JAXBException
 * @throws DOMStructureException 
 */
public static JaxpResponse load(File fileXmlResponse)
        throws ParserConfigurationException, IOException, SAXException, JAXBException, DOMStructureException {
    if (fileXmlResponse == null) {
        throw new NullPointerException("Null File");
    }

    Document document = DOMUtil.loadDocument(fileXmlResponse);
    if (document == null) {
        logger.error("No Document returned parsing \"" + fileXmlResponse.getAbsolutePath() + "\"");
        return null;
    }

    NodeList nodeListRoot = document.getChildNodes();
    if (nodeListRoot == null || nodeListRoot.getLength() == 0) {
        logger.warn("No child elements of the XML document");
        return null;
    }
    Node nodeRoot = nodeListRoot.item(0);
    if (nodeRoot == null || nodeRoot.getNodeType() != Node.ELEMENT_NODE) {
        logger.warn("Root of the document is not an ELEMENT");
        return null;
    }

    JAXBContext context = JAXBContext.newInstance(ResponseType.class);
    Unmarshaller unmarshaller = context.createUnmarshaller();
    JAXBElement<ResponseType> jaxbElementResponse = unmarshaller.unmarshal(nodeRoot, ResponseType.class);
    if (jaxbElementResponse == null || jaxbElementResponse.getValue() == null) {
        logger.error("JAXB unmarshalling did not return a ResponseType node");
        return null;
    }
    return JaxpResponse.newInstance(jaxbElementResponse.getValue());

}

From source file:org.apache.openaz.xacml.util.XACMLPolicyScanner.java

/**
 * readPolicy - does the work to read in policy data from a file.
 *
 * @param policy - The path to the policy file.
 * @return - The policy data object. This *should* be either a PolicySet or a Policy.
 *///from  ww  w  . j  a  va 2 s .  c  o  m
public static Object readPolicy(InputStream is) {
    try {
        //
        // Parse the policy file
        //
        DocumentBuilder db = DOMUtil.getDocumentBuilder();
        Document doc = db.parse(is);
        //
        // Because there is no root defined in xacml,
        // find the first element
        //
        NodeList nodes = doc.getChildNodes();
        Node node = nodes.item(0);
        Element e = null;
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            e = (Element) node;
            //
            // Is it a 3.0 policy?
            //
            if (e.getNamespaceURI().equals("urn:oasis:names:tc:xacml:3.0:core:schema:wd-17")) {
                //
                // A policyset or policy could be the root
                //
                if (e.getNodeName().endsWith("Policy")) {
                    //
                    // Now we can create the context for the policy set
                    // and unmarshall the policy into a class.
                    //
                    JAXBContext context = JAXBContext.newInstance(PolicyType.class);
                    Unmarshaller um = context.createUnmarshaller();
                    JAXBElement<PolicyType> root = um.unmarshal(e, PolicyType.class);
                    //
                    // Here is our policy set class
                    //
                    return root.getValue();
                } else if (e.getNodeName().endsWith("PolicySet")) {
                    //
                    // Now we can create the context for the policy set
                    // and unmarshall the policy into a class.
                    //
                    JAXBContext context = JAXBContext.newInstance(PolicySetType.class);
                    Unmarshaller um = context.createUnmarshaller();
                    JAXBElement<PolicySetType> root = um.unmarshal(e, PolicySetType.class);
                    //
                    // Here is our policy set class
                    //
                    return root.getValue();
                } else {
                    if (logger.isDebugEnabled()) {
                        logger.debug("Not supported yet: " + e.getNodeName());
                    }
                }
            } else {
                logger.warn("unsupported namespace: " + e.getNamespaceURI());
            }
        } else {
            if (logger.isDebugEnabled()) {
                logger.debug("No root element contained in policy " + " Name: " + node.getNodeName() + " type: "
                        + node.getNodeType() + " Value: " + node.getNodeValue());
            }
        }
    } catch (Exception e) {
        logger.error(e.getMessage());
    }
    return null;
}

From source file:org.apache.shindig.gadgets.rewrite.MutableContentTest.java

@Test
public void modifyContentReflectedInTreeAndBytes() throws Exception {
    assertEquals(0, mhc.getNumChanges());
    mhc.setContent("NEW CONTENT");
    assertEquals(1, mhc.getNumChanges());
    assertEquals("NEW CONTENT", new String(IOUtils.toByteArray(mhc.getContentBytes()), "UTF8"));
    Document document = mhc.getDocument();
    assertEquals(1, document.getChildNodes().getLength());
    assertEquals("NEW CONTENT", document.getChildNodes().item(0).getTextContent());
    mhc.documentChanged();/*from   w w w. j  ava 2s . co m*/
    assertEquals(2, mhc.getNumChanges());
}

From source file:org.apache.shindig.gadgets.rewrite.MutableContentTest.java

@Test
public void modifyContentReflectedInTreeUtf8() throws Exception {
    String theContent = "N\uFFFDW C\uFFFDNT\uFFFDNT";

    assertEquals(0, mhc.getNumChanges());
    mhc.setContent(theContent);/*  www . ja  v a 2  s . c  o m*/
    assertEquals(1, mhc.getNumChanges());
    assertEquals(theContent, new String(IOUtils.toByteArray(mhc.getContentBytes()), "UTF8"));
    Document document = mhc.getDocument();
    assertEquals(1, document.getChildNodes().getLength());
    assertEquals(theContent, document.getChildNodes().item(0).getTextContent());
    mhc.documentChanged();
    assertEquals(2, mhc.getNumChanges());
}

From source file:org.apache.shindig.gadgets.rewrite.MutableContentTest.java

@Test
public void modifyBytesReflectedInContentAndTree() throws Exception {
    assertEquals(0, mhc.getNumChanges());
    mhc.setContentBytes("NEW CONTENT".getBytes("UTF8"), Charsets.UTF_8);
    assertEquals(1, mhc.getNumChanges());
    Document document = mhc.getDocument();
    assertEquals(1, document.getChildNodes().getLength());
    assertEquals("NEW CONTENT", document.getChildNodes().item(0).getTextContent());
    assertEquals("NEW CONTENT", mhc.getContent());
    assertEquals(1, mhc.getNumChanges());
    InputStream is = mhc.getContentBytes();
    assertEquals("NEW CONTENT", new String(IOUtils.toByteArray(is), "UTF8"));
    assertEquals(1, mhc.getNumChanges());
}

From source file:org.apache.syncope.client.console.commons.XMLRolesReader.java

private void init() {
    authMap = new HashMap<Pair<String, String>, String>();

    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    dbf.setNamespaceAware(true);/*ww  w.  ja va2  s .co m*/
    try {
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(getClass().getResource("/" + authorizations).openStream());
        doc.getDocumentElement().normalize();

        Node authNode = null;
        NodeList root = doc.getChildNodes();
        for (int i = 0; i < root.getLength() && authNode == null; i++) {
            if ("auth".equals(root.item(i).getNodeName())) {
                authNode = root.item(i);
            }
        }
        if (authNode == null) {
            throw new IllegalArgumentException("Could not find root <auth> node");
        }

        NodeList pages = authNode.getChildNodes();
        for (int i = 0; i < pages.getLength(); i++) {
            if ("page".equals(pages.item(i).getNodeName())) {
                String page = pages.item(i).getAttributes().getNamedItem("id").getTextContent();

                NodeList actions = pages.item(i).getChildNodes();
                for (int j = 0; j < actions.getLength(); j++) {
                    if ("action".equals(actions.item(j).getNodeName())) {
                        String action = actions.item(j).getAttributes().getNamedItem("id").getTextContent();

                        NodeList entitlements = actions.item(j).getChildNodes();
                        for (int k = 0; k < entitlements.getLength(); k++) {
                            if ("entitlement".equals(entitlements.item(k).getNodeName())) {
                                String entitlement = entitlements.item(k).getTextContent();
                                authMap.put(new ImmutablePair<String, String>(page, action), entitlement);
                            }
                        }
                    }
                }
            }
        }
    } catch (Exception e) {
        LOG.error("While initializing parsing of {}", authorizations, e);
    }
}

From source file:org.castafiore.designer.marshalling.DesignableDTO.java

private static DesignableDTO buildContainer_(InputStream xml)
        throws ParserConfigurationException, SAXException, IOException {
    DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
    DocumentBuilder db = dbf.newDocumentBuilder();
    Document doc = db.parse(xml);
    doc.getDocumentElement().normalize();
    NodeList nodeList = doc.getChildNodes();

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node n = nodeList.item(i);

        String name = n.getNodeName();
        if (name.equals("casta:designable")) {
            DesignableDTO dto = getDtoFromNode(n);
            //System.out.println(dto);
            return dto;
        }// ww w .  j  a v  a 2  s .  c  o m
    }

    return null;
}

From source file:org.dita.dost.reader.ChunkMapReader.java

/**
 * Read processing metadata from processing instructions.
 *///from ww w  .  j  av  a 2 s  .com
private void readProcessingInstructions(final Document doc) {
    final NodeList docNodes = doc.getChildNodes();
    for (int i = 0; i < docNodes.getLength(); i++) {
        final Node node = docNodes.item(i);
        if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) {
            final ProcessingInstruction pi = (ProcessingInstruction) node;
            if (pi.getNodeName().equals(PI_WORKDIR_TARGET)) {
                workdir = pi;
            } else if (pi.getNodeName().equals(PI_WORKDIR_TARGET_URI)) {
                workdirUrl = pi;
            } else if (pi.getNodeName().equals(PI_PATH2PROJ_TARGET)) {
                path2proj = pi;
            } else if (pi.getNodeName().equals(PI_PATH2PROJ_TARGET_URI)) {
                path2projUrl = pi;
            }
        }
    }
}

From source file:org.eclipse.wb.internal.layout.group.model.GroupLayoutClipboardCommand.java

@Override
public final void execute(JavaInfo javaInfo) throws Exception {
    try {/*from w  w w  .j  av a  2 s. c om*/
        // prepare
        GroupLayoutSupport _this = getLayoutSupport(javaInfo);
        // map xml ids back to newly created component ids
        Map<String, String> componentsMap = Maps.newHashMap();
        int i = 0;
        for (AbstractComponentInfo component : _this.getLayoutChildren()) {
            componentsMap.put("" + i++, ObjectInfoUtils.getId(component));
        }
        // parse xml
        Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
                .parse(IOUtils.toInputStream(m_layoutXml));
        NodeList nodes = document.getChildNodes().item(0).getChildNodes();
        // load
        _this.getLayoutModel().loadContainerLayout(ObjectInfoUtils.getId(_this.getLayoutContainer()), nodes,
                componentsMap);
        // save
        _this.saveLayout();
    } catch (Throwable e) {
        DesignerPlugin.log(e);
    }
}