Search in sources :

Example 16 with XMLException

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

the class XmlText method removeAttribute.

/**
     * Removes an attribute from XML element.
     *
     * @param xpath XPath , pointing to a XML element
     * @param name the name of the attribute
     * @return this instance
     * @throws XMLException
     */
@PublicAtsApi
public XmlText removeAttribute(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");
    }
    Attribute attribute = element.attribute(name);
    if (attribute == null) {
        throw new XMLException("'" + name + "' attribute cannot be found and replaced for element with xpath '" + xpath + "'.");
    }
    element.attributes().remove(attribute);
    return this;
}
Also used : XMLException(com.axway.ats.common.xml.XMLException) Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 17 with XMLException

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

the class XmlText method getString.

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

Example 18 with XMLException

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

the class XmlText method getElementXPaths.

/**
     * Returns the child XPaths of a XML element
     *
     * @return the XPath of the XML element.
     * @return the child XPaths of a XML element
     * @throws XMLException
     */
@PublicAtsApi
public String[] getElementXPaths(String xpath) throws XMLException {
    ArrayList<String> elementXPaths = 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()) {
        elementXPaths.add(it.next().getUniquePath());
    }
    return elementXPaths.toArray(new String[elementXPaths.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