Search in sources :

Example 6 with Document

use of nu.xom.Document in project CoreNLP by stanfordnlp.

the class CoreNLPServlet method init.

@Override
public void init() throws ServletException {
    pipeline = new StanfordCoreNLP();
    String xslPath = getServletContext().getRealPath("/WEB-INF/data/CoreNLP-to-HTML.xsl");
    try {
        Builder builder = new Builder();
        Document stylesheet = builder.build(new File(xslPath));
        corenlpTransformer = new XSLTransform(stylesheet);
    } catch (Exception e) {
        throw new ServletException(e);
    }
}
Also used : ServletException(javax.servlet.ServletException) Builder(nu.xom.Builder) Document(nu.xom.Document) File(java.io.File) StanfordCoreNLP(edu.stanford.nlp.pipeline.StanfordCoreNLP) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) RuntimeIOException(edu.stanford.nlp.io.RuntimeIOException) XSLTransform(nu.xom.xslt.XSLTransform)

Example 7 with Document

use of nu.xom.Document in project android-selector-chapek by inmite.

the class SelectorGenerator method generate.

public static void generate(VirtualFile newFile, List<SelectorDetector.Result> detectorResults) {
    Log.d("generating XML:");
    Element root = new Element("selector");
    root.addNamespaceDeclaration(NS, SCHEMA);
    List<String> allStatesWithoutNormal = new ArrayList<String>();
    for (SelectorDetector.Result result : detectorResults) {
        for (String state : result.states) {
            if (!state.equals(Constants.NORMAL) && !allStatesWithoutNormal.contains(state)) {
                allStatesWithoutNormal.add(state);
            }
        }
    }
    for (SelectorDetector.Result result : detectorResults) {
        Log.d("fileName=" + result.drawableName + ", states:" + result.states);
        Element item = new Element("item");
        Attribute attribute = new Attribute("drawable", "@drawable/" + result.drawableName);
        attribute.setNamespace(NS, SCHEMA);
        item.addAttribute(attribute);
        for (String state : allStatesWithoutNormal) {
            boolean defaultValue = Constants.sMapping.get(state).defaultValue;
            addState(item, Constants.sMapping.get(state).attributeName, result.states.contains(state) ? (!defaultValue) : defaultValue);
        }
        Log.d("row=" + item.toXML());
        root.appendChild(item);
    }
    Document doc = new Document(root);
    OutputStream os = null;
    try {
        os = newFile.getOutputStream(null);
        Serializer serializer = new Serializer(os);
        serializer.setIndent(4);
        serializer.write(doc);
    } catch (IOException e) {
        Log.e(e);
    } finally {
        if (os != null) {
            try {
                os.close();
            } catch (IOException e) {
                Log.e(e);
            }
        }
    }
}
Also used : Attribute(nu.xom.Attribute) Element(nu.xom.Element) OutputStream(java.io.OutputStream) ArrayList(java.util.ArrayList) IOException(java.io.IOException) Document(nu.xom.Document) Serializer(nu.xom.Serializer)

Example 8 with Document

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

the class BinaryXMLCodec method readElementF.

/**
 * Iterative pull parser reading an entire element subtree (using NodeFactory).
 */
private void readElementF(ArrayByteList src, Element current, InputStream input) throws BinaryParsingException, IOException {
    // final ArrayList stack = new ArrayList();
    final FastStack stack = new FastStack();
    stack.push(current);
    boolean addAttributesAndNamespaces = true;
    while (true) {
        Nodes nodes = null;
        // look ahead
        int type = src.get();
        switch(// three low bits indicate node type
        type & 0x07) {
            case TEXT:
                {
                    nodes = readTextF(src, type);
                    break;
                }
            case ATTRIBUTE:
                {
                    Element elem = addAttributesAndNamespaces ? current : null;
                    nodes = readAttributeF(src, elem, type);
                    break;
                }
            case BEGIN_ELEMENT:
                {
                    Element elem = readStartTagF(src, type, false);
                    // even if it's null
                    stack.push(elem);
                    if (elem != null) {
                        current.insertChild(elem, current.getChildCount());
                        // recurse down
                        current = elem;
                    }
                    addAttributesAndNamespaces = elem != null;
                    continue;
                }
            case END_ELEMENT:
                {
                    Element elem = stack.pop();
                    if (elem == null) {
                        // skip element
                        continue;
                    }
                    ParentNode parent = elem.getParent();
                    if (parent == null)
                        throwTamperedWithParent();
                    if (parent instanceof Document) {
                        // we're done with the root element
                        return;
                    }
                    // recurse up
                    current = (Element) parent;
                    nodes = factory.finishMakingElement(elem);
                    if (nodes.size() == 1 && nodes.get(0) == elem) {
                        // optimization: no need to remove and then readd same element
                        continue;
                    }
                    if (current.getChildCount() - 1 < 0)
                        throwTamperedWithParent();
                    current.removeChild(current.getChildCount() - 1);
                    break;
                }
            case COMMENT:
                {
                    nodes = readCommentF(src, type);
                    break;
                }
            case NAMESPACE_DECLARATION:
                {
                    Element elem = addAttributesAndNamespaces ? current : null;
                    readNamespaceDeclaration(src, elem, type);
                    continue;
                }
            case PROCESSING_INSTRUCTION:
                {
                    nodes = readProcessingInstructionF(src);
                    break;
                }
            case DOC_TYPE:
                {
                    // surrogate hack for BEGIN_PAGE
                    readPage(src, input);
                    continue;
                }
        }
        appendNodes(current, nodes);
    }
}
Also used : ParentNode(nu.xom.ParentNode) Element(nu.xom.Element) Document(nu.xom.Document) Nodes(nu.xom.Nodes)

Example 9 with Document

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

the class PermissionUtils method createPermissionsXmlAsset.

public static Asset createPermissionsXmlAsset(Permission... permissions) {
    final Element permissionsElement = new Element("permissions");
    permissionsElement.setNamespaceURI("http://xmlns.jcp.org/xml/ns/javaee");
    permissionsElement.addAttribute(new Attribute("version", "7"));
    for (Permission permission : permissions) {
        final Element permissionElement = new Element("permission");
        final Element classNameElement = new Element("class-name");
        final Element nameElement = new Element("name");
        classNameElement.appendChild(permission.getClass().getName());
        nameElement.appendChild(permission.getName());
        permissionElement.appendChild(classNameElement);
        permissionElement.appendChild(nameElement);
        final String actions = permission.getActions();
        if (actions != null && !actions.isEmpty()) {
            final Element actionsElement = new Element("actions");
            actionsElement.appendChild(actions);
            permissionElement.appendChild(actionsElement);
        }
        permissionsElement.appendChild(permissionElement);
    }
    Document document = new Document(permissionsElement);
    try (ByteArrayOutputStream stream = new ByteArrayOutputStream()) {
        final NiceSerializer serializer = new NiceSerializer(stream);
        serializer.setIndent(4);
        serializer.setLineSeparator("\n");
        serializer.write(document);
        serializer.flush();
        return new StringAsset(stream.toString("UTF-8"));
    } catch (IOException e) {
        throw new IllegalStateException("Generating permissions.xml failed", e);
    }
}
Also used : StringAsset(org.jboss.shrinkwrap.api.asset.StringAsset) Attribute(nu.xom.Attribute) Element(nu.xom.Element) Permission(java.security.Permission) FilePermission(java.io.FilePermission) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) Document(nu.xom.Document)

Example 10 with Document

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

the class SaveBayesImXmlAction method actionPerformed.

public void actionPerformed(ActionEvent e) {
    try {
        File outfile = EditorUtils.getSaveFile("bayesim", "xml", this.bayesImEditor, false, "Save Bayes IM as XML...");
        BayesIm bayesIm = bayesImEditor.getWizard().getBayesIm();
        FileOutputStream out = new FileOutputStream(outfile);
        Element element = BayesXmlRenderer.getElement(bayesIm);
        Document document = new Document(element);
        Serializer serializer = new Serializer(out);
        serializer.setLineSeparator("\n");
        serializer.setIndent(2);
        serializer.write(document);
        out.close();
    } catch (IOException e1) {
        throw new RuntimeException(e1);
    }
}
Also used : BayesIm(edu.cmu.tetrad.bayes.BayesIm) FileOutputStream(java.io.FileOutputStream) Element(nu.xom.Element) IOException(java.io.IOException) Document(nu.xom.Document) File(java.io.File) Serializer(nu.xom.Serializer)

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