directory-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From akaras...@apache.org
Subject svn commit: rev 9833 - in incubator/directory/snickers/trunk: . ber ber/src/java/org/apache/snickers/ber ber/src/java/org/apache/snickers/ber/digester ber/src/test/org/apache/snickers/ber ber/src/test/org/apache/snickers/ber/digester
Date Thu, 01 Apr 2004 02:05:30 GMT
Author: akarasulu
Date: Wed Mar 31 18:05:28 2004
New Revision: 9833

Added:
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/IntStack.java 
 (contents, props changed)
      - copied, changed from rev 9500, incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/IntStack.java
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/IntStackTest.java
  (props changed)
      - copied unchanged from rev 9609, incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/IntStackTest.java
Modified:
   incubator/directory/snickers/trunk/   (props changed)
   incubator/directory/snickers/trunk/ber/project.xml
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoder.java
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Tag.java
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Tuple.java
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/BERDigester.java
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/Rules.java
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/RulesBase.java
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/TagTree.java
   incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/TagTreeTest.java
Log:
Checking in resurrected copy of IntStack and its unit test case.  Filling in
more of the digester code as I go along.  Need to think about the rules that
we can and cannot use from digester.



Modified: incubator/directory/snickers/trunk/ber/project.xml
==============================================================================
--- incubator/directory/snickers/trunk/ber/project.xml	(original)
+++ incubator/directory/snickers/trunk/ber/project.xml	Wed Mar 31 18:05:28 2004
@@ -33,6 +33,13 @@
     </dependency>
 
     <dependency>
+      <groupId>commons-primitives</groupId>
+      <artifactId>commons-primitives</artifactId>
+      <version>1.1-dev</version>
+      <url>http://jakarta.apache.org/commons/primitives</url>
+    </dependency>
+
+    <dependency>
       <groupId>incubator-directory</groupId>
       <artifactId>codec-stateful</artifactId>
       <version>SNAPSHOT</version>

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoder.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoder.java
(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/BERDecoder.java
Wed Mar 31 18:05:28 2004
@@ -265,6 +265,7 @@
         if ( decoder == tagDecoder )
         {
             Tag tag = ( Tag ) decoded ;
+            tlv.rawTag = tag.getRawTag() ;
             tlv.id = tag.getId() ;
             tlv.isPrimitive = tag.isPrimitive() ;
             tlv.typeClass = tag.getTypeClass() ;
@@ -461,6 +462,28 @@
         } while( tlvStack.size() > 0 ) ;
     }
 
+
+    /*
+     
+     Why copy the raw tag here when we can maintain our own stack in the
+     digester that does the pushing and popping instead?  Keep this here
+     until we decide what to do.
+     
+    public int[] getTagNestingPattern()
+    {
+        int stackSz = tlvStack.size() ;
+        int[] pattern = new int[stackSz+1] ;
+        pattern[stackSz] = tlv.rawTag ;
+        
+        for ( int ii = 0; ii < stackSz; ii++ )
+        {
+            pattern[ii] = ( ( Tuple ) tlvStack.get( ii ) ).rawTag ;
+        }
+        
+        return pattern ;
+    }
+    */
+    
     
     // ------------------------------------------------------------------------
     // Methods used for testing

Copied: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/IntStack.java
(from rev 9500, incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/IntStack.java)
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/IntStack.java
(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/IntStack.java
Wed Mar 31 18:05:28 2004
@@ -34,6 +34,20 @@
     /** the underlying dynamic primitive backing store */
     private ArrayIntList list = new ArrayIntList() ;
     
+    
+    public IntStack()
+    {
+    }
+    
+    
+    public IntStack( int[] numbas )
+    {
+        for ( int ii = 0; ii < numbas.length; ii++ )
+        {    
+            list.add( numbas[ii] ) ;
+        }
+    }
+    
 
     /**
      * Tests if this stack is empty.
@@ -117,5 +131,30 @@
         
         
         return -1 ;
+    }
+    
+    
+    /**
+     * Gets items from the stack where the index is zero based and the top of
+     * the stack is at an index of size()-1 with the bottom of the stack at an
+     * index of 0.
+     * 
+     * @param index the index into the stack treated as a list
+     * @return the int value at the index
+     */
+    public int get( int index )
+    {
+        return list.get( index ) ;
+    }
+    
+    
+    /**
+     * Gets the size of this stack.
+     * 
+     * @return the size of this stack
+     */
+    public int size()
+    {
+        return list.size() ;
     }
 }

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Tag.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Tag.java	(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Tag.java	Wed Mar
31 18:05:28 2004
@@ -149,6 +149,17 @@
     
     
     /**
+     * Gets the raw tag as it is stuffed into a primitive int.
+     * 
+     * @return a primitive int stuffed with the first four octets of the tag
+     */
+    public int getRawTag()
+    {
+        return buf.getIntValue() ; 
+    }
+    
+    
+    /**
      * Checks to see if the tag represented by this Tag is primitive or 
      * constructed.
      * 

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Tuple.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Tuple.java	(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/Tuple.java	Wed
Mar 31 18:05:28 2004
@@ -57,6 +57,8 @@
     /** precalculated left shift of 1 by 28 places */
     private static final int BIT_27 = 1 << 28 ;
     
+    /** the raw tag data */
+    int rawTag = 0 ;
     /** the tag id for this TLV tuple */
     int id = 0 ;
     /** the flag for whether or not this TLV is constructed or primitive */
@@ -185,6 +187,31 @@
         return id ;
     }
     
+    
+    /**
+     * Gets the raw tag as it is stuffed into a primitive int.
+     * 
+     * @return a primitive int stuffed with the first four octets of the tag
+     */
+    public int getRawTag()
+    {
+        return rawTag ;
+    }
+
+    
+    /**
+     * Gets the raw tag with the primitive/constructed flag dubbed out.  
+     * Effectively this makes every tag appear primitive and is done
+     * to remove encoding ambiguities that could interfere with pattern 
+     * matching.
+     * 
+     * @return the raw tag with the primitive/constructed flag dubbed out
+     */
+    public int getRawPrimitiveTag()
+    {
+        return rawTag & 0xDFFFFFFF ;
+    }
+
     
     /**
      * Get's whether or not this tuples's length is indefinate.

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/BERDigester.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/BERDigester.java
(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/BERDigester.java
Wed Mar 31 18:05:28 2004
@@ -17,14 +17,17 @@
 package org.apache.snickers.ber.digester ;
 
 
+import java.nio.ByteBuffer;
+import java.util.List;
 import java.util.Stack ;
 
 import org.apache.snickers.ber.Tuple ;
+import org.apache.snickers.ber.IntStack ;
+import org.apache.snickers.ber.TypeClass ;
 import org.apache.snickers.ber.BERDecoder ;
 import org.apache.snickers.ber.BERDecoderCallback ;
 
 import org.apache.commons.codec.stateful.StatefulDecoder ;
-import org.apache.commons.lang.NotImplementedException ;
 
 
 /**
@@ -41,8 +44,12 @@
     private BERDecoder decoder ;
     /** the object stack where rules push and pop ASN.1 POJO stubs */
     private Stack objectStack ;
+    /** the tag stack used to store the nesting pattern */
+    private IntStack tagStack ;
     /** the rules base used by this digester */
     private Rules rules ;
+    /** the currently matched rules */
+    private List matched ;
 
 
     /**
@@ -53,6 +60,7 @@
         this.rules = new RulesBase() ;
         this.objectStack = new Stack() ;
         this.decoder = new BERDecoder() ;
+        this.decoder.setCallback( this ) ;
     }
     
     
@@ -67,7 +75,9 @@
      */
     public void tagDecoded( Tuple tlv )
     {
-        throw new NotImplementedException( "STUB" ) ;
+        tagStack.push( tlv.getRawPrimitiveTag() ) ;
+        matched = rules.match( tagStack ) ;
+        fireTagEvent( tlv.getId(), tlv.isPrimitive(), tlv.getTypeClass() ) ;
     }
 
     
@@ -77,7 +87,7 @@
      */
     public void lengthDecoded( Tuple tlv )
     {
-        throw new NotImplementedException( "STUB" ) ;
+        fireLengthEvent( tlv.getLength() ) ;
     }
 
     
@@ -87,7 +97,7 @@
      */
     public void partialValueDecoded( Tuple tlv )
     {
-        throw new NotImplementedException( "STUB" ) ;
+        fireValueEvent( tlv.getLastValueChunk() ) ;
     }
 
 
@@ -97,7 +107,8 @@
      */
     public void decodeOccurred( StatefulDecoder decoder, Object decoded )
     {
-        throw new NotImplementedException( "STUB" ) ;
+        fireFinishEvent() ;
+        tagStack.pop() ;
     }
 
 
@@ -170,7 +181,7 @@
      */
     public Object peek( int n )
     {
-        return objectStack.get( n ) ;
+        return objectStack.get( objectStack.size() - n - 1 ) ;
     }
               
     
@@ -208,19 +219,33 @@
     }
     
     
+    /**
+     * Gets the raw int for the tag of the TLV currently being processed.  The
+     * tag int has the primitive flag dubbed out so it appears to be a primitive
+     * even when constructed.
+     * 
+     * @return the raw int for the tag of the TLV currently being processed
+     */
+    public int getCurrentRawPrimitiveTag()
+    {
+        return tagStack.peek() ;
+    }
+
     
     /*
-
      Methods of interest from the original digester class:     
 
  java.lang.ClassLoader getClassLoader() 
           Return the class loader to be used for instantiating application objects when required.

- java.lang.String getCurrentElementName() 
-          Return the name of the XML element that is currently being processed. 
- java.lang.String getMatch() 
-          Return the current rule match path 
  boolean getUseContextClassLoader() 
           Return the boolean as to whether the context classloader should be used. 
+ void setClassLoader(java.lang.ClassLoader classLoader) 
+          Set the class loader to be used for instantiating application objects when required.

+ void setUseContextClassLoader(boolean use) 
+          Determine whether to use the Context ClassLoader (the one found by calling Thread.currentThread().getContextClassLoader())
to resolve/load classes that are defined in various rules. 
+
+ java.lang.String getMatch() 
+          Return the current rule match path 
 
  java.lang.Object peekParams() 
           Return the top object on the parameters stack without removing it. 
@@ -230,11 +255,34 @@
           Pop the top object off of the parameters stack, and return it. 
  void pushParams(java.lang.Object object) 
           Push a new object onto the top of the parameters stack. 
- void setClassLoader(java.lang.ClassLoader classLoader) 
-          Set the class loader to be used for instantiating application objects when required.

- void setUseContextClassLoader(boolean use) 
-          Determine whether to use the Context ClassLoader (the one found by calling Thread.currentThread().getContextClassLoader())
to resolve/load classes that are defined in various rules. 
-                                        
-
     */
+    
+    
+    // ------------------------------------------------------------------------
+    // Event fireing routines that trigger rules
+    // ------------------------------------------------------------------------
+    
+    
+    void fireTagEvent( int id, boolean isPrimitive, TypeClass typeClass ) 
+    {
+        // do nothing for now
+    }
+    
+    
+    void fireLengthEvent( int length )
+    {
+        // do nothing for now
+    }
+    
+    
+    void fireValueEvent( ByteBuffer buf )
+    {
+        // do nothing for now
+    }
+    
+    
+    void fireFinishEvent()
+    {
+        // do nothing for now
+    }
 }

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/Rules.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/Rules.java
(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/Rules.java
Wed Mar 31 18:05:28 2004
@@ -20,6 +20,8 @@
 
 import java.util.List ;
 
+import org.apache.snickers.ber.IntStack;
+
 
 /**
  * Public interface defining a collection of Rule instances (and corresponding
@@ -74,6 +76,17 @@
      */
     public List match( int[] pattern ) ;
 
+    /**
+     * Return a List of all registered Rule instances that match the specified
+     * nesting pattern, or a zero-length List if there are no matches.  If more
+     * than one Rule instance matches, they <strong>must</strong> be returned
+     * in the order originally registered through the <code>add()</code>
+     * method.  The IntStack argument should not be affected by the match call.
+     *
+     * @param pattern Nesting pattern to be matched
+     */
+    public List match( IntStack pattern ) ;
+    
     /**
      * Return a List of all registered Rule instances, or a zero-length List
      * if there are no registered Rule instances.  If more than one Rule

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/RulesBase.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/RulesBase.java
(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/RulesBase.java
Wed Mar 31 18:05:28 2004
@@ -21,6 +21,8 @@
 import java.util.ArrayList ;
 import java.util.Collections ;
 
+import org.apache.snickers.ber.IntStack;
+
 
 /**
  * A base Rules implementation using a fast pattern match.
@@ -36,6 +38,9 @@
     private BERDigester digester ;
     
     
+    /**
+     * Creates a base Rules instance.
+     */
     public RulesBase()
     {
         tagTree = new TagTree() ;
@@ -87,6 +92,15 @@
      * @see org.apache.snickers.ber.digester.Rules#match(int[])
      */
     public List match( int[] pattern )
+    {
+        return tagTree.match( new IntStack( pattern ) ) ;
+    }
+    
+
+    /* (non-Javadoc)
+     * @see org.apache.snickers.ber.digester.Rules#match(int[])
+     */
+    public List match( IntStack pattern )
     {
         return tagTree.match( pattern ) ;
     }

Modified: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/TagTree.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/TagTree.java
(original)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/digester/TagTree.java
Wed Mar 31 18:05:28 2004
@@ -23,6 +23,8 @@
 
 import org.apache.commons.lang.Validate ;
 
+import org.apache.snickers.ber.IntStack ;
+
 
 /**
  * A tree of tags.
@@ -86,6 +88,55 @@
     }
     
     
+    public List match( IntStack stack )
+    {
+        TagNode node = getNode( stack ) ;
+        
+        if ( node == null )
+        {
+            return Collections.EMPTY_LIST ;
+        }
+        
+        return node.getRules() ;
+    }
+    
+    
+    public TagNode getNode( IntStack stack ) 
+    {
+        Integer tag = null ;
+        TagNode node = null ;
+
+        Validate.notNull( stack, "cannot match using null pattern" ) ;
+        Validate.isTrue( !stack.empty(), "cannot match with empty pattern" ) ;
+
+        tag = new Integer( stack.get( 0 ) ) ;
+        if ( nodes.containsKey( tag ) )
+        {
+            node = ( TagNode ) nodes.get( tag ) ;
+        }
+        else
+        {
+            return null ;
+        }
+        
+        for ( int ii = 1; ii < stack.size(); ii++ )
+        {    
+            Integer childTag = new Integer( stack.get( ii ) ) ;
+            TagNode childNode = node.getChild( childTag ) ;
+            
+            if ( childNode == null )
+            {
+                return null ;
+            }
+            
+            node = childNode ;
+            tag = childTag ;
+        }
+        
+        return node ;
+    }
+
+
     public List match( int[] pattern )
     {
         TagNode node = getNode( pattern ) ;

Modified: incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/TagTreeTest.java
==============================================================================
--- incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/TagTreeTest.java
(original)
+++ incubator/directory/snickers/trunk/ber/src/test/org/apache/snickers/ber/digester/TagTreeTest.java
Wed Mar 31 18:05:28 2004
@@ -19,6 +19,8 @@
 
 import java.util.List;
 
+import org.apache.snickers.ber.IntStack;
+
 import junit.framework.TestCase ;
 
 
@@ -63,7 +65,7 @@
     }
 
     
-    public void testMatch()
+    public void testMatchintArray()
     {
         TagTree tree = new TagTree() ;
         int[] pattern0 = {1,2,3} ;
@@ -82,6 +84,34 @@
         assertNotSame( rule0, ( (List) tree.match( pattern1 ) ).get(0) ) ;
         assertNotSame( rule1, ( (List) tree.match( pattern2 ) ).get(0) ) ;
         assertNotSame( rule2, ( (List) tree.match( pattern0 ) ).get(0) ) ;
+    }
+
+
+    public void testMatchIntStack()
+    {
+        TagTree tree = new TagTree() ;
+        int[] pattern0 = {1,2,3} ;
+        int[] pattern1 = {4,2,7} ;
+        int[] pattern2 = {1,5,3} ;
+        Rule rule0 = new MockRule() ;
+        Rule rule1 = new MockRule() ;
+        Rule rule2 = new MockRule() ;
+        tree.addRule( pattern0, rule0 ) ;
+        tree.addRule( pattern1, rule1 ) ;
+        tree.addRule( pattern2, rule2 ) ;
+        
+        assertEquals( rule0, ( (List) tree.match( new IntStack( pattern0 ) ) )
+                .get(0) ) ;
+        assertEquals( rule1, ( (List) tree.match( new IntStack( pattern1 ) ) )
+                .get(0) ) ;
+        assertEquals( rule2, ( (List) tree.match( new IntStack( pattern2 ) ) )
+                .get(0) ) ;
+        assertNotSame( rule0, ( (List) tree.match( new IntStack( pattern1 ) ) ) 
+                .get(0) ) ;
+        assertNotSame( rule1, ( (List) tree.match( new IntStack( pattern2 ) ) )
+                .get(0) ) ;
+        assertNotSame( rule2, ( (List) tree.match( new IntStack( pattern0 ) ) )
+                .get(0) ) ;
     }
 
 

Mime
View raw message