Search in sources :

Example 66 with Document

use of org.jdom.Document in project vcell by virtualcell.

the class XmlHelper method bioModelToXML.

static String bioModelToXML(BioModel bioModel, boolean printkeys) throws XmlParseException {
    String xmlString = null;
    try {
        if (bioModel == null) {
            throw new IllegalArgumentException("Invalid input for BioModel: " + bioModel);
        }
        // NEW WAY, with XML declaration, vcml element, namespace, version #, etc.
        // create root vcml element
        Element vcmlElement = new Element(XMLTags.VcmlRootNodeTag);
        vcmlElement.setAttribute(XMLTags.VersionTag, getEscapedSoftwareVersion());
        // get biomodel element from xmlProducer and add it to vcml root element
        Xmlproducer xmlProducer = new Xmlproducer(printkeys);
        Element biomodelElement = xmlProducer.getXML(bioModel);
        vcmlElement.addContent(biomodelElement);
        // set namespace for vcmlElement
        vcmlElement = XmlUtil.setDefaultNamespace(vcmlElement, Namespace.getNamespace(XMLTags.VCML_NS));
        // create xml doc with vcml root element and convert to string
        Document bioDoc = new Document();
        Comment docComment = new Comment("This biomodel was generated in VCML Version " + getEscapedSoftwareVersion());
        bioDoc.addContent(docComment);
        bioDoc.setRootElement(vcmlElement);
        xmlString = XmlUtil.xmlToString(bioDoc, false);
    // // OLD WAY
    // Element element = xmlProducer.getXML(bioModel);
    // element = XmlUtil.setDefaultNamespace(element, Namespace.getNamespace(XMLTags.VCML_NS));
    // xmlString = XmlUtil.xmlToString(element);
    } catch (Exception e) {
        e.printStackTrace();
        throw new XmlParseException("Unable to generate Biomodel XML: ", e);
    }
    if (lg.isTraceEnabled()) {
        lg.trace(xmlString);
    }
    return xmlString;
}
Also used : Comment(org.jdom.Comment) Element(org.jdom.Element) Document(org.jdom.Document) VCDocument(org.vcell.util.document.VCDocument) XMLStreamException(javax.xml.stream.XMLStreamException) SbmlException(org.vcell.sbml.SbmlException) SBMLException(org.sbml.jsbml.SBMLException) IOException(java.io.IOException) ExpressionException(cbit.vcell.parser.ExpressionException)

Example 67 with Document

use of org.jdom.Document in project vcell by virtualcell.

the class VCellClientTest method startupWithOpen.

private static VCDocument startupWithOpen(String fileName) {
    VCDocument initialDocument = null;
    try {
        Document xmlDoc = XmlUtil.readXML(new File(fileName));
        String vcmlString = XmlUtil.xmlToString(xmlDoc, false);
        java.awt.Component parent = null;
        VCLogger vcLogger = new TranslationLogger(parent);
        initialDocument = XmlHelper.XMLToDocument(vcLogger, vcmlString);
    } catch (Exception e) {
        e.printStackTrace(System.out);
        JOptionPane.showMessageDialog(null, e.getMessage(), "vcell startup error", JOptionPane.ERROR_MESSAGE);
    }
    return initialDocument;
}
Also used : VCDocument(org.vcell.util.document.VCDocument) Document(org.jdom.Document) VCDocument(org.vcell.util.document.VCDocument) File(java.io.File) Component(java.awt.Component) TranslationLogger(cbit.vcell.client.TranslationLogger) VCLogger(cbit.util.xml.VCLogger)

Example 68 with Document

use of org.jdom.Document in project vcell by virtualcell.

the class VCellPubUtils method readNewRecords.

private static void readNewRecords(File xmlFile) throws IOException {
    System.out.println("starting to read");
    /**
     * 	public final Field id					= new Field("title",			"VARCHAR2(4000)",	"")
     * 	public final Field title				= new Field("title",			"VARCHAR2(4000)",	"")
     *  public final Field authors				= new Field("authors",			"VARCHAR2(4000)",	"");
     *  public final Field year					= new Field("year",				"Integer",			"");
     *  public final Field citation				= new Field("citation",			"VARCHAR2(4000)",	"");
     *  public final Field pubmedid				= new Field("pubmedid",			"VARCHAR2(32)",		"");
     *  public final Field doi					= new Field("doi",				"VARCHAR2(50)",		"");
     *  public final Field endnodeid			= new Field("endnoteid",		"VARCHAR2(50)",		"");
     *  public final Field url					= new Field("url",				"VARCHAR2(128)",	"");
     *  public final Field wittid				= new Field("wittid",			"Integer",			"");
     */
    Document doc = XmlUtil.readXML(xmlFile);
    List<Element> recordList = doc.getRootElement().getChild("records").getChildren("record");
    int foundCount = 0;
    for (Element record : recordList) {
        String titleString = record.getChild("titles").getChild("title").getChild("style").getText();
        ArrayList<String> authorsArray = new ArrayList<String>();
        List<Element> authorElements = (List<Element>) record.getChild("contributors").getChild("authors").getChildren("author");
        for (Element authorElement : authorElements) {
            authorsArray.add(authorElement.getChild("style").getText());
        }
        String title = "'" + TokenMangler.getSQLEscapedString(titleString) + "'";
        String authors = "'" + TokenMangler.getSQLEscapedString(String.join(";", authorsArray)) + "'";
        String citation = "NULL";
        String pubmedid = "NULL";
        String year = "NULL";
        try {
            year = "" + Integer.parseInt(record.getChild("dates").getChild("year").getChild("style").getText());
        } catch (Exception e) {
            System.err.println("failed to parse year : " + e.getMessage());
        }
        try {
            pubmedid = "'" + record.getChild("accession-num").getChild("style").getText() + "'";
        } catch (Exception e) {
        }
        String doi = "NULL";
        try {
            doi = "'" + record.getChild("electronic-resource-num").getChild("style").getText() + "'";
        } catch (Exception e) {
        }
        String endnoteid = record.getChild("rec-number").getText();
        String url = "NULL";
        try {
            url = "'" + record.getChild("urls").getChild("related-urls").getChild("url").getChild("style").getText() + "'";
        } catch (Exception e) {
        }
        int wittid = -1;
        String sql = "insert into vc_publication (id, title, authors, year, citation, pubmedid, doi, endnoteid, url, wittid) " + "values (NewSeq.NEXTVAL," + title + "," + authors + "," + year + "," + citation + "," + pubmedid + "," + doi + "," + endnoteid + "," + url + "," + wittid + ")";
        System.out.println(sql + ";");
        Element biomodelRefsElement = record.getChild("custom3");
        if (biomodelRefsElement != null) {
            String biomodelsString = biomodelRefsElement.getChildText("style");
            if (biomodelsString != null) {
                String[] bmRefs = biomodelsString.replace(" ", "").split(",");
                for (String bmRef : bmRefs) {
                    KeyValue pmKey = new KeyValue(bmRef);
                    String sql_link = "insert into vc_publicationmodellink (id, pubRef, bioModelRef, mathModelRef) " + "values (NewSeq.NEXTVAL,(select id from vc_publication where title = " + title + " and year is not null)," + pmKey + ",NULL);";
                    System.out.println(sql_link);
                }
            }
        }
        System.out.println("");
    }
}
Also used : KeyValue(org.vcell.util.document.KeyValue) Element(org.jdom.Element) ArrayList(java.util.ArrayList) ArrayList(java.util.ArrayList) List(java.util.List) Document(org.jdom.Document) IOException(java.io.IOException)

Example 69 with Document

use of org.jdom.Document in project vcell by virtualcell.

the class BiomodelsDB_TestSuite method writeSupportedModelsReport.

public static Document writeSupportedModelsReport(File supportInfoDir, File saveSupportedXMLPathname) throws Exception {
    if (!supportInfoDir.isDirectory()) {
        throw new IllegalArgumentException("File " + supportInfoDir.getAbsolutePath() + " must be a directory.");
    }
    File[] xmlReportFiles = supportInfoDir.listFiles(new FileFilter() {

        public boolean accept(File pathname) {
            if (pathname.isFile()) {
                if (pathname.getName().endsWith("_report.xml")) {
                    return true;
                }
            }
            return false;
        }
    });
    Document supportedDocument = new Document(new Element("Supported_BioModelsNet"));
    List<Element> supportedElements = new ArrayList<Element>();
    for (int i = 0; i < xmlReportFiles.length; i++) {
        byte[] xmlBytes = new byte[(int) xmlReportFiles[i].length()];
        FileInputStream fis = new FileInputStream(xmlReportFiles[i]);
        BufferedInputStream bis = new BufferedInputStream(fis);
        DataInputStream dis = new DataInputStream(bis);
        dis.readFully(xmlBytes);
        dis.close();
        Document document = XmlUtil.stringToXML(new String(xmlBytes), null);
        Element bioModelElement = document.getRootElement().getChild(BioModelsNetPanel.BIOMODELINFO_ELEMENT_NAME);
        Attribute supportedAttribute = bioModelElement.getAttribute(BioModelsNetPanel.SUPPORTED_ATTRIBUTE_NAME);
        if (supportedAttribute.getBooleanValue()) {
            Element newBioModelElement = new Element(BioModelsNetPanel.BIOMODELINFO_ELEMENT_NAME);
            @SuppressWarnings("unchecked") List<Attribute> attrList = bioModelElement.getAttributes();
            Iterator<Attribute> iterAttr = attrList.iterator();
            while (iterAttr.hasNext()) {
                newBioModelElement.setAttribute((Attribute) iterAttr.next().clone());
            }
            supportedElements.add(newBioModelElement);
        }
    }
    // Collections.sort(supportedElements, new ElementComparer());
    Element root = supportedDocument.getRootElement();
    for (Element e : supportedElements) {
        root.addContent(e);
    }
    if (saveSupportedXMLPathname != null) {
        try (FileWriter fw = new FileWriter(saveSupportedXMLPathname.getAbsolutePath())) {
            Format format = Format.getPrettyFormat();
            XMLOutputter out = new XMLOutputter(format);
            out.output(supportedDocument, fw);
        }
    }
    return supportedDocument;
}
Also used : XMLOutputter(org.jdom.output.XMLOutputter) Attribute(org.jdom.Attribute) Element(org.jdom.Element) FileWriter(java.io.FileWriter) ArrayList(java.util.ArrayList) Document(org.jdom.Document) DataInputStream(java.io.DataInputStream) FileInputStream(java.io.FileInputStream) Format(org.jdom.output.Format) BufferedInputStream(java.io.BufferedInputStream) FileFilter(java.io.FileFilter) File(java.io.File)

Example 70 with Document

use of org.jdom.Document 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>/BOY 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(final String filePath, final AbstractBaseEditPart widget) throws Exception {
    final IPath path = buildAbsolutePath(filePath, widget);
    SAXBuilder saxBuilder = new SAXBuilder();
    saxBuilder.setEntityResolver(new CssEntityResolver());
    File file = ResourceUtil.getFile(path);
    final Document doc;
    if (file == null) {
        InputStream 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) IPath(org.eclipse.core.runtime.IPath) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Document(org.jdom.Document) IFile(org.eclipse.core.resources.IFile) File(java.io.File)

Aggregations

Document (org.jdom.Document)144 Element (org.jdom.Element)102 SAXBuilder (org.jdom.input.SAXBuilder)51 IOException (java.io.IOException)49 JDOMException (org.jdom.JDOMException)29 File (java.io.File)27 ArrayList (java.util.ArrayList)22 XMLOutputter (org.jdom.output.XMLOutputter)22 List (java.util.List)16 StringReader (java.io.StringReader)15 Format (org.jdom.output.Format)12 VCDocument (org.vcell.util.document.VCDocument)11 XPath (org.jdom.xpath.XPath)10 StringWriter (java.io.StringWriter)9 InputStream (java.io.InputStream)8 URL (java.net.URL)8 NotNull (org.jetbrains.annotations.NotNull)7 Nullable (org.jetbrains.annotations.Nullable)7 FileNotFoundException (java.io.FileNotFoundException)5 Writer (java.io.Writer)5