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