Search in sources :

Example 1 with Document

use of nu.xom.Document in project apn-proxy by apn-proxy.

the class ApnProxyAbstractXmlConfigReader method read.

public final void read(InputStream xmlConfigFileInputStream) {
    Document doc = null;
    try {
        Builder parser = new Builder();
        doc = parser.build(xmlConfigFileInputStream);
    } catch (ParsingException ex) {
        logger.error(ex.getMessage(), ex);
    } catch (IOException ex) {
        logger.error(ex.getMessage(), ex);
    }
    if (doc == null) {
        return;
    }
    Element rootElement = doc.getRootElement();
    realReadProcess(rootElement);
}
Also used : Builder(nu.xom.Builder) ParsingException(nu.xom.ParsingException) Element(nu.xom.Element) Document(nu.xom.Document)

Example 2 with Document

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

the class LoadBayesImXsdlXmlAction method actionPerformed.

public void actionPerformed(ActionEvent e) {
    if (bayesImWrapper == null) {
        throw new RuntimeException("Not a Bayes IM.");
    }
    JFileChooser chooser = getJFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.showOpenDialog(null);
    File file = chooser.getSelectedFile();
    if (file != null) {
        Preferences.userRoot().put("fileSaveLocation", file.getParent());
    }
    try {
        Builder builder = new Builder();
        Document document = builder.build(file);
        printDocument(document);
        XdslXmlParser parser = new XdslXmlParser();
        BayesIm bayesIm = parser.getBayesIm(document.getRootElement());
        System.out.println(bayesIm);
        boolean allSpecified = true;
        for (edu.cmu.tetrad.graph.Node node : bayesIm.getBayesPm().getDag().getNodes()) {
            if (node.getCenterX() == -1 || node.getCenterY() == -1) {
                allSpecified = false;
            }
        }
        if (!allSpecified) {
            GraphUtils.circleLayout(bayesIm.getBayesPm().getDag(), 200, 200, 150);
        }
        bayesImWrapper.setBayesIm(bayesIm);
        bayesImEditor.getBayesIm(bayesIm);
    } catch (ParsingException e2) {
        e2.printStackTrace();
        throw new RuntimeException("Had trouble parsing that...");
    } catch (IOException e2) {
        e2.printStackTrace();
        throw new RuntimeException("Had trouble reading the file...");
    }
}
Also used : Builder(nu.xom.Builder) IOException(java.io.IOException) Document(nu.xom.Document) XdslXmlParser(edu.cmu.tetrad.search.XdslXmlParser) BayesIm(edu.cmu.tetrad.bayes.BayesIm) ParsingException(nu.xom.ParsingException) File(java.io.File)

Example 3 with Document

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

the class LoadBayesImXmlAction method actionPerformed.

public void actionPerformed(ActionEvent e) {
    if (bayesImWrapper == null) {
        throw new RuntimeException("Not a Bayes IM.");
    }
    JFileChooser chooser = getJFileChooser();
    chooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
    chooser.showOpenDialog(null);
    File file = chooser.getSelectedFile();
    if (file != null) {
        Preferences.userRoot().put("fileSaveLocation", file.getParent());
    }
    try {
        Builder builder = new Builder();
        Document document = builder.build(file);
        printDocument(document);
        BayesXmlParser parser = new BayesXmlParser();
        BayesIm bayesIm = parser.getBayesIm(document.getRootElement());
        System.out.println(bayesIm);
        boolean allSpecified = true;
        for (edu.cmu.tetrad.graph.Node node : bayesIm.getBayesPm().getDag().getNodes()) {
            if (node.getCenterX() == -1 || node.getCenterY() == -1) {
                allSpecified = false;
            }
        }
        if (!allSpecified) {
            GraphUtils.circleLayout(bayesIm.getBayesPm().getDag(), 200, 200, 150);
        }
        bayesImWrapper.setBayesIm(bayesIm);
        bayesImEditor.getBayesIm(bayesIm);
    } catch (ParsingException e2) {
        e2.printStackTrace();
        throw new RuntimeException("Had trouble parsing that...");
    } catch (IOException e2) {
        e2.printStackTrace();
        throw new RuntimeException("Had trouble reading the file...");
    }
}
Also used : Builder(nu.xom.Builder) IOException(java.io.IOException) Document(nu.xom.Document) BayesXmlParser(edu.cmu.tetrad.bayes.BayesXmlParser) BayesIm(edu.cmu.tetrad.bayes.BayesIm) ParsingException(nu.xom.ParsingException) File(java.io.File)

Example 4 with Document

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

the class GraphUtils method getRootElement.

public static Element getRootElement(File file) throws ParsingException, IOException {
    Builder builder = new Builder();
    Document document = builder.build(file);
    return document.getRootElement();
}
Also used : Builder(nu.xom.Builder) Document(nu.xom.Document)

Example 5 with Document

use of nu.xom.Document in project teiid by teiid.

the class BinaryXMLCodec method readDocument.

/**
 * Parses document from encoded src buffer; tokens appear in document order.
 */
private Document readDocument(ArrayByteList src, InputStream input) throws BinaryParsingException, IOException {
    if (DEBUG)
        System.err.println("reading document");
    readPage(src, input);
    Document doc = factory.startMakingDocument();
    // doc.setBaseURI(symbols[src.getInt()]);
    doc.setBaseURI(getInternedName(src.getInt()));
    boolean hasRootElement = false;
    int i = 0;
    // add children of document, retaining the exact same order found in input
    while (src.remaining() > 0) {
        Nodes nodes;
        // look ahead
        int type = src.get();
        if (DEBUG)
            System.err.println("reading type = " + toString(type));
        switch(// three low bits indicate node type
        type & 0x07) {
            case TEXT:
                {
                    throw new BinaryParsingException("Unreachable text");
                }
            case ATTRIBUTE:
                {
                    throw new BinaryParsingException("Unreachable attribute");
                }
            case BEGIN_ELEMENT:
                {
                    if (factory.getClass() == NodeFactory.class) {
                        // fast path
                        Element root = readStartTag(src, type);
                        // reads entire subtree
                        readElement(src, root, input);
                        nodes = new Nodes(root);
                    } else {
                        // slow path
                        Element root = readStartTagF(src, type, true);
                        if (root == null) {
                            throw new NullPointerException("Factory failed to create root element.");
                        }
                        doc.setRootElement(root);
                        readElementF(src, root, input);
                        nodes = factory.finishMakingElement(root);
                    }
                    break;
                }
            case END_ELEMENT:
                {
                    throw new BinaryParsingException("Unreachable end of element");
                }
            case COMMENT:
                {
                    nodes = readCommentF(src, type);
                    break;
                }
            case NAMESPACE_DECLARATION:
                {
                    throw new BinaryParsingException("Unreachable namespace declaration");
                }
            case PROCESSING_INSTRUCTION:
                {
                    nodes = readProcessingInstructionF(src);
                    break;
                }
            case DOC_TYPE:
                {
                    nodes = readDocTypeF(src);
                    break;
                }
            default:
                {
                    throw new BinaryParsingException("Illegal node type code=" + type);
                }
        }
        // append nodes:
        for (int j = 0; j < nodes.size(); j++) {
            Node node = nodes.get(j);
            if (node instanceof Element) {
                // replace fake root with real root
                if (hasRootElement) {
                    throw new IllegalAddException("Factory returned multiple root elements");
                }
                doc.setRootElement((Element) node);
                hasRootElement = true;
            } else {
                doc.insertChild(node, i);
            }
            i++;
        }
    }
    if (!hasRootElement)
        throw new WellformednessException("Factory attempted to remove the root element");
    factory.finishMakingDocument(doc);
    if (DEBUG)
        System.err.println("finished reading document");
    return doc;
}
Also used : NodeFactory(nu.xom.NodeFactory) WellformednessException(nu.xom.WellformednessException) IllegalAddException(nu.xom.IllegalAddException) Element(nu.xom.Element) ParentNode(nu.xom.ParentNode) Node(nu.xom.Node) Document(nu.xom.Document) Nodes(nu.xom.Nodes)

Aggregations

Document (nu.xom.Document)14 IOException (java.io.IOException)9 Builder (nu.xom.Builder)7 Element (nu.xom.Element)7 File (java.io.File)4 ParsingException (nu.xom.ParsingException)4 BayesIm (edu.cmu.tetrad.bayes.BayesIm)3 Nodes (nu.xom.Nodes)3 Serializer (nu.xom.Serializer)3 RuntimeIOException (edu.stanford.nlp.io.RuntimeIOException)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 FileOutputStream (java.io.FileOutputStream)2 OutputStream (java.io.OutputStream)2 StringReader (java.io.StringReader)2 ServletException (javax.servlet.ServletException)2 Attribute (nu.xom.Attribute)2 ParentNode (nu.xom.ParentNode)2 BayesXmlParser (edu.cmu.tetrad.bayes.BayesXmlParser)1 XdslXmlParser (edu.cmu.tetrad.search.XdslXmlParser)1 StanfordCoreNLP (edu.stanford.nlp.pipeline.StanfordCoreNLP)1