Example usage for org.jdom2 Element setText

List of usage examples for org.jdom2 Element setText

Introduction

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

Prototype

public Element setText(final String text) 

Source Link

Document

Sets the content of the element to be the text given.

Usage

From source file:org.culturegraph.mf.jdom.StreamToJDomDocument.java

License:Apache License

@Override
public void literal(final String name, final String value) {
    assert !isClosed();
    if (name.isEmpty()) {
        currentElement.addContent(value);
    } else if (name.startsWith(ATTRIBUTE_MARKER)) {
        final String[] parts = NAMESPACE_DELIMITER.split(name);
        if (parts.length == 2) {
            currentElement.setAttribute(parts[1], value, getNamespace(parts[0].substring(1)));
        } else {/*from  w ww  . j  a  v  a2s  .c om*/
            currentElement.setAttribute(name.substring(1), value);
        }

    } else {
        final Element temp = createElement(name);
        currentElement.addContent(temp);
        temp.setText(value);
    }
}

From source file:org.dvlyyon.net.netconf.Capabilities.java

License:Open Source License

/**
 * Adds the specified capability to the specified parent, depending upon whether it exists or not.
 *
 * @param parent        Element representing parent capabilities node.
 * @param isCapable     True if the capability exists, false if not.
 * @param capURI        String representing the capability URI.
 *///from  ww  w . j a  v a  2  s  .  c  o m
private void addCapability(final Element parent, final boolean isCapable, final String capURI) {
    if (isCapable) {
        Element cap = new Element("capability", s_xmlns);
        cap.setText(capURI);
        parent.addContent(cap);
    }
}

From source file:org.dvlyyon.net.netconf.Client.java

License:Open Source License

@Override
public void editConfig(final String configurationName, final Element xml,
        final EditOperation defaultEditOperation, final TestOption testOption, final ErrorOption errorOption,
        final String xid) throws RuntimeException {
    String store = configurationName == null ? "running" : configurationName;
    if (store.equals("running") && !m_deviceCaps.supportsWritableRunningConfig()) {
        throw new RuntimeException("Target device does not support the writable-running capability");
    }//from w  w w. j a v a  2s .  c  om
    if (store.equals("candidate") && !m_deviceCaps.supportsCandidateConfig()) {
        throw new RuntimeException("Target device does not support the candidate-config capability");
    }
    Element editConfig = new Element("edit-config", s_xmlns);
    Element target = new Element("target", s_xmlns);
    editConfig.addContent(target);
    if (defaultEditOperation != null) {
        Element editOperationE = new Element("default-operation", s_xmlns);
        switch (defaultEditOperation) {
        case None:
            editOperationE.setText("none");
            break;
        case Merge:
            editOperationE.setText("merge");
            break;
        case Replace:
            editOperationE.setText("replace");
            break;
        default:
            break;
        }
        if (editOperationE.getText() != null && !editOperationE.getText().equals("")) {
            editConfig.addContent(editOperationE);
        }
    }
    if (testOption != null) {
        if (testOption != TestOption.None) {
            if (!m_deviceCaps.supportsValidation()) {
                throw new RuntimeException("Device does not support validation capability");
            }
            Element testOptionE = new Element("test-option", s_xmlns);
            switch (testOption) {
            case TestThenSet:
                testOptionE.setText("test-then-set");
                break;
            case Set:
                testOptionE.setText("set");
                break;
            case TestOnly:
                testOptionE.setText("test-only");
                break;
            default:
                break;
            }
            editConfig.addContent(testOptionE);
        }
    }
    if (errorOption != null) {
        if (errorOption != ErrorOption.None) {
            Element errorOptionE = new Element("error-option", s_xmlns);
            if (errorOption == ErrorOption.RollbackOnError && !m_deviceCaps.supportsRollbackOnError()) {
                throw new RuntimeException("Target device does not support the rollback-on-error capability");
            }
            switch (errorOption) {
            case StopOnError:
                errorOptionE.setText("stop-on-error");
                break;
            case ContinueOnError:
                errorOptionE.setText("continue-on-error");
                break;
            case RollbackOnError:
                errorOptionE.setText("rollback-on-error");
                break;
            default:
                break;
            }
            editConfig.addContent(errorOptionE);
        }
    }
    Element config = new Element(store, s_xmlns);
    target.addContent(config);
    Element cfgElem = new Element("config", s_xmlns);
    editConfig.addContent(cfgElem);
    cfgElem.addContent(xml);
    send(editConfig, xid);
}

From source file:org.dvlyyon.net.netconf.Client.java

License:Open Source License

@Override
public void copyConfig(final String sourceConfigurationName, final String targetConfigurationName,
        final String xid) throws RuntimeException {
    if (targetConfigurationName.equals("running") && !m_deviceCaps.supportsWritableRunningConfig()) {
        throw new RuntimeException("Target device does not support the writable-running capability");
    }/*  w w  w.j av  a2 s.  c om*/
    if (targetConfigurationName.equals("candidate") && !m_deviceCaps.supportsCandidateConfig()) {
        throw new RuntimeException("Target device does not support the candidate-config capability");
    }
    if ((sourceConfigurationName.equals("startup") || targetConfigurationName.equals("startup"))
            && !m_deviceCaps.supportsDistinctStartupConfig()) {
        throw new RuntimeException("Target device does not support the distinct startup config capability");
    }
    Element copyConfig = new Element("copy-config", s_xmlns);
    Element target = new Element("target", s_xmlns);
    target.setText(targetConfigurationName);
    copyConfig.addContent(target);
    Element source = new Element("source", s_xmlns);
    source.setText(sourceConfigurationName);
    copyConfig.addContent(source);
    send(copyConfig, xid);
}

From source file:org.dvlyyon.net.netconf.Client.java

License:Open Source License

@Override
public void deleteConfig(final String configurationName, final String xid) throws RuntimeException {
    if (configurationName.equals("candidate") && !m_deviceCaps.supportsCandidateConfig()) {
        throw new RuntimeException("Target device does not support the candidate-config capability");
    }/*w  w w  .j av  a  2s . co  m*/
    if (configurationName.equals("startup") && !m_deviceCaps.supportsDistinctStartupConfig()) {
        throw new RuntimeException("Target device does not support the distinct startup config capability");
    }
    Element deleteConfig = new Element("delete-config", s_xmlns);
    Element target = new Element("target", s_xmlns);
    target.setText(configurationName);
    deleteConfig.addContent(target);
    send(deleteConfig, xid);
}

From source file:org.dvlyyon.net.netconf.Client.java

License:Open Source License

@Override
public void validate(final String configurationName, final String xid) throws RuntimeException {
    if (!m_deviceCaps.supportsValidation()) {
        throw new RuntimeException("Target device does not support validation capability");
    }//from   w w  w  . j  a  v  a2s . com
    if (configurationName.equals("candidate") && !m_deviceCaps.supportsCandidateConfig()) {
        throw new RuntimeException("Target device does not support the candidate-config capability");
    }
    if (configurationName.equals("startup") && !m_deviceCaps.supportsDistinctStartupConfig()) {
        throw new RuntimeException("Target device does not support the distinct startup config capability");
    }
    Element validate = new Element("validate", s_xmlns);
    Element source = new Element("source", s_xmlns);
    source.setText(configurationName);
    validate.addContent(source);
    send(validate, xid);
}

From source file:org.dvlyyon.net.netconf.Client.java

License:Open Source License

@Override
public void commit(final String persistId, final boolean confirm, int timeoutInSeconds, final String xid)
        throws RuntimeException {
    Element commit = new Element("commit", s_xmlns);
    if (!m_deviceCaps.supportsCandidateConfig()) {
        throw new RuntimeException("Device does not support candidate configuration");
    }// ww w  .j  av a 2  s  .c o  m
    if (persistId != null && !persistId.equals("")) {
        if (!m_deviceCaps.supportsPersistId()) {
            throw new RuntimeException("Device does not support persist ID for commit");
        } else {
            Element persist = new Element("persist", s_xmlns);
            persist.setText(persistId);
            commit.addContent(persist);
        }
    }
    if (confirm) {
        if (!m_deviceCaps.supportsConfirmedCommit()) {
            throw new RuntimeException("Device does not support confirmed commit operation");
        }
        Element confirmed = new Element("confirmed", s_xmlns);
        commit.addContent(confirmed);
        Element timeout = new Element("confirm-timeout", s_xmlns);
        timeout.setText("" + timeoutInSeconds);
        commit.addContent(timeout);
    }
    send(commit, xid);
}

From source file:org.dvlyyon.net.netconf.Client.java

License:Open Source License

@Override
public void cancelCommit(final String persistId, final String xid) throws RuntimeException {
    Element cancelCommit = new Element("cancel-commit", s_xmlns);
    if (!m_deviceCaps.supportsCandidateConfig()) {
        throw new RuntimeException("Device does not support candidate configuration");
    }/*from   ww w .  java2  s .  c om*/
    if (!m_deviceCaps.supportsConfirmedCommit()) {
        throw new RuntimeException("Device does not support confirmed commit operation");
    }
    if (persistId != null && !persistId.equals("")) {
        if (!m_deviceCaps.supportsPersistId()) {
            throw new RuntimeException("Device does not support persist ID for commit");
        } else {
            Element persist = new Element("persist-id", s_xmlns);
            persist.setText(persistId);
            cancelCommit.addContent(persist);
        }
    }
    send(cancelCommit, xid);
}

From source file:org.dvlyyon.net.netconf.exception.NetconfException.java

License:Open Source License

/**
 * Converts the NetconfException to its XML version.
 *
 * @return  XML-ized version of the exception, similar to what you would see on the wire.
 *///w  ww.j  a  va  2s  .  c  o  m
public Element toXml() {
    final Element root = new Element("rpc-error", s_xmlns);
    final Element type = new Element("error-type", s_xmlns);
    type.setText(m_type.toString());
    root.addContent(type);
    final Element tag = new Element("error-tag", s_xmlns);
    type.setText(m_tag.getNetconfTag());
    root.addContent(tag);
    final Element severity = new Element("error-severity", s_xmlns);
    type.setText(m_severity.toString());
    root.addContent(severity);
    final Element message = new Element("error-message", s_xmlns);
    type.setText(m_message);
    root.addContent(message);
    if (m_path != null) {
        final Element path = new Element("error-path", s_xmlns);
        path.addContent(m_path);
        root.addContent(path);
    }
    if (m_info != null) {
        try {
            // Note: the info tag may have a different namespace; we lose it in the transformation
            Element info = XmlUtils.fromXmlString(m_info);
            info.setNamespace(s_xmlns);
            root.addContent(info);
        } catch (final Exception ex) {
            s_logger.error("Error converting NetconfException to XML");
            if (s_logger.isDebugEnabled()) {
                s_logger.error(ex, ex);
            }
        }
    }
    return root;
}

From source file:org.dvlyyon.net.netconf.marshalling.Filter.java

License:Open Source License

/**
 * Given a string representing a path end-point, and the class that we are interested in, converts the attributes specified in the
 * end-point as XML entities used in a Filter. For example, if you have a path like 'myClass[key1="val1"][key2="val2"]', an XML
 * node is created for each key and added as a child to the root node called 'myClass'.
 *
 * @param pathToken  Path end-point (this is a path-segment). 
 * @param theClass   Class mapping representing the class we are dealing with.
 * @return           XML node containing Filter XML equivalent of the specified path end-point.
 *///w ww  . j av a2s.  c o m
static Element processToken(final String pathToken, final ClassMapping theClass)
//static Element processToken(final PathSegment pathSegment, final ClassMapping theClass)
{
    // in the form elementTag [attribute1="value1"][attribute2="value2"]
    Element ret = null;
    //final String[] tokens = StringUtils.tokenizeToStringArray(pathSegment.getTag(), " []");
    // ** mike ** final String[] tokens = StringUtils.tokenizeToStringArray(pathToken, " []");
    final String[] tokens = StringUtils.tokenizeToStringArrayQuotes(pathToken, "[]");
    int index = 0;
    for (String token : tokens) {
        Namespace ns = Namespace.getNamespace(theClass.getXmlNamespace());
        //Namespace ns = Namespace.getNamespace(pathSegment.getNamespaceUri());
        if (index == 0) {
            ret = new Element(token, ns);
        } else {
            // This one is name="value"
            String name = token.substring(0, token.indexOf('=')).trim();
            // Actually we should look for the "" (and not assume lack of spaces)
            String value = token.substring(token.indexOf('\"') + 1, token.lastIndexOf('\"'));
            // TODO: Deal with the possibility that the KEY ATTRIBUTE has a different namespace - a bit paranoid here
            Element attrib = new Element(name, ns);
            attrib.setText(value);
            ret.addContent(attrib);
        }
        index++;
    }
    return ret;
}