Example usage for org.jdom2 Element setContent

List of usage examples for org.jdom2 Element setContent

Introduction

In this page you can find the example usage for org.jdom2 Element setContent.

Prototype

public Element setContent(final Content child) 

Source Link

Document

Set this element's content to be the supplied child.

Usage

From source file:password.pwm.config.StoredConfiguration.java

License:Open Source License

public void writeConfigProperty(final ConfigProperty propertyName, final String value) {
    domModifyLock.writeLock().lock();//from  ww w.  j a  v  a 2 s .  c om
    try {

        final XPathExpression xp = XPathBuilder.xpathForConfigProperty(propertyName);
        final List<Element> propertyElements = xp.evaluate(document);
        for (final Element propertyElement : propertyElements) {
            propertyElement.detach();
        }

        final Element propertyElement = new Element(XML_ELEMENT_PROPERTY);
        propertyElement.setAttribute(new Attribute(XML_ATTRIBUTE_KEY, propertyName.getKey()));
        propertyElement.setContent(new Text(value));

        if (null == XPathBuilder.xpathForConfigProperties().evaluateFirst(document)) {
            Element configProperties = new Element(XML_ELEMENT_PROPERTIES);
            configProperties.setAttribute(new Attribute(XML_ATTRIBUTE_TYPE, XML_ATTRIBUTE_VALUE_CONFIG));
            document.getRootElement().addContent(configProperties);
        }

        final XPathExpression xp2 = XPathBuilder.xpathForConfigProperties();
        final Element propertiesElement = (Element) xp2.evaluateFirst(document);
        propertyElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME,
                PwmConstants.DEFAULT_DATETIME_FORMAT.format(new Date()));
        propertiesElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME,
                PwmConstants.DEFAULT_DATETIME_FORMAT.format(new Date()));
        propertiesElement.addContent(propertyElement);
    } finally {
        domModifyLock.writeLock().unlock();
    }
}

From source file:password.pwm.config.StoredConfiguration.java

License:Open Source License

public void writeLocaleBundleMap(final String bundleName, final String keyName,
        final Map<String, String> localeMap) {
    ResourceBundle theBundle = null;
    for (final PwmLocaleBundle bundle : PwmLocaleBundle.values()) {
        if (bundle.getTheClass().getName().equals(bundleName)) {
            theBundle = ResourceBundle.getBundle(bundleName);
        }// w  ww.jav  a  2 s. c o  m
    }

    if (theBundle == null) {
        LOGGER.info("ignoring unknown locale bundle for bundle=" + bundleName + ", key=" + keyName);
        return;
    }

    if (theBundle.getString(keyName) == null) {
        LOGGER.info("ignoring unknown key for bundle=" + bundleName + ", key=" + keyName);
        return;
    }

    resetLocaleBundleMap(bundleName, keyName);
    if (localeMap == null || localeMap.isEmpty()) {
        LOGGER.info("cleared locale bundle map for bundle=" + bundleName + ", key=" + keyName);
        return;
    }

    preModifyActions();
    changeLog.updateChangeLog(bundleName, keyName, localeMap);
    try {
        domModifyLock.writeLock().lock();
        final Element localeBundleElement = new Element("localeBundle");
        localeBundleElement.setAttribute("bundle", bundleName);
        localeBundleElement.setAttribute("key", keyName);
        for (final String locale : localeMap.keySet()) {
            final Element valueElement = new Element("value");
            if (locale != null && locale.length() > 0) {
                valueElement.setAttribute("locale", locale);
            }
            valueElement.setContent(new CDATA(localeMap.get(locale)));
            localeBundleElement.addContent(valueElement);
        }
        localeBundleElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME,
                PwmConstants.DEFAULT_DATETIME_FORMAT.format(new Date()));
        document.getRootElement().addContent(localeBundleElement);
    } finally {
        domModifyLock.writeLock().unlock();
    }
}