Example usage for org.w3c.dom Node getOwnerDocument

List of usage examples for org.w3c.dom Node getOwnerDocument

Introduction

In this page you can find the example usage for org.w3c.dom Node getOwnerDocument.

Prototype

public Document getOwnerDocument();

Source Link

Document

The Document object associated with this node.

Usage

From source file:cz.muni.fi.mir.mathmlunificator.MathMLUnificatorCommandLineTool.java

/**
 * Main (starting) method of the command line application.
 *
 * @param argv Array of command line arguments that are expected to be
 * filesystem paths to input XML documents with MathML to be unified.
 * @throws ParserConfigurationException If a XML DOM builder cannot be
 * created with the configuration requested.
 *///from   w ww.  j a v  a2s  . c  o m
public static void main(String argv[]) throws ParserConfigurationException {

    final Options options = new Options();
    options.addOption("p", "operator-unification", false, "unify operator in addition to other types of nodes");
    options.addOption("h", "help", false, "print help");

    final CommandLineParser parser = new DefaultParser();
    CommandLine line = null;
    try {
        line = parser.parse(options, argv);
    } catch (ParseException ex) {
        printHelp(options);
        System.exit(1);
    }

    if (line != null) {
        if (line.hasOption('h')) {
            printHelp(options);
            System.exit(0);
        }
        operatorUnification = line.hasOption('p');

        final List<String> arguments = Arrays.asList(line.getArgs());
        if (arguments.size() > 0) {

            Document outerDocument = DOMBuilder.getDocumentBuilder().newDocument();
            Node rootNode = outerDocument.createElementNS(UNIFIED_MATHML_NS,
                    UNIFIED_MATHML_NS_PREFIX + ":" + UNIFIED_MATHML_BATCH_OUTPUT_ROOT_ELEM);
            outerDocument.appendChild(rootNode);

            for (String filepath : arguments) {
                try {

                    Document doc = DOMBuilder.buildDocFromFilepath(filepath);
                    MathMLUnificator.unifyMathML(doc, operatorUnification);
                    if (arguments.size() == 1) {
                        xmlStdoutSerializer(doc);
                    } else {
                        Node itemNode = rootNode.getOwnerDocument().createElementNS(UNIFIED_MATHML_NS,
                                UNIFIED_MATHML_NS_PREFIX + ":" + UNIFIED_MATHML_BATCH_OUTPUT_ITEM_ELEM);
                        Attr filenameAttr = itemNode.getOwnerDocument().createAttributeNS(UNIFIED_MATHML_NS,
                                UNIFIED_MATHML_NS_PREFIX + ":"
                                        + UNIFIED_MATHML_BATCH_OUTPUT_ITEM_FILEPATH_ATTR);
                        filenameAttr.setTextContent(String.valueOf(filepath));
                        ((Element) itemNode).setAttributeNodeNS(filenameAttr);
                        itemNode.appendChild(
                                rootNode.getOwnerDocument().importNode(doc.getDocumentElement(), true));
                        rootNode.appendChild(itemNode);

                    }

                } catch (SAXException | IOException ex) {
                    Logger.getLogger(MathMLUnificatorCommandLineTool.class.getName()).log(Level.SEVERE,
                            "Failed processing of file: " + filepath, ex);
                }
            }

            if (rootNode.getChildNodes().getLength() > 0) {
                xmlStdoutSerializer(rootNode.getOwnerDocument());
            }

        } else {
            printHelp(options);
            System.exit(0);
        }
    }

}

From source file:Main.java

protected static DOMImplementationLS getDOMImplementationLS(Node node) {
    Document document = node.getOwnerDocument();
    return (DOMImplementationLS) document.getImplementation();
}

From source file:Main.java

/**
 * Returns the root element.//from w w  w .j  a va 2s .c  o  m
 * 
 * @param node an XML node
 * @return the root element of the tree containing the given element
 */
public static Node getRootElement(Node node) {
    return node.getOwnerDocument().getDocumentElement();
}

From source file:Main.java

/**
 * @param sampleNode/*from w  w w  . ja v  a  2  s.c o  m*/
 */
public static void tTestSetSampleAsReference(Node sampleNode) {
    Attr attr = sampleNode.getOwnerDocument().createAttribute("ttest");
    attr.setNodeValue("reference");
    Element e = (Element) sampleNode;
    e.setAttributeNode(attr);
}

From source file:Main.java

/**
 * Node to string.//www  .  jav  a  2 s .  co  m
 *
 * @param node
 *            the node
 * @return the string
 */
public static String nodeToString(Node node) {
    Document doc = node.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) doc.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();
    serializer.getDomConfig().setParameter("xml-declaration", false);
    String string = serializer.writeToString(node);
    return string.trim();
}

From source file:Main.java

public static Node insertTextNodeAfter(String text, Node sibling) {
    Text newNode = sibling.getOwnerDocument().createTextNode(text);
    return sibling.getParentNode().insertBefore(newNode, sibling.getNextSibling());
}

From source file:Main.java

public static Node insertTextNodeBefore(String text, Node sibling) {
    Text newNode = sibling.getOwnerDocument().createTextNode(text);
    return sibling.getParentNode().insertBefore(newNode, sibling);
}

From source file:Main.java

public static Node addText(Node parent, String value) {
    Text text = parent.getOwnerDocument().createTextNode(value);
    parent.appendChild(text);//from   w  w w  . j a v  a2s . c  o m
    return text;
}

From source file:Main.java

public static String nodeToString(Node node) {
    DOMImplementationLS ls = (DOMImplementationLS) node.getOwnerDocument().getImplementation().getFeature("LS",
            "3.0");
    LSSerializer seri = ls.createLSSerializer();
    return seri.writeToString(node);
}

From source file:Main.java

/**
 * Sets an attribute on the specified node, with the given name and value
 *///w w  w. ja v a  2  s . c  o m
public static void setStrValue(Node node, String name, String value) {
    Attr att = node.getOwnerDocument().createAttribute(name);
    node.getAttributes().setNamedItem(att);
    att.setValue(value);
}