Package org.kiwiproject.xml
Class KiwiXml
- java.lang.Object
-
- org.kiwiproject.xml.KiwiXml
-
public class KiwiXml extends Object
Static utilities for converting to/from XML using JAXB and a few other XML-related utilities.- Implementation Note:
- the appropriate JAXB dependencies must be available at runtime
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classKiwiXml.NamespaceValidationWhether validation should be performed or not.
-
Field Summary
Fields Modifier and Type Field Description static StringDISABLE_NAMESPACE_VALIDATIONConstant that can be used to disable all namespace validations when converting from XML to objects.
-
Constructor Summary
Constructors Constructor Description KiwiXml()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static voidclearCachedJAXBContextClasses()Clear the internal cache of class toJAXBContextmappings.static Set<Class<?>>getCachedJAXBContextClasses()Find the classes for which there is a cachedJAXBContext.static Map<String,String>getNameAndNamespace(Class<?> objectClass)Extract XML name and namespace from the given class, assuming it is annotated withXmlType.static StringstripTags(String xml, String... tagsToRemove)Removes tags from the given XML but ignoring namespaces.static StringstripTagsConsideringNamespace(String xml, String... tagsToRemove)Removes tags from the given XML taking into account the full tag name (i.e.static <T> TtoObject(String xml, Class<T> clazz)Convert the given XML into an object of the specified type.static <T> TtoObjectAssertingValid(String xml, Class<T> clazz, KiwiXml.NamespaceValidation namespaceValidation)Convert the given XML into an object of the specified type, optionally performing namespace validation.static <T> TtoObjectAssertingValid(String xml, Class<T> clazz, KiwiXml.NamespaceValidation namespaceValidation, List<String> ignoredNamespaces)Convert the given XML into an object of the specified type, performing basic validation and ignoring the specified namespaces.static <T> TtoObjectIgnoringNamespace(String xml, Class<T> clazz)Convert the given XML into an object of the specified type, ignoring any XML namespace information and not performing any namespace validation.static StringtoXml(Object object)Convert the given object to an XML representation.static StringtoXml(Object object, Class<?> clazz)Convert the given object to an XML representation.static StringtoXml(Object object, Class<?> clazz, Map<String,Object> marshallerProperties)Convert the given object to an XML representation.static <T> booleanvalidateXmlMatchesType(String xml, Class<T> clazz)Validate that the given XML has a namespace that matches the given class, which is generally assumed to be annotated withXmlType.static <T> booleanvalidateXmlMatchesType(String xml, Class<T> clazz, List<String> ignoredNamespaces)Validate that the given XML has a namespace that matches the given class, which is generally assumed to be annotated withXmlType, but ignoring the given list of namespaces.
-
-
-
Field Detail
-
DISABLE_NAMESPACE_VALIDATION
public static final String DISABLE_NAMESPACE_VALIDATION
Constant that can be used to disable all namespace validations when converting from XML to objects.
-
-
Method Detail
-
getCachedJAXBContextClasses
public static Set<Class<?>> getCachedJAXBContextClasses()
Find the classes for which there is a cachedJAXBContext. Each time an instance of a class is converted to XML, theJAXBContextis retrieved from an internal cache. If there isn't one, then one is created and stored in the cache.- Returns:
- the set of classes for which
KiwiXmlhas a cachedJAXBContext. The returned set is an unmodifiable copy of the actual cached classes - Implementation Note:
- The internal cache is a static
ConcurrentMap.
-
clearCachedJAXBContextClasses
public static void clearCachedJAXBContextClasses()
Clear the internal cache of class toJAXBContextmappings.
-
toXml
public static String toXml(Object object)
Convert the given object to an XML representation.- Parameters:
object- the object to convert to XML- Returns:
- the XML representation of the object
-
toXml
public static String toXml(Object object, Class<?> clazz)
Convert the given object to an XML representation.- Parameters:
object- the object to convert to XMLclazz- the type of class being converted- Returns:
- the XML representation of the object
-
toXml
public static String toXml(Object object, Class<?> clazz, Map<String,Object> marshallerProperties)
Convert the given object to an XML representation.- Parameters:
object- the object to convert to XMLclazz- the type of class being convertedmarshallerProperties- the properties to be set on theMarshallerduring the conversion process- Returns:
- the XML representation of the object
- See Also:
Marshaller.setProperty(String, Object)
-
toObject
public static <T> T toObject(String xml, Class<T> clazz)
Convert the given XML into an object of the specified type.- Type Parameters:
T- the target type- Parameters:
xml- the XML to convertclazz- type of object to convert into- Returns:
- a new instance of the specified type
-
toObjectIgnoringNamespace
public static <T> T toObjectIgnoringNamespace(String xml, Class<T> clazz)
Convert the given XML into an object of the specified type, ignoring any XML namespace information and not performing any namespace validation.- Type Parameters:
T- the target type- Parameters:
xml- the XML to convertclazz- type of object to convert into- Returns:
- a new instance of the specified type
-
toObjectAssertingValid
public static <T> T toObjectAssertingValid(String xml, Class<T> clazz, KiwiXml.NamespaceValidation namespaceValidation)
Convert the given XML into an object of the specified type, optionally performing namespace validation.- Type Parameters:
T- the target type- Parameters:
xml- the XML to convertclazz- type of object to convert intonamespaceValidation- should namespace validation be performed or not- Returns:
- a new instance of the specified type
-
toObjectAssertingValid
public static <T> T toObjectAssertingValid(String xml, Class<T> clazz, KiwiXml.NamespaceValidation namespaceValidation, List<String> ignoredNamespaces)
Convert the given XML into an object of the specified type, performing basic validation and ignoring the specified namespaces.- Type Parameters:
T- the target type- Parameters:
xml- the XML to convertclazz- type of object to convert intonamespaceValidation- should namespace validation be performed or notignoredNamespaces- list of namespaces to ignore- Returns:
- a new instance of the specified type
-
validateXmlMatchesType
public static <T> boolean validateXmlMatchesType(String xml, Class<T> clazz)
Validate that the given XML has a namespace that matches the given class, which is generally assumed to be annotated withXmlType.- Type Parameters:
T- the type of the target class- Parameters:
xml- the input XML to compareclazz- theClassto compare; assumed to be annotated withXmlType- Returns:
- true if the XML namespace (e.g. xmlns) matches the namespace of the
XmlTypeannotation on the given class - Throws:
XmlRuntimeException- if something bad and unexpected happens. The thrown exception wraps aXMLStreamExceptionor other cause.
-
validateXmlMatchesType
public static <T> boolean validateXmlMatchesType(String xml, Class<T> clazz, List<String> ignoredNamespaces)
Validate that the given XML has a namespace that matches the given class, which is generally assumed to be annotated withXmlType, but ignoring the given list of namespaces.- Type Parameters:
T- the type of the target class- Parameters:
xml- the input XML to compareclazz- theClassto compare; assumed to be annotated withXmlTypeignoredNamespaces- the namespaces to ignore- Returns:
- true if the XML namespace (e.g. xmlns) matches the namespace of the
XmlTypeannotation on the given class - Throws:
XmlRuntimeException- if something bad and unexpected happens. The thrown exception wraps aXMLStreamExceptionor other cause.
-
getNameAndNamespace
public static Map<String,String> getNameAndNamespace(Class<?> objectClass)
Extract XML name and namespace from the given class, assuming it is annotated withXmlType.If the given class is not annotated with
XmlType, we return the namespace asnulland the name as the "simple name" of the class fromClass.getSimpleName(). This allows some degree of flexibility when working with non-annotated classes, though this should be the exception not the normal situation.- Parameters:
objectClass- the class to get name and namespace from- Returns:
- a map containing entries for name and namespace
- See Also:
XmlType.name(),XmlType.namespace()
-
stripTags
public static String stripTags(String xml, String... tagsToRemove)
Removes tags from the given XML but ignoring namespaces.- Parameters:
xml- the XML containing tags to be removedtagsToRemove- names of the tags to remove- Returns:
- XML with the given tags removed
-
stripTagsConsideringNamespace
public static String stripTagsConsideringNamespace(String xml, String... tagsToRemove)
Removes tags from the given XML taking into account the full tag name (i.e. possibly including namespace).- Parameters:
xml- the XML containing tags to be removedtagsToRemove- names of the tags to remove- Returns:
- XML with the given tags removed
-
-