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.nokia.helium.diamonds.XMLMerger.java

License:Open Source License

/**
 * Create an XMLMerger, the merge file will be used as input and output.
 * /*from w w w  .j  a v a 2s .  com*/
 * @param merge
 * @throws XMLMergerException
 */
public XMLMerger(InputStream stream, File file) throws XMLMergerException {
    try {
        outputFile = file;
        mergeStream = stream;
        SAXReader reader = new SAXReader();
        doc = reader.read(mergeStream);
        root = doc.getRootElement();
    } catch (DocumentException e) {
        throw new XMLMergerException(e.getMessage());
    }
}

From source file:com.nokia.helium.diamonds.XMLMerger.java

License:Open Source License

public void merge(InputStream stream) throws XMLMergerException {
    try {//  www .  j  a  v  a  2s .  co  m
        SAXReader reader = new SAXReader();
        Document dataDoc = reader.read(stream);
        merge(dataDoc);
    } catch (DocumentException e) {
        throw new XMLMergerException(e.getMessage());
    }
}

From source file:com.nokia.helium.diamonds.XMLMerger.java

License:Open Source License

/**
 * Add all sub element of data file into the merged file. If the root
 * element name is different for the merged file an XMLMergerException is
 * thrown.//from   w w w.ja v a 2 s  .c o m
 * 
 * @param data
 *            the input file.
 * @throws XMLMergerException
 */
public void merge(File data) throws XMLMergerException {
    log.debug("Merging " + data.getAbsolutePath());
    try {
        SAXReader reader = new SAXReader();
        Document dataDoc = reader.read(data);
        Element dataRoot = dataDoc.getRootElement();
        merge(dataDoc);
    } catch (DocumentException e) {
        throw new XMLMergerException(e.getMessage());
    }
}

From source file:com.nokia.helium.sbs.SAXSysdefParser.java

License:Open Source License

/**
 * Constructor//from   w  w  w .  j  ava  2  s  .co m
 * 
 * @return list of available configurations that can be built.
 */
public void parseConfig(String nodeToGet) {
    layers = new ArrayList<String>();
    SAXReader reader = new SAXReader();
    reader.addHandler("/SystemDefinition/systemModel/" + nodeToGet, new ElementHandler() {
        public void onStart(ElementPath path) {
        }

        public void onEnd(ElementPath path) {
            Element row = path.getCurrent();
            Iterator itr = row.attributeIterator();
            while (itr.hasNext()) {
                Attribute child = (Attribute) itr.next();
                String attrName = child.getQualifiedName();
                if (attrName.equals("name")) {
                    layers.add(child.getValue());
                }
            }
            row.detach();
        }
    });
    try {
        reader.read(sysdefFile);
    } catch (DocumentException e) {
        e.printStackTrace();
    }
}

From source file:com.nokia.helium.scm.ant.actions.ChangelogAction.java

License:Open Source License

/**
 * {@inheritDoc}//ww  w.ja  v  a  2  s .c om
 */
@SuppressWarnings("unchecked")
@Override
public void execute(ScmRepository repository) throws ScmException {
    ScmManager scmManager = getTask().getScmManager();
    ScmRevision startRevision = new ScmRevision(startVersion);
    ScmRevision endRevision = new ScmRevision(endVersion);

    ChangeLogScmResult result;
    Date start = null;
    Date end = null;

    if (numDays == 0 && (startDate != null || endDate != null)) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(datePattern);
            start = format.parse(startDate);
            end = format.parse(endDate);
        } catch (ParseException e) {
            throw new ScmException("Date Format not supported:" + e.getMessage());
        }
    } else {
        start = null;
        end = null;
    }
    try {
        if (startVersion == null) {
            result = scmManager.changeLog(repository, getScmFileSet(), start, end, numDays, null, datePattern);
        } else {
            result = scmManager.changeLog(repository, getScmFileSet(), startRevision, endRevision, datePattern);
        }
    } catch (ScmException e) {
        throw new BuildException("Execution of SCM changelog action failed.");
    }
    if (!result.isSuccess()) {
        throw new BuildException("SCM changelog command unsuccessful.");
    }
    // Output changelog information
    ChangeLogSet changelogSet = result.getChangeLog();
    if (logOutput != null && logOutput.equals("xml")) {
        getTask().log(changelogSet.toXML());
    } else if (xmlbom != null) {
        String output = "";
        for (Object object : changelogSet.getChangeSets()) {
            String revision = "";
            ChangeSet changeSet = (ChangeSet) object;
            for (String line : changeSet.toString().split("\n")) {
                if (line.contains("revision:")) {
                    revision = line.replace("revision:", "");
                }
            }

            output = output + "<task><id>" + revision + "</id><synopsis>" + changeSet.getComment()
                    + "</synopsis><completed>"
                    + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(changeSet.getDate())
                    + "</completed></task>";
        }
        String[] path = getTask().getScmUrl().split("/");
        String xml = "<bom><build>untitled</build><content>\n";
        String thisproject = "<project>" + "<name>" + path[path.length - 1] + "</name>" + "<baseline>"
                + getTask().getScmUrl() + "</baseline>" + "<database>mercurial</database>" + output
                + "</project>\n";
        xml = xml + thisproject;
        try {
            if (xmlbom.exists()) {
                SAXReader xmlReader = new SAXReader();
                Document antDoc = xmlReader.read(xmlbom);
                for (Iterator iterator = antDoc.selectNodes("//project").iterator(); iterator.hasNext();) {
                    boolean equal = false;
                    Element element = (Element) iterator.next();
                    for (Iterator iterator2 = antDoc.selectNodes("//baseline").iterator(); iterator2
                            .hasNext();) {
                        Element e2 = (Element) iterator2.next();
                        if (e2.getText().equals(getTask().getScmUrl())) {
                            equal = true;
                        }
                    }
                    if (!equal) {
                        xml = xml + element.asXML() + "\n";
                    }
                }
            }
            xml = xml + "</content></bom>";

            FileWriter fstream = new FileWriter(xmlbom);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(xml);
            out.close();
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    } else {
        Iterator iterator = changelogSet.getChangeSets().iterator();
        while (iterator.hasNext()) {
            getTask().log(iterator.next().toString());
        }
    }
}

From source file:com.nokia.helium.scm.ant.taskdefs.ChangelogAction.java

License:Open Source License

/**
 * {@inheritDoc}// w  ww.j a va  2s.  co m
 */
@SuppressWarnings("unchecked")
@Override
public void execute(ScmRepository repository) throws ScmException {
    ScmManager scmManager = getTask().getScmManager();
    ScmRevision startRevision = new ScmRevision(startVersion);
    ScmRevision endRevision = new ScmRevision(endVersion);

    ChangeLogScmResult result;
    Date start = null;
    Date end = null;

    if (numDays == 0 && (startDate != null || endDate != null)) {
        try {
            SimpleDateFormat format = new SimpleDateFormat(datePattern);
            start = format.parse(startDate);
            end = format.parse(endDate);
        } catch (Exception e) {
            throw new ScmException("Date Format not supported:" + e.getMessage());
        }
    } else {
        start = null;
        end = null;
    }
    try {
        if (startVersion == null)
            result = scmManager.changeLog(repository, getScmFileSet(), start, end, numDays, null, datePattern);
        else
            result = scmManager.changeLog(repository, getScmFileSet(), startRevision, endRevision, datePattern);
    } catch (ScmException e) {
        throw new BuildException("Execution of SCM changelog action failed.");
    }
    if (!result.isSuccess()) {
        throw new BuildException("SCM changelog command unsuccessful.");
    }
    // Output changelog information
    ChangeLogSet changelogSet = result.getChangeLog();
    if (logOutput != null && logOutput.equals("xml")) {
        getTask().log(changelogSet.toXML());
    } else if (xmlbom != null) {
        String output = "";
        for (Object o : changelogSet.getChangeSets()) {
            String revision = "";
            ChangeSet c = (ChangeSet) o;
            for (String x : c.toString().split("\n")) {
                if (x.contains("revision:"))
                    revision = x.replace("revision:", "");
            }

            output = output + "<task><id>" + revision + "</id><synopsis>" + c.getComment()
                    + "</synopsis><completed>"
                    + new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss").format(c.getDate()) + "</completed></task>";
        }
        String[] path = getTask().getScmUrl().split("/");
        String xml = "<bom><build>untitled</build><content>\n";
        String thisproject = "<project>" + "<name>" + path[path.length - 1] + "</name>" + "<baseline>"
                + getTask().getScmUrl() + "</baseline>" + "<database>mercurial</database>" + output
                + "</project>\n";
        xml = xml + thisproject;
        try {
            if (xmlbom.exists()) {
                SAXReader xmlReader = new SAXReader();
                Document antDoc = xmlReader.read(xmlbom);
                for (Iterator iterator = antDoc.selectNodes("//project").iterator(); iterator.hasNext();) {
                    boolean equal = false;
                    Element e = (Element) iterator.next();
                    for (Iterator iterator2 = antDoc.selectNodes("//baseline").iterator(); iterator2
                            .hasNext();) {
                        Element e2 = (Element) iterator2.next();
                        if (e2.getText().equals(getTask().getScmUrl()))
                            equal = true;
                    }
                    if (!equal)
                        xml = xml + e.asXML() + "\n";
                }
            }
            xml = xml + "</content></bom>";

            FileWriter fstream = new FileWriter(xmlbom);
            BufferedWriter out = new BufferedWriter(fstream);
            out.write(xml);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    } else {
        Iterator iterator = changelogSet.getChangeSets().iterator();
        while (iterator.hasNext()) {
            getTask().log(iterator.next().toString());
        }
    }
}

From source file:com.npower.dm.hibernate.management.ModelManagementBeanImpl.java

License:Open Source License

/**
 * /*from w  w w.j  av  a  2 s.co  m*/
 * <pre>
 * Import the TAC list of models form file.
 * &lt;pre&gt;
 * 
 * @param xmlFile
 * @throws DMException
 * 
 */
public void importModelTAC(File xmlFile) throws DMException {

    try {

        ManagementBeanFactory factory = this.getManagementBeanFactory();
        ModelBean modelBean = factory.createModelBean();
        factory.beginTransaction();

        SAXReader reader = new SAXReader();
        Document confDoc = reader.read(xmlFile);
        Element root = confDoc.getRootElement();

        for (Iterator<Element> i = root.elementIterator("Manufacturer"); i.hasNext();) {

            String manufacturerExternalID = "";
            Element manElement = i.next();
            manufacturerExternalID = manElement.elementText("ExternalID");
            Manufacturer manufacturerBean = modelBean.getManufacturerByExternalID(manufacturerExternalID);
            if (manufacturerBean == null) {
                System.err.println("Could not find this manufacturer: " + manufacturerExternalID);
                continue;
            }

            for (Iterator<Element> j = manElement.elementIterator("Model"); j.hasNext();) {
                String modelExternalID = "";
                Element modelElement = j.next();
                modelExternalID = modelElement.elementText("ExternalID");
                Model model = modelBean.getModelByManufacturerModelID(manufacturerBean, modelExternalID);
                if (model == null) {
                    System.err.println("Could not find this model: " + modelExternalID);
                    continue;
                }

                List<Element> TACsList = modelElement.elements("TACs");
                List<String> TACInfos = new ArrayList<String>();
                for (int k = 0; k < TACsList.size(); k++) {
                    List<Element> TACList = ((TACsList.get(k))).elements("TAC");
                    for (int m = 0; m < TACList.size(); m++) {
                        Element tacElement = TACList.get(m);
                        String tac = tacElement.getText();
                        if (StringUtils.isNotEmpty(tac) && tac.length() >= 8) {
                            TACInfos.add(tac);
                        }
                    }

                }
                modelBean.update(model);
                modelBean.setTACInfos(model, TACInfos);
            }
        }
        factory.commit();
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}

From source file:com.npower.dm.hibernate.management.ModelManagementBeanImpl.java

License:Open Source License

/**
 * /*  w ww  .  j ava  2 s .c o m*/
 * @param filename
 * @return
 */
public int formatXMLFile(String filename) {

    int returnValue = 0;

    try {

        SAXReader saxReader = new SAXReader();
        Document document = saxReader.read(new File(filename));
        XMLWriter writer = null;
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding("UTF-8");
        writer = new XMLWriter(new FileWriter(new File(filename)), format);
        writer.write(document);
        writer.close();
        returnValue = 1;

    } catch (Exception ex) {
        ex.printStackTrace();
    }
    return returnValue;
}

From source file:com.npower.dm.util.XMLPrettyFormatter.java

License:Open Source License

private Document loadDocument(Reader reader) throws DocumentException {
    SAXReader saxReader = new SAXReader();
    Document xmlDocument = saxReader.read(reader);
    return xmlDocument;
}

From source file:com.oakhole.core.uitls.XMLMapper.java

License:Apache License

/**
 * SAX??xml,/*from w ww.j  a v  a2 s  . c  om*/
 *
 * @param inputStream
 * @return
 * @throws Exception
 */
public static Map<String, String> parseXml(InputStream inputStream) throws Exception {
    Map<String, String> map = Maps.newHashMap();
    SAXReader reader = new SAXReader();
    Document document = reader.read(inputStream);
    Element root = document.getRootElement();
    List<Element> elementList = root.elements();
    for (Element e : elementList) {
        map.put(e.getName(), e.getText());
    }
    inputStream.close();
    inputStream = null;
    return map;
}