Author: rahul
Date: Mon Jan 26 20:31:22 2009
New Revision: 737824

URL: http://svn.apache.org/viewvc?rev=737824&view=rev
Log:

A first cut at a complete rewrite of the Commons SCXML IO package. JDK 1.6 (or, 
more specifically, JAXP 1.4) finally makes XML-related IO in Java somewhat 
usable. This rewrite is aimed to address couple of higher order points:

 * Eliminate IO package external dependencies. This rewrite has no dependencies 
beyond the JDK (formerly, Commons Digester and thereby, Commons BeanUtils, were 
necessary to use the IO package). This also significantly reduces the need for 
reflection necessary while parsing.
 * Using a pull parser instead of callback based parsing. The new parser uses 
the StAX API developed in JSR-173 and part of JAXP 1.4. The streaming API 
should be easier both on performance and memory. Some quick tests indicate that 
based on the SCXML document chosen, the new parser is between 10 and 25 times 
faster.

This commit also:
 * Updates the rest of the library to use the new implementations (multiple 
related changes in other source files)
 * Adds some missing properties (with necessary getters and setters) to the 
SCXML class
 * Adds a basic XMLReporter implementation
 * Removes the SAX ErrorReporter dependency from the utility methods for test 
cases
 * Improves a couple of error messages
 * Makes a few stylistic improvements in some test cases

The soon to be obsolete classes in the IO package (SCXMLParser and 
SCXMLSerializer) haven't been removed yet, though they aren't referenced by any 
other library or test code. Some more tests and tweaks will go into the new 
implementations before that happens.

Finally, odds and ends:
 * There is a bug in the JAXP 1.4 XMLOutputFactory API that makes it impossible 
to load a custom factory without more magic. The SCXMLWriter class can't allow 
for this facility until the bug is addressed.
 * There seems to be no portable way to pretty print using the JAXP 1.4 
XMLStreamWriter. As a result, a second pass transform (javax.xml.transform) is 
performed if pretty printing is needed. The default is to not pretty print.
 * A package scoped Constants class is probably called for (some redundancy 
there ATM).
 * Some TODOs in code.


Added:
    
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/SimpleXMLReporter.java
   (with props)
    
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/io/SCXMLReader.java
   (with props)
    
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/io/SCXMLWriter.java
   (with props)
    
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/io/SCXMLReaderTest.java
   (contents, props changed)
      - copied, changed from r737162, 
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/io/SCXMLParserTest.java
    
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/io/SCXMLWriterTest.java
   (with props)
Removed:
    
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/io/SCXMLParserTest.java
Modified:
    
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/Builtin.java
    
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/AbstractStateMachine.java
    
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/Tracer.java
    
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/invoke/SimpleSCXMLInvoker.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/io/package.html
    
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/model/History.java
    
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/model/SCXML.java
    
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/test/StandaloneUtils.java
    
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/SCXMLTestHelper.java
    
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/env/StopWatchDisplay.java
    
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/env/javascript/JSEvaluatorTest.java
    
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/env/javascript/JSExampleTest.java
    
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/env/javascript/example-01.xml
    
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/history-default-01.xml
    
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/invoke/InvokeTest.java
    
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/io/IOTestSuite.java
    
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/io/StateSrcTest.java
    
commons/proper/scxml/branches/J6/src/test/java/org/apache/commons/scxml/model/assign-test-01.xml

Modified: 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/Builtin.java
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/Builtin.java?rev=737824&r1=737823&r2=737824&view=diff
==============================================================================
--- 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/Builtin.java
 (original)
+++ 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/Builtin.java
 Mon Jan 26 20:31:22 2009
@@ -122,7 +122,7 @@
         } else {
             if (length > 1) {
                 Log log = LogFactory.getLog(Builtin.class);
-                log.warn("Data(): Multiple nodes matching XPath expression \""
+                log.warn("Data(): Multiple (" + length + ") nodes matching 
XPath expression \""
                     + path + "\", returning first");
             }
             return result.item(0);

Modified: 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/AbstractStateMachine.java
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/AbstractStateMachine.java?rev=737824&r1=737823&r2=737824&view=diff
==============================================================================
--- 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/AbstractStateMachine.java
 (original)
+++ 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/AbstractStateMachine.java
 Mon Jan 26 20:31:22 2009
@@ -21,6 +21,8 @@
 import java.lang.reflect.Method;
 import java.net.URL;
 
+import javax.xml.stream.XMLStreamException;
+
 import org.apache.commons.logging.Log;
 import org.apache.commons.logging.LogFactory;
 import org.apache.commons.scxml.Context;
@@ -30,13 +32,11 @@
 import org.apache.commons.scxml.TriggerEvent;
 import org.apache.commons.scxml.env.jexl.JexlContext;
 import org.apache.commons.scxml.env.jexl.JexlEvaluator;
-import org.apache.commons.scxml.io.SCXMLParser;
+import org.apache.commons.scxml.io.SCXMLReader;
 import org.apache.commons.scxml.model.ModelException;
 import org.apache.commons.scxml.model.SCXML;
 import org.apache.commons.scxml.model.Transition;
 import org.apache.commons.scxml.model.TransitionTarget;
-import org.xml.sax.ErrorHandler;
-import org.xml.sax.SAXException;
 
 /**
  * <p>This class demonstrates one approach for providing the base
@@ -118,14 +118,12 @@
     public AbstractStateMachine(final URL scxmlDocument,
             final Context rootCtx, final Evaluator evaluator) {
         log = LogFactory.getLog(this.getClass());
-        ErrorHandler errHandler = new SimpleErrorHandler();
         try {
-            stateMachine = SCXMLParser.parse(scxmlDocument,
-                errHandler);
+            stateMachine = SCXMLReader.read(scxmlDocument);
         } catch (IOException ioe) {
             logError(ioe);
-        } catch (SAXException sae) {
-            logError(sae);
+        } catch (XMLStreamException xse) {
+            logError(xse);
         } catch (ModelException me) {
             logError(me);
         }

Added: 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/SimpleXMLReporter.java
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/SimpleXMLReporter.java?rev=737824&view=auto
==============================================================================
--- 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/SimpleXMLReporter.java
 (added)
+++ 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/SimpleXMLReporter.java
 Mon Jan 26 20:31:22 2009
@@ -0,0 +1,61 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.commons.scxml.env;
+
+import java.io.Serializable;
+
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLReporter;
+import javax.xml.stream.XMLStreamException;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Custom {...@link XMLReporter} that logs the StAX parsing warnings in the
+ * SCXML document.
+ *
+ * @since 1.0
+ */
+public class SimpleXMLReporter implements XMLReporter, Serializable {
+
+    /** Serial version UID. */
+    private static final long serialVersionUID = 1L;
+
+    /** Log. */
+    private Log log = LogFactory.getLog(getClass());
+
+    /**
+     * Constructor.
+     */
+    public SimpleXMLReporter() {
+        super();
+    }
+
+    /**
+     * @see XMLReporter#report(String, String, Object, Location)
+     */
+    public void report(final String message, final String errorType, final 
Object relatedInformation,
+            final Location location)
+    throws XMLStreamException {
+        if (log.isWarnEnabled()) {
+            log.warn("[" + errorType + "] " + message + " (" + 
relatedInformation + ") at " + location);
+        }
+
+    }
+
+}

Propchange: 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/SimpleXMLReporter.java
------------------------------------------------------------------------------
    svn:eol-style = native

Propchange: 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/SimpleXMLReporter.java
------------------------------------------------------------------------------
    svn:keywords = Date Author Id Revision HeadURL

Modified: 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/Tracer.java
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/Tracer.java?rev=737824&r1=737823&r2=737824&view=diff
==============================================================================
--- 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/Tracer.java
 (original)
+++ 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/env/Tracer.java
 Mon Jan 26 20:31:22 2009
@@ -18,6 +18,10 @@
 
 import java.io.Serializable;
 
+import javax.xml.stream.Location;
+import javax.xml.stream.XMLReporter;
+import javax.xml.stream.XMLStreamException;
+
 import org.apache.commons.scxml.ErrorReporter;
 import org.apache.commons.scxml.SCXMLListener;
 import org.apache.commons.scxml.model.Transition;
@@ -31,7 +35,7 @@
  *
  */
 public class Tracer implements ErrorHandler, ErrorReporter,
-                               SCXMLListener, Serializable {
+                               SCXMLListener, Serializable, XMLReporter {
 
     /** Serial version UID. */
     private static final long serialVersionUID = 1L;
@@ -41,6 +45,8 @@
     private ErrorReporter errReporter;
     /** SCXMLListener delegate. */
     private SCXMLListener scxmlListener;
+    /** XMLReporter delegate. */
+    private XMLReporter xmlReporter;
 
     /**
      * Constructor.
@@ -50,6 +56,7 @@
         errHandler = new SimpleErrorHandler();
         errReporter = new SimpleErrorReporter();
         scxmlListener = new SimpleSCXMLListener();
+        xmlReporter = new SimpleXMLReporter();
     }
 
     /**
@@ -99,12 +106,21 @@
     }
 
     /**
-* @see SCXMLListener#onTransition(TransitionTarget,TransitionTarget,Transition)
+     * @see 
SCXMLListener#onTransition(TransitionTarget,TransitionTarget,Transition)
      */
     public void onTransition(final TransitionTarget from,
             final TransitionTarget to, final Transition transition) {
         scxmlListener.onTransition(from, to, transition);
     }
 
+    /**
+     * @see XMLReporter#report(String, String, Object, Location)
+     */
+       public void report(final String message, final String errorType, final 
Object relatedInformation,
+                       final Location location)
+       throws XMLStreamException {
+               xmlReporter.report(message, errorType, relatedInformation, 
location);
+       }
+
 }
 

Modified: 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/invoke/SimpleSCXMLInvoker.java
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/invoke/SimpleSCXMLInvoker.java?rev=737824&r1=737823&r2=737824&view=diff
==============================================================================
--- 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/invoke/SimpleSCXMLInvoker.java
 (original)
+++ 
commons/proper/scxml/branches/J6/src/main/java/org/apache/commons/scxml/invoke/SimpleSCXMLInvoker.java
 Mon Jan 26 20:31:22 2009
@@ -21,19 +21,19 @@
 import java.net.URL;
 import java.util.Map;
 
+import javax.xml.stream.XMLStreamException;
+
 import org.apache.commons.scxml.Context;
 import org.apache.commons.scxml.Evaluator;
 import org.apache.commons.scxml.SCInstance;
 import org.apache.commons.scxml.SCXMLExecutor;
 import org.apache.commons.scxml.TriggerEvent;
 import org.apache.commons.scxml.env.SimpleDispatcher;
-import org.apache.commons.scxml.env.SimpleErrorHandler;
 import org.apache.commons.scxml.env.SimpleErrorReporter;
 import org.apache.commons.scxml.env.SimpleSCXMLListener;
-import org.apache.commons.scxml.io.SCXMLParser;
+import org.apache.commons.scxml.io.SCXMLReader;
 import org.apache.commons.scxml.model.ModelException;
 import org.apache.commons.scxml.model.SCXML;
-import org.xml.sax.SAXException;
 
 /**
  * A simple {...@link Invoker} for SCXML documents. Invoked SCXML document
@@ -86,14 +86,13 @@
     throws InvokerException {
         SCXML scxml = null;
         try {
-            scxml = SCXMLParser.parse(new URL(source),
-                new SimpleErrorHandler());
+            scxml = SCXMLReader.read(new URL(source));
         } catch (ModelException me) {
             throw new InvokerException(me.getMessage(), me.getCause());
         } catch (IOException ioe) {
             throw new InvokerException(ioe.getMessage(), ioe.getCause());
-        } catch (SAXException se) {
-            throw new InvokerException(se.getMessage(), se.getCause());
+        } catch (XMLStreamException xse) {
+            throw new InvokerException(xse.getMessage(), xse.getCause());
         }
         Evaluator eval = parentSCInstance.getEvaluator();
         executor = new SCXMLExecutor(eval,


Reply via email to