]> gerrit.simantics Code Review - simantics/platform.git/blob
25758343d59751d3dd2a5d28d2aac8d950485ebe
[simantics/platform.git] /
1 package org.simantics.graph.compiler.internal.resourceFiles;
2
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5 import java.io.OutputStreamWriter;
6 import java.io.StringWriter;
7 import java.io.Writer;
8 import java.net.URL;
9 import java.util.Arrays;
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.List;
13
14 import org.simantics.graph.IResourceFile;
15
16 import freemarker.cache.URLTemplateLoader;
17 import freemarker.template.Configuration;
18 import freemarker.template.DefaultObjectWrapper;
19 import freemarker.template.Template;
20
21 public class ResourceFile implements IResourceFile {
22         String packageName;
23         String className;
24         List<ResourceRef> resources;
25         
26         public ResourceFile(String packageName, String className,
27                         List<ResourceRef> resources) {
28                 this.packageName = packageName;
29                 this.className = className;
30                 this.resources = resources;
31         }
32
33         /* (non-Javadoc)
34          * @see org.simantics.graph.resourceFiles.IResourceFile#getPackageName()
35          */
36         @Override
37         public String getPackageName() {
38                 return packageName;
39         }
40         
41         /* (non-Javadoc)
42          * @see org.simantics.graph.resourceFiles.IResourceFile#getClassName()
43          */
44         @Override
45         public String getClassName() {
46                 return className;
47         }
48         
49         public Collection<ResourceRef> getResources() {
50                 return resources;
51         }
52         
53         private static Configuration configuration = null;
54         private static Configuration getConfiguration() {
55                 if(configuration == null) {
56                         configuration = new Configuration();
57                         configuration.setTemplateLoader(
58                                         new URLTemplateLoader() {                                               
59                                                 @Override
60                                                 protected URL getURL(String name) {
61                                                         return ResourceFile.class.getResource(name);
62                                                 }
63                                         });
64                         configuration.setObjectWrapper(new DefaultObjectWrapper());
65                 }
66                 return configuration;
67         }
68         
69         /* (non-Javadoc)
70          * @see org.simantics.graph.resourceFiles.IResourceFile#write(java.io.Writer)
71          */
72         @Override
73         public void write(Writer writer) {
74                 try {
75                         Template temp = 
76                                 getConfiguration().getTemplate("resourceFile.ftl");
77                          
78                         temp.process(this, writer);
79                 } catch (Exception e) {
80                         // TODO Auto-generated catch block
81                         e.printStackTrace();
82                 }  
83         }
84         
85         /* (non-Javadoc)
86          * @see org.simantics.graph.resourceFiles.IResourceFile#getContent()
87          */
88         @Override
89         public InputStream getContent() {
90                 StringWriter writer = new StringWriter();
91                 write(writer);
92                 return new ByteArrayInputStream(writer.toString().getBytes());
93         }
94         
95         /* (non-Javadoc)
96          * @see org.simantics.graph.resourceFiles.IResourceFile#getFileName()
97          */
98         @Override
99         public String getFileName() {
100                 return "src/" + packageName.replace('.', '/') + "/" + className + ".java";
101         }
102         
103         public void sort() {
104                 Collections.sort(resources);
105         }
106         
107         public static void main(String[] args) {
108                 ResourceRef Foo = new ResourceRef("Foo", "http://www.dsf.sdf/Foo");
109                 ResourceRef Bar = new ResourceRef("Foo", "http://www.dsf.sdf/Bar");
110                 Bar.deprecated = true;
111                 new ResourceFile("org.simantics.graph", "Testi", Arrays.<ResourceRef>asList(
112                                 Foo, Bar
113                         )).write(new OutputStreamWriter(System.out));
114         }
115 }