Example usage for org.dom4j Element clearContent

List of usage examples for org.dom4j Element clearContent

Introduction

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

Prototype

void clearContent();

Source Link

Document

Clears the content for this branch, removing any Node instances this branch may contain.

Usage

From source file:org.jivesoftware.openfire.fastpath.WorkgroupSettings.java

License:Open Source License

/**
 * Returns the data stored under a key corresponding to the name and namespace
 * of the given element. The Element must be in the form:<p>
 *
 * <code>&lt;name xmlns='namespace'/&gt;</code><p>
 * /*from ww  w  .  ja v  a  2  s  .c  om*/
 * If no data is currently stored under the given key, an empty element will be
 * returned.
 *
 * @param data an XML document who's element name and namespace is used to
 *      match previously stored private data.
 * @param workgroupName the name of the workgroup who's data is to be stored.
 * @return the data stored under the given key or the data element.
 */
public Element get(String workgroupName, Element data) {
    data.clearContent();
    Connection con = null;
    PreparedStatement pstmt = null;
    ResultSet rs = null;
    try {
        con = DbConnectionManager.getConnection();
        pstmt = con.prepareStatement(LOAD_SETTINGS);
        pstmt.setString(1, workgroupName);
        pstmt.setString(2, data.getNamespaceURI());
        rs = pstmt.executeQuery();
        if (rs.next()) {
            Document document = DocumentHelper.parseText(rs.getString(1).trim());
            data = document.getRootElement();
        }
    } catch (Exception e) {
        Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
    } finally {
        DbConnectionManager.closeConnection(rs, pstmt, con);
    }
    return data;
}

From source file:org.jivesoftware.openfire.PrivateStorage.java

License:Open Source License

/**
 * Returns the data stored under a key corresponding to the name and namespace
 * of the given element. The Element must be in the form:<p>
 *
 * <code>&lt;name xmlns='namespace'/&gt;</code><p>
 *
 * If no data is currently stored under the given key, an empty element will be
 * returned.//  w  w  w .  j  a  va 2s  .c om
 *
 * @param data an XML document who's element name and namespace is used to
 *      match previously stored private data.
 * @param username the username of the account where private data is being stored.
 * @return the data stored under the given key or the data element.
 */
public Element get(String username, Element data) {
    if (enabled) {
        Connection con = null;
        PreparedStatement pstmt = null;
        ResultSet rs = null;
        SAXReader xmlReader = null;
        try {
            // Get a sax reader from the pool
            xmlReader = xmlReaders.take();
            con = DbConnectionManager.getConnection();
            pstmt = con.prepareStatement(LOAD_PRIVATE);
            pstmt.setString(1, username);
            pstmt.setString(2, data.getNamespaceURI());
            rs = pstmt.executeQuery();
            if (rs.next()) {
                data.clearContent();
                String result = rs.getString(1).trim();
                Document doc = xmlReader.read(new StringReader(result));
                data = doc.getRootElement();
            }
        } catch (Exception e) {
            Log.error(LocaleUtils.getLocalizedString("admin.error"), e);
        } finally {
            DbConnectionManager.closeConnection(rs, pstmt, con);
            // Return the sax reader to the pool
            if (xmlReader != null) {
                xmlReaders.add(xmlReader);
            }
        }
    }
    return data;
}

From source file:org.opencms.relations.CmsLinkUpdateUtil.java

License:Open Source License

/**
 * Updates the given xml node with the given value.<p>
 * //w ww  .  j a  v a 2 s .  c o  m
 * @param parent the parent node
 * @param nodeName the node to update
 * @param value the value to use to update the given node, can be <code>null</code>
 * @param cdata if the value should be in a CDATA section or not
 */
private static void updateNode(Element parent, String nodeName, String value, boolean cdata) {

    // get current node element
    Element nodeElement = parent.element(nodeName);
    if (value != null) {
        if (nodeElement == null) {
            // element wasn't there before, add element and set value
            nodeElement = parent.addElement(nodeName);
        }
        // element is there, update element value
        nodeElement.clearContent();
        if (cdata) {
            nodeElement.addCDATA(value);
        } else {
            nodeElement.addText(value);
        }
    } else {
        // remove only if element exists
        if (nodeElement != null) {
            // remove element
            parent.remove(nodeElement);
        }
    }
}

From source file:org.opencms.xml.A_CmsXmlDocument.java

License:Open Source License

/**
 * Creates a partial deep element copy according to the set of element paths.<p>
 * Only elements contained in that set will be copied.
 * /* w  w w.j  av a 2  s .com*/
 * @param parentPath the path of the parent element or <code>null</code>, initially
 * @param parent the parent element
 * @param element the element to copy
 * @param copyElements the set of paths for elements to copy
 * 
 * @return a partial deep copy of <code>element</code>
 */
private Element createDeepElementCopyInternal(String parentPath, Element parent, Element element,
        Set<String> copyElements) {

    String elName = element.getName();
    if (parentPath != null) {
        Element first = element.getParent().element(elName);
        int elIndex = (element.getParent().indexOf(element) - first.getParent().indexOf(first)) + 1;
        elName = parentPath + (parentPath.length() > 0 ? "/" : "") + elName.concat("[" + elIndex + "]");
    }

    if ((parentPath == null) || copyElements.contains(elName)) {
        // this is a content element we want to copy
        Element copy = element.createCopy();
        // copy.detach();
        if (parentPath != null) {
            parent.add(copy);
        }

        // check if we need to copy subelements, too
        boolean copyNested = (parentPath == null);
        for (Iterator<String> i = copyElements.iterator(); !copyNested && i.hasNext();) {
            String path = i.next();
            copyNested = !elName.equals(path) && path.startsWith(elName);
        }

        if (copyNested) {
            copy.clearContent();
            for (Iterator<Element> i = CmsXmlGenericWrapper.elementIterator(element); i.hasNext();) {
                Element el = i.next();
                createDeepElementCopyInternal((parentPath == null) ? "" : elName, copy, el, copyElements);
            }
        }

        return copy;
    } else {
        return null;
    }
}

From source file:org.opencms.xml.containerpage.CmsXmlContainerPage.java

License:Open Source License

/**
 * Adds the given container page to the given element.<p>
 * //  w w  w  .j  ava2  s  .  c  om
 * @param cms the current CMS object
 * @param parent the element to add it
 * @param cntPage the container page to add
 * 
 * @throws CmsException if something goes wrong
 */
protected void saveContainerPage(CmsObject cms, Element parent, CmsContainerPageBean cntPage)
        throws CmsException {

    parent.clearContent();

    for (String containerName : cntPage.getNames()) {
        CmsContainerBean container = cntPage.getContainers().get(containerName);

        // the container
        Element cntElement = parent.addElement(XmlNode.Containers.name());
        cntElement.addElement(XmlNode.Name.name()).addCDATA(container.getName());
        cntElement.addElement(XmlNode.Type.name()).addCDATA(container.getType());

        //            for (Map.Entry<String, String> entry : container.getAttributes().entrySet()) {
        //                Element attrElement = cntElement.addElement(XmlNode.Attribute.name());
        //                attrElement.addElement(XmlNode.Key.name()).addCDATA(entry.getKey());
        //                attrElement.addElement(XmlNode.Value.name()).addCDATA(entry.getValue());
        //            }

        // the elements
        for (CmsContainerElementBean element : container.getElements()) {
            Element elemElement = cntElement.addElement(XmlNode.Elements.name());

            // the element
            Element uriElem = elemElement.addElement(XmlNode.Uri.name());
            CmsResource uriRes = fillResource(cms, uriElem, element.getId());
            Element formatterElem = elemElement.addElement(XmlNode.Formatter.name());
            fillResource(cms, formatterElem, element.getFormatterId());

            // the properties
            Map<String, String> properties = element.getIndividualSettings();
            Map<String, CmsXmlContentProperty> propertiesConf = OpenCms.getADEManager().getElementSettings(cms,
                    uriRes);

            CmsXmlContentPropertyHelper.saveProperties(cms, elemElement, properties, uriRes, propertiesConf);
        }
    }
}

From source file:org.opencms.xml.containerpage.CmsXmlGroupContainer.java

License:Open Source License

/**
 * Adds the given container page to the given element.<p>
 * /*ww w  .j  a  v a 2 s . c o  m*/
 * @param cms the current CMS object
 * @param parent the element to add it
 * @param groupContainer the container page to add
 * 
 * @throws CmsException if something goes wrong
 */
protected void saveGroupContainer(CmsObject cms, Element parent, CmsGroupContainerBean groupContainer)
        throws CmsException {

    parent.clearContent();
    Element groupContainerElem = parent.addElement(XmlNode.GroupContainers.name());

    groupContainerElem.addElement(XmlNode.Title.name()).addCDATA(groupContainer.getTitle());
    groupContainerElem.addElement(XmlNode.Description.name()).addCDATA(groupContainer.getDescription());

    for (String type : groupContainer.getTypes()) {
        groupContainerElem.addElement(XmlNode.Type.name()).addCDATA(type);
    }

    // the elements
    for (CmsContainerElementBean element : groupContainer.getElements()) {
        Element elemElement = groupContainerElem.addElement(XmlNode.Element.name());

        // the element
        Element uriElem = elemElement.addElement(XmlNode.Uri.name());
        CmsResource uriRes = fillResource(cms, uriElem, element.getId());

        // the properties
        Map<String, String> properties = element.getIndividualSettings();
        Map<String, CmsXmlContentProperty> propertiesConf = OpenCms.getADEManager().getElementSettings(cms,
                uriRes);

        CmsXmlContentPropertyHelper.saveProperties(cms, elemElement, properties, uriRes, propertiesConf);
    }
}

From source file:org.opencms.xml.types.A_CmsXmlContentValue.java

License:Open Source License

/**
 * @see org.opencms.xml.types.I_CmsXmlSchemaType#generateXml(org.opencms.file.CmsObject, org.opencms.xml.I_CmsXmlDocument, org.dom4j.Element, java.util.Locale)
 *//*from   w ww . j a  va  2 s.  com*/
public Element generateXml(CmsObject cms, I_CmsXmlDocument document, Element root, Locale locale) {

    Element element = root.addElement(getName());
    // get the default value from the content handler
    String defaultValue = document.getHandler().getDefault(cms, this, locale);
    if (defaultValue != null) {
        try {
            I_CmsXmlContentValue value = createValue(document, element, locale);
            value.setStringValue(cms, defaultValue);
        } catch (CmsRuntimeException e) {
            // should not happen if default value is correct
            LOG.error(Messages.get().getBundle().key(Messages.ERR_XMLCONTENT_INVALID_ELEM_DEFAULT_1,
                    defaultValue), e);
            element.clearContent();
        }
    }
    return element;
}

From source file:org.opencms.xml.types.CmsXmlHtmlValue.java

License:Open Source License

/**
 * @see org.opencms.xml.types.I_CmsXmlSchemaType#generateXml(org.opencms.file.CmsObject, org.opencms.xml.I_CmsXmlDocument, org.dom4j.Element, java.util.Locale)
 *//*from  www. ja v a2  s  .  c o m*/
@Override
public Element generateXml(CmsObject cms, I_CmsXmlDocument document, Element root, Locale locale) {

    Element element = root.addElement(getName());
    int index = element.getParent().elements(element.getQName()).indexOf(element);
    element.addAttribute(CmsXmlPage.ATTRIBUTE_NAME, getName() + index);
    element.addElement(CmsXmlPage.NODE_LINKS);
    element.addElement(CmsXmlPage.NODE_CONTENT);

    // get the default value from the content handler
    String defaultValue = document.getHandler().getDefault(cms, this, locale);
    if (defaultValue != null) {
        try {
            I_CmsXmlContentValue value = createValue(document, element, locale);
            value.setStringValue(cms, defaultValue);
        } catch (CmsRuntimeException e) {
            // should not happen if default value is correct
            LOG.error(Messages.get().getBundle().key(Messages.ERR_XMLCONTENT_INVALID_ELEM_DEFAULT_1,
                    defaultValue), e);
            element.clearContent();
        }
    }
    return element;
}

From source file:org.opencms.xml.types.CmsXmlHtmlValue.java

License:Open Source License

/**
 * @see org.opencms.xml.types.I_CmsXmlContentValue#setStringValue(org.opencms.file.CmsObject, java.lang.String)
 *//*  w  w w .j a  v  a2  s .c  om*/
public void setStringValue(CmsObject cms, String value) {

    Element content = m_element.element(CmsXmlPage.NODE_CONTENT);
    Element links = m_element.element(CmsXmlPage.NODE_LINKS);
    CmsLinkProcessor linkProcessor = null;

    String encoding = m_document.getEncoding();
    linkProcessor = m_document.getLinkProcessor(cms, new CmsLinkTable());

    String finalValue = value;
    if (finalValue != null) {
        // nested CDATA tags are not allowed, so replace CDATA tags with their contents
        finalValue = finalValue.replaceAll("(?s)// <!\\[CDATA\\[(.*?)// \\]\\]>", "$1"); // special case for embedded Javascript 
        finalValue = finalValue.replaceAll("(?s)<!\\[CDATA\\[(.*?)\\]\\]>", "$1");
    }
    if (encoding != null) {
        // ensure all chars in the given content are valid chars for the selected charset
        finalValue = CmsEncoder.adjustHtmlEncoding(finalValue, encoding);
    }

    // remove unnecessary tags if required
    String contentConversion = m_document.getConversion();
    if (CmsHtmlConverter.isConversionEnabled(contentConversion)) {
        CmsHtmlConverter converter = new CmsHtmlConverter(encoding, contentConversion);
        finalValue = converter.convertToStringSilent(finalValue);
    }
    if (linkProcessor != null) {
        try {
            // replace links in HTML by macros and fill link table      
            finalValue = linkProcessor.replaceLinks(finalValue);
        } catch (Exception exc) {
            throw new CmsRuntimeException(Messages.get().container(Messages.ERR_HTML_DATA_PROCESSING_0), exc);
        }
    }

    content.clearContent();
    links.clearContent();

    if (CmsStringUtil.isNotEmptyOrWhitespaceOnly(finalValue)) {
        content.addCDATA(finalValue);
        if (linkProcessor != null) {
            // may be null in case of default value generation (i.e. setStringValue(String) was called)

            CmsLinkTable linkTable = linkProcessor.getLinkTable();
            for (Iterator<CmsLink> i = linkTable.iterator(); i.hasNext();) {
                CmsLink link = i.next();
                CmsLinkUpdateUtil.updateXmlForHtmlValue(link, link.getName(),
                        links.addElement(CmsXmlPage.NODE_LINK));
            }
        }
    }

    // ensure the String value is re-calculated next time
    m_stringValue = null;
}

From source file:org.openxml4j.opc.signature.RelationshipTransform.java

License:Apache License

@SuppressWarnings("unchecked")
public static void RemoveContentsFromRelationshipElements(Document doc) {
    List<Element> rels = (List<Element>) doc.getRootElement().elements();

    for (Element el : rels) {
        el.clearContent();
    }//from ww  w . j  a v a  2s. c  o m
}