You are viewing a plain text version of this content. The canonical link for it is here.
Posted to torque-dev@db.apache.org by tf...@apache.org on 2010/09/03 21:50:12 UTC

svn commit: r992436 - in /db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration: ./ controller/ source/

Author: tfischer
Date: Fri Sep  3 19:50:11 2010
New Revision: 992436

URL: http://svn.apache.org/viewvc?rev=992436&view=rev
Log:
- add possibility to define entity references in the control file.
This is needed in order to access xsd schema files for xml source validation.

Added:
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/EntityReferenceSaxHandler.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/EntityReferences.java
Modified:
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfiguration.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfigurationReader.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfiguration.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfigurationSaxHandler.java
    db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/SourceConfigurationTags.java

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfiguration.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfiguration.java?rev=992436&r1=992435&r2=992436&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfiguration.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfiguration.java Fri Sep  3 19:50:11 2010
@@ -27,6 +27,7 @@ import java.util.List;
 import org.apache.torque.generator.configuration.controller.Loglevel;
 import org.apache.torque.generator.configuration.controller.Output;
 import org.apache.torque.generator.configuration.outlet.OutletConfiguration;
+import org.apache.torque.generator.configuration.source.EntityReferences;
 import org.apache.torque.generator.option.Options;
 import org.apache.torque.generator.source.SourceProvider;
 
@@ -52,6 +53,9 @@ public class UnitConfiguration
     /** The configuration for the available outlets. */
     private OutletConfiguration outletConfiguration;
 
+    /** The entity references for resolving schema files for XML sources. */
+    private EntityReferences entityReferences;
+
     /**
      * The root directory where the generated file which are generated
      * each time anew are put into.
@@ -312,6 +316,40 @@ public class UnitConfiguration
     }
 
     /**
+     * Returns the entityReferences of the associated configuration unit.
+     *
+     * @return the entityReferences, not null.
+     *
+     * @throws IllegalStateException if entityReferences were not yet set.
+     */
+    public EntityReferences getEntityReferences()
+    {
+        if (entityReferences == null)
+        {
+            throw new IllegalStateException(
+                    "entityReferences are not initialized");
+        }
+        return entityReferences;
+    }
+
+    /**
+     * Sets the entityReferences of the associated configuration unit.
+     *
+     * @param options the entityReferences, not null.
+     *
+     * @throws NullPointerException if entityReferences is null.
+     */
+    public void setEntityReferences(EntityReferences entityReferences)
+    {
+        if (entityReferences == null)
+        {
+            throw new NullPointerException(
+                    "entityReferences cannot be null");
+        }
+        this.entityReferences = entityReferences;
+    }
+
+    /**
      * Checks whether the unit configuration is fully initialized.
      *
      * @return true if the unit configuration is fully initialized,
@@ -325,6 +363,7 @@ public class UnitConfiguration
             || newFileTargetDirectory == null
             || modifiedFileTargetDirectory == null
             || loglevel == null
+            || entityReferences == null
             || !overrideSourceProviderInitialized)
         {
             return false;

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfigurationReader.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfigurationReader.java?rev=992436&r1=992435&r2=992436&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfigurationReader.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/UnitConfigurationReader.java Fri Sep  3 19:50:11 2010
@@ -35,6 +35,7 @@ import org.apache.torque.generator.confi
 import org.apache.torque.generator.configuration.option.OptionsConfiguration;
 import org.apache.torque.generator.configuration.outlet.OutletConfiguration;
 import org.apache.torque.generator.configuration.outlet.OutletConfigurationXmlParser;
+import org.apache.torque.generator.configuration.source.EntityReferences;
 import org.apache.torque.generator.option.Option;
 import org.apache.torque.generator.option.Options;
 import org.apache.torque.generator.outlet.Outlet;
@@ -140,6 +141,7 @@ class UnitConfigurationReader
         mergeInheritedOutletConfiguration(unitConfiguration, inherited);
         mergeInheritedOptionConfiguration(unitConfiguration, inherited);
         mergeInheritedOutputFiles(unitConfiguration, inherited);
+        mergeInheritedEntityRefernces(unitConfiguration, inherited);
         // target directory cannot be null and thus the current target directory
         // always overrides the inherited target directory.
 
@@ -188,6 +190,43 @@ class UnitConfigurationReader
     }
 
     /**
+     * Merges the inherited entity references with the entity references
+     * of the current unit configuration.
+     *
+     * @param unitConfiguration the current unit configuration
+     * @param inheritedConfiguration the inherited unit configuration
+     *        which entity references should be merged.
+     */
+    private void mergeInheritedEntityRefernces(
+            UnitConfiguration unitConfiguration,
+            UnitConfiguration inheritedConfiguration)
+    {
+        EntityReferences entityReferences 
+                = unitConfiguration.getEntityReferences();
+        Map<String, byte[]> inheritedReferences
+                = inheritedConfiguration.getEntityReferences()
+                    .getEntityReferences();
+        for (String systemId : inheritedReferences.keySet())
+        {
+            if (!entityReferences.containsSystemId(systemId))
+            {
+                entityReferences.addEntityReference(
+                        systemId, 
+                        inheritedReferences.get(systemId));
+                log.debug("entityReferences with system id " 
+                        + systemId
+                        + " is inherited from the parent.");
+            }
+            else
+            {
+                log.debug("entityReferences with system id " 
+                        + systemId
+                        + " is overidden in the child.");
+            }
+        }
+    }
+
+    /**
      * Merges an inherited option configuration into the option
      * configuration of the current unit configuration.
      *
@@ -310,7 +349,8 @@ class UnitConfigurationReader
                                 configurationProvider));
             }
             unitConfiguration.setOptions(options);
-
+            unitConfiguration.setEntityReferences(
+                    controlConfiguration.getEntityReferences());
         }
         if (log.isDebugEnabled())
         {

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfiguration.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfiguration.java?rev=992436&r1=992435&r2=992436&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfiguration.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfiguration.java Fri Sep  3 19:50:11 2010
@@ -41,6 +41,7 @@ import java.util.List;
 
 import org.apache.torque.generator.configuration.ConfigurationException;
 import org.apache.torque.generator.configuration.option.OptionsConfiguration;
+import org.apache.torque.generator.configuration.source.EntityReferences;
 
 
 /**
@@ -57,6 +58,10 @@ public class ControlConfiguration
     private List<OptionsConfiguration> optionsConfigurations
             = new ArrayList<OptionsConfiguration>();
 
+    /** The entity references within the generation unit. */
+    private EntityReferences entityReferences
+            = new EntityReferences();
+
     /** The loglevel during generation, not null. */
     private Loglevel loglevel = Loglevel.INFO;
 
@@ -123,6 +128,16 @@ public class ControlConfiguration
         return Collections.unmodifiableList(optionsConfigurations);
     }
 
+    /**
+     * Returns the entity references.
+     *
+     * @return the entity references, not null.
+     */
+    public EntityReferences getEntityReferences()
+    {
+        return entityReferences;
+    }
+
     @Override
     public String toString()
     {

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfigurationSaxHandler.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfigurationSaxHandler.java?rev=992436&r1=992435&r2=992436&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfigurationSaxHandler.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/controller/ControlConfigurationSaxHandler.java Fri Sep  3 19:50:11 2010
@@ -23,6 +23,7 @@ import static org.apache.torque.generato
 import static org.apache.torque.generator.configuration.controller.ControlConfigurationTags.CONTROL_TAG;
 import static org.apache.torque.generator.configuration.controller.OutputConfigurationTags.OUTPUT_TAG;
 import static org.apache.torque.generator.configuration.option.OptionTags.OPTIONS_TAG;
+import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.ENTITY_REFERENCE;
 
 import java.io.IOException;
 
@@ -37,6 +38,7 @@ import org.apache.torque.generator.confi
 import org.apache.torque.generator.configuration.option.OptionsSaxHandler;
 import org.apache.torque.generator.configuration.option.OptionsSaxHandlerFactory;
 import org.apache.torque.generator.configuration.paths.ProjectPaths;
+import org.apache.torque.generator.configuration.source.EntityReferenceSaxHandler;
 import org.xml.sax.Attributes;
 import org.xml.sax.InputSource;
 import org.xml.sax.SAXException;
@@ -61,6 +63,12 @@ public class ControlConfigurationSaxHand
     private OutputSaxHandler outputSaxHandler;
 
     /**
+     * The SAX handler which handles the entity reference declarations, or null
+     * if entity reference declarations need not currently be handled.
+     */
+    private EntityReferenceSaxHandler entityReferenceSaxHandler;
+
+    /**
      * The SAX handler which handles the options declarations, or null
      * if options declarations need not currently be handled.
      */
@@ -145,6 +153,14 @@ public class ControlConfigurationSaxHand
                     qName,
                     attributes);
         }
+        else if (entityReferenceSaxHandler != null)
+        {
+            entityReferenceSaxHandler.startElement(
+                    uri,
+                    localName,
+                    qName,
+                    attributes);
+        }
         else if (CONTROL_TAG.equals(unqualifiedName))
         {
             String loglevel = attributes.getValue(CONTROL_LOGLEVEL_ATTRIBUTE);
@@ -193,7 +209,18 @@ public class ControlConfigurationSaxHand
                     localName,
                     qName,
                     attributes);
-         }
+        }
+        else if (ENTITY_REFERENCE.equals(unqualifiedName))
+        {
+            entityReferenceSaxHandler = new EntityReferenceSaxHandler(
+                    configurationProvider,
+                    projectPaths);
+            entityReferenceSaxHandler.startElement(
+                    uri,
+                    localName,
+                    qName,
+                    attributes);
+        }
         else
         {
             throw new SAXException("Unknown element " + unqualifiedName);
@@ -238,6 +265,28 @@ public class ControlConfigurationSaxHand
         {
             outputSaxHandler.endElement(uri, localName, rawName);
         }
+        else if (entityReferenceSaxHandler != null)
+        {
+            entityReferenceSaxHandler.endElement(uri, localName, rawName);
+            if (entityReferenceSaxHandler.isFinished())
+            {
+                try
+                {
+                    controllerConfiguration.getEntityReferences()
+                        .addEntityReference(
+                                entityReferenceSaxHandler.getSystemId(),
+                                entityReferenceSaxHandler.readResource());
+                } 
+                catch (ConfigurationException e)
+                {
+                    throw new SAXException(
+                            "Could not read entity reference"
+                                + entityReferenceSaxHandler.getSystemId(),
+                            e);
+                }
+                entityReferenceSaxHandler = null;
+            }
+        }
     }
 
     /**

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/EntityReferenceSaxHandler.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/EntityReferenceSaxHandler.java?rev=992436&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/EntityReferenceSaxHandler.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/EntityReferenceSaxHandler.java Fri Sep  3 19:50:11 2010
@@ -0,0 +1,163 @@
+package org.apache.torque.generator.configuration.source;
+
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.ENTITY_REFERENCE;
+import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.RESOURCE_ATTRIBUTE;
+import static org.apache.torque.generator.configuration.source.SourceConfigurationTags.SYSTEM_ID_ATTRIBUTE;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.torque.generator.configuration.ConfigurationException;
+import org.apache.torque.generator.configuration.ConfigurationProvider;
+import org.apache.torque.generator.configuration.paths.ProjectPaths;
+import org.xml.sax.Attributes;
+import org.xml.sax.SAXException;
+import org.xml.sax.helpers.DefaultHandler;
+
+/**
+ * Reads a source transformer from the controller configuration file.
+ */
+public class EntityReferenceSaxHandler extends DefaultHandler
+{
+    /** The systemId to resolve. */
+    private String systemId;
+
+    /** The resource where to find the resolved resource. */
+    private String resource;
+
+    /** The configurationProvider. */
+    private ConfigurationProvider configurationProvider;
+
+    /**
+     * Constructor.
+     *
+     * @param configurationProvider The access object for the configuration
+     *        files, not null.
+     * @param projectPaths The paths of the surrounding project, not null.
+     *
+     * @throws NullPointerException if an argument is null.
+     */
+    public EntityReferenceSaxHandler(
+            ConfigurationProvider configurationProvider,
+            ProjectPaths projectPaths)
+    {
+        if (configurationProvider == null)
+        {
+            throw new NullPointerException("configurationProvider must not be null");
+        }
+        if (projectPaths == null)
+        {
+            throw new NullPointerException("projectPaths must not be null");
+        }
+        this.configurationProvider = configurationProvider;
+    }
+
+    /**
+     * {@inheritDoc}
+     */
+    @Override
+    public void startElement(String uri, String localName, String rawName,
+                             Attributes attributes)
+            throws SAXException
+    {
+        if (rawName.equals(ENTITY_REFERENCE))
+        {
+            resource = attributes.getValue(
+                    RESOURCE_ATTRIBUTE);
+            if (resource == null)
+            {
+                throw new SAXException(
+                        "The attribute " + RESOURCE_ATTRIBUTE
+                        + " must not be null for the element "
+                        + ENTITY_REFERENCE);
+            }
+            systemId = attributes.getValue(
+                    SYSTEM_ID_ATTRIBUTE);
+            if (systemId == null)
+            {
+                throw new SAXException(
+                        "The attribute " + SYSTEM_ID_ATTRIBUTE
+                        + " must not be null for the element "
+                        + ENTITY_REFERENCE);
+            }
+        }
+        else
+        {
+            throw new SAXException("Unknown element " + rawName);
+        }
+    }
+
+    /**
+     * Returns the parsed resource path.
+     *
+     * @return the the parsed resource path, not null if a
+     *         matching snippet was processed.
+     */
+    public String getResource()
+    {
+        return resource;
+    }
+
+    public String getSystemId()
+    {
+        return systemId;
+    }
+
+    /**
+     * Returns whether this handler has finished parsing.
+     *
+     * @return true if this handler is finished, false otherwise.
+     */
+    public boolean isFinished()
+    {
+        return (systemId != null);
+    }
+
+    /**
+     * Reads the resource defined in the parsed XML .
+     *
+     * @return the content of the resource, not null.
+     *
+     * @throws ConfigurationException if an error occurs
+     *         while reading the resource.
+     */
+    public byte[] readResource()
+        throws ConfigurationException
+    {
+        InputStream inputStream 
+                = configurationProvider.getResourceInputStream(resource);
+        byte[] content;
+        try
+        {
+            content = IOUtils.toByteArray(inputStream);
+        } 
+        catch (IOException e)
+        {
+            throw new ConfigurationException(
+                    "Could not read Stream from resource path " + resource, 
+                    e);
+        }
+        
+        return content;
+    }
+}

Added: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/EntityReferences.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/EntityReferences.java?rev=992436&view=auto
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/EntityReferences.java (added)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/EntityReferences.java Fri Sep  3 19:50:11 2010
@@ -0,0 +1,92 @@
+package org.apache.torque.generator.configuration.source;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.util.HashMap;
+import java.util.Map;
+
+import org.xml.sax.EntityResolver;
+import org.xml.sax.InputSource;
+import org.xml.sax.SAXException;
+
+/**
+ * Resolves system Ids for schema files to the schema file content.
+ */
+public class EntityReferences implements EntityResolver
+{
+    /** All known entity references. */
+    private Map<String, byte[]> entityReferences 
+            = new HashMap<String, byte[]>();
+
+    /**
+     * Adds a new entity reference.
+     * 
+     * @param systemId the systemId of the entity, not null.
+     * @param content the content of the entity, not null.
+     * 
+     * @throws NullPointerException if systemId or content are null.
+     * @throws IllegalArgumentException if the systemId is already defined.
+     */
+    public void addEntityReference(String systemId, byte[] content)
+    {
+        if (systemId == null)
+        {
+            throw new NullPointerException("systemId must not be null");
+        }
+        if (content == null)
+        {
+            throw new NullPointerException("content must not be null");
+        }
+        if (entityReferences.containsKey(systemId))
+        {
+            throw new IllegalArgumentException("systemId is already defined");
+        }
+        entityReferences.put(systemId, content);
+    }
+
+    /**
+     * Returns whether the given system id is known.
+     * 
+     * @param systemId the system id to check.
+     * 
+     * @return true if the system id can be resolved, false otherwise.
+     */
+    public boolean containsSystemId(String systemId)
+    {
+        return entityReferences.containsKey(systemId);
+    }
+
+    /**
+     * Returns a copy of the entity reference map.
+     * 
+     * @return a copy of the entity reference map.
+     */
+    public Map<String, byte[]> getEntityReferences()
+    {
+        return new HashMap<String, byte[]>(entityReferences);
+    }
+    
+    /**
+     * An implementation of the SAX <code>EntityResolver</code>
+     * interface to be called by the XML parser.  If the systemId is known,
+     * the corresponding resource from the jar is returned.
+     * In all other cases, null is returned to indicate that the parser
+     * should open a regular connection to the systemId URI.
+     *
+     * @param publicId The public identifier of the external entity
+     * @param systemId The system identifier of the external entity
+     * @return An <code>InputSource</code> for the entity if the 
+     *         systemId is known, or null otherwise.
+     */
+    public InputSource resolveEntity(String publicId, String systemId)
+            throws IOException, SAXException
+    {
+        
+        byte[] content = entityReferences.get(systemId);
+        if (content == null)
+        {
+            return null;
+        }
+        return new InputSource(new ByteArrayInputStream(content));
+    }
+}

Modified: db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/SourceConfigurationTags.java
URL: http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/SourceConfigurationTags.java?rev=992436&r1=992435&r2=992436&view=diff
==============================================================================
--- db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/SourceConfigurationTags.java (original)
+++ db/torque/torque4/trunk/torque-generator/src/main/java/org/apache/torque/generator/configuration/source/SourceConfigurationTags.java Fri Sep  3 19:50:11 2010
@@ -55,6 +55,9 @@ public final class SourceConfigurationTa
     /** Tag name for the "transformer" tag. */
     public static final String EXCLUDE_TAG = "exclude";
 
+    /** Tag name for the "entityReference" tag. */
+    public static final String ENTITY_REFERENCE = "entityReference";
+
     /** Attribute name for the "urlOption" attribute. */
     public static final String URL_OPTION_ATTRIBUTE = "urlOption";
 
@@ -69,4 +72,11 @@ public final class SourceConfigurationTa
 
     /** Attribute name for the "schemaOption" attribute. */
     public static final String SCHEMA_OPTION_ATTRIBUTE = "schemaOption";
+
+    /** Attribute name for the "systemId" attribute. */
+    public static final String SYSTEM_ID_ATTRIBUTE = "systemId";
+
+    /** Attribute name for the "resource" attribute. */
+    public static final String RESOURCE_ATTRIBUTE = "resource";
+
 }



---------------------------------------------------------------------
To unsubscribe, e-mail: torque-dev-unsubscribe@db.apache.org
For additional commands, e-mail: torque-dev-help@db.apache.org