The code that Wookie uses to parse and unpack W3C Widgets can also be used in other applications and projects as a standalone library. The parser can be found as a sub-project within the "parser/java/" folder in the main Wookie source tree.
Building the parser
To build the parser as a standalone .jar, run "ant publish-local" from within the parser folder; the jar will be saved in parser/dist. Dependencies are described in the ivy.xml file that can also be found in the parser/dist folder.
Using the parser
The parser is invoked using a W3CWidgetFactory object. This has a number of configuration properties that can be set, and these are described at the bottom of this page. While most of these are optional, to use the factory to parse .wgt files you MUST supply a valid output directory into which the Factory will unpack the widget.
For example, a simple program to unpack a .wgt file, and write the name of the widget to the console looks like this:
public static void main(String[] args) { File out = new File("out"); File zipFile = new File("in/butterfly.wgt"); W3CWidgetFactory fac = new W3CWidgetFactory(); fac.setOutputDirectory(out.getAbsolutePath()); fac.setLocalPath("/out"); try { W3CWidget widget = fac.parse(zipFile); System.out.println(widget.getLocalName("en")); } catch (BadWidgetZipFileException e) { e.printStackTrace(); } catch (BadManifestException e) { e.printStackTrace(); } }
Localized elements
Many of the elements of a W3CWidget object are localized; these elements all implement the ILocalizedElement interface. These elements can be processed using the LocalizationUtils class, which has methods to process these elements based on lists of preferred locales. E.g., to extract the single most preferred Name element for the "fr" locale:
INameEntity[] names = widget.getNames().toArray(new INameEntity[fNamesList.size()]); String[] locales = new String[]{"fr"}; INameEntity name = (INameEntity)LocalizationUtils.getLocalizedElement(names, locales);
LocalizationUtils uses icu4j to process elements using appropriate fallback strategies based on language variants and extensions.
Factory properties reference
outputDirectory
The directory where the widget will be saved. The factory will expand the widget archive into this directory, using the widget's identifier to generate a directory name and structure in which to place it
startPageProcessor
An implementation of the IStartPageProcessor interface. Setting this property allows you to inject a class that can pre-process start pages for the widget; for example to inject javascript, tidy up HTML, insert template code etc. If this is not set, no pre-processing is done by the factory.
locales
The supported locales (as BCP47 language-tags) to be processed for the widget. This determines which start files, icons, and other localized elements will be processed and expanded. This is set to "en" by default
localPath
The base URL from which unpacked widgets will be served, e.g. "/myapp/widgets". The URLs of start files will be appended to this base URL to create the widget URL. The default value of this property is "/widgets"
features
The features supported by the implementation; this should be supplied as IRIs e.g. "http://wave.google.com". The features are matched against features requested by the widget; if the widget requires features that are unsupported, an Exception will be thrown when parsing the widget package. The default value of this property is an empty String array.