Example usage for org.w3c.dom Node hasAttributes

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

Introduction

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

Prototype

public boolean hasAttributes();

Source Link

Document

Returns whether this node (if it is an element) has any attributes.

Usage

From source file:org.wso2.carbon.apimgt.impl.soaptorest.WSDL11SOAPOperationExtractor.java

private void traverseTypeElement(Node element, Node prevNode, ModelImpl model, Property currentProp) {

    if (log.isDebugEnabled()) {
        if (element.hasAttributes()
                && element.getAttributes().getNamedItem(SOAPToRESTConstants.NAME_ATTRIBUTE) != null) {
            log.debug(element.getNodeName() + " with name attr:"
                    + element.getAttributes().getNamedItem(SOAPToRESTConstants.NAME_ATTRIBUTE) + " and "
                    + prevNode);//  w  ww .j ava  2 s  .  c  o m
        } else {
            log.debug(element.getNodeName() + " and " + prevNode);
        }
    }
    if (prevNode != null) {
        currentProperty = generateSwaggerModelForComplexType(element, model, currentProp);
        setNamespaceDetails(model, element);
    } else {
        currentProperty = generateSwaggerModelForComplexType(element, model, currentProp);
        setNamespaceDetails(model, element);
    }
    NodeList nodeList = element.getChildNodes();
    if (nodeList != null) {
        for (int i = 0; i < nodeList.getLength(); i++) {
            Node currentNode = nodeList.item(i);
            if (currentNode.getNodeType() == Node.ELEMENT_NODE) {
                traverseTypeElement(currentNode, prevNode, model, currentProperty);
            }
            prevNode = element;
        }
    }
}

From source file:org.wso2.carbon.apimgt.impl.soaptorest.WSDL11SOAPOperationExtractor.java

private String getXPath(Node node) {

    if (node != null) {
        Node parent = node.getParentNode();
        if (parent == null && node.hasAttributes()
                && node.getAttributes().getNamedItem(SOAPToRESTConstants.NAME_ATTRIBUTE) != null) {
            return "/" + node.getAttributes().getNamedItem(SOAPToRESTConstants.NAME_ATTRIBUTE).getNodeValue();
        }/*  w w w .  java 2 s. c o  m*/
        if (node.hasAttributes()
                && node.getAttributes().getNamedItem(SOAPToRESTConstants.NAME_ATTRIBUTE) != null) {
            return getXPath(parent) + "/"
                    + node.getAttributes().getNamedItem(SOAPToRESTConstants.NAME_ATTRIBUTE).getNodeValue();
        } else if (node.hasAttributes()
                && node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE) != null) {
            return getXPath(parent) + "/"
                    + node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE).getNodeValue();
        }
        return getXPath(parent) + "/";
    }
    return SOAPToRESTConstants.EMPTY_STRING;
}

From source file:org.wso2.carbon.apimgt.impl.soaptorest.WSDL11SOAPOperationExtractor.java

private String getNodeName(Node node) {

    if (node.hasAttributes() && node.getAttributes().getNamedItem(SOAPToRESTConstants.NAME_ATTRIBUTE) != null) {
        return node.getAttributes().getNamedItem(SOAPToRESTConstants.NAME_ATTRIBUTE).getNodeValue();
    }/*ww  w  .  ja  va  2 s . co  m*/
    if (node.hasAttributes() && node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE) != null) {
        return node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE).getNodeValue().contains(":")
                ? node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE).getNodeValue()
                        .split(":")[1]
                : node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE).getNodeValue();
    }
    return SOAPToRESTConstants.EMPTY_STRING;
}

From source file:org.wso2.carbon.apimgt.impl.soaptorest.WSDL11SOAPOperationExtractor.java

private String getRefNodeName(Node node) {
    if (node.hasAttributes() && node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE) != null) {
        return node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE).getNodeValue().contains(":")
                ? node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE).getNodeValue()
                        .split(":")[1]
                : node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE).getNodeValue();
    }//from w  w w.  j  a va 2  s  .c  om
    return SOAPToRESTConstants.EMPTY_STRING;
}

From source file:org.wso2.carbon.apimgt.impl.soaptorest.WSDL11SOAPOperationExtractor.java

/**
 * Creates a swagger property from given wsdl node.
 *
 * @param node wsdl node//from w w w  .ja v  a2 s .  c om
 * @return generated swagger property
 */
private Property createPropertyFromNode(Node node) {

    Property property = null;
    if (node.hasAttributes()) {
        if (node.getAttributes().getNamedItem(SOAPToRESTConstants.TYPE_ATTRIBUTE) != null) {
            if (node.getAttributes().getNamedItem(SOAPToRESTConstants.NAME_ATTRIBUTE) != null) {
                String dataType = node.getAttributes().getNamedItem(SOAPToRESTConstants.TYPE_ATTRIBUTE)
                        .getNodeValue().contains(":")
                                ? node.getAttributes().getNamedItem(SOAPToRESTConstants.TYPE_ATTRIBUTE)
                                        .getNodeValue().split(":")[1]
                                : node.getAttributes().getNamedItem(SOAPToRESTConstants.TYPE_ATTRIBUTE)
                                        .getNodeValue();
                property = getPropertyFromDataType(dataType);
                if (property instanceof RefProperty) {
                    ((RefProperty) property).set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + dataType);
                }
                property.setName(
                        node.getAttributes().getNamedItem(SOAPToRESTConstants.NAME_ATTRIBUTE).getNodeValue());
            }
        } else if (node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE) != null) {
            property = new RefProperty();
            String dataType = node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE)
                    .getNodeValue().contains(":")
                            ? node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE)
                                    .getNodeValue().split(":")[1]
                            : node.getAttributes().getNamedItem(SOAPToRESTConstants.REF_ATTRIBUTE)
                                    .getNodeValue();
            ((RefProperty) property).set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + dataType);
            property.setName(dataType);
        } else if (node.getAttributes().getNamedItem("base") != null) {
            String dataType = node.getAttributes().getNamedItem("base").getNodeValue().contains(":")
                    ? node.getAttributes().getNamedItem("base").getNodeValue().split(":")[1]
                    : node.getAttributes().getNamedItem("base").getNodeValue();
            property = getPropertyFromDataType(dataType);
            if (property instanceof RefProperty) {
                ((RefProperty) property).set$ref(SOAPToRESTConstants.Swagger.DEFINITIONS_ROOT + dataType);
            }
            property.setName(dataType);
        } else if (node.getAttributes().getNamedItem(SOAPToRESTConstants.NAME_ATTRIBUTE) != null) {
            property = new ObjectProperty();
            property.setName(getNodeName(node));
        }
        if (isArrayType(node)) {
            Property arrayProperty = new ArrayProperty();
            ((ArrayProperty) arrayProperty).setItems(property);
            return arrayProperty;
        }
    }
    return property;
}

From source file:org.wso2.developerstudio.eclipse.esb.presentation.ui.XPathSelectorDialog.java

/**
 * Initialize action listeners./*  w w w . j  a  va2s .c  o  m*/
 */
private void initializeActions() {
    treeViewWidget.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            TreeItemData treeItemData = (TreeItemData) event.item.getData(TREE_ITEM_DATA_KEY);
            xpathTextField.setText(XSLTXPathHelper.calculateXPathToNode(treeItemData.getDomNode()));
        }
    });

    treeViewWidget.addListener(SWT.Expand, new Listener() {
        public void handleEvent(Event event) {
            TreeItem currentItem = (TreeItem) event.item;
            TreeItemData treeItemData = (TreeItemData) currentItem.getData(TREE_ITEM_DATA_KEY);

            // Explore if not already explored.
            if (!treeItemData.isMarkedAsExplored()) {
                // Make sure to remove the dummy child item.
                currentItem.removeAll();

                Node node = treeItemData.getDomNode();

                // Attributes.
                if (node.hasAttributes()) {
                    NamedNodeMap attributesMap = node.getAttributes();
                    for (int i = 0; i < attributesMap.getLength(); i++) {
                        Node childNode = attributesMap.item(i);
                        addTreeItem(currentItem, childNode);
                    }
                }

                // Children.
                if (node.hasChildNodes()) {
                    NodeList childNodes = node.getChildNodes();
                    for (int i = 0; i < childNodes.getLength(); i++) {
                        Node childNode = childNodes.item(i);
                        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                            addTreeItem(currentItem, childNode);
                        }
                    }
                }

                // Done exploring.
                treeItemData.setMarkedAsExplored();
            }
        }
    });

    treeViewWidget.addMouseListener(new MouseListener() {
        public void mouseUp(MouseEvent e) {
        }

        public void mouseDown(MouseEvent e) {
        }

        public void mouseDoubleClick(MouseEvent e) {
            TreeItem[] selection = treeViewWidget.getSelection();
            if (selection.length > 0) {
                Node selectedNode = ((TreeItemData) selection[0].getData(TREE_ITEM_DATA_KEY)).getDomNode();
                setSelectedXpath(XSLTXPathHelper.calculateXPathToNode(selectedNode));
                dialogShell.dispose();
            }
        }
    });

    okButton.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            setSelectedXpath(xpathTextField.getText());
            dialogShell.dispose();
        }
    });

    cancelButton.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event arg0) {
            dialogShell.dispose();
        }
    });

    browseButton.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            // Configure file dialog.
            FileDialog fileBrowserDialog = new FileDialog(dialogShell, SWT.OPEN);
            fileBrowserDialog.setText("Select XML Document");
            fileBrowserDialog.setFilterExtensions(new String[] { "*.xml" });

            // Let the user browse for the input xml file.
            final String filePath = fileBrowserDialog.open();

            // If the user selected a file.
            if (!StringUtils.isBlank(filePath)) {
                // Clear tree view.
                treeViewWidget.removeAll();

                // Update the selected path
                filePathTextField.setText(filePath);

                final File selectedFile = new File(filePath);
                if (selectedFile.exists() && selectedFile.canRead()) {
                    // Create a new runnable that can be executed with a progress bar.
                    IRunnableWithProgress runnable = new IRunnableWithProgress() {
                        public void run(IProgressMonitor monitor)
                                throws InvocationTargetException, InterruptedException {
                            // We have no clue how long this would take.
                            monitor.beginTask("Parsing xml document...", IProgressMonitor.UNKNOWN);

                            // Parse the xml document into a dom object.
                            try {
                                Document document = EsbUtils.parseDocument(selectedFile);
                                final Node rootNode = document.getDocumentElement();

                                // Adding a tree item is a ui related
                                // operation which must be executed on the
                                // ui thread. Since we are currently
                                // executing inside a background thread, we
                                // need to explicitly invoke the ui thread.
                                new UIJob("add-root-tree-item-job") {
                                    public IStatus runInUIThread(IProgressMonitor monitor) {
                                        addTreeItem(null, rootNode);
                                        addNameSpaces(rootNode);
                                        return Status.OK_STATUS;
                                    }
                                }.schedule();

                                // Done.
                                monitor.done();
                            } catch (Exception ex) {
                                // Stop progress.
                                monitor.done();

                                // Report error.
                                throw new InvocationTargetException(ex);
                            }
                        }
                    };

                    // Run the operation within a progress monitor dialog,
                    // make sure to fork-off from the ui thread so that we
                    // don't cause the ui to hang.
                    try {
                        new ProgressMonitorDialog(dialogShell).run(true, false, runnable);
                    } catch (Exception ex) {
                        MessageDialog.openError(dialogShell, "Parse Error",
                                "Error while parsing specified xml file.");
                    }
                } else {
                    MessageDialog.openError(dialogShell, "I/O Error", "Unable to read specified input file.");
                }
            }
        }
    });
}

From source file:org.wso2.developerstudio.eclipse.gmf.esb.diagram.custom.configure.ui.XPathSelectorDialog.java

/**
 * Initialize action listeners.// www.j a v a 2s .c  o  m
 */
private void initializeActions() {
    treeViewWidget.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            TreeItemData treeItemData = (TreeItemData) event.item.getData(TREE_ITEM_DATA_KEY);
            xpathTextField.setText(XSLTXPathHelper.calculateXPathToNode(treeItemData.getDomNode()));
        }
    });

    treeViewWidget.addListener(SWT.Expand, new Listener() {
        public void handleEvent(Event event) {
            TreeItem currentItem = (TreeItem) event.item;
            TreeItemData treeItemData = (TreeItemData) currentItem.getData(TREE_ITEM_DATA_KEY);

            // Explore if not already explored.
            if (!treeItemData.isMarkedAsExplored()) {
                // Make sure to remove the dummy child item.
                currentItem.removeAll();

                Node node = treeItemData.getDomNode();

                // Attributes.
                if (node.hasAttributes()) {
                    NamedNodeMap attributesMap = node.getAttributes();
                    for (int i = 0; i < attributesMap.getLength(); i++) {
                        Node childNode = attributesMap.item(i);
                        addTreeItem(currentItem, childNode);
                    }
                }

                // Children.
                if (node.hasChildNodes()) {
                    NodeList childNodes = node.getChildNodes();
                    for (int i = 0; i < childNodes.getLength(); i++) {
                        Node childNode = childNodes.item(i);
                        if (childNode.getNodeType() == Node.ELEMENT_NODE) {
                            addTreeItem(currentItem, childNode);
                        }
                    }
                }

                // Done exploring.
                treeItemData.setMarkedAsExplored();
            }
        }
    });

    treeViewWidget.addMouseListener(new MouseListener() {
        public void mouseUp(MouseEvent e) {
        }

        public void mouseDown(MouseEvent e) {
        }

        public void mouseDoubleClick(MouseEvent e) {
            TreeItem[] selection = treeViewWidget.getSelection();
            if (selection.length > 0) {
                Node selectedNode = ((TreeItemData) selection[0].getData(TREE_ITEM_DATA_KEY)).getDomNode();
                setSelectedXpath(XSLTXPathHelper.calculateXPathToNode(selectedNode));
                dialogShell.dispose();
            }
        }
    });

    okButton.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            setSelectedXpath(xpathTextField.getText());
            dialogShell.dispose();
        }
    });

    cancelButton.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event arg0) {
            dialogShell.dispose();
        }
    });

    browseButton.addListener(SWT.Selection, new Listener() {
        public void handleEvent(Event event) {
            // Configure file dialog.
            FileDialog fileBrowserDialog = new FileDialog(dialogShell, SWT.OPEN);
            fileBrowserDialog.setText("Select XML Document");
            fileBrowserDialog.setFilterExtensions(new String[] { "*.xml" });

            // Let the user browse for the input xml file.
            final String filePath = fileBrowserDialog.open();

            // If the user selected a file.
            if (!StringUtils.isBlank(filePath)) {
                // Clear tree view.
                treeViewWidget.removeAll();

                // Update the selected path
                filePathTextField.setText(filePath);

                final File selectedFile = new File(filePath);
                if (selectedFile.exists() && selectedFile.canRead()) {
                    // Create a new runnable that can be executed with a
                    // progress bar.
                    IRunnableWithProgress runnable = new IRunnableWithProgress() {
                        public void run(IProgressMonitor monitor)
                                throws InvocationTargetException, InterruptedException {
                            // We have no clue how long this would take.
                            monitor.beginTask("Parsing xml document...", IProgressMonitor.UNKNOWN);

                            // Parse the xml document into a dom object.
                            try {
                                Document document = parseDocument(selectedFile);
                                final Node rootNode = document.getDocumentElement();

                                // Adding a tree item is a ui related
                                // operation which must be executed on the
                                // ui thread. Since we are currently
                                // executing inside a background thread, we
                                // need to explicitly invoke the ui thread.
                                new UIJob("add-root-tree-item-job") {
                                    public IStatus runInUIThread(IProgressMonitor monitor) {
                                        addTreeItem(null, rootNode);
                                        addNameSpaces(rootNode);
                                        return Status.OK_STATUS;
                                    }
                                }.schedule();

                                // Done.
                                monitor.done();
                            } catch (Exception ex) {
                                // Stop progress.
                                monitor.done();

                                // Report error.
                                throw new InvocationTargetException(ex);
                            }
                        }
                    };

                    // Run the operation within a progress monitor dialog,
                    // make sure to fork-off from the ui thread so that we
                    // don't cause the ui to hang.
                    try {
                        new ProgressMonitorDialog(dialogShell).run(true, false, runnable);
                    } catch (Exception ex) {
                        MessageDialog.openError(dialogShell, "Parse Error",
                                "Error while parsing specified xml file.");
                    }
                } else {
                    MessageDialog.openError(dialogShell, "I/O Error", "Unable to read specified input file.");
                }
            }
        }
    });
}

From source file:psidev.psi.mi.filemakers.xmlFlattener.structure.XsdTreeStructImpl.java

private void getKeyNodes(String keyName, String keySelector, String keyField) {

    if (document == null)
        return;/*www  .  j  a v  a  2 s  .  c o  m*/

    String[] path = keySelector.split("/");

    /* the node that contains all keys */
    /* find the parent node */
    Node nodeContainer = getContainer(document, path, 0);

    if (nodeContainer == null)
        return;
    log.debug("found list of refered node: " + nodeContainer.getNodeName());

    for (int i = 0; i < nodeContainer.getChildNodes().getLength(); i++) {
        Node child = nodeContainer.getChildNodes().item(i);
        String name = child.getNodeName();
        /* get refId name */
        String idFieldName = getReferedIdFieldName(name);
        if (child.hasAttributes()) {
            for (int j = 0; j < child.getAttributes().getLength(); j++) {
                if (child.getAttributes().item(j).getNodeName().equals(idFieldName)) {
                    String ref = child.getAttributes().item(j).getNodeValue();
                    xsKeyNodes.put(keyName + "#" + ref, child);// keyName
                    log.debug("add: " + keyName + "#" + ref);
                }
            }
        }

    }

}

From source file:psidev.psi.mi.filemakers.xmlFlattener.structure.XsdTreeStructImpl.java

private void buidKeyMaps() {

    log.debug("get keys");
    for (Node node : keyz) {
        String keyName = null;/*from  ww w.j a v  a 2  s. c  o m*/
        String keySelector = null;
        String keyField = null;

        if (node.hasAttributes()) {
            for (int i = 0; i < node.getAttributes().getLength(); i++) {
                if (node.getAttributes().item(i).getNodeName().equals("name")) {
                    keyName = node.getAttributes().item(i).getNodeValue();

                }
            }
        }

        for (int i = 0; i < node.getChildNodes().getLength(); i++) {
            Node child = node.getChildNodes().item(i);

            if (child.getNodeName().equals("xs:selector")) {
                for (int j = 0; j < child.getAttributes().getLength(); j++) {
                    if (child.getAttributes().item(j).getNodeName().equals("xpath")) {
                        keySelector = getXpath(child.getParentNode().getParentNode()) + "/"
                                + child.getAttributes().item(j).getNodeValue();
                    }
                }
            } else if (child.getNodeName().equals("xs:field")) {
                for (int j = 0; j < child.getAttributes().getLength(); j++) {
                    if (child.getAttributes().item(j).getNodeName().equals("xpath")) {
                        keyField = child.getAttributes().item(j).getNodeValue().replace("@", "");
                    }
                }
            }
        }
        getKeyNodes(keyName, keySelector, keyField);

    }

    log.debug("get keyRefs");
    for (Node node : keyRefs) {
        String keyRefName = null;
        String keyRefRefer = null;
        String keyRefSelector = null;
        String keyRefField = null;

        if (node.hasAttributes()) {
            for (int i = 0; i < node.getAttributes().getLength(); i++) {
                if (node.getAttributes().item(i).getNodeName().equals("name")) {
                    keyRefName = node.getAttributes().item(i).getNodeValue();
                } else if (node.getAttributes().item(i).getNodeName().equals("refer")) {
                    keyRefRefer = node.getAttributes().item(i).getNodeValue();
                }
            }
        }
        for (int i = 0; i < node.getChildNodes().getLength(); i++) {
            Node child = node.getChildNodes().item(i);
            if (child.getNodeName().equals("xs:selector")) {
                for (int j = 0; j < child.getAttributes().getLength(); j++) {
                    if (child.getAttributes().item(j).getNodeName().equals("xpath")) {
                        keyRefSelector = child.getAttributes().item(j).getNodeValue();
                    }
                }
            } else if (child.getNodeName().equals("xs:field")) {
                for (int j = 0; j < child.getAttributes().getLength(); j++) {
                    if (child.getAttributes().item(j).getNodeName().equals("xpath")) {
                        keyRefField = child.getAttributes().item(j).getNodeValue();
                    }
                }
            }
        }
        refType2referedType.put(getSchemaXpath(node.getParentNode()) + "/" + keyRefSelector, keyRefRefer);
    }
}

From source file:psidev.psi.mi.filemakers.xmlFlattener.structure.XsdTreeStructImpl.java

/**
 * the name of an element in the schema is contained in the attribute 'name'
 * /*ww  w .j a  v a 2 s . c o  m*/
 * @param node
 * @return
 */
private String getName(Node node) {
    if (node.hasAttributes() == false)
        return null;

    for (int i = 0; i < node.getAttributes().getLength(); i++) {
        if (node.getAttributes().item(i).getNodeName().equals("name"))
            return node.getAttributes().item(i).getNodeValue();
    }
    return null;
}