Example usage for org.dom4j Element addText

List of usage examples for org.dom4j Element addText

Introduction

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

Prototype

Element addText(String text);

Source Link

Document

Adds a new Text node with the given text to this element.

Usage

From source file:org.opencms.setup.xml.CmsSetupXmlHelper.java

License:Open Source License

/**
 * Handles the xpath name, by creating the given node and its children.<p>
 * /*from   ww w  .j a  v  a  2s .  com*/
 * @param parent the parent node to use
 * @param xpathName the xpathName, ie <code>a[@b='c'][d='e'][text()='f']</code>
 * 
 * @return the new created element
 */
private static Element handleNode(Element parent, String xpathName) {

    // if node is no attribute, create a new node
    String childrenPart = null;
    String nodeName;
    int pos = xpathName.indexOf("[");
    if (pos > 0) {
        childrenPart = xpathName.substring(pos + 1, xpathName.length() - 1);
        nodeName = xpathName.substring(0, pos);
    } else {
        nodeName = xpathName;
    }
    // create node
    Element elem = parent.addElement(nodeName);
    if (childrenPart != null) {
        pos = childrenPart.indexOf("[");
        if ((pos > 0) && (childrenPart.indexOf("]") > pos)) {
            handleNode(elem, childrenPart);
            return elem;
        }
        Map<String, String> children = CmsStringUtil.splitAsMap(childrenPart, "][", "=");
        // handle child nodes
        for (Map.Entry<String, String> child : children.entrySet()) {
            String childName = child.getKey();
            String childValue = child.getValue();
            if (childValue.startsWith("'")) {
                childValue = childValue.substring(1);
            }
            if (childValue.endsWith("'")) {
                childValue = childValue.substring(0, childValue.length() - 1);
            }
            if (childName.startsWith("@")) {
                elem.addAttribute(childName.substring(1), childValue);
            } else if (childName.equals("text()")) {
                elem.setText(childValue);
            } else if (!childName.contains("(")) {
                Element childElem = elem.addElement(childName);
                if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(childValue)) {
                    childElem.addText(childValue);
                }
            }
        }
    }
    return elem;
}

From source file:org.opencms.util.ant.CmsSetupXmlHelper.java

License:Open Source License

/**
 * Handles the xpath name, by creating the given node and its children.<p>
 * /* w  w w  . j a  va  2s.c om*/
 * @param parent the parent node to use
 * @param xpathName the xpathName, ie <code>a[@b='c'][d='e'][text()='f']</code>
 * 
 * @return the new created element
 */
private static Element handleNode(Element parent, String xpathName) {

    // if node is no attribute, create a new node
    String childrenPart = null;
    String nodeName;
    int pos = xpathName.indexOf("[");
    if (pos > 0) {
        childrenPart = xpathName.substring(pos + 1, xpathName.length() - 1);
        nodeName = xpathName.substring(0, pos);
    } else {
        nodeName = xpathName;
    }
    // create node
    Element elem = parent.addElement(nodeName);
    if (childrenPart != null) {
        pos = childrenPart.indexOf("[");
        if ((pos > 0) && (childrenPart.indexOf("]") > pos)) {
            handleNode(elem, childrenPart);
            return elem;
        }
        Map<String, String> children = CmsStringUtil.splitAsMap(childrenPart, "][", "=");
        // handle child nodes
        for (Map.Entry<String, String> child : children.entrySet()) {
            String childName = child.getKey();
            String childValue = child.getValue();
            if (childValue.startsWith("'")) {
                childValue = childValue.substring(1);
            }
            if (childValue.endsWith("'")) {
                childValue = childValue.substring(0, childValue.length() - 1);
            }
            if (childName.startsWith("@")) {
                elem.addAttribute(childName.substring(1), childValue);
            } else if (childName.equals("text()")) {
                elem.setText(childValue);
            } else if (!childName.contains("(")) {
                Element childElem = elem.addElement(childName);
                if (!CmsStringUtil.isEmptyOrWhitespaceOnly(childValue)) {
                    childElem.addText(childValue);
                }
            }
        }
    }
    return elem;
}

From source file:org.opencms.webdav.CmsWebdavServlet.java

License:Open Source License

/**
 * Propfind helper method.<p>/*  ww  w .  j  a  v  a  2  s.  com*/
 *
 * @param req the servlet request
 * @param elem the parent element where to add the generated subelements
 * @param item the current item where to parse the properties
 * @param type the propfind type
 * @param propertiesVector if the propfind type is find properties by
 *          name, then this Vector contains those properties
 */
private void parseProperties(HttpServletRequest req, Element elem, I_CmsRepositoryItem item, int type,
        List<String> propertiesVector) {

    String path = item.getName();
    Element responseElem = addElement(elem, TAG_RESPONSE);

    String status = "HTTP/1.1 " + CmsWebdavStatus.SC_OK + " "
            + CmsWebdavStatus.getStatusText(CmsWebdavStatus.SC_OK);

    // Generating href element
    Element hrefElem = addElement(responseElem, TAG_HREF);

    String href = req.getContextPath() + req.getServletPath();
    if ((href.endsWith("/")) && (path.startsWith("/"))) {
        href += path.substring(1);
    } else {
        href += path;
    }

    try {
        hrefElem.addText(rewriteUrl(href));
    } catch (UnsupportedEncodingException ex) {
        return;
    }

    String resourceName = path;

    Element propstatElem = addElement(responseElem, TAG_PROPSTAT);
    Element propElem = addElement(propstatElem, TAG_PROP);

    switch (type) {

    case FIND_ALL_PROP:

        addElement(propElem, TAG_CREATIONDATE).addText(ISO8601_FORMAT.format(new Date(item.getCreationDate())));
        addElement(propElem, TAG_DISPLAYNAME).addCDATA(resourceName);

        // properties only for files (no collections)
        if (!item.isCollection()) {

            addElement(propElem, TAG_LASTMODIFIED)
                    .addText(HTTP_DATE_FORMAT.format(new Date(item.getLastModifiedDate())));

            addElement(propElem, TAG_CONTENTLENGTH).addText(String.valueOf(item.getContentLength()));

            String contentType = getServletContext().getMimeType(item.getName());
            if (contentType != null) {
                addElement(propElem, TAG_CONTENTTYPE).addText(contentType);
            }
            addElement(propElem, TAG_ETAG).addText(getETag(item));
            addElement(propElem, TAG_RESOURCETYPE);
        } else {
            addElement(addElement(propElem, TAG_RESOURCETYPE), TAG_COLLECTION);
        }

        addElement(propElem, TAG_SOURCE).addText("");

        Element suppLockElem = addElement(propElem, TAG_SUPPORTEDLOCK);
        Element lockEntryElem = addElement(suppLockElem, TAG_LOCKENTRY);
        addElement(addElement(lockEntryElem, TAG_LOCKSCOPE), CmsRepositoryLockInfo.SCOPE_EXCLUSIVE);
        addElement(addElement(lockEntryElem, TAG_LOCKTYPE), CmsRepositoryLockInfo.TYPE_WRITE);
        lockEntryElem = addElement(suppLockElem, TAG_LOCKENTRY);
        addElement(addElement(lockEntryElem, TAG_LOCKSCOPE), CmsRepositoryLockInfo.SCOPE_SHARED);
        addElement(addElement(lockEntryElem, TAG_LOCKTYPE), CmsRepositoryLockInfo.TYPE_WRITE);

        generateLockDiscovery(path, propElem, req);

        addElement(propstatElem, TAG_STATUS).addText(status);

        break;

    case FIND_PROPERTY_NAMES:

        addElement(propElem, TAG_CREATIONDATE);
        addElement(propElem, TAG_DISPLAYNAME);
        if (!item.isCollection()) {

            addElement(propElem, TAG_CONTENTLANGUAGE);
            addElement(propElem, TAG_CONTENTLENGTH);
            addElement(propElem, TAG_CONTENTTYPE);
            addElement(propElem, TAG_ETAG);
        }
        addElement(propElem, TAG_LASTMODIFIED);
        addElement(propElem, TAG_RESOURCETYPE);
        addElement(propElem, TAG_SOURCE);
        addElement(propElem, TAG_LOCKDISCOVERY);

        addElement(propstatElem, TAG_STATUS).addText(status);

        break;

    case FIND_BY_PROPERTY:

        List<String> propertiesNotFound = new Vector<String>();

        // Parse the list of properties
        Iterator<String> iter = propertiesVector.iterator();
        while (iter.hasNext()) {
            String property = iter.next();

            if (property.equals(TAG_CREATIONDATE)) {
                addElement(propElem, TAG_CREATIONDATE)
                        .addText(ISO8601_FORMAT.format(new Date(item.getCreationDate())));
            } else if (property.equals(TAG_DISPLAYNAME)) {
                addElement(propElem, TAG_DISPLAYNAME).addCDATA(resourceName);
            } else if (property.equals(TAG_CONTENTLANGUAGE)) {
                if (item.isCollection()) {
                    propertiesNotFound.add(property);
                } else {
                    addElement(propElem, TAG_CONTENTLANGUAGE);
                }
            } else if (property.equals(TAG_CONTENTLENGTH)) {
                if (item.isCollection()) {
                    propertiesNotFound.add(property);
                } else {
                    addElement(propElem, TAG_CONTENTLENGTH).addText((String.valueOf(item.getContentLength())));
                }
            } else if (property.equals(TAG_CONTENTTYPE)) {
                if (item.isCollection()) {
                    propertiesNotFound.add(property);
                } else {
                    String contentType = item.getMimeType();
                    if (contentType == null) {
                        contentType = getServletContext().getMimeType(item.getName());
                    }

                    if (contentType != null) {
                        addElement(propElem, TAG_CONTENTTYPE).addText(contentType);
                    }
                }
            } else if (property.equals(TAG_ETAG)) {
                if (item.isCollection()) {
                    propertiesNotFound.add(property);
                } else {
                    addElement(propElem, TAG_ETAG).addText(getETag(item));
                }
            } else if (property.equals(TAG_LASTMODIFIED)) {
                addElement(propElem, TAG_LASTMODIFIED)
                        .addText(HTTP_DATE_FORMAT.format(new Date(item.getLastModifiedDate())));
            } else if (property.equals(TAG_RESOURCETYPE)) {
                if (item.isCollection()) {
                    addElement(addElement(propElem, TAG_RESOURCETYPE), TAG_COLLECTION);
                } else {
                    addElement(propElem, TAG_RESOURCETYPE);
                }
            } else if (property.equals(TAG_SOURCE)) {
                addElement(propElem, TAG_SOURCE).addText("");
            } else if (property.equals(TAG_SUPPORTEDLOCK)) {
                suppLockElem = addElement(propElem, TAG_SUPPORTEDLOCK);
                lockEntryElem = addElement(suppLockElem, TAG_LOCKENTRY);
                addElement(addElement(lockEntryElem, TAG_LOCKSCOPE), CmsRepositoryLockInfo.SCOPE_EXCLUSIVE);
                addElement(addElement(lockEntryElem, TAG_LOCKTYPE), CmsRepositoryLockInfo.TYPE_WRITE);
                lockEntryElem = addElement(suppLockElem, TAG_LOCKENTRY);
                addElement(addElement(lockEntryElem, TAG_LOCKSCOPE), CmsRepositoryLockInfo.SCOPE_SHARED);
                addElement(addElement(lockEntryElem, TAG_LOCKTYPE), CmsRepositoryLockInfo.TYPE_WRITE);
            } else if (property.equals(TAG_LOCKDISCOVERY)) {
                if (!generateLockDiscovery(path, propElem, req)) {
                    addElement(propElem, TAG_LOCKDISCOVERY);
                }
            } else {
                propertiesNotFound.add(property);
            }
        }

        addElement(propstatElem, TAG_STATUS).addText(status);

        if (propertiesNotFound.size() > 0) {
            status = "HTTP/1.1 " + CmsWebdavStatus.SC_NOT_FOUND + " "
                    + CmsWebdavStatus.getStatusText(CmsWebdavStatus.SC_NOT_FOUND);

            propstatElem = addElement(responseElem, TAG_PROPSTAT);
            propElem = addElement(propstatElem, TAG_PROP);

            Iterator<String> notFoundIter = propertiesNotFound.iterator();
            while (notFoundIter.hasNext()) {
                addElement(propElem, notFoundIter.next());
            }

            addElement(propstatElem, TAG_STATUS).addText(status);
        }

        break;

    default:

        if (LOG.isErrorEnabled()) {
            LOG.error(Messages.get().getBundle().key(Messages.LOG_INVALID_PROPFIND_TYPE_0));
        }
        break;
    }
}

From source file:org.opensha.commons.geo.RegionUtils.java

License:Apache License

public static void regionToKML(Region region, String filename, Color c) {
    String kmlFileName = filename + ".kml";
    Document doc = DocumentHelper.createDocument();
    Element root = new DefaultElement("kml", new Namespace("", "http://www.opengis.net/kml/2.2"));
    doc.add(root);/*from   www  .  j  a  va2s .  com*/

    Element e_doc = root.addElement("Document");
    Element e_doc_name = e_doc.addElement("name");
    e_doc_name.addText(kmlFileName);

    addBorderStyle(e_doc, c);
    addBorderVertexStyle(e_doc);
    addGridNodeStyle(e_doc, c);

    Element e_folder = e_doc.addElement("Folder");
    Element e_folder_name = e_folder.addElement("name");
    e_folder_name.addText("region");
    Element e_open = e_folder.addElement("open");
    e_open.addText("1");

    addBorder(e_folder, region);
    addPoints(e_folder, "Border Nodes", region.getBorder(), Style.BORDER_VERTEX);
    if (region.getInteriors() != null) {
        for (LocationList interior : region.getInteriors()) {
            addPoints(e_folder, "Interior Nodes", interior, Style.BORDER_VERTEX);
        }
    }

    if (region instanceof GriddedRegion) {
        addPoints(e_folder, "Grid Nodes", ((GriddedRegion) region).getNodeList(), Style.GRID_NODE);
    }

    // TODO absolutely need to create seom platform specific output directory
    // that is not in project space (e.g. desktop, Decs and Settings);

    String outDirName = "sha_kml/";
    File outDir = new File(outDirName);
    outDir.mkdirs();
    String tmpFile = outDirName + kmlFileName;

    try {
        //XMLUtils.writeDocumentToFile(tmpFile, doc);
        XMLWriter writer;
        OutputFormat format = new OutputFormat("\t", true);
        writer = new XMLWriter(new FileWriter(tmpFile), format);
        writer.write(doc);
        writer.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    //Element e = new Elem
}

From source file:org.opensha.commons.geo.RegionUtils.java

License:Apache License

public static void locListToKML(LocationList locs, String filename, Color c) {
    String kmlFileName = filename + ".kml";
    Document doc = DocumentHelper.createDocument();
    Element root = new DefaultElement("kml", new Namespace("", "http://www.opengis.net/kml/2.2"));
    doc.add(root);/* w ww. j  av  a2  s  .c o m*/

    Element e_doc = root.addElement("Document");
    Element e_doc_name = e_doc.addElement("name");
    e_doc_name.addText(kmlFileName);

    addBorderStyle(e_doc, c);
    addBorderVertexStyle(e_doc);
    addGridNodeStyle(e_doc, c);

    Element e_folder = e_doc.addElement("Folder");
    Element e_folder_name = e_folder.addElement("name");
    e_folder_name.addText("region");
    Element e_open = e_folder.addElement("open");
    e_open.addText("1");

    //      addLocationPoly(e_folder, locs);
    addLocationLine(e_folder, locs);
    addPoints(e_folder, "Border Nodes", locs, Style.BORDER_VERTEX);

    // TODO absolutely need to create seom platform specific output directory
    // that is not in project space (e.g. desktop, Decs and Settings);

    String outDirName = "sha_kml/";
    File outDir = new File(outDirName);
    outDir.mkdirs();
    String tmpFile = outDirName + kmlFileName;

    try {
        //XMLUtils.writeDocumentToFile(tmpFile, doc);
        XMLWriter writer;
        OutputFormat format = new OutputFormat("\t", true);
        writer = new XMLWriter(new FileWriter(tmpFile), format);
        writer.write(doc);
        writer.close();
    } catch (IOException ioe) {
        ioe.printStackTrace();
    }
    //Element e = new Elem
}

From source file:org.opensha.commons.geo.RegionUtils.java

License:Apache License

private static Element addBorder(Element e, Region region) {
    Element e_placemark = e.addElement("Placemark");
    Element e_name = e_placemark.addElement("name");
    e_name.addText("Border");
    Element e_style = e_placemark.addElement("styleUrl");
    e_style.addText("#" + Style.BORDER.toString());
    Element e_poly = e_placemark.addElement("Polygon");
    Element e_tessellate = e_poly.addElement("tessellate");
    e_tessellate.addText("1");

    addPoly(e_poly, "outerBoundaryIs", region.getBorder());
    if (region.getInteriors() != null) {
        for (LocationList interior : region.getInteriors()) {
            addPoly(e_poly, "innerBoundaryIs", interior);
        }//  ww w .  j a v  a 2  s .c  o m
    }

    return e;
}

From source file:org.opensha.commons.geo.RegionUtils.java

License:Apache License

private static Element addLocationPoly(Element e, LocationList locs) {
    Element e_placemark = e.addElement("Placemark");
    Element e_name = e_placemark.addElement("name");
    e_name.addText("Border");
    Element e_style = e_placemark.addElement("styleUrl");
    e_style.addText("#" + Style.BORDER.toString());
    Element e_poly = e_placemark.addElement("Polygon");
    Element e_tessellate = e_poly.addElement("tessellate");
    e_tessellate.addText("1");

    addPoly(e_poly, "outerBoundaryIs", locs);
    return e;/*from w ww.  j  a  v a2  s.  c  om*/
}

From source file:org.opensha.commons.geo.RegionUtils.java

License:Apache License

private static Element addPoly(Element e, String polyName, LocationList locations) {

    Element e_BI = e.addElement(polyName);
    Element e_LR = e_BI.addElement("LinearRing");
    Element e_coord = e_LR.addElement("coordinates");

    StringBuffer sb = new StringBuffer(NL);
    for (Location loc : locations) {
        sb.append(loc.toKML() + NL);//from  w w  w  . j a  v a 2s .c o  m
    }
    // region borders do not repeat the first
    // vertex, but kml closed polygons do
    sb.append(locations.get(0).toKML() + NL);
    e_coord.addText(sb.toString());

    return e;
}

From source file:org.opensha.commons.geo.RegionUtils.java

License:Apache License

private static Element addLocationLine(Element e, LocationList locs) {
    Element e_placemark = e.addElement("Placemark");
    Element e_name = e_placemark.addElement("name");
    e_name.addText("Trace");
    Element e_style = e_placemark.addElement("styleUrl");
    e_style.addText("#" + Style.BORDER.toString());
    Element e_line = e_placemark.addElement("LineString");
    Element e_tessellate = e_line.addElement("tessellate");
    e_tessellate.addText("1");
    Element e_coord = e_line.addElement("coordinates");
    StringBuffer sb = new StringBuffer(NL);
    for (Location loc : locs) {
        sb.append(loc.toKML() + NL);/*from w  w w  . ja va2s.  c  om*/
    }
    e_coord.addText(sb.toString());
    return e;
}

From source file:org.opensha.commons.geo.RegionUtils.java

License:Apache License

private static Element addPoints(Element e, String folderName, LocationList locations, Style style) {
    Element e_folder = e.addElement("Folder");
    Element e_folder_name = e_folder.addElement("name");
    e_folder_name.addText(folderName);
    Element e_open = e_folder.addElement("open");
    e_open.addText("0");
    // loop nodes
    for (Location loc : locations) {
        Element e_placemark = e_folder.addElement("Placemark");
        Element e_style = e_placemark.addElement("styleUrl");
        e_style.addText("#" + style.toString());
        Element e_poly = e_placemark.addElement("Point");
        Element e_coord = e_poly.addElement("coordinates");
        //System.out.println(loc.toKML()); // TODO clean
        e_coord.addText(loc.toKML());//from ww  w  .  j  ava2  s .co m
    }
    return e;
}