From 193d6530dd4d38ce4d1cb4766b4fb4e5a6a88e17 Mon Sep 17 00:00:00 2001 From: "gyeongseok.seo" Date: Mon, 13 May 2013 16:02:48 +0900 Subject: [PATCH] [Title] Added pre-templatating logic [Desc.] MENIFEST.MF also template. and preload 'message.properties' [Issue] Change-Id: I0a488e880079cb32216a1fc40824bc46e27ebac0 --- .../common/verrari/template/DirectoryTemplate.java | 122 +++++++++++++++++-- .../verrari/template/DirectoryTemplateTest.java | 134 ++++++++++++++++++++- .../web-ui-fw/0.1.1/META-INF/messages.properties | 1 + 3 files changed, 247 insertions(+), 10 deletions(-) create mode 100644 org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/template/web-ui-fw/0.1.1/META-INF/messages.properties diff --git a/org.tizen.common.verrari.realm/src/org/tizen/common/verrari/template/DirectoryTemplate.java b/org.tizen.common.verrari.realm/src/org/tizen/common/verrari/template/DirectoryTemplate.java index b2c7aed..a66af89 100644 --- a/org.tizen.common.verrari.realm/src/org/tizen/common/verrari/template/DirectoryTemplate.java +++ b/org.tizen.common.verrari.realm/src/org/tizen/common/verrari/template/DirectoryTemplate.java @@ -18,10 +18,12 @@ import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; +import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; import java.io.StringReader; +import java.util.ArrayList; import java.util.Collection; import java.util.HashMap; import java.util.Locale; @@ -70,23 +72,123 @@ extends CommonTemplate protected String templateRootPath = null; public DirectoryTemplate( String path ) throws IOException, TemplateException { - if ( StringUtil.isEmpty( path ) ) { + if ( StringUtil.isEmpty( path ) && !(new File( path ).isDirectory()) ) { throw new TemplateException(); } this.templateRootPath = path; } - protected + private void - preload( String path ) throws IOException, TemplateException + templateManifest( File manifestfile, IModelProvider models ) throws IOException, TemplateException { - FileInputStream in = new FileInputStream( path+"/META-INF/MANIFEST.MF" ); + // try MANIFEST.MF generate + FileInputStream fin = new FileInputStream( manifestfile ); + + final ITemplateEngine engine = TemplateEngineFactory.getInstance().create(); + final TemplateManager templates = new TemplateManager(); + engine.setTemplateProvider( templates ); + + InMemoryTemplate manifestTemplate = + new InMemoryTemplate( getBytes(fin), "utf-8", BufferFactory.getInstance() ); + templates.addTemplate( "MANIFEST.MF", manifestTemplate ); - loadAttribute( in ); + final ByteArrayOutputStream out = new ByteArrayOutputStream(); + InputStream is = null; + try { + engine.generate( "MANIFEST.MF", models, out ); - final String version = attrs.get( ATTR_VERSION ); + is=new ByteArrayInputStream(out.toByteArray() ); + loadAttribute( is ); + } catch (Exception e) { + throw new TemplateException( e.getCause() ); + } finally { + tryClose( fin, out, is ); + } } + protected + void + recursiveBundleLoad( File root ) throws IOException { + File [] lists = root.listFiles(); + + for ( File file : lists ) { + if ( file.isDirectory() ) { + recursiveBundleLoad( file ); + } + String name = file.getName(); + FileInputStream in = null; + + try { + if ( null == file.getParentFile() && + !( "META-INF".equals( file.getParentFile().getName() ) ) ) { + continue; + } + + if ( "messages.properties".equals( name ) ) + { + in = new FileInputStream(file); + defaultBundle = new PropertyResourceBundle( in ); + } + else if ( name.startsWith( "messages_" ) && name.endsWith( ".properties" ) ) + { + final String localeStr = name.substring( "messages_".length(), name.length() - ".properties".length() ); + final String[] fragments = StringUtil.split( localeStr, "_" ); + final String lang = pickupFirst( fragments ); + final String country = (2==fragments.length)?fragments[1]:""; + + Locale locale = new Locale( lang, country ); + in = new FileInputStream(file); + final ResourceBundle bundle = new PropertyResourceBundle( in ); + bundles.put( locale, bundle ); + } + } catch ( IOException e ) { + throw e; + } finally { + tryClose( in ); + } + } + } + + protected + void + recursiveManifestLoad( File root, IModelProvider models ) throws IOException, TemplateException { + File [] lists = root.listFiles(); + + for ( File file : lists ) { + if ( file.isDirectory() ) { + recursiveManifestLoad( file, models ); + } + String name = file.getName(); + + if ( null == file.getParentFile() && + !( "META-INF".equals( file.getParentFile().getName() ) ) ) { + continue; + } + + if ( "MANIFEST.MF".equals( name ) ) + { + templateManifest( file, models ); + } + } + } + + protected + void + preload( String path, IModelProvider models ) throws IOException, TemplateException + { + // current template setting in context + context.set( this ); + + // bundles setting + File rootFile = new File( path ); + recursiveBundleLoad( rootFile ); + + // preload MANIFEST.MF + recursiveManifestLoad( rootFile, models ); + + } + /** * Read contents from in and make template info * @@ -109,6 +211,8 @@ extends CommonTemplate final String mappings = attrs.get( ATTR_MAPPING ); this.mapper = getMapper( attrs.get( ATTR_MAPPER ), mappings ); + + final Filter filter = createFilter( StandardRealm.separate( includes ), StandardRealm.separate( excludes ) ); final Filter copyFilter = createFilter( StandardRealm.separate( copies ), null ); @@ -147,8 +251,8 @@ extends CommonTemplate engine.setTemplateProvider( templates ); } - protected void pretemplate() throws IOException, TemplateException { - preload( this.templateRootPath ); + protected void pretemplate(IModelProvider models) throws IOException, TemplateException { + preload( this.templateRootPath, models ); load( this.templateRootPath ); } @@ -159,7 +263,7 @@ extends CommonTemplate System.out.println("Start template process..." ); try { - pretemplate(); + pretemplate( models ); } catch (IOException e) { throw new TemplateException( e ); } diff --git a/org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/template/DirectoryTemplateTest.java b/org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/template/DirectoryTemplateTest.java index f243f5d..6795e71 100644 --- a/org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/template/DirectoryTemplateTest.java +++ b/org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/template/DirectoryTemplateTest.java @@ -7,12 +7,32 @@ import java.net.URL; import java.util.Collection; import java.util.Iterator; +import org.junit.After; +import org.junit.Before; import org.junit.Test; +import org.tizen.common.Factory; +import org.tizen.common.config.Preference; +import org.tizen.common.core.command.ExecutionContext; +import org.tizen.common.core.command.Executor; +import org.tizen.common.core.command.Prompter; +import org.tizen.common.core.command.policy.PolicyRegistry; +import org.tizen.common.core.command.prompter.GenericOption; +import org.tizen.common.core.command.prompter.NopPrompter; +import org.tizen.common.file.VirtualFileHandler; import org.tizen.common.verrari.AbstractTestCase; +import org.tizen.common.verrari.IModelProvider; + +import static org.tizen.common.util.ObjectUtil.nvl; +import static org.tizen.common.util.StringUtil.nvl; public class DirectoryTemplateTest extends AbstractTestCase { + protected static final Object NULL = new Object(); + protected DirectoryTemplate template = null; + protected IModelProvider models = null; + protected String path = "./test/src/org/tizen/common/verrari/template/web-ui-fw/0.1.1"; + @Test public void @@ -31,7 +51,119 @@ extends AbstractTestCase String key = iter.next(); String attribute = template.getAttribute( key ); - System.out.println( attribute ); + assertNotNull( attribute ); + } + } + + @Test + public + void + test_preload() + throws Exception + { + template.preload(path, models); + + String expected = "0.1.1_Theme/original/*"; + String actual = template.getAttribute( TemplateConstants.ATTR_COPY ); + assertEquals(expected, actual); + } + + /** + * Set up test + * @throws Exception + */ + @Before + public void setUp() throws Exception + { + // test template root path setting + File file = new File( path ); + this.path = file.getAbsolutePath(); + + // test template class setting + this.template = new DirectoryTemplate( path ); + + // test template model setting + final VirtualFileHandler fileHandler = new VirtualFileHandler(); + Executor executor = new Executor( new Factory() { + public ExecutionContext create() { + return new ExecutionContext( new PolicyRegistry(), new NopPrompter(), fileHandler ); + } + } ); + this.models = new TestTemplateModelProvider( executor.getContext() ); + } + + /** + * Tear down test + */ + @After + public void tearDown() + { + } + + /** + * Test Mocking TemplateModelProvider class + */ + public class + TestTemplateModelProvider + implements IModelProvider + { + protected final ExecutionContext context; + + public + TestTemplateModelProvider( + final ExecutionContext context + ) + { + this.context = context; + } + + @Override + public boolean isEmpty() + { + return false; + } + + @Override + public Collection keys() + { + throw new UnsupportedOperationException(); + } + + @Override + public + Object + getModel( + final String key + ) + { + Object obj = context.getValue( key ); + if ( NULL.equals( obj ) ) + { + return null; + } else if ( null != obj ) + { + return obj; + } + + obj = Preference.getValue( key, null ); + if ( null != obj ) + { + return obj; + } + + obj = inputFromUser( key ); + + this.context.setValue( key, nvl( obj, NULL ) ); + + return obj; } + + protected Object inputFromUser( final String key ) + { + // mocking ( It's like user input key "original" ) + return "original"; + } + } + } diff --git a/org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/template/web-ui-fw/0.1.1/META-INF/messages.properties b/org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/template/web-ui-fw/0.1.1/META-INF/messages.properties new file mode 100644 index 0000000..be99ed5 --- /dev/null +++ b/org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/template/web-ui-fw/0.1.1/META-INF/messages.properties @@ -0,0 +1 @@ +SRCOPTIMIZE = Select Source Type [minified, original] ? \ No newline at end of file -- 2.7.4