Search in sources :

Example 6 with Text

use of nu.xom.Text in project tetrad by cmu-phil.

the class BayesXmlRenderer method getCptsElement.

private static Element getCptsElement(BayesIm bayesIm) {
    Element cpts = new Element("cpts");
    cpts.addAttribute(new Attribute("rowSumTolerance", "0.0001"));
    for (int i = 0; i < bayesIm.getNumNodes(); i++) {
        Element cpt = new Element("cpt");
        cpts.appendChild(cpt);
        String varName = bayesIm.getNode(i).getName();
        int numRows = bayesIm.getNumRows(i);
        int numCols = bayesIm.getNumColumns(i);
        cpt.addAttribute(new Attribute("variable", varName));
        cpt.addAttribute(new Attribute("numRows", "" + numRows));
        cpt.addAttribute(new Attribute("numCols", "" + numCols));
        for (int j = 0; j < numRows; j++) {
            Element row = new Element("row");
            cpt.appendChild(row);
            StringBuilder buf = new StringBuilder();
            for (int k = 0; k < numCols; k++) {
                double probability = bayesIm.getProbability(i, j, k);
                buf.append(NumberFormatUtil.getInstance().getNumberFormat().format(probability)).append(" ");
            }
            String s = buf.toString();
            row.appendChild(new Text(s.trim()));
        }
    }
    return cpts;
}
Also used : Attribute(nu.xom.Attribute) Element(nu.xom.Element) Text(nu.xom.Text)

Example 7 with Text

use of nu.xom.Text in project tetrad by cmu-phil.

the class GraphUtils method convertToXml.

/**
 * @return an XML element representing the given graph. (Well, only a basic
 * graph for now...)
 */
public static Element convertToXml(Graph graph) {
    Element element = new Element("graph");
    Element variables = new Element("variables");
    element.appendChild(variables);
    for (Node node : graph.getNodes()) {
        Element variable = new Element("variable");
        Text text = new Text(node.getName());
        variable.appendChild(text);
        variables.appendChild(variable);
    }
    Element edges = new Element("edges");
    element.appendChild(edges);
    for (Edge edge : graph.getEdges()) {
        Element _edge = new Element("edge");
        Text text = new Text(edge.toString());
        _edge.appendChild(text);
        edges.appendChild(_edge);
    }
    Set<Triple> ambiguousTriples = graph.getAmbiguousTriples();
    if (!ambiguousTriples.isEmpty()) {
        Element underlinings = new Element("ambiguities");
        element.appendChild(underlinings);
        for (Triple triple : ambiguousTriples) {
            Element underlining = new Element("ambiguities");
            Text text = new Text(niceTripleString(triple));
            underlining.appendChild(text);
            underlinings.appendChild(underlining);
        }
    }
    Set<Triple> underlineTriples = graph.getUnderLines();
    if (!underlineTriples.isEmpty()) {
        Element underlinings = new Element("underlines");
        element.appendChild(underlinings);
        for (Triple triple : underlineTriples) {
            Element underlining = new Element("underline");
            Text text = new Text(niceTripleString(triple));
            underlining.appendChild(text);
            underlinings.appendChild(underlining);
        }
    }
    Set<Triple> dottedTriples = graph.getDottedUnderlines();
    if (!dottedTriples.isEmpty()) {
        Element dottedUnderlinings = new Element("dottedUnderlines");
        element.appendChild(dottedUnderlinings);
        for (Triple triple : dottedTriples) {
            Element dottedUnderlining = new Element("dottedUnderline");
            Text text = new Text(niceTripleString(triple));
            dottedUnderlining.appendChild(text);
            dottedUnderlinings.appendChild(dottedUnderlining);
        }
    }
    return element;
}
Also used : Element(nu.xom.Element) Text(nu.xom.Text)

Example 8 with Text

use of nu.xom.Text in project tetrad by cmu-phil.

the class BayesXmlParser method makeBayesIm.

private static BayesIm makeBayesIm(BayesPm bayesPm, Element element2) {
    if (!"cpts".equals(element2.getQualifiedName())) {
        throw new IllegalArgumentException("Expecting 'cpts' element.");
    }
    MlBayesIm bayesIm = new MlBayesIm(bayesPm);
    Elements elements2 = element2.getChildElements();
    for (int nodeIndex = 0; nodeIndex < elements2.size(); nodeIndex++) {
        Element e1 = elements2.get(nodeIndex);
        if (!"cpt".equals(e1.getQualifiedName())) {
            throw new IllegalArgumentException("Expecting 'cpt' element.");
        }
        String numRowsString = e1.getAttributeValue("numRows");
        String numColsString = e1.getAttributeValue("numCols");
        int numRows = Integer.parseInt(numRowsString);
        int numCols = Integer.parseInt(numColsString);
        Elements e1Elements = e1.getChildElements();
        if (e1Elements.size() != numRows) {
            throw new IllegalArgumentException("Element cpt claimed " + +numRows + " rows, but there are only " + e1Elements.size() + " rows in the file.");
        }
        for (int rowIndex = 0; rowIndex < numRows; rowIndex++) {
            Element e2 = e1Elements.get(rowIndex);
            if (!"row".equals(e2.getQualifiedName())) {
                throw new IllegalArgumentException("Expecting 'parent' element.");
            }
            Text rowNode = (Text) e2.getChild(0);
            String rowString = rowNode.getValue();
            StringTokenizer t = new StringTokenizer(rowString);
            for (int colIndex = 0; colIndex < numCols; colIndex++) {
                String token = t.nextToken();
                try {
                    double value = Double.parseDouble(token);
                    bayesIm.setProbability(nodeIndex, rowIndex, colIndex, value);
                } catch (NumberFormatException e) {
                // Skip.
                }
            }
            if (t.hasMoreTokens()) {
                throw new IllegalArgumentException("Element cpt claimed " + numCols + " columnns , but there are more that that " + "in the file.");
            }
        }
    }
    return bayesIm;
}
Also used : Element(nu.xom.Element) Text(nu.xom.Text) Elements(nu.xom.Elements)

Example 9 with Text

use of nu.xom.Text in project cdk by cdk.

the class RssWriter method write.

/**
 * Writes a IChemObject to the MDL molfile formated output.
 *
 * @param object Best choice is a set of molecules
 */
/*
     * (non-Javadoc)
     * @see
     * org.openscience.cdk.io.IChemObjectWriter#write(org.openscience.cdk.interfaces
     * .IChemObject)
     */
@Override
public void write(IChemObject object) throws CDKException {
    try {
        ProcessingInstruction processingInstruction = new ProcessingInstruction("xml-stylesheet", "href=\"http://www.w3.org/2000/08/w3c-synd/style.css\" type=\"text/css\"");
        Element rdfElement = new Element("rdf:RDF", NS_RDF);
        rdfElement.addNamespaceDeclaration("", NS_RSS10);
        rdfElement.addNamespaceDeclaration("mn", "http://usefulinc.com/rss/manifest/");
        rdfElement.addNamespaceDeclaration("dc", NS_DCELEMENTS);
        rdfElement.addNamespaceDeclaration("cml", Convertor.NS_CML);
        Document doc = new Document(rdfElement);
        doc.insertChild(processingInstruction, 0);
        Element channelElement = new Element("channel", NS_RSS10);
        Element titleElement = new Element("title", NS_RSS10);
        titleElement.appendChild(new Text(title));
        channelElement.appendChild(titleElement);
        Element linkElement = new Element("link", NS_RSS10);
        linkElement.appendChild(new Text(link));
        channelElement.appendChild(linkElement);
        Element descriptionElement = new Element("description", NS_RSS10);
        descriptionElement.appendChild(new Text(description));
        channelElement.appendChild(descriptionElement);
        Element publisherElement = new Element("dc:publisher", NS_DCELEMENTS);
        publisherElement.appendChild(new Text(publisher));
        channelElement.appendChild(publisherElement);
        Element creatorElement = new Element("dc:creator", NS_DCELEMENTS);
        creatorElement.appendChild(new Text(creator));
        channelElement.appendChild(creatorElement);
        Element imageElement = new Element("image", NS_RSS10);
        imageElement.addAttribute(new Attribute("rdf:resource", NS_RDF, imagelink));
        channelElement.appendChild(imageElement);
        Element itemsElement = new Element("items", NS_RSS10);
        Element seqElement = new Element("rdf:Seq", NS_RDF);
        itemsElement.appendChild(seqElement);
        channelElement.appendChild(itemsElement);
        channelElement.addAttribute(new Attribute("rdf:about", NS_RDF, about));
        rdfElement.appendChild(channelElement);
        List<IChemObject> list = new ArrayList<>();
        if (object instanceof IAtomContainerSet) {
            for (int i = 0; i < ((IAtomContainerSet) object).getAtomContainerCount(); i++) {
                list.add(((IAtomContainerSet) object).getAtomContainer(i));
            }
        } else {
            list.add(object);
        }
        for (IChemObject iChemObject : list) {
            IChemObject chemObject = iChemObject;
            Element itemElement = new Element("item", NS_RSS10);
            String easylink = (String) linkmap.get(chemObject);
            if (easylink != null)
                itemElement.addAttribute(new Attribute("rdf:about", NS_RDF, easylink));
            Element link2Element = new Element("link", NS_RSS10);
            link2Element.appendChild(new Text(easylink));
            itemElement.appendChild(link2Element);
            String title = (String) chemObject.getProperties().get(CDKConstants.TITLE);
            if (titlemap.get(chemObject) != null) {
                Element title2Element = new Element("title", NS_RSS10);
                title2Element.appendChild(new Text((String) titlemap.get(chemObject)));
                itemElement.appendChild(title2Element);
            }
            if (title != null) {
                Element description2Element = new Element("description", NS_RSS10);
                description2Element.appendChild(new Text(title));
                itemElement.appendChild(description2Element);
                Element subjectElement = new Element("dc:subject", NS_DCELEMENTS);
                subjectElement.appendChild(new Text(title));
                itemElement.appendChild(subjectElement);
            }
            if (datemap.get(chemObject) != null) {
                Element dateElement = new Element("dc:date", NS_DCELEMENTS);
                SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss", Locale.ROOT);
                dateElement.appendChild(new Text(formatter.format((Date) datemap.get(chemObject)) + timezone));
                itemElement.appendChild(dateElement);
            }
            Element creator2Element = new Element("dc:creator", NS_DCELEMENTS);
            creator2Element.appendChild(new Text((String) creatormap.get(chemObject)));
            itemElement.appendChild(creator2Element);
            // add the InChI to the CMLRSS feed
            if (inchimap.get(chemObject) != null) {
                Element inchiElement = new Element("cml:identifier", "http://www.xml-cml.org/schema/cml2/core/");
                inchiElement.appendChild(new Text((String) inchimap.get(chemObject)));
                itemElement.appendChild(inchiElement);
            }
            Element root;
            Convertor convertor = new Convertor(true, null);
            object = iChemObject;
            if (object instanceof ICrystal) {
                root = convertor.cdkCrystalToCMLMolecule((ICrystal) object);
            } else if (object instanceof IAtomContainer) {
                root = convertor.cdkAtomContainerToCMLMolecule((IAtomContainer) object);
            } else if (object instanceof IAtom) {
                root = convertor.cdkAtomToCMLAtom(null, (IAtom) object);
            } else if (object instanceof IBond) {
                root = convertor.cdkBondToCMLBond((IBond) object);
            } else if (object instanceof IReaction) {
                root = convertor.cdkReactionToCMLReaction((IReaction) object);
            } else if (object instanceof IReactionSet) {
                root = convertor.cdkReactionSetToCMLReactionList((IReactionSet) object);
            } else if (object instanceof IAtomContainerSet) {
                root = convertor.cdkAtomContainerSetToCMLList((IAtomContainerSet) object);
            } else if (object instanceof IChemSequence) {
                root = convertor.cdkChemSequenceToCMLList((IChemSequence) object);
            } else if (object instanceof IChemModel) {
                root = convertor.cdkChemModelToCMLList((IChemModel) object);
            } else if (object instanceof IChemFile) {
                root = convertor.cdkChemFileToCMLList((IChemFile) object);
            } else {
                throw new CDKException("Unsupported chemObject: " + object.getClass().getName());
            }
            itemElement.appendChild(root);
            if (multiMap.get(chemObject) != null) {
                Collection coll = (Collection) multiMap.get(chemObject);
                for (Object o : coll) {
                    itemElement.appendChild((Element) o);
                }
            }
            rdfElement.appendChild(itemElement);
            Element imageElement2 = new Element("rdf:li", NS_RDF);
            imageElement2.addAttribute(new Attribute("rdf:resource", NS_RDF, (String) linkmap.get(chemObject)));
            seqElement.appendChild(imageElement2);
        }
        writer.write(doc.toXML());
        writer.flush();
    } catch (IOException ex) {
        throw new CDKException(ex.getMessage(), ex);
    }
}
Also used : IAtomContainer(org.openscience.cdk.interfaces.IAtomContainer) IReactionSet(org.openscience.cdk.interfaces.IReactionSet) Attribute(nu.xom.Attribute) Element(nu.xom.Element) ArrayList(java.util.ArrayList) IBond(org.openscience.cdk.interfaces.IBond) IChemModel(org.openscience.cdk.interfaces.IChemModel) Document(nu.xom.Document) IChemObject(org.openscience.cdk.interfaces.IChemObject) IAtom(org.openscience.cdk.interfaces.IAtom) CDKException(org.openscience.cdk.exception.CDKException) IChemFile(org.openscience.cdk.interfaces.IChemFile) Text(nu.xom.Text) IOException(java.io.IOException) Date(java.util.Date) Convertor(org.openscience.cdk.libio.cml.Convertor) ICrystal(org.openscience.cdk.interfaces.ICrystal) IAtomContainerSet(org.openscience.cdk.interfaces.IAtomContainerSet) Collection(java.util.Collection) IReaction(org.openscience.cdk.interfaces.IReaction) IChemObject(org.openscience.cdk.interfaces.IChemObject) IChemSequence(org.openscience.cdk.interfaces.IChemSequence) SimpleDateFormat(java.text.SimpleDateFormat) ProcessingInstruction(nu.xom.ProcessingInstruction)

Example 10 with Text

use of nu.xom.Text in project meico by cemfi.

the class Comment method parseData.

/**
 * parse the comment element and set the according class variables
 * @param xml
 * @throws Exception
 */
@Override
protected void parseData(Element xml) throws Exception {
    if (xml == null)
        throw new Exception("Cannot generate Comment object. XML Element is null.");
    this.setXml(xml);
    if ((xml.getChildCount() == 0) || !(xml.getChild(0) instanceof Text)) {
        // if no text node is given in the xml source
        // generate a placeholder text node
        this.text = new Text("");
        // add it to the xml
        xml.appendChild(this.text);
    } else
        this.text = (Text) xml.getChild(0);
    this.id = Helper.getAttribute("id", xml);
}
Also used : Text(nu.xom.Text)

Aggregations

Text (nu.xom.Text)10 Element (nu.xom.Element)6 Node (nu.xom.Node)3 ArrayList (java.util.ArrayList)2 Attribute (nu.xom.Attribute)2 ParentNode (nu.xom.ParentNode)2 XMLException (nu.xom.XMLException)2 Range (de.catma.document.Range)1 TagReference (de.catma.document.annotation.TagReference)1 Property (de.catma.tag.Property)1 PropertyDefinition (de.catma.tag.PropertyDefinition)1 TagDefinition (de.catma.tag.TagDefinition)1 TagInstance (de.catma.tag.TagInstance)1 TagsetDefinition (de.catma.tag.TagsetDefinition)1 IOException (java.io.IOException)1 SimpleDateFormat (java.text.SimpleDateFormat)1 Collection (java.util.Collection)1 Date (java.util.Date)1 List (java.util.List)1 Document (nu.xom.Document)1