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:org.dasein.cloud.aws.platform.SimpleDB.java

@Override
public Iterable<KeyValuePair> getKeyValuePairs(String inDomainId, String itemId, boolean consistentRead)
        throws CloudException, InternalException {
    APITrace.begin(provider, "KVDB.getKeyValuePairs");
    try {/*from ww  w.  j a va 2s  .c  o m*/
        Map<String, String> parameters = provider.getStandardSimpleDBParameters(provider.getContext(),
                GET_ATTRIBUTES);
        EC2Method method;
        Document doc;

        parameters.put("DomainName", inDomainId);
        parameters.put("ItemName", itemId);
        parameters.put("ConsistentRead", String.valueOf(consistentRead));
        method = new EC2Method(SERVICE_ID, provider, parameters);
        try {
            doc = method.invoke();
        } catch (EC2Exception e) {
            String code = e.getCode();

            if (code != null && code.equals("NoSuchDomain")) {
                return null;
            }
            throw new CloudException(e);
        }
        ;
        ArrayList<KeyValuePair> pairs = new ArrayList<KeyValuePair>();

        NodeList blocks = doc.getElementsByTagName("Attribute");
        for (int i = 0; i < blocks.getLength(); i++) {
            Node node = blocks.item(i);

            if (node.hasChildNodes()) {
                NodeList children = node.getChildNodes();
                String key = null, value = null;

                for (int j = 0; j < children.getLength(); j++) {
                    Node item = children.item(j);

                    if (item.hasChildNodes()) {
                        String nv = item.getFirstChild().getNodeValue();

                        if (item.getNodeName().equals("Name")) {
                            key = nv;
                        } else if (item.getNodeName().equals("Value")) {
                            value = nv;
                        }
                    }
                }
                if (key != null) {
                    pairs.add(new KeyValuePair(key, value));
                }
            }
        }
        return pairs;
    } finally {
        APITrace.end();
    }
}

From source file:com.l2jfree.gameserver.datatables.MultisellTable.java

private MultiSellEntry parseEntry(Node n, int entryId) {
    //int entryId = Integer.parseInt(n.getAttributes().getNamedItem("id").getNodeValue());

    Node first = n.getFirstChild();
    MultiSellEntry entry = new MultiSellEntry(entryId);

    for (n = first; n != null; n = n.getNextSibling()) {
        if ("ingredient".equalsIgnoreCase(n.getNodeName())) {
            Node attribute;//from www  . jav a 2s.c om

            int id = Integer.parseInt(n.getAttributes().getNamedItem("id").getNodeValue());
            long count = Long.parseLong(n.getAttributes().getNamedItem("count").getNodeValue());

            boolean isTaxIngredient = false, maintainIngredient = false;

            attribute = n.getAttributes().getNamedItem("isTaxIngredient");

            if (attribute != null)
                isTaxIngredient = Boolean.parseBoolean(attribute.getNodeValue());

            attribute = n.getAttributes().getNamedItem("maintain");

            if (attribute != null)
                maintainIngredient = Boolean.parseBoolean(attribute.getNodeValue());

            MultiSellIngredient e = new MultiSellIngredient(id, count, isTaxIngredient, maintainIngredient);
            entry.addIngredient(e);

            validateItemId(id);
        } else if ("production".equalsIgnoreCase(n.getNodeName())) {
            int id = Integer.parseInt(n.getAttributes().getNamedItem("id").getNodeValue());
            long count = Long.parseLong(n.getAttributes().getNamedItem("count").getNodeValue());

            MultiSellIngredient e = new MultiSellIngredient(id, count);
            entry.addProduct(e);

            validateItemId(id);
        }
    }

    return entry;
}

From source file:org.dasein.cloud.aws.platform.CloudFront.java

private @Nullable Distribution toDistributionFromInfo(@Nonnull ProviderContext ctx, @Nullable Node node) {
    if (node == null) {
        return null;
    }// w w  w. j  a  v  a 2  s.c  om
    ArrayList<String> cnames = new ArrayList<String>();
    Distribution distribution = new Distribution();
    NodeList attrs = node.getChildNodes();

    distribution.setProviderOwnerId(ctx.getAccountNumber());
    for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        String name;

        name = attr.getNodeName();
        if (name.equals("Id")) {
            distribution.setProviderDistributionId(attr.getFirstChild().getNodeValue().trim());
        } else if (name.equals("Status")) {
            String s = attr.getFirstChild().getNodeValue();

            distribution.setDeployed(s != null && s.trim().equalsIgnoreCase("deployed"));
        } else if (name.equals("DomainName")) {
            distribution.setDnsName(attr.getFirstChild().getNodeValue().trim());
        } else if (name.equals("DistributionConfig")) {
            NodeList configList = attr.getChildNodes();

            for (int j = 0; j < configList.getLength(); j++) {
                Node config = configList.item(j);

                if (config.getNodeName().equals("Enabled")) {
                    String s = config.getFirstChild().getNodeValue();

                    distribution.setActive(s != null && s.trim().equalsIgnoreCase("true"));
                } else if (config.getNodeName().equals("Origin")) {
                    String origin = config.getFirstChild().getNodeValue().trim();

                    distribution.setLocation(origin);
                } else if (config.getNodeName().equals("CNAME")) {
                    cnames.add(config.getFirstChild().getNodeValue().trim());
                } else if (config.getNodeName().equals("Logging")) {
                    if (config.hasChildNodes()) {
                        NodeList logging = config.getChildNodes();

                        for (int k = 0; k < logging.getLength(); k++) {
                            Node logInfo = logging.item(k);

                            if (logInfo.getNodeName().equals("Bucket")) {
                                if (logInfo.hasChildNodes()) {
                                    distribution.setLogDirectory(logInfo.getFirstChild().getNodeValue());
                                }
                            } else if (logInfo.getNodeName().equals("Prefix")) {
                                if (logInfo.hasChildNodes()) {
                                    distribution.setLogName(logInfo.getFirstChild().getNodeValue());
                                }
                            }
                        }
                    }
                } else if (config.getNodeName().equals("Comment")) {
                    if (config.hasChildNodes()) {
                        String comment = config.getFirstChild().getNodeValue();

                        if (comment != null) {
                            distribution.setName(comment.trim());
                        }
                    }
                }
            }
        }
    }
    if (distribution.getName() == null) {
        String name = distribution.getDnsName();

        if (name == null) {
            name = distribution.getProviderDistributionId();
            if (name == null) {
                return null;
            }
        }
        distribution.setName(name);
    }
    String[] aliases = new String[cnames.size()];
    int i = 0;
    for (String cname : cnames) {
        aliases[i++] = cname;
    }
    distribution.setAliases(aliases);
    return distribution;
}

From source file:com.verisign.epp.codec.gen.EPPUtil.java

/**
 * Return the text data within an XML tag.<br>
 * <id>12345</id> yields the String "12345"<br>
 * If the node==null, child==null, value==null, or value=="" => Exception<br>
 * UNLESS allowEmpty, in which case return ""
 * <p>//from   ww w  . ja  v  a2  s.com
 * For reference:<br>
 * &lt;tag&gt;&lt;/tag&gt; => child is null<br>
 * &lt;tag/&gt; => child is null<br>
 * &lt;tag&gt; &lt;/tag&gt; => value is empty
 */
static public String getTextContent(Node node, boolean allowEmpty) throws EPPDecodeException {
    if (node != null) {
        Node child = node.getFirstChild();
        if (child != null) {
            String value = child.getNodeValue();
            if (value != null) {
                value = value.trim();
                if (value.length() > 0) {
                    return value;
                }
            }
        }
    }
    if (allowEmpty)
        return "";
    throw new EPPDecodeException("Empty tag encountered during decode");
}

From source file:org.dasein.cloud.aws.platform.SNS.java

private @Nullable ResourceStatus toStatus(@Nullable Node fromAws) throws InternalException, CloudException {
    if (fromAws == null) {
        return null;
    }/*from   w w w  .  j a v  a 2s  .  co  m*/
    NodeList attrs = fromAws.getChildNodes();
    String topicId = null;

    for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        String name;

        name = attr.getNodeName();
        if (name.equals("TopicArn")) {
            topicId = attr.getFirstChild().getNodeValue().trim();
        }
    }
    return new ResourceStatus(topicId, true);
}

From source file:org.escidoc.browser.elabsmodul.service.ELabsService.java

private RigBean resolveRig(final ItemProxy itemProxy) throws EscidocBrowserException {
    Preconditions.checkNotNull(itemProxy, "Resource is null");

    final RigBean rigBean = new RigBean();
    final String URI_DC = "http://purl.org/dc/elements/1.1/";
    final String URI_EL = "http://escidoc.org/ontologies/bw-elabs/re#";
    final String URI_RDF = "http://www.w3.org/1999/02/22-rdf-syntax-ns#";

    final NodeList nodeList = itemProxy.getMetadataRecords().get("escidoc").getContent().getChildNodes();

    rigBean.setObjectId(itemProxy.getId());
    for (int i = 0; i < nodeList.getLength(); i++) {
        final Node node = nodeList.item(i);
        final String nodeName = node.getLocalName();
        final String nsUri = node.getNamespaceURI();

        if ("title".equals(nodeName) && URI_DC.equals(nsUri)) {
            rigBean.setName((node.getFirstChild() != null) ? node.getFirstChild().getNodeValue() : null);
        } else if ("description".equals(nodeName) && URI_DC.equals(nsUri)) {
            rigBean.setDescription((node.getFirstChild() != null) ? node.getFirstChild().getNodeValue() : null);
        } else if ("instrument".equals(nodeName) && URI_EL.equals(nsUri)) {
            if (node.getAttributes() != null
                    && node.getAttributes().getNamedItemNS(URI_RDF, "resource") != null) {
                final Node attributeNode = node.getAttributes().getNamedItemNS(URI_RDF, "resource");
                final String instrumentID = attributeNode.getNodeValue();

                try {
                    final ItemProxy instrumentProxy = (ItemProxy) repositories.item().findById(instrumentID);
                    rigBean.getContentList().add(resolveInstrument(instrumentProxy));
                } catch (final EscidocClientException e) {
                    LOG.error(e.getLocalizedMessage());
                }//ww w .j  a  v  a 2 s . co m
            }
        }
    }
    return rigBean;
}

From source file:com.haulmont.cuba.restapi.XMLConverter.java

private Object parseEntityReference(Node node, CommitRequest commitRequest) throws InstantiationException,
        IllegalAccessException, InvocationTargetException, IntrospectionException {
    Node childNode = node.getFirstChild();
    if (ELEMENT_NULL_REF.equals(childNode.getNodeName())) {
        return null;
    }// w w w .  ja  v a  2s.  c o m

    InstanceRef ref = commitRequest.parseInstanceRefAndRegister(getIdAttribute(childNode));
    return ref.getInstance();
}

From source file:org.dasein.cloud.aws.platform.SNS.java

private Topic toTopic(Node fromAws) throws InternalException, CloudException {
    NodeList attrs = fromAws.getChildNodes();
    Topic topic = new Topic();

    topic.setProviderOwnerId(provider.getContext().getAccountNumber());
    topic.setProviderRegionId(provider.getContext().getRegionId());
    topic.setActive(true);// ww  w .ja  v a  2s.c om
    for (int i = 0; i < attrs.getLength(); i++) {
        Node attr = attrs.item(i);
        String name;

        name = attr.getNodeName();
        if (name.equals("TopicArn")) {
            String id = attr.getFirstChild().getNodeValue().trim();

            topic.setProviderTopicId(id);
        }
    }
    if (topic.getProviderTopicId() == null) {
        return null;
    }
    setTopicAttributes(topic);
    return topic;
}

From source file:edu.wpi.margrave.MCommunicator.java

private static Document handleExplore(String originalXMLText, Node n) {
    MExploreCondition exploreCondition;//from  w  ww . j a v  a  2 s  .c  om

    // Catch and re-throw any exception, because if EXPLORE fails,
    // need to reset lastResult to -1.

    try {
        n = n.getFirstChild();
        String name = n.getNodeName();
        if (name.equalsIgnoreCase("EXPLORE")) {
            //Explore should only have one child - "Condition". exploreHelper takes the node one down from condition
            String queryID = getAttributeOfChildNodeOrNode(n, "EXPLORE", "id");

            if (MEnvironment.isQueryIDUsed(queryID)) {
                // Don't allow ID re-use.
                return MEnvironment.errorResponse(MEnvironment.sQuery, MEnvironment.sFailure,
                        "The query identifier " + queryID + " is already in use.");
            }

            MQuery result = null;

            //Default Values
            List<MIDBCollection> under = new LinkedList<MIDBCollection>();
            List<String> publ = new ArrayList<String>();
            Map<String, String> publSorts = new HashMap<String, String>();
            Integer debugLevel = 0;

            Node underNode = getUnderNode(n);
            Node publishNode = getPublishNode(n);
            Node debugNode = getDebugNode(n);
            Node ceilingsNode = getCeilingsNode(n);

            if (underNode != null) {
                under = namesToIDBCollections(getUnderList(n));

            }
            if (publishNode != null) {
                // <PUBLISH><VARIABLE-DECLARATION sort=\"B\"><VARIABLE-TERM id=\"y\" /></VARIABLE-DECLARATION>
                //          <VARIABLE-DECLARATION sort=\"C\"><VARIABLE-TERM id=\"x\" /></VARIABLE-DECLARATION></PUBLISH>

                List<Node> decls = getElementChildren(publishNode);
                for (Node varDeclNode : decls) {
                    String varname = getAttributeOfChildNodeOrNode(varDeclNode, "VARIABLE-DECLARATION",
                            "varname");
                    String vartype = getAttributeOfChildNodeOrNode(varDeclNode, "VARIABLE-DECLARATION", "sort");
                    publ.add(varname);
                    publSorts.put(varname, vartype);
                }

            }
            if (debugNode != null) {
                debugLevel = Integer.parseInt(getDebugLevel(debugNode));
            }

            Map<String, Integer> ceilingMap = new HashMap<String, Integer>();
            if (ceilingsNode != null) {
                List<Node> ceilingNodes = getElementChildren(ceilingsNode);

                for (Node ceil : ceilingNodes) {
                    // LOCAL ceilings (for this query only)
                    String sortname = getNodeAttribute(ceil, "sort");
                    String ceiling = getNodeAttribute(ceil, "value");
                    writeToLog("\nCEILING: " + sortname + " is " + ceiling);

                    try {
                        int intValue = Integer.parseInt(ceiling);
                        ceilingMap.put(sortname, intValue);
                    } catch (NumberFormatException e) {
                        return MEnvironment.errorResponse(MEnvironment.sNotDocument, "Not an integer", ceiling);
                    }
                }
            }

            // Finally extract the explore condition. Do last because need to gather
            // info from the other nodes (list of published vars).
            exploreCondition = exploreHelper(n.getFirstChild().getFirstChild(), new ArrayList<String>(publ));
            if (exploreCondition == null)
                throw new MCommunicatorException("Explore condition is null!");

            // Exception will be thrown and caught by caller to return an EXCEPTION element.
            result = MQuery.createFromExplore(queryID, exploreCondition.addSeenIDBCollections(under), publ,
                    publSorts, debugLevel, ceilingMap);

            writeToLog("AT END OF EXPLORE");
            return MEnvironment.returnQueryResponse(result, originalXMLText);

        } // end if explore node

        throw new MUserException("Internal error: Command promised a query definition but did not provide it.");

    } catch (MBaseException e) {
        MEnvironment.clearLastQuery();
        throw e;
    }
}

From source file:de.fuberlin.wiwiss.marbles.MarblesServlet.java

/**
 * Enhances source data with consistently colored icons;
 * adds detailed source list to Fresnel output
 * /* w w  w  . ja  v  a2 s. c o  m*/
 * @param doc   The Fresnel tree 
 */
private void addSources(Document doc, List<org.apache.commons.httpclient.URI> retrievedURLs) {
    int colorIndex = 0;
    HashMap<String, Source> sources = new HashMap<String, Source>();

    NodeList nodeList = doc.getElementsByTagName("source");
    int numNodes = nodeList.getLength();

    for (int i = 0; i < numNodes; i++) {
        Node node = nodeList.item(i);
        String uri = node.getFirstChild().getFirstChild().getNodeValue();

        Source source;

        /* Get source, create it if necessary */
        if (null == (source = sources.get(uri))) {
            source = new Source(uri);
            colorIndex = source.determineIcon(colorIndex);
            sources.put(uri, source);
        }

        /* Enhance source reference with icon */
        Element sourceIcon = doc.createElementNS(Constants.nsFresnelView, "sourceIcon");
        sourceIcon.appendChild(doc.createTextNode(source.getIcon()));
        node.appendChild(sourceIcon);
    }

    /* Supplement source list with retrieved URLs */
    if (retrievedURLs != null)
        for (org.apache.commons.httpclient.URI uri : retrievedURLs) {
            Source source;
            if (null == (source = sources.get(uri.toString()))) {
                source = new Source(uri.toString());
                colorIndex = source.determineIcon(colorIndex);
                sources.put(uri.toString(), source);
            }
        }

    /* Provide list of sources */
    RepositoryConnection metaDataConn = null;
    try {
        metaDataConn = metaDataRepository.getConnection();
        Element sourcesElement = doc.createElementNS(Constants.nsFresnelView, "sources");
        for (String uri : sources.keySet()) {
            Source source = sources.get(uri);
            sourcesElement.appendChild(source.toElement(doc, cacheController, metaDataConn));
        }

        Node results = doc.getFirstChild();
        results.appendChild(sourcesElement);

    } catch (RepositoryException e) {
        e.printStackTrace();
    } finally {
        try {
            if (metaDataConn != null)
                metaDataConn.close();
        } catch (RepositoryException e) {
            e.printStackTrace();
        }
    }
}