summaryrefslogtreecommitdiffstats
path: root/tags/java/sca/1.5.1-RC1/itest/databindings/common/src/main/java/org/apache/tuscany/sca/itest/generate/Generate.java
blob: adb546a1eafac3d5c792085cd13bfc05ae8ced0e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * 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.    
 */
package org.apache.tuscany.sca.itest.generate;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileWriter;
import java.util.List;
import java.util.Properties;

import org.apache.tuscany.generate.GenerateFactory;
import org.apache.tuscany.generate.GenerateType;
import org.apache.tuscany.generate.InputFileType;
import org.apache.tuscany.generate.TemplateType;
import org.apache.tuscany.sdo.generate.XSD2JavaGenerator;
import org.apache.tuscany.sdo.util.SDOUtil;
import org.apache.velocity.VelocityContext;
import org.apache.velocity.app.Velocity;

import commonj.sdo.helper.HelperContext;
import commonj.sdo.helper.XMLDocument;

/**
 * Generates test files based on the information in a configuration files (generate.xml)
 * and a set of velocity templates. The process is
 *
 * for each template
 *     for each xsd file
 *         generate SDOs
 *         include the factory into the composite
 *         for each type
 *             add client iface method
 *             add client impl method
 *             add service iface method
 *             add service impl method
 *             add test method
 *             add wsdl type and method
 *
 * @version $Rev$ $Date$
 */
public class Generate {
	
    /**
     * Does all the hard work of running the velocity templates against the 
     * the list of types to test. Both the list of templates and the list of 
     * XSD files is held in the configuration file (generate.xsd) which lives in the
     * resources/generate directory of the project being generated. 
     *
     * @param projectBuildDir the path to the target dir of the project being generated. 
     */
    public static void generate(String projectBuildDir) {
        System.out.println(">> Building project from dir: " + projectBuildDir);
        FileInputStream fis = null;

        try {
            // Load the config file into a stream
            fis = new FileInputStream(projectBuildDir + "/classes/generate/generate.xml");

            // Load the stream into SDO
            // We are just using SDO as a convenient way to parse the XML config file
            HelperContext scope       = SDOUtil.createHelperContext();
            GenerateFactory.INSTANCE.register(scope);
            XMLDocument xmlDoc        = scope.getXMLHelper().load(fis);
            GenerateType generateType = (GenerateType)xmlDoc.getRootObject();

            // Get the file list. This is the list of XSD that is passed into the 
            // the velocity templates. Each configured file holds a list of types
            // that the velocity templates expand into appropriate methods and method calls           
            List fileList = generateType.getInputFile();
            
            //Initialise velocity ready to generate the various files
            Properties p = new Properties();
            p.setProperty("file.resource.loader.path", projectBuildDir + "/classes/generate");            
            Velocity.init(p);
            VelocityContext context = new VelocityContext();
            context.put("fileList", fileList);
            
            List templateList = generateType.getTemplate();

            // For each velocity template in the template list pass in the XSD file list
            for ( Object item: templateList){
            	TemplateType template = (TemplateType)item;
                context.put("template", template);
            	String tmp = template.getTemplateName();
            	String filename = projectBuildDir + "/" + template.getTemplateTargetDir() + "/" + tmp.substring(0,tmp.length() - 3);
            	File f = new File(filename);
            	// Create folders since the package doesn't exist before the code-gen for the 1st time
            	f.getParentFile().mkdirs();
                FileWriter fw = new FileWriter(f);
            	System.out.println(">> Processing " + template.getTemplateName() + " to " + filename);
                Velocity.mergeTemplate(template.getTemplateName(), context, fw );
                fw.flush();
                fw.close();
            } 


        } catch (Exception e) {
            System.out.println("Exception : " + e.toString());
            e.printStackTrace();
            return;
        }        
    }
    
    /**
     * The SDO generator tool does all of the hard work
     *
     * @param projectBuildDir the path to the target dir of the project being generated. 
     */
    public static void generateSDO(String projectBuildDir) {
        System.out.println(">> Building SDOs from dir: " + projectBuildDir);
        FileInputStream fis = null;

        try {
            // Load the config file into a stream
            fis = new FileInputStream(projectBuildDir + "/classes/generate/generate.xml");

            // Load the stream into SDO
            // We are just using SDO as a convenient way to parse the XML config file
            HelperContext scope       = SDOUtil.createHelperContext();
            GenerateFactory.INSTANCE.register(scope);
            XMLDocument xmlDoc        = scope.getXMLHelper().load(fis);
            GenerateType generateType = (GenerateType)xmlDoc.getRootObject();

            // Get the file list. This is the list of XSD that is passed into the 
            // the velocity templates. Each configured file holds a list of types
            // that the velocity templates expand into appropriate methods and method calls           
            List fileList = generateType.getInputFile();
                        
            // for each XSD in the XSD file list generate an SDO.
            XSD2JavaGenerator generator = new XSD2JavaGenerator();
            
            for ( Object item : fileList){
                InputFileType file = (InputFileType)item;
                
                XSD2JavaGenerator.generateFromXMLSchema(projectBuildDir + "/classes/xsd/" + file.getFileName(), 
                                                        file.getNamespace(), 
                                                        projectBuildDir + "/sdo-source", 
                                                        file.getJavaPackage(), 
                                                        null, //file.getPrefix(), 
                                                        XSD2JavaGenerator.OPTION_NO_CONTAINMENT | XSD2JavaGenerator.OPTION_NO_NOTIFICATION | XSD2JavaGenerator.OPTION_NO_UNSETTABLE);
                             
            }

        } catch (Exception e) {
            System.out.println("Exception : " + e.toString());
            e.printStackTrace();
            return;
        }        
    }    

    /**
     * The mainline
     * 
     * @param args the target directory where project in which files are being generated
     */
    public static void main(String[] args) {
        
        Generate.generate(args[0]);
        
        if (args.length > 1){
            Generate.generateSDO(args[0]);
        } 
    }

}