Author: scottbw
Date: Sat Apr 17 18:51:28 2010
New Revision: 935227
URL: http://svn.apache.org/viewvc?rev=935227&view=rev
Log:
Allow setting supported character encodings for the W3CWidgetFactory in the
same manner as setting supported locales, and added supporting tests. This is
important as otherwise there is no way to specify a set of supported encodings
for the parser other than editing the IW3CXMLConfiguration constants.
As part of this change I've moved the "processWidgetPackage" method out of
WidgetPackageUtils and into the W3CWidgetFactory - this made sense as all its
parameters apart from the file it parses were instance variables of the
W3CWidgetFactory anyway.
Modified:
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/EntityTest.java
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/PackagingAndConfiguration.java
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/W3CWidgetFactoryTest.java
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/IContentEntity.java
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/IW3CXMLConfiguration.java
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java
Modified:
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/EntityTest.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/EntityTest.java?rev=935227&r1=935226&r2=935227&view=diff
==============================================================================
---
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/EntityTest.java
(original)
+++
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/EntityTest.java
Sat Apr 17 18:51:28 2010
@@ -148,13 +148,13 @@ public class EntityTest {
@Test
public void widget() throws JDOMException, IOException,
BadManifestException{
- WidgetManifestModel widget = new WidgetManifestModel("<widget
xmlns=\""+IW3CXMLConfiguration.MANIFEST_NAMESPACE+"\"><name>test</name></widget>",null,null,null);
+ WidgetManifestModel widget = new WidgetManifestModel("<widget
xmlns=\""+IW3CXMLConfiguration.MANIFEST_NAMESPACE+"\"><name>test</name></widget>",null,null,null,null);
assertNull(widget.getAuthorEmail());
assertNull(widget.getAuthorHref());
assertEquals("test",widget.getLocalName("en"));
assertEquals("floating",widget.getViewModes());
- widget = new WidgetManifestModel("<widget
xmlns=\""+IW3CXMLConfiguration.MANIFEST_NAMESPACE+"\"
viewmodes=\"fullscreen\"></widget>",null,null,null);
+ widget = new WidgetManifestModel("<widget
xmlns=\""+IW3CXMLConfiguration.MANIFEST_NAMESPACE+"\"
viewmodes=\"fullscreen\"></widget>",null,null,null,null);
assertNull(widget.getAuthorEmail());
assertNull(widget.getAuthorHref());
assertEquals(IW3CXMLConfiguration.UNKNOWN,widget.getLocalName("en"));
Modified:
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/PackagingAndConfiguration.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/PackagingAndConfiguration.java?rev=935227&r1=935226&r2=935227&view=diff
==============================================================================
---
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/PackagingAndConfiguration.java
(original)
+++
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/PackagingAndConfiguration.java
Sat Apr 17 18:51:28 2010
@@ -80,6 +80,7 @@ public class PackagingAndConfiguration{
W3CWidgetFactory fac = new W3CWidgetFactory();
fac.setLocalPath("http:localhost/widgets");
fac.setFeatures(new String[]{"feature:a9bb79c1"});
+ fac.setEncodings(new String[]{"UTF-8",
"ISO-8859-1","Windows-1252"});
if (download.exists()) download.delete();
if (output.exists()) output.delete();
output.mkdir();
Modified:
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/W3CWidgetFactoryTest.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/W3CWidgetFactoryTest.java?rev=935227&r1=935226&r2=935227&view=diff
==============================================================================
---
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/W3CWidgetFactoryTest.java
(original)
+++
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/W3CWidgetFactoryTest.java
Sat Apr 17 18:51:28 2010
@@ -84,6 +84,18 @@ public class W3CWidgetFactoryTest {
fac.parse(wgt);
}
+ @Test(expected = NullPointerException.class)
+ public void testSetNullEncodings() throws Exception{
+ W3CWidgetFactory fac = new W3CWidgetFactory();
+ fac.setEncodings(null);
+ }
+
+ @Test(expected = Exception.class)
+ public void testSetNoEncodings() throws Exception{
+ W3CWidgetFactory fac = new W3CWidgetFactory();
+ fac.setEncodings(new String[]{});
+ }
+
@Test
public void testSetFeaturesNull() throws Exception{
W3CWidgetFactory fac = new W3CWidgetFactory();
Modified:
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/IContentEntity.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/IContentEntity.java?rev=935227&r1=935226&r2=935227&view=diff
==============================================================================
---
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/IContentEntity.java
(original)
+++
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/IContentEntity.java
Sat Apr 17 18:51:28 2010
@@ -36,6 +36,6 @@ public interface IContentEntity extends
public void setType(String type);
- public void fromXML(Element element, String[] locales, ZipFile zip)
throws BadManifestException;
+ public void fromXML(Element element, String[] locales, String[]
encodings, ZipFile zip) throws BadManifestException;
}
Modified:
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/IW3CXMLConfiguration.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/IW3CXMLConfiguration.java?rev=935227&r1=935226&r2=935227&view=diff
==============================================================================
---
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/IW3CXMLConfiguration.java
(original)
+++
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/IW3CXMLConfiguration.java
Sat Apr 17 18:51:28 2010
@@ -85,9 +85,6 @@ public interface IW3CXMLConfiguration {
public static final String[] SUPPORTED_CONTENT_TYPES = {"text/html",
"image/svg+xml","application/xhtml+xml"};
public static final String[] START_FILES =
{"index.htm","index.html","index.svg","index.xhtml","index.xht"};
public static final String[] DEFAULT_ICON_FILES =
{"icon.svg","icon.ico","icon.png","icon.gif","icon.jpg"};
- public static final String[] SUPPORTED_ENCODINGS = {"UTF-8"};
- //Only use this value for supported encodings when checking conformance
- //public static final String[] SUPPORTED_ENCODINGS = {"UTF-8",
"ISO-8859-1","Windows-1252"};
// Deprecated: used in early drafts of spec:
Modified:
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java?rev=935227&r1=935226&r2=935227&view=diff
==============================================================================
---
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java
(original)
+++
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java
Sat Apr 17 18:51:28 2010
@@ -18,6 +18,7 @@ import java.io.FileNotFoundException;
import java.io.IOException;
import java.net.URL;
+import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.methods.GetMethod;
@@ -26,6 +27,8 @@ import org.apache.commons.io.IOUtils;
import org.apache.wookie.w3c.exceptions.BadManifestException;
import org.apache.wookie.w3c.exceptions.BadWidgetZipFileException;
import org.apache.wookie.w3c.exceptions.InvalidContentTypeException;
+import org.apache.wookie.w3c.exceptions.InvalidStartFileException;
+import org.apache.wookie.w3c.impl.WidgetManifestModel;
import org.apache.wookie.w3c.util.WidgetPackageUtils;
/**
@@ -47,6 +50,9 @@ import org.apache.wookie.w3c.util.Widget
* <dt>locales</dt>
* <dd>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</dd>
+ * <dt>encodings</dt>
+ * <dd>The supported encodings to be processed for the widget. This
determines which custom encodings will be allowed for start files.
+ * This is set to UTF-8 by default.</dd>
* <dt>localPath</dt>
* <dd>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"</dd>
@@ -64,6 +70,7 @@ public class W3CWidgetFactory {
private String[] locales;
private String localPath;
private String[] features;
+ private String[] encodings;
/**
* Set the features to be included when parsing widgets
@@ -79,6 +86,7 @@ public class W3CWidgetFactory {
this.features = new String[0];
this.localPath = "/widgets";
this.outputDirectory = null;
+ this.encodings = new String[]{"UTF-8"};
this.startPageProcessor = new IStartPageProcessor(){
public void processStartFile(File startFile, W3CWidget
model)
throws Exception {
@@ -139,7 +147,7 @@ public class W3CWidgetFactory {
*/
public W3CWidget parse(final File zipFile) throws Exception,
BadWidgetZipFileException, BadManifestException{
if (outputDirectory == null) throw new Exception("No output
directory has been set; use setOutputDirectory(File) to set the location to
output widget files");
- return WidgetPackageUtils.processWidgetPackage(zipFile,
localPath, outputDirectory, locales, startPageProcessor, features);
+ return processWidgetPackage(zipFile);
}
/**
@@ -198,4 +206,71 @@ public class W3CWidgetFactory {
method.releaseConnection();
return file;
}
+
+ public void setEncodings(final String[] encodings) throws Exception {
+ if (encodings == null) throw new
NullPointerException("Supported encodings cannot be set to null");
+ if (encodings.length == 0) throw new Exception("At least one
encoding must be specified");
+ this.encodings = encodings;
+ }
+
+ /**
+ * Process a widget package for the given zip file
+ * @param zipFile
+ * @return a W3CWidget representing the widget
+ * @throws BadWidgetZipFileException
+ * @throws BadManifestException
+ */
+ private W3CWidget processWidgetPackage(File zipFile) throws
BadWidgetZipFileException, BadManifestException{
+ ZipFile zip;
+ try {
+ zip = new ZipFile(zipFile);
+ } catch (IOException e) {
+ throw new BadWidgetZipFileException();
+ }
+ if (WidgetPackageUtils.hasManifest(zip)){
+ try {
+ // build the model
+ WidgetManifestModel widgetModel = new
WidgetManifestModel(WidgetPackageUtils.extractManifest(zip), locales, features,
encodings, zip);
+
+ // get the widget identifier
+ String manifestIdentifier =
widgetModel.getIdentifier();
+ // create the folder structure to unzip the zip
into
+ File newWidgetFolder =
WidgetPackageUtils.createUnpackedWidgetFolder(outputDirectory,
manifestIdentifier);
+ // now unzip it into that folder
+ WidgetPackageUtils.unpackZip(zip,
newWidgetFolder);
+
+ // Iterate over all start files and update paths
+ for (IContentEntity content:
widgetModel.getContentList()){
+ // now update the js links in the start
page
+ File startFile = new
File(newWidgetFolder.getCanonicalPath() + File.separator + content.getSrc());
+ String relativestartUrl =
(WidgetPackageUtils.getURLForWidget(localPath, manifestIdentifier,
content.getSrc()));
+ content.setSrc(relativestartUrl);
+ if(startFile.exists() &&
startPageProcessor != null){
+
startPageProcessor.processStartFile(startFile, widgetModel);
+ }
+ }
+ if (widgetModel.getContentList().isEmpty()){
+ throw new InvalidStartFileException();
+ }
+
+ // 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;
+ } catch (InvalidStartFileException e) {
+ throw e;
+ } catch (BadManifestException e) {
+ throw e;
+ } catch (Exception e){
+ throw new BadManifestException(e);
+ }
+ }
+ else{
+ // no manifest file found in zip archive
+ throw new BadWidgetZipFileException(); //$NON-NLS-1$
+ }
+ }
}
Modified:
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java?rev=935227&r1=935226&r2=935227&view=diff
==============================================================================
---
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java
(original)
+++
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/util/WidgetPackageUtils.java
Sat Apr 17 18:51:28 2010
@@ -35,14 +35,7 @@ import org.apache.commons.io.FileUtils;
import org.apache.commons.io.IOUtils;
import org.apache.log4j.Logger;
import org.apache.wookie.w3c.util.LocalizationUtils;
-import org.apache.wookie.w3c.IContentEntity;
-import org.apache.wookie.w3c.W3CWidget;
-import org.apache.wookie.w3c.IStartPageProcessor;
import org.apache.wookie.w3c.IW3CXMLConfiguration;
-import org.apache.wookie.w3c.exceptions.BadManifestException;
-import org.apache.wookie.w3c.exceptions.BadWidgetZipFileException;
-import org.apache.wookie.w3c.exceptions.InvalidStartFileException;
-import org.apache.wookie.w3c.impl.WidgetManifestModel;
/**
* Utilities for working with Widget packages, i.e. Zip Files with an XML
manifest
@@ -228,60 +221,6 @@ public class WidgetPackageUtils {
return true;
}
- public static W3CWidget processWidgetPackage(File zipFile, String
localWidgetPath, File WIDGETFOLDER, String[] locales, IStartPageProcessor
processor, String[] features) throws BadWidgetZipFileException,
BadManifestException{
- ZipFile zip;
- try {
- zip = new ZipFile(zipFile);
- } catch (IOException e) {
- throw new BadWidgetZipFileException();
- }
- if (WidgetPackageUtils.hasManifest(zip)){
- try {
- // build the model
- WidgetManifestModel widgetModel = new
WidgetManifestModel(WidgetPackageUtils.extractManifest(zip), locales, features,
zip);
-
- // get the widget identifier
- String manifestIdentifier =
widgetModel.getIdentifier();
- // create the folder structure to unzip the zip
into
- File newWidgetFolder =
WidgetPackageUtils.createUnpackedWidgetFolder(WIDGETFOLDER, manifestIdentifier);
- // now unzip it into that folder
- WidgetPackageUtils.unpackZip(zip,
newWidgetFolder);
-
- // Iterate over all start files and update paths
- for (IContentEntity content:
widgetModel.getContentList()){
- // now update the js links in the start
page
- File startFile = new
File(newWidgetFolder.getCanonicalPath() + File.separator + content.getSrc());
- String relativestartUrl =
(WidgetPackageUtils.getURLForWidget(localWidgetPath, manifestIdentifier,
content.getSrc()));
- content.setSrc(relativestartUrl);
- if(startFile.exists() && processor !=
null){
-
processor.processStartFile(startFile, widgetModel);
- }
- }
- if (widgetModel.getContentList().isEmpty()){
- throw new InvalidStartFileException();
- }
-
- // get the path to the root of the unzipped
folder
- String localPath =
WidgetPackageUtils.getURLForWidget(localWidgetPath, manifestIdentifier, "");
- // now pass this to the model which will
prepend the path to local resources (not web icons)
- widgetModel.updateIconPaths(localPath);
-
- // check to see if this widget already exists
in the DB - using the ID (guid) key from the manifest
- return widgetModel;
- } catch (InvalidStartFileException e) {
- throw e;
- } catch (BadManifestException e) {
- throw e;
- } catch (Exception e){
- throw new BadManifestException(e);
- }
- }
- else{
- // no manifest file found in zip archive
- throw new BadWidgetZipFileException(); //$NON-NLS-1$
- }
- }
-
/**
* Checks for the existence of the Manifest.
* TODO not sure if this properly handles case-sensitive entries?