View Javadoc

1   /*******************************************************************************
2    * Copyright (c) 2009 Ordina and committers to Mod4j
3    * All rights reserved. This program and the accompanying materials
4    * are made available under the terms of the Eclipse Public License v1.0
5    * which accompanies this distribution, and is available at
6    * http://www.eclipse.org/legal/epl-v10.html
7    *
8    * Contributors:
9    *     Ordina - initial implementation
10   *******************************************************************************/
11  package org.mod4j.dslcommon.openarchitectureware;
12  
13  import java.io.File;
14  import java.net.URL;
15  import java.util.Map;
16  
17  import org.mod4j.common.generator.admin.Mod4jTracker;
18  import org.mod4j.dslcommon.generator.helpers.ModelHelpers;
19  import org.mod4j.dslcommon.generator.helpers.StringHelpers;
20  import org.mod4j.dslcommon.io.IDirectoryVisitor;
21  
22  /**
23   * @author jos
24   */
25  public class CodegenDirectoryVisitor implements IDirectoryVisitor {
26  
27      private static final String MODEL_DIR = "src/model";
28  
29      private DslExtension dsl = null;
30  
31      private String workDir = "";
32  
33      private String propertiesFile = null;
34  
35      private String codegenWorkflow = null;
36  
37      private Map<String, String> properties = null;
38  
39      private boolean standaloneSetup;
40  
41      /**
42       * @param dsl
43       * @param theWorkDir
44       * @param standaloneSetup
45       */
46      public CodegenDirectoryVisitor(DslExtension dsl, String theWorkDir, boolean standaloneSetup) {
47          this.dsl = dsl;
48          this.workDir = theWorkDir;
49          this.standaloneSetup = standaloneSetup;
50          Mod4jTracker.getFileTracker().initResource(theWorkDir);
51          setupDsl();
52      }
53  
54      /*
55       * (non-Javadoc)
56       * 
57       * @see org.mod4j.dslcommon.io.IDirectoryVisitor#visitDirectoryBefore(java.io.File)
58       */
59      public Object visitDirectoryBefore(File directory) {
60          return null;
61      }
62  
63      /*
64       * (non-Javadoc)
65       * 
66       * @see org.mod4j.dslcommon.io.IDirectoryVisitor#visitDirectoryAfter(java.io.File)
67       */
68      public Object visitDirectoryAfter(File directory) {
69          return null;
70      }
71  
72      /*
73       * (non-Javadoc)
74       * 
75       * @see org.mod4j.dslcommon.io.IDirectoryVisitor#visitFile(java.io.File)
76       */
77      public Object visitFile(File file) throws Mod4jWorkflowException {
78          if (file.isFile() && file.getName().endsWith(dsl.getDslFileExtension())) {
79              generateCode(file);
80          }
81          return null;
82      }
83  
84      /**
85       * 
86       */
87      private void setupDsl() {
88  
89          propertiesFile = workDir + "/" + MODEL_DIR + "/" + dsl.getDslCodegenProperties();
90          File file = new File(propertiesFile);
91          if (!file.exists()) {
92              System.err.println("Mod4j: code generation properties file [" + propertiesFile
93                      + "] not found, cannot generate code for dsl " + dsl.getDslName() + ".");
94              System.exit(1);
95          }
96  
97          codegenWorkflow = dsl.getDslCodegenWorkflow();
98          ClassLoader cls = this.getClass().getClassLoader();
99          URL url = cls.getResource(codegenWorkflow);
100         if (url == null) {
101             System.err.println("Code generation oaW file [" + codegenWorkflow + "] not found");
102             System.exit(1);
103         }
104         codegenWorkflow = url.toString();
105 
106         properties = ModelHelpers.getProperties(propertiesFile);
107 
108         propertiesFile = StringHelpers.replaceAllSubstrings(propertiesFile, "\\", "/");
109 
110         properties.put("appPropFilePath", propertiesFile);
111 
112         // Get the relative applicationPath property and make it absolute
113         String applicationPath = properties.get("applicationPath");
114         String newAppPath = workDir + "/" + applicationPath;
115         properties.put("applicationPath", newAppPath);
116         properties.put("workDir", workDir);
117         properties.put("project", workDir);
118     }
119 
120     /**
121      * @param file
122      * @throws Mod4jWorkflowException
123      */
124     private void generateCode(final File file) throws Mod4jWorkflowException {
125         String modelfile = file.getAbsolutePath();
126         modelfile = StringHelpers.replaceAllSubstrings(modelfile, "\\", "/");
127         modelfile = "file:/" + modelfile;
128 
129         properties.put("modelFile", modelfile);
130         properties.put("isStandaloneSetup", standaloneSetup ? "true" : "false");
131         properties.put("isEclipseSetup", standaloneSetup ? "false" : "true");
132         
133         Mod4jWorkflowRunner genWf = new Mod4jWorkflowRunner();
134         genWf.runWorkflow(codegenWorkflow, properties);
135     }
136 
137     public DslExtension isDslFile(String filename) {
138         // TODO NEed a way to find the DslExtension info outside eclipse
139         return null;
140     }
141 
142 }