Example usage for org.dom4j.io SAXReader read

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

Introduction

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

Prototype

public Document read(InputSource in) throws DocumentException 

Source Link

Document

Reads a Document from the given InputSource using SAX

Usage

From source file:com.celexus.conniption.foreman.util.XMLHandler.java

License:Apache License

private Document getDocument(String response) throws UtilityException {
    SAXReader reader = new SAXReader();
    Document document;/*from  w  ww .j a va2  s  .  c o  m*/
    try {
        document = reader.read(new ByteArrayInputStream(response.getBytes()));
    } catch (DocumentException e) {
        throw new UtilityException("Parse response failed", e);
    }
    return document;
}

From source file:com.chinarewards.license.util.XmlUtil_dom4j.java

public static Document loadXml(String filename) {
    Document document = null;//www .j a  va 2s  .  co m
    try {
        SAXReader saxReader = new SAXReader();
        document = saxReader.read(new File(filename));
    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return document;
}

From source file:com.chinarewards.license.util.XmlUtil_dom4j.java

public static Document readResult(StringBuffer paramStringBuffer) {
    Document localDocument = null;
    StringReader localStringReader = new StringReader(paramStringBuffer.toString());
    SAXReader localSAXReader = new SAXReader();
    try {//from   w ww .  java2s  .com
        localDocument = localSAXReader.read(localStringReader);
    } catch (DocumentException localDocumentException) {
        localDocumentException.printStackTrace();
        System.out.println(localDocumentException.getMessage());
    }
    return localDocument;
}

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 w  w .  j  av a2 s .  co m*/
@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()
 *//*from   w w  w .  j  a  va 2s . 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.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  w w  w  .  ja va2 s.  co 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.HTMLFragment.java

License:Open Source License

private void parseFragment() throws PageTemplateException {
    try {//w  ww  .  j av  a2 s . co  m
        StringBuffer fragment = new StringBuffer(html.length() + 26);
        fragment.append("<html><body>");
        fragment.append(html);
        fragment.append("</body></html>");

        Reader input = new StringReader(fragment.toString());
        SAXReader reader = PageTemplateImpl.getXMLReader();
        try {
            dom = reader.read(input).getRootElement().element("body");
        } catch (DocumentException e) {
            try {
                reader = PageTemplateImpl.getHTMLReader();
                input.close();
                input = new StringReader(fragment.toString());
                dom = reader.read(input).getRootElement().element("body");
            } catch (NoClassDefFoundError ee) {
                // Allow user to omit nekohtml package
                // to disable html parsing
                //System.err.println( "Warning: no nekohtml" );
                //ee.printStackTrace();
                throw e;
            }
        }
    } catch (Exception e) {
        throw new PageTemplateException(e);
    }
}

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

License:Open Source License

public PageTemplateImpl(InputStream input, Resolver resolver) throws PageTemplateException {
    try {/* w w w  . ja v  a 2s  . com*/
        this.uri = null;
        this.userResolver = resolver;

        SAXReader reader = getXMLReader();
        try {
            template = reader.read(input);
        } catch (DocumentException e) {
            try {
                reader = getHTMLReader();
                template = reader.read(input);
            } catch (NoClassDefFoundError ee) {
                // Allow user to omit nekohtml package
                // to disable html parsing
                //System.err.println( "Warning: no nekohtml" );
                //ee.printStackTrace();
                throw e;
            }
        }
    } catch (Exception e) {
        throw new PageTemplateException(e);
    }
}

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

License:Open Source License

public PageTemplateImpl(URL url) throws PageTemplateException {
    try {/*from  w  ww.j av a  2  s . c o m*/
        this.uri = new URI(url.toString());
        SAXReader reader = getXMLReader();
        try {
            template = reader.read(url);
        } catch (DocumentException e) {
            try {
                reader = getHTMLReader();
                template = reader.read(url);
            } catch (NoClassDefFoundError ee) {
                // Allow user to omit nekohtml package
                // to disable html parsing
                System.err.println("Warning: no nekohtml");
                ee.printStackTrace();
                throw e;
            }
        }
    } catch (RuntimeException e) {
        throw e;
    } catch (Exception e) {
        throw new PageTemplateException(e);
    }
}