Return-Path: Delivered-To: apmail-commons-commits-archive@locus.apache.org Received: (qmail 49100 invoked from network); 21 Nov 2008 20:59:48 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 21 Nov 2008 20:59:48 -0000 Received: (qmail 88527 invoked by uid 500); 21 Nov 2008 20:59:56 -0000 Delivered-To: apmail-commons-commits-archive@commons.apache.org Received: (qmail 88477 invoked by uid 500); 21 Nov 2008 20:59:56 -0000 Mailing-List: contact commits-help@commons.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@commons.apache.org Delivered-To: mailing list commits@commons.apache.org Received: (qmail 88468 invoked by uid 99); 21 Nov 2008 20:59:56 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 21 Nov 2008 12:59:56 -0800 X-ASF-Spam-Status: No, hits=-2000.0 required=10.0 tests=ALL_TRUSTED X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Fri, 21 Nov 2008 20:58:31 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 5D306238889C; Fri, 21 Nov 2008 12:58:47 -0800 (PST) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r719710 - in /commons/proper/scxml/branches/J6/src: main/java/org/apache/commons/scxml/io/ main/java/org/apache/commons/scxml/model/ test/java/org/apache/commons/scxml/io/ Date: Fri, 21 Nov 2008 20:58:47 -0000 To: commits@commons.apache.org From: rahul@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20081121205847.5D306238889C@eris.apache.org> X-Virus-Checked: Checked by ClamAV on apache.org Author: rahul Date: Fri Nov 21 12:58:46 2008 New Revision: 719710 URL: http://svn.apache.org/viewvc?rev=719710&view=rev Log: Porting r719709 from trunk. SCXMLSerializer does not serialize custom namespace declarations. Partial fix, with assumptions / limitations added to class Javadoc. SCXML-88 Modified: commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/io/SCXMLParser.java commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/io/SCXMLSerializer.java commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/model/SCXML.java commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/io/SCXMLSerializerTest.java Modified: commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/io/SCXMLParser.java URL: http://svn.apache.org/viewvc/commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/io/SCXMLParser.java?rev=719710&r1=719709&r2=719710&view=diff ============================================================================== --- commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/io/SCXMLParser.java (original) +++ commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/io/SCXMLParser.java Fri Nov 21 12:58:46 2008 @@ -727,6 +727,7 @@ //// SCXML scxmlRules.add(XP_SM, new ObjectCreateRule(SCXML.class)); scxmlRules.add(XP_SM, new SetPropertiesRule()); + scxmlRules.add(XP_SM, new SetCurrentNamespacesRule()); //// Datamodel at document root i.e. datamodel addDatamodelRules(XP_SM + XPF_DM, scxmlRules, scxml, pr); Modified: commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/io/SCXMLSerializer.java URL: http://svn.apache.org/viewvc/commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/io/SCXMLSerializer.java?rev=719710&r1=719709&r2=719710&view=diff ============================================================================== --- commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/io/SCXMLSerializer.java (original) +++ commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/io/SCXMLSerializer.java Fri Nov 21 12:58:46 2008 @@ -48,6 +48,7 @@ import org.apache.commons.scxml.model.Initial; import org.apache.commons.scxml.model.Invoke; import org.apache.commons.scxml.model.Log; +import org.apache.commons.scxml.model.NamespacePrefixesHolder; import org.apache.commons.scxml.model.OnEntry; import org.apache.commons.scxml.model.OnExit; import org.apache.commons.scxml.model.Parallel; @@ -61,11 +62,22 @@ import org.w3c.dom.Node; /** - * Utility class for serializing the Commons SCXML Java object + *

Utility class for serializing the Commons SCXML Java object * model. Class uses the visitor pattern to trace through the * object heirarchy. Used primarily for testing, debugging and - * visual verification. + * visual verification.

* + * NOTE: This serializer makes the following assumptions about the + * original SCXML document(s) parsed to create the object model: + *
    + *
  • The default document namespace is the SCXML namespace: + * http://www.w3.org/2005/07/scxml
  • + *
  • The Commons SCXML namespace + * ( http://commons.apache.org/scxml ), if needed, uses the + * "cs" prefix
  • + *
  • All namespace prefixes needed throughout the document are + * declared on the document root element (<scxml>)
  • + *
*/ public class SCXMLSerializer { @@ -91,8 +103,8 @@ StringBuffer b = new StringBuffer("\n"). append("\n"); if (XFORMER == null) { @@ -102,6 +114,7 @@ + " the document will be skipped since a suitable" + " JAXP Transformer could not be instantiated."); } + b.append(INDENT).append("\n"); Datamodel dm = scxml.getDatamodel(); if (dm != null) { serializeDatamodel(b, dm, INDENT); @@ -573,6 +586,41 @@ } /** + * Serialize namespace declarations for the root SCXML element. + * + * @param holder The {@link NamespacePrefixesHolder} object + */ + private static String serializeNamespaceDeclarations( + final NamespacePrefixesHolder holder) { + Map namespaces = holder.getNamespaces(); + StringBuffer b = new StringBuffer(); + if (namespaces != null) { + for (Map.Entry entry : namespaces.entrySet()) { + String prefix = entry.getKey(); + String nsURI = entry.getValue(); + if (prefix.length() == 0 && !nsURI.equals(NAMESPACE_SCXML)) { + org.apache.commons.logging.Log log = LogFactory. + getLog(SCXMLSerializer.class); + log.warn("When using the SCXMLSerializer, the default " + + "namespace must be the SCXML namespace:" + + NAMESPACE_SCXML); + } if (prefix.equals("cs") && + !nsURI.equals(NAMESPACE_COMMONS_SCXML)) { + org.apache.commons.logging.Log log = LogFactory. + getLog(SCXMLSerializer.class); + log.warn("When using the SCXMLSerializer, the namespace" + + "prefix \"cs\" must bind to the Commons SCXML " + + "namespace:" + NAMESPACE_COMMONS_SCXML); + } else if (prefix.length() > 0) { + b.append(" xmlns:").append(prefix).append("=\""). + append(nsURI).append("\""); + } + } + } + return b.toString(); + } + + /** * Get a Transformer instance. * * @return Transformer The Transformer instance. Modified: commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/model/SCXML.java URL: http://svn.apache.org/viewvc/commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/model/SCXML.java?rev=719710&r1=719709&r2=719710&view=diff ============================================================================== --- commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/model/SCXML.java (original) +++ commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/model/SCXML.java Fri Nov 21 12:58:46 2008 @@ -29,7 +29,8 @@ * root". * */ -public class SCXML implements Serializable, Observable { +public class SCXML implements Serializable, Observable, + NamespacePrefixesHolder { /** * Serial version UID. @@ -81,6 +82,12 @@ private Map targets; /** + * The XML namespaces defined on the SCXML document root node, + * preserved primarily for serialization. + */ + private Map namespaces; + + /** * Constructor. */ public SCXML() { @@ -258,6 +265,27 @@ } /** + * Get the namespace definitions specified on the SCXML element. + * May be null. + * + * @return The namespace definitions specified on the SCXML element, + * may be null. + */ + public final Map getNamespaces() { + return namespaces; + } + + /** + * Set the namespace definitions specified on the SCXML element. + * + * @param namespaces The namespace definitions specified on the + * SCXML element. + */ + public final void setNamespaces(final Map namespaces) { + this.namespaces = namespaces; + } + + /** * Get the ID of the initial state. * * @return String Returns the initial state ID (used by XML Digester only). Modified: commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/io/SCXMLSerializerTest.java URL: http://svn.apache.org/viewvc/commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/io/SCXMLSerializerTest.java?rev=719710&r1=719709&r2=719710&view=diff ============================================================================== --- commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/io/SCXMLSerializerTest.java (original) +++ commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/io/SCXMLSerializerTest.java Fri Nov 21 12:58:46 2008 @@ -17,7 +17,9 @@ package org.apache.commons.scxml.io; import java.util.ArrayList; +import java.util.LinkedHashMap; import java.util.List; +import java.util.Map; import junit.framework.Test; import junit.framework.TestCase; @@ -58,13 +60,21 @@ public void testSerializeSCXMLNoStates() { SCXML scxml = new SCXML(); + Map namespaces = new LinkedHashMap(); + namespaces.put("", "http://www.w3.org/2005/07/scxml"); + namespaces.put("cs", "http://commons.apache.org/scxml"); + namespaces.put("foo", "http://f.o.o"); + namespaces.put("bar", "http://b.a.r"); + scxml.setNamespaces(namespaces); scxml.setVersion("version1"); scxml.setInitial("off"); scxml.addChild(new State()); String assertValue = "\n" + "\n \n \n\n"; + + "xmlns:foo=\"http://f.o.o\" xmlns:bar=\"http://b.a.r\" " + + "version=\"version1\" initial=\"off\">\n \n \n " + + "\n\n"; assertEquals(assertValue, SCXMLSerializer.serialize(scxml)); } @@ -328,8 +338,9 @@ scxml.addChild(s1); String assertValue = "\n" - + "\n \n \n\n"; + + "\n \n" + + " \n \n\n"; assertEquals(assertValue, SCXMLSerializer.serialize(scxml)); } @@ -365,8 +376,9 @@ scxml.addChild(par); String assertValue = "\n" - + "\n" + + " \n" + " \n" + " \n" + " \n"