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/04/28 19:20:38 UTC

svn commit: r1097554 - in /incubator/wookie/trunk/parser/java: src-test/org/apache/wookie/w3c/test/ src/org/apache/wookie/w3c/ src/org/apache/wookie/w3c/impl/ src/org/apache/wookie/w3c/updates/

Author: scottbw
Date: Thu Apr 28 17:20:38 2011
New Revision: 1097554

URL: http://svn.apache.org/viewvc?rev=1097554&view=rev
Log:
Added support for supplying a "default identifier" when parsing a widget package. This is important for the Widget Updates feature (see WOOKIE-103), as the updated widget may not contain an identifier element, or may have one that differs from the original for some reason, however we need to ensure the correct folders are written to with the new content.

Modified:
    incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/EntityTest.java
    incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/W3CWidgetFactory.java
    incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/impl/WidgetManifestModel.java
    incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/UpdateUtils.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=1097554&r1=1097553&r2=1097554&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 Thu Apr 28 17:20:38 2011
@@ -148,12 +148,12 @@ 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,null);
+		WidgetManifestModel widget = new WidgetManifestModel("<widget xmlns=\""+IW3CXMLConfiguration.MANIFEST_NAMESPACE+"\"><name>test</name></widget>",null,null,null,null,null);
 		assertNull(widget.getAuthor());
 		assertEquals("test",widget.getLocalName("en"));
 		assertEquals("floating",widget.getViewModes());
 		
-		widget = new WidgetManifestModel("<widget xmlns=\""+IW3CXMLConfiguration.MANIFEST_NAMESPACE+"\" viewmodes=\"fullscreen\"></widget>",null,null,null,null);
+		widget = new WidgetManifestModel("<widget xmlns=\""+IW3CXMLConfiguration.MANIFEST_NAMESPACE+"\" viewmodes=\"fullscreen\"></widget>",null,null,null,null,null);
 		assertNull(widget.getAuthor());
 		assertEquals(IW3CXMLConfiguration.UNKNOWN,widget.getLocalName("en"));
 		assertEquals("fullscreen",widget.getViewModes());

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=1097554&r1=1097553&r2=1097554&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 Thu Apr 28 17:20:38 2011
@@ -152,7 +152,22 @@ 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 processWidgetPackage(zipFile);
+		return processWidgetPackage(zipFile, null);
+	}
+	
+	/**
+	 * Parse a given ZipFile and return a W3CWidget object representing the processed information in the package.
+	 * The widget will be saved in the outputFolder.
+	 * 
+	 * @param zipFile
+	 * @param defaultIdentifier a default identifier to use if the widget package does not have an identifier
+	 * @return the widget model
+	 * @throws BadWidgetZipFileException if there is a problem with the zip package
+	 * @throws BadManifestException if there is a problem with the config.xml manifest file in the package
+	 */
+	public W3CWidget parse(final File zipFile, String defaultIdentifier) 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 processWidgetPackage(zipFile, defaultIdentifier);
 	}
 	
 	/**
@@ -187,6 +202,23 @@ public class W3CWidgetFactory {
 	}
 	
 	/**
+	 * Parse a widget at a given URL and return a W3CWidget object representing the processed information in the package.
+	 * The widget will be saved in the outputFolder.
+	 * @param url
+	 * @param ignoreContentType set to true to instruct the parser to ignore invalid content type exceptions
+	 * @param defaultIdentifier an identifier to use if the downloaded widget has no identifier - for example when updating a widget
+	 * @return
+	 * @throws BadWidgetZipFileException if there is a problem with the zip package
+	 * @throws BadManifestException if there is a problem with the config.xml manifest file in the package
+	 * @throws InvalidContentTypeException if the widget has an invalid content type
+	 * @throws IOException if the widget cannot be downloaded
+	 */
+	public W3CWidget parse(final URL url, boolean ignoreContentType, String defaultIdentifier) throws BadWidgetZipFileException, BadManifestException, InvalidContentTypeException, IOException, Exception{
+		File file = download(url,ignoreContentType);
+		return parse(file, defaultIdentifier);
+	}
+	
+	/**
 	 * The standard MIME type for a W3C Widget
 	 */
 	private static final String WIDGET_CONTENT_TYPE = "application/widget";
@@ -217,7 +249,7 @@ public class W3CWidgetFactory {
 		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
@@ -225,7 +257,7 @@ public class W3CWidgetFactory {
 	 * @throws BadWidgetZipFileException
 	 * @throws BadManifestException
 	 */
-	private W3CWidget processWidgetPackage(File zipFile) throws BadWidgetZipFileException, BadManifestException{
+	private W3CWidget processWidgetPackage(File zipFile, String defaultIdentifier) throws BadWidgetZipFileException, BadManifestException{
 		ZipFile zip;
 		try {
 			zip = new ZipFile(zipFile);
@@ -235,7 +267,7 @@ public class W3CWidgetFactory {
 		if (WidgetPackageUtils.hasManifest(zip)){
 			try {
 				// build the model
-				WidgetManifestModel widgetModel = new WidgetManifestModel(WidgetPackageUtils.extractManifest(zip), locales, features, encodings, zip);															
+				WidgetManifestModel widgetModel = new WidgetManifestModel(WidgetPackageUtils.extractManifest(zip), locales, features, encodings, zip, defaultIdentifier);															
 
 				// get the widget identifier
 				String manifestIdentifier = widgetModel.getIdentifier();						

Modified: incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/impl/WidgetManifestModel.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/impl/WidgetManifestModel.java?rev=1097554&r1=1097553&r2=1097554&view=diff
==============================================================================
--- incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/impl/WidgetManifestModel.java (original)
+++ incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/impl/WidgetManifestModel.java Thu Apr 28 17:20:38 2011
@@ -55,6 +55,7 @@ public class WidgetManifestModel extends
 	
 	static Logger fLogger = Logger.getLogger(WidgetManifestModel.class.getName());
 	
+	private String defaultIdentifier;
 	private String fIdentifier;
 	private String fVersion;
 	private Integer fHeight;
@@ -83,7 +84,7 @@ public class WidgetManifestModel extends
 	 * @throws IOException
 	 * @throws BadManifestException
 	 */
-	public WidgetManifestModel (String xmlText, String[] locales, String[] features, String[] encodings, ZipFile zip) throws JDOMException, IOException, BadManifestException {		
+	public WidgetManifestModel (String xmlText, String[] locales, String[] features, String[] encodings, ZipFile zip, String defaultIdentifier) throws JDOMException, IOException, BadManifestException {		
 		super();		
 		this.zip = zip;
 		this.features = features;
@@ -96,6 +97,7 @@ public class WidgetManifestModel extends
 		fAccessList = new ArrayList<IAccessEntity>();
 		fFeaturesList = new ArrayList<IFeatureEntity>();
 		fPreferencesList = new ArrayList<IPreferenceEntity>();
+		this.defaultIdentifier = defaultIdentifier;
 		SAXBuilder builder = new SAXBuilder();
 		Element root;
 		try {
@@ -246,9 +248,14 @@ public class WidgetManifestModel extends
 			fIdentifier = null;
 		}
 		if(fIdentifier == null){
-			//give up & generate one
-			RandomGUID r = new RandomGUID();
-			fIdentifier = "http://incubator.apache.org/wookie/generated/" + r.toString();
+			//is there a default?
+			if (defaultIdentifier != null){
+				fIdentifier = defaultIdentifier;
+			} else {
+				//give up & generate one
+				RandomGUID r = new RandomGUID();
+				fIdentifier = "http://incubator.apache.org/wookie/generated/" + r.toString();
+			}
 		}
 		// VERSION IS OPTIONAL		
 		fVersion = UnicodeUtils.normalizeSpaces(element.getAttributeValue(IW3CXMLConfiguration.VERSION_ATTRIBUTE));

Modified: incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/UpdateUtils.java
URL: http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/UpdateUtils.java?rev=1097554&r1=1097553&r2=1097554&view=diff
==============================================================================
--- incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/UpdateUtils.java (original)
+++ incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/UpdateUtils.java Thu Apr 28 17:20:38 2011
@@ -64,7 +64,7 @@ public class UpdateUtils {
 	 */
 	public static W3CWidget getUpdate(W3CWidgetFactory factory, W3CWidget widget){
 		try {
-			return getUpdate(factory, widget.getUpdate(), widget.getVersion(), false);
+			return getUpdate(factory,  widget.getIdentifier(), widget.getUpdate(), widget.getVersion(), false);
 		} catch (Exception e) {
 			return null;
 		}
@@ -79,7 +79,7 @@ public class UpdateUtils {
 	 */
 	public static W3CWidget getUpdate(W3CWidgetFactory factory, W3CWidget widget, boolean onlyUseHttps){
 		try {
-			return getUpdate(factory, widget.getUpdate(), widget.getVersion(), onlyUseHttps);
+			return getUpdate(factory, widget.getIdentifier(), widget.getUpdate(), widget.getVersion(), onlyUseHttps);
 		} catch (Exception e) {
 			return null;
 		}
@@ -98,11 +98,11 @@ public class UpdateUtils {
 	 * @throws BadWidgetZipFileException 
 	 * @throws InvalidContentTypeException 
 	 */
-	public static W3CWidget getUpdate(W3CWidgetFactory factory, String href, String version, boolean onlyUseHttps) throws InvalidContentTypeException, BadWidgetZipFileException, BadManifestException, IOException, Exception{
+	public static W3CWidget getUpdate(W3CWidgetFactory factory, String identifier, String href, String version, boolean onlyUseHttps) throws InvalidContentTypeException, BadWidgetZipFileException, BadManifestException, IOException, Exception{
 		UpdateDescriptionDocument udd = checkForUpdate(href, version);
 		if (udd == null) return null;
 		if (onlyUseHttps && !udd.getUpdateSource().getProtocol().equalsIgnoreCase("https")) return  null;
-		W3CWidget updatedWidget = factory.parse(udd.getUpdateSource());
+		W3CWidget updatedWidget = factory.parse(udd.getUpdateSource(), false, identifier);
 		return updatedWidget;
 	}