List of usage examples for org.w3c.dom Node hasChildNodes
public boolean hasChildNodes();
From source file:org.wso2.developerstudio.eclipse.esb.presentation.ui.XPathSelectorDialog.java
/** * Initialize action listeners.// w w w . j av a 2 s. 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.esb.presentation.ui.XPathSelectorDialog.java
/** * Utility method for adding a child {@link TreeItem} into the specified * parent {@link TreeItem}.//from w w w. j av a 2 s. c o m * * @param parent parent {@link TreeItem} or null if this is root. * @param node dom {@link Node} to be associated with this {@link TreeItem}. */ private void addTreeItem(TreeItem parent, Node node) { TreeItem childTreeItem; if (null == parent) { childTreeItem = new TreeItem(treeViewWidget, SWT.NONE); } else { childTreeItem = new TreeItem(parent, SWT.NONE); } childTreeItem.setText(node.getNodeName()); // Select the icon based on the node type. String iconPath = (node.getNodeType() == Node.ELEMENT_NODE) ? ELEMENT_ICON_PATH : ATTRIBUTE_ICON_PATH; try { URL imageUrl = new URL(EsbEditorPlugin.getPlugin().getBaseURL(), iconPath); childTreeItem.setImage(ImageDescriptor.createFromURL(imageUrl).createImage()); } catch (MalformedURLException ex) { // TODO: Log. } // Associated dom node with the newly created tree item. childTreeItem.setData(TREE_ITEM_DATA_KEY, new TreeItemData(node)); // Here we determine if a dummy grand-child should be added into the // newly created child item. The grand-child is needed so that the // expand handle (little triangle besides the tree item) appears and the // user is able to expand the node (upon which event we explore the rest // of the tree dynamically). boolean hasChildren = (node.getNodeType() == Node.ELEMENT_NODE) ? node.hasChildNodes() : false; if (hasChildren) { TreeItem grandChild = new TreeItem(childTreeItem, SWT.NONE); grandChild.setText("Working..."); } }
From source file:org.wso2.developerstudio.eclipse.gmf.esb.diagram.custom.configure.ui.XPathSelectorDialog.java
/** * Initialize action listeners.// w w w . j av a 2 s.com */ 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:org.wso2.developerstudio.eclipse.gmf.esb.diagram.custom.configure.ui.XPathSelectorDialog.java
/** * Utility method for adding a child {@link TreeItem} into the specified * parent {@link TreeItem}./*from w ww . java 2s. c o m*/ * * @param parent * parent {@link TreeItem} or null if this is root. * @param node * dom {@link Node} to be associated with this {@link TreeItem}. */ private void addTreeItem(TreeItem parent, Node node) { TreeItem childTreeItem; if (null == parent) { childTreeItem = new TreeItem(treeViewWidget, SWT.NONE); } else { childTreeItem = new TreeItem(parent, SWT.NONE); } childTreeItem.setText(node.getNodeName()); // Select the icon based on the node type. ImageDescriptor icon; if (node.getNodeType() == Node.ELEMENT_NODE) { icon = EsbDiagramEditorPlugin.getBundledImageDescriptor(ELEMENT_ICON_PATH); } else { icon = EsbDiagramEditorPlugin.getBundledImageDescriptor(ATTRIBUTE_ICON_PATH); } childTreeItem.setImage(icon.createImage()); // Associated dom node with the newly created tree item. childTreeItem.setData(TREE_ITEM_DATA_KEY, new TreeItemData(node)); // Here we determine if a dummy grand-child should be added into the // newly created child item. The grand-child is needed so that the // expand handle (little triangle besides the tree item) appears and the // user is able to expand the node (upon which event we explore the rest // of the tree dynamically). boolean hasChildren = (node.getNodeType() == Node.ELEMENT_NODE) ? node.hasChildNodes() : false; if (hasChildren) { TreeItem grandChild = new TreeItem(childTreeItem, SWT.NONE); grandChild.setText("Working..."); } }
From source file:org.yawlfoundation.yawl.util.DOMUtil.java
/** * Extracts to text from the supplied node and it's children. * * @param node to extract text from.// w ww. ja va2 s . c o m * @return String representation of the node text. */ public static String getNodeText(Node node) { if (node == null || !node.hasChildNodes()) return ""; StringBuilder result = new StringBuilder(); NodeList list = node.getChildNodes(); for (int i = 0; i < list.getLength(); i++) { Node subnode = list.item(i); if (subnode.getNodeType() == Node.TEXT_NODE) { result.append(subnode.getNodeValue()); } else if (subnode.getNodeType() == Node.CDATA_SECTION_NODE) { result.append(subnode.getNodeValue()); } else if (subnode.getNodeType() == Node.ENTITY_REFERENCE_NODE) { // Recurse into the subtree for text // (and ignore comments) result.append(getNodeText(subnode)); } } return result.toString(); }
From source file:org.yawlfoundation.yawl.util.DOMUtil.java
/** * Takes the supplied node and recursively removes empty (no content/child nodes) elements * * @param node to remove all empty elements from * @return Trimmed node// ww w. j ava 2 s .com * @deprecated * @throws XPathExpressionException */ public static Node removeEmptyElements(Node node) throws XPathExpressionException { NodeList list = selectNodeList(node, "*[string-length(normalize-space(.)) = 0]"); for (int i = 0; i < list.getLength(); i++) { node.removeChild(list.item(i)); } if (node.hasChildNodes()) { NodeList childs = node.getChildNodes(); for (int i = 0; i < childs.getLength(); i++) { if (childs.item(i) instanceof Element) { removeEmptyElements(childs.item(i)); } } } return node; }
From source file:oscar.oscarMessenger.docxfer.util.MsgCommxml.java
public static String getText(Node node) { String ret = ""; if (node.hasChildNodes()) { int i;//w w w .j a v a 2s . c om for (i = 0; i < node.getChildNodes().getLength(); i++) { Node sub = node.getChildNodes().item(i); if (sub.getNodeType() == Node.TEXT_NODE) { if (sub.getNodeValue() != null) { ret += sub.getNodeValue(); } } } } return ret; }
From source file:pl.psnc.synat.wrdz.common.metadata.mets.MetsMetadataBuilder.java
/** * Clears whitespaces between elements in XML document node. JAXB cannot do it because it does not known all context * - we don't restrict aou system to specific XML metadata namespaces. * /*from ww w. j a v a2s . co m*/ * @param node * node for which clear whitespaces */ private void clearWhitespacesBetweenElements(Node node) { if (node.hasChildNodes()) { NodeList childNodes = node.getChildNodes(); if (!(childNodes.getLength() == 1 && childNodes.item(0).getNodeType() != Node.ELEMENT_NODE)) { for (int i = 0; i < childNodes.getLength(); i++) { clearWhitespacesBetweenElements(childNodes.item(i)); } } } else { node.setNodeValue(""); } }
From source file:psidev.psi.mi.filemakers.xmlFlattener.structure.XsdTreeStructImpl.java
private Node getContainer(Node node, String[] path, int startIdx) { if (startIdx == path.length - 1) return node; if (false == node.hasChildNodes()) { return null; }/*from w ww . j a va2 s . co m*/ for (int j = 0; j < node.getChildNodes().getLength(); j++) { if (node.getChildNodes().item(j).getNodeName().equals(path[startIdx])) { return getContainer(node.getChildNodes().item(j), path, ++startIdx); } } return null; }
From source file:renderer.DependencyGrapher.java
private DependencyDirectedSparceMultiGraph<String, Number> createGraph() { DependencyDirectedSparceMultiGraph<String, Number> g = new DependencyDirectedSparceMultiGraph<String, Number>(); DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder db;//from w w w . j a v a2 s.c o m try { db = dbf.newDocumentBuilder(); Document doc = db.parse(_dependencyTree); doc.getDocumentElement().normalize(); NodeList nodes = doc.getElementsByTagName(NODE_DEPLOY); for (int i = 0; i < nodes.getLength(); i++) { Node nodeFrom = nodes.item(i); String vFrom = nodeFrom.getAttributes().getNamedItem(ATTR_NODE_TYPE).getNodeValue(); g.addVertex(vFrom); if (nodeFrom.hasChildNodes()) { NodeList childNodes = nodeFrom.getChildNodes(); for (int j = 0; j < childNodes.getLength(); j++) { Node nodeTo = childNodes.item(j); if (nodeTo.getNodeName().equals("dependency")) { Attributes attr; String attrName = null; boolean isRequired; String vTo = nodeTo.getAttributes().getNamedItem(ATTR_NODE_TYPE).getNodeValue(); if (nodeTo.getAttributes().getNamedItem(ATTR_DEP_ATTRIBUTE) != null) { attrName = nodeTo.getAttributes().getNamedItem(ATTR_DEP_ATTRIBUTE).getNodeValue(); } if (nodeTo.getAttributes().getNamedItem(ATTR_DEP_ISREQUIRED) != null) { isRequired = Boolean.parseBoolean( nodeTo.getAttributes().getNamedItem(ATTR_DEP_ISREQUIRED).getNodeValue()); } else { isRequired = false; } createEdge(g, vFrom, vTo, new Attributes(attrName, isRequired)); } } } } } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } return g; }