You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@directory.apache.org by we...@apache.org on 2004/01/27 22:07:37 UTC

svn commit: rev 6325 - incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber

Author: wesmckean
Date: Tue Jan 27 13:07:36 2004
New Revision: 6325

Added:
   incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TLV.java
Log:
New TLV object

Added: incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TLV.java
==============================================================================
--- (empty file)
+++ incubator/directory/snickers/trunk/ber/src/java/org/apache/snickers/ber/TLV.java	Tue Jan 27 13:07:36 2004
@@ -0,0 +1,266 @@
+/*
+
+ ============================================================================
+                   The Apache Software License, Version 1.1
+ ============================================================================
+
+ Copyright (C) 1999-2002 The Apache Software Foundation. All rights reserved.
+
+ Redistribution and use in source and binary forms, with or without modifica-
+ tion, are permitted provided that the following conditions are met:
+
+ 1. Redistributions of  source code must  retain the above copyright  notice,
+    this list of conditions and the following disclaimer.
+
+ 2. Redistributions in binary form must reproduce the above copyright notice,
+    this list of conditions and the following disclaimer in the documentation
+    and/or other materials provided with the distribution.
+
+ 3. The end-user documentation included with the redistribution, if any, must
+    include  the following  acknowledgment:  "This product includes  software
+    developed  by the  Apache Software Foundation  (http://www.apache.org/)."
+    Alternately, this  acknowledgment may  appear in the software itself,  if
+    and wherever such third-party acknowledgments normally appear.
+
+ 4. The names "Eve Directory Server", "Apache Directory Project", "Apache Eve" 
+    and "Apache Software Foundation"  must not be used to endorse or promote
+    products derived  from this  software without  prior written
+    permission. For written permission, please contact apache@apache.org.
+
+ 5. Products  derived from this software may not  be called "Apache", nor may
+    "Apache" appear  in their name,  without prior written permission  of the
+    Apache Software Foundation.
+
+ THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES,
+ INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ FITNESS  FOR A PARTICULAR  PURPOSE ARE  DISCLAIMED.  IN NO  EVENT SHALL  THE
+ APACHE SOFTWARE  FOUNDATION  OR ITS CONTRIBUTORS  BE LIABLE FOR  ANY DIRECT,
+ INDIRECT, INCIDENTAL, SPECIAL,  EXEMPLARY, OR CONSEQUENTIAL  DAMAGES (INCLU-
+ DING, BUT NOT LIMITED TO, PROCUREMENT  OF SUBSTITUTE GOODS OR SERVICES; LOSS
+ OF USE, DATA, OR  PROFITS; OR BUSINESS  INTERRUPTION)  HOWEVER CAUSED AND ON
+ ANY  THEORY OF LIABILITY,  WHETHER  IN CONTRACT,  STRICT LIABILITY,  OR TORT
+ (INCLUDING  NEGLIGENCE OR  OTHERWISE) ARISING IN  ANY WAY OUT OF THE  USE OF
+ THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+ This software  consists of voluntary contributions made  by many individuals
+ on  behalf of the Apache Software  Foundation. For more  information on the
+ Apache Software Foundation, please see <http://www.apache.org/>.
+
+*/
+package org.apache.snickers.ber;
+
+import java.util.ArrayList;
+import java.util.Collection;
+
+import org.apache.commons.codec.binary.Binary;
+
+/**
+ * Basic TLV structure consisting of:
+ * 
+ *      Tag
+ *      Length
+ *      Value
+ * 
+ * @author <a href="mailto:wesmckean@apache.org">Wes McKean</a>
+ * @author $Author$
+ * @version $Rev$
+ */
+public class TLV {
+
+    /** The tag for encoded attribute */
+    private int tag;
+    
+    /** The length of the value portion of our encoded attribute */
+    private int length;
+    
+    /** The value of this encoded attribute */
+    byte[] value = null;
+    
+    /** 
+     * The TLV children of this TLV.  This occurs when the V portion
+     * of a TLV is constructed, that is, the V portion is made up of
+     * more TLVs.
+     */
+    ArrayList children = null;
+        
+	/**
+	 * Empty constructor
+	 */
+	public TLV() {
+		super();
+        
+        tag = 0;
+        length = 0;
+	}
+
+    /**
+     * Constructor for a TLV with specified values
+     * 
+     * @param tag the tag for this TLV
+     * @param length the length of the value
+     * @param value the bytes representing the encoded value
+     */
+    public TLV( int tag, int length, byte[] value ) {
+        this.tag = tag;
+        this.length = length;
+        this.value = new byte[ value.length ];
+        System.arraycopy(value, 0, this.value, 0, value.length );
+    }
+
+    /**
+     * Constructor for a TLV with specified values
+     * 
+     * @param tag the tag for this TLV
+     * @param length the length of the value
+     * @param value the bytes representing the encoded value
+     * @param children a collection of TLVs comprising the children
+     *                 of this TLV.
+     */
+    public TLV( int tag, int length, byte[] value, Collection children ) {
+        this.tag = tag;
+        this.length = length;
+        this.value = new byte[ value.length ];
+        System.arraycopy(value, 0, this.value, 0, value.length );
+        
+        this.children = new ArrayList();
+        this.children.addAll( children );
+    }
+    
+    /**
+     * Retrieves the tag value for this TLV
+     * 
+     * @return the tag, an int value for this TLV
+     */
+    public int getTag() {
+        return tag;
+    }
+    
+    /**
+     * Set the tag value associated with a TLV
+     * 
+     * @param tag the new tag, an int, for this TLV
+     */
+    public void setTag( int tag ) {
+        this.tag = tag;
+    }
+    
+    /**
+     * Return the length of the V portion of this TLV
+     * 
+     * @return an integer representing the length
+     */
+    public int getLength() {
+        return length;
+    }
+    
+    /**
+     * Set the length portion of this TLV
+     * 
+     * @param length an integer representation of the length
+     */
+    public void setLength( int length ) {
+        this.length = length;
+    }
+    
+    /**
+     * Return the value portion of this TLV
+     * 
+     * @return a byte array of the encoded value
+     */
+    public byte[] getValue() {
+        return value;
+    }
+    
+    /**
+     * Set the encoded value of this TLV
+     * 
+     * @param value the byte array of the encoded value
+     */
+    public void setValue( byte[] value ) {
+        this.value = new byte[ value.length ];
+        System.arraycopy(value, 0, this.value, 0, value.length );
+    }
+
+    /**
+     * Return the child TLVs of this TLV
+     * 
+     * @return a collection of TLVs
+     */    
+    public Collection getChildren() {
+        return children;
+    }
+    
+    /**
+     * Sets the children associated with this TLV
+     * 
+     * @param children the child TLVs to associate with this TLV
+     */
+    public void setChildren(Collection children) {
+        this.children = new ArrayList();
+        this.children.addAll( children );
+    }
+    
+    /**
+     * Return the <code>TypeClass</code> associated with this TLV
+     * 
+     * @return the type class
+     */
+    public TypeClass getTypeClass() {
+        return BerUtils.getTypeClass( tag );
+    }
+
+    /**
+     * Return the unique ID of this TLV as specified in the ASN.1
+     * grammar.  Not all TLVs will have valid ids.  Some TLVs are
+     * just expected!
+     * 
+     * @return the id of this encoded value, or 0 if it doesn't have one
+     */    
+    public int getId() {
+        return ( tag & (Binary.BIT_4 | Binary.BIT_3 | Binary.BIT_2 | Binary.BIT_1 | Binary.BIT_0 ) );
+   }
+   
+   /**
+    * Returns a boolean indicating that the value portion of
+    * this TLV is a primative.  If not, then it is constructed
+    * of more TLVs.
+    * 
+    * @return true if the value is a primative, else false.
+    */
+   public boolean isPrimative() {
+       return BerUtils.isPrimitive( tag );
+   }
+   
+   /**
+    * Get the number of child TLVs
+    * 
+    * @return the count of children
+    */
+   public int getChildCount() {
+       
+       int result = 0;
+       
+      if( children != null ) {
+            result = children.size();
+      }
+      
+      return result;
+   }
+   
+   /**
+    * Retrieves a specific child TLV or null
+    * if the index is not valid.
+    * 
+    * @param i the index of the child
+    * @return the TLV child object
+    */
+   public TLV getChild( int i ) {
+       TLV result = null;
+       
+       if( children != null ) {
+           result = (TLV)children.get(i);
+       }
+       
+       return result;
+   }
+}