Example usage for org.jdom2 Text Text

List of usage examples for org.jdom2 Text Text

Introduction

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

Prototype

public Text(String str) 

Source Link

Document

This constructor creates a new Text node, with the supplied string value as its character content.

Usage

From source file:org.plasma.xml.uml.DefaultUMLModelAssembler.java

License:Open Source License

/**
 * Adds a UML owned opaque behavior ala. below for each provisioning
 * behavior.// w  ww. j  a  va  2s . co  m
 * 
 * <ownedBehavior xmi:type='uml:OpaqueBehavior' xmi:id='_17_0_6_1707042b_1376433440849_3356_7405' name='create' visibility='public'>
 *   <body>FOO BAR;</body>
 *    <language>SQL</language>
 * </ownedBehavior>
 * 
 * @param clss the 
 * @param clssElem the element
 * @param behaviors the bahaviors
 */
private void addBehaviors(Class clss, Element clssElem, List<Behavior> behaviors) {

    for (Behavior behavior : behaviors) {
        Element ownedBehavior = new Element("ownedBehavior");
        clssElem.addContent(ownedBehavior);
        ownedBehavior.setAttribute(new Attribute("type", "uml:OpaqueBehavior", xmiNs));
        ownedBehavior.setAttribute(new Attribute("id", UUID.randomUUID().toString(), xmiNs));
        ownedBehavior.setAttribute(new Attribute("visibility", "public"));
        ownedBehavior.setAttribute(new Attribute("name", behavior.getType().name()));

        Element language = new Element("language");
        language.setText(behavior.getLanguage());
        ownedBehavior.addContent(language);

        Element body = new Element("body");
        Text text = new Text(behavior.getValue());
        body.addContent(text);
        ownedBehavior.addContent(body);

    }

}

From source file:org.polago.deployconf.task.AbstractTask.java

License:Open Source License

/**
 * Create a JDOM Text Element with the given name and possibly text.
 *
 * @param name the element name/* w  w w. jav a2 s  . c o m*/
 * @param text the element text or null
 * @return a JDOM Element instance
 */
protected Element createJDOMTextElement(String name, String text) {
    Element result = new Element(name);
    if (text != null) {
        result.setContent(new Text(text));
    }

    return result;
}

From source file:org.rascalmpl.library.lang.xml.DOM.java

License:Open Source License

private Content nodeToContent(IConstructor n) {
    if (n.getConstructorType() == Factory.Node_element) {
        return nodeToElement(n);
    }/*ww  w  .j av  a 2 s . co m*/
    if (n.getConstructorType() == Factory.Node_pi) {
        IString target = (IString) n.get(0);
        IString data = (IString) n.get(1);
        return new ProcessingInstruction(target.getValue(), data.getValue());

    }
    if (n.getConstructorType() == Factory.Node_charRef) {
        IInteger code = (IInteger) n.get(0);
        int c = java.lang.Integer.parseInt(code.getStringRepresentation());
        return new Text(new java.lang.String(Character.toChars(c)));
    }
    if (n.getConstructorType() == Factory.Node_entityRef) {
        return new EntityRef(((IString) n.get(0)).getValue());
    }

    java.lang.String text = ((IString) n.get(0)).getValue();
    if (n.getConstructorType() == Factory.Node_cdata) {
        return new CDATA(text);
    }
    if (n.getConstructorType() == Factory.Node_charData) {
        return new Text(text);
    }
    if (n.getConstructorType() == Factory.Node_comment) {
        return new Comment(text);
    }

    wellformednessError();
    return null;
}

From source file:org.xflatdb.xflat.query.XPathUpdate.java

License:Apache License

/**
 * Applies the update operations to the given DOM Element representing
 * the data in a selected row.//from   www. ja v  a 2s.c  o  m
 * @param rowData The DOM Element representing the data in a selected row.
 * @return true if any updates were applied.
 */
public int apply(Element rowData) {
    int updateCount = 0;

    for (Update update : this.updates) {
        //the update's value will be one or the other, don't know which
        Content asContent = null;
        String asString = null;

        if (update.value instanceof String) {
            asString = (String) update.value;
        }

        if (update.value instanceof Content) {
            asContent = (Content) update.value;
        }

        for (Object node : update.path.evaluate(rowData)) {
            if (node == null)
                continue;

            Parent parent;
            Element parentElement;

            if (update.getUpdateType() == UpdateType.UNSET) {
                if (node instanceof Attribute) {
                    parentElement = ((Attribute) node).getParent();
                    if (parentElement != null) {
                        parentElement.removeAttribute((Attribute) node);
                        updateCount++;
                    }
                } else if (node instanceof Content) {
                    parent = ((Content) node).getParent();
                    //remove this node from its parent element
                    if (parent != null) {
                        parent.removeContent((Content) node);
                        updateCount++;
                    }
                }

                continue;
            }

            //it's a set

            if (node instanceof Attribute) {
                //for attributes we set the value to empty string
                //this way it can still be selected by xpath for future updates
                if (update.value == null) {
                    ((Attribute) node).setValue("");
                    updateCount++;
                } else {
                    if (asString == null) {
                        asString = getStringValue(update.value);
                    }

                    //if we fail conversion then do nothing.
                    if (asString != null) {
                        ((Attribute) node).setValue(asString);
                        updateCount++;
                    }
                }

                continue;
            } else if (!(node instanceof Content)) {
                //can't do anything
                continue;
            }

            Content contentNode = (Content) node;

            //need to convert
            if (update.value != null && asContent == null) {
                asContent = getContentValue(update.value);
                if (asContent == null) {
                    //failed conversion, try text
                    asString = getStringValue(update.value);
                    if (asString != null) {
                        //success!
                        asContent = new Text(asString);
                    }
                }
            }

            if (node instanceof Element) {
                //for elements we also set the value, but the value could be Content
                if (update.value == null) {
                    ((Element) node).removeContent();
                    updateCount++;
                } else if (asContent != null) {
                    if (asContent.getParent() != null) {
                        //we used the content before, need to clone it
                        asContent = asContent.clone();
                    }

                    ((Element) node).setContent(asContent);
                    updateCount++;
                }
                continue;
            }

            //at this point the node is Text, CDATA or something else.
            //The strategy now is to replace the value in its parent.
            parentElement = contentNode.getParentElement();
            if (parentElement == null) {
                //can't do anything
                continue;
            }

            if (update.value == null || asContent != null) {
                //replace this content in the parent element
                int index = parentElement.indexOf(contentNode);
                parentElement.removeContent(index);
                if (update.value != null) {
                    //if it was null then act like an unset, otherwise
                    //its a replace

                    if (asContent.getParent() != null) {
                        //we used the content before, need to clone it
                        asContent = asContent.clone();
                    }

                    parentElement.addContent(index, asContent);
                }
                updateCount++;
            }
        }
    }

    return updateCount;
}

From source file:password.pwm.config.stored.StoredConfigurationImpl.java

License:Open Source License

@Override
public void writeConfigProperty(final ConfigurationProperty propertyName, final String value) {
    domModifyLock.writeLock().lock();//from w w  w  .  j a  v a 2 s .  co  m
    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)) {
            final 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, JavaHelper.toIsoDate(Instant.now()));
        propertiesElement.setAttribute(XML_ATTRIBUTE_MODIFY_TIME, JavaHelper.toIsoDate(Instant.now()));
        propertiesElement.addContent(propertyElement);
    } finally {
        domModifyLock.writeLock().unlock();
    }
}

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

License:Open Source License

public void writeConfigProperty(final ConfigProperty propertyName, final String value) {
    domModifyLock.writeLock().lock();/*  www .  java2s  .c  o  m*/
    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();
    }
}