Class XMLConversionService

java.lang.Object
org.dlese.dpc.xml.XMLConversionService

public final class XMLConversionService extends Object
Manages the conversion of XML files from one format to another using XSL or Java classes. Caches the converted XML to disc and provides rapid access to the converted format. Conversions may be accomplished using XSL stylesheets or Java classes that implement one of either the XMLFormatConverter or XMLDocumentFormatConverter interfaces.
Version:
$Id: XMLConversionService.java,v 1.24 2007/02/23 21:20:00 jweather Exp $
Author:
John Weatherley
See Also:
  • Constructor Details

    • XMLConversionService

      public XMLConversionService(File cacheDir, boolean filterDeclarations) throws IOException
      Constructor for the XMLConversionService.
      Parameters:
      cacheDir - The directory where converted files will be cached and stored for later retrieval by ID.
      filterDeclarations - Set to true to filter out the XML and DTD declarations in the converted XML.
      Throws:
      IOException - If the directory is not valid or does not have read/write access.
  • Method Details

    • getConvertedXml

      public StringBuffer getConvertedXml(String fromFormat, String toFormat, File originalXMLFile)
      Converts XML from one format to another, saving and retrieving the converted content to and from a file cache. Returns null if there are no known conversions available for the requested conversion, or if a processing error occurs. The cache file is automatically updated if the modification time of the input XML file is newer than the cache. Characters returned in the StringBuffer are encoded in UTF-8.
      Parameters:
      fromFormat - The XML format to convert from. Example: 'dlese_ims.'
      toFormat - The format to convert to. Example: 'adn.'
      originalXMLFile - The original XML file, in the 'from' format.
      Returns:
      Content converted to the 'to' format, or null if unable to process.
    • convertXml

      public String convertXml(String fromFormat, String toFormat, String originalXML)
      Converts XML from one format to another, without the use of a cache to store or save results. Returns null if there are no known conversions available for the requested conversion, or if a processing error occurs. Characters returned in the StringBuffer are encoded in UTF-8.
      Parameters:
      fromFormat - The XML format to convert from. Example: 'dlese_ims.'
      toFormat - The format to convert to. Example: 'adn.'
      originalXML - The original XML as a String, in the 'from' format. Should not be null.
      Returns:
      Content converted to the 'to' format, or null if unable to process.
    • getConvertedXml

      public StringBuffer getConvertedXml(String fromFormat, String toFormat, File originalXMLFile, XMLDocReader luceneDoc)
      Converts XML from one format to another, saving and retrieving the converted content to and from a file cache. Returns null if there are no known conversions available for the requested conversion, or if a processing error occurs. The cache file is automatically updated if the modification time of the input XML file is newer than the cache. Characters returned in the StringBuffer are encoded in UTF-8.
      Parameters:
      fromFormat - The XML format to convert from. Example: 'dlese_ims.'
      toFormat - The format to convert to. Example: 'adn.'
      originalXMLFile - The original XML file, in the 'from' format. Should not be null.
      luceneDoc - A Lucene Document that holds content that may be used by the conversion class, or null.
      Returns:
      Content converted to the 'to' format, or null if unable to process.
    • canConvert

      public boolean canConvert(String fromFormat, String toFormat)
      Determines whether this XMLConversionService can perform the given converstion. Returns true of the toFormat is the same as the fromFormat.
      Parameters:
      fromFormat - The format to convert from.
      toFormat - The format to convert to.
      Returns:
      True if this XMLConversionService can perform the conversion.
    • getAvailableFormats

      public ArrayList getAvailableFormats(String fromFormat)
      Gets the metadata format conversions that are available for the given format, including the given format.
      Parameters:
      fromFormat - The format to convert from.
      Returns:
      The availableFormats.
    • addXslStylesheet

      public void addXslStylesheet(String fromFormat, String toFormat, String xslFilePath)
      Adds a XSL stylesheet that can convert from one XML format to another.
      Parameters:
      fromFormat - The XML format from which the stylesheet will convert.
      toFormat - The XML format to which the stylesheet will convert.
      xslFilePath - The absolute path to the stylesheet.
    • addJavaConverterClass

      public void addJavaConverterClass(String fromFormat, String toFormat, String className, ServletContext servletContext)
      Adds a concrete implementation of the XMLFormatConverter interface that can convert XML from one format to another. The class must be available in the classpath for Tomcat or the running JVM that is using this broker.
      Parameters:
      fromFormat - The XML format from which the class will convert.
      toFormat - The XML format to which the class will convert.
      className - The fully-qualified class that will perform the conversion.
      servletContext - The feature to be added to the JavaConverterClass attribute
    • addXslConverterHelper

      public boolean addXslConverterHelper(String paramVal, File xslFilesDirecoryPath)
      Adds an XSL converter by parsing a String of the form [ fromFormat | toFormat | xslFileName ]. Useful for using initParameters to configure XML format converters.
      Parameters:
      paramVal - A String of the form [ fromFormat | toFormat | xslFileName ].
      xslFilesDirecoryPath - The full path to the directory where the xsl file exists.
      Returns:
      False if the number of values parsed in paramVal is incorrect, otherwise true.
    • addJavaConverterHelper

      public boolean addJavaConverterHelper(String paramVal, ServletContext servletContext)
      Adds an Java class XML converter by parsing a String of the form [ fromFormat | toFormat | className ]. Useful for using initParameters to configure XML format converters.
      Parameters:
      paramVal - A String of the form [ fromFormat | toFormat | xslFileName ].
      servletContext - The ServletContext that will be available to the class when it executes.
      Returns:
      False if the number of values parsed in paramVal is incorrect, otherwise true.
    • xmlConversionServiceFactoryForServlets

      public static XMLConversionService xmlConversionServiceFactoryForServlets(ServletContext servletContext, File xslFilesDirecory, File xmlCachDirecory, boolean filterDeclarations) throws Exception
      Factory for use in Servlets to create an XMLConversionService using configurations found in the Servlet context parameters. This method also places the path to the XSL files directory in the servlet context under 'xslFilesDirecoryPath', for use by the JavaXmlConverters.

      To configure a format converter that uses an XSL style sheet or Java class, the context param-name must begin with the either 'xslconverter' or 'javaconverter' and must be unique. The param-value must be of the form 'xslfile.xsl|[from format]\[to format]' or '[fully qualified Java class]|[from format]\[to format]'.

      Parameters:
      servletContext - The ServletContext
      xslFilesDirecory - The base directory where the XSL files are located
      xmlCachDirecory - The directory where the service will cache the converted XML files
      filterDeclarations - Set to true to filter out the XML and DTD declarations in the converted XML
      Returns:
      The XMLConversionService
      Throws:
      Exception - If error
    • stripXmlDeclaration

      public static final StringBuffer stripXmlDeclaration(BufferedReader rdr) throws IOException
      Strips the XML declaration and DTD declaration from the given XML. The resulting content is sutable for insertion inside an existing XML element.
      Parameters:
      rdr - A BufferedReader containing XML.
      Returns:
      Content with the XML and DTD declarations stipped out.
      Throws:
      IOException - If error
    • getContentFromXML

      public static final String getContentFromXML(String input)
      Gets the content from XML by stripping all XML tags. The input XML should be valid prior to calling this method.
      Parameters:
      input - A valid XML string.
      Returns:
      The contentFromXML.
    • getDateStamp

      public static final String getDateStamp()
      Gets a datestamp of the current time formatted for display with logs and output.
      Returns:
      A datestamp for display purposes.
    • setDebug

      public static final void setDebug(boolean db)
      Sets the debug attribute of the SimpleLuceneIndex object
      Parameters:
      db - The new debug value