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.mpaike.core.config.xml.XMLConfigService.java

License:Open Source License

protected void parse(InputStream stream) {
    Map<String, ConfigElementReader> parsedElementReaders = null;
    Map<String, Evaluator> parsedEvaluators = null;
    List<ConfigSection> parsedConfigSections = new ArrayList<ConfigSection>();

    String currentArea = null;/*  w  ww  .  ja va 2  s . c o m*/
    try {
        // get the root element
        SAXReader reader = new SAXReader();
        Document document = reader.read(stream);
        Element rootElement = document.getRootElement();

        // see if there is an area defined
        currentArea = rootElement.attributeValue("area");

        // parse the plug-ins section of a config file
        Element pluginsElement = rootElement.element(ELEMENT_PLUG_INS);
        if (pluginsElement != null) {
            // parse the evaluators section
            parsedEvaluators = parseEvaluatorsElement(pluginsElement.element(ELEMENT_EVALUATORS));

            // parse the element readers section
            parsedElementReaders = parseElementReadersElement(pluginsElement.element(ELEMENT_ELEMENT_READERS));
        }

        // parse each config section in turn
        @SuppressWarnings("unchecked")
        Iterator<Element> configElements = rootElement.elementIterator(ELEMENT_CONFIG);
        while (configElements.hasNext()) {
            Element configElement = configElements.next();
            parsedConfigSections.add(parseConfigElement(parsedElementReaders, configElement, currentArea));
        }
    } catch (Throwable e) {
        if (e instanceof ConfigException) {
            throw (ConfigException) e;
        } else {
            throw new ConfigException("Failed to parse config stream", e);
        }
    }

    try {
        // valid for this stream, now add to config service ...

        if (parsedEvaluators != null) {
            for (Map.Entry<String, Evaluator> entry : parsedEvaluators.entrySet()) {
                // add the evaluators to the config service
                addEvaluator(entry.getKey(), entry.getValue());
            }
        }

        if (parsedElementReaders != null) {
            for (Map.Entry<String, ConfigElementReader> entry : parsedElementReaders.entrySet()) {
                // add the element readers to the config service
                addConfigElementReader(entry.getKey(), entry.getValue());
            }
        }

        if (parsedConfigSections != null) {
            for (ConfigSection section : parsedConfigSections) {
                // add the config sections to the config service
                addConfigSection(section, currentArea);
            }
        }
    } catch (Throwable e) {
        throw new ConfigException("Failed to add config to config service", e);
    }
}

From source file:com.ms.commons.test.datareader.impl.TreeXmlReaderUtil.java

License:Open Source License

public static TreeDatabase read(String fileName) {
    String absPath = getAbsolutedPath(fileName);
    File file = new File(absPath);
    if (file.exists()) {
        checkDataFile(absPath);/*from   w ww  .j  av  a  2s  . co  m*/
    }

    TreeDatabase database = new TreeDatabase();
    SAXReader reader = new SAXReader();
    Document doc;
    InputStream inputStream = null;
    try {
        inputStream = new BufferedInputStream(new FileInputStream(file));
        XStream xStream = new XStream();
        doc = reader.read(inputStream);

        initAlias(doc, xStream);

        List<TreeObject> objects = readObjects(doc, xStream);

        database.setTreeObjects(objects);
        inputStream.close();
    } catch (FileNotFoundException e) {
        throw new RuntimeException(e);
    } catch (DocumentException e) {
        throw new RuntimeException(e);
    } catch (IOException e) {
        throw new RuntimeException(e);
    } finally {
        close(inputStream);
    }
    return database;
}

From source file:com.ms.commons.test.datareader.impl.XmlReaderUtil.java

License:Open Source License

@SuppressWarnings("unchecked")
public static MemoryDatabase readDocument(String file) {
    try {/*from   www . j a v a 2 s. co  m*/
        SAXReader reader = new SAXReader();
        Document doc = reader.read(new BufferedInputStream(new FileInputStream(getAbsolutedPath(file))));
        List<Element> elements = doc.getRootElement().elements("table");
        List<MemoryTable> tableList = new ArrayList<MemoryTable>();
        for (Element tableE : elements) {
            tableList.add(readTable(tableE));
        }

        MemoryDatabase database = new MemoryDatabase();
        database.setTableList(tableList);
        return database;
    } catch (FileNotFoundException e) {
        throw new ResourceNotFoundException(e);
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}

From source file:com.msg.wmTestHelper.util.XmlUtil.java

License:Apache License

/**
 * Parse an XML file via dom4j. Can throw a runtime exception.
 *
 * @param xmlFile the xml file.//w ww  . j  av a  2 s.c  om
 * @return A dom4j document.
 */
public static Document parse(File xmlFile) {
    try {
        SAXReader reader = new SAXReader();

        return reader.read(xmlFile);
    } catch (DocumentException e) {
        String reason = String.format("Unable to parse file '%s'", xmlFile.getAbsolutePath());
        throw new TestHelperException(reason, e);
    }

}

From source file:com.msg.wmTestHelper.util.XmlUtil.java

License:Apache License

/**
 * Parse an XML file via dom4j. Can throw a runtime exception.
 *
 * @param xmlStream the xml stream./* w  w  w  .j a  va  2 s.c om*/
 * @return A dom4j document.
 */
public static Document parse(InputStream xmlStream) {
    try {
        SAXReader reader = new SAXReader();

        return reader.read(xmlStream);
    } catch (DocumentException e) {
        String reason = String.format("Unable to parse stream");
        throw new TestHelperException(reason, e);
    }

}

From source file:com.mycompany.simple.weather.YahooParser.java

License:Apache License

public Weather parse(InputStream inputStream) throws Exception {
    Weather weather = new Weather();

    log.info("Creating XML Reader");
    SAXReader xmlReader = createXmlReader();
    Document doc = xmlReader.read(inputStream);

    log.info("Parsing XML Response");
    weather.setCity(doc.valueOf("/rss/channel/y:location/@city"));
    weather.setRegion(doc.valueOf("/rss/channel/y:location/@region"));
    weather.setCountry(doc.valueOf("/rss/channel/y:location/@country"));
    weather.setCondition(doc.valueOf("/rss/channel/item/y:condition/@text"));
    weather.setTemp(doc.valueOf("/rss/channel/item/y:condition/@temp"));
    weather.setChill(doc.valueOf("/rss/channel/y:wind/@chill"));
    weather.setHumidity(doc.valueOf("/rss/channel/y:atmosphere/@humidity"));

    return weather;
}

From source file:com.neu.utils.io.NetworkToXMLUtil.java

License:Open Source License

public static List<List<String>> unicomFilmXmlRead(String fileName) throws DocumentException {

    List<List<String>> cc = new ArrayList<List<String>>();

    File inputXml = new File(fileName);
    SAXReader saxReader = new SAXReader();

    Document document = saxReader.read(inputXml);

    Element employees = document.getRootElement();
    for (Iterator i = employees.elementIterator(); i.hasNext();) {
        Element employee = (Element) i.next();
        List<String> list = new ArrayList<String>();
        for (Iterator j = employee.elementIterator(); j.hasNext();) {
            Element node = (Element) j.next();
            list.add(node.getText());//from  ww w.  ja  va2  s  .co  m
        }
        cc.add(list);
    }
    return cc;
}

From source file:com.nokia.ant.AtsCondition.java

License:Open Source License

/** Read from diamonds and signal if ats failed */
public boolean eval() {
    String bid = project.getProperty("diamonds.build.id");
    if (bid == null)
        log.info("Diamonds not enabled");
    else {/*from  w  w  w .j a  va2  s .co m*/
        boolean testsfound = false;
        log.info("Looking for tests in diamonds");
        SAXReader xmlReader = new SAXReader();

        while (!testsfound) {
            Document antDoc = null;

            try {
                URL url = new URL("http://" + project.getProperty("diamonds.host") + bid + "?fmt=xml");
                antDoc = xmlReader.read(url);
            } catch (Exception e) {
                // We are Ignoring the errors as no need to fail the build.
                log.error("Not able to read the Diamonds URL http://" + project.getProperty("diamonds.host")
                        + bid + "?fmt=xml" + e.getMessage());
            }

            for (Iterator iterator = antDoc.selectNodes("//test/failed").iterator(); iterator.hasNext();) {
                testsfound = true;
                Element e = (Element) iterator.next();
                String failed = e.getText();
                if (!failed.equals("0")) {
                    log.error("ATS tests failed");

                    for (Iterator iterator2 = antDoc.selectNodes("//actual_result").iterator(); iterator2
                            .hasNext();) {
                        Element e2 = (Element) iterator2.next();
                        log.error(e2.getText());
                    }
                    return false;
                }
            }

            int noofdrops = Integer.parseInt(project.getProperty("drop.file.counter"));
            if (noofdrops > 0) {
                int testsrun = antDoc.selectNodes("//test").size();
                if (testsrun < noofdrops) {
                    log.info(testsrun + " test completed, " + noofdrops + " total");
                    testsfound = false;
                }
            }
            if (!testsfound) {
                log.info("Tests not found sleeping for " + sleeptimesecs + " seconds");
                try {
                    Thread.sleep(sleeptimesecs * 1000);
                } catch (InterruptedException e) {
                    // This will not affect the build process so ignoring.
                    log.debug("Interrupted while reading ATS build status " + e.getMessage());
                }
            }
        }
    }
    return true;
}

From source file:com.nokia.ant.BuildStatusDef.java

License:Open Source License

/**
 * Find the xml Element for the target/*from   ww w  .  j  a va  2s.c  om*/
 * 
 */
public Element findTargetElement(Target target, Project project) {
    SAXReader xmlReader = new SAXReader();

    Document antDoc = null;

    String location = target.getLocation().getFileName();

    try {
        File file = new File(location);
        antDoc = xmlReader.read(file);
    } catch (Exception e) {
        // We are Ignoring the errors as no need to fail the build.
        log("Not able read the XML file. " + e.getMessage(), Project.MSG_WARN);
    }

    String projectName = antDoc.valueOf("/project/@name");
    for (Iterator iterator = antDoc.selectNodes("//target").iterator(); iterator.hasNext();) {
        Element e = (Element) iterator.next();

        String targetName = e.attributeValue("name");
        if (targetName.equals(target.getName()) || (projectName + "." + targetName).equals(target.getName()))
            return e;
    }
    return null;
}

From source file:com.nokia.ant.BuildStatusDef.java

License:Open Source License

public void checkTargetsProperties(Project project) {
    if (project.getProperty("data.model.file") != null) {
        SAXReader xmlReader = new SAXReader();
        Document antDoc = null;/*from  w  ww.ja va2s  .c om*/

        ArrayList customerProps = getCustomerProperties(project);

        try {
            File model = new File(project.getProperty("data.model.file"));
            antDoc = xmlReader.read(model);
        } catch (Exception e) {
            // We are Ignoring the errors as no need to fail the build.
            log("Not able read the data model file. " + e.getMessage(), Project.MSG_WARN);
        }

        List<Node> statements = antDoc.selectNodes("//property");

        for (Node statement : statements) {
            if (statement.valueOf("editStatus").equals("never")) {
                if (customerProps.contains(statement.valueOf("name"))) {
                    output.add("Warning: " + statement.valueOf("name") + " property has been overridden");
                }
            }
        }
    }
}