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.util.Arrays;
10 import java.util.Collection;
11 import java.util.Collections;
12 import java.util.List;
14 import org.simantics.graph.IResourceFile;
16 import freemarker.cache.URLTemplateLoader;
17 import freemarker.template.Configuration;
18 import freemarker.template.DefaultObjectWrapper;
19 import freemarker.template.Template;
21 public class ResourceFile implements IResourceFile {
24 List<ResourceRef> resources;
26 public ResourceFile(String packageName, String className,
27 List<ResourceRef> resources) {
28 this.packageName = packageName;
29 this.className = className;
30 this.resources = resources;
34 * @see org.simantics.graph.resourceFiles.IResourceFile#getPackageName()
37 public String getPackageName() {
42 * @see org.simantics.graph.resourceFiles.IResourceFile#getClassName()
45 public String getClassName() {
49 public Collection<ResourceRef> getResources() {
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() {
60 protected URL getURL(String name) {
61 return ResourceFile.class.getResource(name);
64 configuration.setObjectWrapper(new DefaultObjectWrapper());
70 * @see org.simantics.graph.resourceFiles.IResourceFile#write(java.io.Writer)
73 public void write(Writer writer) {
76 getConfiguration().getTemplate("resourceFile.ftl");
78 temp.process(this, writer);
79 } catch (Exception e) {
80 // TODO Auto-generated catch block
86 * @see org.simantics.graph.resourceFiles.IResourceFile#getContent()
89 public InputStream getContent() {
90 StringWriter writer = new StringWriter();
92 return new ByteArrayInputStream(writer.toString().replace("\r\n", "\n").getBytes());
96 * @see org.simantics.graph.resourceFiles.IResourceFile#getFileName()
99 public String getFileName() {
100 return "src/" + packageName.replace('.', '/') + "/" + className + ".java";
104 Collections.sort(resources);
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(
113 )).write(new OutputStreamWriter(System.out));