You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@wookie.apache.org by sc...@apache.org on 2011/02/14 16:25:47 UTC

svn commit: r1070520 - in /incubator/wookie/trunk: src-tests/org/apache/wookie/tests/flatpack/ src/org/apache/wookie/flatpack/

Author: scottbw
Date: Mon Feb 14 15:25:46 2011
New Revision: 1070520

URL: http://svn.apache.org/viewvc?rev=1070520&view=rev
Log:
Initial commit of Flatpack feature (see WOOKIE-182) with some tests to show how the code works. This is early days - still to do are persisting links between Widget objects and their original .wgt archives, handling flattened Features, and the API & UI for exposing the capability to clients

Added:
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/FlatpackFactoryTest.java
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/PreferenceMock.java
    incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/WidgetInstanceMock.java
    incubator/wookie/trunk/src/org/apache/wookie/flatpack/
    incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackFactory.java
    incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackProcessor.java

Added: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/FlatpackFactoryTest.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/FlatpackFactoryTest.java?rev=1070520&view=auto
==============================================================================
--- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/FlatpackFactoryTest.java (added)
+++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/FlatpackFactoryTest.java Mon Feb 14 15:25:46 2011
@@ -0,0 +1,260 @@
+/*
+ *  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.
+ */
+
+package org.apache.wookie.tests.flatpack;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.ArrayList;
+
+import org.apache.wookie.beans.IPreference;
+import org.apache.wookie.beans.IWidgetInstance;
+import org.apache.wookie.flatpack.FlatpackFactory;
+import org.apache.wookie.w3c.W3CWidget;
+import org.apache.wookie.w3c.W3CWidgetFactory;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * Tests the FlatpackFactory functionalty.
+ * @author scottbw@apache.org
+ *
+ */
+public class FlatpackFactoryTest {
+	
+	static File download;
+	static File output;
+	static File flatpack;
+	
+	/*
+	 * Create some temporary folders
+	 */
+	@BeforeClass
+	public static void setup() throws IOException{
+		download = File.createTempFile("wookie-download", "wgt");
+		output = File.createTempFile("wookie-output", "tmp");
+		flatpack = File.createTempFile("wookie-flatpack", "");
+	}
+	
+	/*
+	 * Setup temporary folders before using
+	 */
+	@Before
+	public void setupEach(){
+		if (download.exists()) download.delete();
+		if (output.exists()) output.delete();
+		if (flatpack.exists()) flatpack.delete();
+		output.mkdir();
+		flatpack.mkdir();
+	}
+	
+	/*
+	 * Delete temporary folders
+	 */
+	@AfterClass
+	public static void tearDown(){
+		download.delete();
+		output.delete();
+		flatpack.delete();
+	}
+	
+	@Test(expected=Exception.class)
+	public void testNoInstance() throws Exception{
+		FlatpackFactory flatfac = new FlatpackFactory(null);
+		flatfac.setInputWidget(null); // this is the original .wgt
+		File file = flatfac.pack(); // Get the new .wgt file	
+	}
+
+	/**
+	 * Test creating a flatpack for an instance of a widget using the factory defaults
+	 * @throws Exception
+	 */
+	@Test
+	public void createBasicFlatpackUsingDefaults() throws Exception{
+		// upload a new widget to test with
+		W3CWidgetFactory fac = getFactory();
+		File testWidget = new File("build/widgets/bubbles.wgt");
+		fac.parse(testWidget);
+		download = fac.getUnzippedWidgetDirectory(); //download is where we unzipped the widget
+		
+		// Create an instance of it
+		IWidgetInstance instance = new WidgetInstanceMock();
+		
+		// Flatpack it
+		FlatpackFactory flatfac = new FlatpackFactory(instance);
+		flatfac.setInputWidget(testWidget); // this is the original .wgt
+		File file = flatfac.pack(); // Get the new .wgt file
+	
+		// Test it works!
+		System.out.println(file.getAbsolutePath());
+		W3CWidget fpWidget = fac.parse(file);
+		assertNotNull(fpWidget);
+		
+	}
+	
+	/**
+	 * Test creating a flatpack for an instance of a widget
+	 * @throws Exception
+	 */
+	@Test
+	public void createBasicFlatpack() throws Exception{
+		// upload a new widget to test with
+		W3CWidgetFactory fac = getFactory();
+		File testWidget = new File("build/widgets/bubbles.wgt");
+		fac.parse(testWidget);
+		download = fac.getUnzippedWidgetDirectory(); //download is where we unzipped the widget
+		
+		// Create an instance of it
+		IWidgetInstance instance = new WidgetInstanceMock();
+		
+		// Flatpack it
+		FlatpackFactory flatfac = new FlatpackFactory(instance);
+		flatfac.setParser(fac);
+		flatfac.setInputWidget(testWidget); // this is the original .wgt
+		flatfac.setFlatpackFolder(flatpack); // flatpack is our new temp folder. This would probably be "/flatpack" or similar in deployment
+		File file = flatfac.pack(); // Get the new .wgt file
+	
+		// Test it works!
+		System.out.println(file.getAbsolutePath());
+		W3CWidget fpWidget = fac.parse(file);
+		assertNotNull(fpWidget);
+		
+	}
+	
+	/**
+	 * Test that we add preference defaults from an instance
+	 * @throws Exception
+	 */
+	@Test
+	public void createFlatpackWithPreferences() throws Exception{
+		
+		// upload a new widget to test with
+		W3CWidgetFactory fac = getFactory();
+		File testWidget = new File("build/widgets/bubbles.wgt");
+		fac.parse(testWidget);
+		download = fac.getUnzippedWidgetDirectory(); //download is where we unzipped the widget
+		
+		// Create an instance of it
+		IWidgetInstance instance = new WidgetInstanceMock();
+		ArrayList<IPreference> prefs = new ArrayList<IPreference>();
+		IPreference pref = new PreferenceMock();
+		pref.setDkey("hiScore");
+		pref.setDvalue("1000");
+		pref.setReadOnly(false);
+		prefs.add(pref);
+		instance.setPreferences(prefs);
+		
+		// Flatpack it
+		FlatpackFactory flatfac = new FlatpackFactory(instance);
+		flatfac.setParser(fac);
+		flatfac.setInputWidget(testWidget); // this is the original .wgt
+		flatfac.setFlatpackFolder(flatpack); // flatpack is our new temp folder. This would probably be "/flatpack" or similar in deployment
+		File file = flatfac.pack(); // Get the new .wgt file
+	
+		// Test it works!
+		System.out.println(file.getAbsolutePath());
+		W3CWidget fpWidget = fac.parse(file);
+		assertEquals("hiScore", fpWidget.getPrefences().get(0).getName());
+		assertEquals("1000", fpWidget.getPrefences().get(0).getValue());
+	}
+	
+	
+	/**
+	 * Test that we add preference defaults from an instance, but don't duplicate
+	 * existing preferences.
+	 * 
+	 * @throws Exception
+	 */
+	@Test
+	public void createFlatpackWithPreferences2() throws Exception{
+		
+		// upload a new widget to test with
+		W3CWidgetFactory fac = getFactory();
+		fac.setFeatures(new String[]{"http://wave.google.com"});
+		File testWidget = new File("build/widgets/simplechat.wgt");
+		fac.parse(testWidget);
+		download = fac.getUnzippedWidgetDirectory(); //download is where we unzipped the widget
+		
+		// Create an instance of it
+		IWidgetInstance instance = new WidgetInstanceMock();
+		ArrayList<IPreference> prefs = new ArrayList<IPreference>();
+		IPreference pref = new PreferenceMock();
+		pref.setDkey("moderator");
+		pref.setDvalue("true");
+		pref.setReadOnly(false);
+		prefs.add(pref);
+		instance.setPreferences(prefs);
+		
+		// Flatpack it
+		FlatpackFactory flatfac = new FlatpackFactory(instance);
+		flatfac.setParser(fac);
+		flatfac.setInputWidget(testWidget); // this is the original .wgt
+		flatfac.setFlatpackFolder(flatpack); // flatpack is our new temp folder. This would probably be "/flatpack" or similar in deployment
+		File file = flatfac.pack(); // Get the new .wgt file
+	
+		// Test it works!
+		System.out.println(file.getAbsolutePath());
+		W3CWidget fpWidget = fac.parse(file);
+		assertEquals("moderator", fpWidget.getPrefences().get(0).getName());
+		assertEquals("true", fpWidget.getPrefences().get(0).getValue());
+		assertEquals(1,fpWidget.getPrefences().size());
+	}
+	
+	/**
+	 * Test that we can add WAC feature
+	 * @throws Exception
+	 */
+	@Test
+	public void createFlatpackWithWAC() throws Exception{
+		
+		// upload a new widget to test with
+		W3CWidgetFactory fac = getFactory();
+		File testWidget = new File("build/widgets/bubbles.wgt");
+		fac.parse(testWidget);
+		download = fac.getUnzippedWidgetDirectory(); //download is where we unzipped the widget
+		
+		// Create an instance of it
+		IWidgetInstance instance = new WidgetInstanceMock();
+		
+		// Flatpack it
+		FlatpackFactory flatfac = new FlatpackFactory(instance);
+		flatfac.setParser(fac);
+		flatfac.setIncludeWacFeatures(true);
+		flatfac.setInputWidget(testWidget); // this is the original .wgt
+		flatfac.setFlatpackFolder(flatpack); // flatpack is our new temp folder. This would probably be "/flatpack" or similar in deployment
+		File file = flatfac.pack(); // Get the new .wgt file
+	
+		// Test it works!
+		System.out.println(file.getAbsolutePath());
+		fac.setFeatures(new String[]{"http://jil.org/jil/api/1.1/widget"});
+		W3CWidget fpWidget = fac.parse(file);
+		assertEquals("http://jil.org/jil/api/1.1/widget", fpWidget.getFeatures().get(0).getName());
+	}
+
+	/*
+	 * Construct a standard W3CWidgetFactory for testing
+	 */
+	private W3CWidgetFactory getFactory() throws Exception{
+		W3CWidgetFactory fac = new W3CWidgetFactory();
+		fac.setLocalPath("/widgets");
+		fac.setEncodings(new String[]{"UTF-8", "ISO-8859-1","Windows-1252"});
+		fac.setOutputDirectory(output.getAbsolutePath());
+		return fac;
+	}
+}

Added: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/PreferenceMock.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/PreferenceMock.java?rev=1070520&view=auto
==============================================================================
--- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/PreferenceMock.java (added)
+++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/PreferenceMock.java Mon Feb 14 15:25:46 2011
@@ -0,0 +1,80 @@
+/*
+ *  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.
+ */
+package org.apache.wookie.tests.flatpack;
+
+import org.apache.wookie.beans.IPreference;
+
+/**
+ * Mock used for testing preferences
+ * @author scottbw@apache.org
+ *
+ */
+public class PreferenceMock implements IPreference {
+	
+	private Object id;
+	private String key;
+	private String value;
+	private boolean isReadOnly;
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IBean#getId()
+	 */
+	public Object getId() {
+		return id;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IPreference#getDkey()
+	 */
+	public String getDkey() {
+
+		return key;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IPreference#setDkey(java.lang.String)
+	 */
+	public void setDkey(String dkey) {
+		key = dkey;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IPreference#getDvalue()
+	 */
+	public String getDvalue() {
+		return value;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IPreference#setDvalue(java.lang.String)
+	 */
+	public void setDvalue(String dvalue) {
+		value = dvalue;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IPreference#isReadOnly()
+	 */
+	public boolean isReadOnly() {
+		return isReadOnly;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IPreference#setReadOnly(boolean)
+	 */
+	public void setReadOnly(boolean readOnly) {
+		isReadOnly = readOnly;
+	}
+
+}

Added: incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/WidgetInstanceMock.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/WidgetInstanceMock.java?rev=1070520&view=auto
==============================================================================
--- incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/WidgetInstanceMock.java (added)
+++ incubator/wookie/trunk/src-tests/org/apache/wookie/tests/flatpack/WidgetInstanceMock.java Mon Feb 14 15:25:46 2011
@@ -0,0 +1,290 @@
+/*
+ *  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.
+ */
+package org.apache.wookie.tests.flatpack;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.wookie.beans.IPreference;
+import org.apache.wookie.beans.ISharedData;
+import org.apache.wookie.beans.IToken;
+import org.apache.wookie.beans.IWidget;
+import org.apache.wookie.beans.IWidgetInstance;
+
+/**
+ * Mock used for simulating a Widget Instance in tests
+ * @author scottbw@apache.org
+ */
+public class WidgetInstanceMock implements IWidgetInstance {
+	
+	private Object id;
+	private String lang;
+	private IWidget widget;
+	private String shareddatakey;
+	private boolean hidden;
+	private String idkey;
+	private String apikey;
+	private String ostoken;
+	private String userid;
+	
+	private ArrayList<ISharedData> sharedData;
+	private ArrayList<IPreference> preferences;
+	
+	public WidgetInstanceMock(){
+		sharedData = new ArrayList<ISharedData>();
+		preferences = new ArrayList<IPreference>();
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IBean#getId()
+	 */
+	public Object getId() {
+		return id;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.w3c.ILocalizedElement#getLang()
+	 */
+	public String getLang() {
+		return lang;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#getWidget()
+	 */
+	public IWidget getWidget() {
+		return widget;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setWidget(org.apache.wookie.beans.IWidget)
+	 */
+	public void setWidget(IWidget widget) {
+		this.widget = widget;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setLang(java.lang.String)
+	 */
+	public void setLang(String lang) {
+		this.lang = lang;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#getSharedDataKey()
+	 */
+	public String getSharedDataKey() {
+		return shareddatakey;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setSharedDataKey(java.lang.String)
+	 */
+	public void setSharedDataKey(String sharedDataKey) {
+		shareddatakey = sharedDataKey;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#isHidden()
+	 */
+	public boolean isHidden() {
+		return false;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setHidden(boolean)
+	 */
+	public void setHidden(boolean hidden) {
+		this.hidden = hidden;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#getIdKey()
+	 */
+	public String getIdKey() {
+		return idkey;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setIdKey(java.lang.String)
+	 */
+	public void setIdKey(String idkey) {
+		this.idkey = idkey;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#getApiKey()
+	 */
+	public String getApiKey() {
+		return apikey;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setApiKey(java.lang.String)
+	 */
+	public void setApiKey(String apikey) {
+		this.apikey = apikey;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#getOpensocialToken()
+	 */
+	public String getOpensocialToken() {
+		return ostoken;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setOpensocialToken(java.lang.String)
+	 */
+	public void setOpensocialToken(String opensocialToken) {
+		this.ostoken = opensocialToken;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#getNonce()
+	 */
+	public String getNonce() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setNonce(java.lang.String)
+	 */
+	public void setNonce(String nonce) {
+		// TODO Auto-generated method stub
+
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#isShown()
+	 */
+	public boolean isShown() {
+		return !hidden;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setShown(boolean)
+	 */
+	public void setShown(boolean shown) {
+		this.hidden = !shown;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#isUpdated()
+	 */
+	public boolean isUpdated() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setUpdated(boolean)
+	 */
+	public void setUpdated(boolean updated) {
+		// TODO Auto-generated method stub
+
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#getUserId()
+	 */
+	public String getUserId() {
+		return userid;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setUserId(java.lang.String)
+	 */
+	public void setUserId(String userId) {
+		userid = userId;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#isLocked()
+	 */
+	public boolean isLocked() {
+		// TODO Auto-generated method stub
+		return false;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setLocked(boolean)
+	 */
+	public void setLocked(boolean locked) {
+		// TODO Auto-generated method stub
+
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#getPreferences()
+	 */
+	public Collection<IPreference> getPreferences() {
+		return preferences;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setPreferences(java.util.Collection)
+	 */
+	public void setPreferences(Collection<IPreference> preferences) {
+		this.preferences.clear();
+		this.preferences.addAll(preferences);
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#getTokens()
+	 */
+	public Collection<IToken> getTokens() {
+		// TODO Auto-generated method stub
+		return null;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#setTokens(java.util.Collection)
+	 */
+	public void setTokens(Collection<IToken> tokens) {
+		// TODO Auto-generated method stub
+
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#getSharedData()
+	 */
+	public ISharedData[] getSharedData() {
+		return this.sharedData.toArray(new ISharedData[sharedData.size()]);
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#getSharedData(java.lang.String)
+	 */
+	public ISharedData getSharedData(String name) {
+		for (ISharedData data:this.sharedData){
+			if (data.getDkey().equals(name)) return data;
+		}
+		return null;
+	}
+
+	/* (non-Javadoc)
+	 * @see org.apache.wookie.beans.IWidgetInstance#getPreference(java.lang.String)
+	 */
+	public IPreference getPreference(String key) {
+		for (IPreference pref: this.preferences){
+			if (pref.getDkey().equals(key)){
+				return pref;
+			}
+		}
+		return null;
+	}
+
+}

Added: incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackFactory.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackFactory.java?rev=1070520&view=auto
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackFactory.java (added)
+++ incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackFactory.java Mon Feb 14 15:25:46 2011
@@ -0,0 +1,220 @@
+/*
+ *  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.
+ */
+package org.apache.wookie.flatpack;
+
+import java.io.File;
+import java.io.IOException;
+
+import org.apache.wookie.beans.IPreference;
+import org.apache.wookie.beans.IWidgetInstance;
+import org.apache.wookie.w3c.IPreferenceEntity;
+import org.apache.wookie.w3c.W3CWidget;
+import org.apache.wookie.w3c.W3CWidgetFactory;
+import org.apache.wookie.w3c.impl.FeatureEntity;
+import org.apache.wookie.w3c.impl.PreferenceEntity;
+import org.apache.wookie.w3c.util.WidgetOutputter;
+import org.apache.wookie.w3c.util.WidgetPackageUtils;
+
+/**
+ * Factory class for creating flatpacks - Widgets re-packaged with Widget Instance information and exported as a .wgt package.
+ * 
+ * <p>For example, this can be used to create an exported Widget for side-loading into a mobile widget runtime.</p>
+ * 
+ * <p>Factory properties:</p>
+ * 
+ * <dl>
+ *   <dt>parser</dt>
+ *   <dd>The W3CWidgetFactory to use to parse the widget. If this is not specified, DEFAULT_PARSER will be used.</dd>
+ *   <dt>instance</dt>
+ *   <dd>The Widget Instance to be flatpacked.</dd>
+ *   <dt>flatpackFolder</dt>
+ *   <dd>The folder on the file system where the flatpacked Widget package should be saved. If this is not specified, DEFAULT_FLATPACK_FOLDER will be used</dd>
+ *   <dt>includeWacFeatures</dt>
+ *   <dd>If set to true, the exported Widget package will include core WAC/JIL feature elements. This is false by default.</dd>
+ *   <dt>featuresToFlatten</dt>
+ *   <dd>The features that should be "flattened" rather than omitted - that is, that should be injected into the final package.</dd>
+ * </dl>
+ * @author scottbw@apache.org
+ *
+ */
+public class FlatpackFactory {
+	
+	public static final W3CWidgetFactory DEFAULT_PARSER = createDefaultParser();
+	public static final File DEFAULT_FLATPACK_FOLDER = new File("export");
+	private static final String DEFAULT_LOCAL_PATH = "/widgets"; // The default local path to use
+	private static final String WAC_FEATURE_NAME = "http://jil.org/jil/api/1.1/widget";
+	
+	private W3CWidgetFactory parser; // the widget parser to use
+	private IWidgetInstance instance; // the instance of the widget to flatpack
+	private File inputWidget; // the source .wgt file for the instance
+	private File flatpackFolder; // the folder where we put all our flatpack .wgt files once we've created them
+	private boolean includeWacFeatures = false; // set to true if the WAC/JIL core features should be added
+	
+	/**
+	 * Constructor, takes a Widget Instance as its argument
+	 * @param instance the instance to flatpack
+	 */
+	public FlatpackFactory(IWidgetInstance instance){
+		this.instance = instance;
+	}
+	
+	/**
+	 * Packages a widget instance into a new .wgt Widget package
+	 * @return the widget file
+	 * @throws Exception
+	 */
+	public File pack() throws Exception{
+		// Verify configuration and apply defaults
+		if (instance == null) throw new Exception("No instance specified");
+		if (flatpackFolder == null) flatpackFolder = DEFAULT_FLATPACK_FOLDER;
+		if (inputWidget == null){
+			// TODO try to locate the widget upload package from the WidgetInstance
+			// for now we just set this manually for testing using setInputWidget()
+			throw new Exception("Input widget not specified or not found");
+		}
+		if (parser == null) parser = DEFAULT_PARSER;
+		
+		// Verify the file locations we're using exist
+		if (!inputWidget.exists()) throw new Exception("Input widget file does not exist:"+inputWidget.getPath());
+		if (!flatpackFolder.exists()){
+			if (!flatpackFolder.mkdir()) throw new Exception("Flatpack folder could not be created:"+flatpackFolder.getPath());
+		}
+		
+		// Create tmp working area
+		File workingArea = File.createTempFile("wookie-flatpack", "");
+		if (workingArea.exists()) workingArea.delete();
+		workingArea.mkdir();
+		
+		// Set the working area for unpacking the widget
+		parser.setOutputDirectory(workingArea.getAbsolutePath());
+		
+		// Parse the widget and unpack it into the working area
+		W3CWidget widget = parser.parse(inputWidget);
+		
+		// Process the config.xml file
+		widget = processWidget(widget);
+		
+		// Save the config.xml
+		WidgetOutputter outputter = new WidgetOutputter();
+		outputter.setWidgetFolder(DEFAULT_LOCAL_PATH);
+		
+		File configXml = new File(parser.getUnzippedWidgetDirectory(), "config.xml");
+		outputter.outputXML(widget, configXml);
+		
+		// Pack up the widget
+		File outputWidget = new File(flatpackFolder,"test.wgt");
+		WidgetPackageUtils.repackZip(parser.getUnzippedWidgetDirectory(), outputWidget);
+		
+		// Delete the working area
+		workingArea.delete();
+		
+		return outputWidget;
+	}
+	
+	/**
+	 * Processes the Widget object, for example adding any preferences
+	 * set in the Widget Instance
+	 * @param widget the widget to process
+	 * @return the processed widget
+	 */
+	private W3CWidget processWidget(W3CWidget widget){
+		// Add each preferences from the instance to the widget
+		for (IPreference pref: instance.getPreferences()){
+			PreferenceEntity newPref = (PreferenceEntity)getPreference(pref.getDkey(), widget);
+			newPref.setValue(pref.getDvalue());
+			newPref.setReadOnly(pref.isReadOnly());
+			widget.getPrefences().add(newPref);
+		}
+		
+		// TODO Remove any flattened features
+		
+		// Add WAC/JIL features if needed
+		if (includeWacFeatures){
+			FeatureEntity wac = new FeatureEntity(WAC_FEATURE_NAME, true);
+			widget.getFeatures().add(wac);
+		}
+		
+		return widget;
+	}
+	
+	/**
+	 * Get the preference entity for the named preference; either use the existing
+	 * one from the configuration or create a new instance if there was no existing
+	 * entity.
+	 * @param name
+	 * @param widget
+	 * @return a preference entity for the named preference
+	 */
+	private IPreferenceEntity getPreference(String name, W3CWidget widget){
+		for (IPreferenceEntity pref:widget.getPrefences()){
+			if (pref.getName().equals(name)) return pref;
+		}
+		PreferenceEntity pref = new PreferenceEntity();
+		pref.setName(name);
+		return pref;
+	}
+
+	/**
+	 * Set the Widget file to flatpack. TODO remove this as the WidgetInstance should be capable of being used to locate the Widget.
+	 * @param inputWidget
+	 */
+	public void setInputWidget(File inputWidget) {
+		this.inputWidget = inputWidget;
+	}
+
+	/**
+	 * Set the folder where flatpacked Widgets should be exported, e.g. "/flatpack" or "/exports"
+	 * @param flatpackFolder
+	 */
+	public void setFlatpackFolder(File flatpackFolder) {
+		this.flatpackFolder = flatpackFolder;
+	}
+	
+	/**
+	 * Set whether to include WAC/JIL features in the flatpacked Widget
+	 * @param includeWacFeatures
+	 */
+	public void setIncludeWacFeatures(boolean includeWacFeatures){
+		this.includeWacFeatures = includeWacFeatures;
+	}
+	
+	/**
+	 * Sets the W3CWidgetFactory to use as the widget parser
+	 * Note that we override the startPageProcessor with FlatpackProcessor
+	 * and rewrite the local path to DEFAULT_LOCAL_PATH.
+	 * @param factory
+	 * @throws IOException 
+	 */
+	public void setParser(W3CWidgetFactory factory) throws IOException{
+		parser = factory;
+		parser.setStartPageProcessor(new FlatpackProcessor(this.instance));
+		parser.setLocalPath(DEFAULT_LOCAL_PATH);
+	}
+	
+	/*
+	 * Construct a standard W3CWidgetFactory parser for testing
+	 */
+	private static W3CWidgetFactory createDefaultParser() {
+		W3CWidgetFactory fac = new W3CWidgetFactory();
+		fac.setLocalPath(DEFAULT_LOCAL_PATH);
+		try {
+			fac.setEncodings(new String[]{"UTF-8"});
+		} catch (Exception e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+		return fac;
+	}	
+
+}

Added: incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackProcessor.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackProcessor.java?rev=1070520&view=auto
==============================================================================
--- incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackProcessor.java (added)
+++ incubator/wookie/trunk/src/org/apache/wookie/flatpack/FlatpackProcessor.java Mon Feb 14 15:25:46 2011
@@ -0,0 +1,59 @@
+/*
+ *  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.
+ */
+package org.apache.wookie.flatpack;
+
+import java.io.File;
+
+import org.apache.wookie.beans.IWidgetInstance;
+import org.apache.wookie.w3c.IContentEntity;
+import org.apache.wookie.w3c.IStartPageProcessor;
+import org.apache.wookie.w3c.W3CWidget;
+
+/**
+ * Flatpack Processor
+ * 
+ * This class is used to help create a "Flatpack" - a .wgt archive that can also include WidgetInstance 
+ * information.
+ * 
+ * This class is invoked by the W3CWidgetFactory class when invoked by FlatpackFactory to unpack a
+ * Widget. The purpose of this processor is to modify the HTML start files in the Widget package,
+ * injecting scripts only for the features set in the includedFeatures array.
+ * 
+ * NOTE: At the moment this class doesn't actually do _anything_ as we haven't decided how to flatten features
+ * 
+ * @author scottbw@apache.org
+ *
+ */
+public class FlatpackProcessor  implements	IStartPageProcessor {
+	
+	private IWidgetInstance instance;
+
+	/**
+	 * Constructs a FlatpackProcessor taking a WidgetInstance as the constructor argument.
+	 * @param instance
+	 */
+	public FlatpackProcessor(IWidgetInstance instance) {
+		this.instance = instance;
+	}
+
+	/**
+	 * Processes the start file.
+	 * @param startFile the HTML file to process
+	 * @param model the Widget object to apply
+	 * @content the Content element to apply
+	 * TODO implement
+	 */
+	public void processStartFile(File startFile, W3CWidget model,IContentEntity content) throws Exception {
+	}
+}