Author: tfischer
Date: Thu Apr 12 01:35:00 2012
New Revision: 1325095
URL: http://svn.apache.org/viewvc?rev=1325095&view=rev
Log:
TORQUE-183 add possibility to include other schema files
TORQUE-189 schema changes for inclusion of other schema files
Added:
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/IncludeSchemaTransformer.java
Modified:
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/TorqueSchemaElementName.java
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMTransformer.java
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/xsd/database-4-0.xsd
Modified:
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/TorqueSchemaElementName.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/TorqueSchemaElementName.java?rev=1325095&r1=1325094&r2=1325095&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/TorqueSchemaElementName.java
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/TorqueSchemaElementName.java
Thu Apr 12 01:35:00 2012
@@ -30,8 +30,12 @@ public enum TorqueSchemaElementName impl
{
/** element database. */
DATABASE("database"),
+ /** element include-schema */
+ INCLUDE_SCHEMA("include-schema"),
/** element external-schema */
EXTERNAL_SCHEMA("external-schema"),
+ /** element domain. */
+ DOMAIN("domain"),
/** element table. */
TABLE("table"),
/** element view. */
Added:
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/IncludeSchemaTransformer.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/IncludeSchemaTransformer.java?rev=1325095&view=auto
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/IncludeSchemaTransformer.java
(added)
+++
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/IncludeSchemaTransformer.java
Thu Apr 12 01:35:00 2012
@@ -0,0 +1,150 @@
+package org.apache.torque.templates.transformer.om;
+
+/*
+ * 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 java.io.File;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.torque.generator.control.ControllerState;
+import org.apache.torque.generator.source.SourceElement;
+import org.apache.torque.generator.source.SourceException;
+import org.apache.torque.generator.source.stream.FileSource;
+import org.apache.torque.generator.source.stream.XmlSourceFormat;
+import org.apache.torque.generator.source.transform.SourceTransformer;
+import org.apache.torque.generator.source.transform.SourceTransformerException;
+import org.apache.torque.templates.TorqueSchemaAttributeName;
+import org.apache.torque.templates.TorqueSchemaElementName;
+
+/**
+ * A SourceTransformer which includes other schemata into the current graph.
+ *
+ * @version $Id: $
+ */
+public class IncludeSchemaTransformer implements SourceTransformer
+{
+ /** The class log. */
+ private static Log log
+ = LogFactory.getLog(IncludeSchemaTransformer.class);
+
+ /**
+ * The base dir for the external schema,
+ * or null to compute from the current source file.
+ */
+ private File baseDir;
+
+ /**
+ * Standard constructor.
+ */
+ public IncludeSchemaTransformer()
+ {
+ }
+
+ /**
+ * Constructor to override base dir.
+ *
+ * @param baseDir the new base dir.
+ */
+ public IncludeSchemaTransformer(File baseDir)
+ {
+ this.baseDir = baseDir;
+ }
+
+ /**
+ * Loads the external schemata tables into the current graph.
+ * The external database element is added as child of the
+ * external-schema element.
+ * Also, an all-tables child element is added to the root element,
+ * which is filled with all tables from the external schema plus its own
+ * tables.
+ *
+ * @param root the database root element of the source tree, not null.
+ * @param controllerState the controller state, not null.
+ *
+ * @throws SourceTransformerException if the transformation fails.
+ */
+ public SourceElement transform(
+ SourceElement root,
+ ControllerState controllerState)
+ throws SourceTransformerException
+ {
+ List<SourceElement> includeSchemaList
+ = root.getChildren(TorqueSchemaElementName.INCLUDE_SCHEMA);
+ List<SourceElement> childrenList = root.getChildren();
+
+ for (SourceElement includeSchemaElement : includeSchemaList)
+ {
+ File includeSchemaBaseDir;
+ if (this.baseDir == null)
+ {
+ File currentSourceFile = controllerState.getSourceFile();
+ includeSchemaBaseDir = currentSourceFile.getParentFile();
+ }
+ else
+ {
+ includeSchemaBaseDir = baseDir;
+ }
+ String relativePath = includeSchemaElement.getAttribute(
+ TorqueSchemaAttributeName.FILENAME)
+ .toString();
+ File includeSchemaPath
+ = new File(includeSchemaBaseDir, relativePath);
+ log.trace("Trying to read included file " + includeSchemaPath);
+ try
+ {
+ FileSource fileSource = new FileSource(
+ new XmlSourceFormat(),
+ includeSchemaPath,
+ controllerState);
+ SourceElement includeSchemaRootElement
+ = fileSource.getRootElement();
+ log.trace("successfully read included file "
+ + includeSchemaPath);
+
+ this.transform(includeSchemaRootElement, controllerState);
+
+ // disattach children from their current parent
+ // so that the new parent is the primary parent
+ List<SourceElement> toIncludeList
+ = new ArrayList<SourceElement>(
+ includeSchemaRootElement.getChildren());
+ for (SourceElement childToInclude : toIncludeList)
+ {
+ childToInclude.getParents().clear();
+ }
+
+ childrenList.addAll(toIncludeList);
+
+ log.trace("finished processing included file "
+ + includeSchemaPath);
+ }
+ catch (SourceException e)
+ {
+ log.error("Could not construct source from schema file "
+ + includeSchemaPath,
+ e);
+ throw new SourceTransformerException(e);
+ }
+ }
+ return root;
+ }
+}
Modified:
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMTransformer.java
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMTransformer.java?rev=1325095&r1=1325094&r2=1325095&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMTransformer.java
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/java/org/apache/torque/templates/transformer/om/OMTransformer.java
Thu Apr 12 01:35:00 2012
@@ -43,6 +43,7 @@ import org.apache.torque.templates.trans
*
* This transformer calls the following other transformers on the source tree:
* <ul>
+ * <li>IncludeSchemaTransformer on the root node</li>
* <li>LoadExternalSchemaTransformer on the root node</li>
* <li>OMTableAndViewTransformer on all tables and views</li>
* <li>OMForeignKeyColumnTransformer on all columns</li>
@@ -87,6 +88,14 @@ public class OMTransformer implements So
private static final SourceTransformer loadExternalSchemaTransformer
= new LoadExternalSchemaTransformer();
+ /**
+ * The transformer which includes the included schemata.
+ *
+ * @see LoadExternalSchemaTransformer
+ */
+ private static final SourceTransformer includeSchemaTransformer
+ = new IncludeSchemaTransformer();
+
static
{
try
@@ -119,6 +128,8 @@ public class OMTransformer implements So
root,
(String) root.getAttribute(TorqueSchemaAttributeName.NAME),
controllerState);
+ // include included schemata
+ includeSchemaTransformer.transform(root, controllerState);
// load referenced external schemata
loadExternalSchemaTransformer.transform(root, controllerState);
Modified:
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/xsd/database-4-0.xsd
URL:
http://svn.apache.org/viewvc/db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/xsd/database-4-0.xsd?rev=1325095&r1=1325094&r2=1325095&view=diff
==============================================================================
---
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/xsd/database-4-0.xsd
(original)
+++
db/torque/torque4/trunk/torque-templates/src/main/resources/org/apache/torque/templates/xsd/database-4-0.xsd
Thu Apr 12 01:35:00 2012
@@ -72,11 +72,20 @@ A set of key/value options to be passed
</xs:annotation>
</xs:element>
+ <xs:element name="include-schema" type="includeSchemaType"
+ minOccurs="0" maxOccurs="unbounded" >
+ <xs:annotation>
+ <xs:documentation xml:lang="en">
+Include another schema file in this schema (as if it were a part of this
+schema file).
+ </xs:documentation>
+ </xs:annotation>
+ </xs:element>
<xs:element name="external-schema" type="externalSchemaType"
minOccurs="0" maxOccurs="unbounded" >
<xs:annotation>
<xs:documentation xml:lang="en">
-Include another schema file.
+Reference another schema file from this schema (outbound connections only).
</xs:documentation>
</xs:annotation>
</xs:element>
@@ -183,6 +192,19 @@ templates.
</xs:complexType>
<!-- =====================================
+ include-schema element definition
+ ===================================== -->
+ <xs:complexType name="includeSchemaType" >
+ <xs:annotation>
+ <xs:documentation xml:lang="en">
+Include another schema file in this schema (as if it were a part of this
+schema file).
+ </xs:documentation>
+ </xs:annotation>
+ <xs:attribute name="filename" type="xs:string" use="required" />
+ </xs:complexType>
+
+<!-- =====================================
external-schema element definition
===================================== -->
<xs:complexType name="externalSchemaType" >
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]