Example usage for org.dom4j Element elementByID

List of usage examples for org.dom4j Element elementByID

Introduction

In this page you can find the example usage for org.dom4j Element elementByID.

Prototype

Element elementByID(String elementID);

Source Link

Document

Returns the element of the given ID attribute value.

Usage

From source file:ch.javasoft.metabolic.efm.config.DistributedConfig.java

License:BSD License

private static final Element getDistConfigFromPackage() throws XmlConfigException, IOException {
    final String xmlName = "config/metabolic-efm.xml";
    InputStream xmlIn = DistributedConfig.class.getClassLoader().getResourceAsStream(xmlName);
    if (xmlIn == null) {
        throw new IOException("cannot find resource: " + xmlName);
    }/*from  w w w  .  j a  v a  2 s. c  o  m*/
    try {
        final SAXReader reader = new SAXReader();
        final Document doc = reader.read(xmlIn);
        final Element root = doc.getRootElement();
        final Element elDist = root.elementByID(XmlElement.distribute.getXmlName());
        //         final String defName = root.attributeValue("default", "default");
        //         final Element elConfig    = XmlUtil.getChildElementByAttributeValue(root, ch.javasoft.xml.config.XmlConfig.XmlElement.config, XmlAttribute.name, defName, true /*throwExceptionIfNull*/);
        //         final Element elMetEfm    = XmlUtil.getRequiredSingleChildElement(elConfig, ch.javasoft.metabolic.efm.main.CalculateFluxModes.XmlElement.metabolic_efm);
        //         final Element elImpl      = XmlUtil.getRequiredSingleChildElement(elMetEfm, ch.javasoft.metabolic.efm.main.CalculateFluxModes.XmlElement.efm_impl);
        //         final Element elImplCfg   = XmlUtil.getRequiredSingleChildElement(elImpl, ch.javasoft.metabolic.efm.main.CalculateFluxModes.XmlElement.config);
        //         final Element elDist      = XmlUtil.getRequiredSingleChildElement(elImplCfg, XmlElement.distribute);
        return elDist;
    } catch (DocumentException e) {
        throw ExceptionUtil.toIOException("cannot parse " + xmlName + ", e=" + e, e);
    } finally {
        xmlIn.close();
    }
}

From source file:org.pentaho.ui.xul.impl.AbstractXulLoader.java

License:Open Source License

protected void processOverlay(Element overlayEle, Element srcEle) {
    for (Object child : overlayEle.elements()) {
        Element overlay = (Element) child;
        String overlayId = overlay.attributeValue("ID");
        logger.debug("Processing overlay\nID: " + overlayId);
        Element sourceElement = srcEle.getDocument().elementByID(overlayId);
        if (sourceElement == null) {
            logger.error("Could not find corresponding element in src doc with id: " + overlayId);
            continue;
        }/*  ww w  .ja  va2 s . com*/
        logger.debug("Found match in source doc:");

        String removeElement = overlay.attributeValue("removeelement");
        if (removeElement != null && removeElement.equalsIgnoreCase("true")) {
            sourceElement.getParent().remove(sourceElement);
        } else {

            List attribs = overlay.attributes();

            // merge in attributes
            for (Object o : attribs) {
                Attribute atr = (Attribute) o;
                sourceElement.addAttribute(atr.getName(), atr.getValue());
            }

            Document targetDocument = srcEle.getDocument();

            // lets start out by just including everything
            for (Object overlayChild : overlay.elements()) {
                Element pluckedElement = (Element) overlay.content()
                        .remove(overlay.content().indexOf(overlayChild));

                if (pluckedElement.getName().equals("dialog")) {
                    String newOnload = pluckedElement.attributeValue("onload");
                    if (newOnload != null) {
                        String existingOnload = targetDocument.getRootElement().attributeValue("onload");
                        String finalOnload = "";
                        if (existingOnload != null) {
                            finalOnload = existingOnload + ", ";
                        }
                        finalOnload += newOnload;
                        targetDocument.getRootElement().setAttributeValue("onload", finalOnload);
                    }
                }

                String insertBefore = pluckedElement.attributeValue("insertbefore");
                String insertAfter = pluckedElement.attributeValue("insertafter");
                String position = pluckedElement.attributeValue("position");

                // determine position to place it
                int positionToInsert = -1;
                if (insertBefore != null) {
                    Element insertBeforeTarget = sourceElement.elementByID(insertBefore);
                    positionToInsert = sourceElement.elements().indexOf(insertBeforeTarget);

                } else if (insertAfter != null) {
                    Element insertAfterTarget = sourceElement.elementByID(insertAfter);
                    positionToInsert = sourceElement.elements().indexOf(insertAfterTarget);
                    if (positionToInsert != -1) {
                        positionToInsert++; // we want to be after that point;
                    }
                } else if (position != null) {
                    int pos = Integer.parseInt(position);
                    positionToInsert = (pos <= sourceElement.elements().size()) ? pos : -1;
                }
                if (positionToInsert == -1) {
                    // default to last
                    positionToInsert = sourceElement.elements().size();
                }
                if (positionToInsert > sourceElement.elements().size()) {
                    sourceElement.elements().add(pluckedElement);
                } else {
                    sourceElement.elements().add(positionToInsert, pluckedElement);
                }
                logger.debug("processed overlay child: " + ((Element) overlayChild).getName() + " : "
                        + pluckedElement.getName());
            }
        }
    }
}