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.arc.intro.CompilerSamplesProvider.java

License:Open Source License

public void init(IIntroContentProviderSite site) {
    if (samplesDoc == null) {
        File samplesXML = computeSampleXML();
        Document doc = null;//from  w ww  . j ava2 s .  c o  m
        if (samplesXML.exists()) {
            SAXReader reader = new SAXReader();
            try {
                doc = reader.read(samplesXML);
            } catch (Exception e) {
                ToolchainPlugin.log("Error in reading " + samplesXML, e);
            }
        }
        if (doc == null) {
            // Create a makeshift version
            DocumentFactory f = DocumentFactory.getInstance();
            Element root = f.createElement(SAMPLES_TAG);
            root.addAttribute(DEMOS_ATTR, DEFAULT_DEMOS_DIR);
            for (int i = 0; i < targetNames.length; i += 2) {
                Element target = root.addElement(TARGET_TAG);
                target.addAttribute(NAME_ATTR, targetNames[i]);
                target.addAttribute(DESC_ATTR, targetNames[i + 1]);
            }
            Element category = root.addElement(CATEGORY_TAG);
            category.addAttribute(TITLE_ATTR, "Compiler Samples");
            category.addAttribute(WORKSPACE_ATTR, ".*mide_workspace");
            category.addAttribute(ID_ATTR, "compiler");
            doc = f.createDocument(root);
        }
        samplesDoc = doc;
    }
}

From source file:com.arc.mw.util.StateSaver.java

License:Open Source License

public static void restoreState(String path, IXMLSavable object) throws DocumentException {
    SAXReader reader = new SAXReader();
    Document doc = reader.read(path);
    object.restoreState(doc.getRootElement());
}

From source file:com.arc.xml.XmlProcessor.java

License:Open Source License

/**
 * Read a document from the source given the constraints defined by the meta-xml that was received in the
 * {@link #XmlProcessor(File) constructor}.
 * <P>/*from w  w  w  .ja va2 s  . c  o  m*/
 * The result is a document whose root element's <code>getData()</code> method will return the resulting data
 * obect.
 * @param source the XML file to be read.
 * @param instantiator the object for instantiating classes to form objects as we walk the XML document.
 * @param rootPackage the package relative to which classes are located.
 * @param ehandler error handler
 */
public Document read(InputSource source, IBuilderInstantiator instantiator, String rootPackage,
        ErrorHandler ehandler) throws SAXParseException, SAXException {
    mInstantiator = instantiator;
    mRootPackage = rootPackage;
    SAXReader reader = new MySAXReader();
    if (mXMLReader != null)
        reader.setXMLReader(mXMLReader);
    mErrorHandler = ehandler;
    reader.setErrorHandler(ehandler);
    try {
        Document doc = reader.read(source);
        walk(doc);
        return doc;
    } catch (SAXParseException x) {
        if (x.getException() instanceof RuntimeException)
            throw (RuntimeException) x.getException();
        if (mErrorHandler != null)
            mErrorHandler.error(x);
        else
            throw x;
    } catch (SAXException x) {
        // unwrap nested exceptions
        while (x.getException() != null) {
            Exception t = x.getException();
            if (t instanceof SAXException && !(t instanceof SAXParseException))
                x = (SAXException) t;
            else if (t instanceof RuntimeException)
                throw (RuntimeException) t;
        }
        if (mErrorHandler != null && x instanceof SAXParseException) {
            mErrorHandler.error((SAXParseException) x);
        } else
            throw x;
    } catch (DocumentException x) {
        throw new SAXException("DocumentException", x);
    }
    return null;
}

From source file:com.aricojf.util.ConfigParser.java

/**
 * ??/* w  w  w . j  ava2s . co  m*/
 * ?String "/?/?"
 * ep: "config/conn/user"
 * @return
 */
public static String parseConfigParmByPath(String configFilePath, String nodes) {
    value = "";
    FileInputStream fs = null;
    File file = new File(configFilePath);
    if (file.exists()) {
        file = null;
        try {
            fs = new FileInputStream(configFilePath);
            SAXReader reader = new SAXReader();
            org.dom4j.Document document = reader.read(fs);
            org.dom4j.Element element = document.getRootElement();
            String[] nod = nodes.trim().split(FILE_SEPARATOR);
            for (int i = 1; i < nod.length; i++) {
                List<Element> elements = element.elements(nod[i]);
                /**
                 * ???????
                 * ??xml?????
                 */
                for (Element ele : elements) {
                    element = ele;
                    value = ele.getText();
                    break;
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeConn(fs);
        }
    }
    return null == value ? "" : value;
}

From source file:com.augmentum.common.util.DocumentUtil.java

License:Open Source License

public static Document readDocumentFromFile(File file, boolean validate) throws DocumentException {

    SAXReader reader = SAXReaderFactory.getInstance(validate);

    return reader.read(file);
}

From source file:com.augmentum.common.util.DocumentUtil.java

License:Open Source License

public static Document readDocumentFromStream(InputStream is, boolean validate) throws DocumentException {

    SAXReader reader = SAXReaderFactory.getInstance(validate);

    return reader.read(is);
}

From source file:com.augmentum.common.util.DocumentUtil.java

License:Open Source License

public static Document readDocumentFromURL(String url, boolean validate) throws DocumentException, IOException {

    SAXReader reader = SAXReaderFactory.getInstance(validate);

    return reader.read(new URL(url));
}

From source file:com.augmentum.common.util.DocumentUtil.java

License:Open Source License

public static Document readDocumentFromXML(String xml, boolean validate) throws DocumentException {

    SAXReader reader = SAXReaderFactory.getInstance(validate);

    return reader.read(new XMLSafeReader(xml));
}

From source file:com.augmentum.common.util.XMLFormatter.java

License:Open Source License

public static String toString(String xml, String indent) throws DocumentException, IOException {

    SAXReader reader = new SAXReader();

    Document doc = reader.read(new StringReader(xml));

    return toString(doc, indent);
}

From source file:com.bay12games.df.rawedit.xml.RawsDescriptionLoader.java

License:Open Source License

/**
 * Load and parse specified raws definition file. If elements is null, only
 * the content of the current file will be returned. If elements is not null
 * and contains not-null token and container maps, the content of current
 * file will be appended as if the definitions were in one file.<br><br>
 *
 * The instances of the underlying containers are preserved.<br><br>
 *
 * <p><strong>Note:</strong> There should be only one global access to the token/container
 * definitions in the application.<br>
 *
 * @param file The file to load and parse
 * @param elements The tuple-container for token, container and id elements
 * @return New instance of ElementContainer with maps of tokens and containers if the
 * supplied ElementContainer was null, or the same instance with updated content.
 * The supplied instance (possibly null) is returned on error
 *//* w  w w  .  j a  v a 2 s  . c  o m*/
public ElementContainer parse(File file, ElementContainer elements) {
    Document d;
    try {
        SAXReader reader = new SAXReader();
        d = reader.read(file);
    } catch (DocumentException ex) {
        log.error("Unable to load file:" + file + '.', ex);
        return elements;
    }

    if (elements == null) {
        containers = new HashMap<String, Container>();
        tokens = new HashMap<String, Token>();
        ids = new HashMap<String, Id>();
        elements = new ElementContainer(containers, tokens, ids);
    } else {
        if (elements.getContainers() != null) {
            containers = elements.getContainers();
        } else {
            containers = new HashMap<String, Container>();
        }

        if (elements.getTokens() != null) {
            tokens = elements.getTokens();
        } else {
            tokens = new HashMap<String, Token>();
        }

        if (elements.getIds() != null) {
            ids = elements.getIds();
        } else {
            ids = new HashMap<String, Id>();
        }
    }

    Element root = d.getRootElement();
    for (Element e : root.elements()) {
        if ("c".equals(e.getName())) {
            parseContainer(e);
        } else if ("t".equals(e.getName())) {
            parseToken(e);
        } else if ("id".equals(e.getName())) {
            parseId(e);
        }
    }

    return elements;//new ElementContainer(containers, tokens, ids);
}