Return-Path: Delivered-To: apmail-directory-commits-archive@www.apache.org Received: (qmail 24903 invoked from network); 1 Apr 2010 00:28:37 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 1 Apr 2010 00:28:37 -0000 Received: (qmail 73076 invoked by uid 500); 1 Apr 2010 00:28:37 -0000 Delivered-To: apmail-directory-commits-archive@directory.apache.org Received: (qmail 73013 invoked by uid 500); 1 Apr 2010 00:28:37 -0000 Mailing-List: contact commits-help@directory.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: dev@directory.apache.org Delivered-To: mailing list commits@directory.apache.org Received: (qmail 73006 invoked by uid 99); 1 Apr 2010 00:28:36 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Apr 2010 00:28:36 +0000 X-ASF-Spam-Status: No, hits=-1233.1 required=10.0 tests=ALL_TRUSTED,AWL X-Spam-Check-By: apache.org Received: from [140.211.11.4] (HELO eris.apache.org) (140.211.11.4) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 01 Apr 2010 00:28:33 +0000 Received: by eris.apache.org (Postfix, from userid 65534) id 93AA823888FE; Thu, 1 Apr 2010 00:28:13 +0000 (UTC) Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Subject: svn commit: r929756 - in /directory/shared/trunk/ldap/src: main/java/org/apache/directory/shared/ldap/entry/ test/java/org/apache/directory/shared/ldap/entry/ Date: Thu, 01 Apr 2010 00:28:13 -0000 To: commits@directory.apache.org From: elecharny@apache.org X-Mailer: svnmailer-1.0.8 Message-Id: <20100401002813.93AA823888FE@eris.apache.org> Author: elecharny Date: Thu Apr 1 00:28:12 2010 New Revision: 929756 URL: http://svn.apache.org/viewvc?rev=929756&view=rev Log: o Moved some methods from String/BinaryValue to AbstractValue o Removed the clear() methods o Renamed a field o AttributeType is also moved to AbstractValue o Fixed some potential NPE o Some more minor refactoring Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/AbstractValue.java directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/BinaryValue.java directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/StringValue.java directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Value.java directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/BinaryValueTest.java directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/StringValueAttributeTypeTest.java directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/StringValueTest.java Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/AbstractValue.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/AbstractValue.java?rev=929756&r1=929755&r2=929756&view=diff ============================================================================== --- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/AbstractValue.java (original) +++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/AbstractValue.java Thu Apr 1 00:28:12 2010 @@ -22,6 +22,10 @@ package org.apache.directory.shared.ldap import org.apache.directory.shared.ldap.exception.LdapException; import org.apache.directory.shared.i18n.I18n; +import org.apache.directory.shared.ldap.schema.AttributeType; +import org.apache.directory.shared.ldap.schema.LdapComparator; +import org.apache.directory.shared.ldap.schema.MatchingRule; +import org.apache.directory.shared.ldap.schema.Normalizer; import org.apache.directory.shared.ldap.schema.SyntaxChecker; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -38,9 +42,11 @@ public abstract class AbstractValue i /** logger for reporting errors that might not be handled properly upstream */ private static final Logger LOG = LoggerFactory.getLogger( AbstractValue.class ); - + /** reference to the attributeType zssociated with the value */ + protected transient AttributeType attributeType; + /** the wrapped binary value */ - protected T wrapped; + protected T wrappedValue; /** the canonical representation of the wrapped value */ protected T normalizedValue; @@ -55,18 +61,6 @@ public abstract class AbstractValue i protected transient boolean same; /** - * Reset the value - */ - public void clear() - { - wrapped = null; - normalized = false; - normalizedValue = null; - valid = null; - } - - - /** * {@inheritDoc} */ public Value clone() @@ -92,11 +86,165 @@ public abstract class AbstractValue i */ public T getReference() { - return wrapped; + return wrappedValue; + } + + + /** + * Get the associated AttributeType + * @return The AttributeType + */ + public AttributeType getAttributeType() + { + return attributeType; + } + + + public void apply( AttributeType attributeType ) + { + if ( this.attributeType != null ) + { + if ( !attributeType.equals( this.attributeType ) ) + { + throw new IllegalArgumentException( I18n.err( I18n.ERR_04476, attributeType.getName(), this.attributeType.getName() ) ); + } + else + { + return; + } + } + + this.attributeType = attributeType; + + try + { + normalize(); + } + catch ( LdapException ne ) + { + String message = I18n.err( I18n.ERR_04447, ne.getLocalizedMessage() ); + LOG.info( message ); + normalized = false; + } + } + + + /** + * Gets a comparator using getMatchingRule() to resolve the matching + * that the comparator is extracted from. + * + * @return a comparator associated with the attributeType or null if one cannot be found + * @throws LdapException if resolution of schema entities fail + */ + protected LdapComparator getLdapComparator() throws LdapException + { + if ( attributeType != null ) + { + MatchingRule mr = getMatchingRule(); + + if ( mr == null ) + { + return null; + } + + return (LdapComparator)mr.getLdapComparator(); + } + else + { + return null; + } + } + + + /** + * Find a matchingRule to use for normalization and comparison. If an equality + * matchingRule cannot be found it checks to see if other matchingRules are + * available: SUBSTR, and ORDERING. If a matchingRule cannot be found null is + * returned. + * + * @return a matchingRule or null if one cannot be found for the attributeType + * @throws LdapException if resolution of schema entities fail + */ + protected MatchingRule getMatchingRule() throws LdapException + { + if ( attributeType != null ) + { + MatchingRule mr = attributeType.getEquality(); + + if ( mr == null ) + { + mr = attributeType.getOrdering(); + } + + if ( mr == null ) + { + mr = attributeType.getSubstring(); + } + + return mr; + } + else + { + return null; + } + } + + + /** + * Gets a normalizer using getMatchingRule() to resolve the matchingRule + * that the normalizer is extracted from. + * + * @return a normalizer associated with the attributeType or null if one cannot be found + * @throws LdapException if resolution of schema entities fail + */ + protected Normalizer getNormalizer() throws LdapException + { + if ( attributeType != null ) + { + MatchingRule mr = getMatchingRule(); + + if ( mr == null ) + { + return null; + } + + return mr.getNormalizer(); + } + else + { + return null; + } } /** + * Check if the value is stored into an instance of the given + * AttributeType, or one of its ascendant. + * + * For instance, if the Value is associated with a CommonName, + * checking for Name will match. + * + * @param attributeType The AttributeType we are looking at + * @return true if the value is associated with the given + * attributeType or one of its ascendant + */ + public boolean instanceOf( AttributeType attributeType ) throws LdapException + { + if ( ( attributeType != null ) && this.attributeType.equals( attributeType ) ) + { + if ( this.attributeType.equals( attributeType ) ) + { + return true; + } + + return this.attributeType.isDescendantOf( attributeType ); + } + + return false; + } + + + /** * Gets a copy of the wrapped binary value. * * @return a copy of the binary value that is wrapped @@ -147,7 +295,7 @@ public abstract class AbstractValue i if ( normalizedValue == null ) { - return wrapped; + return wrappedValue; } return normalizedValue; @@ -162,7 +310,7 @@ public abstract class AbstractValue i */ public final boolean isNull() { - return wrapped == null; + return wrappedValue == null; } @@ -175,26 +323,35 @@ public abstract class AbstractValue i } - /** - * Check if the Valid flag is set or not. This flag is set by a call - * to the isValid( SyntaxChecker ) method for client values. It is overridden - * for server values. - * - * if the flag is not set, returns false + /** + * Uses the syntaxChecker associated with the attributeType to check if the + * value is valid. Repeated calls to this method do not attempt to re-check + * the syntax of the wrapped value every time if the wrapped value does not + * change. Syntax checks only result on the first check, and when the wrapped + * value changes. * - * @see ServerValue#isValid() + * @see Value#isValid() */ - public boolean isValid() + public final boolean isValid() { if ( valid != null ) { return valid; } - return false; + if ( attributeType != null ) + { + valid = attributeType.getSyntax().getSyntaxChecker().isValidSyntax( get() ); + } + else + { + valid = false; + } + + return valid; } - - + + /** * Uses the syntaxChecker associated with the attributeType to check if the * value is valid. Repeated calls to this method do not attempt to re-check @@ -206,11 +363,6 @@ public abstract class AbstractValue i */ public final boolean isValid( SyntaxChecker syntaxChecker ) throws LdapException { - if ( valid != null ) - { - return valid; - } - if ( syntaxChecker == null ) { String message = I18n.err( I18n.ERR_04139, toString() ); @@ -219,6 +371,7 @@ public abstract class AbstractValue i } valid = syntaxChecker.isValidSyntax( getReference() ); + return valid; } @@ -232,7 +385,7 @@ public abstract class AbstractValue i public void normalize() throws LdapException { normalized = true; - normalizedValue = wrapped; + normalizedValue = wrappedValue; } Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/BinaryValue.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/BinaryValue.java?rev=929756&r1=929755&r2=929756&view=diff ============================================================================== --- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/BinaryValue.java (original) +++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/BinaryValue.java Thu Apr 1 00:28:12 2010 @@ -31,7 +31,6 @@ import org.apache.directory.shared.ldap. import org.apache.directory.shared.i18n.I18n; import org.apache.directory.shared.ldap.schema.AttributeType; import org.apache.directory.shared.ldap.schema.LdapComparator; -import org.apache.directory.shared.ldap.schema.MatchingRule; import org.apache.directory.shared.ldap.schema.Normalizer; import org.apache.directory.shared.ldap.schema.comparators.ByteArrayComparator; import org.apache.directory.shared.ldap.util.StringTools; @@ -56,9 +55,6 @@ public class BinaryValue extends Abstrac /** logger for reporting errors that might not be handled properly upstream */ protected static final Logger LOG = LoggerFactory.getLogger( BinaryValue.class ); - /** reference to the attributeType which is not serialized */ - protected transient AttributeType attributeType; - /** * Creates a BinaryValue without an initial wrapped value. * @@ -66,7 +62,7 @@ public class BinaryValue extends Abstrac */ public BinaryValue() { - wrapped = null; + wrappedValue = null; normalized = false; valid = null; normalizedValue = null; @@ -103,18 +99,18 @@ public class BinaryValue extends Abstrac * Creates a BinaryValue with an initial wrapped binary value. * * @param attributeType the schema type associated with this BinaryValue - * @param wrapped the binary value to wrap which may be null, or a zero length byte array + * @param value the binary value to wrap which may be null, or a zero length byte array */ - public BinaryValue( byte[] wrapped ) + public BinaryValue( byte[] value ) { - if ( wrapped != null ) + if ( value != null ) { - this.wrapped = new byte[ wrapped.length ]; - System.arraycopy( wrapped, 0, this.wrapped, 0, wrapped.length ); + this.wrappedValue = new byte[ value.length ]; + System.arraycopy( value, 0, this.wrappedValue, 0, value.length ); } else { - this.wrapped = null; + this.wrappedValue = null; } normalized = false; @@ -127,32 +123,18 @@ public class BinaryValue extends Abstrac * Creates a BinaryValue with an initial wrapped binary value. * * @param attributeType the schema type associated with this BinaryValue - * @param wrapped the binary value to wrap which may be null, or a zero length byte array + * @param value the binary value to wrap which may be null, or a zero length byte array */ - public BinaryValue( AttributeType attributeType, byte[] wrapped ) + public BinaryValue( AttributeType attributeType, byte[] value ) { this( attributeType ); - this.wrapped = wrapped; + this.wrappedValue = value; } // ----------------------------------------------------------------------- // Value Methods // ----------------------------------------------------------------------- - /** - * Reset the value - */ - public void clear() - { - wrapped = null; - normalized = false; - normalizedValue = null; - valid = null; - } - - - - /* * Sets the wrapped binary value. Has the side effect of setting the * normalizedValue and the valid flags to null if the wrapped value is @@ -161,15 +143,13 @@ public class BinaryValue extends Abstrac * * @see ServerValue#set(Object) */ - public final void set( byte[] wrapped ) + public final void set( byte[] value ) { // Why should we invalidate the normalized value if it's we're setting the // wrapper to it's current value? - byte[] value = getReference(); - - if ( value != null ) + if ( wrappedValue != null ) { - if ( Arrays.equals( wrapped, value ) ) + if ( Arrays.equals( value, wrappedValue ) ) { return; } @@ -179,50 +159,18 @@ public class BinaryValue extends Abstrac normalized = false; valid = null; - if ( wrapped == null ) + if ( value == null ) { - this.wrapped = null; + this.wrappedValue = null; } else { - this.wrapped = new byte[ wrapped.length ]; - System.arraycopy( wrapped, 0, this.wrapped, 0, wrapped.length ); + this.wrappedValue = new byte[ value.length ]; + System.arraycopy( value, 0, this.wrappedValue, 0, value.length ); } } - /** - * Get the associated AttributeType - * @return The AttributeType - */ - public AttributeType getAttributeType() - { - return attributeType; - } - - - /** - * Check if the value is stored into an instance of the given - * AttributeType, or one of its ascendant. - * - * For instance, if the Value is associated with a CommonName, - * checking for Name will match. - * - * @param attributeType The AttributeType we are looking at - * @return true if the value is associated with the given - * attributeType or one of its ascendant - */ - public boolean instanceOf( AttributeType attributeType ) throws LdapException - { - if ( this.attributeType.equals( attributeType ) ) - { - return true; - } - - return this.attributeType.isDescendantOf( attributeType ); - } - - // ----------------------------------------------------------------------- // ServerValue Methods // ----------------------------------------------------------------------- @@ -306,7 +254,7 @@ public class BinaryValue extends Abstrac } else { - return wrapped; + return wrappedValue; } } @@ -340,9 +288,9 @@ public class BinaryValue extends Abstrac { if ( normalizer != null ) { - if ( wrapped == null ) + if ( wrappedValue == null ) { - normalizedValue = wrapped; + normalizedValue = wrappedValue; normalized = true; same = true; } @@ -350,12 +298,12 @@ public class BinaryValue extends Abstrac { normalizedValue = normalizer.normalize( this ).getBytes(); normalized = true; - same = Arrays.equals( wrapped, normalizedValue ); + same = Arrays.equals( wrappedValue, normalizedValue ); } } else { - normalizedValue = wrapped; + normalizedValue = wrappedValue; normalized = false; same = true; } @@ -380,7 +328,7 @@ public class BinaryValue extends Abstrac } else { - normalizedValue = wrapped; + normalizedValue = wrappedValue; normalized = true; same = true; } @@ -420,7 +368,7 @@ public class BinaryValue extends Abstrac { try { - Comparator comparator = (Comparator)getLdapComparator(); + LdapComparator comparator = getLdapComparator(); if ( comparator != null ) { @@ -473,73 +421,6 @@ public class BinaryValue extends Abstrac /** - * Find a matchingRule to use for normalization and comparison. If an equality - * matchingRule cannot be found it checks to see if other matchingRules are - * available: SUBSTR, and ORDERING. If a matchingRule cannot be found null is - * returned. - * - * @return a matchingRule or null if one cannot be found for the attributeType - * @throws LdapException if resolution of schema entities fail - */ - private MatchingRule getMatchingRule() throws LdapException - { - MatchingRule mr = attributeType.getEquality(); - - if ( mr == null ) - { - mr = attributeType.getOrdering(); - } - - if ( mr == null ) - { - mr = attributeType.getSubstring(); - } - - return mr; - } - - - /** - * Gets a comparator using getMatchingRule() to resolve the matching - * that the comparator is extracted from. - * - * @return a comparator associated with the attributeType or null if one cannot be found - * @throws LdapException if resolution of schema entities fail - */ - private LdapComparator getLdapComparator() throws LdapException - { - MatchingRule mr = getMatchingRule(); - - if ( mr == null ) - { - return null; - } - - return mr.getLdapComparator(); - } - - - /** - * Gets a normalizer using getMatchingRule() to resolve the matchingRule - * that the normalizer is extracted from. - * - * @return a normalizer associated with the attributeType or null if one cannot be found - * @throws LdapException if resolution of schema entities fail - */ - private Normalizer getNormalizer() throws LdapException - { - MatchingRule mr = getMatchingRule(); - - if ( mr == null ) - { - return null; - } - - return mr.getNormalizer(); - } - - - /** * Checks to see if this BinaryValue equals the supplied object. * * This equals implementation overrides the BinaryValue implementation which @@ -589,7 +470,7 @@ public class BinaryValue extends Abstrac // Shortcut : if the values are equals, no need to compare // the normalized values - if ( Arrays.equals( wrapped, other.get() ) ) + if ( Arrays.equals( wrappedValue, other.wrappedValue ) ) { return true; } @@ -641,10 +522,10 @@ public class BinaryValue extends Abstrac System.arraycopy( normalizedValue, 0, clone.normalizedValue, 0, normalizedValue.length ); } - if ( wrapped != null ) + if ( wrappedValue != null ) { - clone.wrapped = new byte[ wrapped.length ]; - System.arraycopy( wrapped, 0, clone.wrapped, 0, wrapped.length ); + clone.wrappedValue = new byte[ wrappedValue.length ]; + System.arraycopy( wrappedValue, 0, clone.wrappedValue, 0, wrappedValue.length ); } return clone; @@ -658,14 +539,14 @@ public class BinaryValue extends Abstrac */ public byte[] getCopy() { - if ( wrapped == null ) + if ( wrappedValue == null ) { return null; } - final byte[] copy = new byte[ wrapped.length ]; - System.arraycopy( wrapped, 0, copy, 0, wrapped.length ); + final byte[] copy = new byte[ wrappedValue.length ]; + System.arraycopy( wrappedValue, 0, copy, 0, wrappedValue.length ); return copy; } @@ -682,40 +563,11 @@ public class BinaryValue extends Abstrac /** - * Uses the syntaxChecker associated with the attributeType to check if the - * value is valid. Repeated calls to this method do not attempt to re-check - * the syntax of the wrapped value every time if the wrapped value does not - * change. Syntax checks only result on the first check, and when the wrapped - * value changes. - * - * @see Value#isValid() - */ - public final boolean isValid() - { - if ( valid != null ) - { - return valid; - } - - if ( attributeType != null ) - { - valid = attributeType.getSyntax().getSyntaxChecker().isValidSyntax( getReference() ); - return valid; - } - else - { - // Always false if we don't have an AttributeType - return false; - } - } - - - /** * @return The length of the interned value */ public int length() { - return wrapped != null ? wrapped.length : 0; + return wrappedValue != null ? wrappedValue.length : 0; } @@ -738,7 +590,7 @@ public class BinaryValue extends Abstrac */ public String getString() { - return StringTools.utf8ToString( wrapped ); + return StringTools.utf8ToString( wrappedValue ); } @@ -752,11 +604,11 @@ public class BinaryValue extends Abstrac if ( wrappedLength >= 0 ) { - wrapped = new byte[wrappedLength]; + wrappedValue = new byte[wrappedLength]; if ( wrappedLength > 0 ) { - in.read( wrapped ); + in.read( wrappedValue ); } } @@ -786,13 +638,13 @@ public class BinaryValue extends Abstrac public void writeExternal( ObjectOutput out ) throws IOException { // Write the wrapped value, if it's not null - if ( wrapped != null ) + if ( wrappedValue != null ) { - out.writeInt( wrapped.length ); + out.writeInt( wrappedValue.length ); - if ( wrapped.length > 0 ) + if ( wrappedValue.length > 0 ) { - out.write( wrapped, 0, wrapped.length ); + out.write( wrappedValue, 0, wrappedValue.length ); } } else @@ -847,16 +699,16 @@ public class BinaryValue extends Abstrac */ public void serialize( ObjectOutput out ) throws IOException { - if ( wrapped != null ) + if ( wrappedValue != null ) { // write a the wrapped length - out.writeInt( wrapped.length ); + out.writeInt( wrappedValue.length ); // Write the data if not empty - if ( wrapped.length > 0 ) + if ( wrappedValue.length > 0 ) { // The data - out.write( wrapped ); + out.write( wrappedValue ); // Normalize the data try @@ -929,21 +781,21 @@ public class BinaryValue extends Abstrac { // If the value is null, the length will be set to -1 same = true; - wrapped = null; + wrappedValue = null; } else if ( wrappedLength == 0 ) { - wrapped = StringTools.EMPTY_BYTES; + wrappedValue = StringTools.EMPTY_BYTES; same = true; normalized = true; - normalizedValue = wrapped; + normalizedValue = wrappedValue; } else { - wrapped = new byte[wrappedLength]; + wrappedValue = new byte[wrappedLength]; // Read the data - in.readFully( wrapped ); + in.readFully( wrappedValue ); // Check if we have a normalized value normalized = in.readBoolean(); @@ -973,7 +825,7 @@ public class BinaryValue extends Abstrac else { normalizedValue = new byte[wrappedLength]; - System.arraycopy( wrapped, 0, normalizedValue, 0, wrappedLength ); + System.arraycopy( wrappedValue, 0, normalizedValue, 0, wrappedLength ); } } } @@ -987,22 +839,22 @@ public class BinaryValue extends Abstrac */ public String toString() { - if ( wrapped == null ) + if ( wrappedValue == null ) { return "null"; } - else if ( wrapped.length > 16 ) + else if ( wrappedValue.length > 16 ) { // Just dump the first 16 bytes... byte[] copy = new byte[16]; - System.arraycopy( wrapped, 0, copy, 0, 16 ); + System.arraycopy( wrappedValue, 0, copy, 0, 16 ); return "'" + StringTools.dumpBytes( copy ) + "...'"; } else { - return "'" + StringTools.dumpBytes( wrapped ) + "'"; + return "'" + StringTools.dumpBytes( wrappedValue ) + "'"; } } } \ No newline at end of file Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/StringValue.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/StringValue.java?rev=929756&r1=929755&r2=929756&view=diff ============================================================================== --- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/StringValue.java (original) +++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/StringValue.java Thu Apr 1 00:28:12 2010 @@ -30,7 +30,6 @@ import org.apache.directory.shared.i18n. import org.apache.directory.shared.ldap.NotImplementedException; import org.apache.directory.shared.ldap.schema.AttributeType; import org.apache.directory.shared.ldap.schema.LdapComparator; -import org.apache.directory.shared.ldap.schema.MatchingRule; import org.apache.directory.shared.ldap.schema.Normalizer; import org.apache.directory.shared.ldap.util.StringTools; import org.slf4j.Logger; @@ -54,9 +53,6 @@ public class StringValue extends Abstrac /** logger for reporting errors that might not be handled properly upstream */ protected static final Logger LOG = LoggerFactory.getLogger( StringValue.class ); - /** reference to the attributeType which is not serialized */ - protected transient AttributeType attributeType; - // ----------------------------------------------------------------------- // Constructors @@ -101,11 +97,11 @@ public class StringValue extends Abstrac /** * Creates a StringValue with an initial wrapped String value. * - * @param wrapped the value to wrap which can be null + * @param value the value to wrap which can be null */ - public StringValue( String wrapped ) + public StringValue( String value ) { - this.wrapped = wrapped; + this.wrappedValue = value; normalized = false; valid = null; } @@ -117,28 +113,10 @@ public class StringValue extends Abstrac * @param attributeType the schema type associated with this StringValue * @param wrapped the value to wrap which can be null */ - public StringValue( AttributeType attributeType, String wrapped ) + public StringValue( AttributeType attributeType, String value ) { this( attributeType ); - this.wrapped = wrapped; - } - - - /** - * Creates a StringValue with an initial wrapped String value and - * a normalized value. - * - * @param attributeType the schema type associated with this StringValue - * @param wrapped the value to wrap which can be null - * @param normalizedValue the normalized value - */ - StringValue( AttributeType attributeType, String wrapped, String normalizedValue, boolean valid ) - { - this( wrapped ); - this.normalized = true; - this.attributeType = attributeType; - this.normalizedValue = normalizedValue; - this.valid = valid; + this.wrappedValue = value; } @@ -154,7 +132,7 @@ public class StringValue extends Abstrac { // The String is immutable, we can safely return the internal // object without copying it. - return wrapped; + return wrappedValue; } @@ -166,11 +144,11 @@ public class StringValue extends Abstrac * * @see ServerValue#set(Object) */ - public final void set( String wrapped ) + public final void set( String value ) { // Why should we invalidate the normalized value if it's we're setting the // wrapper to it's current value? - if ( !StringTools.isEmpty( wrapped ) && wrapped.equals( getString() ) ) + if ( !StringTools.isEmpty( value ) && value.equals( getString() ) ) { return; } @@ -178,7 +156,7 @@ public class StringValue extends Abstrac normalizedValue = null; normalized = false; valid = null; - this.wrapped = wrapped; + this.wrappedValue = value; } @@ -216,42 +194,13 @@ public class StringValue extends Abstrac if ( normalizedValue == null ) { - return wrapped; + return wrappedValue; } return normalizedValue; } - public void apply( AttributeType attributeType ) - { - if ( this.attributeType != null ) - { - if ( !attributeType.equals( this.attributeType ) ) - { - throw new IllegalArgumentException( I18n.err( I18n.ERR_04476, attributeType.getName(), this.attributeType.getName() ) ); - } - else - { - return; - } - } - - this.attributeType = attributeType; - - try - { - normalize(); - } - catch ( LdapException ne ) - { - String message = I18n.err( I18n.ERR_04447, ne.getLocalizedMessage() ); - LOG.info( message ); - normalized = false; - } - } - - /** * Gets a copy of the the normalized (canonical) representation * for the wrapped value. @@ -284,11 +233,11 @@ public class StringValue extends Abstrac if ( normalizer == null ) { - normalizedValue = wrapped; + normalizedValue = wrappedValue; } else { - normalizedValue = ( String ) normalizer.normalize( wrapped ); + normalizedValue = ( String ) normalizer.normalize( wrappedValue ); } normalized = true; @@ -309,7 +258,7 @@ public class StringValue extends Abstrac { if ( normalizer != null ) { - normalizedValue = (String)normalizer.normalize( wrapped ); + normalizedValue = (String)normalizer.normalize( wrappedValue ); normalized = true; } } @@ -505,7 +454,7 @@ public class StringValue extends Abstrac { try { - LdapComparator comparator = getLdapComparator(); + LdapComparator comparator = getLdapComparator(); // Compare normalized values if ( comparator == null ) @@ -549,40 +498,11 @@ public class StringValue extends Abstrac /** - * Uses the syntaxChecker associated with the attributeType to check if the - * value is valid. Repeated calls to this method do not attempt to re-check - * the syntax of the wrapped value every time if the wrapped value does not - * change. Syntax checks only result on the first check, and when the wrapped - * value changes. - * - * @see Value#isValid() - */ - public final boolean isValid() - { - if ( valid != null ) - { - return valid; - } - - if ( attributeType != null ) - { - valid = attributeType.getSyntax().getSyntaxChecker().isValidSyntax( get() ); - } - else - { - valid = false; - } - - return valid; - } - - - /** * @return The length of the interned value */ public int length() { - return wrapped != null ? wrapped.length() : 0; + return wrappedValue != null ? wrappedValue.length() : 0; } @@ -592,7 +512,7 @@ public class StringValue extends Abstrac */ public byte[] getBytes() { - return StringTools.getBytesUtf8( wrapped ); + return StringTools.getBytesUtf8( wrappedValue ); } @@ -603,7 +523,7 @@ public class StringValue extends Abstrac */ public String getString() { - return wrapped != null ? wrapped : ""; + return wrappedValue != null ? wrappedValue : ""; } @@ -615,7 +535,7 @@ public class StringValue extends Abstrac // Read the wrapped value, if it's not null if ( in.readBoolean() ) { - wrapped = in.readUTF(); + wrappedValue = in.readUTF(); } // Read the isNormalized flag @@ -638,10 +558,10 @@ public class StringValue extends Abstrac public void writeExternal( ObjectOutput out ) throws IOException { // Write the wrapped value, if it's not null - if ( wrapped != null ) + if ( wrappedValue != null ) { out.writeBoolean( true ); - out.writeUTF( wrapped ); + out.writeUTF( wrappedValue ); } else { @@ -675,151 +595,6 @@ public class StringValue extends Abstrac /** - * Get the associated AttributeType - * @return The AttributeType - */ - public AttributeType getAttributeType() - { - return attributeType; - } - - - /** - * Check if the value is stored into an instance of the given - * AttributeType, or one of its ascendant. - * - * For instance, if the Value is associated with a CommonName, - * checking for Name will match. - * - * @param attributeType The AttributeType we are looking at - * @return true if the value is associated with the given - * attributeType or one of its ascendant - */ - public boolean instanceOf( AttributeType attributeType ) throws LdapException - { - if ( attributeType != null ) - { - if ( this.attributeType.equals( attributeType ) ) - { - return true; - } - - return this.attributeType.isDescendantOf( attributeType ); - } - - return false; - } - - - /** - * Check the attributeType member. It should not be null, - * and it should contains a syntax. - */ - protected String checkAttributeType( AttributeType attributeType ) - { - if ( attributeType == null ) - { - return "The AttributeType parameter should not be null"; - } - - if ( attributeType.getSyntax() == null ) - { - return "There is no Syntax associated with this attributeType"; - } - - return null; - } - - - /** - * Gets a comparator using getMatchingRule() to resolve the matching - * that the comparator is extracted from. - * - * @return a comparator associated with the attributeType or null if one cannot be found - * @throws LdapException if resolution of schema entities fail - */ - protected LdapComparator getLdapComparator() throws LdapException - { - if ( attributeType != null ) - { - MatchingRule mr = getMatchingRule(); - - if ( mr == null ) - { - return null; - } - - return mr.getLdapComparator(); - } - else - { - return null; - } - } - - - /** - * Find a matchingRule to use for normalization and comparison. If an equality - * matchingRule cannot be found it checks to see if other matchingRules are - * available: SUBSTR, and ORDERING. If a matchingRule cannot be found null is - * returned. - * - * @return a matchingRule or null if one cannot be found for the attributeType - * @throws LdapException if resolution of schema entities fail - */ - protected MatchingRule getMatchingRule() throws LdapException - { - if ( attributeType != null ) - { - MatchingRule mr = attributeType.getEquality(); - - if ( mr == null ) - { - mr = attributeType.getOrdering(); - } - - if ( mr == null ) - { - mr = attributeType.getSubstring(); - } - - return mr; - } - else - { - return null; - } - } - - - /** - * Gets a normalizer using getMatchingRule() to resolve the matchingRule - * that the normalizer is extracted from. - * - * @return a normalizer associated with the attributeType or null if one cannot be found - * @throws LdapException if resolution of schema entities fail - */ - protected Normalizer getNormalizer() throws LdapException - { - if ( attributeType != null ) - { - MatchingRule mr = getMatchingRule(); - - if ( mr == null ) - { - return null; - } - - return mr.getNormalizer(); - } - else - { - return null; - } - } - - - /** * We will write the value and the normalized value, only * if the normalized value is different. * @@ -838,13 +613,13 @@ public class StringValue extends Abstrac */ public void serialize( ObjectOutput out ) throws IOException { - if ( wrapped != null ) + if ( wrappedValue != null ) { // write a flag indicating that the value is not null out.writeBoolean( true ); // Write the data - out.writeUTF( wrapped ); + out.writeUTF( wrappedValue ); // Normalize the data try @@ -852,7 +627,7 @@ public class StringValue extends Abstrac normalize(); out.writeBoolean( true ); - if ( wrapped.equals( normalizedValue ) ) + if ( wrappedValue.equals( normalizedValue ) ) { out.writeBoolean( true ); } @@ -928,6 +703,6 @@ public class StringValue extends Abstrac */ public String toString() { - return wrapped == null ? "null": wrapped; + return wrappedValue == null ? "null": wrappedValue; } } Modified: directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Value.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Value.java?rev=929756&r1=929755&r2=929756&view=diff ============================================================================== --- directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Value.java (original) +++ directory/shared/trunk/ldap/src/main/java/org/apache/directory/shared/ldap/entry/Value.java Thu Apr 1 00:28:12 2010 @@ -107,12 +107,6 @@ public interface Value extends Clonea /** - * Reset the value - */ - void clear(); - - - /** * Tells if the value has already be normalized or not. * * @return true if the value has already been normalized. Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/BinaryValueTest.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/BinaryValueTest.java?rev=929756&r1=929755&r2=929756&view=diff ============================================================================== --- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/BinaryValueTest.java (original) +++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/BinaryValueTest.java Thu Apr 1 00:28:12 2010 @@ -214,26 +214,6 @@ public class BinaryValueTest @Test - public void testClear() throws LdapException - { - BinaryValue cbv = new BinaryValue( BYTES2 ); - cbv.normalize( BINARY_NORMALIZER ); - cbv.isValid( BINARY_CHECKER ); - - cbv.clear(); - assertTrue( cbv.isNull() ); - assertFalse( cbv.isNormalized() ); - assertFalse( cbv.isValid() ); - assertNull( cbv.get() ); - assertNull( cbv.getCopy() ); - assertNull( cbv.getReference() ); - assertNull( cbv.getNormalizedValue() ); - assertNull( cbv.getNormalizedValueCopy() ); - assertNull( cbv.getNormalizedValueReference() ); - } - - - @Test public void testBinaryValueNull() throws LdapException { BinaryValue cbv = new BinaryValue( (byte[])null ); @@ -478,9 +458,6 @@ public class BinaryValueTest cbv.set( BYTES1 ); assertEquals( "'0x01 0x02 0x03 0x04 '", cbv.toString() ); - - cbv.clear(); - assertEquals( "null", cbv.toString() ); } @@ -582,9 +559,6 @@ public class BinaryValueTest cbv.set( BYTES1 ); assertFalse( cbv.isNull() ); - - cbv.clear(); - assertTrue( cbv.isNull() ); } @@ -726,8 +700,6 @@ public class BinaryValueTest assertTrue( cbv.isNormalized() ); cbv.normalize( BINARY_NORMALIZER ); - cbv.clear(); - assertFalse( cbv.isNormalized() ); } Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/StringValueAttributeTypeTest.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/StringValueAttributeTypeTest.java?rev=929756&r1=929755&r2=929756&view=diff ============================================================================== --- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/StringValueAttributeTypeTest.java (original) +++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/StringValueAttributeTypeTest.java Thu Apr 1 00:28:12 2010 @@ -410,25 +410,25 @@ public class StringValueAttributeTypeTes public void testClone() throws LdapException { AttributeType at1 = EntryUtils.getCaseIgnoringAttributeNoNumbersType(); - StringValue ssv = new StringValue( at1, "Test" ); + StringValue sv = new StringValue( at1, "Test" ); - StringValue ssv1 = ssv.clone(); + StringValue sv1 = sv.clone(); - assertEquals( ssv, ssv1 ); + assertEquals( sv, sv1 ); - ssv.set( "" ); + sv.set( "" ); - assertNotSame( ssv, ssv1 ); - assertEquals( "", ssv.getString() ); + assertNotSame( sv, sv1 ); + assertEquals( "", sv.getString() ); - ssv.set( " This is a TEST " ); - ssv1 = ssv.clone(); + sv.set( " This is a TEST " ); + sv1 = sv.clone(); - assertEquals( ssv, ssv1 ); + assertEquals( sv, sv1 ); - ssv.normalize(); + sv.normalize(); - assertEquals( ssv, ssv1 ); + assertEquals( sv, sv1 ); } Modified: directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/StringValueTest.java URL: http://svn.apache.org/viewvc/directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/StringValueTest.java?rev=929756&r1=929755&r2=929756&view=diff ============================================================================== --- directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/StringValueTest.java (original) +++ directory/shared/trunk/ldap/src/test/java/org/apache/directory/shared/ldap/entry/StringValueTest.java Thu Apr 1 00:28:12 2010 @@ -32,8 +32,11 @@ import java.io.ObjectInputStream; import java.io.ObjectOutputStream; import org.apache.directory.shared.ldap.exception.LdapException; +import org.apache.directory.shared.ldap.schema.comparators.StringComparator; import org.apache.directory.shared.ldap.schema.normalizers.DeepTrimToLowerNormalizer; import org.apache.directory.shared.ldap.schema.syntaxCheckers.Ia5StringSyntaxChecker; +import org.apache.directory.shared.ldap.schema.syntaxCheckers.OctetStringSyntaxChecker; +import org.junit.Before; import org.junit.Test; /** @@ -45,6 +48,9 @@ import org.junit.Test; */ public class StringValueTest { + //---------------------------------------------------------------------------------- + // Helper method + //---------------------------------------------------------------------------------- /** * Serialize a StringValue */ @@ -117,8 +123,56 @@ public class StringValueTest } } } - - + + + //---------------------------------------------------------------------------------- + // Test the clone() method + //---------------------------------------------------------------------------------- + /** + * Test cloning an empty value + */ + @Test + public void testCloneEmptyValue() throws LdapException + { + StringValue sv = new StringValue(); + + StringValue sv1 = (StringValue)sv.clone(); + + assertEquals( sv, sv1 ); + + sv.set( "" ); + + assertNotSame( sv, sv1 ); + assertNull( sv1.get() ); + assertEquals( "", sv.getString() ); + } + + + /** + * Test cloning a value + */ + @Test + public void testCloneValue() throws LdapException + { + StringValue sv = new StringValue( " This is a TEST " ); + + StringValue sv1 = (StringValue)sv.clone(); + + sv1 = sv.clone(); + + assertEquals( sv, sv1 ); + assertEquals( " This is a TEST ", sv.getString() ); + + sv.normalize( new DeepTrimToLowerNormalizer( "1.1.1" ) ); + + assertNotSame( sv, sv1 ); + assertEquals( " This is a TEST ", sv1.getString() ); + assertEquals( " This is a TEST ", sv1.getNormalizedValue() ); + assertEquals( " This is a TEST ", sv.getString() ); + assertEquals( "this is a test", sv.getNormalizedValue() ); + } + + /** * Test method for {@link org.apache.directory.shared.ldap.entry.StringValue#hashCode()}. */ @@ -199,9 +253,6 @@ public class StringValueTest csv.set( "" ); assertEquals( "", csv.get() ); - - csv.clear(); - assertNull( csv.get() ); } @@ -217,9 +268,6 @@ public class StringValueTest csv.set( "" ); assertEquals( "", csv.getCopy() ); - - csv.clear(); - assertNull( csv.getCopy() ); } @@ -266,32 +314,9 @@ public class StringValueTest csv.set( "test" ); assertFalse( csv.isNull() ); - - csv.clear(); - assertTrue( csv.isNull() ); } - - /** - * Test method for {@link org.apache.directory.shared.ldap.entry.StringValue#clear()}. - */ - @Test - public void testClear() throws LdapException - { - StringValue csv = new StringValue(); - - csv.clear(); - assertTrue( csv.isNull() ); - - csv.set( "test" ); - assertTrue( csv.isValid( new Ia5StringSyntaxChecker() ) ); - csv.clear(); - assertTrue( csv.isNull() ); - assertTrue( csv.isValid( new Ia5StringSyntaxChecker() ) ); - assertFalse( csv.isNormalized() ); - } - - + /** * Test method for {@link org.apache.directory.shared.ldap.entry.StringValue#isNormalized()}. */ @@ -341,8 +366,6 @@ public class StringValueTest assertFalse( csv.isNormalized() ); csv.normalize( new DeepTrimToLowerNormalizer( "1.1.1" ) ); - csv.clear(); - assertFalse( csv.isNormalized() ); } @@ -362,10 +385,6 @@ public class StringValueTest csv.normalize( new DeepTrimToLowerNormalizer( "1.1.1" ) ); assertEquals( "this is a test", csv.getNormalizedValue() ); - - csv.clear(); - assertFalse( csv.isNormalized() ); - assertEquals( null, csv.getNormalizedValue() ); } @@ -385,10 +404,6 @@ public class StringValueTest csv.normalize( new DeepTrimToLowerNormalizer( "1.1.1" ) ); assertEquals( "this is a test", csv.getNormalizedValueCopy() ); - - csv.clear(); - assertFalse( csv.isNormalized() ); - assertEquals( null, csv.getNormalizedValueCopy() ); } @@ -466,35 +481,6 @@ public class StringValueTest /** - * Test method for {@link org.apache.directory.shared.ldap.entry.StringValue#clone()}. - */ - @Test - public void testClone() throws LdapException - { - StringValue csv = new StringValue(); - - StringValue csv1 = (StringValue)csv.clone(); - - assertEquals( csv, csv1 ); - - csv.set( "" ); - - assertNotSame( csv, csv1 ); - assertNull( csv1.get() ); - assertEquals( "", csv.getString() ); - - csv.set( " This is a TEST " ); - csv1 = csv.clone(); - - assertEquals( csv, csv1 ); - - csv.normalize( new DeepTrimToLowerNormalizer( "1.1.1" ) ); - - assertNotSame( csv, csv1 ); - } - - - /** * Test method for {@link org.apache.directory.shared.ldap.entry.StringValue#equals(java.lang.Object)}. */ @Test @@ -534,9 +520,6 @@ public class StringValueTest csv.set( "Test" ); assertEquals( "Test", csv.toString() ); - - csv.clear(); - assertEquals( "null", csv.toString() ); }