Search in sources :

Example 11 with XMLException

use of com.axway.ats.common.xml.XMLException in project ats-framework by Axway.

the class XmlText method setText.

/**
     * Change the text of a XML element.
     *
     * @param xpath XPath , pointing to a XML element
     * @param text the new text for a XML element
     * @return this instance
     * @throws XMLException
     */
@PublicAtsApi
public XmlText setText(String xpath, String text) throws XMLException {
    if (StringUtils.isNullOrEmpty(xpath)) {
        throw new XMLException("Null/empty xpath is not allowed.");
    }
    if (StringUtils.isNullOrEmpty(text)) {
        throw new XMLException("Null/empty text is not allowed.");
    }
    Element element = findElement(xpath);
    if (element == null) {
        throw new XMLException("'" + xpath + "' is not a valid path");
    }
    element.setText(text);
    return this;
}
Also used : XMLException(com.axway.ats.common.xml.XMLException) Element(org.dom4j.Element) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 12 with XMLException

use of com.axway.ats.common.xml.XMLException in project ats-framework by Axway.

the class XmlText method getBoolean.

/**
     * @param xpath XPath , pointing to a XML element
     * @return a boolean XML value
     * @throws XMLException
     */
@PublicAtsApi
public boolean getBoolean(String xpath) throws XMLException {
    Object object = get(xpath);
    Element root = ((XmlText) object).root;
    if (root.isTextOnly()) {
        object = root.getText().trim();
    } else {
        object = "";
    }
    if ("true".equalsIgnoreCase((String) object) || "false".equalsIgnoreCase((String) object)) {
        return Boolean.parseBoolean(((String) object).trim());
    } else {
        throw new XMLException("'" + xpath + "' does not point to an boolean value:\n" + object.toString());
    }
}
Also used : XMLException(com.axway.ats.common.xml.XMLException) Element(org.dom4j.Element) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 13 with XMLException

use of com.axway.ats.common.xml.XMLException in project ats-framework by Axway.

the class XmlText method add.

/**
     * Add XML element from XMLText or String
     *
     * @param xpath XPath , pointing to a XML element
     * @param object the XML element
     * @return this instance
     * @throws XMLException
     */
@PublicAtsApi
public XmlText add(String xpath, Object object) throws XMLException {
    if (StringUtils.isNullOrEmpty(xpath)) {
        throw new XMLException("Null/empty xpath is not allowed.");
    }
    if (object == null) {
        throw new XMLException("Null object is not allowed.");
    }
    Element newElement = null;
    Element parent = findElement(xpath);
    if (parent == null) {
        throw new XMLException("'" + xpath + "' is not a valid path");
    }
    if (object instanceof XmlText) {
        newElement = ((XmlText) object).root;
    }
    if (object instanceof String) {
        newElement = new XmlText((String) object).root;
    }
    if (newElement == null) {
        throw new XMLException("Given object for adding to xml document is from invallid class instance '" + object.getClass().getSimpleName() + "'. " + "Use String or XMLText instances only.");
    }
    parent.add(newElement);
    return this;
}
Also used : XMLException(com.axway.ats.common.xml.XMLException) Element(org.dom4j.Element) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 14 with XMLException

use of com.axway.ats.common.xml.XMLException in project ats-framework by Axway.

the class XmlText method getAttribute.

/**
     * @param xpath XPath , pointing to a XML element
     * @param name the name of the attribute
     * @return the value of the attribute
     * @throws XMLException
     */
@PublicAtsApi
public String getAttribute(String xpath, String name) throws XMLException {
    if (StringUtils.isNullOrEmpty(xpath)) {
        throw new XMLException("Null/empty xpath is not allowed.");
    }
    if (StringUtils.isNullOrEmpty(name)) {
        throw new XMLException("Null/empty attribute name is not allowed.");
    }
    Element element = findElement(xpath);
    if (element == null) {
        throw new XMLException("'" + xpath + "' is not a valid path");
    }
    String attributeValue = element.attributeValue(name);
    if (attributeValue == null) {
        throw new XMLException("'" + name + "' attribute is not found for XML element with xpath '" + xpath + "'.");
    }
    return attributeValue;
}
Also used : XMLException(com.axway.ats.common.xml.XMLException) Element(org.dom4j.Element) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 15 with XMLException

use of com.axway.ats.common.xml.XMLException in project ats-framework by Axway.

the class XmlText method getElementNames.

/**
     * Returns the child names of a XML element
     *
     * @return the XPath of the XML element.
     * @return the child names of a XML element
     * @throws XMLException
     */
@PublicAtsApi
public String[] getElementNames(String xpath) throws XMLException {
    ArrayList<String> elementNames = new ArrayList<>(1);
    if (StringUtils.isNullOrEmpty(xpath)) {
        throw new XMLException("Null/empty xpath is not allowed.");
    }
    Element element = findElement(xpath);
    if (element == null) {
        throw new XMLException("'" + xpath + "' is not a valid path");
    }
    Iterator<Element> it = element.elementIterator();
    while (it.hasNext()) {
        elementNames.add(it.next().getName());
    }
    return elementNames.toArray(new String[elementNames.size()]);
}
Also used : XMLException(com.axway.ats.common.xml.XMLException) Element(org.dom4j.Element) ArrayList(java.util.ArrayList) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Aggregations

XMLException (com.axway.ats.common.xml.XMLException)18 PublicAtsApi (com.axway.ats.common.PublicAtsApi)14 Element (org.dom4j.Element)14 File (java.io.File)2 IOException (java.io.IOException)2 ArrayList (java.util.ArrayList)2 Attribute (org.dom4j.Attribute)2 BaseTest (com.axway.ats.action.BaseTest)1 StringReader (java.io.StringReader)1 StringWriter (java.io.StringWriter)1 HashMap (java.util.HashMap)1 Document (org.dom4j.Document)1 DocumentException (org.dom4j.DocumentException)1 SAXReader (org.dom4j.io.SAXReader)1 XMLWriter (org.dom4j.io.XMLWriter)1 Before (org.junit.Before)1 Test (org.junit.Test)1