X-Git-Url: https://gerrit.simantics.org/r/gitweb?p=simantics%2Fplatform.git;a=blobdiff_plain;f=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2Finternal%2FresourceFiles%2FResourceFile.java;fp=bundles%2Forg.simantics.graph.compiler%2Fsrc%2Forg%2Fsimantics%2Fgraph%2Fcompiler%2Finternal%2FresourceFiles%2FResourceFile.java;h=2b3fd2b71e74580bb36c0471c83648baa4041e8a;hp=0000000000000000000000000000000000000000;hb=969bd23cab98a79ca9101af33334000879fb60c5;hpb=866dba5cd5a3929bbeae85991796acb212338a08 diff --git a/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/ResourceFile.java b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/ResourceFile.java new file mode 100644 index 000000000..2b3fd2b71 --- /dev/null +++ b/bundles/org.simantics.graph.compiler/src/org/simantics/graph/compiler/internal/resourceFiles/ResourceFile.java @@ -0,0 +1,115 @@ +package org.simantics.graph.compiler.internal.resourceFiles; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; +import java.io.OutputStreamWriter; +import java.io.StringWriter; +import java.io.Writer; +import java.net.URL; +import java.util.Arrays; +import java.util.Collection; +import java.util.Collections; +import java.util.List; + +import org.simantics.graph.IResourceFile; + +import freemarker.cache.URLTemplateLoader; +import freemarker.template.Configuration; +import freemarker.template.DefaultObjectWrapper; +import freemarker.template.Template; + +public class ResourceFile implements IResourceFile { + String packageName; + String className; + List resources; + + public ResourceFile(String packageName, String className, + List resources) { + this.packageName = packageName; + this.className = className; + this.resources = resources; + } + + /* (non-Javadoc) + * @see org.simantics.graph.resourceFiles.IResourceFile#getPackageName() + */ + @Override + public String getPackageName() { + return packageName; + } + + /* (non-Javadoc) + * @see org.simantics.graph.resourceFiles.IResourceFile#getClassName() + */ + @Override + public String getClassName() { + return className; + } + + public Collection getResources() { + return resources; + } + + private static Configuration configuration = null; + private static Configuration getConfiguration() { + if(configuration == null) { + configuration = new Configuration(); + configuration.setTemplateLoader( + new URLTemplateLoader() { + @Override + protected URL getURL(String name) { + return ResourceFile.class.getResource(name); + } + }); + configuration.setObjectWrapper(new DefaultObjectWrapper()); + } + return configuration; + } + + /* (non-Javadoc) + * @see org.simantics.graph.resourceFiles.IResourceFile#write(java.io.Writer) + */ + @Override + public void write(Writer writer) { + try { + Template temp = + getConfiguration().getTemplate("resourceFile.ftl"); + + temp.process(this, writer); + } catch (Exception e) { + // TODO Auto-generated catch block + e.printStackTrace(); + } + } + + /* (non-Javadoc) + * @see org.simantics.graph.resourceFiles.IResourceFile#getContent() + */ + @Override + public InputStream getContent() { + StringWriter writer = new StringWriter(); + write(writer); + return new ByteArrayInputStream(writer.toString().getBytes()); + } + + /* (non-Javadoc) + * @see org.simantics.graph.resourceFiles.IResourceFile#getFileName() + */ + @Override + public String getFileName() { + return "src/" + packageName.replace('.', '/') + "/" + className + ".java"; + } + + public void sort() { + Collections.sort(resources); + } + + public static void main(String[] args) { + ResourceRef Foo = new ResourceRef("Foo", "http://www.dsf.sdf/Foo"); + ResourceRef Bar = new ResourceRef("Foo", "http://www.dsf.sdf/Bar"); + Bar.deprecated = true; + new ResourceFile("org.simantics.graph", "Testi", Arrays.asList( + Foo, Bar + )).write(new OutputStreamWriter(System.out)); + } +}