Example usage for org.dom4j Node getParent

List of usage examples for org.dom4j Node getParent

Introduction

In this page you can find the example usage for org.dom4j Node getParent.

Prototype

Element getParent();

Source Link

Document

getParent returns the parent Element if this node supports the parent relationship or null if it is the root element or does not support the parent relationship.

Usage

From source file:nl.tue.gale.common.GaleUtil.java

License:Open Source License

@SuppressWarnings("unchecked")
public static Node replaceNode(Node oldnode, Node newnode) {
    List<Node> content = oldnode.getParent().content();
    if (newnode != null)
        content.add(content.indexOf(oldnode), newnode);
    content.remove(oldnode);/*from   ww w .  j a va 2s . c om*/
    return newnode;
}

From source file:org.ballproject.knime.base.config.CTDFileNodeConfigurationReader.java

License:Open Source License

public String getPath(Node n) {

    List<String> path_nodes = new ArrayList<String>();
    while (n != null && !n.getName().equals("PARAMETERS")) {
        path_nodes.add(n.valueOf("@name"));
        n = n.getParent();
    }/*  w  w w  . ja  va  2s  . com*/

    Collections.reverse(path_nodes);

    String ret = "";
    int N = path_nodes.size();
    for (int i = 0; i < N; i++) {
        if (i == N - 1) {
            ret += path_nodes.get(i);
        } else {
            ret += path_nodes.get(i) + ".";
        }
    }
    return ret;
}

From source file:org.collectionspace.chain.util.xtmpl.XTmplDocument.java

License:Educational Community License

@SuppressWarnings("unchecked")
public void setTexts(String key, String[] texts) {
    Node basis = document.selectSingleNode(attach.get(key));
    Element parent = basis.getParent();
    int pos = parent.indexOf(basis);
    List<Node> nodes = (List<Node>) parent.content();
    List<Node> after = new ArrayList(nodes.subList(pos + 1, nodes.size()));
    basis.detach();//from w  w  w. j av a  2 s .  c  o m
    if (texts.length > 0) {
        for (Node n : after)
            n.detach();
        for (String text : texts) {
            Node renewed = (Node) basis.clone();
            renewed.setText(text);
            parent.add(renewed);
        }
        for (Node n : after)
            parent.add(n);
    }
}

From source file:org.danann.cernunnos.xml.AppendNodeTask.java

License:Apache License

@SuppressWarnings("unchecked")
public void perform(TaskRequest req, TaskResponse res) {

    // Figure out where to put the content...
    Branch p = null;//from  w w w  .  ja v  a 2  s .  c o  m
    int index;
    if (sibling != null) {
        Node sib = (Node) sibling.evaluate(req, res);
        p = sib.getParent();
        index = p.indexOf(sib) + 1;
    } else {
        // Work from the PARENT...
        p = (Branch) parent.evaluate(req, res);
        index = p.content().size();
    }

    // Figure out what content to add...
    List list = null;
    if (content != null && content.size() > 0) {
        list = content;
    } else {
        list = new LinkedList();
        list.add(node.evaluate(req, res));
    }

    // Evaluate phrases & add...
    for (Object o : list) {

        Node n = (Node) ((Node) o).clone();
        NodeProcessor.evaluatePhrases(n, grammar, req, res);

        // If the parent is an element, check if we should
        // carry the parent namespace over to the child...
        if ((Boolean) apply_namespace.evaluate(req, res) && p.getNodeType() == Node.ELEMENT_NODE
                && !((Element) p).getNamespace().equals(Namespace.NO_NAMESPACE)) {
            // We know the parent is an Element w/ a namespace,
            // is the child (also) an Element w/ none?
            if (n.getNodeType() == Node.ELEMENT_NODE
                    && ((Element) n).getNamespace().equals(Namespace.NO_NAMESPACE)) {
                // Yes -- we need to port the namespace forward...
                Namespace nsp = ((Element) p).getNamespace();
                if (log.isTraceEnabled()) {
                    StringBuffer msg = new StringBuffer();
                    msg.append("Adding the following namespace to <").append(n.getName()).append(">:  ")
                            .append(nsp);
                    log.trace(msg.toString());
                }
                NodeProcessor.applyNamespace(nsp, (Element) n);
            }
        }

        // Although they *are* nodes, attributes are not technically
        // content, and therefore they must have special treatment...
        if (p.getNodeType() == Node.ELEMENT_NODE && n.getNodeType() == Node.ATTRIBUTE_NODE) {
            // Add attributes by calling addAttribute on the Element contract...
            ((Element) p).add((Attribute) n);
        } else {
            // Add everything else as 'content'...
            p.content().add(index++, n);
        }

    }

}

From source file:org.danann.cernunnos.xml.DeleteNodeTask.java

License:Apache License

public void perform(TaskRequest req, TaskResponse res) {

    Node n = (Node) node.evaluate(req, res);
    n.getParent().remove(n);

}

From source file:org.danann.cernunnos.xml.PrependNodeTask.java

License:Apache License

@SuppressWarnings("unchecked")
public void perform(TaskRequest req, TaskResponse res) {

    // Figure out where to put the content...
    Branch p = null;//from w  w  w.java2  s  .c  o  m
    int index;
    if (sibling != null) {
        Node sib = (Node) sibling.evaluate(req, res);
        p = sib.getParent();
        index = p.indexOf(sib);
    } else {
        // Work from the PARENT...
        p = (Branch) parent.evaluate(req, res);
        index = 0;
    }

    // Figure out what content to add...
    List list = null;
    if (content != null && content.size() > 0) {
        list = content;
    } else {
        list = new LinkedList();
        list.add(node.evaluate(req, res));
    }

    // Evaluate phrases & add...
    for (Object o : list) {

        Node n = (Node) ((Node) o).clone();
        NodeProcessor.evaluatePhrases(n, grammar, req, res);

        // If the parent is an element, check if we should
        // carry the parent namespace over to the child...
        if ((Boolean) apply_namespace.evaluate(req, res) && p.getNodeType() == Node.ELEMENT_NODE
                && !((Element) p).getNamespace().equals(Namespace.NO_NAMESPACE)) {
            // We know the parent is an Element w/ a namespace,
            // is the child (also) an Element w/ none?
            if (n.getNodeType() == Node.ELEMENT_NODE
                    && ((Element) n).getNamespace().equals(Namespace.NO_NAMESPACE)) {
                // Yes -- we need to port the namespace forward...
                Namespace nsp = ((Element) p).getNamespace();
                if (log.isTraceEnabled()) {
                    StringBuffer msg = new StringBuffer();
                    msg.append("Adding the following namespace to <").append(n.getName()).append(">:  ")
                            .append(nsp);
                    log.trace(msg.toString());
                }
                NodeProcessor.applyNamespace(nsp, (Element) n);
            }
        }

        // Although they *are* nodes, attributes are not technically
        // content, and therefore they must have special treatment...
        if (p.getNodeType() == Node.ELEMENT_NODE && n.getNodeType() == Node.ATTRIBUTE_NODE) {
            // Add attributes by calling addAttribute on the Element contract...
            ((Element) p).add((Attribute) n);
        } else {
            // Add everything else as 'content'...
            p.content().add(index++, n);
        }

    }

}

From source file:org.firesoa.common.jxpath.model.dom4j.Dom4JNodePointer.java

License:Open Source License

/**
 * Get the parent of the specified node.
 * @param node to check/*  w  w  w  .  ja  v a  2s .  co m*/
 * @return parent Element
 */
private static Element nodeParent(Node node) {
    return node.getParent();

}

From source file:org.hibernate.console.ConfigurationFactory.java

License:Open Source License

@SuppressWarnings("unchecked")
private IConfiguration loadConfigurationXML(IConfiguration localCfg, boolean includeMappings,
        EntityResolver entityResolver) {
    File configXMLFile = prefs.getConfigXMLFile();
    if (!includeMappings) {
        org.dom4j.Document doc;//from w w  w . j av a 2 s .  co m
        XMLHelper xmlHelper = new XMLHelper();
        InputStream stream = null;
        String resourceName = "<unknown>"; //$NON-NLS-1$
        if (configXMLFile != null) {
            resourceName = configXMLFile.toString();
            try {
                stream = new FileInputStream(configXMLFile);
            } catch (FileNotFoundException e1) {
                throw new HibernateConsoleRuntimeException(
                        ConsoleMessages.ConsoleConfiguration_could_not_access + configXMLFile, e1);
            }
        } else {
            resourceName = "/hibernate.cfg.xml"; //$NON-NLS-1$
            if (checkHibernateResoureExistence(resourceName)) {
                stream = getResourceAsStream(resourceName); // simulate hibernate's
                                                            // default look up
            } else {
                return localCfg;
            }
        }
        try {
            List<SAXParseException> errors = new ArrayList<SAXParseException>();
            doc = xmlHelper.createSAXReader(resourceName, errors, entityResolver).read(new InputSource(stream));
            if (errors.size() != 0) {
                throw new MappingException(ConsoleMessages.ConsoleConfiguration_invalid_configuration,
                        errors.get(0));
            }
            List<Node> list = doc.getRootElement().element("session-factory").elements("mapping"); //$NON-NLS-1$ //$NON-NLS-2$
            for (Node element : list) {
                element.getParent().remove(element);
            }
            DOMWriter dw = new DOMWriter();
            Document document = dw.write(doc);
            return localCfg.configure(document);

        } catch (DocumentException e) {
            throw new HibernateException(
                    ConsoleMessages.ConsoleConfiguration_could_not_parse_configuration + resourceName, e);
        } finally {
            try {
                if (stream != null)
                    stream.close();
            } catch (IOException ioe) {
                // log.warn( "could not close input stream for: " + resourceName, ioe );
            }
        }
    } else {
        if (configXMLFile != null) {
            return localCfg.configure(configXMLFile);
        } else {
            IConfiguration resultCfg = localCfg;
            if (checkHibernateResoureExistence("/hibernate.cfg.xml")) { //$NON-NLS-1$
                resultCfg = localCfg.configure();
            }
            return resultCfg;
        }
    }
}

From source file:org.jboss.tools.hibernate3_6.ConfigurationFactory.java

License:Open Source License

@SuppressWarnings("unchecked")
private Configuration loadConfigurationXML(Configuration localCfg, boolean includeMappings,
        EntityResolver entityResolver) {
    File configXMLFile = prefs.getConfigXMLFile();
    if (!includeMappings) {
        org.dom4j.Document doc;//from www  . ja  v  a  2 s  .  c  om
        XMLHelper xmlHelper = new XMLHelper();
        InputStream stream = null;
        String resourceName = "<unknown>"; //$NON-NLS-1$
        if (configXMLFile != null) {
            resourceName = configXMLFile.toString();
            try {
                stream = new FileInputStream(configXMLFile);
            } catch (FileNotFoundException e1) {
                throw new HibernateConsoleRuntimeException(
                        ConsoleMessages.ConsoleConfiguration_could_not_access + configXMLFile, e1);
            }
        } else {
            resourceName = "/hibernate.cfg.xml"; //$NON-NLS-1$
            if (checkHibernateResoureExistence(resourceName)) {
                stream = ConfigHelper.getResourceAsStream(resourceName); // simulate hibernate's
                // default look up
            } else {
                return localCfg;
            }
        }
        try {
            List<Throwable> errors = new ArrayList<Throwable>();
            doc = xmlHelper.createSAXReader(resourceName, errors, entityResolver).read(new InputSource(stream));
            if (errors.size() != 0) {
                throw new MappingException(ConsoleMessages.ConsoleConfiguration_invalid_configuration,
                        errors.get(0));
            }
            List<Node> list = doc.getRootElement().element("session-factory").elements("mapping"); //$NON-NLS-1$ //$NON-NLS-2$
            for (Node element : list) {
                element.getParent().remove(element);
            }
            DOMWriter dw = new DOMWriter();
            Document document = dw.write(doc);
            return localCfg.configure(document);

        } catch (DocumentException e) {
            throw new HibernateException(
                    ConsoleMessages.ConsoleConfiguration_could_not_parse_configuration + resourceName, e);
        } finally {
            try {
                if (stream != null)
                    stream.close();
            } catch (IOException ioe) {
                // log.warn( "could not close input stream for: " + resourceName, ioe );
            }
        }
    } else {
        if (configXMLFile != null) {
            return localCfg.configure(configXMLFile);
        } else {
            Configuration resultCfg = localCfg;
            if (checkHibernateResoureExistence("/hibernate.cfg.xml")) { //$NON-NLS-1$
                resultCfg = localCfg.configure();
            }
            return resultCfg;
        }
    }
}

From source file:org.nuxeo.ecm.jsf2.migration.parser.ReRenderParser.java

License:Open Source License

@Override
public void migrate(Document input) throws Exception {
    // Migrate the elements matching the rule
    if (rule.isMigrationAuto()) {
        for (Node node : listElementsToMigrate) {
            // Change the name of attribute "reRender"
            Attribute attribute = createNewAttribute((Attribute) node);
            Element parentElement = node.getParent();
            parentElement.remove(node);/*from w  w  w.  j a v a 2 s  .c  om*/
            parentElement.add(attribute);
        }
    }
}