Class XsdGeneratorHelper


  • public final class XsdGeneratorHelper
    extends java.lang.Object
    Utility class holding algorithms used when generating XSD schema.
    Since:
    1.4
    • Field Summary

      Fields 
      Modifier and Type Field Description
      private static javax.xml.transform.TransformerFactory FACTORY  
      private static java.lang.String MISCONFIG  
      private static java.io.FileFilter RECURSIVE_XSD_FILTER  
    • Constructor Summary

      Constructors 
      Modifier Constructor Description
      private XsdGeneratorHelper()
      Hide the constructor for utility classes.
    • Method Summary

      All Methods Static Methods Concrete Methods 
      Modifier and Type Method Description
      private static void addRecursively​(java.util.List<java.io.File> toPopulate, java.io.FileFilter fileFilter, java.io.File aDir)  
      private static java.lang.String getDuplicationErrorMessage​(java.lang.String propertyName, java.lang.String propertyValue, int firstIndex, int currentIndex)  
      private static javax.xml.transform.TransformerFactory getFactory()  
      static java.util.Map<java.lang.String,​SimpleNamespaceResolver> getFileNameToResolverMap​(java.io.File outputDirectory)
      Acquires a map relating generated schema filename to its SimpleNamespaceResolver.
      protected static java.lang.String getHumanReadableXml​(org.w3c.dom.Node node)
      Converts the provided DOM Node to a pretty-printed XML-formatted string.
      static int insertJavaDocAsAnnotations​(org.apache.maven.plugin.logging.Log log, java.lang.String encoding, java.io.File outputDir, SearchableDocumentation docs, JavaDocRenderer renderer)
      Inserts XML documentation annotations into all generated XSD files found within the supplied outputDir.
      static org.w3c.dom.Document parseXmlStream​(java.io.Reader xmlStream)
      Parses the provided InputStream to create a dom Document.
      private static org.w3c.dom.Document parseXmlToDocument​(java.io.File xmlFile)
      Creates a Document from parsing the XML within the provided xmlFile.
      static void process​(org.w3c.dom.Node node, boolean recurseToChildren, NodeProcessor visitor)
      Drives the supplied visitor to process the provided Node and all its children, should the recurseToChildren flag be set to true.
      static void renameGeneratedSchemaFiles​(java.util.Map<java.lang.String,​SimpleNamespaceResolver> resolverMap, java.util.List<TransformSchema> configuredTransformSchemas, org.apache.maven.plugin.logging.Log mavenLog, java.io.File schemaDirectory, java.lang.String charsetName)
      Updates all schemaLocation attributes within the generated schema files to match the 'file' properties within the Schemas read from the plugin configuration.
      static void replaceNamespacePrefixes​(java.util.Map<java.lang.String,​SimpleNamespaceResolver> resolverMap, java.util.List<TransformSchema> configuredTransformSchemas, org.apache.maven.plugin.logging.Log mavenLog, java.io.File schemaDirectory, java.lang.String encoding)
      Replaces all namespaces within generated schema files, as instructed by the configured Schema instances.
      private static void savePrettyPrintedDocument​(org.w3c.dom.Document toSave, java.io.File targetFile, java.lang.String charsetName)  
      private static void validatePrefixSubstitutionIsPossible​(java.lang.String oldPrefix, java.lang.String newPrefix, SimpleNamespaceResolver currentResolver)
      Validates that the transformation from oldPrefix to newPrefix is possible, in that newPrefix is not already used by a schema file.
      static void validateSchemasInPluginConfiguration​(java.util.List<TransformSchema> configuredTransformSchemas)
      Validates that the list of Schemas provided within the configuration all contain unique values.
      • Methods inherited from class java.lang.Object

        clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
    • Field Detail

      • FACTORY

        private static javax.xml.transform.TransformerFactory FACTORY
      • RECURSIVE_XSD_FILTER

        private static final java.io.FileFilter RECURSIVE_XSD_FILTER
    • Constructor Detail

      • XsdGeneratorHelper

        private XsdGeneratorHelper()
        Hide the constructor for utility classes.
    • Method Detail

      • getFileNameToResolverMap

        public static java.util.Map<java.lang.String,​SimpleNamespaceResolver> getFileNameToResolverMap​(java.io.File outputDirectory)
                                                                                                      throws org.apache.maven.plugin.MojoExecutionException
        Acquires a map relating generated schema filename to its SimpleNamespaceResolver.
        Parameters:
        outputDirectory - The output directory of the generated schema files.
        Returns:
        a map relating generated schema filename to an initialized SimpleNamespaceResolver.
        Throws:
        org.apache.maven.plugin.MojoExecutionException - if two generated schema files used the same namespace URI.
      • validateSchemasInPluginConfiguration

        public static void validateSchemasInPluginConfiguration​(java.util.List<TransformSchema> configuredTransformSchemas)
                                                         throws org.apache.maven.plugin.MojoExecutionException
        Validates that the list of Schemas provided within the configuration all contain unique values. Should a MojoExecutionException be thrown, it contains informative text about the exact nature of the configuration problem - we should simplify for all plugin users.
        Parameters:
        configuredTransformSchemas - The List of configuration schemas provided to this mojo.
        Throws:
        org.apache.maven.plugin.MojoExecutionException - if any two configuredSchemas instances contain duplicate values for any of the properties uri, prefix or file. Also throws a MojoExecutionException if the uri of any Schema is null or empty, or if none of the 'file' and 'prefix' properties are given within any of the configuredSchema instances.
      • insertJavaDocAsAnnotations

        public static int insertJavaDocAsAnnotations​(org.apache.maven.plugin.logging.Log log,
                                                     java.lang.String encoding,
                                                     java.io.File outputDir,
                                                     SearchableDocumentation docs,
                                                     JavaDocRenderer renderer)
        Inserts XML documentation annotations into all generated XSD files found within the supplied outputDir.
        Parameters:
        log - A Maven Log.
        outputDir - The outputDir, where generated XSD files are found.
        docs - The SearchableDocumentation for the source files within the compilation unit.
        renderer - The JavaDocRenderer used to convert JavaDoc annotations into XML documentation annotations.
        Returns:
        The number of processed XSDs.
      • replaceNamespacePrefixes

        public static void replaceNamespacePrefixes​(java.util.Map<java.lang.String,​SimpleNamespaceResolver> resolverMap,
                                                    java.util.List<TransformSchema> configuredTransformSchemas,
                                                    org.apache.maven.plugin.logging.Log mavenLog,
                                                    java.io.File schemaDirectory,
                                                    java.lang.String encoding)
                                             throws org.apache.maven.plugin.MojoExecutionException
        Replaces all namespaces within generated schema files, as instructed by the configured Schema instances.
        Parameters:
        resolverMap - The map relating generated schema file name to SimpleNamespaceResolver instances.
        configuredTransformSchemas - The Schema instances read from the configuration of this plugin.
        mavenLog - The active Log.
        schemaDirectory - The directory where all generated schema files reside.
        encoding - The encoding to use when writing the file.
        Throws:
        org.apache.maven.plugin.MojoExecutionException - If the namespace replacement could not be done.
      • renameGeneratedSchemaFiles

        public static void renameGeneratedSchemaFiles​(java.util.Map<java.lang.String,​SimpleNamespaceResolver> resolverMap,
                                                      java.util.List<TransformSchema> configuredTransformSchemas,
                                                      org.apache.maven.plugin.logging.Log mavenLog,
                                                      java.io.File schemaDirectory,
                                                      java.lang.String charsetName)
        Updates all schemaLocation attributes within the generated schema files to match the 'file' properties within the Schemas read from the plugin configuration. After that, the files are physically renamed.
        Parameters:
        resolverMap - The map relating generated schema file name to SimpleNamespaceResolver instances.
        configuredTransformSchemas - The Schema instances read from the configuration of this plugin.
        mavenLog - The active Log.
        schemaDirectory - The directory where all generated schema files reside.
        charsetName - The encoding / charset name.
      • process

        public static void process​(org.w3c.dom.Node node,
                                   boolean recurseToChildren,
                                   NodeProcessor visitor)
        Drives the supplied visitor to process the provided Node and all its children, should the recurseToChildren flag be set to true. All attributes of the current node are processed before recursing to children (i.e. breadth first recursion).
        Parameters:
        node - The Node to process.
        recurseToChildren - if true, processes all children of the supplied node recursively.
        visitor - The NodeProcessor instance which should process the nodes.
      • parseXmlStream

        public static org.w3c.dom.Document parseXmlStream​(java.io.Reader xmlStream)
        Parses the provided InputStream to create a dom Document.
        Parameters:
        xmlStream - An InputStream connected to an XML document.
        Returns:
        A DOM Document created from the contents of the provided stream.
      • getHumanReadableXml

        protected static java.lang.String getHumanReadableXml​(org.w3c.dom.Node node)
        Converts the provided DOM Node to a pretty-printed XML-formatted string.
        Parameters:
        node - The Node whose children should be converted to a String.
        Returns:
        a pretty-printed XML-formatted string.
      • getDuplicationErrorMessage

        private static java.lang.String getDuplicationErrorMessage​(java.lang.String propertyName,
                                                                   java.lang.String propertyValue,
                                                                   int firstIndex,
                                                                   int currentIndex)
      • validatePrefixSubstitutionIsPossible

        private static void validatePrefixSubstitutionIsPossible​(java.lang.String oldPrefix,
                                                                 java.lang.String newPrefix,
                                                                 SimpleNamespaceResolver currentResolver)
                                                          throws org.apache.maven.plugin.MojoExecutionException
        Validates that the transformation from oldPrefix to newPrefix is possible, in that newPrefix is not already used by a schema file. This would corrupt the schema by assigning elements from one namespace to another.
        Parameters:
        oldPrefix - The old/current namespace prefix.
        newPrefix - The new/future namespace prefix.
        currentResolver - The currently active SimpleNamespaceResolver.
        Throws:
        org.apache.maven.plugin.MojoExecutionException - if any schema file currently uses newPrefix.
      • parseXmlToDocument

        private static org.w3c.dom.Document parseXmlToDocument​(java.io.File xmlFile)
        Creates a Document from parsing the XML within the provided xmlFile.
        Parameters:
        xmlFile - The XML file to be parsed.
        Returns:
        The Document corresponding to the xmlFile.
      • savePrettyPrintedDocument

        private static void savePrettyPrintedDocument​(org.w3c.dom.Document toSave,
                                                      java.io.File targetFile,
                                                      java.lang.String charsetName)
      • addRecursively

        private static void addRecursively​(java.util.List<java.io.File> toPopulate,
                                           java.io.FileFilter fileFilter,
                                           java.io.File aDir)
      • getFactory

        private static javax.xml.transform.TransformerFactory getFactory()