Example usage for org.w3c.dom Node getFirstChild

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

Introduction

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

Prototype

public Node getFirstChild();

Source Link

Document

The first child of this node.

Usage

From source file:com.connexta.arbitro.PolicyReference.java

/**
 * Creates an instance of a <code>PolicyReference</code> object based on a DOM node.
 * /*from  w w w .j a  va 2 s .c om*/
 * @param root the DOM root of a PolicyIdReference or a PolicySetIdReference XML type
 * @param finder the <code>PolicyFinder</code> used to handle the reference
 * @param metaData the meta-data associated with the containing policy
 * @return an instance of PolicyReference
 * @exception ParsingException if the node is invalid
 */
public static PolicyReference getInstance(Node root, PolicyFinder finder, PolicyMetaData metaData)
        throws ParsingException {

    URI reference;
    int policyType;

    // see what type of reference we are
    String name = DOMHelper.getLocalName(root);
    if (name.equals("PolicyIdReference")) {
        policyType = POLICY_REFERENCE;
    } else if (name.equals("PolicySetIdReference")) {
        policyType = POLICYSET_REFERENCE;
    } else {
        throw new ParsingException("Unknown reference type: " + name);
    }

    // next get the reference
    try {
        reference = new URI(root.getFirstChild().getNodeValue());
    } catch (Exception e) {
        throw new ParsingException("Invalid URI in Reference", e);
    }

    // now get any constraints
    NamedNodeMap map = root.getAttributes();

    String versionConstraint = null;
    Node versionNode = map.getNamedItem("Version");
    if (versionNode != null)
        versionConstraint = versionNode.getNodeValue();

    String earlyConstraint = null;
    Node earlyNode = map.getNamedItem("EarliestVersion");
    if (earlyNode != null)
        earlyConstraint = earlyNode.getNodeValue();

    String lateConstraint = null;
    Node lateNode = map.getNamedItem("LatestVersion");
    if (lateNode != null)
        lateConstraint = lateNode.getNodeValue();

    VersionConstraints constraints = new VersionConstraints(versionConstraint, earlyConstraint, lateConstraint);

    // finally, create the reference
    return new PolicyReference(reference, policyType, constraints, finder, metaData);
}

From source file:com.amazon.s3.util.XpathUtils.java

private static String evaluateXPath(Node node, String xPath) {
    int currentSearchIndex = 0;
    while (currentSearchIndex < xPath.length()) {

        int endingIndex = xPath.indexOf("/", currentSearchIndex);

        String noderNameFromXPath = null;
        if (endingIndex == -1) {
            noderNameFromXPath = xPath.substring(currentSearchIndex);
        } else {//  ww w  .j av a2  s .c  o m
            noderNameFromXPath = xPath.substring(currentSearchIndex, endingIndex);
        }

        node = findChildNodeWithName(node, noderNameFromXPath);

        if (endingIndex == -1) {
            break;
        }

        currentSearchIndex = endingIndex + 1;
    }

    if (node != null && node.getFirstChild() != null) {
        return node.getFirstChild().getNodeValue();
    } else if (node != null) {
        return node.getNodeValue();
    } else {
        return null;
    }
}

From source file:com.apress.prospringintegration.webservice.web.TicketIssuerEndpoint.java

@ServiceActivator
public Source handleRequest(DOMSource source) throws Exception {

    NodeList nodeList = source.getNode().getChildNodes();
    String description = "";
    String priority = "";

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node node = nodeList.item(i);
        if (node.getNodeName().equals("priority")) {
            priority = node.getFirstChild().getNodeValue();
        } else if (node.getNodeName().equals("description")) {
            description = node.getFirstChild().getNodeValue();
        }// w w  w  .  j a v a2 s .co  m
    }

    // Transfer properties to an XML document
    String xml = String.format(replyTemplate, description, priority, new Random().nextLong() * 1000,
            new Date());

    return new DomSourceFactory().createSource(xml);
}

From source file:com.pari.nm.modules.jobs.PcbImportJob.java

public static void populateDiscoveredList(Element docRoot, int customerId, int instanceId) throws Exception {
    if (!docRoot.getTagName().equals("DiscoveredDeviceList")) {
        throw new Exception("Invalid format. Expecting: DiscoveredDeviceList, found " + docRoot.getTagName());
    }// ww  w  .  j  a v  a  2s. co  m
    NodeList devList = docRoot.getElementsByTagName("DiscoveredDevice");
    int numCh = devList.getLength();
    for (int i = 0; i < numCh; i++) {
        Element specRoot = (Element) devList.item(i);
        String tagName = specRoot.getTagName();
        if (!tagName.equals("DiscoveredDevice")) {
            throw new Exception("Invalid format. Expecting: DiscoveredDevice, found " + tagName);
        }
        DiscoveredDevice dev = new DiscoveredDevice();
        dev.setCustomer_id(customerId);
        dev.setInstanceId(instanceId);
        NodeList aList = specRoot.getElementsByTagName("IpAddress");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setIpAddress(nn.getNodeValue().trim());
        }
        aList = specRoot.getElementsByTagName("HostName");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setNodeName(nn.getNodeValue().trim());
        }
        aList = specRoot.getElementsByTagName("MACAddress");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setMacAddress(nn.getNodeValue().trim());
        }
        aList = specRoot.getElementsByTagName("Description");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setDescription(nn.getNodeValue().trim());
        }
        aList = specRoot.getElementsByTagName("DeviceFamily");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setDevice_family(nn.getNodeValue().trim());
        }
        aList = specRoot.getElementsByTagName("ProductFamily");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setProduct_family(nn.getNodeValue().trim());
        }
        aList = specRoot.getElementsByTagName("ProductModel");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setProduct_model(nn.getNodeValue().trim());
        }
        aList = specRoot.getElementsByTagName("OSName");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setOsName(nn.getNodeValue().trim());
        }
        aList = specRoot.getElementsByTagName("OSVersion");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setOsVersion(nn.getNodeValue().trim());
        }
        aList = specRoot.getElementsByTagName("VendorName");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setVendor_name(nn.getNodeValue().trim());
        }
        aList = specRoot.getElementsByTagName("IsManaged");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setManaged(nn.getNodeValue().trim().equalsIgnoreCase("yes"));
        }
        aList = specRoot.getElementsByTagName("DiscoveryMethod");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setDiscovery_method(nn.getNodeValue().trim());
        }
        aList = specRoot.getElementsByTagName("DiscoveryCredential");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setDiscovery_credential(nn.getNodeValue().trim());
        }
        aList = specRoot.getElementsByTagName("DiscoveryTime");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setDiscovery_time(nn.getNodeValue().trim());
        }
        aList = specRoot.getElementsByTagName("DiscoveredFrom");
        if (aList.getLength() > 0) {
            Node n = aList.item(0);
            Node nn = n.getFirstChild();
            dev.setDiscovered_from(nn.getNodeValue().trim());
        }
        InventoryDBHelper.insertDiscoveredDevice(dev);
    }
}

From source file:com.l2jfree.gameserver.document.DocumentBase.java

final void parseTemplate(Node n, Object template) {
    n = n.getFirstChild();

    for (; n != null; n = n.getNextSibling()) {
        parseTemplateNode(n, template);/*from w  w w. j a  v a 2  s .c  om*/
    }
}

From source file:Main.java

/**
 * This method is a tree-search to help prevent against wrapping attacks. It checks that no other
 * Element than the given "knownElement" argument has an ID attribute that matches the "value"
 * argument, which is the ID value of "knownElement". If this is the case then "false" is returned.
 *///from  w w  w . j  a v a2 s  .co  m
public static boolean protectAgainstWrappingAttack(Node startNode, Element knownElement, String value) {
    Node startParent = startNode.getParentNode();
    Node processedNode = null;

    String id = value.trim();
    if (id.charAt(0) == '#') {
        id = id.substring(1);
    }

    while (startNode != null) {
        if (startNode.getNodeType() == Node.ELEMENT_NODE) {
            Element se = (Element) startNode;

            NamedNodeMap attributes = se.getAttributes();
            if (attributes != null) {
                for (int i = 0; i < attributes.getLength(); i++) {
                    Attr attr = (Attr) attributes.item(i);
                    if (attr.isId() && id.equals(attr.getValue()) && se != knownElement) {
                        //log.debug("Multiple elements with the same 'Id' attribute value!");
                        return false;
                    }
                }
            }
        }

        processedNode = startNode;
        startNode = startNode.getFirstChild();

        // no child, this node is done.
        if (startNode == null) {
            // close node processing, get sibling
            startNode = processedNode.getNextSibling();
        }

        // no more siblings, get parent, all children
        // of parent are processed.
        while (startNode == null) {
            processedNode = processedNode.getParentNode();
            if (processedNode == startParent) {
                return true;
            }
            // close parent node processing (processed node now)
            startNode = processedNode.getNextSibling();
        }
    }
    return true;
}

From source file:net.d53dev.dslfy.web.service.GifService.java

private void configureGIFFrame(IIOMetadata meta, String delayTime, int imageIndex, String disposalMethod,
        int loopCount) {
    String metaFormat = meta.getNativeMetadataFormatName();

    if (!"javax_imageio_gif_image_1.0".equals(metaFormat)) {
        throw new IllegalArgumentException("Unfamiliar gif metadata format: " + metaFormat);
    }/*from ww w .  j  a  v  a2s.c o  m*/

    Node root = meta.getAsTree(metaFormat);

    Node child = root.getFirstChild();
    while (child != null) {
        if ("GraphicControlExtension".equals(child.getNodeName()))
            break;
        child = child.getNextSibling();
    }

    IIOMetadataNode gce = (IIOMetadataNode) child;
    gce.setAttribute("userDelay", "FALSE");
    gce.setAttribute("delayTime", delayTime);
    gce.setAttribute("disposalMethod", disposalMethod);

    if (imageIndex == 0) {
        IIOMetadataNode aes = new IIOMetadataNode("ApplicationExtensions");
        IIOMetadataNode ae = new IIOMetadataNode("ApplicationExtension");
        ae.setAttribute("applicationID", "NETSCAPE");
        ae.setAttribute("authenticationCode", "2.0");
        byte[] uo = new byte[] { 0x1, (byte) (loopCount & 0xFF), (byte) ((loopCount >> 8) & 0xFF) };
        ae.setUserObject(uo);
        aes.appendChild(ae);
        root.appendChild(aes);
    }

    try {
        meta.setFromTree(metaFormat, root);
    } catch (IIOInvalidTreeException e) {
        throw new Error(e);
    }
}

From source file:org.apache.solr.kelvin.responseanalyzers.LegacyResponseAnalyzer.java

public void decode(Map<String, Object> previousResponses) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode response = mapper.createArrayNode();

    if (!previousResponses.containsKey(XmlResponseAnalyzer.XML_DOM)) {
        previousResponses.put(XmlDoclistExtractorResponseAnalyzer.DOC_LIST, response); //empty
        return;//from  w  w w  .j a  va  2 s. c om
    }

    NodeList nodeList = (NodeList) expr.evaluate((Document) previousResponses.get(XmlResponseAnalyzer.XML_DOM),
            XPathConstants.NODESET);

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node doc = nodeList.item(i);
        ObjectNode oDoc = mapper.createObjectNode();
        Node subel = doc.getFirstChild();
        while (subel != null) {
            String localName = subel.getNodeName();
            if (localName == "field") {
                String fieldName = subel.getAttributes().getNamedItem("name").getNodeValue();
                if (!"infoold".equals(fieldName)) {
                    String value = subel.getTextContent();
                    oDoc.put(fieldName, value);
                }
            } else if (localName == "slug_preferenziale") {
                NodeList slugNodes = subel.getChildNodes();
                oDoc.put("slug_preferenziale", slugNodes.item(slugNodes.getLength() - 1).getAttributes()
                        .getNamedItem("path").getTextContent());
            }

            subel = subel.getNextSibling();
        }
        response.add(oDoc);
    }
    previousResponses.put(XmlDoclistExtractorResponseAnalyzer.DOC_LIST, response);
}

From source file:com.hp.autonomy.searchcomponents.idol.search.fields.FieldsParserImpl.java

private <T> List<T> parseFields(final Element node, final String name, final FieldType fieldType,
        final Class<T> type) {
    final NodeList childNodes = node.getElementsByTagName(name.toUpperCase());
    final int length = childNodes.getLength();
    final List<T> values = new ArrayList<>(length);

    for (int i = 0; i < length; i++) {
        final Node childNode = childNodes.item(i);
        values.add(fieldType.parseValue(type, childNode.getFirstChild().getNodeValue()));
    }//  w  w w .  j a  v  a2 s .c  o  m

    return values;
}

From source file:org.apache.solr.kelvin.responseanalyzers.XmlDoclistExtractorResponseAnalyzer.java

public void decode(Map<String, Object> previousResponses) throws Exception {
    ObjectMapper mapper = new ObjectMapper();
    ArrayNode response = mapper.createArrayNode();

    if (!previousResponses.containsKey(XmlResponseAnalyzer.XML_DOM)) {
        previousResponses.put(DOC_LIST, response); //empty
        return;/* ww  w  . jav a 2  s  .  c om*/
    }

    NodeList nodeList = (NodeList) expr.evaluate((Document) previousResponses.get(XmlResponseAnalyzer.XML_DOM),
            XPathConstants.NODESET);

    for (int i = 0; i < nodeList.getLength(); i++) {
        Node doc = nodeList.item(i);
        ObjectNode oDoc = mapper.createObjectNode();
        Node subel = doc.getFirstChild();
        while (subel != null) {
            if (subel.getNodeType() != Node.TEXT_NODE) {
                String fieldName = subel.getAttributes().getNamedItem("name").getNodeValue();
                String elementName = subel.getNodeName();
                if ("arr".equals(elementName)) {
                    ArrayNode multivaluedField = mapper.createArrayNode();
                    Node mvItem = subel.getFirstChild();
                    while (mvItem != null) {
                        multivaluedField.add(mvItem.getTextContent());
                        mvItem = mvItem.getNextSibling();
                    }
                    oDoc.put(fieldName, multivaluedField);
                } else {
                    String value = subel.getTextContent();
                    oDoc.put(fieldName, value);
                }
            }
            subel = subel.getNextSibling();
        }
        response.add(oDoc);
    }
    previousResponses.put(DOC_LIST, response);
}