Example usage for org.dom4j.io SAXReader SAXReader

List of usage examples for org.dom4j.io SAXReader SAXReader

Introduction

In this page you can find the example usage for org.dom4j.io SAXReader SAXReader.

Prototype

public SAXReader() 

Source Link

Usage

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 w w .  j  a v a 2  s.  c om*/

    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.document.AbstractDocument.java

AbstractDocument(File documentFile) throws DocumentException {
    this.documentFile = documentFile;
    this.document = new SAXReader().read(documentFile);
}

From source file:com.chingo247.structureapi.plan.document.Loader.java

License:Open Source License

public List<T> load(File configXML) throws DocumentException, StructureDataException {
    return load(new SAXReader().read(configXML));
}

From source file:com.chingo247.structureapi.plan.document.PlanDocument.java

License:Open Source License

PlanDocument(PlanDocumentManager documentManager, File documentFile) throws DocumentException {
    super(documentFile);
    this.documentFile = documentFile;
    this.document = new SAXReader().read(documentFile);
    this.documentManager = documentManager;
}

From source file:com.chingo247.structureapi.plan.document.PlanDocumentManager.java

License:Open Source License

/**
 * Loads all planDocuments & structureDocuments Multi-Core. Number of cores used is defined by
 * the number of cores available using Runtime.getRuntime.availableProcessors()
 *//*from   w ww.j  av  a2  s  .  c  om*/
@Override
public synchronized void loadDocuments() {

    // Go throug all XML files inside the 'Plans' folder
    Iterator<File> it = FileUtils.iterateFiles(structureAPI.getPlanDataFolder(), new String[] { "xml" }, true);
    final List<Future> tasks = new LinkedList<>();
    while (it.hasNext()) {
        final File planDocFile = it.next();
        tasks.add(executor.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    SAXReader reader = new SAXReader();
                    Document d = reader.read(planDocFile);
                    // If the RootElement is not 'StructurePlan', skip it
                    if (!isStructurePlan(d)) {
                        return;
                    }
                    List<Element> elements = d.getRootElement().elements();

                    // Form plan document
                    PlanDocument planDocument = new PlanDocument(PlanDocumentManager.this, planDocFile);
                    for (Element pluginElement : elements) {
                        planDocument.putPluginElement(pluginElement.getName(), new PlanDocumentPluginElement(
                                pluginElement.getName(), planDocument, pluginElement));
                    }
                    // Save the document
                    PlanDocumentManager.this.put(planDocument.getRelativePath(), planDocument);
                } catch (DocumentException ex) {
                    Logger.getLogger(PlanDocumentManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }));
    }

    // Block until all tasks are done
    for (Future f : tasks) {
        try {
            f.get();
        } catch (InterruptedException | ExecutionException ex) {
            Logger.getLogger(PlanDocumentManager.class.getName()).log(Level.SEVERE, null, ex);
            for (Future fu : tasks) {
                fu.cancel(true);
            }
        }
    }
}

From source file:com.chingo247.structureapi.plan.document.StructureDocumentManager.java

/**
 * Loads all structureDocuments multi-threaded. Number of cores used is defined by
 * the number of cores available using Runtime.getRuntime.availableProcessors()
 *//* ww  w .j av  a 2 s.c  o  m*/
@Override
public synchronized void loadDocuments() {
    Session session = HibernateUtil.getSession();
    JPQLQuery query = new HibernateQuery(session);
    QStructure qs = QStructure.structure;
    List<Structure> structures = query.from(qs).where(qs.state.ne(Structure.State.REMOVED)).list(qs);
    session.close();

    final List<Future> tasks = new LinkedList<>();

    for (final Structure structure : structures) {
        final File structureDocFile = structureAPI.getStructurePlanFile(structure);
        tasks.add(executor.submit(new Runnable() {

            @Override
            public void run() {
                try {
                    SAXReader reader = new SAXReader();
                    Document d = reader.read(structureDocFile);
                    // If the RootElement is not 'StructurePlan', skip it
                    if (!isStructurePlan(d)) {
                        return;
                    }
                    List<Element> elements = d.getRootElement().elements();

                    // Form plan document
                    StructureDocument structureDocument = new StructureDocument(StructureDocumentManager.this,
                            structure, structureDocFile);
                    for (Element pluginElement : elements) {
                        structureDocument.putPluginElement(pluginElement.getName(),
                                new StructureDocumentPluginElement(pluginElement.getName(), structureDocument,
                                        pluginElement));
                    }
                    // Save the document
                    put(structure.getId(), structureDocument);
                } catch (DocumentException ex) {
                    Logger.getLogger(PlanDocumentManager.class.getName()).log(Level.SEVERE, null, ex);
                }
            }
        }));
    }

    // Block until all tasks are done
    for (Future f : tasks) {
        try {
            f.get();
        } catch (InterruptedException | ExecutionException ex) {
            Logger.getLogger(PlanDocumentManager.class.getName()).log(Level.SEVERE, null, ex);
            for (Future fu : tasks) {
                fu.cancel(true);
            }
        }
    }

}

From source file:com.chingo247.structureapi.plan.document.Validator.java

public void validate(File configXML) throws DocumentException, StructureDataException {
    validate(new SAXReader().read(configXML));
}

From source file:com.chingo247.structureapi.plan.StructurePlanManager.java

License:Open Source License

private boolean isStructurePlan(File file) throws DocumentException {
    SAXReader reader = new SAXReader();
    Document d = reader.read(file);
    return d.getRootElement().getName().equals("StructurePlan");
}

From source file:com.chingo247.structureapi.plan.util.PlanDocumentUtil.java

License:Open Source License

public static boolean isStructurePlan(File structurePlanFile) {
    SAXReader reader = new SAXReader();
    try {/*from  www. java 2  s  . c  o  m*/
        Document d = reader.read(structurePlanFile);
        return isStructurePlan(d);
    } catch (DocumentException ex) {
        Logger.getLogger(PlanDocumentUtil.class.getName()).log(Level.SEVERE, null, ex);
    }
    return false;
}

From source file:com.christophermrossi.jpt.PageTemplateImpl.java

License:Open Source License

static final SAXReader getHTMLReader() throws Exception {
    if (htmlReader == null) {
        htmlReader = new SAXReader();
        SAXParser parser = new SAXParser();
        parser.setProperty("http://cyberneko.org/html/properties/names/elems", "match");
        parser.setProperty("http://cyberneko.org/html/properties/names/attrs", "no-change");
        parser.setProperty("http://cyberneko.org/html/properties/default-encoding", "UTF-8");
        htmlReader.setXMLReader(parser);
    }/*from  ww  w  . j  av a2 s. c  om*/
    return htmlReader;
}