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:ca.coder2000.recipes.RecipeFile.java

License:Mozilla Public License

/**
 * Gets the ingredients list./*w w  w.  j  a va2  s  .  com*/
 * @return The list of ingredients in the recipe.
 */
public ArrayList getIngredients() {
    ArrayList<Ingredient> list = new ArrayList<Ingredient>();
    List lst = doc.selectNodes("//Ingredients/Ingredient");
    Iterator i = lst.iterator();

    while (i.hasNext()) {
        Node ing = (Node) i.next();
        Node name = ing.selectSingleNode("//Name");
        Node amount = ing.selectSingleNode("//Amount");
        Node measure = ing.selectSingleNode("//Measure");
        Ingredient j = new Ingredient(name.getStringValue(), amount.getStringValue(), measure.getStringValue());
        list.add(j);
    }

    return list;
}

From source file:cinnamon.global.Conf.java

License:Open Source License

@SuppressWarnings("unchecked")
public Collection<String> getRepositoryList() {
    List<Node> nodeList = xml.selectNodes("/cinnamon_config/repositories/repository");
    log.debug("number of repositories found:" + nodeList.size());
    Collection<String> list = new LinkedList<>();
    for (Node n : nodeList) {
        String name = n.selectSingleNode("name").getText();
        log.debug("found repository in config: '" + name + "'");
        list.add(name);// w  w  w.  j a va  2 s  .c  om
    }

    return list;
}

From source file:cinnamon.global.Conf.java

License:Open Source License

public String getPersistenceUnit(String repository) {
    Node repNode = xml.selectSingleNode("/cinnamon_config/repositories/repository[name='" + repository + "']");
    //      log.debug("found repNode: "+repNode.asXML());
    if (repNode == null) {
        throw new CinnamonConfigurationException(
                "Could not find repository " + repository + " in config file.");

    }//from w  ww .  j  a  va2 s. c om
    String pu = repNode.selectSingleNode("persistence_unit").getText();

    log.debug(String.format("persistence-unit %s found for repository %s", pu, repository));
    return pu;
}

From source file:com.alefissak.parsers.PlanningParserImpl.java

License:Apache License

/**
 * {@inheritDoc}//from w  ww.ja  va 2  s .  co m
 */
@Override
public List<Planning> parse() throws AlefissakFileNotFoundException, AlefissakParsingException {

    LOG.debug("Retrieving the document object from configuration file.");

    final Document document = getDocument();

    List<Planning> pList = new ArrayList<Planning>();

    try {

        Node plannings = document.selectSingleNode(Constants.CFG_ROOT_PATH);

        if (plannings == null) {
            throw new AlefissakParsingException("root element missing");
        }

        @SuppressWarnings("unchecked")
        List<Node> planningNodes = document.selectNodes(Constants.CFG_PLANNINGS_PATH);

        Node planningNode, timeNode, messageNode, fromNode, workingNode;
        String planningName;

        int rank = 1;

        for (Iterator<Node> iter = planningNodes.iterator(); iter.hasNext();) {
            planningNode = iter.next();

            Planning planning = new Planning();
            planningName = planningNode.valueOf(CFG_NAME_ATTRIBUTE_NAME);
            planning.setName(planningName);

            timeNode = planningNode.selectSingleNode(Constants.CFG_TIME_ATTRIBUTE_NAME);
            if (timeNode == null) {
                throw new AlefissakParsingException(
                        "Time configuration attributes are not defined for the planning " + planningName);
            }

            workingNode = timeNode.selectSingleNode(Constants.CFG_START_TIME_ATTRIBUTE_NAME);
            if (workingNode == null) {
                throw new AlefissakParsingException(
                        "Time configuration attributes are not defined for the planning " + planningName);
            }

            planning.setStartTime(AlefissakUtil.toDate(workingNode.getText().toString()));

            workingNode = timeNode.selectSingleNode(Constants.CFG_END_TIME_ATTRIBUTE_NAME);
            if (workingNode == null) {
                throw new AlefissakParsingException(
                        "Time configuration attributes are not defined for the planning " + planningName);
            }

            planning.setEndTime(AlefissakUtil.toDate(workingNode.getText().toString().trim()));

            workingNode = timeNode.selectSingleNode(Constants.CFG_TIME_INTERVAL_ATTRIBUTE_NAME);
            if (workingNode == null) {
                throw new AlefissakParsingException(
                        "Time configuration attributes are not defined for the planning " + planningName);
            }

            final TimeInterval timeInterval = AlefissakUtil.toTimeInterval(workingNode.getText().toString());

            planning.setTimeInterval(timeInterval);
            planning.setCronExpression(AlefissakUtil.getCronExpression(timeInterval));

            Message message = new AlefissakMessageConcret();

            if ((messageNode = planningNode.selectSingleNode(Constants.CFG_MESSAGE_TAG_NAME)) == null) {

                throw new AlefissakParsingException("No message defined for the planning " + planningName);
            }

            workingNode = messageNode.selectSingleNode(Constants.CFG_MESSAGE_OBJECT_TAG_NAME);

            message.setObject(workingNode == null ? "" : workingNode.getText().toString());

            fromNode = messageNode.selectSingleNode(Constants.CFG_MESSAGE_FROM_TAG_NAME);

            if (fromNode == null) {

                throw new AlefissakParsingException("From tag is missing in the configuration file");
            }

            workingNode = fromNode.selectSingleNode(Constants.CFG_NAME_TAG_NAME);
            AlefissakSender sender = new AlefissakSender();

            sender.setName(workingNode == null ? "" : workingNode.getText().toString());

            workingNode = fromNode.selectSingleNode(Constants.CFG_MAIL_TAG_NAME);

            sender.setMail(workingNode == null ? "" : workingNode.getText().toString());

            message.setSender(sender);

            workingNode = messageNode.selectSingleNode(Constants.CFG_MESSAGE_CONTENT_TAG_NAME);

            if (workingNode == null) {

                throw new AlefissakParsingException(
                        "There is no message to send for the planning " + planningName);
            }

            message.setContent(workingNode.getText().toString());

            planning.setMessage(message);

            parseReceivers(planning, planningNode);

            planning.setRank(rank++);

            pList.add(planning); // Add the new planning to the plannings
            // list
        }

    } catch (Exception e) {

        throw new AlefissakParsingException(e);
    }

    return pList;
}

From source file:com.alefissak.parsers.PlanningParserImpl.java

License:Apache License

/**
 * Parse nodes of receivers, get all informations from these nodes, then
 * add them into planning receivers list
 * //from  w  w w .  jav  a2  s.  c  o  m
 * @param planning the planning to be fill
 * @param planningNode node containing informations on receivers
 * @throws AlefissakParsingException
 */
private void parseReceivers(Planning planning, Node planningNode) throws AlefissakParsingException {

    @SuppressWarnings("unchecked")
    List<Node> receiverNodes = planningNode
            .selectNodes(Constants.CFG_RECEIVERS_TAG_NAME + "/" + Constants.CFG_RECEIVER_TAG_NAME);

    if (receiverNodes.isEmpty()) {
        throw new AlefissakParsingException("You have to specify at least one receiver");
    }

    Node workingNode;

    for (Iterator<Node> iter = receiverNodes.iterator(); iter.hasNext();) {
        Node receiverNode = iter.next();

        workingNode = receiverNode.selectSingleNode(Constants.CFG_NAME_TAG_NAME);

        if (workingNode == null) {
            throw new AlefissakParsingException("Please define all receiver's name");
        }

        AlefissakReceiver receiver = new AlefissakReceiver();
        receiver.setName(workingNode.getText().toString());

        workingNode = receiverNode.selectSingleNode(Constants.CFG_MAIL_TAG_NAME);
        if (workingNode == null) {
            throw new AlefissakParsingException("Please define all receiver's mail");
        }

        receiver.setMail(workingNode.getText().toString());

        workingNode = receiverNode.selectSingleNode(Constants.CFG_VARS_TAG_NAME);

        if (workingNode != null) {
            setVars(receiver, workingNode.getText().toString());
        }

        planning.addReceiver(receiver);
    }
}

From source file:com.amalto.workbench.widgets.xmleditor.pagecontent.ExtensibleXMLEditorPageContent.java

License:Open Source License

protected String getSingleValueByPath(Node parent, String relativePath, String valueForNull) {

    if (parent == null) {
        return valueForNull;
    }//  www .j av  a 2s. c  om

    Node node = parent.selectSingleNode("./" + relativePath);//$NON-NLS-1$

    if (node == null) {
        return valueForNull;
    }

    return node.getText();
}

From source file:com.chingo247.settlercraft.structure.plan.PlanMenuManager.java

License:Open Source License

/**
 * Loads the PlanMenu from the menu.xml. If menu.xml doesn't exist, the menu.xml from the jar
 * will be written to the FileSystem./*from www.  j  a v  a  2s  .  co m*/
 *
 * @throws DocumentException When XML is invalid
 * @throws StructureAPIException When XML contains invalid data
 */
public final void init() throws DocumentException, StructureAPIException {
    File file = new File(PLUGIN_FOLDER, "menu.xml");
    if (!file.exists()) {
        InputStream input = PlanMenuManager.class.getClassLoader()
                .getResourceAsStream(RESOURCE_FOLDER + "/menu.xml");
        FileUtil.write(input, file);
    }

    Document d = new SAXReader().read(file);
    List<Node> rows = d.selectNodes("Menu/SlotRow");
    if (rows.size() > 2) {
        throw new StructureAPIException("Max rows is 2 for menu.xml");
    }

    planMenu = MenuAPI.createMenu(SettlerCraft.getInstance(), PLANSHOP_NAME, 54);
    boolean hasRow2 = false;

    for (int row = 0; row < rows.size(); row++) {
        Node rowNode = rows.get(row);
        List<Node> slotNodes = rowNode.selectNodes("Slot");
        // Slot #1 is reserved for all category
        if (row == 0 && slotNodes.size() > 8) {
            throw new StructureAPIException(" 'SlotRow#1' has max 8 slots to customize");
        } else if (slotNodes.size() > 9) {
            throw new StructureAPIException(" 'SlotRow#" + (row + 2) + "' has max 9 slots to customize");
        }
        int count = 0;

        for (Node categorySlotNode : slotNodes) {

            if (!categorySlotNode.hasContent()) {
                count++;
                continue;
            }

            Node mId = categorySlotNode.selectSingleNode("MaterialID");
            Node cat = categorySlotNode.selectSingleNode("Category");
            Node ali = categorySlotNode.selectSingleNode("Aliases");

            if (mId == null) {
                throw new StructureAPIException("Missing 'MaterialID' element in 'SlotRow#" + (row + 1)
                        + "' 'Slot#" + (count + 1) + "'");
            }
            if (cat == null) {
                throw new StructureAPIException(
                        "Missing 'Category' element in 'SlotRow#" + (row + 1) + "' 'Slot#" + (count + 1) + "'");
            }

            int id;
            try {
                id = Integer.parseInt(mId.getText());
            } catch (NumberFormatException nfe) {
                throw new StructureAPIException("Invalid number for 'MaterialID' element in 'SlotRow#"
                        + (row + 1) + "' 'Slot#" + (count + 1) + "'");
            }
            String category = cat.getText();
            if (category.isEmpty()) {
                Element catEl = (Element) cat;
                category = catEl.attributeValue("value");
            }
            if (category.trim().isEmpty()) {
                throw new StructureAPIException("Empty 'Category' element in 'SlotRow#" + (row + 1)
                        + "' and 'Slot#" + (count + 1) + "'");
            }
            category = category.replaceAll(" AND ", "&");

            String[] aliases;
            if (ali == null) {
                aliases = new String[0];
            } else {
                List<Node> aliasNodes = ali.selectNodes("Alias");
                aliases = new String[aliasNodes.size()];
                for (int j = 0; j < aliasNodes.size(); j++) {
                    String alias = aliasNodes.get(j).getText();

                    if (alias.isEmpty()) {
                        Element aliasEl = (Element) cat;
                        alias = aliasEl.attributeValue("value");
                    }
                    if (alias.trim().isEmpty()) {
                        throw new StructureAPIException("Empty 'Alias' element in  'SlotRow#" + (row + 1)
                                + "' and 'Slot#" + (count + 1) + "' and 'Alias#" + (j + 1) + "'");
                    }

                    aliases[j] = aliasNodes.get(j).getText();
                }
            }
            int slot = count;
            if (row == 0) {
                slot += 1; // slot 0 is reserved...
            } else {

                hasRow2 = true;
            }

            planMenu.putCategorySlot((row * 9) + slot, category, Material.getMaterial(id), aliases);

            count++;
        }
        // fill remaining
        if (count < 8 && row == 0) {
            for (int i = count; i < 8; i++) {
                planMenu.putLocked(i);
            }

        } else if (row > 0 && count < 9) {
            for (int i = count; i < 9; i++) {
                planMenu.putLocked((row * 9) + i);
            }
        }
    }

    if (hasRow2) {
        planMenu.putLocked(19, 20, 21, 22, 23, 24, 25);
        planMenu.putActionSlot(18, "Previous", Material.COAL_BLOCK);
        planMenu.putActionSlot(26, "Next", Material.COAL_BLOCK);
    } else {
        planMenu.putLocked(10, 11, 12, 13, 14, 15, 16);
        planMenu.putActionSlot(9, "Previous", Material.COAL_BLOCK);
        planMenu.putActionSlot(17, "Next", Material.COAL_BLOCK);
    }

}

From source file:com.chingo247.structureapi.menus.plans.StructurePlanMenuReader.java

License:Open Source License

public CategoryMenu read(File file) throws DocumentException, SettlerCraftException {
    Preconditions.checkArgument(file.exists(), "File '" + file.getAbsolutePath() + "' does not exist!");

    Document d = new SAXReader().read(file);

    CategoryMenu menu = new DefaultCategoryMenu("Buy & Build");

    List<Node> rows = d.selectNodes("Menu/SlotRow");
    if (rows.size() > 2) {
        throw new SettlerCraftException("Max rows is 2 for menu.xml");
    }/*from w ww . ja v  a2 s  .  co  m*/

    boolean hasRow2 = false;

    for (int row = 0; row < rows.size(); row++) {
        Node rowNode = rows.get(row);
        List<Node> slotNodes = rowNode.selectNodes("Slot");
        // Slot #1 is reserved for all category
        if (row == 0 && slotNodes.size() > 8) {
            throw new SettlerCraftException(" 'SlotRow#1' has max 8 slots to customize");
        } else if (slotNodes.size() > 9) {
            throw new SettlerCraftException(" 'SlotRow#" + (row + 2) + "' has max 9 slots to customize");
        }
        int count = 0;

        for (Node categorySlotNode : slotNodes) {

            if (!categorySlotNode.hasContent()) {
                count++;
                continue;
            }

            Node mId = categorySlotNode.selectSingleNode("MaterialID");
            Node cat = categorySlotNode.selectSingleNode("Category");
            //                Node ali = categorySlotNode.selectSingleNode("Aliases");

            if (mId == null) {
                throw new SettlerCraftException("Missing 'MaterialID' element in 'SlotRow#" + (row + 1)
                        + "' 'Slot#" + (count + 1) + "'");
            }
            if (cat == null) {
                throw new SettlerCraftException(
                        "Missing 'Category' element in 'SlotRow#" + (row + 1) + "' 'Slot#" + (count + 1) + "'");
            }

            int id;
            try {
                id = Integer.parseInt(mId.getText());
            } catch (NumberFormatException nfe) {
                throw new SettlerCraftException("Invalid number for 'MaterialID' element in 'SlotRow#"
                        + (row + 1) + "' 'Slot#" + (count + 1) + "'");
            }

            Node catNameNode = cat.selectSingleNode("Name");
            if (catNameNode == null) {
                throw new SettlerCraftException(
                        "Missing 'Name' element in 'SlotRow#" + (row + 1) + "' 'Slot#" + (count + 1) + "'");
            }

            String category = catNameNode.getText();
            if (category.isEmpty()) {
                Element catEl = (Element) cat;
                category = catEl.attributeValue("value");
            }
            if (category.trim().isEmpty()) {
                throw new SettlerCraftException("Empty 'Category' element in 'SlotRow#" + (row + 1)
                        + "' and 'Slot#" + (count + 1) + "'");
            }
            category = category.replaceAll(" AND ", "&");

            Node synonymsNode = cat.selectSingleNode("Synonyms");

            // Set aliases
            String[] synonyms;
            if (synonymsNode == null) {
                synonyms = new String[0];
            } else {
                List<Node> synonymNodes = synonymsNode.selectNodes("Synonym");
                synonyms = new String[synonymNodes.size()];
                for (int j = 0; j < synonymNodes.size(); j++) {
                    String synonym = synonymNodes.get(j).getText();

                    if (synonym.isEmpty()) {
                        Element synoEl = (Element) cat;
                        synonym = synoEl.attributeValue("value");
                    }
                    if (synonym.trim().isEmpty()) {
                        throw new SettlerCraftException("Empty 'Synonym' element in  'SlotRow#" + (row + 1)
                                + "' and 'Slot#" + (count + 1) + "' and 'Synonym#" + (j + 1) + "'");
                    }

                    synonyms[j] = synonymNodes.get(j).getText();
                }
            }
            int slot = count;
            if (row == 0) {
                slot += 1; // slot 0 is reserved...
            } else {

                hasRow2 = true;
            }

            CategorySlot categorySlot = SlotFactory.getInstance().createCategorySlot(category, id);
            categorySlot.addSynonyms(synonyms);
            menu.setCategorySlot((row * 9) + slot, categorySlot);

            count++;
        }
        // fill remaining
        if (count < 8 && row == 0) {
            for (int i = count; i < 8; i++) {
                menu.setLocked(i);
            }

        } else if (row > 0 && count < 9) {
            for (int i = count; i < 9; i++) {
                menu.setLocked((row * 9) + i);
            }
        }
    }

    if (hasRow2) {
        menu.setLocked(19, 20, 21, 22, 23, 24, 25);
        menu.setActionSlot(18, "Previous", 173); // block of coal
        menu.setActionSlot(26, "Next", 173);
    } else {
        menu.setLocked(10, 11, 12, 13, 14, 15, 16);
        menu.setActionSlot(9, "Previous", 173);
        menu.setActionSlot(17, "Next", 173);
    }
    return menu;
}

From source file:com.chingo247.structureapi.plan.holograms.StructureHologramLoader.java

License:Open Source License

@Override
public List<StructureHologram> load(Element hologramsElement) throws StructureDataException {
    if (hologramsElement == null) {
        throw new AssertionError("Overviews element was null");
    }/*from   w ww  . j  av  a2 s.co m*/

    if (!hologramsElement.getName().equals(Elements.STRUCTURE_HOLOGRAMS)) {
        throw new AssertionError("Expected '" + Elements.STRUCTURE_HOLOGRAMS + "' element, but got '"
                + hologramsElement.getName() + "'");
    }

    List<StructureHologram> holograms = new ArrayList<>();
    List<Node> hologramNodes = hologramsElement.selectNodes(Elements.STRUCTURE_HOLOGRAM);
    int count = 0;

    for (Node hologramNode : hologramNodes) {

        Node xNode = hologramNode.selectSingleNode(Elements.X);
        Node yNode = hologramNode.selectSingleNode(Elements.Y);
        Node zNode = hologramNode.selectSingleNode(Elements.Z);
        if (xNode == null) {
            throw new StructureDataException("Missing 'X' node for 'StructureOverview#" + count + "'");
        }
        if (yNode == null) {
            throw new StructureDataException("Missing 'Y' node for 'StructureOverview#" + count + "'");
        }
        if (zNode == null) {
            throw new StructureDataException("Missing 'Z' node for 'StructureOverview#" + count + "'");
        }

        try {
            int x = Integer.parseInt(xNode.getText());
            int y = Integer.parseInt(yNode.getText());
            int z = Integer.parseInt(zNode.getText());

            List<Node> lines = hologramNode.selectNodes("Lines/Line");
            String[] tArray = new String[lines.size()];
            for (int i = 0; i < tArray.length; i++) {
                tArray[i] = (lines.get(i)).getText();
            }
            holograms.add(new StructureHologram(x, y, z, tArray));

        } catch (NumberFormatException nfe) {
            throw new StructureDataException(
                    "Values for (x,y,z) are not of type number in 'StructureOverview#" + count + "'");
        }
        count++;

    }

    return holograms;
}

From source file:com.chingo247.structureapi.plan.holograms.StructureHologramValidator.java

License:Open Source License

@Override
public void validate(Element e) throws StructureDataException {
    List<Node> nodes = e.selectNodes(Nodes.HOLOGRAM_NODE);

    if (nodes != null && !nodes.isEmpty()) {
        int count = 0;
        for (Node n : nodes) {
            Node xNode = n.selectSingleNode(Elements.X);
            Node yNode = n.selectSingleNode(Elements.Y);
            Node zNode = n.selectSingleNode(Elements.Z);

            if (xNode == null) {
                throw new StructureDataException("Missing 'X' node for 'Hologram#" + count + "'");
            }/*  w  ww  .  j a  v  a2s . com*/

            if (yNode == null) {
                throw new StructureDataException("Missing 'Y' node for 'Hologram#" + count + "'");
            }

            if (zNode == null) {
                throw new StructureDataException("Missing 'Z' node for 'Hologram#" + count + "'");
            }
            try {
                Integer.parseInt(xNode.getText());
            } catch (NumberFormatException nfe) {
                throw new StructureDataException("Invalid X value should 'Hologram#" + count + "'");
            }

            try {
                Integer.parseInt(yNode.getText());
            } catch (NumberFormatException nfe) {
                throw new StructureDataException("Invalid Y value for 'Hologram#" + count + "'");
            }

            try {
                Integer.parseInt(zNode.getText());
            } catch (NumberFormatException nfe) {
                throw new StructureDataException("Invalid Z value for 'Hologram#" + count + "'");
            }

            Node linesNode = n.selectSingleNode("Lines");
            if (linesNode == null) {
                throw new StructureDataException("Missing 'Lines' node for 'Hologram#" + count + "'");
            }

            List<Node> lineNodes = n.selectNodes("Lines/Line");
            if (lineNodes == null || lineNodes.isEmpty()) {
                throw new StructureDataException("Missing 'Line' nodes for 'Hologram#" + count + "'");
            }

            count++;
        }
    }
}