001/**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements.  See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License.  You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017package org.apache.activemq.openwire.tool;
018
019import java.io.File;
020
021import org.apache.tools.ant.BuildException;
022import org.apache.tools.ant.Project;
023import org.apache.tools.ant.Task;
024import org.codehaus.jam.JamService;
025import org.codehaus.jam.JamServiceFactory;
026import org.codehaus.jam.JamServiceParams;
027
028/**
029 *
030 */
031public class JavaGeneratorTask extends Task {
032
033    int version = 2;
034    File basedir = new File(".");
035    File outputdir = null;
036    boolean generateMarshalers = true;
037    boolean generateTests = true;
038
039    public static void main(String[] args) {
040
041        Project project = new Project();
042        project.init();
043        JavaGeneratorTask generator = new JavaGeneratorTask();
044        generator.setProject(project);
045
046        if (args.length > 0) {
047            generator.version = Integer.parseInt(args[0]);
048        }
049
050        if (args.length > 1) {
051            generator.basedir = new File(args[1]);
052        }
053
054        generator.execute();
055    }
056
057    @Override
058    public void execute() throws BuildException {
059        try {
060
061            String sourceDir = basedir + "/src/main/java";
062
063            System.out.println("Parsing source files in: " + sourceDir);
064
065            JamServiceFactory jamServiceFactory = JamServiceFactory.getInstance();
066            JamServiceParams params = jamServiceFactory.createServiceParams();
067            File[] dirs = new File[] {
068                new File(sourceDir)
069            };
070            params.includeSourcePattern(dirs, "**/*.java");
071            JamService jam = jamServiceFactory.createService(params);
072
073            File outputBase = outputdir != null ? outputdir : basedir;
074
075            if (generateMarshalers) {
076                JavaMarshallingGenerator script = new JavaMarshallingGenerator();
077                script.setJam(jam);
078                script.setTargetDir(outputBase + "/src/main/java");
079                script.setOpenwireVersion(version);
080                script.run();
081            }
082
083            if (generateTests) {
084                JavaTestsGenerator script = new JavaTestsGenerator();
085                script.setJam(jam);
086                script.setTargetDir(outputBase + "/src/test/java");
087                script.setOpenwireVersion(version);
088                script.run();
089            }
090
091        } catch (Exception e) {
092            throw new BuildException(e);
093        }
094    }
095
096    public int getVersion() {
097        return version;
098    }
099
100    public void setVersion(int version) {
101        this.version = version;
102    }
103
104    public File getBasedir() {
105        return basedir;
106    }
107
108    public void setBasedir(File basedir) {
109        this.basedir = basedir;
110    }
111
112    public File getOutputdir() {
113        return outputdir;
114    }
115
116    public void setOutputdir(File outputdir) {
117        this.outputdir = outputdir;
118    }
119
120    public boolean isGenerateMarshalers() {
121        return generateMarshalers;
122    }
123
124    public void setGenerateMarshalers(boolean generateMarshalers) {
125        this.generateMarshalers = generateMarshalers;
126    }
127
128    public boolean isGenerateTests() {
129        return generateTests;
130    }
131
132    public void setGenerateTests(boolean generateTests) {
133        this.generateTests = generateTests;
134    }
135}