Search in sources :

Example 1 with XMLException

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

the class Test_XmlText method constructXmlTextInvallidXMLFile.

@Test
public void constructXmlTextInvallidXMLFile() {
    File xml = null;
    try {
        xml = File.createTempFile("temp_xml", null);
    } catch (IOException ex) {
        ex.printStackTrace();
    }
    XmlText xmlText = null;
    try {
        xmlText = new XmlText(xml);
    } catch (XMLException e) {
        log.error(e.getMessage());
    }
    assertEquals(null, xmlText);
    xml.delete();
}
Also used : XMLException(com.axway.ats.common.xml.XMLException) IOException(java.io.IOException) File(java.io.File) BaseTest(com.axway.ats.action.BaseTest) Test(org.junit.Test)

Example 2 with XMLException

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

the class Test_XmlText method setUpTest_XmlText.

@Before
public void setUpTest_XmlText() {
    file = new File(Test_XmlText.class.getResource("test.xml").getPath());
    try {
        xmlText1 = new XmlText(body1.toString());
        xmlText2 = new XmlText(body2.toString());
        xmlText3 = new XmlText(body3.toString());
    } catch (XMLException e) {
        log.error(e.getMessage());
    }
}
Also used : XMLException(com.axway.ats.common.xml.XMLException) File(java.io.File) Before(org.junit.Before)

Example 3 with XMLException

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

the class XmlText method replace.

/**
     * Replace a XML element.
     *
     * @param xpath XPath , pointing to a XML element
     * @param object the new XML element
     * @return this instance
     * @throws XMLException
     */
@PublicAtsApi
public XmlText replace(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 for replacement." + "If you want to remove existing XML element, use XMLText.remove().");
    }
    Element newElement = null;
    if (object instanceof XmlText) {
        newElement = ((XmlText) object).root;
    }
    if (object instanceof String) {
        if (StringUtils.isNullOrEmpty((String) object)) {
            throw new XMLException("Null/empty String object is not allowed for replacement." + "If you want to remove existing XML element, use XMLText.remove().");
        }
        newElement = new XmlText((String) object).root;
    }
    if (newElement == null) {
        throw new XMLException("Given object for replacing an existing one is from invallid class instance. " + "Use String or XMLText instances only.");
    }
    Element oldElement = findElement(xpath);
    if (oldElement != null) {
        if (oldElement.isRootElement()) {
            throw new XMLException("You cannot replace the root element of the XML document.");
        }
        Element parent = oldElement.getParent();
        if (parent != null) {
            parent.elements().set(parent.elements().indexOf(oldElement), newElement);
        } else {
            throw new XMLException("Parent for element with xpath '" + xpath + "' could not be found.");
        }
    } else {
        throw new XMLException("'" + xpath + "' is not a valid path");
    }
    return this;
}
Also used : XMLException(com.axway.ats.common.xml.XMLException) Element(org.dom4j.Element) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 4 with XMLException

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

the class XmlText method getAttributes.

/**
     * @param xpath XPath , pointing to a XML element
     * @return a HashMap , containing the attributes and their values
     * @throws XMLException
     */
@PublicAtsApi
public Map<String, String> getAttributes(String xpath) throws XMLException {
    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");
    }
    HashMap<String, String> attributes = new HashMap<>(1);
    Iterator<Attribute> it = element.attributeIterator();
    while (it.hasNext()) {
        Attribute attr = it.next();
        attributes.put(attr.getName(), attr.getValue());
    }
    return attributes;
}
Also used : XMLException(com.axway.ats.common.xml.XMLException) HashMap(java.util.HashMap) Attribute(org.dom4j.Attribute) Element(org.dom4j.Element) PublicAtsApi(com.axway.ats.common.PublicAtsApi)

Example 5 with XMLException

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

the class XmlText method init.

private void init(String xmlText) throws XMLException {
    Document document = null;
    try {
        document = new SAXReader().read(new StringReader(xmlText));
    } catch (DocumentException e) {
        throw new XMLException("Error parsing XML text:\n" + xmlText, e);
    }
    this.root = document.getRootElement();
}
Also used : XMLException(com.axway.ats.common.xml.XMLException) SAXReader(org.dom4j.io.SAXReader) DocumentException(org.dom4j.DocumentException) StringReader(java.io.StringReader) Document(org.dom4j.Document)

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