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:ca.upei.roblib.fedora.servletfilter.DrupalAuthModule.java

License:Apache License

/**
 * @deprecated/*from   w  w w.java  2  s.c  om*/
 *   This is only still here because it was public... Just in case.
 * @param file
 * @return
 * @throws DocumentException
 */
public Document parse(File file) throws DocumentException {
    SAXReader reader = new SAXReader();
    Document document = reader.read(file);
    return document;
}

From source file:cbir.reader.XMLReader.java

License:Open Source License

/**
 * Parses image information from a given XML file and returns a list of
 * images with their descriptors (only MPEG_EHD and CEDD extracted by
 * img(rummager))./*from ww  w  .  ja v a2s . c o  m*/
 * 
 * @param file
 *            The given img(rummager) XML file.
 * @return a list of images with their descriptors.
 * @throws DocumentException
 */
public List<ImageContainer> parseXMLFile(File file) throws DocumentException {

    // Read XML file
    SAXReader reader = new SAXReader();
    Document document = reader.read(file);

    // Get first Element
    Element rootElement = document.getRootElement();

    // Iterate + get image paths + descriptors

    // outer loop (Info, Data)
    for (@SuppressWarnings("rawtypes")
    Iterator iter = rootElement.elementIterator(); iter.hasNext();) {
        Element element = (Element) iter.next();

        // inner loop (Image Data)
        for (@SuppressWarnings("rawtypes")
        Iterator innerIter = element.elementIterator(); innerIter.hasNext();) {
            Element innerElement = (Element) innerIter.next();

            // data loop (Filename, EHD, RGB)
            for (@SuppressWarnings("rawtypes")
            Iterator dataIter = innerElement.elementIterator(); dataIter.hasNext();) {

                // get Filename
                Element dataElement = (Element) dataIter.next();
                String filename = new String(dataElement.getText());
                List<Descriptor> descriptors = new LinkedList<Descriptor>();
                while (dataIter.hasNext()) {
                    // get descriptors
                    dataElement = (Element) dataIter.next();
                    if (dataElement.getName().equals("EHD") || dataElement.getName().equals("CEDD"))
                        descriptors.add(readDescriptor(dataElement));
                }

                // construct Image and save in list
                ImageContainer image = new ImageContainer(filename,
                        descriptors.toArray(new Descriptor[descriptors.size()]));
                imageList.add(image);
            }
        }
    }
    return imageList;

}

From source file:cc.dectinc.video.tagging.tools.MetaImporter.java

/**
 * Given path, load the metadata of the fiven video file
 * //from w  w w.  j  a v a 2s.  com
 * @param path
 */
private void parseFile(String path) {
    File file = new File(path);
    if (!file.exists()) {
        // System.out.println("File not found: " + file.getName());
        return;
    }
    Document document = null;
    try {
        SAXReader reader = new SAXReader();
        document = reader.read(file);
        Element root = document.getRootElement();
        Meta meta = new Meta();

        meta.set("identifier", root.elementText("identifier"));
        meta.set("title", root.elementText("title"));
        meta.set("mediatype", root.elementText("mediatype"));
        // meta.set("resource", root.elementText("licenseurl"));
        meta.set("uploader", root.elementText("uploader"));
        // meta.set("description", root.elementText("description"));
        // meta.set("year", Integer.parseInt(root.elementText("date")));
        // meta.set("holder", root.elementText(""));
        DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        meta.set("pubdate", df.parse(root.elementText("publicdate")));

        meta.save();
    } catch (Exception e) {
        e.printStackTrace();
    }
}

From source file:cc.warlock.build.FeatureVersionReplacer.java

License:Open Source License

protected void replaceVersions() throws BuildException {
    SAXReader reader = new SAXReader();

    try {//from   ww w  .jav  a  2s  .com
        File featureFile = new File(new File(new File(buildDirectory, "features"), feature), "feature.xml");
        Document featureDoc = reader.read(featureFile);
        Element featureEl = featureDoc.getRootElement();
        featureEl.attribute("version").setValue(version);

        //          Properties versions = new Properties();
        //          File versionsFile = new File(buildDirectory, "finalPluginsVersions.properties");
        //          FileInputStream stream = new FileInputStream(versionsFile);
        //          versions.load(stream);
        //          stream.close();
        //          
        //          for (Element pluginEl : (List<Element>) featureEl.elements("plugin"))
        //          {
        //             if (pluginEl.attributeValue("version").equals("0.0.0"))
        //             {
        //                if (versions.containsKey(pluginEl.attributeValue("id")))
        //                   pluginEl.attribute("version").setValue(versions.getProperty(pluginEl.attributeValue("id")));
        //             }
        //          }

        OutputFormat format = OutputFormat.createPrettyPrint();

        FileOutputStream outStream = new FileOutputStream(featureFile);
        XMLWriter writer = new XMLWriter(outStream, format);
        writer.write(featureDoc);
        outStream.close();

    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:cc.warlock.core.configuration.WarlockConfiguration.java

License:Open Source License

protected void loadXML() {
    if (configFile.length() == 0)
        return;//ww  w .j a va  2s .co  m

    try {
        SAXReader reader = new SAXReader();
        document = reader.read(configFile);
        warlockConfigElement = document.getRootElement();

        for (Element element : (List<Element>) warlockConfigElement.elements()) {
            unhandledElements.add(element);
        }

    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:cc.warlock.rcp.help.WikiCategory.java

License:Open Source License

public static List<WikiPage> search(String text) {
    ArrayList<WikiPage> pages = new ArrayList<WikiPage>();

    try {/*from   www  .  j a  v a 2 s. c o  m*/
        URL url = new URL(MEDIAWIKI_API + "?action=query&list=search&srsearch=" + text + "&format=xml");
        SAXReader reader = new SAXReader();
        Document document = reader.read(url);

        for (Element page : (List<Element>) document.selectNodes("//p")) {
            WikiPage wikiPage = new WikiPage(null, page.attributeValue("title"));

            pages.add(wikiPage);
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return pages;
}

From source file:cc.warlock.rcp.help.WikiCategory.java

License:Open Source License

protected void loadPages() {
    try {/* w w w  .  ja v a 2 s.c  o m*/
        URL url = new URL(
                MEDIAWIKI_API + "?action=query&list=categorymembers&cmcategory=" + name + "&format=xml");

        SAXReader reader = new SAXReader();
        Document document = reader.read(url);

        for (Element page : (List<Element>) document.selectNodes("//cm")) {
            WikiPage wikiPage = new WikiPage(page.attributeValue("pageid"), page.attributeValue("title"));

            pages.add(wikiPage);
        }
    } catch (MalformedURLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (DocumentException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

From source file:ch.epfl.codimsd.qep.QEPFactory.java

License:Open Source License

/**
 * Reads a qep file in xml or string format, and build the hashtable of opNode structures
 * representing the operators./*from   w  ww. j a v  a  2 s . c o m*/
 * 
 * If this method is called remotly, the parameter "type" should be set to Constants.qepAccessTypeRemote;
 * in order to read from a string file.
 * 
 * @param qepFile path to QEP file.
 * @param type type of access (remote mode or centralized mode).
 * @param qep this qep. It is send in order to be filled with corresponding hashtable.
 * @return hashtable containing opNode structures.
 * @throws QEPInitializationException
 */
public static Hashtable<String, OpNode> loadQEP(String qepFile, String type, QEP qep)
        throws QEPInitializationException {

    // Initializations.
    Hashtable<String, OpNode> operatorList = null;
    Document document = null;

    try {
        // Call Sax reader in order to parse the xml document.
        SAXReader reader = new SAXReader();
        document = reader.read(qepFile);
        qep.setDocument(document);

        // Build the hashtable of opNodes.
        operatorList = buildOpNodeTable(document);

    } catch (DocumentException ex) {
        ex.printStackTrace();
        throw new QEPInitializationException(
                "DocumentException error while loadind the XML QEP : " + ex.getMessage());
    }

    return operatorList;
}

From source file:ch.javasoft.metabolic.efm.config.DistributedConfig.java

License:BSD License

private static final Element getDistConfigFromPackage() throws XmlConfigException, IOException {
    final String xmlName = "config/metabolic-efm.xml";
    InputStream xmlIn = DistributedConfig.class.getClassLoader().getResourceAsStream(xmlName);
    if (xmlIn == null) {
        throw new IOException("cannot find resource: " + xmlName);
    }/*from   w  w w .  j  a v  a  2  s  .  co  m*/
    try {
        final SAXReader reader = new SAXReader();
        final Document doc = reader.read(xmlIn);
        final Element root = doc.getRootElement();
        final Element elDist = root.elementByID(XmlElement.distribute.getXmlName());
        //         final String defName = root.attributeValue("default", "default");
        //         final Element elConfig    = XmlUtil.getChildElementByAttributeValue(root, ch.javasoft.xml.config.XmlConfig.XmlElement.config, XmlAttribute.name, defName, true /*throwExceptionIfNull*/);
        //         final Element elMetEfm    = XmlUtil.getRequiredSingleChildElement(elConfig, ch.javasoft.metabolic.efm.main.CalculateFluxModes.XmlElement.metabolic_efm);
        //         final Element elImpl      = XmlUtil.getRequiredSingleChildElement(elMetEfm, ch.javasoft.metabolic.efm.main.CalculateFluxModes.XmlElement.efm_impl);
        //         final Element elImplCfg   = XmlUtil.getRequiredSingleChildElement(elImpl, ch.javasoft.metabolic.efm.main.CalculateFluxModes.XmlElement.config);
        //         final Element elDist      = XmlUtil.getRequiredSingleChildElement(elImplCfg, XmlElement.distribute);
        return elDist;
    } catch (DocumentException e) {
        throw ExceptionUtil.toIOException("cannot parse " + xmlName + ", e=" + e, e);
    } finally {
        xmlIn.close();
    }
}

From source file:cinnamon.global.Conf.java

License:Open Source License

public Conf(String configName) {
    String separator = File.separator;
    File configFile = null;/* w ww.j a  v  a 2  s  .co m*/
    try {
        String currentDir = new File(".").getAbsolutePath();
        configPath = currentDir.substring(0, currentDir.length() - 1) + "webapps" + separator + "cinnamon"
                + separator + configName;
        configFile = new File(configPath);
        if (!configFile.exists()) {
            log.debug("Config file not found in " + configPath);
            log.debug("Will try in CINNAMON_HOME_DIR='" + System.getenv().get("CINNAMON_HOME_DIR") + "'");
        }
    } catch (AccessControlException ace) {
        log.error("AccessControlException: " + ace.getLocalizedMessage(), ace);
    } finally {
        if (configFile == null || !configFile.exists()) {
            // try do instantiate the configuration via environment var:
            String path = System.getenv().get("CINNAMON_HOME_DIR");
            setConfigPath(path + separator + configName);
            log.debug("ConfigPath: " + getConfigPath());
            configFile = new File(getConfigPath());
        }
    }

    log.debug("ConfigPath: " + configPath);
    SAXReader reader = new SAXReader();

    try {
        File cfg = new File(configPath);
        xml = reader.read(cfg);
        configureUseSessionLogging(xml);
    } catch (Exception ex) {
        log.debug("", ex);
        emergencyLog("Failure loading config file: " + ex.getMessage());
        throw new RuntimeException(ex);
    }
}