Example usage for org.dom4j DocumentHelper createElement

List of usage examples for org.dom4j DocumentHelper createElement

Introduction

In this page you can find the example usage for org.dom4j DocumentHelper createElement.

Prototype

public static Element createElement(String name) 

Source Link

Usage

From source file:org.pentaho.platform.web.refactor.SubscriptionAdminUIComponent.java

License:Open Source License

Element getException(final String message, final Throwable t) {
    Element root = DocumentHelper.createElement(SubscriptionAdminUIComponent.NODE_EXCEPTION);
    if (message != null) {
        root.addElement("message").addText(message); //$NON-NLS-1$
    }// w w w.j  a  va2 s  . com

    // Some exceptions may have null messages, look for one in the chain.
    String exMsg = null;
    Throwable tmpT = t;
    while ((tmpT != null) && (exMsg == null)) {
        exMsg = tmpT.getLocalizedMessage();
        tmpT = tmpT.getCause();
    }

    root.addElement("exceptionMessage").addText((exMsg != null) ? exMsg //$NON-NLS-1$
            : Messages.getString("SubscriptionAdminUIComponent.ERROR_CAUSE_UNKNOWN", t.getClass().getName())); //$NON-NLS-1$
    return (root);
}

From source file:org.pentaho.platform.web.refactor.SubscriptionAdminUIComponent.java

License:Open Source License

private Element createTextElement(final String elementName, final String text) {
    return (DocumentHelper.createElement(elementName).addText(text));
}

From source file:org.pentaho.platform.web.refactor.SubscriptionAdminUIComponent.java

License:Open Source License

/**
 * Verify that the passed in parameterNames exist in the HTTP request. Missing
 * parameters are added to ele as new parameter error nodes (see
 * NODE_PARAM_ERRORS, and NODE_PARAM_MISSING) If 'ele' is null, a new Element is
 * created to contain the error messages. The Element 'ele' is returned to the
 * caller unchanged if all the parameters pass verification.
 * //  w w w  .j  a  v  a2  s.  c  o m
 * @param ele
 *            The element that the new parameter error Elements will be added to.
 *            If null, a new one will be created
 * @param paramNames
 *            The names of the parameters to verify in the request.
 * @param notEmpty
 *            if true, the parameter must exist and can not be the empty string ""
 */
Element validateParameters(final String params[], final boolean notEmpty, Element ele) {
    Object param;
    for (String element : params) {
        param = getObjectParameter(element, null);
        if ((param == null) || (notEmpty && isEmpty(param))) {
            if (ele == null) {
                ele = DocumentHelper.createElement(SubscriptionAdminUIComponent.NODE_PARAM_ERRORS);
            }
            ele.addElement(SubscriptionAdminUIComponent.NODE_PARAM_MISSING).addText(element);
        }
    }
    return (ele);
}

From source file:org.snipsnap.snip.attachment.Attachments.java

License:Open Source License

private String serialize() {
    if (null == attachments) {
        return cache;
    }/*from   w w  w  .  j a  v a2 s . com*/

    Element attElement = DocumentHelper.createElement(ATTACHMENTS);
    Iterator it = attachments.values().iterator();
    while (it.hasNext()) {
        Attachment attachment = (Attachment) it.next();
        Element attachmentNode = attElement.addElement(ATTACHMENT);
        attachmentNode.addElement(NAME).addText(attachment.getName());
        attachmentNode.addElement(CONTENTTYPE).addText(attachment.getContentType());
        attachmentNode.addElement(SIZE).addText("" + attachment.getSize());
        attachmentNode.addElement(DATE).addText("" + attachment.getDate().getTime());
        attachmentNode.addElement(LOCATION).addText(attachment.getLocation());
    }

    return cache = toString(attElement);
}

From source file:org.snipsnap.snip.storage.SnipDataSerializer.java

License:Open Source License

/**
 * Special serialize method that does not have dependencies on regular Snip
 * or other implementations used throughout snipsnap. This is necessary to
 * be able to dump a database with a small utility.
 *
 * @param snipMap a map containing the snips data
 * @return an element that can be serializes as XML
 *//* w  w  w  . j a v a 2 s.  c  o  m*/
public Element serialize(Map snipMap) {
    Element snipElement = DocumentHelper.createElement(SNIP);

    snipElement.addElement(SNIP_NAME).addText((String) snipMap.get(SNIP_NAME));
    snipElement.addElement(SNIP_OUSER).addText(notNull(snipMap.get(SNIP_OUSER)));
    snipElement.addElement(SNIP_CUSER).addText(notNull(snipMap.get(SNIP_CUSER)));
    snipElement.addElement(SNIP_MUSER).addText(notNull(snipMap.get(SNIP_MUSER)));
    snipElement.addElement(SNIP_CTIME).addText(notNull(snipMap.get(SNIP_CTIME)));
    snipElement.addElement(SNIP_MTIME).addText(notNull(snipMap.get(SNIP_MTIME)));
    snipElement.addElement(SNIP_PERMISSIONS).addText(notNull(snipMap.get(SNIP_PERMISSIONS)));
    snipElement.add(addCDATAContent(SNIP_BACKLINKS, (String) snipMap.get(SNIP_BACKLINKS)));
    snipElement.add(addCDATAContent(SNIP_SNIPLINKS, (String) snipMap.get(SNIP_SNIPLINKS)));
    snipElement.add(addCDATAContent(SNIP_LABELS, (String) snipMap.get(SNIP_LABELS)));
    snipElement.add(addXMLContent(SNIP_ATTACHMENTS, notNull(snipMap.get(SNIP_ATTACHMENTS))));
    snipElement.addElement(SNIP_VIEWCOUNT).addText(notNull(snipMap.get(SNIP_VIEWCOUNT)));
    snipElement.add(addCDATAContent(SNIP_CONTENT, (String) snipMap.get(SNIP_CONTENT)));
    snipElement.addElement(SNIP_VERSION).addText(notNull(snipMap.get(SNIP_VERSION)));
    snipElement.addElement(SNIP_APPLICATION).addText(notNull(snipMap.get(SNIP_APPLICATION)));

    // TODO deprecated
    snipElement.addElement(SNIP_PARENT).addText(notNull(snipMap.get(SNIP_PARENT)));
    snipElement.addElement(SNIP_COMMENTED).addText(notNull(snipMap.get(SNIP_COMMENTED)));

    return snipElement;
}

From source file:org.snipsnap.snip.storage.SnipDataSerializer.java

License:Open Source License

/**
 * Add an element whose content is put into a cdata section
 * @param elementName the element to be created
 * @param content the content as a string
 * @return the newly created element/*from  ww w.j a v  a 2 s.  c o  m*/
 */
private Element addCDATAContent(String elementName, String content) {
    Element element = DocumentHelper.createElement(elementName);
    if (null == content || "".equals(content)) {
        return element;
    }
    element.addCDATA(content);
    return element;
}

From source file:org.snipsnap.snip.storage.SnipDataSerializer.java

License:Open Source License

/**
 * Create an element whose content is also xml to make sure it is not
 * escaped in the output//from www  .  ja  v a 2  s. c om
 * @param elementName name of the new element
 * @param content the actual xml as a string
 * @return the serialized element
 */
private Element addXMLContent(String elementName, String content) {
    if (null != content && !"".equals(content)) {
        try {
            StringReader stringReader = new StringReader(content);
            SAXReader saxReader = new SAXReader();
            Document doc = saxReader.read(stringReader);
            return doc.getRootElement();
        } catch (Exception e) {
            Logger.warn("SnipSerializer: unable to add xml content: " + e);
            e.printStackTrace();
        }
    }
    return DocumentHelper.createElement(elementName);
}

From source file:org.snipsnap.snip.storage.UserSerializer.java

License:Open Source License

/**
 * Store a snip in an XML node./*from  w w  w . j  av  a  2 s  .c  o m*/
 * @param user the user to store
 * @return the serialized user as XML
 */
public Element serialize(snipsnap.api.user.User user) {
    Element userElement = DocumentHelper.createElement(USER);
    userElement.addElement(USER_NAME).addText(user.getLogin());
    userElement.addElement(USER_PASSWORD).addText(notNull(user.getPasswd()));
    userElement.addElement(USER_EMAIL).addText(notNull(user.getEmail()));
    userElement.addElement(USER_ROLES).addText(user.getRoles().toString());
    userElement.addElement(USER_STATUS).addText(notNull(user.getStatus()));
    userElement.addElement(USER_CTIME).addText(getStringTimestamp(user.getCTime()));
    userElement.addElement(USER_MTIME).addText(getStringTimestamp(user.getMTime()));
    userElement.addElement(USER_LAST_ACCESS).addText(getStringTimestamp(user.getLastAccess()));
    userElement.addElement(USER_LAST_LOGIN).addText(getStringTimestamp(user.getLastLogin()));
    userElement.addElement(USER_LAST_LOGOUT).addText(getStringTimestamp(user.getLastLogout()));
    userElement.addElement(USER_APPLICATION).addText(notNull(user.getApplication()));

    return userElement;
}

From source file:org.snipsnap.snip.XMLSnipExport.java

License:Open Source License

public static void store(OutputStream out, List snips, List users, String filter, List ignoreElements,
        File fileStore) {/*from   ww w.  j  av a 2  s. c o m*/
    try {
        OutputFormat outputFormat = new OutputFormat();
        outputFormat.setEncoding("UTF-8");
        outputFormat.setNewlines(true);
        XMLWriter xmlWriter = new XMLWriter(out, outputFormat);
        Element root = DocumentHelper.createElement("snipspace");
        xmlWriter.writeOpen(root);
        storeUsers(xmlWriter, users);
        storeSnips(xmlWriter, snips, filter, ignoreElements, fileStore);
        xmlWriter.writeClose(root);
        xmlWriter.flush();
    } catch (Exception e) {
        e.printStackTrace();
    }
}