Example usage for org.dom4j Node getPath

List of usage examples for org.dom4j Node getPath

Introduction

In this page you can find the example usage for org.dom4j Node getPath.

Prototype

String getPath();

Source Link

Document

Returns the XPath expression which will return a node set containing the given node such as /a/b/@c.

Usage

From source file:bard.pubchem.xml.PubChemXMLParserFactory.java

License:Open Source License

protected PCAssay populateAssayFromXMLNode(Node topNode) throws PubChemException {
    // String assayDescPath =
    // "PC-AssaySubmit_assay/PC-AssaySubmit_assay_descr/PC-AssayDescription";
    Node assayDescNode = null;//from w  w w. j  a  va  2 s  .  c om
    if (topNode.getName().equals("PC-AssayDescription"))
        assayDescNode = topNode;
    else {
        assayDescNode = topNode.selectSingleNode(".//PC-AssayDescription");
    }
    if (assayDescNode == null)
        throw new PubChemException(
                String.format("Cannot find PC-AssayDescription node in provided node %s", topNode.getPath()));

    Node node = assayDescNode.selectSingleNode("PC-AssayDescription_aid/PC-ID/PC-ID_id");
    Integer aid = new Integer(node.getText());

    try {
        PCAssay assay = new PCAssay();
        if (aid > 0)
            assay.setAID(aid);

        node = assayDescNode.selectSingleNode("PC-AssayDescription_aid/PC-ID/PC-ID_version");
        Integer version = new Integer(node.getText());
        assay.setVersion(version);

        node = assayDescNode.selectSingleNode("PC-AssayDescription_revision");
        Integer revision = new Integer(node.getText());
        assay.setRevision(revision);

        Node trackingNode = assayDescNode
                .selectSingleNode("PC-AssayDescription_aid-source/PC-Source/PC-Source_db/PC-DBTracking");

        node = trackingNode.selectSingleNode("PC-DBTracking_name");
        assay.setSourceName(node.getText());

        node = trackingNode.selectSingleNode("PC-DBTracking_source-id/Object-id/Object-id_str");
        assay.setExtRegId(node.getText());

        // hold until date
        node = trackingNode.selectSingleNode("PC-DBTracking_date");
        if (node != null) {
            String year = node.selectSingleNode("Date/Date_std/Date-std/Date-std_year").getText();
            String month = node.selectSingleNode("Date/Date_std/Date-std/Date-std_month").getText();
            String day = node.selectSingleNode("Date/Date_std/Date-std/Date-std_day").getText();
            if (DEBUGGING)
                log.info("year: " + year + " month: " + month + " day: " + day);
            Calendar calendar = Calendar.getInstance();
            calendar.set(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));
            assay.setHoldUntilDate(calendar.getTime());
            if (DEBUGGING)
                log.info(calendar.getTime().toString());
        }

        node = assayDescNode.selectSingleNode("PC-AssayDescription_name");
        assay.setName(node.getText());

        List<Node> nodes = assayDescNode
                .selectNodes("PC-AssayDescription_description/PC-AssayDescription_description_E");
        assay.setDescription(join(nodes, separator));

        nodes = assayDescNode.selectNodes("PC-AssayDescription_protocol/PC-AssayDescription_protocol_E");
        assay.setProtocol(join(nodes, separator));

        nodes = assayDescNode.selectNodes("PC-AssayDescription_comment/PC-AssayDescription_comment_E");
        assay.setComment(join(nodes, separator));

        node = assayDescNode.selectSingleNode("PC-AssayDescription_activity-outcome-method");
        if (node != null)
            assay.setActivityOutcomeMethod(node.valueOf("@value"));

        handlePlots(assay, assayDescNode);

        node = assayDescNode
                .selectSingleNode("PC-AssayDescription_grant-number/PC-AssayDescription_grant-number_E");
        if (node != null)
            assay.setGrantNumber(node.getText());

        node = assayDescNode.selectSingleNode("PC-AssayDescription_project-category");
        if (node != null)
            assay.setProjectCategory(node.valueOf("@value"));

        assay.getAssayXRefs().removeAll(assay.getAssayXRefs());

        nodes = assayDescNode.selectNodes("PC-AssayDescription_xref/PC-AnnotatedXRef");
        handleXRefs(assay, null, nodes);

        nodes = assayDescNode.selectNodes("PC-AssayDescription_target/PC-AssayTargetInfo");
        handleTargetXRefs(assay, null, nodes);

        handlePanels(assay, assayDescNode);

        handleColumns(assay, assayDescNode);

        handleComments(assay, assayDescNode);

        return assay;
    } catch (Exception ex) {
        throw new RuntimeException("Problem with AID " + aid, ex);
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsAddNodeAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action.//from w ww  .  j  av  a  2  s. co  m
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsAddNodeDialog(parent, props);
    }
    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError("Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }

    dialog.show(document.getDeclaredNamespaces(), currentXPath);

    if (!dialog.isCancelled()) {
        parent.setWait(true);
        parent.setStatus("Adding Nodes ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {
                    ExchangerDocument tempDoc = new ExchangerDocument(document.getText());

                    String newString = addNode(tempDoc, dialog.xpathPanel.getXpathPredicate(),
                            (String) dialog.nodeTypeCombo.getSelectedItem(),
                            (String) dialog.namespaceCombo.getSelectedItem(), dialog.prefixTextField.getText(),
                            dialog.nameTextField.getText(), dialog.valueTextField.getText());
                    if (newString != null) {

                        //need to parse the new document to make sure that it
                        //will produce well-formed xml.
                        ExchangerDocument newDocument = new ExchangerDocument(newString);
                        boolean createDocument = true;

                        if (newDocument.isError()) {
                            int questionResult = MessageHandler.showConfirm(parent,
                                    "The resulting document will not be well-formed\n"
                                            + "Do you wish to continue?");

                            if (questionResult == MessageHandler.CONFIRM_NO_OPTION) {
                                createDocument = false;
                            }
                        }

                        if (createDocument) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(newDocument, null);
                            } else {
                                parent.getView().getEditor().setText(newString);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }

                    }

                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Error - Cannot Add Nodes", "Add Node Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsAddNodeToNamespaceAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action.//from   www . ja  v  a2s. c  o  m
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsAddNodeToNamespaceDialog(parent, props);
    }

    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }
    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    //create temporary document
    dialog.show(document.getDeclaredNamespaces(), currentXPath);

    if (!dialog.isCancelled()) {
        parent.setWait(true);
        parent.setStatus("Adding Nodes To Namespace ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {
                    ExchangerDocument tempDoc = new ExchangerDocument(document.getText());
                    String uri = (String) dialog.nsURICombo.getSelectedItem();
                    String prefix = dialog.nsPrefixTextField.getText();
                    if (uri.equalsIgnoreCase("none")) {
                        uri = "";
                    }
                    if (prefix.equalsIgnoreCase("none")) {
                        prefix = "";
                    }
                    Namespace ns = new Namespace(prefix, uri);

                    String newString = ToolsAddNodeToNamespaceAction.this.addNamespaceToNode(tempDoc,
                            dialog.xpathPanel.getXpathPredicate(), ns);

                    if (newString != null) {

                        //need to parse the new document to make sure that it
                        //will produce well-formed xml.
                        ExchangerDocument newDocument = new ExchangerDocument(newString);
                        boolean createDocument = true;

                        if (newDocument.isError()) {
                            int questionResult = MessageHandler.showConfirm(parent,
                                    "The resulting document will not be well-formed\n"
                                            + "Do you wish to continue?");

                            if (questionResult == MessageHandler.CONFIRM_NO_OPTION) {
                                createDocument = false;
                            }
                        }

                        if (createDocument) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(newDocument, null);
                            } else {
                                parent.getView().getEditor().setText(newString);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }

                    }
                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Add Nodes To Namespace",
                            "Tools Add Nodes To Namespace Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsCapitalizeAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action./*from   www. j  a v a2s.c om*/
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsCapitalizeDialog(parent, props);
    }

    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }
    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {

        parent.setWait(true);
        parent.setStatus("Changing Capitals ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {

                    if ((dialog.elementsRadio.isSelected()) || (dialog.attributeRadio.isSelected())
                            || (dialog.elementsAndAttributesRadio.isSelected())) {
                        String newDocument = null;
                        ExchangerDocument tempDoc = new ExchangerDocument(document.getText());

                        boolean TRAVERSE_CHILDREN = true;

                        if (dialog.xpathPanel.xpathBox.isSelected()) {
                            String xpathPredicate = dialog.xpathPanel.getXpathPredicate();
                            newDocument = ToolsCapitalizeAction.this.capitalize(tempDoc, xpathPredicate,
                                    dialog.elementsRadio.isSelected(), dialog.attributeRadio.isSelected(),
                                    dialog.elementsAndAttributesRadio.isSelected(), TRAVERSE_CHILDREN);

                        } else {

                            //set the string to the new capitalize document
                            newDocument = ToolsCapitalizeAction.this.capitalize(tempDoc,
                                    dialog.elementsRadio.isSelected(), dialog.attributeRadio.isSelected(),
                                    dialog.elementsAndAttributesRadio.isSelected());
                        }
                        if (newDocument != null) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(new ExchangerDocument(newDocument), null);
                            } else {
                                parent.getView().getEditor().setText(newDocument);

                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }
                    }

                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Capitalize Document", "Tools Capitalize Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //            }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsConvertNodeAction.java

License:Open Source License

/**
 * The implementation of the validate action, called after a user action.
 * //from  ww  w.  j a v  a  2s .  c  om
 * @param event
 *            the action event.
 */
public void actionPerformed(ActionEvent event) {

    if (dialog == null) {
        dialog = new ToolsConvertNodeDialog(parent, props);
    }
    //called to make sure that the model is up to date to
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();
    //get the document
    final ExchangerDocument document = parent.getDocument();
    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {
        parent.setWait(true);
        parent.setStatus("Converting Nodes ...");
        // Run in Thread!!!
        Runnable runner = new Runnable() {

            public void run() {

                try {
                    ExchangerDocument tempDoc = new ExchangerDocument(document.getText());
                    String newString = convertNode(tempDoc, dialog.xpathPanel.getXpathPredicate(),
                            (String) dialog.nodeTypeCombo.getSelectedItem());
                    if (newString != null) {

                        //need to parse the new document to make sure that it
                        //will produce well-formed xml.
                        ExchangerDocument newDocument = new ExchangerDocument(newString);
                        boolean createDocument = true;

                        if (newDocument.isError()) {
                            int questionResult = MessageHandler.showConfirm(parent,
                                    "The resulting document will not be well-formed\n"
                                            + "Do you wish to continue?");

                            if (questionResult == MessageHandler.CONFIRM_NO_OPTION) {
                                createDocument = false;
                            }
                        }

                        if (createDocument) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(newDocument, null);
                            } else {
                                parent.getView().getEditor().setText(newString);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }

                    }
                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Convert Nodes", "Tools Convert Node Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };
        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //        }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsDeCapitalizeAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action.//from  w  w w .java 2 s.  c o m
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsDeCapitalizeDialog(parent, props);
    }

    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {
        parent.setWait(true);
        parent.setStatus("Changing Capitals ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {
                    //make sure one of the options was selected
                    if ((dialog.elementsRadio.isSelected()) || (dialog.attributeRadio.isSelected())
                            || (dialog.elementsAndAttributesRadio.isSelected())) {

                        ExchangerDocument tempDoc = new ExchangerDocument(document.getText());
                        String newDocument = null;

                        boolean TRAVERSE_CHILDREN = true;

                        if (dialog.xpathPanel.xpathBox.isSelected()) {
                            String xpathPredicate = dialog.xpathPanel.getXpathPredicate();
                            newDocument = deCapitalize(tempDoc, xpathPredicate,
                                    dialog.elementsRadio.isSelected(), dialog.attributeRadio.isSelected(),
                                    dialog.elementsAndAttributesRadio.isSelected(), TRAVERSE_CHILDREN);

                        } else {

                            //set the string to the new deCapitalize document
                            newDocument = ToolsDeCapitalizeAction.this.deCapitalize(tempDoc,
                                    dialog.elementsRadio.isSelected(), dialog.attributeRadio.isSelected(),
                                    dialog.elementsAndAttributesRadio.isSelected());
                        }
                        if (newDocument != null) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(new ExchangerDocument(newDocument), null);
                            } else {
                                parent.getView().getEditor().setText(newDocument);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }
                    }

                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot DeCapitalize Document",
                            "Tools DeCapitalize Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsLowercaseAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action./* w w  w.j  a va2  s .  co  m*/
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsLowercaseDialog(parent, props);
    }

    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {
        parent.setWait(true);
        parent.setStatus("Changing Case ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {
                    //make sure one of the options was selected
                    if ((dialog.elementsRadio.isSelected()) || (dialog.attributeRadio.isSelected())
                            || (dialog.elementsAndAttributesRadio.isSelected())) {

                        ExchangerDocument tempDoc = new ExchangerDocument(document.getText());
                        boolean TRAVERSE_CHILDREN = true;
                        String newDocument = null;

                        if (dialog.xpathPanel.xpathBox.isSelected()) {
                            String xpathPredicate = dialog.xpathPanel.getXpathPredicate();
                            newDocument = lowercase(tempDoc, xpathPredicate, dialog.elementsRadio.isSelected(),
                                    dialog.attributeRadio.isSelected(),
                                    dialog.elementsAndAttributesRadio.isSelected(), TRAVERSE_CHILDREN);

                        } else {

                            //set the string to the new lowercase document
                            newDocument = lowercase(tempDoc, dialog.elementsRadio.isSelected(),
                                    dialog.attributeRadio.isSelected(),
                                    dialog.elementsAndAttributesRadio.isSelected());
                        }
                        if (newDocument != null) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(new ExchangerDocument(newDocument), null);
                            } else {
                                parent.getView().getEditor().setText(newDocument);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }

                        }
                    }

                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Convert To Lowercase", "Tools Lowercase Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsRemoveNodeAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action.//  w  w w.  java2  s . co  m
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsRemoveNodeDialog(parent, props);
    }
    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    //create temporary document
    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {
        //get the new vector of namespaces
        //Vector newNamespaces = dialog.getNamespaces();
        parent.setWait(true);
        parent.setStatus("Removing Nodes ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {
                    ExchangerDocument tempDoc = new ExchangerDocument(document.getText());

                    String newString = removeByXPath(tempDoc, dialog.xpathPanel.getXpathPredicate());
                    if (newString != null) {

                        //need to parse the new document to make sure that it
                        //will produce well-formed xml.
                        ExchangerDocument newDocument = new ExchangerDocument(newString);
                        boolean createDocument = true;

                        if (newDocument.isError()) {
                            int questionResult = MessageHandler.showConfirm(parent,
                                    "The resulting document will not be well-formed\n"
                                            + "Do you wish to continue?");

                            if (questionResult == MessageHandler.CONFIRM_NO_OPTION) {
                                createDocument = false;
                            }
                        }

                        if (createDocument) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(newDocument, null);
                            } else {
                                parent.getView().getEditor().setText(newString);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }

                    }

                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Remove Nodes", "Tools Remove Node Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsRenameNodeAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action.//w ww  .j a  va 2  s.  c o m
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsRenameNodeDialog(parent, props);
    }
    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    //create temporary document
    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }

    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {
        //get the new vector of namespaces
        //Vector newNamespaces = dialog.getNamespaces();
        parent.setWait(true);
        parent.setStatus("Renaming Nodes ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {

                    ExchangerDocument tempDoc = new ExchangerDocument(document.getText());

                    String newString = renameByXPath(tempDoc, dialog.xpathPanel.getXpathPredicate(),
                            dialog.getNewName());
                    if (newString != null) {

                        //need to parse the new document to make sure that it
                        //will produce well-formed xml.
                        ExchangerDocument newDocument = new ExchangerDocument(newString);
                        boolean createDocument = true;

                        if (newDocument.isError()) {
                            int questionResult = MessageHandler.showConfirm(parent,
                                    "The resulting document will not be well-formed\n"
                                            + "Do you wish to continue?");

                            if (questionResult == MessageHandler.CONFIRM_NO_OPTION) {
                                createDocument = false;
                            }
                        }

                        if (createDocument) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(newDocument, null);
                            } else {
                                parent.getView().getEditor().setText(newString);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }

                    }
                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Rename Nodes", "Tools Rename Node Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}

From source file:com.cladonia.xngreditor.actions.ToolsSetNodeValueAction.java

License:Open Source License

/**
 * The implementation of the validate action, called 
 * after a user action./* ww  w.j  a  va 2  s  .c  om*/
 *
 * @param event the action event.
 */
public void actionPerformed(ActionEvent event) {
    if (dialog == null) {
        dialog = new ToolsSetNodeValueDialog(parent, props);
    }
    //called to make sure that the model is up to date to 
    //prevent any problems found when undo-ing etc.
    parent.getView().updateModel();

    //get the document
    final ExchangerDocument document = parent.getDocument();

    if (document.isError()) {
        MessageHandler.showError(parent, "Please make sure the document is well-formed.", "Parser Error");
        return;
    }

    //create temporary document
    String currentXPath = null;
    Node node = (Node) document.getLastNode(parent.getView().getEditor().getCursorPosition(), true);

    if (props.isUniqueXPath()) {
        currentXPath = node.getUniquePath();
    } else {
        currentXPath = node.getPath();
    }
    dialog.show(currentXPath);

    if (!dialog.isCancelled()) {
        //get the new vector of namespaces
        //Vector newNamespaces = dialog.getNamespaces();
        parent.setWait(true);
        parent.setStatus("Setting Node Value ...");

        // Run in Thread!!!
        Runnable runner = new Runnable() {
            public void run() {
                try {
                    ExchangerDocument tempDoc = new ExchangerDocument(document.getText());

                    String newString = setNodeValue(tempDoc, dialog.xpathPanel.getXpathPredicate(),
                            dialog.valueTextField.getText());
                    if (newString != null) {

                        //need to parse the new document to make sure that it
                        //will produce well-formed xml.
                        ExchangerDocument newDocument = new ExchangerDocument(newString);
                        boolean createDocument = true;

                        if (newDocument.isError()) {
                            int questionResult = MessageHandler.showConfirm(parent,
                                    "The resulting document will not be well-formed\n"
                                            + "Do you wish to continue?");

                            if (questionResult == MessageHandler.CONFIRM_NO_OPTION) {
                                createDocument = false;
                            }
                        }

                        if (createDocument) {
                            if (dialog.toNewDocumentRadio.isSelected()) {
                                //user has selected to create the result as a new document
                                parent.open(newDocument, null);
                            } else {
                                parent.getView().getEditor().setText(newString);
                                SwingUtilities.invokeLater(new Runnable() {
                                    public void run() {
                                        parent.switchToEditor();

                                        parent.getView().updateModel();
                                    }
                                });
                            }
                        }

                    }
                } catch (Exception e) {
                    // This should never happen, just report and continue
                    MessageHandler.showError(parent, "Cannot Set Node Value", "Tools Set Node Value Error");
                } finally {
                    parent.setStatus("Done");
                    parent.setWait(false);
                }
            }
        };

        // Create and start the thread ...
        Thread thread = new Thread(runner);
        thread.start();
        //          }
    }
}