Search in sources :

Example 71 with SAXBuilder

use of org.jdom.input.SAXBuilder in project vcell by virtualcell.

the class XMLDict method main.

// tests the x-path functionality
@SuppressWarnings("unchecked")
public static void main(String[] args) throws Exception {
    if (args.length < 1)
        System.out.println("Test Usage: XMLDict test_BioModel_File");
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(new FileReader(args[0]));
    Element root = doc.getRootElement();
    // not the actual usage, but works for testing
    XMLDict dict = new XMLDict();
    // add just one round
    dict.put(root, root.getClass().getName() + ":" + root.getAttributeValue(XMLTags.NameTag), root);
    Iterator<Element> i = root.getChildren().iterator();
    while (i.hasNext()) {
        Element temp = i.next();
        dict.put(temp, temp.getClass().getName() + ":" + temp.getAttributeValue(XMLTags.NameTag), temp);
    }
    Enumeration<String> e = dict.keys();
    while (e.hasMoreElements()) {
        String key = e.nextElement();
        Object value = dict.get(key);
        System.out.println("key: " + key + " value: " + value);
    }
    i = root.getChildren().iterator();
    while (i.hasNext()) {
        Element temp = (Element) i.next();
        Element value = (Element) dict.get(temp, temp.getClass().getName() + ":" + temp.getAttributeValue(XMLTags.NameTag));
        System.out.println(value.getName() + " " + value.getAttributeValue(XMLTags.NameTag) + " " + temp.getName() + " " + temp.getAttributeValue(XMLTags.NameTag));
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) FileReader(java.io.FileReader) Document(org.jdom.Document)

Example 72 with SAXBuilder

use of org.jdom.input.SAXBuilder in project vcell by virtualcell.

the class SEDMLReader method readFile.

public static SedML readFile(File file) throws JDOMException, IOException, XMLException {
    SAXBuilder builder = new SAXBuilder();
    Document doc = builder.build(file);
    Element sedRoot = doc.getRootElement();
    try {
        SEDMLElementFactory.getInstance().setStrictCreation(false);
        SEDMLReader reader = new SEDMLReader();
        SedML sedML = reader.getSedDocument(sedRoot);
        return sedML;
    } finally {
        SEDMLElementFactory.getInstance().setStrictCreation(true);
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) Document(org.jdom.Document)

Example 73 with SAXBuilder

use of org.jdom.input.SAXBuilder in project vcell by virtualcell.

the class SEDMLUtils method readSedDocument.

/**
 * Reads in a SEDML (XML) file into a SedDocument
 * @param fileName
 * @return
 */
static SedML readSedDocument(String fileName) {
    try {
        SAXBuilder builder = new SAXBuilder();
        Document doc = builder.build(new File(fileName));
        Element sedRoot = doc.getRootElement();
        SEDMLReader reader = new SEDMLReader();
        SedML sedDoc = reader.getSedDocument(sedRoot);
        return sedDoc;
    } catch (Exception e) {
        e.printStackTrace(System.out);
        throw new RuntimeException("Could not create SedMLDocument from file '" + fileName + "'");
    }
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Element(org.jdom.Element) Document(org.jdom.Document) File(java.io.File) JaxenException(org.jaxen.JaxenException) IOException(java.io.IOException) ParsingException(nu.xom.ParsingException)

Example 74 with SAXBuilder

use of org.jdom.input.SAXBuilder in project vcell by virtualcell.

the class Libsedml method createDocument.

private static Document createDocument(InputStream in) throws XMLException {
    SAXBuilder builder = new SAXBuilder();
    Document doc;
    try {
        doc = builder.build(in);
    } catch (JDOMException e) {
        throw new XMLException("Error reading file", e);
    } catch (IOException e) {
        throw new XMLException("Error reading file", e);
    }
    return doc;
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) IOException(java.io.IOException) Document(org.jdom.Document) JDOMException(org.jdom.JDOMException)

Example 75 with SAXBuilder

use of org.jdom.input.SAXBuilder in project yamcs-studio by yamcs.

the class FileUtil method loadXMLFile.

/**
 * Load the root element of an XML file. The element is a JDOM Element.
 *
 * @param filePath
 *            path of the file. It can be an absolute path or a relative path to the OPI that contains the specified
 *            widget. If it is an absolute path, it can be either<br>
 *            a workspace path such as <code>/OPI Examples/Scripts/myfile.xml</code><br>
 *            a local file system path such as <code>C:\myfile.xml</code><br>
 *            or an URL path such as <code>http://mysite.com/myfile.xml</code>.
 * @param widget
 *            a widget in the OPI, which is used to provide relative path reference. It can be null if the path is
 *            an absolute path.
 * @return root element of the XML file.
 * @throws Exception
 *             if the file does not exist or is not a correct XML file.
 */
public static Element loadXMLFile(String filePath, AbstractBaseEditPart widget) throws Exception {
    var path = buildAbsolutePath(filePath, widget);
    var saxBuilder = new SAXBuilder();
    saxBuilder.setEntityResolver(new CssEntityResolver());
    var file = ResourceUtil.getFile(path);
    Document doc;
    if (file == null) {
        var inputStream = ResourceUtil.pathToInputStream(path);
        doc = saxBuilder.build(inputStream);
        inputStream.close();
    } else {
        doc = saxBuilder.build(file);
    }
    return doc.getRootElement();
}
Also used : SAXBuilder(org.jdom.input.SAXBuilder) Document(org.jdom.Document)

Aggregations

SAXBuilder (org.jdom.input.SAXBuilder)146 Document (org.jdom.Document)110 Element (org.jdom.Element)84 IOException (java.io.IOException)61 JDOMException (org.jdom.JDOMException)50 StringReader (java.io.StringReader)35 InputStream (java.io.InputStream)29 File (java.io.File)18 ApsSystemException (com.agiletec.aps.system.exception.ApsSystemException)13 ArrayList (java.util.ArrayList)12 XPath (org.jdom.xpath.XPath)11 HttpMethod (org.apache.commons.httpclient.HttpMethod)10 XMLOutputter (org.jdom.output.XMLOutputter)10 URL (java.net.URL)9 List (java.util.List)8 GoraException (org.apache.gora.util.GoraException)8 ByteArrayInputStream (java.io.ByteArrayInputStream)7 FileInputStream (java.io.FileInputStream)7 Namespace (org.jdom.Namespace)6 StringWriter (java.io.StringWriter)5