org.apache.wookie.w3c.util
Class WidgetPackageUtils

java.lang.Object
  extended by org.apache.wookie.w3c.util.WidgetPackageUtils

public class WidgetPackageUtils
extends java.lang.Object

Utilities for working with Widget packages, i.e. Zip Files with an XML manifest

Author:
scott

Constructor Summary
WidgetPackageUtils()
           
 
Method Summary
static java.lang.String convertIdToFolderName(java.lang.String id)
          Converts a widget identifier (usually a URI) into a form suitable for use as a file system folder name
static java.lang.String convertPathToPlatform(java.lang.String path)
           
static java.lang.String convertPathToRelativeUri(java.lang.String path)
           
static java.io.File createUnpackedWidgetFolder(java.io.File widgetFolder, java.lang.String id)
          Returns a File representing the unpacked folder for a widget with the given identifier.
static java.lang.String extractManifest(org.apache.commons.compress.archivers.zip.ZipFile zipFile)
          Retrieves the Manifest entry as a String
static java.lang.String[] getDefaults(org.apache.commons.compress.archivers.zip.ZipFile zip, java.lang.String[] locales, java.lang.String[] defaults)
          Return the set of valid default files for each locale in the zip
static java.io.InputStream getEntryStream(java.lang.String entry, org.apache.commons.compress.archivers.zip.ZipFile zipFile)
          Gets the input stream for an entry in the zip
static java.lang.String getURLForWidget(java.lang.String widgetFolder, java.lang.String id, java.lang.String file)
          Returns the local URL for a widget
static boolean hasManifest(org.apache.commons.compress.archivers.zip.ZipFile zipfile)
          Checks for the existence of the Manifest.
static java.lang.String languageTagForPath(java.lang.String path)
          Return the language tag for a given path
static java.lang.String locateFilePath(java.lang.String path, java.lang.String[] locales, org.apache.commons.compress.archivers.zip.ZipFile zip)
          Implements the rule for finding a file within a widget; it returns the first localized version first before returning a root version.
static java.lang.String[] locateFilePaths(java.lang.String path, java.lang.String[] locales, org.apache.commons.compress.archivers.zip.ZipFile zip)
          Returns the set of valid file paths for a given resource.
static void repackZip(java.io.File source, java.io.File target)
          Packages the source file/folder up as a new Zip file
static void unpackZip(org.apache.commons.compress.archivers.zip.ZipFile zipfile, java.io.File targetFolder)
          uses apache commons compress to unpack all the zip entries into a target folder partly adapted from the examples on the apache commons compress site, and an example of generic Zip unpacking.
 
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 

Constructor Detail

WidgetPackageUtils

public WidgetPackageUtils()
Method Detail

locateFilePath

public static java.lang.String locateFilePath(java.lang.String path,
                                              java.lang.String[] locales,
                                              org.apache.commons.compress.archivers.zip.ZipFile zip)
                                       throws java.lang.Exception
Implements the rule for finding a file within a widget; it returns the first localized version first before returning a root version. An exception is thrown if the path points to a folder, and null is returned if no file is found

Parameters:
path -
locales - the supported locales, in list of preference
zip -
Returns:
the path to the file as a String
Throws:
java.lang.Exception

locateFilePaths

public static java.lang.String[] locateFilePaths(java.lang.String path,
                                                 java.lang.String[] locales,
                                                 org.apache.commons.compress.archivers.zip.ZipFile zip)
                                          throws java.lang.Exception
Returns the set of valid file paths for a given resource. All valid paths are returned, starting with localized versions for supported locales before the root version (if present).

Parameters:
path -
locales -
zip -
Returns:
an array of paths for the resource
Throws:
java.lang.Exception

languageTagForPath

public static java.lang.String languageTagForPath(java.lang.String path)
Return the language tag for a given path

Parameters:
path -
Returns:
the language tag for the path

getDefaults

public static java.lang.String[] getDefaults(org.apache.commons.compress.archivers.zip.ZipFile zip,
                                             java.lang.String[] locales,
                                             java.lang.String[] defaults)
Return the set of valid default files for each locale in the zip

Parameters:
zip -
locales -
defaults -
Returns:
an array of file paths as Strings

createUnpackedWidgetFolder

public static java.io.File createUnpackedWidgetFolder(java.io.File widgetFolder,
                                                      java.lang.String id)
                                               throws java.io.IOException
Returns a File representing the unpacked folder for a widget with the given identifier.

Parameters:
widgetFolder - the folder that contains unpacked widgets
id - the widget identifier URI
Returns:
a File where the widget may be unpacked
Throws:
java.io.IOException

getURLForWidget

public static java.lang.String getURLForWidget(java.lang.String widgetFolder,
                                               java.lang.String id,
                                               java.lang.String file)
Returns the local URL for a widget

Parameters:
widgetFolder - the folder that contains unpacked widgets
id - the widget identifier URI
file - a file in the widget package; use "" to obtain the root of the widget folder
Returns:
the URL for the widget as a String

convertIdToFolderName

public static java.lang.String convertIdToFolderName(java.lang.String id)
Converts a widget identifier (usually a URI) into a form suitable for use as a file system folder name

Parameters:
id - the widget identifier URI
Returns:
the folder name to use for the widget

convertPathToRelativeUri

public static java.lang.String convertPathToRelativeUri(java.lang.String path)

convertPathToPlatform

public static java.lang.String convertPathToPlatform(java.lang.String path)

hasManifest

public static boolean hasManifest(org.apache.commons.compress.archivers.zip.ZipFile zipfile)
Checks for the existence of the Manifest. TODO not sure if this properly handles case-sensitive entries?

Parameters:
zipfile -
Returns:
true if the zip file has a manifest

extractManifest

public static java.lang.String extractManifest(org.apache.commons.compress.archivers.zip.ZipFile zipFile)
                                        throws java.io.IOException
Retrieves the Manifest entry as a String

Parameters:
zipFile - the zip file from which to extract the manifest
Returns:
a String representing the manifest contents
Throws:
java.io.IOException

getEntryStream

public static java.io.InputStream getEntryStream(java.lang.String entry,
                                                 org.apache.commons.compress.archivers.zip.ZipFile zipFile)
                                          throws java.util.zip.ZipException,
                                                 java.io.IOException
Gets the input stream for an entry in the zip

Parameters:
entry - the name of the entry
zipFile -
Returns:
the input stream
Throws:
java.util.zip.ZipException
java.io.IOException

unpackZip

public static void unpackZip(org.apache.commons.compress.archivers.zip.ZipFile zipfile,
                             java.io.File targetFolder)
                      throws java.io.IOException
uses apache commons compress to unpack all the zip entries into a target folder partly adapted from the examples on the apache commons compress site, and an example of generic Zip unpacking. Note this iterates over the ZipArchiveEntry enumeration rather than use the more typical ZipInputStream parsing model, as according to the doco it will more reliably read the entries correctly. More info here: http://commons.apache.org/compress/zip.html

Parameters:
zipfile - the Zip File to unpack
targetFolder - the folder into which to unpack the Zip file
Throws:
java.io.IOException

repackZip

public static void repackZip(java.io.File source,
                             java.io.File target)
                      throws java.io.IOException
Packages the source file/folder up as a new Zip file

Parameters:
source - the source file or folder to be zipped
target - the zip file to create
Throws:
java.io.IOException


2012 Apache Software Foundation.