| 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
| 2 | * |
| 3 | * ***** BEGIN LICENSE BLOCK ***** |
| 4 | * Version: MPL 1.1/GPL 2.0/LGPL 2.1 |
| 5 | * |
| 6 | * The contents of this file are subject to the Mozilla Public License Version |
| 7 | * 1.1 (the "License"); you may not use this file except in compliance with |
| 8 | * the License. You may obtain a copy of the License at |
| 9 | * http://www.mozilla.org/MPL/ |
| 10 | * |
| 11 | * Software distributed under the License is distributed on an "AS IS" basis, |
| 12 | * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License |
| 13 | * for the specific language governing rights and limitations under the |
| 14 | * License. |
| 15 | * |
| 16 | * The Original Code is mozilla.org code. |
| 17 | * |
| 18 | * The Initial Developer of the Original Code is |
| 19 | * Netscape Communications Corporation. |
| 20 | * Portions created by the Initial Developer are Copyright (C) 1999 |
| 21 | * the Initial Developer. All Rights Reserved. |
| 22 | * |
| 23 | * Contributor(s): |
| 24 | * |
| 25 | * Alternatively, the contents of this file may be used under the terms of |
| 26 | * either the GNU General Public License Version 2 or later (the "GPL"), or |
| 27 | * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"), |
| 28 | * in which case the provisions of the GPL or the LGPL are applicable instead |
| 29 | * of those above. If you wish to allow use of your version of this file only |
| 30 | * under the terms of either the GPL or the LGPL, and not to allow others to |
| 31 | * use your version of this file under the terms of the MPL, indicate your |
| 32 | * decision by deleting the provisions above and replace them with the notice |
| 33 | * and other provisions required by the GPL or the LGPL. If you do not delete |
| 34 | * the provisions above, a recipient may use your version of this file under |
| 35 | * the terms of any one of the MPL, the GPL or the LGPL. |
| 36 | * |
| 37 | * ***** END LICENSE BLOCK ***** */ |
| 38 | package org.ietf.ldap; |
| 39 | |
| 40 | import java.util.Vector; |
| 41 | |
| 42 | /** |
| 43 | * The definition of a matching rule in the schema. |
| 44 | * <A HREF="http://ds.internic.net/rfc/rfc2252.txt" |
| 45 | * TARGET="_blank">RFC 2252, Lightweight Directory Access Protocol (v3): |
| 46 | * Attribute Syntax Definitions</A> covers the types of information |
| 47 | * that need to be specified in the definition of a matching rule. |
| 48 | * According to the RFC, the description of a matching rule can |
| 49 | * include the following information: |
| 50 | * <P> |
| 51 | * |
| 52 | * <UL> |
| 53 | * <LI>an OID identifying the matching rule |
| 54 | * <LI>a name identifying the matching rule |
| 55 | * <LI>a description of the matching rule |
| 56 | * <LI>the syntax of the matching rule |
| 57 | * </UL> |
| 58 | * <P> |
| 59 | * |
| 60 | * The <CODE>LDAPMatchingRuleSchema</CODE> class also specifies |
| 61 | * the matching rule "use description", which describes the |
| 62 | * attributes which can be used with the matching rule. |
| 63 | * <P> |
| 64 | * |
| 65 | * When you construct an <CODE>LDAPMatchingRuleSchema</CODE> object, you can |
| 66 | * specify these types of information as arguments to the constructor or |
| 67 | * in the MatchingRuleDescription and MatchingRuleUseDescription formats |
| 68 | * specified in RFC 2252. |
| 69 | * When an LDAP client searches an LDAP server for the schema, the server |
| 70 | * returns schema information as an object with attribute values in this |
| 71 | * format. |
| 72 | * <P> |
| 73 | * |
| 74 | * You can get the name, OID, and description of this matching rule |
| 75 | * definition by using the <CODE>getName</CODE>, <CODE>getOID</CODE>, and |
| 76 | * <CODE>getDescription</CODE> methods inherited from the abstract class |
| 77 | * <CODE>LDAPSchemaElement</CODE>. Custom qualifiers are |
| 78 | * accessed with <CODE>getQualifier</CODE> and <CODE>getQualifierNames</CODE> |
| 79 | * from <CODE>LDAPSchemaElement</CODE>. |
| 80 | * <P> |
| 81 | * |
| 82 | * To add or remove this matching rule definition from the |
| 83 | * schema, use the <CODE>add</CODE> and <CODE>remove</CODE> |
| 84 | * methods, which this class inherits from the <CODE>LDAPSchemaElement</CODE> |
| 85 | * abstract class. |
| 86 | * <P> |
| 87 | * RFC 2252 defines MatchingRuleDescription and MatchingRuleUseDescription |
| 88 | * as follows: |
| 89 | * <P> |
| 90 | * <PRE> |
| 91 | * MatchingRuleDescription = "(" whsp |
| 92 | * numericoid whsp ; MatchingRule identifier |
| 93 | * [ "NAME" qdescrs ] |
| 94 | * [ "DESC" qdstring ] |
| 95 | * [ "OBSOLETE" whsp ] |
| 96 | * "SYNTAX" numericoid |
| 97 | * whsp ")" |
| 98 | * |
| 99 | * Values of the matchingRuleUse list the attributes which are suitable |
| 100 | * for use with an extensible matching rule. |
| 101 | * |
| 102 | * MatchingRuleUseDescription = "(" whsp |
| 103 | * numericoid whsp ; MatchingRule identifier |
| 104 | * [ "NAME" qdescrs ] |
| 105 | * [ "DESC" qdstring ] |
| 106 | * [ "OBSOLETE" ] |
| 107 | * "APPLIES" oids ; AttributeType identifiers |
| 108 | * whsp ")" |
| 109 | * </PRE> |
| 110 | * <P> |
| 111 | * <CODE>LDAPMatchingRuleSchema</CODE> abstracts away from the two types and |
| 112 | * manages their relationships transparently. |
| 113 | * |
| 114 | * @version 1.0 |
| 115 | * @see org.ietf.ldap.LDAPSchemaElement |
| 116 | **/ |
| 117 | |
| 118 | public class LDAPMatchingRuleSchema extends LDAPAttributeSchema { |
| 119 | |
| 120 | static final long serialVersionUID = 6466155218986944131L; |
| 121 | |
| 122 | /** |
| 123 | * Constructs a matching rule definition, using the specified |
| 124 | * information. |
| 125 | * @param names name of the matching rule |
| 126 | * @param oid object identifier (OID) of the matching rule |
| 127 | * in dotted-decimal format (for example, "1.2.3.4"). |
| 128 | * @param description description of the matching rule. |
| 129 | * @param attributes array of the OIDs of the attributes for which |
| 130 | * the matching rule is applicable. |
| 131 | * @param obsolete <CODE>true</CODE> if the element is obsolete |
| 132 | * @param syntaxString syntax of this matching rule in dotted-decimal |
| 133 | * format |
| 134 | */ |
| 135 | public LDAPMatchingRuleSchema( String[] names, |
| 136 | String oid, |
| 137 | String description, |
| 138 | String[] attributes, |
| 139 | boolean obsolete, |
| 140 | String syntaxString ) { |
| 141 | if ( (oid == null) || (oid.trim().length() < 1) ) { |
| 142 | throw new IllegalArgumentException( "OID required" ); |
| 143 | } |
| 144 | this.names = names; |
| 145 | this.oid = oid; |
| 146 | this.description = description; |
| 147 | attrName = "matchingrules"; |
| 148 | syntaxElement.syntax = syntaxElement.syntaxCheck( syntaxString ); |
| 149 | syntaxElement.syntaxString = syntaxString; |
| 150 | setQualifier( SYNTAX, syntaxElement.syntaxString ); |
| 151 | _attributes = new String[attributes.length]; |
| 152 | for( int i = 0; i < attributes.length; i++ ) { |
| 153 | this._attributes[i] = attributes[i]; |
| 154 | } |
| 155 | if ( obsolete ) { |
| 156 | setQualifier( OBSOLETE, "" ); |
| 157 | } |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Constructs a matching rule definition based on descriptions in |
| 162 | * the MatchingRuleDescription format and MatchingRuleUseDescription |
| 163 | * format. For information on this format, |
| 164 | * (see <A HREF="http://ds.internic.net/rfc/rfc2252.txt" |
| 165 | * TARGET="_blank">RFC 2252, Lightweight Directory Access Protocol (v3): |
| 166 | * Attribute Syntax Definitions</A>. This is the format that LDAP servers |
| 167 | * and clients use to exchange schema information. For example, when |
| 168 | * you search an LDAP server for its schema, the server returns an entry |
| 169 | * with attributes that include "matchingrule" and "matchingruleuse". |
| 170 | * The values of these attributes are matching rule descriptions |
| 171 | * in this format. |
| 172 | * <P> |
| 173 | * |
| 174 | * @param raw definition of the matching rule in the |
| 175 | * MatchingRuleDescription format |
| 176 | * @param use definition of the use of the matching rule in the |
| 177 | * MatchingRuleUseDescription format |
| 178 | */ |
| 179 | public LDAPMatchingRuleSchema( String raw, String use ) { |
| 180 | attrName = "matchingrules"; |
| 181 | if ( raw != null ) { |
| 182 | parseValue( raw ); |
| 183 | } |
| 184 | if ( use != null ) { |
| 185 | parseValue( use ); |
| 186 | } |
| 187 | Vector<String> v = (Vector<String>)properties.get( "APPLIES" ); |
| 188 | if ( v != null ) { |
| 189 | _attributes = new String[v.size()]; |
| 190 | v.copyInto( _attributes ); |
| 191 | v.removeAllElements(); |
| 192 | } |
| 193 | String val = (String)properties.get( "SYNTAX" ); |
| 194 | if ( val != null ) { |
| 195 | syntaxElement.syntaxString = val; |
| 196 | syntaxElement.syntax = syntaxElement.syntaxCheck( val ); |
| 197 | } |
| 198 | } |
| 199 | |
| 200 | /** |
| 201 | * Gets the list of the OIDs of the attribute types which can be used |
| 202 | * with the matching rule. |
| 203 | * @return array of the OIDs of the attribute types which can be used |
| 204 | * with the matching rule. |
| 205 | */ |
| 206 | public String[] getAttributes() { |
| 207 | return _attributes; |
| 208 | } |
| 209 | |
| 210 | /** |
| 211 | * Prepare a value in RFC 2252 format for submitting to a server |
| 212 | * |
| 213 | * @param quotingBug <CODE>true</CODE> if SUP and SYNTAX values are to |
| 214 | * be quoted; that is to satisfy bugs in certain LDAP servers. |
| 215 | * @return a String ready to be submitted to an LDAP server |
| 216 | */ |
| 217 | String getValue( boolean quotingBug ) { |
| 218 | String s = getValuePrefix(); |
| 219 | if ( syntaxElement.syntaxString != null ) { |
| 220 | s += "SYNTAX "; |
| 221 | if ( quotingBug ) { |
| 222 | s += '\''; |
| 223 | } |
| 224 | s += syntaxElement.syntaxString; |
| 225 | if ( quotingBug ) { |
| 226 | s += '\''; |
| 227 | } |
| 228 | s += ' '; |
| 229 | } |
| 230 | String val = getCustomValues(); |
| 231 | if ( val.length() > 0 ) { |
| 232 | s += val + ' '; |
| 233 | } |
| 234 | s += ')'; |
| 235 | return s; |
| 236 | } |
| 237 | |
| 238 | /** |
| 239 | * Gets the attribute name for a schema element |
| 240 | * |
| 241 | * @return The attribute name of the element |
| 242 | */ |
| 243 | String getAttributeName() { |
| 244 | return "matchingrules"; |
| 245 | } |
| 246 | |
| 247 | /** |
| 248 | * Gets the matching rule definition in the string representation |
| 249 | * of the MatchingRuleDescription data type defined in X.501 (see |
| 250 | * <A HREF="http://ds.internic.net/rfc/rfc2252.txt" |
| 251 | * TARGET="_blank">RFC 2252, Lightweight Directory Access Protocol |
| 252 | * (v3): Attribute Syntax Definitions</A> |
| 253 | * for a description of these formats). |
| 254 | * This is the format that LDAP servers and clients use to exchange |
| 255 | * schema information. (For example, when |
| 256 | * you search an LDAP server for its schema, the server returns an entry |
| 257 | * with the attributes "matchingrules" and "matchingruleuse". The |
| 258 | * values of these attributes are matching rule description and |
| 259 | * matching rule use description in these formats.) |
| 260 | * <P> |
| 261 | * |
| 262 | * @return a string in a format that can be used as the value of |
| 263 | * the <CODE>matchingrule</CODE> attribute (which describes |
| 264 | * a matching rule in the schema) of a <CODE>subschema</CODE> object |
| 265 | */ |
| 266 | String getValue() { |
| 267 | return getValue( false ); |
| 268 | } |
| 269 | |
| 270 | /** |
| 271 | * Gets the matching rule use definition in the string representation |
| 272 | * of the MatchingRuleUseDescription data type defined in X.501 (see |
| 273 | * <A HREF="http://ds.internic.net/rfc/rfc2252.txt" |
| 274 | * TARGET="_blank">RFC 2252, Lightweight Directory Access Protocol |
| 275 | * (v3): Attribute Syntax Definitions</A> |
| 276 | * for a description of these formats). |
| 277 | * This is the format that LDAP servers and clients use to exchange |
| 278 | * schema information. (For example, when |
| 279 | * you search an LDAP server for its schema, the server returns an entry |
| 280 | * with the attributes "matchingrules" and "matchingruleuse". The |
| 281 | * values of these attributes are matching rule description and |
| 282 | * matching rule use description in these formats.) |
| 283 | * <P> |
| 284 | * |
| 285 | * @return a string in a format that can be used as the value of |
| 286 | * the <CODE>matchingruleuse</CODE> attribute (which describes the use of |
| 287 | * a matching rule in the schema) of a <CODE>subschema</CODE> object |
| 288 | */ |
| 289 | public String getUseValue() { |
| 290 | String s = getValuePrefix(); |
| 291 | if ( (_attributes != null) && (_attributes.length > 0) ) { |
| 292 | s += "APPLIES ( "; |
| 293 | for( int i = 0; i < _attributes.length; i++ ) { |
| 294 | if ( i > 0 ) |
| 295 | s += " $ "; |
| 296 | s += _attributes[i]; |
| 297 | } |
| 298 | s += " ) "; |
| 299 | } |
| 300 | s += ')'; |
| 301 | return s; |
| 302 | } |
| 303 | |
| 304 | /** |
| 305 | * Gets the definition of the matching rule in Directory format |
| 306 | * |
| 307 | * @return definition of the matching rule in Directory format |
| 308 | */ |
| 309 | public String toString() { |
| 310 | return getValue( false ); |
| 311 | } |
| 312 | |
| 313 | // Qualifiers tracked explicitly |
| 314 | static final String[] EXPLICIT = { OBSOLETE, |
| 315 | SYNTAX }; |
| 316 | |
| 317 | private String[] _attributes = null; |
| 318 | } |