Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.samples » vxsdb » [javadoc | source]

    1   /* Copyright 2003-2004 The Apache Software Foundation
    2    *
    3    * Licensed under the Apache License, Version 2.0 (the "License");
    4    * you may not use this file except in compliance with the License.
    5    * You may obtain a copy of the License at
    6    *
    7    *     http://www.apache.org/licenses/LICENSE-2.0
    8    *
    9    * Unless required by applicable law or agreed to in writing, software
   10    * distributed under the License is distributed on an "AS IS" BASIS,
   11    * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
   12    * See the License for the specific language governing permissions and
   13    * limitations under the License.
   14    */
   15   package org.apache.xmlbeans.samples.vxsdb;
   16   
   17   import java.io.File;
   18   import java.io.FileWriter;
   19   import java.math.BigInteger;
   20   import java.util.ArrayList;
   21   import java.util.HashMap;
   22   import java.util.List;
   23   import java.util.Map;
   24   
   25   import org.apache.commons.logging.Log;
   26   import org.apache.commons.logging.LogFactory;
   27   import org.apache.tools.ant.BuildException;
   28   import org.apache.tools.ant.Task;
   29   import org.apache.velocity.Template;
   30   import org.apache.velocity.VelocityContext;
   31   import org.apache.velocity.app.Velocity;
   32   import org.apache.xmlbeans.SchemaGlobalElement;
   33   import org.apache.xmlbeans.SchemaProperty;
   34   import org.apache.xmlbeans.SchemaTypeSystem;
   35   import org.apache.xmlbeans.XmlBeans;
   36   import org.apache.xmlbeans.XmlObject;
   37   
   38   /**
   39    * @author Philip Mark Donaghy
   40    */
   41   public class VelocityXmlBeansDB extends Task {
   42   
   43       private static final Log log = LogFactory.getLog(VelocityXmlBeansDB.class);
   44   
   45       private String template;
   46   
   47       private String output;
   48   
   49       private String schema;
   50   
   51       /**
   52        * @param output
   53        *            The output to set.
   54        */
   55       public void setOutput(String output) {
   56           this.output = output;
   57       }
   58   
   59       /**
   60        * @param schema
   61        *            The schema to set.
   62        */
   63       public void setSchema(String schema) {
   64           this.schema = schema;
   65       }
   66   
   67       /**
   68        * @param template
   69        *            The template to set.
   70        */
   71       public void setTemplate(String template) {
   72           this.template = template;
   73       }
   74   
   75       /**
   76        * Default Constructor
   77        */
   78       public VelocityXmlBeansDB() {
   79       }
   80   
   81       /**
   82        * Puts the XmlBeans SchemaTypeSystem into the Velocity Context
   83        */
   84       public void execute() throws BuildException {
   85   
   86           // Create a Velocity Context and a Velocity Template
   87           VelocityContext ctx = new VelocityContext();
   88           Template template = null;
   89   
   90           // Output to a file
   91           FileWriter writer = null;
   92   
   93           // XmlBeans
   94           SchemaTypeSystem schemaTypeSystem = null;
   95   
   96           try {
   97   
   98               // Initialize Velocity
   99               Velocity.init();
  100               log.info("Using the Velocity template, " + this.template);
  101               template = Velocity.getTemplate(this.template);
  102   
  103               // Create Schema Type System
  104               log.info("Using the xml schema, " + this.schema);
  105               schemaTypeSystem = XmlBeans.compileXsd(
  106                       new XmlObject[] { XmlBeans.typeLoaderForClassLoader(this.getClass().getClassLoader()).
  107                               parse(new File(this.schema), null, null) },
  108   					XmlBeans.getBuiltinTypeSystem(),
  109                       null);
  110   
  111               // Place SchemaTypeSystem in the Velocity Context
  112               ctx.put("xsd", schemaTypeSystem);
  113   
  114               // Place a exported key Map in the Velocity Context
  115               ctx.put("exportedKeyMap", createExportedKeyMap(schemaTypeSystem));
  116   
  117               // Write to the file
  118               log.info("Using the output file, " + this.output);
  119               writer = new FileWriter(new File(this.output));
  120               template.merge(ctx, writer);
  121               writer.close();
  122   
  123           } catch (Exception e) {
  124               throw new BuildException(e);
  125           }
  126       }
  127   
  128       /**
  129        * 
  130        * @param sts
  131        * @return
  132        */
  133       private Map createExportedKeyMap(SchemaTypeSystem sts) {
  134           
  135           // Map of exported keys (foreign keys)
  136           // The key is the name of the element exporting key(s)
  137           // The value is a List of tables importing this key
  138           Map exportedKeyMap = new HashMap();
  139   
  140           // For all global elements and all global types
  141           // Create a map of exported key lists
  142           SchemaGlobalElement[] globals = sts.globalElements();
  143           for (int i = 0; i < globals.length; i++) {
  144               processProperties(globals[i].getName().getLocalPart().toUpperCase(), globals[i].getType().getProperties(), exportedKeyMap);
  145           }
  146           return exportedKeyMap;
  147       }
  148   
  149       private void processProperties(String tableName, SchemaProperty[] properties, Map exportedKeyMap) {
  150           // For all properties
  151           for (int i = 0; i < properties.length; i++) {
  152               processProperty(tableName, properties[i], exportedKeyMap);
  153           }
  154       }
  155   
  156       private void processProperty(String tableName, SchemaProperty property, Map exportedKeyMap) {
  157           // If property maxOccurs is greater than one or unbounded (null)
  158           if (property.getMaxOccurs() == null || property.getMaxOccurs().compareTo(BigInteger.ONE) > 0) {
  159               
  160               // Tables that import this properties type (ex. line-item type exports a foreign key to purchase-order,
  161               // PURCHASE_ORDER is in the list of importers) 
  162               List importers = (List) exportedKeyMap.get(property.getType().getName().toString());
  163               if (importers == null) {
  164                   importers = new ArrayList();
  165                   exportedKeyMap.put(property.getType().getName().toString(), importers);
  166               }
  167               importers.add(tableName);
  168           }
  169       }
  170   
  171       /**
  172        * @param args
  173        */
  174       public static void main(String[] args) {
  175           VelocityXmlBeansDB beans = new VelocityXmlBeansDB();
  176   
  177           // Verify arguments
  178           if (args.length < 3) {
  179               log
  180                       .error("Usage : java org.apache.xmlbeans.samples.vxsdb.VelocityXmlBeansDB TEMPLATE OUTPUT SCHEMA1 [SCHEMA2] [...]");
  181               System.exit(1);
  182           }
  183           beans.setTemplate(args[0]);
  184           beans.setOutput(args[1]);
  185           beans.setSchema(args[2]);
  186           try {
  187               beans.execute();
  188           } catch (Exception e) {
  189               e.printStackTrace();
  190           }
  191       }
  192   }

Home » xmlbeans-2.5.0-src » org.apache.xmlbeans.samples » vxsdb » [javadoc | source]