com.jayway.restassured.config.XmlConfig Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of rest-assured Show documentation
Show all versions of rest-assured Show documentation
Java DSL for easy testing of REST services
package com.jayway.restassured.config;
import org.apache.commons.lang3.Validate;
import java.util.HashMap;
import java.util.Map;
/**
* Allows you to configure properties of XML and HTML parsing.
*/
public class XmlConfig {
private final Map properties;
private final Map features;
private final Map declaredNamespaces;
private final boolean namespaceAware;
/**
* Create a new instance of XmlConfig without any features and that is namespace unaware.
*/
public XmlConfig() {
this(new HashMap(), new HashMap(), new HashMap(), false);
}
private XmlConfig(Map features, Map declaredNamespaces, Map properties, boolean namespaceAware) {
Validate.notNull(features, "Features cannot be null");
Validate.notNull(declaredNamespaces, "Declared namespaces cannot be null");
Validate.notNull(properties, "Properties cannot be null");
this.namespaceAware = namespaceAware;
this.features = features;
this.declaredNamespaces = declaredNamespaces;
this.properties = properties;
}
/**
* @return A map containing features that will be used by the underlying {@link groovy.util.XmlSlurper}.
* @see org.xml.sax.XMLReader#setFeature(java.lang.String, boolean)
*/
public Map features() {
return new HashMap(features);
}
/**
* @return A map containing properties that will be used by the underlying {@link groovy.util.XmlSlurper}.
* @see org.xml.sax.XMLReader#setProperty(String, Object)
*/
public Map properties() {
return new HashMap(properties);
}
/**
* Specify features that will be used when parsing XML.
*
* @param features A map containing features that will be used by the underlying {@link groovy.util.XmlSlurper}.
* @return
* @see org.xml.sax.XMLReader#setFeature(java.lang.String, boolean)
*/
public XmlConfig features(Map features) {
return new XmlConfig(features, declaredNamespaces, properties, namespaceAware);
}
/**
* Specify properties that will be used when parsing XML.
*
* @param properties A map containing properties that will be used by the underlying {@link groovy.util.XmlSlurper}.
* @return
* @see org.xml.sax.XMLReader#setProperty(String, Object)
*/
public XmlConfig properties(Map properties) {
return new XmlConfig(features, declaredNamespaces, this.properties, namespaceAware);
}
/**
* Set a value of a feature flag.
*
* @param uri The feature name, which is a fully-qualified URI.
* @param enabled The requested value of the feature (true or false).
* @return A new XmlConfig instance
* @see org.xml.sax.XMLReader#setFeature(java.lang.String, boolean)
*/
public XmlConfig feature(String uri, boolean enabled) {
Validate.notEmpty(uri, "URI cannot be empty");
Map newFeatures = new HashMap(features);
newFeatures.put(uri, enabled);
return new XmlConfig(newFeatures, declaredNamespaces, properties, namespaceAware);
}
/**
* Set a value of a property.
*
* @param name The property name.
* @param value The requested value of the feature (true or false).
* @return A new XmlConfig instance
* @see org.xml.sax.XMLReader#setFeature(java.lang.String, boolean)
*/
public XmlConfig property(String name, Object value) {
Validate.notEmpty(name, "Name cannot be empty");
Map newProperties = new HashMap(properties);
newProperties.put(name, value);
return new XmlConfig(features, declaredNamespaces, newProperties, namespaceAware);
}
/**
* @return A map containing namespaces that will be used by the underlying {@link groovy.util.XmlSlurper}.
* @see org.xml.sax.XMLReader#setFeature(java.lang.String, boolean)
*/
public Map declaredNamespaces() {
return new HashMap(declaredNamespaces);
}
/**
* Specify declared namespaces that will be used when parsing XML. Will also set {@link #namespaceAware(boolean)} to true
of namespaces are not empty.
* Note that you cannot use this to add namespaces for the {@link org.hamcrest.xml.HasXPath} matcher.
* This has to be done by providing a {@link javax.xml.namespace.NamespaceContext} to the matcher instance.
*
* @param namespacesToDeclare A map containing features that will be used by the underlying {@link groovy.util.XmlSlurper}.
* @return A new instance of XmlConfig
* @see org.xml.sax.XMLReader#setFeature(java.lang.String, boolean)
*/
public XmlConfig declareNamespaces(Map namespacesToDeclare) {
final boolean shouldBeNamespaceAware = namespacesToDeclare == null ? namespaceAware : !namespacesToDeclare.isEmpty();
return new XmlConfig(features, namespacesToDeclare, properties, shouldBeNamespaceAware);
}
/**
* Declares a namespace and also sets {@link #namespaceAware(boolean)} to true
.
*
* Note that you cannot use this to add namespaces for the {@link org.hamcrest.xml.HasXPath} matcher.
* This has to be done by providing a {@link javax.xml.namespace.NamespaceContext} to the matcher instance.
*
* @param prefix The feature name, which is a fully-qualified URI.
* @param namespaceURI The requested value of the feature (true or false).
* @return A new XmlConfig instance
* @see org.xml.sax.XMLReader#setFeature(java.lang.String, boolean)
*/
public XmlConfig declareNamespace(String prefix, String namespaceURI) {
Validate.notEmpty(prefix, "Prefix cannot be empty");
Validate.notEmpty(namespaceURI, "Namespace URI cannot be empty");
Map updatedNamespaces = new HashMap(declaredNamespaces);
updatedNamespaces.put(prefix, namespaceURI);
return new XmlConfig(features, updatedNamespaces, properties, true);
}
/**
* Disables external DTD loading.
*
* This is a shortcut for doing:
*
* setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
*
*
*
* @return A new XmlConfig instance
* @see #feature(String, boolean)
*/
public XmlConfig disableLoadingOfExternalDtd() {
Map newFeatures = new HashMap(features);
newFeatures.put("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
return new XmlConfig(newFeatures, declaredNamespaces, properties, namespaceAware);
}
/**
* Configure whether or not REST Assured should be aware of namespaces when parsing XML.
*
* @param shouldBeAwareOfNamespaces true
if xml parsing should take namespaces into account.
* @return A new XmlConfig instance
*/
public XmlConfig namespaceAware(boolean shouldBeAwareOfNamespaces) {
return new XmlConfig(features, declaredNamespaces, properties, shouldBeAwareOfNamespaces);
}
/**
* @return true
if REST Assured should be namespace aware.
*/
public boolean isNamespaceAware() {
return namespaceAware;
}
/**
* For syntactic sugar.
*
* @return The same XmlConfig instance
*/
public XmlConfig with() {
return this;
}
/**
* For syntactic sugar.
*
* @return The same XmlConfig instance
*/
public XmlConfig and() {
return this;
}
/**
* @return A static way to create a new XmlConfig instance without calling "new" explicitly. Mainly for syntactic sugar.
*/
public static XmlConfig xmlConfig() {
return new XmlConfig();
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy