You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@commons.apache.org by sk...@apache.org on 2005/02/18 13:52:38 UTC

svn commit: r154269 - jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SAXHandler.java

Author: skitching
Date: Fri Feb 18 04:52:36 2005
New Revision: 154269

URL: http://svn.apache.org/viewcvs?view=rev&rev=154269
Log:
* provide generic storage facility for actions that need to store
  configuration info persistently across parses. This will be needed
  by the o.a.c.d.plugins module.
* use context's RuleManager during parsing, so that actions can modify
  the current RuleManager without having to mess with the original
  rulemanager reference held by the SAXHandler.

Modified:
    jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SAXHandler.java

Modified: jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SAXHandler.java
URL: http://svn.apache.org/viewcvs/jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SAXHandler.java?view=diff&r1=154268&r2=154269
==============================================================================
--- jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SAXHandler.java (original)
+++ jakarta/commons/proper/digester/branches/digester2/src/java/org/apache/commons/digester2/SAXHandler.java Fri Feb 18 04:52:36 2005
@@ -92,6 +92,47 @@
 public class SAXHandler extends DefaultHandler implements LexicalHandler {
 
     // ---------------------------------------------------
+    // Local classes
+    // ---------------------------------------------------
+
+    /**
+     * The SAXHandler class provides "scratch storage" that any other 
+     * object can use; instances of this class must be used as the key
+     * to identify entries in this storage area. See #putItem for more
+     * information.
+     * <p>
+     * This "scratch storage" is intended to store data that persists 
+     * between parses, in particular configuration information that might be
+     * accessed by all instances of a particular Action class. This facility
+     * is not expected to be frequently used, but when needed is important!
+     * <p>
+     * Configuration information that is associated with a particular Action 
+     * instance would, of course, be stored directly as a member of that 
+     * Action instead.
+     * <p>
+     * Example:
+     * <pre>
+     *    private final static SAXHandler.ItemId MY_CONFIG_ITEM
+     *      = new SAXHandler.ItemId(Foo.class, "MyConfigItem");
+     *    ....
+     *    saxHandler.putItem(MY_CONFIG_ITEM, someObject);
+     *    ....
+     *    Object savedObject = saxHandler.getItem(MY_CONFIG_ITEM);
+     * </pre>
+     * <p>
+     * See method {@link #putItem} for more information.
+     */
+    public static class ItemId extends MapKey {
+        public ItemId(Class sourceClass, String desc) {
+            super(sourceClass, desc);
+        }
+
+        public ItemId(Class sourceClass, String desc, Object owner) {
+            super(sourceClass, desc, owner);
+        }
+    }
+
+    // ---------------------------------------------------
     // Instance Variables
     // ---------------------------------------------------
 
@@ -153,7 +194,7 @@
      * established before the first rule is added, a default implementation
      * will be provided.
      */
-    private RuleManager ruleManager = null;
+    private RuleManager origRuleManager = null;
 
     /**
      * The initial object (if any) that the stack should be primed with
@@ -198,6 +239,12 @@
      */
     private boolean initialized = false;
 
+    /**
+     * Place where other objects can store any data they like.
+     * See method {@link #putItem} for more information.
+     */
+    private HashMap persistentItems = new HashMap();
+
     // -------------------------------------------------------------------
     // Instance variables that are modified during a parse.
     // -------------------------------------------------------------------
@@ -378,7 +425,7 @@
      * @param ruleManager New RuleManager implementation
      */
     public void setRuleManager(RuleManager ruleManager) {
-        this.ruleManager = ruleManager;
+        this.origRuleManager = ruleManager;
     }
 
     /**
@@ -387,10 +434,10 @@
      * established, a default implementation will be created and returned.
      */
     public RuleManager getRuleManager() {
-        if (ruleManager == null) {
-            ruleManager = new DefaultRuleManager();
+        if (origRuleManager == null) {
+            origRuleManager = new DefaultRuleManager();
         }
-        return ruleManager;
+        return origRuleManager;
     }
 
     /**
@@ -683,6 +730,44 @@
         root = null;
     }
 
+    /**
+     * This method provides the ability for other objects to store arbitrary
+     * data that persists between parses, in particular configuration 
+     * information that might be accessed by all instances of a particular 
+     * Action class. This facility is not expected to be frequently used,
+     * but when needed is important!
+     * <p>
+     * Configuration information that is associated with a particular Action 
+     * instance would, of course, be stored directly as a member of that 
+     * Action instead.
+     * <p>
+     * Example:
+     * <pre>
+     *    private final static SAXHandler.ItemId MY_CONFIG_ITEM
+     *      = new SAXHandler.ItemId(Foo.class, "MyConfigItem");
+     *    ....
+     *    saxHandler.putItem(MY_CONFIG_ITEM, someObject);
+     *    ....
+     *    Object savedObject = saxHandler.getItem(MY_CONFIG_ITEM);
+     * </pre>
+     * <p>
+     *
+     * @param id is used as the key to the stored item, and must be passed
+     *  to the getItem method to retrieve the data.
+     *
+     * @param data is the object to be stored for later retrieval.
+     */
+    public void putItem(ItemId id, Object data) {
+        persistentItems.put(id, data);
+    }
+
+    /**
+     * Retrieve a piece of data stored earlier via putItem.
+     */
+    public Object getItem(ItemId id) {
+        return persistentItems.get(id);
+    }
+
     // -------------------------------------------------------
     // Overridable methods
     //
@@ -800,9 +885,16 @@
         dtdSystemId = null;
         root = null; // just in case
 
+        // Get a reference to the rulemanager containing the actions to
+        // be applied to this input document. Note that this call also
+        // has the side-effect of creating a RuleManager if one has not
+        // been created before. Of course this would only happen if no
+        // rules had ever been added...
+        RuleManager currRuleManager = getRuleManager();
+
         // Create a new parsing context. This guarantees that Actions have
         // a clean slate for handling this new input document.
-        context = new Context(this, log, documentLocator);
+        context = new Context(this, log, documentLocator, currRuleManager);
 
         if (initialObject != null) {
             context.setRoot(initialObject);
@@ -817,11 +909,8 @@
         configure();
 
         try {
-            // This also has the side-effect of creating a RuleManager if
-            // one has not been created before. Of course this would only
-            // happen if no rules had ever been added...
-            getRuleManager().startParse(context);
-            List actions = ruleManager.getActions();
+            currRuleManager.startParse(context);
+            List actions = currRuleManager.getActions();
             for(Iterator i = actions.iterator(); i.hasNext(); ) {
                 Action action = (Action) i.next();
                 action.startParse(context);
@@ -854,16 +943,17 @@
             }
         }
 
+        RuleManager currRuleManager = context.getRuleManager();
         try {
             // Fire "finish" events for all defined actions
-            List actions = ruleManager.getActions();
+            List actions = currRuleManager.getActions();
             for(Iterator i = actions.iterator(); i.hasNext(); ) {
                 Action action = (Action) i.next();
                 action.finishParse(context);
             }
 
             // And finally for the RuleManager too
-            ruleManager.finishParse(context);
+            currRuleManager.finishParse(context);
         } catch(DigestionException ex) {
             log.error("RuleManager.finishParse threw exception", ex);
             throw new NestedSAXException(ex);
@@ -1092,9 +1182,8 @@
         // Fire "begin" events for all relevant rules
         List actions;
         try {
-            // NB: don't need getRuleManager here, as we know it was
-            // created at startDocument if not before..
-            actions = ruleManager.getMatchingActions(matchPath);
+            RuleManager currRuleManager = context.getRuleManager();
+            actions = currRuleManager.getMatchingActions(matchPath);
         } catch(DigestionException ex) {
             throw new NestedSAXException(ex);
         }



---------------------------------------------------------------------
To unsubscribe, e-mail: commons-dev-unsubscribe@jakarta.apache.org
For additional commands, e-mail: commons-dev-help@jakarta.apache.org