1 package org.simantics.graph.compiler.internal.resourceFiles;
\r
3 import java.io.ByteArrayInputStream;
\r
4 import java.io.InputStream;
\r
5 import java.io.OutputStreamWriter;
\r
6 import java.io.StringWriter;
\r
7 import java.io.Writer;
\r
9 import java.util.Arrays;
\r
10 import java.util.Collection;
\r
11 import java.util.Collections;
\r
12 import java.util.List;
\r
14 import org.simantics.graph.IResourceFile;
\r
16 import freemarker.cache.URLTemplateLoader;
\r
17 import freemarker.template.Configuration;
\r
18 import freemarker.template.DefaultObjectWrapper;
\r
19 import freemarker.template.Template;
\r
21 public class ResourceFile implements IResourceFile {
\r
24 List<ResourceRef> resources;
\r
26 public ResourceFile(String packageName, String className,
\r
27 List<ResourceRef> resources) {
\r
28 this.packageName = packageName;
\r
29 this.className = className;
\r
30 this.resources = resources;
\r
34 * @see org.simantics.graph.resourceFiles.IResourceFile#getPackageName()
\r
37 public String getPackageName() {
\r
42 * @see org.simantics.graph.resourceFiles.IResourceFile#getClassName()
\r
45 public String getClassName() {
\r
49 public Collection<ResourceRef> getResources() {
\r
53 private static Configuration configuration = null;
\r
54 private static Configuration getConfiguration() {
\r
55 if(configuration == null) {
\r
56 configuration = new Configuration();
\r
57 configuration.setTemplateLoader(
\r
58 new URLTemplateLoader() {
\r
60 protected URL getURL(String name) {
\r
61 return ResourceFile.class.getResource(name);
\r
64 configuration.setObjectWrapper(new DefaultObjectWrapper());
\r
66 return configuration;
\r
70 * @see org.simantics.graph.resourceFiles.IResourceFile#write(java.io.Writer)
\r
73 public void write(Writer writer) {
\r
76 getConfiguration().getTemplate("resourceFile.ftl");
\r
78 temp.process(this, writer);
\r
79 } catch (Exception e) {
\r
80 // TODO Auto-generated catch block
\r
81 e.printStackTrace();
\r
86 * @see org.simantics.graph.resourceFiles.IResourceFile#getContent()
\r
89 public InputStream getContent() {
\r
90 StringWriter writer = new StringWriter();
\r
92 return new ByteArrayInputStream(writer.toString().getBytes());
\r
96 * @see org.simantics.graph.resourceFiles.IResourceFile#getFileName()
\r
99 public String getFileName() {
\r
100 return "src/" + packageName.replace('.', '/') + "/" + className + ".java";
\r
103 public void sort() {
\r
104 Collections.sort(resources);
\r
107 public static void main(String[] args) {
\r
108 ResourceRef Foo = new ResourceRef("Foo", "http://www.dsf.sdf/Foo");
\r
109 ResourceRef Bar = new ResourceRef("Foo", "http://www.dsf.sdf/Bar");
\r
110 Bar.deprecated = true;
\r
111 new ResourceFile("org.simantics.graph", "Testi", Arrays.<ResourceRef>asList(
\r
113 )).write(new OutputStreamWriter(System.out));
\r