/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
*
* ***** BEGIN LICENSE BLOCK *****
* Version: MPL 1.1/GPL 2.0/LGPL 2.1
*
* The contents of this file are subject to the Mozilla Public License Version
* 1.1 (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
* http://www.mozilla.org/MPL/
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
* for the specific language governing rights and limitations under the
* License.
*
* The Original Code is mozilla.org code.
*
* The Initial Developer of the Original Code is
* Netscape Communications Corporation.
* Portions created by the Initial Developer are Copyright (C) 1999
* the Initial Developer. All Rights Reserved.
*
* Contributor(s):
*
* Alternatively, the contents of this file may be used under the terms of
* either the GNU General Public License Version 2 or later (the "GPL"), or
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
* in which case the provisions of the GPL or the LGPL are applicable instead
* of those above. If you wish to allow use of your version of this file only
* under the terms of either the GPL or the LGPL, and not to allow others to
* use your version of this file under the terms of the MPL, indicate your
* decision by deleting the provisions above and replace them with the notice
* and other provisions required by the GPL or the LGPL. If you do not delete
* the provisions above, a recipient may use your version of this file under
* the terms of any one of the MPL, the GPL or the LGPL.
*
* ***** END LICENSE BLOCK ***** */
package org.ietf.ldap;
import java.io.Serializable;
import java.util.Enumeration;
import java.util.Hashtable;
import java.util.StringTokenizer;
import java.util.Vector;
/**
*
* Abstract class representing an element (such as an object class
* definition, an attribute type definition, or a matching rule
* definition) in the schema. The specific types of elements are
* represented by the LDAPObjectClassSchema,
* LDAPAttributeSchema, and LDAPMatchingRuleSchema
* subclasses.
*
* * RFC 2252, Lightweight Directory Access Protocol (v3): * Attribute Syntax Definitions covers the types of information * that need to be specified in the definition of an object class, * attribute type, or matching rule. All of these schema elements * can specify the following information: *
* *
*
* In addition, there are optional standard qualifiers for attribute
* types (see LDAPAttributeSchema), and implementation-specific
* qualifiers may be added. Non-standard qualifiers must have names
* starting with X-, e.g. "X-OWNER 'John Jacobson'". Optional and
* non-standard qualifiers can be accessed with getQualifier and
* setQualifier, and enumerated with
* getQualifierNames.
*
*
* The LDAPSchemaElement class implements methods that
* you can use with different types of schema elements (object class
* definitions, attribute type definitions, and matching rule definitions).
* You can do the following:
*
*
* @see org.ietf.ldap.LDAPObjectClassSchema
* @see org.ietf.ldap.LDAPAttributeSchema
* @see org.ietf.ldap.LDAPMatchingRuleSchema
* @version 1.0
**/
public abstract class LDAPSchemaElement implements Serializable {
static final long serialVersionUID = -3972153461950418863L;
/**
* Constructs a blank element.
*/
protected LDAPSchemaElement() {
}
/**
* Constructs a definition explicitly
*
* @param names names of element
* @param oid dotted-string object identifier
* @param description description of element
*/
protected LDAPSchemaElement( String[] names,
String oid,
String description ) {
if ( oid == null ) {
throw new IllegalArgumentException( "OID required" );
}
this.names = names;
this.oid = oid;
this.description = description;
}
/**
* Gets the description of the object class, attribute type,
* or matching rule.
* @return the description of the object class, attribute type,
* or matching rule.
*/
public String getDescription() {
return description;
}
/**
* Gets the object ID (OID) of the object class, attribute type,
* or matching rule in dotted-string format (for example, "1.2.3.4").
* @return the OID of the object class, attribute type,
* or matching rule.
*/
public String getID() {
return oid;
}
/**
* Gets the names of the schema element
*
* @return the names of the schema element
*/
public String[] getNames() {
return names;
}
/**
* Gets the value of a qualifier which is not predefined.
* @param name name of qualifier
* @return value or values of qualifier;
* @param quotingBug null if not
* present, a zero-length array if present but with no value.
*/
public String[] getQualifier( String name ) {
if ( properties == null ) {
return null;
}
Object o = properties.get( name );
if ( o == null ) {
return null;
}
if ( o instanceof Vector ) {
Vectortrue if the element is defined as obsolete.
*/
public boolean isObsolete() {
return properties == null ? false : properties.containsKey(OBSOLETE);
}
/**
* Keeps track of qualifiers which are not predefined.
* @param name name of qualifier
* @param value value of qualifier. "" for no value, null
* to remove the qualifier
*/
public void setQualifier( String name, String value ) {
if ( properties == null ) {
properties = new Hashtabletrue if single quotes are to be
* supplied around the SYNTAX and SUP value
* @return a formatted String for defining a schema element.
*/
String getValue() {
return getValue( false );
}
String getValue( boolean quotingBug ) {
return null;
}
/**
* Prepares the initial common part of a schema element value in
* RFC 2252 format for submitting to a server
*
* @return the OID, name, description, and possibly OBSOLETE
* fields of a schema element definition.
*/
String getValuePrefix() {
String s = "( " + oid + ' ';
s += getNameField();
if ( description != null ) {
s += "DESC \'" + description + "\' ";
}
if ( isObsolete() ) {
s += OBSOLETE + ' ';
}
return s;
}
String getNameField() {
String s = "";
if ( (names != null) && (names.length > 0) ) {
s += "NAME ";
if ( names.length > 1 ) {
s += "( ";
}
for( int i = 0; i < names.length; i++ ) {
s += '\'' + names[i] + "\' ";
}
if ( names.length > 1 ) {
s += ") ";
}
}
return s;
}
/**
* Gets qualifiers which may or may not be present
*
* @param names list of qualifiers to look up
* @return String in RFC 2252 format containing any values
* found, not terminated with ' '.
*/
protected String getOptionalValues( String[] names ) {
String s = "";
for( int i = 0; i < names.length; i++ ) {
String[] vals = getQualifier( names[i] );
if ( (vals != null) && (vals.length > 0) ) {
s += names[i] + ' ' + vals[0];
}
}
return s;
}
/**
* Gets any qualifiers marked as custom (starting with "X-")
*
* @return string in RFC 2252 format, without a terminating
* ' '.
*/
protected String getCustomValues() {
String s = "";
Enumerationtrue if values should be enveloped
* with single quotes
* @param doDollar true if a list of values should use
* " $ " as separator; that is true for object class attribute lists
* @return String in RFC 2252 format, without a terminating
* ' '.
*/
String getValue( String key, boolean doQuote, boolean doDollar ) {
String s = "";
Object o = properties.get( key );
if ( o == null ) {
return s;
}
if ( o instanceof String ) {
if ( ((String)o).length() > 0 ) {
s += key + ' ';
if ( doQuote ) {
s += '\'';
}
s += (String)o;
if ( doQuote ) {
s += '\'';
}
}
} else {
s += key + " ( ";
Vectortrue if values should be enveloped
* with single quotes
* @return String in RFC 2252 format, without a terminating
* ' '.
*/
String getValue( String key, boolean doQuote ) {
return getValue( key, doQuote, true );
}
/**
* Creates a string for use in toString with any qualifiers of the element.
*
* @param ignore any qualifiers to NOT include
* @return a String with any known qualifiers.
*/
String getQualifierString( String[] ignore ) {
Hashtable