[Title] Add logic - recursive add all tempaltes in directory.
authorgyeongseok.seo <gyeongseok.seo@samsung.com>
Mon, 13 May 2013 09:55:36 +0000 (18:55 +0900)
committergyeongseok.seo <gyeongseok.seo@samsung.com>
Mon, 13 May 2013 09:55:36 +0000 (18:55 +0900)
[Desc.] add templates AND classification copy file and tempalte file
[Issue]

Change-Id: I5953e5a679a521099a33e9fad53639dc54ee2621

org.tizen.common.verrari.realm/src/org/tizen/common/verrari/template/CommonTemplate.java
org.tizen.common.verrari.realm/src/org/tizen/common/verrari/template/DirectoryTemplate.java
org.tizen.common.verrari.realm/src/org/tizen/common/verrari/template/StandardTemplate.java
org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/template/DirectoryTemplateTest.java
org.tizen.common.verrari.realm/test/src/org/tizen/common/verrari/template/web-ui-fw/0.1.1/META-INF/MANIFEST.MF

index 1ef30a6..a9cf622 100644 (file)
@@ -1,3 +1,28 @@
+/*
+ *  Verrari - Realm
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: 
+ * GyeongSeok Seo <gyeongseok.seo@samsung.com>
+ * BonYong Lee <bonyong.lee@samsung.com>
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
 package org.tizen.common.verrari.template;
 
 import static org.tizen.common.util.StringUtil.nvl;
@@ -9,6 +34,7 @@ import java.io.InputStream;
 import java.io.StringReader;
 import java.util.Collection;
 import java.util.HashMap;
+import java.util.HashSet;
 import java.util.Locale;
 import java.util.ResourceBundle;
 import java.util.jar.Attributes;
@@ -29,6 +55,19 @@ import org.tizen.common.verrari.Template;
 import org.tizen.common.verrari.TemplateException;
 import org.tizen.common.verrari.exception.InvalidMapperException;
 
+/**
+ * <p>
+ * CommonTemplate
+ * 
+ * Built-in {@link Template} to provide RI
+ * 
+ * </p>
+ * 
+ * @author GyeongSeok Seo{@literal <gyeongseok.seo@samsung.com>} (S-Core)
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ * 
+ * @see {@link Template}, {@link StandardTemplate}, {@link DirectoryTemplate}
+ */
 public abstract class
 CommonTemplate
 implements Template
@@ -64,7 +103,7 @@ implements Template
      * template engine
      */
     protected ITemplateEngine engine = null;
-    
+
     /**
      * name mapper
      * 
@@ -72,6 +111,16 @@ implements Template
      */
     protected Mapper mapper = null;
 
+    /**
+     * template list to start
+     */
+    protected HashSet<String> templateNames = new HashSet<String>();
+    
+    /**
+     * list to copy
+     */
+    protected HashSet<String> copyNames = new HashSet<String>();
+
     public abstract void template( IModelProvider models, Storage storage ) throws TemplateException;
 
     public static
index a66af89..40fdd02 100644 (file)
@@ -1,50 +1,50 @@
+/*
+ *  Verrari - Realm
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: 
+ * GyeongSeok Seo <gyeongseok.seo@samsung.com>
+ * BonYong Lee <bonyong.lee@samsung.com>
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
 package org.tizen.common.verrari.template;
 
 import static org.tizen.common.util.ArrayUtil.pickupFirst;
 import static org.tizen.common.util.IOUtil.getBytes;
 import static org.tizen.common.util.IOUtil.tryClose;
-import static org.tizen.common.util.StringUtil.nvl;
 import static org.tizen.common.verrari.template.TemplateConstants.ATTR_COPY;
 import static org.tizen.common.verrari.template.TemplateConstants.ATTR_EXCLUDE;
 import static org.tizen.common.verrari.template.TemplateConstants.ATTR_INCLUDE;
 import static org.tizen.common.verrari.template.TemplateConstants.ATTR_MAPPER;
 import static org.tizen.common.verrari.template.TemplateConstants.ATTR_MAPPING;
-import static org.tizen.common.verrari.template.TemplateConstants.ATTR_VERSION;
-import static org.tizen.common.verrari.template.TemplateConstants.DV_MAPPER;
 
-import java.io.BufferedInputStream;
-import java.io.BufferedReader;
 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;
 import java.util.PropertyResourceBundle;
 import java.util.ResourceBundle;
-import java.util.jar.Attributes;
-import java.util.jar.JarEntry;
-import java.util.jar.JarInputStream;
-import java.util.jar.Manifest;
-import java.util.jar.Attributes.Name;
-
-import org.slf4j.Logger;
-import org.slf4j.LoggerFactory;
-import org.tizen.common.FactoryWithArgument;
-import org.tizen.common.core.command.ExecutionContext;
+
 import org.tizen.common.file.Filter;
-import org.tizen.common.file.SimpleFileFilter;
-import org.tizen.common.file.filter.WildCardFilterFactory;
-import org.tizen.common.util.FileUtil;
-import org.tizen.common.util.IOUtil;
-import org.tizen.common.util.ReflectionUtil;
 import org.tizen.common.util.StringUtil;
 import org.tizen.common.verrari.IModelProvider;
 import org.tizen.common.verrari.ITemplateEngine;
@@ -52,32 +52,74 @@ import org.tizen.common.verrari.Storage;
 import org.tizen.common.verrari.Template;
 import org.tizen.common.verrari.TemplateException;
 import org.tizen.common.verrari.engine.TemplateEngineFactory;
-import org.tizen.common.verrari.exception.InvalidMapperException;
-import org.tizen.common.verrari.model.ModelManager;
 import org.tizen.common.verrari.realm.StandardRealm;
 import org.tizen.common.verrari.util.BufferFactory;
 
-
+/**
+ * <p>
+ * DirectoryTemplate
+ * 
+ * Built-in {@link Template} to provide RI
+ * 
+ * </p>
+ * 
+ * @author GyeongSeok Seo{@literal <gyeongseok.seo@samsung.com>} (S-Core)
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ * 
+ * @see Template
+ */
 public class
 DirectoryTemplate
 extends CommonTemplate
 {
     /**
-     * name mapper
-     * 
-     * @see Mapper
+     * Directory Template's root path
      */
-    protected Mapper mapper = null;
-
     protected String templateRootPath = null;
 
-    public DirectoryTemplate( String path ) throws IOException, TemplateException {
-        if ( StringUtil.isEmpty( path ) && !(new File( path ).isDirectory()) ) {
+    /**
+     * Directory Template's root file, it must directory
+     */
+    protected File templateRootFile = null;
+
+    /**
+     * constructor with template contents in path, path is must directory
+     * 
+     * @param path in template contents
+     * 
+     * @throws TemplateException If template mapping is invalid
+     */
+    public DirectoryTemplate( String path ) throws TemplateException {
+        if ( StringUtil.isEmpty( path ) ||
+                !( this.templateRootFile = new File( path ) ).isDirectory() ) {
             throw new TemplateException();
         }
-        this.templateRootPath = path;
+        this.templateRootPath = this.templateRootFile.getAbsolutePath();
     }
 
+    /**
+     * constructor with template contents in file, file is must directory
+     * 
+     * @param file in template contents
+     * 
+     * @throws TemplateException If template mapping is invalid
+     */
+    public DirectoryTemplate( File file ) throws TemplateException {
+        if ( null == file || !file.isDirectory() ) {
+            throw new TemplateException();
+        }
+        this.templateRootFile = file;
+        this.templateRootPath = this.templateRootFile.getAbsolutePath();
+    }
+
+    /**
+     * pre-changing MANIFEST.MF file, because of MANIFEST.MF file also marking to template engine
+     * 
+     * @param manifestfile
+     * @param models
+     * @throws IOException
+     * @throws TemplateException
+     */
     private
     void
     templateManifest( File manifestfile, IModelProvider models ) throws IOException, TemplateException
@@ -107,6 +149,13 @@ extends CommonTemplate
         }
     }
     
+    /**
+     * directory template's message bundle loading.
+     * It working on recursive
+     * 
+     * @param root
+     * @throws IOException
+     */
     protected
     void
     recursiveBundleLoad( File root ) throws IOException {
@@ -115,6 +164,7 @@ extends CommonTemplate
         for ( File file : lists ) {
             if ( file.isDirectory() ) {
                 recursiveBundleLoad( file );
+                continue;
             }
             String name = file.getName();
             FileInputStream in = null;
@@ -150,6 +200,15 @@ extends CommonTemplate
         }
     }
 
+    /**
+     * directory template's MANIFES.MF loading.
+     * It working on recursive
+     * 
+     * @param root
+     * @param models
+     * @throws IOException
+     * @throws TemplateException
+     */
     protected
     void
     recursiveManifestLoad( File root, IModelProvider models ) throws IOException, TemplateException {
@@ -158,6 +217,7 @@ extends CommonTemplate
         for ( File file : lists ) {
             if ( file.isDirectory() ) {
                 recursiveManifestLoad( file, models );
+                continue;
             }
             String name = file.getName();
 
@@ -173,15 +233,60 @@ extends CommonTemplate
         }
     }
 
+    /**
+     * copy file and template file classification
+     * 
+     * @param root
+     * @param templates
+     * @param templateFilter
+     * @param copyFilter
+     * @throws IOException
+     */
+    protected
+    void
+    recursiveAddTemplate( File root, TemplateManager templates, Filter templateFilter, Filter copyFilter ) throws IOException {
+        File [] lists = root.listFiles();
+
+        for ( File file : lists ) {
+            if ( file.isDirectory() ) {
+                recursiveAddTemplate( file, templates, templateFilter, copyFilter );
+                continue;
+            }
+            String relativePath = file.getAbsolutePath().replace( templateRootPath+System.getProperty("file.separator"), "");
+            final String name = relativePath;
+
+            logger.trace( "Name :{}", name );
+            
+            FileInputStream in = new FileInputStream( file );
+            final byte[] contents = getBytes( in );
+            templates.addTemplate( name, new InMemoryTemplate(contents, "utf-8", BufferFactory.getInstance() ) );
+
+            if ( copyFilter.accept( "/", name ) )
+            {
+                logger.debug( "{} is copy target", name ); 
+                this.copyNames.add( name );
+                continue;
+            }
+            else if ( !templateFilter.accept( "/", name ) )
+            {
+                logger.info( "{} filtered", name );
+                continue;
+            }
+            else
+            {
+                this.templateNames.add( name );
+            }
+        }
+    }
+
     protected
     void
-    preload( String path, IModelProvider models ) throws IOException, TemplateException
+    preload( File rootFile, 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
@@ -199,7 +304,7 @@ extends CommonTemplate
      */
     protected
     void
-    load( String path )
+    load( File rootFile )
     throws TemplateException, IOException
     {
         this.engine = TemplateEngineFactory.getInstance().create();
@@ -211,49 +316,19 @@ 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 templateFilter = createFilter( StandardRealm.separate( includes ), StandardRealm.separate( excludes ) );
         
         final Filter copyFilter = createFilter( StandardRealm.separate( copies ), null );
         
         // Process files
-//        JarEntry iter = null;
-//        while ( null != ( iter = in.getNextJarEntry() ) )
-//        {
-//            if ( iter.isDirectory() )
-//            {
-//                continue;
-//            }
-//            final String name = iter.getName();
-//            logger.trace( "Name :{}", name );
-//            final byte[] contents = getBytes( in );
-//            
-//            templates.addTemplate( name, new InMemoryTemplate(contents, "utf-8", BufferFactory.getInstance() ) );
-//            if ( copyFilter.accept( "/", name ) )
-//            {
-//                logger.debug( "{} is copy target", name ); 
-//                this.copyNames.add( name );
-//                continue;
-//            }
-//            else if ( !filter.accept( "/", name ) )
-//            {
-//                logger.info( "{} filtered", name );
-//                continue;
-//            }
-//            else
-//            {
-//                this.templateNames.add( name );
-//            }
-//            
-//        }
-        
+        recursiveAddTemplate( rootFile, templates, templateFilter, copyFilter );
+
         engine.setTemplateProvider( templates );
     }
 
     protected void pretemplate(IModelProvider models) throws IOException, TemplateException {
-        preload( this.templateRootPath, models );
-        load( this.templateRootPath );
+        preload( templateRootFile, models );
+        load( templateRootFile );
     }
 
     @Override
@@ -267,9 +342,7 @@ extends CommonTemplate
         } catch (IOException e) {
             throw new TemplateException( e );
         }
-        //storage.getTarget(key);
-        
-        
+
         logger.info( "End template process..." );
         System.out.println("End template process..." );
     }
index ab6748f..ff33691 100755 (executable)
@@ -92,16 +92,6 @@ StandardTemplate
 extends CommonTemplate\r
 {\r
     /**\r
-     * template list to start\r
-     */\r
-    protected HashSet<String> templateNames = new HashSet<String>();\r
-    \r
-    /**\r
-     * list to copy\r
-     */\r
-    protected HashSet<String> copyNames = new HashSet<String>();\r
-\r
-    /**\r
      * constructor with template contents in jar file format byte[]\r
      * \r
      * @param bytes template contents\r
index 6795e71..a639ce2 100644 (file)
@@ -1,9 +1,33 @@
+/*
+ *  Verrari - Realm
+ *
+ * Copyright (c) 2000 - 2011 Samsung Electronics Co., Ltd. All rights reserved.
+ *
+ * Contact: 
+ * GyeongSeok Seo <gyeongseok.seo@samsung.com>
+ * BonYong Lee <bonyong.lee@samsung.com>
+ * 
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ *
+ * Contributors:
+ * - S-Core Co., Ltd
+ *
+ */
 package org.tizen.common.verrari.template;
 
 import static org.junit.Assert.*;
 
 import java.io.File;
-import java.net.URL;
 import java.util.Collection;
 import java.util.Iterator;
 
@@ -14,17 +38,25 @@ 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;
 
+/**
+ * DirectoryTemplateTest.
+ *
+ * Test case for {@link DirectoryTemplate}
+ * 
+ * @author GyeongSeok Seo{@literal <gyeongseok.seo@samsung.com>} (S-Core)
+ * @author BonYong Lee{@literal <bonyong.lee@samsung.com>} (S-Core)
+ *
+ * @see DirectoryTemplate
+ *
+ */
 public class DirectoryTemplateTest
 extends AbstractTestCase
 {
@@ -33,17 +65,22 @@ extends AbstractTestCase
     protected IModelProvider models = null;
     protected String path = "./test/src/org/tizen/common/verrari/template/web-ui-fw/0.1.1";
 
+    /**
+     * Test {@link DirectoryTemplate#getAttribute(String)}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see DirectoryTemplate#getAttribute(String)
+     */
     @Test
     public
     void
     test_getAttribute()
     throws Exception
     {
-        String path = "./test/src/org/tizen/common/verrari/template/web-ui-fw/0.1.1";
-        File file = new File( path );
-        path = file.getAbsolutePath();
+        File rootFile = new File( path );
+        template.preload( rootFile, models );
 
-        DirectoryTemplate template = new DirectoryTemplate( path );
         Collection<String> keys = template.getAttributeKeys();
         Iterator<String> iter = keys.iterator();
         
@@ -54,18 +91,44 @@ extends AbstractTestCase
             assertNotNull( attribute );
         }
     }
-    
+
+    /**
+     * Test {@link DirectoryTemplate#preload(File, IModelProvider)}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see DirectoryTemplate#preload(File, IModelProvider)
+     */
     @Test
     public
     void
     test_preload()
     throws Exception
     {
-        template.preload(path, models);
-        
+        File rootFile = new File( path );
+        template.preload( rootFile, models );
+
         String expected = "0.1.1_Theme/original/*";
         String actual = template.getAttribute( TemplateConstants.ATTR_COPY );
-        assertEquals(expected, actual);
+        assertEquals( expected, actual );
+    }
+
+    /**
+     * Test {@link DirectoryTemplate#load(File)}
+     * 
+     * @throws Exception in case of failure in test
+     * 
+     * @see DirectoryTemplate#load(File)
+     */
+    @Test
+    public
+    void
+    test_load()
+    throws Exception
+    {
+        File rootFile = new File( path );
+        template.preload( rootFile, models );
+        template.load( rootFile );
     }
 
     /**
index 1295513..3b1d7fa 100755 (executable)
@@ -2,6 +2,5 @@ Manifest-Version: 1.0
 Tizen-Template-Name: web-ui-fw
 Tizen-Template-Version: 0.1.1
 Tizen-Template-Copy: 0.1.1_Theme/$${SRCOPTIMIZE|'minified'}/*
-Tizen-Template-Mappings: 0\.1\.1_Theme/minified/(.*) -> $1,0\\.1\\.1_Theme/origin/(.*) -> $1
 Tizen-Template-Exclude: META-INF/*.*
-
+Tizen-Template-Mapping: 0\.1\.1_Theme/minified/(.*) -> $1,0\\.1\\.1_Theme/origin/(.*) -> $1