Author: scottbw
Date: Sat Mar  9 15:09:41 2013
New Revision: 1454732

URL: http://svn.apache.org/r1454732
Log:
Added support to the W3C Widget Parser to use a wildcard ("*") for supported 
locales; if this is set then the parser will attempt to return localized 
content for all child directories of "locales" in imported widgets. See 
WOOKIE-360.

Modified:
    
wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/WidgetPackageUtilsTest.java
    wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java
    
wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java

Modified: 
wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/WidgetPackageUtilsTest.java
URL: 
http://svn.apache.org/viewvc/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/WidgetPackageUtilsTest.java?rev=1454732&r1=1454731&r2=1454732&view=diff
==============================================================================
--- 
wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/WidgetPackageUtilsTest.java
 (original)
+++ 
wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/WidgetPackageUtilsTest.java
 Sat Mar  9 15:09:41 2013
@@ -17,6 +17,10 @@ import static org.junit.Assert.assertEqu
 import static org.junit.Assert.assertNull;
 import static org.junit.Assert.fail;
 
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.commons.compress.archivers.zip.ZipFile;
 import org.apache.wookie.w3c.util.WidgetPackageUtils;
 import org.junit.Test;
 
@@ -77,4 +81,20 @@ public class WidgetPackageUtilsTest {
                String locale = 
WidgetPackageUtils.languageTagForPath("locales/");
                assertNull(locale);
        }
+       
+       /**
+        * Tests that we can extract the names of locale folders from a widget
+        * @throws IOException
+        */
+       @Test
+       public void getLocalesForPackage() throws IOException{
+               File widget = new 
File("parser/java/src-test/resources/localetest.wgt");
+               assert widget.exists();
+               ZipFile zipFile = new ZipFile(widget);
+               String[] localesFromZip = 
WidgetPackageUtils.getLocalesFromZipFile(zipFile);            
+               assertEquals(3, localesFromZip.length);
+               assertEquals("en", localesFromZip[0]);
+               assertEquals("en-gb-yorks", localesFromZip[1]);
+               assertEquals("fr", localesFromZip[2]);
+       }
 }

Modified: 
wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java
URL: 
http://svn.apache.org/viewvc/wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java?rev=1454732&r1=1454731&r2=1454732&view=diff
==============================================================================
--- wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java 
(original)
+++ wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java 
Sat Mar  9 15:09:41 2013
@@ -269,8 +269,8 @@ public class W3CWidgetFactory {
         * @throws BadManifestException
         */
        private W3CWidget processWidgetPackage(File zipFile, String 
defaultIdentifier) throws BadWidgetZipFileException,
-                       BadManifestException, InsecuredWidgetContentException {
-         ZipFile zip;
+       BadManifestException, InsecuredWidgetContentException {
+               ZipFile zip;
                try {
                        zip = new ZipFile(zipFile);
                } catch (IOException e) {
@@ -278,6 +278,15 @@ public class W3CWidgetFactory {
                }
                if (WidgetPackageUtils.hasManifest(zip)){
                        try {
+                               
+                               //
+                               // If locales is set to "*" then look in the 
package and process all locales found
+                               //
+                               
+                               if (locales.length == 1 && 
locales[0].equals("*")){
+                                       locales = 
WidgetPackageUtils.getLocalesFromZipFile(zip);
+                               }
+                               
                                // build the model
                                WidgetManifestModel widgetModel = new 
WidgetManifestModel(WidgetPackageUtils.extractManifest(zip), locales, features, 
encodings, zip, defaultIdentifier);                                             
                                                                          
 
@@ -286,13 +295,13 @@ public class W3CWidgetFactory {
                                // create the folder structure to unzip the zip 
into
                                unzippedWidgetDirectory = 
WidgetPackageUtils.createUnpackedWidgetFolder(outputDirectory, 
manifestIdentifier);
                                // now unzip it into that folder
-        WidgetPackageUtils.unpackZip(zip, unzippedWidgetDirectory);
-        // checks for validity of widget using digital signatures
-        if (digitalSignatureParser != null) {
-          digitalSignatureParser
-              .processDigitalSignatures(unzippedWidgetDirectory
-                  .getAbsolutePath());
-        }
+                               WidgetPackageUtils.unpackZip(zip, 
unzippedWidgetDirectory);
+                               // checks for validity of widget using digital 
signatures
+                               if (digitalSignatureParser != null) {
+                                       digitalSignatureParser
+                                       
.processDigitalSignatures(unzippedWidgetDirectory
+                                                       .getAbsolutePath());
+                               }
                                // Iterate over all start files and update paths
                                for (IContent content: 
widgetModel.getContentList()){
                                        // now update the js links in the start 
page
@@ -306,12 +315,12 @@ public class W3CWidgetFactory {
                                if (widgetModel.getContentList().isEmpty()){
                                        throw new 
InvalidStartFileException("Widget has no start page");
                                }
-                               
+
                                // get the path to the root of the unzipped 
folder
                                String thelocalPath = 
WidgetPackageUtils.getURLForWidget(localPath, manifestIdentifier, "");
                                // now pass this to the model which will 
prepend the path to local resources (not web icons)
                                widgetModel.updateIconPaths(thelocalPath);
-                               
+
                                // check to see if this widget already exists 
in the DB - using the ID (guid) key from the manifest
                                return widgetModel;
 
@@ -324,20 +333,20 @@ public class W3CWidgetFactory {
                        } catch (Exception e){
                                throw new BadManifestException(e);
                        } finally {     
-                           try {
-            zip.close();
-          } catch (IOException e) {
-            _logger.error("Unable to close wgt file:" + e.getMessage());
-          }
+                               try {
+                                       zip.close();
+                               } catch (IOException e) {
+                                       _logger.error("Unable to close wgt 
file:" + e.getMessage());
+                               }
                        }
-                       
+
                }
                else{
-                   try {
-                zip.close();
-            } catch (IOException e) {
-                _logger.error("Unable to close wgt file (without manifest):" + 
e.getMessage());
-            }
+                       try {
+                               zip.close();
+                       } catch (IOException e) {
+                               _logger.error("Unable to close wgt file 
(without manifest):" + e.getMessage());
+                       }
                        // no manifest file found in zip archive
                        throw new BadWidgetZipFileException(); //$NON-NLS-1$ 
                }

Modified: 
wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java
URL: 
http://svn.apache.org/viewvc/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java?rev=1454732&r1=1454731&r2=1454732&view=diff
==============================================================================
--- 
wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java 
(original)
+++ 
wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java 
Sat Mar  9 15:09:41 2013
@@ -104,6 +104,76 @@ public class WidgetPackageUtils {
                return locale;
        }
        
+    /*
+     * Utility method for creating a temp directory
+     * @return a new temp directory
+     * @throws IOException
+     */
+       public static File createTempDirectory() throws IOException {
+               final File temp;
+
+               temp = File.createTempFile("temp", 
Long.toString(System.nanoTime()));
+
+               if (!(temp.delete())) {
+                       throw new IOException("Could not delete temp file: "
+                                       + temp.getAbsolutePath());
+               }
+
+               if (!(temp.mkdir())) {
+                       throw new IOException("Could not create temp directory: 
"
+                                       + temp.getAbsolutePath());
+               }
+
+               return (temp);
+       }
+
+       
+       /**
+        * Given a zipfile, iterate over the contents of the "locales" folder 
within the
+        * archive, should it exist, and return the names of all the 
subdirectories, 
+        * assumed to be localized content folders.
+        * 
+        * Note that this method does not validate the directory names as being 
valid
+        * language tags.
+        * @param zipFile
+        * @return an array of locale names
+        * @throws IOException
+        */
+       public static String[] getLocalesFromZipFile(ZipFile zipFile) throws 
IOException{
+               File temp = createTempDirectory();
+               unpackZip(zipFile, temp);
+               return getLocalesFromPackage(temp);
+       }
+       
+       
+       /**
+        * Give a directory, assumed to be the root of a widget package, 
iterate over
+        * the contents of the "locales" folder, should it exist, and return 
the names
+        * of all the subdirectories, assumed to be localized content folders.
+        * 
+        * Note that this method does not validate the directory names as being 
valid
+        * language tags.
+        * @param file
+        * @return an array of locale names
+        */
+       public static String[] getLocalesFromPackage(File file){
+               ArrayList<String> locales = new ArrayList<String>();
+               
+               if (file.exists()){
+                       File localesFolder = new File(file + File.separator + 
"locales");
+                       if (localesFolder.exists() && 
localesFolder.isDirectory()){
+                               for (String localeName : localesFolder.list()){
+                                       File localeFolder = new 
File(localesFolder + File.separator + localeName);
+                                       if (localeFolder.isDirectory()){
+                                               locales.add(localeName);
+                                       }
+                               }
+                       }
+               }
+               
+               return (String[])locales.toArray(new String[locales.size()]);
+       }
+       
        /**
         * Return the set of valid default files for each locale in the zip
         * @param zip


Reply via email to