Author: rahul
Date: Sat May  3 19:45:17 2008
New Revision: 653172

URL: http://svn.apache.org/viewvc?rev=653172&view=rev
Log:
Better error reporting for ignored / misplaced elements by parser.
SCXML-64

Modified:
    
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml/io/SCXMLParser.java
    
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/issues/issue64-02.xml

Modified: 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml/io/SCXMLParser.java
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml/io/SCXMLParser.java?rev=653172&r1=653171&r2=653172&view=diff
==============================================================================
--- 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml/io/SCXMLParser.java
 (original)
+++ 
commons/proper/scxml/trunk/src/main/java/org/apache/commons/scxml/io/SCXMLParser.java
 Sat May  3 19:45:17 2008
@@ -34,6 +34,7 @@
 import org.apache.commons.digester.Rule;
 import org.apache.commons.digester.SetNextRule;
 import org.apache.commons.digester.SetPropertiesRule;
+import org.apache.commons.digester.WithDefaultsRulesWrapper;
 import org.apache.commons.logging.LogFactory;
 import org.apache.commons.scxml.PathResolver;
 import org.apache.commons.scxml.SCXMLHelper;
@@ -76,6 +77,7 @@
 import org.xml.sax.Attributes;
 import org.xml.sax.ErrorHandler;
 import org.xml.sax.InputSource;
+import org.xml.sax.Locator;
 import org.xml.sax.SAXException;
 
 /**
@@ -487,7 +489,10 @@
         digester.setNamespaceAware(true);
         //Uncomment next line after SCXML DTD is available
         //digester.setValidating(true);
-        digester.setRules(initRules(scxml, pr, customActions));
+        WithDefaultsRulesWrapper rules =
+            new WithDefaultsRulesWrapper(initRules(scxml, pr, customActions));
+        rules.addDefault(new IgnoredElementRule());
+        digester.setRules(rules);
         return digester;
     }
 
@@ -1642,5 +1647,33 @@
         }
     }
 
+    /**
+     * Custom digestion rule logging ignored elements.
+     */
+    private static class IgnoredElementRule extends Rule {
+
+        /**
+         * @see Rule#begin(String, String, Attributes)
+         */
+        public final void begin(final String namespace, final String name,
+                final Attributes attributes) {
+            org.apache.commons.logging.Log log = LogFactory.
+                getLog(SCXMLParser.class);
+            Locator l = digester.getDocumentLocator();
+            String identifier = l.getSystemId();
+            if (identifier == null) {
+                identifier = l.getPublicId();
+            }
+            StringBuffer sb = new StringBuffer();
+            sb.append("Ignoring element <").append(name).
+                append("> in namespace \"").append(namespace).
+                append("\" at ").append(identifier).append(":").
+                append(l.getLineNumber()).append(":").
+                append(l.getColumnNumber()).append(" and digester match \"").
+                append(digester.getMatch()).append("\"");
+            log.warn(sb.toString());
+        }
+    }
+
 }
 

Modified: 
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/issues/issue64-02.xml
URL: 
http://svn.apache.org/viewvc/commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/issues/issue64-02.xml?rev=653172&r1=653171&r2=653172&view=diff
==============================================================================
--- 
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/issues/issue64-02.xml
 (original)
+++ 
commons/proper/scxml/trunk/src/test/java/org/apache/commons/scxml/issues/issue64-02.xml
 Sat May  3 19:45:17 2008
@@ -15,17 +15,29 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
 -->
-<!-- Bad SCXML document -->
+<!-- Bad SCXML document, many elements will be ignored with warnings from 
parser -->
 <scxml xmlns="http://www.w3.org/2005/07/scxml";
+       xmlns:my="http://my.foo.example/";
        version="1.0"
        initialstate="tranbug">
 
+    <datamodel>
+        <data name="foo">
+            <alpha xmlns="">
+                <beta/>
+            </alpha>
+        </data>
+        <misplaced/>
+    </datamodel>
+
     <state id="tranbug">
         <onentry>
             <log expr="'Begin transition bug test ...'" />
+            <foo/>
+            <my:bar/>
         </onentry>
         <transition event="show.bug" target="end">
-         <!-- FOLLOWING datamodel IS MISPLACED -->
+         <!-- For example, FOLLOWING datamodel IS MISPLACED -->
          <datamodel>
                        <data name="dummy" expr="'somedata'"/>
          </datamodel>
@@ -33,6 +45,8 @@
          <log expr="'*****' + dummy" />
         </transition>
     </state>
+
+    <my:baz/>
     
     <state id="end" final="true" />
 


Reply via email to