Example usage for org.dom4j Element isRootElement

List of usage examples for org.dom4j Element isRootElement

Introduction

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

Prototype

boolean isRootElement();

Source Link

Document

DOCUMENT ME!

Usage

From source file:ch.javasoft.xml.config.XmlUtil.java

License:BSD License

/**
 * Returns an xpath like string for the given xml element
 * /*  w w w . j  a  v  a  2 s  .co  m*/
 * @param elem            the element to convert to a string
 * @param recurseParents   true if parents should be included
 * 
 * @return an xpath like string for the given element
 */
public static String getElementPath(Element elem, boolean recurseParents) {
    StringBuilder sb = new StringBuilder(elem.getName());
    String name = elem.attributeValue(XmlAttribute.name.getXmlName());
    if (name != null) {
        sb.append("[" + name + "]");
    }
    if (recurseParents && !elem.isRootElement() && elem.getParent() != null) {
        sb.insert(0, '/');
        sb.insert(0, getElementPath(elem.getParent(), recurseParents));
    }
    return sb.toString();
}

From source file:com.xdtech.report.controller.ReadReportController.java

License:Apache License

@RequestMapping(params = "configReportPage")
public ModelAndView configReportPage(HttpServletRequest request, Long configId) throws Exception {
    String title = "";
    if (configId != null) {
        ConfigItem configItem = configService.loadConfigItem(configId);
        title = configItem.getName();// w w w.j  a  v  a  2  s  .  c o m
        request.setAttribute("configItem", configItem);
        SAXReader reader = new SAXReader(); // User.hbm.xml??xml 
        //         Document doc = reader.read(new File("G:/my-dev/git/xdtech-shop/src/test/java/pageTemplate.xml"));
        // ??xml 
        Document doc = DocumentHelper.parseText(configItem.getXml()); // XML 
        Element rootElement = doc.getRootElement();
        Element subGridDataElement = rootElement.element("datagrid");// ?datagrid
        //??title
        if (null != subGridDataElement.elementText("title")) {
            title = subGridDataElement.elementText("title");
        }
        //??
        Element columnsElement = subGridDataElement.element("columns");
        if (null != columnsElement && !columnsElement.isRootElement()) {
            List<Element> columnList = columnsElement.elements();
            JSONArray columnsArray = new JSONArray();
            JSONObject columnJson = null;
            for (Element element : columnList) {
                columnJson = new JSONObject();
                columnJson.put("field", element.attributeValue("field"));
                columnJson.put("title", element.attributeValue("title"));
                columnsArray.add(columnJson);
            }
            //            System.out.println(columnsArray.toString());
            request.setAttribute("columns", columnsArray.toString());
        }
    }
    request.setAttribute("showReportGridId", UUID.randomUUID().toString() + "_grid");
    request.setAttribute("title", title);
    return new ModelAndView("report/read/showReportGrid_ftl");
}

From source file:org.apache.taglibs.xtags.xpath.ReplaceTag.java

License:Apache License

public int doEndTag() throws JspException {
    Object context = TagHelper.getInputNodes(pageContext, this, false);
    if (context == null) {
        logInfo("No current node to replace");
        return EVAL_PAGE;
    }/*from   ww  w.  j av a  2  s.co m*/

    try {
        String xmlFragment = null;
        if (bodyContent != null) {
            xmlFragment = bodyContent.getString();
        }
        if (context instanceof List) {
            List els = (List) context;
            if (els.size() > 1) {
                throw new JspException("Current context contains more than one node");
            }
            if (els.size() == 1) {
                context = els.get(0);
            }
        }
        if (context instanceof Document) {
            if (xmlFragment == null) {
                throw new JspException("Cannot replace document with empty body");
            }
            Document sourceDoc = (Document) context;
            Document newDoc = DocumentHelper.parseText(xmlFragment);

            // clear source doc contents
            sourceDoc.clearContent();
            for (int i = 0, size = newDoc.nodeCount(); i < size; i++) {
                Node node = newDoc.node(i);
                // detach from new doc
                node.detach();
                // add to source
                sourceDoc.add(node);
            }
        } else {
            if (!(context instanceof Element)) {
                throw new JspException("Current node is not an Element: " + context.getClass().getName());
            }
            Element element = (Element) context;

            SAXReader reader = new SAXReader();

            if (element.isRootElement()) {
                if (xmlFragment == null) {
                    throw new JspException("Cannot replace root element with empty body");
                }
                Document newDoc = DocumentHelper.parseText(xmlFragment);
                Document sourceDoc = element.getDocument();
                Element newRoot = newDoc.getRootElement();
                newRoot.detach();
                sourceDoc.setRootElement(newRoot);
            } else {
                Element parent = element.getParent();
                List parentContent = parent.content();
                int index = parentContent.indexOf(element);
                parentContent.remove(index);
                if (xmlFragment != null) {
                    Document newDoc = DocumentHelper.parseText("<dummy>" + xmlFragment + "</dummy>");
                    parentContent.addAll(index, newDoc.getRootElement().content());
                }
            }
        }
    } catch (DocumentException e) {
        handleException(e);
    }
    return EVAL_PAGE;
}