Class XmlDocumentReader<T>

java.lang.Object
org.openhab.core.config.core.xml.util.XmlDocumentReader<T>
Type Parameters:
T - the result type of the conversion

@NonNullByDefault public abstract class XmlDocumentReader<@NonNull T> extends Object
The XmlDocumentReader is an abstract class used to read XML documents of a certain type and converts them to its according objects.

This class uses XStream and StAX to parse and convert the XML document.

Author:
Michael Grammling - Initial contribution, Wouter Born - Configure XStream security
  • Field Details

    • DEFAULT_ALLOWED_TYPES_WILDCARD

      protected static final String[] DEFAULT_ALLOWED_TYPES_WILDCARD
  • Constructor Details

  • Method Details

    • setClassLoader

      protected void setClassLoader(ClassLoader classLoader)
      Sets the classloader for XStream.
      Parameters:
      classLoader - the classloader to set (must not be null)
    • configureSecurity

      protected void configureSecurity(com.thoughtworks.xstream.XStream xstream)
      Configures the security of the XStream instance to protect against vulnerabilities.
      Parameters:
      xstream - the XStream object to be configured
      See Also:
    • registerConverters

      protected abstract void registerConverters(com.thoughtworks.xstream.XStream xstream)
      Registers any Converters at the XStream instance.
      Parameters:
      xstream - the XStream object to be configured
    • registerAliases

      protected abstract void registerAliases(com.thoughtworks.xstream.XStream xstream)
      Registers any aliases at the XStream instance.
      Parameters:
      xstream - the XStream object to be configured
    • readFromXML

      public @Nullable T readFromXML(URL xmlURL) throws com.thoughtworks.xstream.converters.ConversionException
      Reads the XML document containing a specific XML tag from the specified URL and converts it to the according object.

      This method returns null if the given URL is null.

      Parameters:
      xmlURL - the URL pointing to the XML document to be read (could be null)
      Returns:
      the conversion result object (could be null)
      Throws:
      com.thoughtworks.xstream.converters.ConversionException - if the specified document contains invalid content
    • readFromXML

      public @Nullable T readFromXML(String xml) throws com.thoughtworks.xstream.converters.ConversionException
      Reads the XML document containing a specific XML tag from the specified xml string and converts it to the according object.
      Parameters:
      xml - a string containing the XML document to be read.
      Returns:
      the conversion result object (could be null).
      Throws:
      com.thoughtworks.xstream.XStreamException - if the object cannot be deserialized.
      com.thoughtworks.xstream.converters.ConversionException - if the specified document contains invalid content