Example usage for org.w3c.dom Element getNodeType

List of usage examples for org.w3c.dom Element getNodeType

Introduction

In this page you can find the example usage for org.w3c.dom Element getNodeType.

Prototype

public short getNodeType();

Source Link

Document

A code representing the type of the underlying object, as defined above.

Usage

From source file:Main.java

public static ArrayList<String> getNodeListAttValAsStringCols(String Xpath, Node node, String[] attrNames,
        String sep) throws Exception {
    ArrayList<String> retV = new ArrayList<String>();

    if (sep == null) {
        sep = " ";
    }/*from  www.j a  v a 2s.  c o m*/
    int aNamesL = attrNames.length;
    if (aNamesL > 0) {

        NodeList nl = getNodesListXpathNode(Xpath, node);
        int l = nl.getLength();
        Element e = null;
        String val = "";

        for (int i = 0; i < l; i++) {
            e = (Element) nl.item(i);
            if (e.getNodeType() == Node.ELEMENT_NODE) {
                StringBuilder sb = new StringBuilder();
                for (int y = 0; y < aNamesL; y++) {
                    sb.append(e.getAttribute(attrNames[y]));
                    if (y < aNamesL - 1) {
                        sb.append(sep);
                    }
                }
                val = sb.toString();
                if (val != null && val.length() > 0) {
                    //log.info("getNodeListAttValAsStringCol val = "+val +" attrNames = "+attrNames);
                    /*try {
                       log.info(convertToStringLeaveCDATA(e));
                    }catch(Exception E) {
                       E.printStackTrace();
                    }*/
                    retV.add(val);
                }
            }
        }
    }
    return retV;
}

From source file:Main.java

public static ArrayList<String> getNodeListAttValAsStringCols(final String xPath, final Node node,
        final String[] attrNames, final String sep) throws Exception {
    ArrayList<String> retV = new ArrayList<String>();

    String locSep = " ";

    if (sep != null) {
        locSep = sep;//from w  w w  .j  a  v  a 2 s .com
    }
    int aNamesL = attrNames.length;
    if (aNamesL > 0) {

        NodeList nl = getNodesListXpathNode(xPath, node);
        int l = nl.getLength();
        Element e = null;
        String val = "";

        for (int i = 0; i < l; i++) {
            e = (Element) nl.item(i);
            if (e.getNodeType() == Node.ELEMENT_NODE) {
                StringBuilder sb = new StringBuilder();
                for (int y = 0; y < aNamesL; y++) {
                    sb.append(e.getAttribute(attrNames[y]));
                    if (y < aNamesL - 1) {
                        sb.append(locSep);
                    }
                }
                val = sb.toString();
                if (val != null && val.length() > 0) {
                    retV.add(val);
                }
            }
        }
    }
    return retV;
}

From source file:it.delli.mwebc.servlet.MWebCRenderer.java

public static void buildLayout(Element elem, Widget parent, Page page) throws Exception {
    Widget widget = null;// w  ww.  j  a va2 s.c om
    if (elem.getNodeType() == Node.ELEMENT_NODE && elem.getNodeName().equals("Page")) {
        for (int j = 0; j < elem.getAttributes().getLength(); j++) {
            Node attribute = elem.getAttributes().item(j);
            if (!attribute.getNodeName().equals("id")) {
                if (attribute.getNodeName().equals("eventListener")) {
                    log.debug("Setting PageEventListener");
                    try {
                        page.setEventListener(
                                (PageEventListener) Class.forName(attribute.getNodeValue()).newInstance());
                    } catch (Exception e) {
                        log.error("Exception in setting PageEventListener");
                    }
                }
            }
        }
        if (page.getEventListener() == null) {
            log.debug("Setting default PageEventListener");
            page.setEventListener(new PageEventListener() {

                @Override
                public void onLoad(Event event) {
                }

                @Override
                public void onInit(Event event) {
                }

            });
        }
        for (int i = 0; i < elem.getChildNodes().getLength(); i++) {
            Node node = elem.getChildNodes().item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                buildLayout((Element) node, widget, page);
            }
        }
    } else if (elem.getNodeType() == Node.ELEMENT_NODE && elem.getNodeName().equals("PageFragment")) {
        for (int i = 0; i < elem.getChildNodes().getLength(); i++) {
            Node node = elem.getChildNodes().item(i);
            if (node.getNodeType() == Node.ELEMENT_NODE) {
                buildLayout((Element) node, parent, page);
            }
        }
    } else {
        Class<?> widgetClass = page.getApplication().getWidgetClass(elem.getNodeName());
        if (widgetClass == null) {
            log.fatal("Exception in getting widget class for widget " + elem.getNodeName());
            throw new Exception();
        } else if (widgetClass.getName().equals("com.dodifferent.applications.commander.widget.DataListItem")) {
            System.out.println();
        }
        CastHelper castHelper = page.getApplication().getCastHelper(widgetClass);

        String id = null;
        for (int j = 0; j < elem.getAttributes().getLength(); j++) {
            Node attribute = elem.getAttributes().item(j);
            if (attribute.getNodeName().equals("id")) {
                id = attribute.getNodeValue();
                break;
            }
        }

        HashMap<String, Object> initParams = new HashMap<String, Object>();
        for (int j = 0; j < elem.getAttributes().getLength(); j++) {
            Node attributeNode = elem.getAttributes().item(j);
            if (!attributeNode.getNodeName().equals("id") && !attributeNode.getNodeName().startsWith("on")) {
                Field fieldAttribute = ReflectionUtils.getAnnotatedField(widgetClass,
                        attributeNode.getNodeName(), WidgetAttribute.class);
                if (fieldAttribute != null) {
                    fieldAttribute.setAccessible(true);
                    Class<?> propertyType = fieldAttribute.getType();
                    //                  Type[] genericTypes = ((ParameterizedType)fieldAttribute.getGenericType()).getActualTypeArguments();
                    initParams.put(attributeNode.getNodeName(),
                            castHelper.toType(attributeNode.getNodeValue(), propertyType));
                } else {
                    log.warn("Warning in updating server widgets attribute. The attribute '"
                            + attributeNode.getNodeName() + "' doesn't exist for widget "
                            + widgetClass.getName());
                }
            }
        }

        try {
            if (id != null && initParams.keySet().size() == 0) {
                Class[] constructorParams = { Page.class, String.class };
                Constructor widgetConstructor = page.getApplication().getWidgetClass(elem.getNodeName())
                        .getConstructor(constructorParams);
                widget = (Widget) widgetConstructor.newInstance(page, id);
            } else if (id != null && initParams.keySet().size() > 0) {
                Class[] constructorParams = { Page.class, String.class, Map.class };
                Constructor widgetConstructor = page.getApplication().getWidgetClass(elem.getNodeName())
                        .getConstructor(constructorParams);
                widget = (Widget) widgetConstructor.newInstance(page, id, initParams);
            } else if (id == null && initParams.keySet().size() == 0) {
                Class[] constructorParams = { Page.class };
                Constructor widgetConstructor = page.getApplication().getWidgetClass(elem.getNodeName())
                        .getConstructor(constructorParams);
                widget = (Widget) widgetConstructor.newInstance(page);
            } else if (id == null && initParams.keySet().size() > 0) {
                Class[] constructorParams = { Page.class, Map.class };
                Constructor widgetConstructor = page.getApplication().getWidgetClass(elem.getNodeName())
                        .getConstructor(constructorParams);
                widget = (Widget) widgetConstructor.newInstance(page, initParams);
            }
        } catch (Exception e) {
            log.error("Exception in constructing widget " + widget.getClass().getName() + " (" + widget.getId()
                    + ")", e);
        }

        if (widget != null) {
            for (int j = 0; j < elem.getAttributes().getLength(); j++) {
                Node attributeNode = elem.getAttributes().item(j);
                if (!attributeNode.getNodeName().equals("id") && attributeNode.getNodeName().startsWith("on")) {
                    String event = attributeNode.getNodeName();
                    try {
                        widget.addEventListener(event, page.getEventListener(), attributeNode.getNodeValue());
                        //                     Method eventMethod = widget.getClass().getMethod("addEventListener", String.class, EventListener.class, String.class);
                        //                     eventMethod.invoke(widget, event, page.getEventListener(), attributeNode.getNodeValue());
                    } catch (Exception e) {
                        log.warn("Warning in registering EventListener for widget "
                                + widget.getClass().getName() + " (" + widget.getId() + ")", e);
                    }
                }
            }
            for (int i = 0; i < elem.getChildNodes().getLength(); i++) {
                Node node = elem.getChildNodes().item(i);
                if (node.getNodeType() == Node.CDATA_SECTION_NODE || node.getNodeType() == Node.TEXT_NODE) {
                    try {
                        String content = node.getNodeValue().replace("\t", "").replace("\n", "").replace("\"",
                                "\\\"");
                        if (!content.equals("")) {
                            //                        Method attributeMethod = widget.getClass().getMethod("addTextContent", String.class);
                            //                        attributeMethod.invoke(widget, content);
                            widget.addTextContent(content);
                        }
                    } catch (Exception e) {
                        log.warn("Warning in setting content for widget " + widget.getClass().getName() + " ("
                                + widget.getId() + ")", e);
                    }
                } else if (node.getNodeType() == Node.ELEMENT_NODE) {
                    buildLayout((Element) node, widget, page);
                }
            }
            widget.setParent(parent);
        }
    }
}

From source file:com.msopentech.odatajclient.engine.data.ODataBinder.java

private static ODataProperty fromCollectionPropertyElement(final Element prop, final EdmType edmType) {
    final ODataCollectionValue value = new ODataCollectionValue(
            edmType == null ? null : edmType.getTypeExpression());

    final EdmType type = edmType == null ? null : new EdmType(edmType.getBaseType());
    final NodeList elements = prop.getChildNodes();

    for (int i = 0; i < elements.getLength(); i++) {
        final Element child = (Element) elements.item(i);
        if (child.getNodeType() != Node.TEXT_NODE) {
            switch (guessPropertyType(child)) {
            case COMPLEX:
                value.add(fromComplexValueElement(child, type));
                break;
            case PRIMITIVE:
                value.add(fromPrimitiveValueElement(child, type));
                break;
            default:
                // do not add null or empty values
            }/*from  ww  w  .  ja v a 2 s .c o m*/
        }
    }

    return ODataFactory.newCollectionProperty(XMLUtils.getSimpleName(prop), value);
}

From source file:de.betterform.xml.dom.DOMUtil.java

/**
 * equivalent to the XPath expression './/tnuri:tagName[@anuri:attrName='attrValue']'
 *//*w ww.j  a v  a2 s.c  om*/
public static Element getElementByAttributeValueNS(Node start, String tnuri, String tagName, String anuri,
        String attrName, String attrValue) {
    NodeList nl = ((Element) start).getElementsByTagNameNS(tnuri, tagName);

    if (nl != null) {
        int l = nl.getLength();

        if (l == 0) {
            return null;
        }

        Element e = null;
        String compareValue = null;

        for (int i = 0; i < l; i++) {
            e = (Element) nl.item(i);

            if (e.getNodeType() == Node.ELEMENT_NODE) {
                compareValue = e.getAttributeNS(anuri, attrName);

                if (compareValue.equals(attrValue)) {
                    return e;
                }
            }
        }
    }

    return null;
}

From source file:com.msopentech.odatajclient.engine.data.AbstractODataBinder.java

protected ODataProperty fromCollectionPropertyElement(final Element prop, final EdmType edmType) {
    final ODataCollectionValue value = new ODataCollectionValue(
            edmType == null ? null : edmType.getTypeExpression());

    final EdmType type = edmType == null ? null : newEdmType(edmType.getBaseType());
    final NodeList elements = prop.getChildNodes();

    for (int i = 0; i < elements.getLength(); i++) {
        final Element child = (Element) elements.item(i);
        if (child.getNodeType() != Node.TEXT_NODE) {
            switch (guessPropertyType(child)) {
            case COMPLEX:
                value.add(fromComplexValueElement(child, type));
                break;
            case PRIMITIVE:
                value.add(fromPrimitiveValueElement(child, type));
                break;
            default:
                // do not add null or empty values
            }/* w w w.  j a va2 s  .c om*/
        }
    }

    return ODataObjectFactory.newCollectionProperty(XMLUtils.getSimpleName(prop), value);
}

From source file:it.ciroppina.idol.generic.tunnel.IdolOEMTunnel.java

/**
 * Method that returns a List<Map> where every map contains hit's fields 
 *  and the autn:content text, BUT it do not contains <DOCUMENT> structure !
 *  <br/>//from   w  w w.  ja va 2s .  co m
 *  Notice that: this method M^UST be invoked only for a print=indexText query !
 *  
 * @return ArrayList<Hit> list of Hit, each containing a Map (of dreFields)
 * 
 */
@WebMethod(operationName = "getQueryHitsNoDocumentMap")
public ArrayList<Hit> getQueryHitsNoDocumentMap(String xml) {
    ArrayList<Hit> result = new ArrayList<Hit>();
    Document document = getDocumentFrom(xml);

    NodeList temp = null;
    temp = document.getElementsByTagName("response");
    String response = (temp.getLength() > 0)
            ? document.getElementsByTagName("response").item(0).getTextContent()
            : "FAILURE";
    temp = document.getElementsByTagName("autn:numhits");
    String numHits = (temp.getLength() > 0)
            ? document.getElementsByTagName("autn:numhits").item(0).getTextContent()
            : "0";
    temp = document.getElementsByTagName("autn:totalhits");
    String totalHits = (temp.getLength() > 0)
            ? document.getElementsByTagName("autn:totalhits").item(0).getTextContent()
            : "0";

    NodeList hits = document.getElementsByTagName("autn:hit");

    for (int i = 0; i < hits.getLength(); i++) {
        HashMap<String, String> map = new HashMap<String, String>();
        Node nodo = hits.item(i);
        NodeList hitChilds = nodo.getChildNodes();

        for (int j = 0; j < hitChilds.getLength(); j++) {
            Node n = hitChilds.item(j);
            if (n.getNodeType() == Node.ELEMENT_NODE) {
                Element e2 = (Element) n;
                if (!e2.getNodeName().equals("autn:content")) {
                    String value = "";
                    if (map.containsKey(e2.getNodeName())) {
                        value = map.get(e2.getNodeName()) + "," + e2.getTextContent();
                        map.put(e2.getNodeName(), value);
                    } else {
                        map.put(e2.getNodeName(), e2.getTextContent());
                    }

                } else {
                    if (e2.getNodeType() == Node.ELEMENT_NODE) {
                        String nodeValue = e2.getFirstChild().getTextContent();
                        //Element el = (Element) content;
                        String value = "";
                        if (map.containsKey(e2.getNodeName())) {
                            value = map.get(e2.getNodeName()) + "," + nodeValue;
                            map.put(e2.getNodeName(), value);
                        } else {
                            map.put(e2.getNodeName(), nodeValue);
                        }
                    }
                }
            }
        }
        Hit hit = new Hit(map);
        hit.getDreFields().put("response", response);
        hit.getDreFields().put("numhits", numHits);
        hit.getDreFields().put("totalhits", totalHits);

        result.add(hit);
    }
    return result;
}

From source file:com.twinsoft.convertigo.engine.util.WsReference.java

private static void extractSoapVariables(XmlSchema xmlSchema, List<RequestableHttpVariable> variables,
        Node node, String longName, boolean isMulti, QName variableType) throws EngineException {
    if (node == null)
        return;/* w w w. j  ava  2  s .co  m*/
    int type = node.getNodeType();

    if (type == Node.ELEMENT_NODE) {
        Element element = (Element) node;
        if (element != null) {
            String elementName = element.getLocalName();
            if (longName != null)
                elementName = longName + "_" + elementName;

            if (!element.getAttribute("soapenc:arrayType").equals("") && !element.hasChildNodes()) {
                String avalue = element.getAttribute("soapenc:arrayType");
                element.setAttribute("soapenc:arrayType", avalue.replaceAll("\\[\\]", "[1]"));

                Element child = element.getOwnerDocument().createElement("item");
                String atype = avalue.replaceAll("\\[\\]", "");
                child.setAttribute("xsi:type", atype);
                if (atype.startsWith("xsd:")) {
                    String variableName = elementName + "_item";
                    child.appendChild(
                            element.getOwnerDocument().createTextNode("$(" + variableName.toUpperCase() + ")"));
                    RequestableHttpVariable httpVariable = createHttpVariable(true, variableName,
                            new QName(Constants.URI_2001_SCHEMA_XSD, atype.split(":")[1]));
                    variables.add(httpVariable);
                }
                element.appendChild(child);
            }

            // extract from attributes
            NamedNodeMap map = element.getAttributes();
            for (int i = 0; i < map.getLength(); i++) {
                Node child = map.item(i);
                if (child.getNodeName().equals("soapenc:arrayType"))
                    continue;
                if (child.getNodeName().equals("xsi:type"))
                    continue;
                if (child.getNodeName().equals("soapenv:encodingStyle"))
                    continue;

                String variableName = getVariableName(variables, elementName + "_" + child.getLocalName());

                child.setNodeValue("$(" + variableName.toUpperCase() + ")");

                RequestableHttpVariable httpVariable = createHttpVariable(false, variableName,
                        Constants.XSD_STRING);
                variables.add(httpVariable);
            }

            // extract from children nodes
            boolean multi = false;
            QName qname = Constants.XSD_STRING;
            NodeList children = element.getChildNodes();
            if (children.getLength() > 0) {
                Node child = element.getFirstChild();
                while (child != null) {
                    if (child.getNodeType() == Node.COMMENT_NODE) {
                        String value = child.getNodeValue();
                        if (value.startsWith("type:")) {
                            String schemaType = child.getNodeValue().substring("type:".length()).trim();
                            qname = getVariableSchemaType(xmlSchema, schemaType);
                        }
                        if (value.indexOf("repetitions:") != -1) {
                            multi = true;
                        }
                    } else if (child.getNodeType() == Node.TEXT_NODE) {
                        String value = child.getNodeValue().trim();
                        if (value.equals("?") || !value.equals("")) {
                            String variableName = getVariableName(variables, elementName);

                            child.setNodeValue("$(" + variableName.toUpperCase() + ")");

                            RequestableHttpVariable httpVariable = createHttpVariable(isMulti, variableName,
                                    variableType);
                            variables.add(httpVariable);
                        }
                    } else if (child.getNodeType() == Node.ELEMENT_NODE) {
                        extractSoapVariables(xmlSchema, variables, child, elementName, multi, qname);
                        multi = false;
                        qname = Constants.XSD_STRING;
                    }

                    child = child.getNextSibling();
                }
            }

        }
    }
}

From source file:edu.uams.clara.webapp.xml.processor.impl.DefaultXmlProcessorImpl.java

private synchronized String duplicate(final Document originalDom, final Element originalRootElement,
        final Element patchElement) throws Exception {

    boolean isdone = false;
    Element parentElement = null;

    DuplicateChildElementObject childElementObject = isChildElement(originalRootElement, patchElement);
    if (!childElementObject.isNeedDuplicate()) {
        isdone = true;//from  w  w  w .  j  a  v  a  2  s  . com
        parentElement = childElementObject.getElement();
    } else if (childElementObject.getElement() != null) {
        parentElement = childElementObject.getElement();
    } else {
        parentElement = originalRootElement;
    }

    String son_name = patchElement.getNodeName();

    Element subITEM = null;
    if (!isdone) {
        subITEM = originalDom.createElement(son_name);

        if (patchElement.hasChildNodes()) {
            if (patchElement.getFirstChild().getNodeType() == Node.TEXT_NODE) {
                subITEM.setTextContent(patchElement.getTextContent());

            }
        }

        if (patchElement.hasAttributes()) {
            NamedNodeMap attributes = patchElement.getAttributes();
            for (int i = 0; i < attributes.getLength(); i++) {
                String attribute_name = attributes.item(i).getNodeName();
                String attribute_value = attributes.item(i).getNodeValue();
                subITEM.setAttribute(attribute_name, attribute_value);
            }
        }
        parentElement.appendChild(subITEM);
    } else {
        subITEM = parentElement;
    }

    NodeList sub_messageItems = patchElement.getChildNodes();
    int sub_item_number = sub_messageItems.getLength();
    logger.debug("patchEl: " + DomUtils.elementToString(patchElement) + "length: " + sub_item_number);
    if (sub_item_number == 0) {
        isdone = true;
    } else {
        for (int j = 0; j < sub_item_number; j++) {
            if (sub_messageItems.item(j).getNodeType() == Node.ELEMENT_NODE) {
                Element sub_messageItem = (Element) sub_messageItems.item(j);
                logger.debug("node name: " + DomUtils.elementToString(subITEM) + " node type: "
                        + subITEM.getNodeType());
                duplicate(originalDom, subITEM, sub_messageItem);
            }

        }
    }

    return (parentElement != null) ? DomUtils.elementToString(parentElement) : "";
}

From source file:org.apache.xml.security.keys.keyresolver.KeyResolver.java

/**
 * Method getX509Certificate/*from w  ww  . j av  a 2s  . c om*/
 *
 * @param element
 * @param BaseURI
 * @param storage
 * @return The certificate represented by the element.
 * 
 * @throws KeyResolverException
 */
public static final X509Certificate getX509Certificate(Element element, String BaseURI, StorageResolver storage)
        throws KeyResolverException {
    for (KeyResolver resolver : resolverVector) {
        if (resolver == null) {
            Object exArgs[] = {
                    (((element != null) && (element.getNodeType() == Node.ELEMENT_NODE)) ? element.getTagName()
                            : "null") };

            throw new KeyResolverException("utils.resolver.noClass", exArgs);
        }
        if (log.isDebugEnabled()) {
            log.debug("check resolvability by class " + resolver.getClass());
        }

        X509Certificate cert = resolver.resolveX509Certificate(element, BaseURI, storage);
        if (cert != null) {
            return cert;
        }
    }

    Object exArgs[] = {
            (((element != null) && (element.getNodeType() == Node.ELEMENT_NODE)) ? element.getTagName()
                    : "null") };

    throw new KeyResolverException("utils.resolver.noClass", exArgs);
}