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