removed test class writer and code
[owl2java:owl2java.git] / src / main / java / owl2java / core / generator / jena / JenaWriter.java
1 package owl2java.core.generator.jena;
2
3 import java.text.SimpleDateFormat;
4 import java.util.Calendar;
5 import java.util.Iterator;
6
7 import org.apache.commons.logging.Log;
8 import org.apache.commons.logging.LogFactory;
9 import org.apache.velocity.VelocityContext;
10
11 import owl2java.core.generator.AbstractWriter;
12 import owl2java.core.model.jmodel.JClass;
13 import owl2java.core.model.jmodel.JModel;
14 import owl2java.core.model.jmodel.utils.NamingUtils;
15 import owl2java.core.model.xsd.XsdMapTestData;
16
17
18 public class JenaWriter extends AbstractWriter {
19         
20         static Log log = LogFactory.getLog(JenaWriter.class);
21         public static final String templateDirJena = "jenaTemplates"; 
22
23         private String vocabularyName;
24         private String factoryName;
25         private String testClassName;
26         
27         public static String getTemplatePath(String templateName) {
28                 return "/" + templateDirJena + "/" + templateName;
29         }
30
31
32         public void generate(JModel model, String baseDir, String basePackage) {
33                 this.baseDir = baseDir;
34                 this.jmodel = model;
35                 this.basePackage = basePackage;
36
37                 log.info("");
38                 log.info("Writing JModel to java");
39
40                 // create the package structure
41                 createPackageDirectories();
42
43                 // init the templating engine
44                 initVelocityEngine(templateDirJena);
45
46                 // write interfaces
47                 createInterfaces();
48
49                 // write classes
50                 createClasses();
51
52                 // write vocabulary
53                 createVocabulary();
54
55                 // write factory
56                 createFactory();
57                 
58                 // write factory
59                 createReificationProvider();
60
61         }
62
63         protected void createFactory() {
64                 log.info("Creating Factory");
65                 JenaFactoryWriter fWriter = new JenaFactoryWriter(vEngine, getBaseVelocityContext());
66                 fWriter.setFactoryName(factoryName);
67                 fWriter.setToolsPackage(toolsPackage);
68                 fWriter.writeFactory(jmodel, baseDir, basePackage);
69         }
70         
71         protected void createReificationProvider() {
72                 log.info("Creating ReificationProvider");
73                 JenaReificationProviderWriter fWriter = new JenaReificationProviderWriter(vEngine, getBaseVelocityContext());
74                 fWriter.setFactoryName(factoryName);
75                 fWriter.setToolsPackage(toolsPackage);
76                 fWriter.writeFactory(jmodel, baseDir, basePackage);
77         }
78
79         protected void createVocabulary() {
80                 log.info("Creating vocabulary");
81                 JenaVocabularyWriter vWriter = new JenaVocabularyWriter(vEngine, getBaseVelocityContext());
82                 vWriter.setVocabularyName(vocabularyName);
83                 vWriter.setToolsPackage(toolsPackage);
84                 vWriter.writeVocabulary(jmodel, baseDir, basePackage);
85         }
86
87
88         protected VelocityContext getBaseVelocityContext() {
89                 // add some default stuff to our context. These are reused over all writers
90                 VelocityContext vContext = new VelocityContext();
91                 Calendar c = Calendar.getInstance();
92                 vContext.put("now", SimpleDateFormat.getInstance().format(c.getTime()));
93                 vContext.put("pkgBase", basePackage);
94                 vContext.put("pkgTools", toolsPackage);
95                 vContext.put("jmodel", jmodel);
96                 vContext.put("factoryName", factoryName);
97                 vContext.put("factoryPkg", NamingUtils.getJavaPackageName(basePackage, toolsPackage));
98                 vContext.put("vocabName", vocabularyName);
99                 vContext.put("vocabPkg", NamingUtils.getJavaPackageName(basePackage, toolsPackage));
100                 vContext.put("testcaseName", testClassName);
101                 vContext.put("testcasePkg", NamingUtils.getJavaPackageName(basePackage, toolsPackage));
102                 XsdMapTestData xsdMap = new XsdMapTestData();
103                 vContext.put("xsdMap", xsdMap);
104                 return vContext;
105         }
106
107         protected void createInterfaces() {
108                 log.info("Creating java interfaces");
109
110                 JenaInterfaceWriter ifaceWriter = new JenaInterfaceWriter(vEngine, getBaseVelocityContext());
111
112                 Iterator<JClass> clsIt = jmodel.listJClasses().iterator();
113                 while (clsIt.hasNext()) {
114                         JClass cls = clsIt.next();
115                         ifaceWriter.writeInterface(jmodel, cls, baseDir);
116                 }
117         }
118
119         protected void createClasses() {
120                 log.info("Creating java classes");
121                 JenaClassWriter clsWriter = new JenaClassWriter(vEngine, getBaseVelocityContext());
122
123                 Iterator<JClass> clsIt = jmodel.listJClasses().iterator();
124                 while (clsIt.hasNext()) {
125                         JClass cls = clsIt.next();
126                         clsWriter.writeClass(jmodel, cls, baseDir);
127                 }
128
129         }
130
131         public void setVocabularyName(String vocabularyName) {
132                 this.vocabularyName = vocabularyName;
133         }
134
135
136         public void setFactoryName(String factoryName) {
137                 this.factoryName = factoryName;
138         }
139
140 }