Example usage for org.dom4j Node selectSingleNode

List of usage examples for org.dom4j Node selectSingleNode

Introduction

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

Prototype

Node selectSingleNode(String xpathExpression);

Source Link

Document

selectSingleNode evaluates an XPath expression and returns the result as a single Node instance.

Usage

From source file:org.pdfsam.console.business.pdf.handlers.ConcatCmdExecutor.java

License:Open Source License

/**
 * @param pdfNode// w w  w.j  a  v  a2  s . com
 *            input node
 * @param parentPath
 *            file parent path or null
 * @return a PdfFile object given a file node
 * @throws Exception
 */
private PdfFile getPdfFileFromNode(Node pdfNode, String parentPath) throws Exception {
    PdfFile retVal = null;
    String pwd = null;
    String fileName = null;
    // get filename
    Node fileNode = pdfNode.selectSingleNode("@value");
    if (fileNode != null) {
        fileName = fileNode.getText().trim();
    } else {
        throw new ConcatException(ConcatException.ERR_READING_CSV_OR_XML, new String[] { "Empty file name." });
    }
    // get pwd value
    Node pwdNode = pdfNode.selectSingleNode("@password");
    if (pwdNode != null) {
        pwd = pwdNode.getText();
    }
    if (parentPath != null && parentPath.length() > 0) {
        retVal = new PdfFile(new File(parentPath, fileName), pwd);
    } else {
        retVal = new PdfFile(fileName, pwd);
    }
    return retVal;
}

From source file:org.pdfsam.console.business.pdf.handlers.SlideShowCmdExecutor.java

License:Open Source License

/**
 * Parse the input xml file adding the informations to the Transitions object
 * @param inputFile//from ww  w .  ja  v  a2  s  . com
 * @param transitions
 * @return
 */
private Transitions parseXmlInput(File inputFile, Transitions transitions) throws SlideShowException {
    Transitions retVal = transitions;
    if (inputFile != null) {
        try {
            LOG.debug("Parsing xml transitions file " + inputFile.getAbsolutePath());
            SAXReader reader = new SAXReader();
            Document document = reader.read(inputFile);
            Node rootNode = document.selectSingleNode("/transitions");
            if (rootNode != null) {
                Node defType = rootNode.selectSingleNode("@defaulttype");
                Node defTransDur = rootNode.selectSingleNode("@defaulttduration");
                Node defDur = rootNode.selectSingleNode("@defaultduration");
                if (defType != null && defTransDur != null && defDur != null) {
                    if (transitions.getDefaultTransition() != null) {
                        throw new SlideShowException(SlideShowException.ERR_DEFAULT_TRANSITION_ALREADY_SET);
                    } else {
                        transitions.setDefaultTransition(new Transition(Transition.EVERY_PAGE,
                                new Integer(defTransDur.getText().trim()).intValue(), defType.getText().trim(),
                                new Integer(defDur.getText().trim()).intValue()));
                    }
                }
                List transitionsList = document.selectNodes("/transitions/transition");
                for (int i = 0; transitionsList != null && i < transitionsList.size(); i++) {
                    Node transitionNode = (Node) transitionsList.get(i);
                    Node type = transitionNode.selectSingleNode("@type");
                    Node transDuration = transitionNode.selectSingleNode("@tduration");
                    Node duration = transitionNode.selectSingleNode("@duration");
                    Node page = transitionNode.selectSingleNode("@pagenumber");
                    if (type != null && transDuration != null && duration != null && page != null) {
                        transitions.addTransition(new Transition(new Integer(page.getText().trim()).intValue(),
                                new Integer(transDuration.getText().trim()).intValue(), type.getText().trim(),
                                new Integer(duration.getText().trim()).intValue()));
                    } else {
                        throw new SlideShowException(SlideShowException.ERR_READING_TRANSITION,
                                new String[] { i + "" });
                    }
                }
            } else {
                throw new SlideShowException(SlideShowException.ERR_READING_XML_TRANSITIONS_FILE);
            }
        } catch (Exception e) {
            throw new SlideShowException(SlideShowException.ERR_READING_XML_TRANSITIONS_FILE, e);
        }
    }
    return retVal;
}

From source file:org.pdfsam.console.business.pdf.handlers.SplitCmdExecutor.java

License:Open Source License

/**
 * Execute the split of a pdf document when split type is S_BLEVEL
 * //from  w ww.  j av  a 2s .c o  m
 * @param inputCommand
 * @throws Exception
 */
private void executeBookmarksSplit(SplitParsedCommand inputCommand) throws Exception {
    pdfReader = PdfUtility.readerFor(inputCommand.getInputFile());
    int bLevel = inputCommand.getBookmarksLevel().intValue();
    Hashtable bookmarksTable = new Hashtable();
    if (bLevel > 0) {
        pdfReader.removeUnusedObjects();
        pdfReader.consolidateNamedDestinations();
        List bookmarks = SimpleBookmark.getBookmark(pdfReader);
        ByteArrayOutputStream out = new ByteArrayOutputStream();
        SimpleBookmark.exportToXML(bookmarks, out, "UTF-8", false);
        ByteArrayInputStream input = new ByteArrayInputStream(out.toByteArray());
        int maxDepth = PdfUtility.getMaxBookmarksDepth(input);
        input.reset();
        if (bLevel <= maxDepth) {
            SAXReader reader = new SAXReader();
            org.dom4j.Document document = reader.read(input);
            // head node
            String headBookmarkXQuery = "/Bookmark/Title[@Action=\"GoTo\"]";
            Node headNode = document.selectSingleNode(headBookmarkXQuery);
            if (headNode != null && headNode.getText() != null && headNode.getText().trim().length() > 0) {
                bookmarksTable.put(new Integer(1), headNode.getText().trim());
            }
            // bLevel nodes
            StringBuffer buffer = new StringBuffer("/Bookmark");
            for (int i = 0; i < bLevel; i++) {
                buffer.append("/Title[@Action=\"GoTo\"]");
            }
            String xQuery = buffer.toString();
            List nodes = document.selectNodes(xQuery);
            input.close();
            input = null;
            if (nodes != null && nodes.size() > 0) {
                LinkedHashSet pageSet = new LinkedHashSet(nodes.size());
                for (Iterator nodeIter = nodes.iterator(); nodeIter.hasNext();) {
                    Node currentNode = (Node) nodeIter.next();
                    Node pageAttribute = currentNode.selectSingleNode("@Page");
                    if (pageAttribute != null && pageAttribute.getText().length() > 0) {
                        String attribute = pageAttribute.getText();
                        int blankIndex = attribute.indexOf(' ');
                        if (blankIndex > 0) {
                            Integer currentNumber = new Integer(attribute.substring(0, blankIndex));
                            String bookmarkText = currentNode.getText().trim();
                            // fix #2789963
                            if (currentNumber.intValue() > 0) {
                                // bookmarks regexp matching if any
                                if (StringUtils.isBlank(inputCommand.getBookmarkRegexp())
                                        || bookmarkText.matches(inputCommand.getBookmarkRegexp())) {
                                    // to split just before the given page
                                    if ((currentNumber.intValue()) > 1) {
                                        pageSet.add(new Integer(currentNumber.intValue() - 1));
                                    }
                                    if (StringUtils.isNotBlank(bookmarkText)) {
                                        bookmarksTable.put(currentNumber, bookmarkText.trim());
                                    }
                                }
                            }
                        }
                    }
                }
                if (pageSet.size() > 0) {
                    if (StringUtils.isBlank(inputCommand.getBookmarkRegexp())) {
                        LOG.debug("Found " + pageSet.size() + " destination pages at level " + bLevel);
                    } else {
                        LOG.debug("Found " + pageSet.size() + " destination pages at level " + bLevel
                                + " matching '" + inputCommand.getBookmarkRegexp() + "'");
                    }
                    inputCommand.setSplitPageNumbers((Integer[]) pageSet.toArray(new Integer[pageSet.size()]));
                } else {
                    throw new SplitException(SplitException.ERR_BLEVEL_NO_DEST, new String[] { "" + bLevel });
                }
            } else {
                throw new SplitException(SplitException.ERR_BLEVEL, new String[] { "" + bLevel });
            }
        } else {
            input.close();
            pdfReader.close();
            throw new SplitException(SplitException.ERR_BLEVEL_OUTOFBOUNDS,
                    new String[] { "" + bLevel, "" + maxDepth });

        }
    } else {
        pdfReader.close();
        throw new SplitException(SplitException.ERR_NOT_VALID_BLEVEL, new String[] { "" + bLevel });
    }
    pdfReader.close();
    executeSplit(inputCommand, bookmarksTable);
}

From source file:org.pdfsam.emp4j.messages.sources.XmlMessagesSource.java

License:Open Source License

public XmlMessagesSource(Node node) throws Exception {
    super(node);/*from  w w  w.  ja  va 2s  . co m*/
    Node arg1 = node.selectSingleNode("xmlsource/@filename");
    if (arg1 != null) {
        document = getDocument(arg1.getText());
    } else {
        throw new DOMException(DOMException.NOT_FOUND_ERR, "Error reading configuration node, node is null.");
    }
}

From source file:org.pdfsam.guiclient.gui.panels.JSettingsPanel.java

License:Open Source License

/**
 * Loads the available languages//from w ww  . j  av a2  s . c  om
 */
@SuppressWarnings("unchecked")
private Vector<StringItem> loadLanguages() {
    Vector<StringItem> langs = new Vector<StringItem>(10, 5);
    try {
        Document document = XmlUtility
                .parseXmlFile(this.getClass().getResource("/org/pdfsam/i18n/languages.xml"));
        List<Node> nodeList = document.selectNodes("/languages/language");
        for (int i = 0; nodeList != null && i < nodeList.size(); i++) {
            Node langNode = ((Node) nodeList.get(i));
            if (langNode != null) {
                langs.add(new StringItem(langNode.selectSingleNode("@value").getText(),
                        langNode.selectSingleNode("@description").getText()));
            }
        }
    } catch (Exception e) {
        log.error(GettextResource.gettext(config.getI18nResourceBundle(), "Error: "), e);
        langs.add(new StringItem("en_GB", "English (UK)"));
    }
    Collections.sort(langs);
    return langs;
}

From source file:org.pdfsam.guiclient.utils.xml.XmlUtility.java

License:Open Source License

/**
 * @param pageNode//from w  w w .ja v a 2s .c o m
 * @return given a page dom4j node it returns a DocumentPage object
 */
public static DocumentPage getDocumentPage(Node pageNode) {
    DocumentPage retVal = null;
    try {
        if (pageNode != null) {
            retVal = new DocumentPage();

            Node deletedNode = (Node) pageNode.selectSingleNode("@deleted");
            if (deletedNode != null && deletedNode.getText().length() > 0) {
                retVal.setDeleted(Boolean.valueOf(deletedNode.getText()));
            }

            Node numberNode = (Node) pageNode.selectSingleNode("@number");
            if (numberNode != null && numberNode.getText().length() > 0) {
                retVal.setPageNumber(Integer.valueOf(numberNode.getText()));
            }

            Node rotationNode = (Node) pageNode.selectSingleNode("@rotation");
            if (rotationNode != null && rotationNode.getText().length() > 0) {
                retVal.setRotation(Rotation.getRotation(Integer.valueOf(rotationNode.getText())));
            }

        }
    } catch (Exception e) {
        log.warn(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),
                "Error retrieving page saved informations"), e);
    }
    return retVal;
}

From source file:org.pdfsam.guiclient.utils.XmlUtility.java

License:Open Source License

/**
 * @param pageNode/*from  w  ww.  j  a va2 s  . com*/
 * @return given a page dom4j node it returns a DocumentPage object
 */
public static DocumentPage getDocumentPage(Node pageNode) {
    DocumentPage retVal = null;
    try {
        if (pageNode != null) {
            retVal = new DocumentPage();

            Node deletedNode = (Node) pageNode.selectSingleNode("@deleted");
            if (deletedNode != null && deletedNode.getText().length() > 0) {
                retVal.setDeleted(Boolean.valueOf(deletedNode.getText()));
            }

            Node numberNode = (Node) pageNode.selectSingleNode("@number");
            if (numberNode != null && numberNode.getText().length() > 0) {
                retVal.setPageNumber(Integer.valueOf(numberNode.getText()));
            }

            Node rotationNode = (Node) pageNode.selectSingleNode("@rotation");
            if (rotationNode != null && rotationNode.getText().length() > 0) {
                retVal.setRotation(Rotation.getRotation(Integer.valueOf(rotationNode.getText())));
            }

        }
    } catch (Exception e) {
        LOG.warn(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),
                "Error retrieving page saved informations"), e);
    }
    return retVal;
}

From source file:org.pdfsam.plugin.coverfooter.GUI.CoverFooterMainGUI.java

License:Open Source License

@SuppressWarnings("unchecked")
public void loadJobNode(Node arg0) throws LoadJobException {
    try {/*from   w  w  w .  j a  v  a2  s  .c o  m*/
        Node coverSource = (Node) arg0.selectSingleNode("cover/@value");
        if (coverSource != null && coverSource.getText().length() > 0) {
            Node coverPageSelection = (Node) arg0.selectSingleNode("cover/@pageselection");
            Node coverFilePwd = (Node) arg0.selectSingleNode("cover/@password");
            coverSelectionPanel.getLoader().addFile(new File(coverSource.getText()),
                    (coverFilePwd != null) ? coverFilePwd.getText() : null,
                    (coverPageSelection != null) ? coverPageSelection.getText() : null);
        }

        footerSelectionPanel.getClearButton().doClick();
        Node footerSource = (Node) arg0.selectSingleNode("cover/@value");
        if (footerSource != null && footerSource.getText().length() > 0) {
            Node footerPageSelection = (Node) arg0.selectSingleNode("cover/@pageselection");
            Node footerFilePwd = (Node) arg0.selectSingleNode("cover/@password");
            footerSelectionPanel.getLoader().addFile(new File(footerSource.getText()),
                    (footerFilePwd != null) ? footerFilePwd.getText() : null,
                    (footerPageSelection != null) ? footerPageSelection.getText() : null);
        }

        Node fileDestination = (Node) arg0.selectSingleNode("destination/@value");
        if (fileDestination != null) {
            destinationTextField.setText(fileDestination.getText());
        }

        Node fileOverwrite = (Node) arg0.selectSingleNode("overwrite/@value");
        if (fileOverwrite != null) {
            overwriteCheckbox.setSelected(TRUE.equals(fileOverwrite.getText()));
        }

        Node mergeType = (Node) arg0.selectSingleNode("merge_type/@value");
        if (mergeType != null) {
            mergeTypeCheck.setSelected(TRUE.equals(mergeType.getText()));
        }

        selectionPanel.getClearButton().doClick();
        List fileList = arg0.selectNodes("filelist/file");
        for (int i = 0; fileList != null && i < fileList.size(); i++) {
            Node fileNode = (Node) fileList.get(i);
            if (fileNode != null) {
                Node fileName = (Node) fileNode.selectSingleNode("@name");
                if (fileName != null && fileName.getText().length() > 0) {
                    Node pageSelection = (Node) fileNode.selectSingleNode("@pageselection");
                    Node filePwd = (Node) fileNode.selectSingleNode("@password");
                    selectionPanel.getLoader().addFile(new File(fileName.getText()),
                            (filePwd != null) ? filePwd.getText() : null,
                            (pageSelection != null) ? pageSelection.getText() : null);
                }
            }
        }

        Node fileCompressed = (Node) arg0.selectSingleNode("compressed/@value");
        if (fileCompressed != null && TRUE.equals(fileCompressed.getText())) {
            outputCompressedCheck.doClick();
        }

        Node pdfVersion = (Node) arg0.selectSingleNode("pdfversion/@value");
        if (pdfVersion != null) {
            for (int i = 0; i < versionCombo.getItemCount(); i++) {
                if (((StringItem) versionCombo.getItemAt(i)).getId().equals(pdfVersion.getText())) {
                    versionCombo.setSelectedIndex(i);
                    break;
                }
            }
        }
        log.info(GettextResource.gettext(config.getI18nResourceBundle(), "Cover And Footer section loaded."));
    } catch (Exception ex) {
        log.error(GettextResource.gettext(config.getI18nResourceBundle(), "Error: "), ex);
    }
}

From source file:org.pdfsam.plugin.decrypt.GUI.DecryptMainGUI.java

License:Open Source License

@SuppressWarnings("unchecked")
public void loadJobNode(Node arg0) throws LoadJobException {
    try {//from   w w  w  . jav a 2 s .  c  om
        List fileList = arg0.selectNodes("filelist/file");
        for (int i = 0; fileList != null && i < fileList.size(); i++) {
            Node fileNode = (Node) fileList.get(i);
            if (fileNode != null) {
                Node fileName = (Node) fileNode.selectSingleNode("@name");
                if (fileName != null && fileName.getText().length() > 0) {
                    Node filePwd = (Node) fileNode.selectSingleNode("@password");
                    selectionPanel.getLoader().addFile(new File(fileName.getText()),
                            (filePwd != null) ? filePwd.getText() : null);
                }
            }
        }

        Node fileDestination = (Node) arg0.selectSingleNode("destination/@value");
        if (fileDestination != null) {
            destinationTextField.setText(fileDestination.getText());
        }

        Node fileOverwrite = (Node) arg0.selectSingleNode("overwrite/@value");
        if (fileOverwrite != null) {
            overwriteCheckbox.setSelected(fileOverwrite.getText().equals("true"));
        }

        Node fileCompressed = (Node) arg0.selectSingleNode("compressed/@value");
        if (fileCompressed != null && TRUE.equals(fileCompressed.getText())) {
            outputCompressedCheck.doClick();
        }

        Node filePrefix = (Node) arg0.selectSingleNode("prefix/@value");
        if (filePrefix != null) {
            outPrefixTextField.setText(filePrefix.getText());
        }

        Node pdfVersion = (Node) arg0.selectSingleNode("pdfversion/@value");
        if (pdfVersion != null) {
            for (int i = 0; i < versionCombo.getItemCount(); i++) {
                if (((StringItem) versionCombo.getItemAt(i)).getId().equals(pdfVersion.getText())) {
                    versionCombo.setSelectedIndex(i);
                    break;
                }
            }
        }

        log.info(GettextResource.gettext(config.getI18nResourceBundle(), "Decrypt section loaded."));
    } catch (Exception ex) {
        log.error(GettextResource.gettext(config.getI18nResourceBundle(), "Error: "), ex);
    }
}

From source file:org.pdfsam.plugin.docinfo.GUI.DocInfoMainGUI.java

License:Open Source License

public void loadJobNode(Node arg0) throws LoadJobException {
    try {/*from   w ww. j  a v  a2  s.  co  m*/
        Node fileSource = (Node) arg0.selectSingleNode("source/@value");
        if (fileSource != null && fileSource.getText().length() > 0) {
            Node filePwd = (Node) arg0.selectSingleNode("source/@password");
            String password = null;
            if (filePwd != null && filePwd.getText().length() > 0) {
                password = filePwd.getText();
            }
            selectionPanel.getLoader().addFile(new File(fileSource.getText()), password);
        }
        Node titleNode = (Node) arg0.selectSingleNode("title/@value");
        if (titleNode != null) {
            titleTextField.setText(titleNode.getText());
        }

        Node authoreNode = (Node) arg0.selectSingleNode("author/@value");
        if (authoreNode != null) {
            authorTextField.setText(authoreNode.getText());
        }

        Node subjectNode = (Node) arg0.selectSingleNode("subject/@value");
        if (subjectNode != null) {
            subjectTextField.setText(subjectNode.getText());
        }

        Node keywordsNode = (Node) arg0.selectSingleNode("keywords/@value");
        if (keywordsNode != null) {
            keywordsTextField.setText(keywordsNode.getText());
        }

        Node fileDestination = (Node) arg0.selectSingleNode("destination/@value");
        if (fileDestination != null && fileDestination.getText().length() > 0) {
            destinationTextField.setText(fileDestination.getText());
            chooseAFileRadio.doClick();
        } else {
            sameAsSourceRadio.doClick();
        }

        Node fileOverwrite = (Node) arg0.selectSingleNode("overwrite/@value");
        if (fileOverwrite != null) {
            overwriteCheckbox.setSelected(fileOverwrite.getText().equals("true"));
        }

        Node fileCompressed = (Node) arg0.selectSingleNode("compressed/@value");
        if (fileCompressed != null && TRUE.equals(fileCompressed.getText())) {
            outputCompressedCheck.doClick();
        }

        Node pdfVersion = (Node) arg0.selectSingleNode("pdfversion/@value");
        if (pdfVersion != null) {
            for (int i = 0; i < versionCombo.getItemCount(); i++) {
                if (((StringItem) versionCombo.getItemAt(i)).getId().equals(pdfVersion.getText())) {
                    versionCombo.setSelectedIndex(i);
                    break;
                }
            }
        }

        log.info(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(),
                "Rotate section loaded."));
    } catch (Exception ex) {
        log.error(GettextResource.gettext(Configuration.getInstance().getI18nResourceBundle(), "Error: "), ex);
    }
}