1 package org.simantics.graph.compiler.internal.resourceFiles;
3 import java.io.ByteArrayInputStream;
4 import java.io.InputStream;
5 import java.io.OutputStreamWriter;
6 import java.io.StringWriter;
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;
15 import org.simantics.graph.IResourceFile;
17 import freemarker.cache.URLTemplateLoader;
18 import freemarker.template.Configuration;
19 import freemarker.template.DefaultObjectWrapper;
20 import freemarker.template.Template;
22 public class ResourceFile implements IResourceFile {
23 private final static Charset UTF8 = Charset.forName("UTF-8");
27 List<ResourceRef> resources;
29 public ResourceFile(String packageName, String className,
30 List<ResourceRef> resources) {
31 this.packageName = packageName;
32 this.className = className;
33 this.resources = resources;
37 * @see org.simantics.graph.resourceFiles.IResourceFile#getPackageName()
40 public String getPackageName() {
45 * @see org.simantics.graph.resourceFiles.IResourceFile#getClassName()
48 public String getClassName() {
52 public Collection<ResourceRef> getResources() {
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() {
63 protected URL getURL(String name) {
64 return ResourceFile.class.getResource(name);
67 configuration.setObjectWrapper(new DefaultObjectWrapper());
73 * @see org.simantics.graph.resourceFiles.IResourceFile#write(java.io.Writer)
76 public void write(Writer writer) {
79 getConfiguration().getTemplate("resourceFile.ftl");
81 temp.process(this, writer);
82 } catch (Exception e) {
83 // TODO Auto-generated catch block
89 * @see org.simantics.graph.resourceFiles.IResourceFile#getContent()
92 public InputStream getContent() {
93 StringWriter writer = new StringWriter();
94 write(new FilterCRWriter(writer));
95 return new ByteArrayInputStream(writer.toString().getBytes(UTF8));
99 * @see org.simantics.graph.resourceFiles.IResourceFile#getFileName()
102 public String getFileName() {
103 return "src/" + packageName.replace('.', '/') + "/" + className + ".java";
107 Collections.sort(resources);
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(
116 )).write(new OutputStreamWriter(System.out));