Author: scottbw
Date: Thu Apr 29 10:57:44 2010
New Revision: 939257
URL: http://svn.apache.org/viewvc?rev=939257&view=rev
Log:
Added an Update Description Document parser to the parser module with
supporting test cases; this provides the foundation for supporting the W3C
Widget Updates spec (WOOKIE-103).
Added:
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/UpdateDescriptionDocumentTest.java
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/InvalidUDDException.java
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/UpdateDescriptionDocument.java
Added:
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/UpdateDescriptionDocumentTest.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/UpdateDescriptionDocumentTest.java?rev=939257&view=auto
==============================================================================
---
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/UpdateDescriptionDocumentTest.java
(added)
+++
incubator/wookie/trunk/parser/java/src-test/org/apache/wookie/w3c/test/UpdateDescriptionDocumentTest.java
Thu Apr 29 10:57:44 2010
@@ -0,0 +1,137 @@
+/*
+ * 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.w3c.test;
+
+import static org.junit.Assert.assertEquals;
+
+import java.io.IOException;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+import org.apache.wookie.w3c.IW3CXMLConfiguration;
+import org.apache.wookie.w3c.updates.InvalidUDDException;
+import org.apache.wookie.w3c.updates.UpdateDescriptionDocument;
+import org.jdom.Document;
+import org.jdom.Element;
+import org.junit.Test;
+
+public class UpdateDescriptionDocumentTest {
+
+ @Test
+ public void createDocument() throws InvalidUDDException,
MalformedURLException{
+ UpdateDescriptionDocument udd = new
UpdateDescriptionDocument("test", new
URL("http://incubator.apache.org/wookie"), "1.0");
+ assertEquals("1.0", udd.getVersionTag());
+ assertEquals("http://incubator.apache.org/wookie",
udd.getUpdateSource().toString());
+ assertEquals("test", udd.getDetails("en"));
+ }
+
+ @Test (expected=InvalidUDDException.class)
+ public void emptyDocument() throws InvalidUDDException{
+ UpdateDescriptionDocument udd = new
UpdateDescriptionDocument(null, null, null);
+ Document doc = new Document();
+ udd.fromXML(doc);
+ }
+
+ @Test (expected=InvalidUDDException.class)
+ public void nullDocument() throws InvalidUDDException{
+ UpdateDescriptionDocument udd = new
UpdateDescriptionDocument(null, null, null);
+ udd.fromXML(null);
+ }
+
+ @Test (expected=InvalidUDDException.class)
+ public void invalidRootDocument() throws InvalidUDDException{
+ UpdateDescriptionDocument udd = new
UpdateDescriptionDocument(null, null, null);
+ Document doc = new Document();
+ Element el = new Element("update",
IW3CXMLConfiguration.MANIFEST_NAMESPACE);
+ doc.setRootElement(el);
+ udd.fromXML(doc);
+ }
+
+ @Test (expected=InvalidUDDException.class)
+ public void badNamespaceDocument() throws InvalidUDDException{
+ UpdateDescriptionDocument udd = new
UpdateDescriptionDocument(null, null, null);
+ Document doc = new Document();
+ Element el = new Element("update-info", "http://bogus.net");
+ doc.setRootElement(el);
+ udd.fromXML(doc);
+ }
+
+ @Test (expected=InvalidUDDException.class)
+ public void missingVersionAttrDocument() throws InvalidUDDException{
+ UpdateDescriptionDocument udd = new
UpdateDescriptionDocument(null, null, null);
+ Document doc = new Document();
+ Element el = new Element("update-info",
IW3CXMLConfiguration.MANIFEST_NAMESPACE);
+ el.setAttribute("src","http://incubator.apache.org/wookie");
+ doc.setRootElement(el);
+ udd.fromXML(doc);
+ }
+
+ @Test (expected=InvalidUDDException.class)
+ public void missingSrcAttrDocument() throws InvalidUDDException{
+ UpdateDescriptionDocument udd = new
UpdateDescriptionDocument(null, null, null);
+ Document doc = new Document();
+ Element el = new Element("update-info",
IW3CXMLConfiguration.MANIFEST_NAMESPACE);
+ el.setAttribute("version","1.0");
+ doc.setRootElement(el);
+ udd.fromXML(doc);
+ }
+
+ @Test (expected=InvalidUDDException.class)
+ public void invalidSrcAttrDocument() throws InvalidUDDException{
+ UpdateDescriptionDocument udd = new
UpdateDescriptionDocument(null, null, null);
+ Document doc = new Document();
+ Element el = new Element("update-info",
IW3CXMLConfiguration.MANIFEST_NAMESPACE);
+ el.setAttribute("version","1.0");
+ el.setAttribute("src","!notavaliduri");
+ doc.setRootElement(el);
+ udd.fromXML(doc);
+ }
+
+ @Test
+ public void validDocument() throws InvalidUDDException{
+ UpdateDescriptionDocument udd = new
UpdateDescriptionDocument(null, null, null);
+ Document doc = new Document();
+ Element el = new Element("update-info",
IW3CXMLConfiguration.MANIFEST_NAMESPACE);
+ el.setAttribute("version","1.0");
+
el.setAttribute("src","http://incubator.apache.org/wookie/test.wgt");
+ Element details = new Element("details",
IW3CXMLConfiguration.MANIFEST_NAMESPACE);
+ details.setText("test");
+ el.addContent(details);
+ doc.setRootElement(el);
+ udd.fromXML(doc);
+ assertEquals("1.0", udd.getVersionTag());
+ assertEquals("http://incubator.apache.org/wookie/test.wgt",
udd.getUpdateSource().toString());
+ assertEquals("test", udd.getDetails("en"));
+ }
+
+ @Test
+ public void validRemoteDocument() throws InvalidUDDException,
IOException{
+ UpdateDescriptionDocument udd = new
UpdateDescriptionDocument("http://svn.apache.org/repos/asf/incubator/wookie/trunk/parser/java/src-test/update.xml");
+ assertEquals("1.0", udd.getVersionTag());
+ assertEquals("http://incubator.apache.org/wookie/test.wgt",
udd.getUpdateSource().toString());
+ assertEquals("test", udd.getDetails("en"));
+ }
+
+ @Test (expected = InvalidUDDException.class)
+ public void missingRemoteDocument() throws InvalidUDDException,
IOException{
+ new
UpdateDescriptionDocument("http://incubator.apache.org/wookie/nosuchdoc");
+ }
+
+ @Test (expected = InvalidUDDException.class)
+ public void invalidRemoteDocument() throws InvalidUDDException,
IOException{
+ new
UpdateDescriptionDocument("http://incubator.apache.org/wookie/index.data/wookie-overview-sm.png");
+ }
+
+
+}
Added:
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/InvalidUDDException.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/InvalidUDDException.java?rev=939257&view=auto
==============================================================================
---
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/InvalidUDDException.java
(added)
+++
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/InvalidUDDException.java
Thu Apr 29 10:57:44 2010
@@ -0,0 +1,27 @@
+/*
+ * 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.w3c.updates;
+
+/**
+ * An exception thrown when an Update Description Document is not valid
+ */
+public class InvalidUDDException extends Exception {
+
+ private static final long serialVersionUID = -6638297159167656522L;
+
+ public InvalidUDDException(String message) {
+ super(message);
+ }
+
+}
Added:
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/UpdateDescriptionDocument.java
URL:
http://svn.apache.org/viewvc/incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/UpdateDescriptionDocument.java?rev=939257&view=auto
==============================================================================
---
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/UpdateDescriptionDocument.java
(added)
+++
incubator/wookie/trunk/parser/java/src/org/apache/wookie/w3c/updates/UpdateDescriptionDocument.java
Thu Apr 29 10:57:44 2010
@@ -0,0 +1,142 @@
+/*
+ * 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.w3c.updates;
+
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.ArrayList;
+import java.util.List;
+
+import org.apache.wookie.w3c.IW3CXMLConfiguration;
+import org.apache.wookie.w3c.impl.AbstractLocalizedEntity;
+import org.apache.wookie.w3c.util.LocalizationUtils;
+import org.jdom.Document;
+import org.jdom.Element;
+import org.jdom.Namespace;
+import org.jdom.input.SAXBuilder;
+
+/**
+ * An update description document (UDD) as defined at
http://www.w3.org/TR/widgets-updates
+ */
+public class UpdateDescriptionDocument{
+
+ private URL updateSource;
+ private String versionTag;
+ private ArrayList<Details> details;
+
+ /**
+ * Get the details of the update, typically a short description of any
new features.
+ * @param locale the preferred locale for the details
+ * @return the update details
+ */
+ public String getDetails(String locale) {
+ Details localDetails = (Details)
LocalizationUtils.getLocalizedElement(details.toArray(new
Details[details.size()]), new String[]{locale});
+ return localDetails.text;
+ }
+
+ /**
+ * Get the URL for the updated widget
+ * @return the URL
+ */
+ public URL getUpdateSource() {
+ return updateSource;
+ }
+
+ /**
+ * Get the version tag for the update
+ * @return the version tag
+ */
+ public String getVersionTag() {
+ return versionTag;
+ }
+
+ /**
+ * Manually construct a document. Only used for testing.
+ * @param details
+ * @param updateSource
+ * @param versionTag
+ */
+ public UpdateDescriptionDocument(String details, URL updateSource,
String versionTag){
+ this.details = new ArrayList<Details>();
+ Details d = new Details();
+ d.text = details;
+ this.details.add(d);
+ this.updateSource = updateSource;
+ this.versionTag = versionTag;
+ }
+
+ /**
+ * Load a UDD from a URL
+ * @param href the URL to load the UDD from
+ * @throws InvalidUDDException if the UDD cannot be found, or is not
valid
+ */
+ public UpdateDescriptionDocument(String href) throws
InvalidUDDException{
+ try {
+ URL url = new URL(href);
+ Document doc;
+ doc = new SAXBuilder().build(url);
+ fromXML(doc);
+ } catch (Exception e) {
+ throw new InvalidUDDException("the document is not a
valid UDD");
+ }
+ }
+
+ /**
+ * Parse a UDD from XML
+ * @param document the XML document to parse
+ * @throws InvalidUDDException if the document is not a valid UDD
+ */
+ public void fromXML(Document document) throws InvalidUDDException{
+ if (document == null) throw new InvalidUDDException("No
document found");
+ Element root;
+ try {
+ root = document.getRootElement();
+ } catch (Exception e1) {
+ throw new InvalidUDDException("Root element must be
<update-info>");
+ }
+ if (!root.getName().equals("update-info")) throw new
InvalidUDDException("Root element must be <update-info>");
+ if (root.getNamespace() !=
Namespace.getNamespace(IW3CXMLConfiguration.MANIFEST_NAMESPACE)) throw new
InvalidUDDException("Wrong namespace for Update Description Document");
+ if (root.getAttribute("version") == null) throw new
InvalidUDDException("no version attribute");
+ if (root.getAttribute("src") == null) throw new
InvalidUDDException("no src attribute");
+ versionTag = root.getAttributeValue("version");
+ try {
+ updateSource = new URL(root.getAttributeValue("src"));
+ } catch (MalformedURLException e) {
+ throw new InvalidUDDException("src attribute is not a
valid URL");
+ }
+ List<?> detailsElements = root.getChildren("details",
Namespace.getNamespace(IW3CXMLConfiguration.MANIFEST_NAMESPACE));
+ this.details = new ArrayList<Details>();
+ for (Object o: detailsElements){
+ Details detailsItem = new Details();
+ detailsItem.fromXML((Element) o);
+ this.details.add(detailsItem);
+ }
+ }
+
+ /**
+ * Inner class used to represent Details as a localized entity
+ */
+ class Details extends AbstractLocalizedEntity{
+ public String text;
+
+ public Details(){
+ }
+
+ @Override
+ public void fromXML(Element element) {
+ super.fromXML(element);
+ this.text = getLocalizedTextContent(element);
+ }
+ }
+}