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();
}
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());
}
}
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;
}
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;
}
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();
}
Aggregations