Example usage for org.w3c.dom NamedNodeMap getLength

List of usage examples for org.w3c.dom NamedNodeMap getLength

Introduction

In this page you can find the example usage for org.w3c.dom NamedNodeMap getLength.

Prototype

public int getLength();

Source Link

Document

The number of nodes in this map.

Usage

From source file:de.betterform.xml.xforms.model.Instance.java

private void listModelItems(List list, Node node, boolean deep) {
    ModelItem modelItem = (ModelItem) node.getUserData("");
    if (modelItem == null) {
        modelItem = createModelItem(node);
    }//from  w w w  . ja  va  2s.c o  m
    list.add(modelItem);

    if (deep) {
        NamedNodeMap attributes = node.getAttributes();
        for (int index = 0; attributes != null && index < attributes.getLength(); index++) {
            listModelItems(list, attributes.item(index), deep);
        }
        if (node.getNodeType() != Node.ATTRIBUTE_NODE) {
            NodeList children = node.getChildNodes();
            for (int index = 0; index < children.getLength(); index++) {
                listModelItems(list, children.item(index), deep);
            }
        }
    }
}

From source file:DomPrintUtil.java

/**
 * Returns XML text converted from the target DOM
 * /*from   w ww . jav  a2  s .c  om*/
 * @return String format XML converted from the target DOM
 */
public String toXMLString() {
    StringBuffer tmpSB = new StringBuffer(8192);

    TreeWalkerImpl treeWalker = new TreeWalkerImpl(document, whatToShow, nodeFilter, entityReferenceExpansion);

    String lt = escapeTagBracket ? ESC_LT : LT;
    String gt = escapeTagBracket ? ESC_GT : GT;
    String line_sep = indent ? LINE_SEP : EMPTY_STR;

    Node tmpN = treeWalker.nextNode();
    boolean prevIsText = false;

    String indentS = EMPTY_STR;
    while (tmpN != null) {
        short type = tmpN.getNodeType();
        switch (type) {
        case Node.ELEMENT_NODE:
            if (prevIsText) {
                tmpSB.append(line_sep);
            }
            tmpSB.append(indentS + lt + tmpN.getNodeName());
            NamedNodeMap attrs = tmpN.getAttributes();
            int len = attrs.getLength();
            for (int i = 0; i < len; i++) {
                Node attr = attrs.item(i);
                String value = attr.getNodeValue();
                if (null != value) {
                    tmpSB.append(getAttributeString((Element) tmpN, attr));
                }
            }
            if (tmpN instanceof HTMLTitleElement && !tmpN.hasChildNodes()) {
                tmpSB.append(gt + ((HTMLTitleElement) tmpN).getText());
                prevIsText = true;
            } else if (checkNewLine(tmpN)) {
                tmpSB.append(gt + line_sep);
                prevIsText = false;
            } else {
                tmpSB.append(gt);
                prevIsText = true;
            }
            break;
        case Node.TEXT_NODE:
            if (!prevIsText) {
                tmpSB.append(indentS);
            }
            tmpSB.append(getXMLString(tmpN.getNodeValue()));
            prevIsText = true;
            break;
        case Node.COMMENT_NODE:
            String comment;
            if (escapeTagBracket) {
                comment = getXMLString(tmpN.getNodeValue());
            } else {
                comment = tmpN.getNodeValue();
            }
            tmpSB.append(line_sep + indentS + lt + "!--" + comment + "--" + gt + line_sep);
            prevIsText = false;
            break;
        case Node.CDATA_SECTION_NODE:
            tmpSB.append(line_sep + indentS + lt + "!CDATA[" + tmpN.getNodeValue() + "]]" + line_sep);
            break;
        case Node.DOCUMENT_TYPE_NODE:
            if (tmpN instanceof DocumentType) {
                DocumentType docType = (DocumentType) tmpN;
                String pubId = docType.getPublicId();
                String sysId = docType.getSystemId();
                if (null != pubId && pubId.length() > 0) {
                    if (null != sysId && sysId.length() > 0) {
                        tmpSB.append(lt + "!DOCTYPE " + docType.getName() + " PUBLIC \"" + pubId + " \"" + sysId
                                + "\">" + line_sep);
                    } else {
                        tmpSB.append(
                                lt + "!DOCTYPE " + docType.getName() + " PUBLIC \"" + pubId + "\">" + line_sep);
                    }
                } else {
                    tmpSB.append(lt + "!DOCTYPE " + docType.getName() + " SYSTEM \"" + docType.getSystemId()
                            + "\">" + line_sep);

                }
            } else {
                System.out.println("Document Type node does not implement DocumentType: " + tmpN);
            }
            break;
        default:
            System.out.println(tmpN.getNodeType() + " : " + tmpN.getNodeName());
        }

        Node next = treeWalker.firstChild();
        if (null != next) {
            if (indent && type == Node.ELEMENT_NODE) {
                indentS = indentS + " ";
            }
            tmpN = next;
            continue;
        }

        if (tmpN.getNodeType() == Node.ELEMENT_NODE) {
            tmpSB.append(lt + "/" + tmpN.getNodeName() + gt + line_sep);
            prevIsText = false;
        }

        next = treeWalker.nextSibling();
        if (null != next) {
            tmpN = next;
            continue;
        }

        tmpN = null;
        next = treeWalker.parentNode();
        while (null != next) {
            if (next.getNodeType() == Node.ELEMENT_NODE) {
                if (indent) {
                    if (indentS.length() > 0) {
                        indentS = indentS.substring(1);
                    } else {
                        System.err.println("indent: " + next.getNodeName() + " " + next);
                    }
                }
                tmpSB.append(line_sep + indentS + lt + "/" + next.getNodeName() + gt + line_sep);
                prevIsText = false;
            }
            next = treeWalker.nextSibling();
            if (null != next) {
                tmpN = next;
                break;
            }
            next = treeWalker.parentNode();
        }
    }
    return tmpSB.toString();
}

From source file:com.twinsoft.convertigo.engine.translators.WebServiceTranslator.java

private void copyNode(Node sourceNode, Node destinationNode) {
    Document destinationDoc = destinationNode.getOwnerDocument();

    switch (sourceNode.getNodeType()) {
    case Node.TEXT_NODE:
        Text text = destinationDoc.createTextNode(sourceNode.getNodeValue());
        destinationNode.appendChild(text);
        break;//from w  w  w.  j  av  a  2s  .c  o m
    case Node.ELEMENT_NODE:
        Element element = destinationDoc.createElement(sourceNode.getNodeName());
        destinationNode.appendChild(element);

        element.setTextContent(sourceNode.getNodeValue());

        // Copy attributes
        NamedNodeMap attributes = sourceNode.getAttributes();
        int nbAttributes = attributes.getLength();

        for (int i = 0; i < nbAttributes; i++) {
            Node attribute = attributes.item(i);
            element.setAttribute(attribute.getNodeName(), attribute.getNodeValue());
        }

        // Copy children nodes
        NodeList children = sourceNode.getChildNodes();
        int nbChildren = children.getLength();
        for (int i = 0; i < nbChildren; i++) {
            Node child = children.item(i);
            copyNode(child, element);
        }
    }
}

From source file:com.zoho.creator.jframework.XMLParser.java

public static ZCView parseForView(Document rootDocument, String appLinkName, String appOwner,
        String componentType, int month, int year) throws ZCException {
    NodeList nl = rootDocument.getChildNodes();

    ZCView toReturn = null;//w w  w.j  a v a2 s  .c  om
    for (int i = 0; i < nl.getLength(); i++) {

        Node responseNode = nl.item(i);
        if (responseNode.getNodeName().equals("response")) {
            NodeList responseNodes = responseNode.getChildNodes();
            for (int j = 0; j < responseNodes.getLength(); j++) {
                Node responseChildNode = responseNodes.item(j);

                if (responseChildNode.getNodeName().equals("errorlist")) {
                    NodeList errorListNodes = responseChildNode.getChildNodes();
                    for (int o = 0; o < errorListNodes.getLength(); o++) {
                        Node errorlistNode = errorListNodes.item(o);
                        if (errorlistNode.getNodeName().equals("error")) {
                            NodeList errorlistchildNodes = errorlistNode.getChildNodes();
                            int code = 0;
                            boolean hasErrorOcured = false;
                            String errorMessage = "";
                            for (int p = 0; p < errorlistchildNodes.getLength(); p++) {
                                Node errorlistchildNode = errorlistchildNodes.item(p);
                                if (errorlistchildNode.getNodeName().equals("code")) {
                                    code = getIntValue(errorlistchildNode, code);
                                } else if (errorlistchildNode.getNodeName().equals("message")) {
                                    hasErrorOcured = true;
                                    errorMessage = getDecodedString(getStringValue(errorlistchildNode, ""));

                                }
                            }

                            if (hasErrorOcured) {
                                ZCException exception = new ZCException(errorMessage, ZCException.ERROR_OCCURED,
                                        "");
                                exception.setCode(code);
                                throw exception;
                            }
                        }
                    }
                }

                if (responseChildNode.getNodeName().equals("metadata")) {
                    NodeList viewNodes = responseChildNode.getChildNodes();
                    for (int k = 0; k < viewNodes.getLength(); k++) {
                        Node viewNode = viewNodes.item(k);
                        if (viewNode.getNodeName().equals("View")) {
                            NamedNodeMap attrMap = viewNode.getAttributes();
                            String componentLinkName = getDecodedString(
                                    attrMap.getNamedItem("LinkName").getNodeValue());//No I18N

                            String componentName = getDecodedString(getChildNodeValue(viewNode, "DisplayName")); //No I18N
                            String dateFormat = getDecodedString(
                                    attrMap.getNamedItem("DateFormat").getNodeValue());//No I18N
                            //String appOwner, String appLinkName, String componentName, String componentLinkName
                            toReturn = new ZCView(appOwner, appLinkName, componentType, componentName,
                                    componentLinkName);
                            toReturn.setDateFormat(dateFormat);
                            NodeList viewChildNodes = viewNode.getChildNodes();
                            for (int l = 0; l < viewChildNodes.getLength(); l++) {
                                Node viewChildNode = viewChildNodes.item(l);
                                if (viewChildNode.getNodeName().equals("BaseForm")) {
                                    NamedNodeMap baseFormAttrMap = viewChildNode.getAttributes();
                                    String baseFormLinkName = getDecodedString(
                                            baseFormAttrMap.getNamedItem("linkname").getNodeValue()); //No I18N
                                    toReturn.setBaseFormLinkName(baseFormLinkName);
                                } else if (viewChildNode.getNodeName().equals("customFilters")) {
                                    NodeList customFilterNodeList = viewChildNode.getChildNodes();
                                    List<ZCCustomFilter> customFilters = new ArrayList<ZCCustomFilter>();
                                    for (int m = 0; m < customFilterNodeList.getLength(); m++) {
                                        Node customFilterNode = customFilterNodeList.item(m);
                                        NamedNodeMap customFilterAttrMap = customFilterNode.getAttributes();
                                        long id = Long.parseLong(
                                                customFilterAttrMap.getNamedItem("Id").getNodeValue()); //No I18N                                 
                                        String customFilterName = getDecodedString(
                                                getChildNodeValue(customFilterNode, "DisplayName"));//No I18N
                                        ZCCustomFilter customFilter = new ZCCustomFilter(customFilterName, id);
                                        customFilters.add(customFilter);
                                    }
                                    toReturn.addCustomFilters(customFilters);
                                } else if (viewChildNode.getNodeName().equals("filters")) {
                                    NodeList filterNodeList = viewChildNode.getChildNodes();
                                    List<ZCFilter> filters = new ArrayList<ZCFilter>();
                                    for (int m = 0; m < filterNodeList.getLength(); m++) {
                                        Node filterNode = filterNodeList.item(m);
                                        NamedNodeMap filterAttrMap = filterNode.getAttributes();
                                        String FilterLinkName = getDecodedString(
                                                filterAttrMap.getNamedItem("name").getNodeValue()); //No I18N
                                        String filterName = getDecodedString(
                                                getChildNodeValue(filterNode, "displayname")); //filterAttrMap.getNamedItem("displayname").getNodeValue(); //No I18N
                                        ZCFilter filter = new ZCFilter(FilterLinkName, filterName);
                                        NodeList filterValuesList = filterNode.getChildNodes();
                                        List<ZCFilterValue> filterValues = new ArrayList<ZCFilterValue>();
                                        for (int n = 0; n < filterValuesList.getLength(); n++) {
                                            Node filterValueNode = filterValuesList.item(n);
                                            if (filterValueNode.getNodeName().equals("value")) {

                                                String filterValue = getDecodedString(
                                                        getStringValue(filterValueNode, "")); //No I18N
                                                filterValue = filterValue
                                                        .substring(filterValue.indexOf(":") + 1);
                                                String displayValue = filterValue;
                                                NamedNodeMap filterValAttrMap = filterValueNode.getAttributes();
                                                if (filterValAttrMap.getLength() > 0) {
                                                    displayValue = getDecodedString(filterValAttrMap
                                                            .getNamedItem("display").getNodeValue());//No I18N
                                                }
                                                filterValues.add(new ZCFilterValue(filterValue, displayValue));
                                            }
                                        }
                                        filter.addValues(filterValues);
                                        filters.add(filter);
                                    }
                                    toReturn.addFilters(filters);
                                    //                           } else if(viewChildNode.getNodeName().equals("rec_filter")) {

                                    //                           } else if(viewChildNode.getNodeName().equals("groupby")) {

                                    //                           } else if(viewChildNode.getNodeName().equals("opensearch")) {

                                } else if (viewChildNode.getNodeName().equals("permissions")) {
                                    NamedNodeMap permissionAttrMap = viewChildNode.getAttributes();
                                    boolean isAddAllowed = Boolean
                                            .parseBoolean(permissionAttrMap.getNamedItem("add").getNodeValue()); //No I18N
                                    boolean isEditAllowed = Boolean.parseBoolean(
                                            permissionAttrMap.getNamedItem("edit").getNodeValue()); //No I18N
                                    boolean isDeleteAllowed = Boolean.parseBoolean(
                                            permissionAttrMap.getNamedItem("delete").getNodeValue()); //No I18N
                                    boolean isDuplicateAllowed = Boolean.parseBoolean(
                                            permissionAttrMap.getNamedItem("duplicate").getNodeValue()); //No I18N
                                    boolean isBulkEditAllowed = Boolean.parseBoolean(
                                            permissionAttrMap.getNamedItem("bulkedit").getNodeValue()); //No I18N
                                    toReturn.setAddAllowed(isAddAllowed);
                                    toReturn.setEditAllowed(isEditAllowed);
                                    toReturn.setDeleteAllowed(isDeleteAllowed);
                                    toReturn.setDuplicateAllowed(isDuplicateAllowed);
                                    toReturn.setBulkEditAllowed(isBulkEditAllowed);
                                } else if (viewChildNode.getNodeName().equals("Actions")) {
                                    NodeList actionList = viewChildNode.getChildNodes();
                                    List<ZCCustomAction> headerCustomActions = new ArrayList<ZCCustomAction>();
                                    List<ZCCustomAction> recordCustomActions = new ArrayList<ZCCustomAction>();
                                    for (int m = 0; m < actionList.getLength(); m++) {
                                        Node actionNode = actionList.item(m);
                                        NamedNodeMap actionAttrMap = actionNode.getAttributes();
                                        String actionType = getDecodedString(
                                                actionAttrMap.getNamedItem("type").getNodeValue()); //No I18N
                                        String actionName = getDecodedString(
                                                getChildNodeValue(actionNode, "name")); //actionAttrMap.getNamedItem("name").getNodeValue(); //No I18N
                                        String headerAction = getDecodedString(
                                                getChildNodeValue(actionNode, "isHeaderAction"));//No I18N
                                        Long actionId = Long
                                                .parseLong(actionAttrMap.getNamedItem("id").getNodeValue()); //No I18N
                                        ZCCustomAction action = new ZCCustomAction(actionType, actionName,
                                                actionId);
                                        if (actionType.equals("row")) {
                                            recordCustomActions.add(action);
                                        }
                                        if (headerAction.equals("true")) {
                                            headerCustomActions.add(action);
                                        }
                                    }
                                    toReturn.addHeaderCustomActions(headerCustomActions);
                                    toReturn.addRecordCustomActions(recordCustomActions);
                                } else if (viewChildNode.getNodeName().equals("Fields")) {
                                    // String fieldName, int type, String displayName, String delugeType, long compID
                                    List<ZCColumn> columns = new ArrayList<ZCColumn>();
                                    NodeList fieldList = viewChildNode.getChildNodes();
                                    for (int m = 0; m < fieldList.getLength(); m++) {
                                        Node fieldNode = fieldList.item(m);
                                        NamedNodeMap fieldAttrMap = fieldNode.getAttributes();
                                        String displayName = getDecodedString(
                                                getChildNodeValue(fieldNode, "DisplayName")); //fieldAttrMap.getNamedItem("DisplayName").getNodeValue(); //No I18N
                                        String fieldName = getDecodedString(
                                                fieldAttrMap.getNamedItem("BaseLabelName").getNodeValue()); //No I18N
                                        String compType = getDecodedString(
                                                fieldAttrMap.getNamedItem("ComponentType").getNodeValue()); //No I18N
                                        FieldType fieldType = FieldType.getFieldType(compType);
                                        int seqNo = Integer.parseInt(
                                                fieldAttrMap.getNamedItem("SequenceNumber").getNodeValue()); //No I18N
                                        ZCColumn column = new ZCColumn(fieldName, fieldType, displayName);
                                        column.setSequenceNumber(seqNo);
                                        columns.add(column);
                                    }
                                    Collections.sort(columns);
                                    toReturn.addColumns(columns);
                                }
                            }
                        }
                    }
                } else if (responseChildNode.getNodeName().equals("records")) {
                    parseAndSetRecords(toReturn, responseChildNode);
                } else if (responseChildNode.getNodeName().equals("calendar")) {
                    toReturn.setRecordsMonthYear(new ZCPair<Integer, Integer>(month, year));
                    parseAndSetCalendarRecords(toReturn, responseChildNode);
                }
            }
        }
    }
    //printDocument(rootDocument);      
    return toReturn;
}

From source file:com.connexta.arbitro.ctx.xacml3.XACML3EvaluationCtx.java

private Set<String> getChildXPaths(Node root, String xPath) {

    Set<String> xPaths = new HashSet<String>();
    NamespaceContext namespaceContext = null;

    XPathFactory factory = XPathFactory.newInstance();
    XPath xpath = factory.newXPath();

    if (namespaceContext == null) {

        //see if the request root is in a namespace
        String namespace = null;//from  www  .  ja v  a 2  s  .  c o m
        if (root != null) {
            namespace = root.getNamespaceURI();
        }
        // name spaces are used, so we need to lookup the correct
        // prefix to use in the search string
        NamedNodeMap namedNodeMap = root.getAttributes();

        Map<String, String> nsMap = new HashMap<String, String>();
        if (namedNodeMap != null) {
            for (int i = 0; i < namedNodeMap.getLength(); i++) {
                Node n = namedNodeMap.item(i);
                // we found the matching namespace, so get the prefix
                // and then break out
                String prefix = DOMHelper.getLocalName(n);
                String nodeValue = n.getNodeValue();
                nsMap.put(prefix, nodeValue);
            }
        }

        // if there is not any namespace is defined for content element, default XACML request
        //  name space would be there.
        if (XACMLConstants.REQUEST_CONTEXT_3_0_IDENTIFIER.equals(namespace)
                || XACMLConstants.REQUEST_CONTEXT_2_0_IDENTIFIER.equals(namespace)
                || XACMLConstants.REQUEST_CONTEXT_1_0_IDENTIFIER.equals(namespace)) {
            nsMap.put("xacml", namespace);
        }

        namespaceContext = new DefaultNamespaceContext(nsMap);
    }

    xpath.setNamespaceContext(namespaceContext);

    try {
        XPathExpression expression = xpath.compile(xPath);
        NodeList matches = (NodeList) expression.evaluate(root, XPathConstants.NODESET);
        if (matches != null && matches.getLength() > 0) {

            for (int i = 0; i < matches.getLength(); i++) {
                String text = null;
                Node node = matches.item(i);
                short nodeType = node.getNodeType();

                // see if this is straight text, or a node with data under
                // it and then get the values accordingly
                if ((nodeType == Node.CDATA_SECTION_NODE) || (nodeType == Node.COMMENT_NODE)
                        || (nodeType == Node.TEXT_NODE) || (nodeType == Node.ATTRIBUTE_NODE)) {
                    // there is no child to this node
                    text = node.getNodeValue();
                } else {

                    // the data is in a child node
                    text = "/" + DOMHelper.getLocalName(node);
                }
                String newXPath = '(' + xPath + ")[" + (i + 1) + ']';
                xPaths.add(newXPath);
            }
        }
    } catch (Exception e) {
        // TODO
    }

    return xPaths;
}

From source file:DOMProcessor.java

/** Converts the given DOM node into XML. Recursively converts
  * any child nodes. This version allows the XML version, encoding and stand-alone
  * status to be set.//from   w  w w  .ja va 2 s.  c  om
  * @param node DOM Node to display.
  * @param version XML version, or null if default ('1.0') is to be used.
  * @param encoding XML encoding, or null if encoding is not to be specified.
  * @param standalone XML stand-alone status or null if not to be specified.
  */
private void outputNodeAsXML(Node node, String version, String encoding, Boolean standalone) {
    // Store node name, type and value.
    String name = node.getNodeName(), value = makeFriendly(node.getNodeValue());
    int type = node.getNodeType();

    // Ignore empty nodes (e.g. blank lines etc.)
    if ((value != null) && (value.trim().equals(""))) {
        return;
    }

    switch (type) {
    case Node.DOCUMENT_NODE: // Start of document.
    {
        if (version == null) {
            out.print("<?xml version=\"1.0\" ");
        } else {
            out.print("<?xml version=\"" + version + "\" ");
        }

        if (encoding != null) {
            out.print("encoding=\"" + encoding + "\" ");
        }

        if (standalone != null) {
            if (standalone.booleanValue()) {
                out.print("standalone=\"yes\" ");
            } else {
                out.print("standalone=\"no\" ");
            }
        }

        out.println("?>");

        // Output the document's child nodes.
        NodeList children = node.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            outputNodeAsXML(children.item(i));
        }
        break;
    }

    case Node.ELEMENT_NODE: // Document element with attributes.
    {
        // Output opening element tag.
        indent++;
        indent();
        out.print("<" + name);

        // Output any attributes the element might have.
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attribute = attributes.item(i);
            out.print(" " + attribute.getNodeName() + "=\"" + attribute.getNodeValue() + "\"");
        }
        out.print(">");

        // Output any child nodes that exist.                    
        NodeList children = node.getChildNodes();

        for (int i = 0; i < children.getLength(); i++) {
            outputNodeAsXML(children.item(i));
        }

        break;
    }

    case Node.CDATA_SECTION_NODE: // Display text.
    case Node.TEXT_NODE: {
        out.print(value);
        break;
    }

    case Node.COMMENT_NODE: // Comment node.
    {
        indent++;
        indent();
        out.print("<!--" + value + "-->");
        indent--;
        break;
    }

    case Node.ENTITY_REFERENCE_NODE: // Entity reference nodes.
    {
        indent++;
        indent();
        out.print("&" + name + ";");
        indent--;
        break;
    }

    case Node.PROCESSING_INSTRUCTION_NODE: // Processing instruction.
    {
        indent++;
        indent();
        out.print("<?" + name);
        if ((value != null) && (value.length() > 0)) {
            out.print(" " + value);
        }
        out.println("?>");
        indent--;
        break;
    }
    }

    // Finally output closing tags for each element.
    if (type == Node.ELEMENT_NODE) {
        out.print("</" + node.getNodeName() + ">");
        indent--;
        if (node.getNextSibling() == null) {
            indent(); // Only throw new line if this is the last sibling.
        }
    }
}

From source file:com.twinsoft.convertigo.engine.translators.WebServiceTranslator.java

private SOAPElement addSoapElement(Context context, SOAPEnvelope se, SOAPElement soapParent, Node node)
        throws Exception {
    String prefix = node.getPrefix();
    String namespace = node.getNamespaceURI();
    String nodeName = node.getNodeName();
    String localName = node.getLocalName();
    String value = node.getNodeValue();

    boolean includeResponseElement = true;
    if (context.sequenceName != null) {
        includeResponseElement = ((Sequence) context.requestedObject).isIncludeResponseElement();
    }/* w  w  w  .j a v a  2  s  .  c  o  m*/

    SOAPElement soapElement = null;
    if (node.getNodeType() == Node.ELEMENT_NODE) {
        Element element = (Element) node;

        boolean toAdd = true;
        if (!includeResponseElement && "response".equalsIgnoreCase(localName)) {
            toAdd = false;
        }
        if ("http://schemas.xmlsoap.org/soap/envelope/".equals(element.getParentNode().getNamespaceURI())
                || "http://schemas.xmlsoap.org/soap/envelope/".equals(namespace)
                || nodeName.toLowerCase().endsWith(":envelope") || nodeName.toLowerCase().endsWith(":body")
        //element.getParentNode().getNodeName().toUpperCase().indexOf("NS0:") != -1 ||
        //nodeName.toUpperCase().indexOf("NS0:") != -1
        ) {
            toAdd = false;
        }

        if (toAdd) {
            if (prefix == null || prefix.equals("")) {
                soapElement = soapParent.addChildElement(nodeName);
            } else {
                soapElement = soapParent.addChildElement(se.createName(localName, prefix, namespace));
            }
        } else {
            soapElement = soapParent;
        }

        if (soapElement != null) {
            if (soapParent.equals(se.getBody()) && !soapParent.equals(soapElement)) {
                if (XsdForm.qualified == context.project.getSchemaElementForm()) {
                    if (soapElement.getAttribute("xmlns") == null) {
                        soapElement.addAttribute(se.createName("xmlns"), context.project.getTargetNamespace());
                    }
                }
            }

            if (element.hasAttributes()) {
                String attrType = element.getAttribute("type");
                if (("attachment").equals(attrType)) {
                    if (context.requestedObject instanceof AbstractHttpTransaction) {
                        AttachmentDetails attachment = AttachmentManager.getAttachment(element);
                        if (attachment != null) {
                            byte[] raw = attachment.getData();
                            if (raw != null)
                                soapElement.addTextNode(Base64.encodeBase64String(raw));
                        }

                        /* DON'T WORK YET *\
                        AttachmentPart ap = responseMessage.createAttachmentPart(new ByteArrayInputStream(raw), element.getAttribute("content-type"));
                        ap.setContentId(key);
                        ap.setContentLocation(element.getAttribute("url"));
                        responseMessage.addAttachmentPart(ap);
                        \* DON'T WORK YET */
                    }
                }

                if (!includeResponseElement && "response".equalsIgnoreCase(localName)) {
                    // do not add attributes
                } else {
                    NamedNodeMap attributes = element.getAttributes();
                    int len = attributes.getLength();
                    for (int i = 0; i < len; i++) {
                        Node item = attributes.item(i);
                        addSoapElement(context, se, soapElement, item);
                    }
                }
            }

            if (element.hasChildNodes()) {
                NodeList childNodes = element.getChildNodes();
                int len = childNodes.getLength();
                for (int i = 0; i < len; i++) {
                    Node item = childNodes.item(i);
                    switch (item.getNodeType()) {
                    case Node.ELEMENT_NODE:
                        addSoapElement(context, se, soapElement, item);
                        break;
                    case Node.CDATA_SECTION_NODE:
                    case Node.TEXT_NODE:
                        String text = item.getNodeValue();
                        text = (text == null) ? "" : text;
                        soapElement.addTextNode(text);
                        break;
                    default:
                        break;
                    }
                }
            }
        }
    } else if (node.getNodeType() == Node.ATTRIBUTE_NODE) {
        if (prefix == null || prefix.equals("")) {
            soapElement = soapParent.addAttribute(se.createName(nodeName), value);
        } else {
            soapElement = soapParent.addAttribute(se.createName(localName, prefix, namespace), value);
        }
    }
    return soapElement;
}

From source file:com.krawler.workflow.bizservice.WorkflowServiceImpl.java

@Override
public String reloadWorkflow(String processid) throws ServiceException {
    String result = "{\"success\":false}";
    try {/*from  w  w w  .j a  v a  2s.  c om*/
        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        String path = ConfigReader.getinstance().get("workflowpath") + processid;

        File fdir = new File(path);
        File file = new File(fdir + System.getProperty("file.separator") + "bpmn.xml");
        Document doc = docBuilder.parse(file);
        int s;
        int a;
        String name = "";
        ObjectInfo obj = new ObjectInfo();
        ArrayList taskContainer = new ArrayList();

        String nodeName = "";
        NodeList nodeList = doc.getElementsByTagName("Pool");
        for (s = 1; s < nodeList.getLength(); s++) {
            Node node = nodeList.item(s);
            ObjectInfo processObj = new ObjectInfo();
            processObj.type = "process";
            getNodeInfo(node, processObj);
            NodeList childrenList = node.getChildNodes();
            for (int cnt = 0; cnt < childrenList.getLength(); cnt++) {
                node = childrenList.item(cnt);
                nodeName = node.getNodeName();
                if (nodeName.compareToIgnoreCase("Lanes") == 0) {
                    NodeList laneList = node.getChildNodes();
                    for (int laneCount = 0; laneCount < laneList.getLength(); laneCount++) {
                        node = laneList.item(laneCount);
                        nodeName = node.getNodeName();
                        if (nodeName.compareToIgnoreCase("Lane") == 0) {
                            ObjectInfo laneObj = new ObjectInfo();
                            laneObj.type = "lane";
                            getNodeInfo(node, laneObj);
                            NodeList laneChildren = node.getChildNodes();
                            for (int laneChildrencnt = 0; laneChildrencnt < laneChildren
                                    .getLength(); laneChildrencnt++) {
                                node = laneChildren.item(laneChildrencnt);
                                nodeName = node.getNodeName();
                                if (nodeName.compareToIgnoreCase("NodeGraphicsInfos") == 0) {
                                    getGraphicsNodeInfo(getActivityNode(node, 1), laneObj);
                                }
                            }
                            taskContainer.add(laneObj);
                        }
                    }
                } else {
                    if (nodeName.compareToIgnoreCase("NodeGraphicsInfos") == 0) {
                        getGraphicsNodeInfo(getActivityNode(node, 1), processObj);
                    }
                }

            }
            taskContainer.add(processObj);
        }

        nodeList = doc.getElementsByTagName("Activities");

        for (s = 0; s < nodeList.getLength(); s++) {
            name = "";
            Node node = nodeList.item(s);
            NodeList childrenList = node.getChildNodes();
            for (int cnt = 0; cnt < childrenList.getLength(); cnt++) {
                node = childrenList.item(cnt);
                Node activityNode = getActivityNode(node, 0);
                if (activityNode.getNodeType() == Node.ELEMENT_NODE) {
                    obj = new ObjectInfo();
                    obj.type = "activity";
                    getNodeInfo(activityNode, obj);
                    Node graphicsInfoNode = getActivityNode(activityNode, 1);
                    getGraphicsNodeInfo(graphicsInfoNode, obj);
                    taskContainer.add(obj);
                }
            }
        }

        JSONObject jtemp = new com.krawler.utils.json.base.JSONObject();
        for (int j = 0; j < taskContainer.size(); j++) {
            obj = (ObjectInfo) taskContainer.get(j);
            JSONObject jobj = new com.krawler.utils.json.base.JSONObject();
            jobj.put("Id", obj.objId);
            jobj.put("name", obj.name);
            jobj.put("xpos", obj.xpos);
            jobj.put("ypos", obj.ypos);
            jobj.put("height", obj.height);
            jobj.put("width", obj.width);
            jobj.put("parent", obj.parentId);
            jobj.put("refId", obj.refId);
            jobj.put("hasStart", obj.hasStart);
            jobj.put("hasEnd", obj.hasEnd);
            jobj.put("startRefId", obj.startRefId);
            jobj.put("endRefId", obj.endRefId);
            jobj.put("derivationRule", obj.derivationRule);
            jobj.put("domEl", obj.domEl);
            if (obj.type.compareToIgnoreCase("activity") == 0) {
                jtemp.append("data", jobj);
            } else if (obj.type.compareToIgnoreCase("process") == 0) {
                jtemp.append("processes", jobj);
            } else if (obj.type.compareToIgnoreCase("lane") == 0) {
                jtemp.append("lanes", jobj);
            }
        }

        NodeList transitionList = doc.getElementsByTagName("Transitions");
        for (int i = 0; i < transitionList.getLength(); i++) {
            Node node = transitionList.item(i);
            NodeList childrenList = node.getChildNodes();

            for (int cnt = 0; cnt < childrenList.getLength(); cnt++) {
                node = childrenList.item(cnt);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    JSONObject jobj = new com.krawler.utils.json.base.JSONObject();
                    NamedNodeMap attr = node.getAttributes();
                    for (int b = 0; b < attr.getLength(); b++) {
                        Node attribute = attr.item(b);
                        name = attribute.getNodeName();
                        if (name.compareToIgnoreCase("From") == 0) {
                            jobj.put("fromId", attribute.getNodeValue());
                        } else if (name.compareToIgnoreCase("To") == 0) {
                            jobj.put("toId", attribute.getNodeValue());
                        }
                    }
                    jtemp.append("Lines", jobj);
                }
            }
        }

        return jtemp.toString();
    } catch (ParserConfigurationException ex) {
        logger.warn(ex.getMessage(), ex);
        result = "{\"success\":false}";
        throw ServiceException.FAILURE("workflow.reloadWorkflow", ex);
    } catch (SAXException ex) {
        logger.warn(ex.getMessage(), ex);
        result = "{\"success\":false}";
        throw ServiceException.FAILURE("workflow.reloadWorkflow", ex);
    } catch (IOException ex) {
        logger.warn(ex.getMessage(), ex);
        result = "{\"success\":false}";
        throw ServiceException.FAILURE("workflow.reloadWorkflow", ex);
    } catch (JSONException ex) {
        logger.warn(ex.getMessage(), ex);
        result = "{\"success\":false}";
        throw ServiceException.FAILURE("workflow.reloadWorkflow", ex);
    }
}

From source file:com.esri.geoevent.solutions.adapter.cot.CoTAdapterInbound.java

private String getAttribute(Node node, String attributeName) throws Exception {
    try {//w w  w  .  j  ava  2  s  .  c o m
        NamedNodeMap attributes = node.getAttributes();
        for (int i = 0; i < attributes.getLength(); i++) {
            Node attributeNode = attributes.item(i);
            if (attributeNode.getNodeName().equals(attributeName)) {
                return attributeNode.getNodeValue();
            }
        }
        return null;
    } catch (Exception e) {
        log.error(e);
        log.error(e.getStackTrace());
        throw (e);
    }
}

From source file:com.krawler.workflow.bizservice.WorkflowServiceImpl.java

public String importWorkflow(String processid) throws ServiceException {
    String result = "{\"success\":false}";
    try {/*from  w w  w.j  ava 2  s  .c  om*/

        DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
        DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();

        String path = ConfigReader.getinstance().get("workflowpath") + processid;

        File fdir = new File(path);
        File file = new File(fdir + System.getProperty("file.separator") + "bpmn.xml");
        Document doc = docBuilder.parse(file);
        int s;
        int a;
        String name = "";
        ObjectInfo obj = new ObjectInfo();

        HashMap<String, ObjectInfo> activityHm = new HashMap<String, ObjectInfo>();
        NodeList nodeList = doc.getElementsByTagName("Activity");

        for (s = 0; s < nodeList.getLength(); s++) {
            name = "";
            Node node = nodeList.item(s);

            if (node.getNodeType() == Node.ELEMENT_NODE) {
                obj = new ObjectInfo();
                obj.type = getNodeType(node);
                getNodeInfo(node, obj);
                if (obj.type.equals("activity")) {
                    Node graphicsInfoNode = getActivityNode(node, 1);
                    getGraphicsNodeInfo(graphicsInfoNode, obj);
                }
                activityHm.put(obj.objId, obj);
            }

        }

        NodeList transitionList = doc.getElementsByTagName("Transitions");
        String fromId = "";
        String toId = "";
        ObjectInfo fromObj;
        ObjectInfo toObj;
        ObjectInfo tempObj;
        JSONObject jobj;
        JSONObject jtemp = new com.krawler.utils.json.base.JSONObject();
        HashMap<String, String> fromConditionHm = new HashMap<String, String>();
        MultiMap toConditionHm = new MultiHashMap();
        for (int i = 0; i < transitionList.getLength(); i++) {
            Node node = transitionList.item(i);
            NodeList childrenList = node.getChildNodes();
            for (int cnt = 0; cnt < childrenList.getLength(); cnt++) {
                node = childrenList.item(cnt);
                if (node.getNodeType() == Node.ELEMENT_NODE) {
                    NamedNodeMap attr = node.getAttributes();
                    for (int b = 0; b < attr.getLength(); b++) {
                        Node attribute = attr.item(b);
                        name = attribute.getNodeName();
                        if (name.compareToIgnoreCase("From") == 0) {
                            fromId = attribute.getNodeValue();
                        } else if (name.compareToIgnoreCase("To") == 0) {
                            toId = attribute.getNodeValue();
                        }
                    }
                    fromObj = activityHm.get(fromId);
                    toObj = activityHm.get(toId);
                    if (fromObj.type.equals("start")) {
                        tempObj = new ObjectInfo();
                        tempObj = activityHm.get(toId);
                        tempObj.hasStart = "true";
                        activityHm.put(toId, tempObj);
                        continue;
                    }
                    if (toObj.type.equals("end")) {
                        tempObj = new ObjectInfo();
                        tempObj = activityHm.get(fromId);
                        tempObj.hasEnd = "true";
                        activityHm.put(fromId, tempObj);
                        continue;
                    }
                    if (fromObj.type.equals("activity") && toObj.type.equals("activity")) {
                        jobj = new com.krawler.utils.json.base.JSONObject();
                        jobj.put("fromId", "flowPanel" + fromId);
                        jobj.put("toId", "flowPanel" + toId);
                        jtemp.append("Lines", jobj);
                        tempObj = new ObjectInfo();
                        tempObj = activityHm.get(fromId);
                        tempObj.derivationRule = "sequence";
                        activityHm.put(fromId, tempObj);
                        continue;
                    }
                    if (fromObj.type.equals("activity") && toObj.type.equals("condition")) {
                        fromConditionHm.put(toId, fromId);
                        tempObj = new ObjectInfo();
                        tempObj = activityHm.get(fromId);
                        tempObj.derivationRule = "evaluation";
                        activityHm.put(fromId, tempObj);
                        continue;
                    }
                    if (fromObj.type.equals("condition") && toObj.type.equals("activity")) {
                        toConditionHm.put(fromId, toId);
                        continue;
                    }
                }
            }
        }

        Set keys = activityHm.keySet();
        Iterator ite = keys.iterator();
        while (ite.hasNext()) {
            String key = (String) ite.next();
            obj = new ObjectInfo();
            obj = activityHm.get(key);
            if (obj.type.equals("activity")) {
                jobj = new com.krawler.utils.json.base.JSONObject();
                jobj.put("Id", "flowPanel" + obj.objId);
                jobj.put("name", obj.name);
                jobj.put("xpos", obj.xpos);
                jobj.put("ypos", obj.ypos);
                jobj.put("height", obj.height);
                jobj.put("width", obj.width);
                jobj.put("parent", obj.parentId);
                jobj.put("refId", obj.refId);
                jobj.put("hasStart", obj.hasStart);
                jobj.put("hasEnd", obj.hasEnd);
                jobj.put("startRefId", obj.startRefId);
                jobj.put("endRefId", obj.endRefId);
                jobj.put("derivationRule", obj.derivationRule);
                jobj.put("domEl", obj.domEl);
                jtemp.append("data", jobj);
            }
        }

        keys = fromConditionHm.keySet();
        ite = keys.iterator();
        Iterator ite1 = null;
        String key = "";
        while (ite.hasNext()) {
            key = (String) ite.next();
            fromId = fromConditionHm.get(key);
            List toList = (List) toConditionHm.get(key);
            ite1 = toList.iterator();
            while (ite1.hasNext()) {
                toId = (String) ite1.next();
                jobj = new com.krawler.utils.json.base.JSONObject();
                jobj.put("fromId", "flowPanel" + fromId);
                jobj.put("toId", "flowPanel" + toId);
                jtemp.append("Lines", jobj);
            }
        }
        return jtemp.toString();
    } catch (ParserConfigurationException ex) {
        logger.warn(ex.getMessage(), ex);
        result = "{\"success\":false}";
        throw ServiceException.FAILURE("workflow.reloadWorkflow", ex);
    } catch (SAXException ex) {
        logger.warn(ex.getMessage(), ex);
        result = "{\"success\":false}";
        throw ServiceException.FAILURE("workflow.reloadWorkflow", ex);
    } catch (IOException ex) {
        logger.warn(ex.getMessage(), ex);
        result = "{\"success\":false}";
        throw ServiceException.FAILURE("workflow.reloadWorkflow", ex);
    } catch (JSONException ex) {
        logger.warn(ex.getMessage(), ex);
        result = "{\"success\":false}";
        throw ServiceException.FAILURE("workflow.reloadWorkflow", ex);
    }
}