Debian Codegraph @

java-sdk/ietfldap/org/ietf/ldap/LDAPSchemaElement.java

raw symbols SCIP
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.io.Serializable;
41 import java.util.Enumeration;
42 import java.util.Hashtable;
43 import java.util.StringTokenizer;
44 import java.util.Vector;
45
46 /**
47 *
48 * Abstract class representing an element (such as an object class
49 * definition, an attribute type definition, or a matching rule
50 * definition) in the schema. The specific types of elements are
51 * represented by the <CODE>LDAPObjectClassSchema</CODE>,
52 * <CODE>LDAPAttributeSchema</CODE>, and <CODE>LDAPMatchingRuleSchema</CODE>
53 * subclasses.
54 * <P>
55 *
56 * <A HREF="http://ds.internic.net/rfc/rfc2252.txt"
57 * TARGET="_blank">RFC 2252, Lightweight Directory Access Protocol (v3):
58 * Attribute Syntax Definitions</A> covers the types of information
59 * that need to be specified in the definition of an object class,
60 * attribute type, or matching rule. All of these schema elements
61 * can specify the following information:
62 * <P>
63 *
64 * <UL>
65 * <LI>a name identifying the element
66 * <LI>an OID identifying the element
67 * <LI>a description of the element
68 * <LI>a qualifier "OBSOLETE"
69 * </UL>
70 * <P>
71 *
72 * In addition, there are optional standard qualifiers for attribute
73 * types (see LDAPAttributeSchema), and implementation-specific
74 * qualifiers may be added. Non-standard qualifiers must have names
75 * starting with X-, e.g. "X-OWNER 'John Jacobson'". Optional and
76 * non-standard qualifiers can be accessed with <CODE>getQualifier</CODE> and
77 * <CODE>setQualifier</CODE>, and enumerated with
78 * <CODE>getQualifierNames</CODE>.
79 * <P>
80 *
81 * The <CODE>LDAPSchemaElement</CODE> class implements methods that
82 * you can use with different types of schema elements (object class
83 * definitions, attribute type definitions, and matching rule definitions).
84 * You can do the following:
85 * <UL>
86 * <LI>get the name of a schema element
87 * <LI>get the OID of a schema element
88 * <LI>get the description of a schema element
89 * <LI>add an element to the schema
90 * <LI>remove an element from the schema
91 * </UL>
92 * <P>
93 *
94 * @see org.ietf.ldap.LDAPObjectClassSchema
95 * @see org.ietf.ldap.LDAPAttributeSchema
96 * @see org.ietf.ldap.LDAPMatchingRuleSchema
97 * @version 1.0
98 **/
99
100 public abstract class LDAPSchemaElement implements Serializable {
101
102 static final long serialVersionUID = -3972153461950418863L;
103
104 /**
105 * Constructs a blank element.
106 */
107 protected LDAPSchemaElement() {
108 }
109
110 /**
111 * Constructs a definition explicitly
112 *
113 * @param names names of element
114 * @param oid dotted-string object identifier
115 * @param description description of element
116 */
117 protected LDAPSchemaElement( String[] names,
118 String oid,
119 String description ) {
120 if ( oid == null ) {
121 throw new IllegalArgumentException( "OID required" );
122 }
123 this.names = names;
124 this.oid = oid;
125 this.description = description;
126 }
127
128 /**
129 * Gets the description of the object class, attribute type,
130 * or matching rule.
131 * @return the description of the object class, attribute type,
132 * or matching rule.
133 */
134 public String getDescription() {
135 return description;
136 }
137
138 /**
139 * Gets the object ID (OID) of the object class, attribute type,
140 * or matching rule in dotted-string format (for example, "1.2.3.4").
141 * @return the OID of the object class, attribute type,
142 * or matching rule.
143 */
144 public String getID() {
145 return oid;
146 }
147
148 /**
149 * Gets the names of the schema element
150 *
151 * @return the names of the schema element
152 */
153 public String[] getNames() {
154 return names;
155 }
156
157 /**
158 * Gets the value of a qualifier which is not predefined.
159 * @param name name of qualifier
160 * @return value or values of qualifier; <CODE>null</CODE> if not
161 * present, a zero-length array if present but with no value.
162 */
163 public String[] getQualifier( String name ) {
164 if ( properties == null ) {
165 return null;
166 }
167 Object o = properties.get( name );
168 if ( o == null ) {
169 return null;
170 }
171 if ( o instanceof Vector ) {
172 Vector<String> v = (Vector<String>)o;
173 String[] vals = new String[v.size()];
174 v.copyInto( vals );
175 return vals;
176 }
177 String s = (String)o;
178 if ( s.length() < 1 ) {
179 return new String[0];
180 } else {
181 return new String[] { s };
182 }
183 }
184
185 /**
186 * Gets an enumeration of all qualifiers which are not predefined.
187 * @return enumeration of qualifiers.
188 */
189 public Enumeration<String> getQualifierNames() {
190 return properties.keys();
191 }
192
193 /**
194 * Reports if the element is marked as obsolete.
195 * @return <CODE>true<CODE> if the element is defined as obsolete.
196 */
197 public boolean isObsolete() {
198 return properties == null ? false : properties.containsKey(OBSOLETE);
199 }
200
201 /**
202 * Keeps track of qualifiers which are not predefined.
203 * @param name name of qualifier
204 * @param value value of qualifier. "" for no value, <CODE>null</CODE>
205 * to remove the qualifier
206 */
207 public void setQualifier( String name, String value ) {
208 if ( properties == null ) {
209 properties = new Hashtable<String, Object>();
210 }
211 if ( value != null ) {
212 properties.put( name, value );
213 } else {
214 properties.remove( name );
215 }
216 }
217
218 /**
219 * Keeps track of qualifiers which are not predefined.
220 * @param name name of qualifier
221 * @param values array of values
222 */
223 public void setQualifier( String name, String[] values ) {
224 if ( values == null ) {
225 return;
226 }
227 if ( properties == null ) {
228 properties = new Hashtable<String, Object>();
229 }
230 Vector<String> v = new Vector<>();
231 for( int i = 0; i < values.length; i++ ) {
232 v.addElement( values[i] );
233 }
234 properties.put( name, v );
235 }
236
237 /**
238 * Returns a String in a format suitable for directly adding to a
239 * Directory, as a value of the particular schema
240 * element attribute. See the format definition for each derived class
241 *
242 * @return Directory format of the schema element as a String
243 */
244 public abstract String toString();
245
246 /**
247 * Parses a raw schema value into OID, name, description, and
248 * a Hashtable of other qualifiers and values.
249 *
250 * @param raw a raw schema definition
251 */
252 protected void parseValue( String raw ) {
253 names = null;
254 if ( properties == null ) {
255 properties = new Hashtable<String, Object>();
256 }
257 int l = raw.length();
258 // Processing is faster in char array than in String
259 char[] ch = new char[l];
260 raw.getChars( 0, l, ch, 0 );
261 // Trim leading and trailing space
262 l--;
263 while( ch[l] == ' ' ) {
264 l--;
265 }
266 int start = 0;
267 while( ch[start] == ' ' ) {
268 start++;
269 }
270 // Skip past "( " and ")" to start of OID
271 start += 2;
272 // Find end of OID
273 int ind = start + 1;
274 while( ch[ind] != ' ' ) {
275 ind++;
276 }
277 oid = new String( ch, start, ind - start );
278
279 ind = ind + 1;
280 String s;
281 String val;
282 while ( ind < l ) {
283 // Skip past blanks to start of next token
284 while( ch[ind] == ' ' ) {
285 ind++;
286 }
287 // Find end of token
288 int last = ind + 1;
289 while( (last < l) && (ch[last] != ' ') )
290 last++;
291 if ( last < l ) {
292 // Found a token
293 s = new String( ch, ind, last-ind );
294 ind = last;
295 if ( novalsTable.containsKey( s ) ) {
296 properties.put( s, "" );
297 continue;
298 }
299 } else {
300 // Reached end of string with no end of token
301 s = "";
302 ind = l;
303 break;
304 }
305
306 // Find the start of the value of the token
307 while( (ind < l) && (ch[ind] == ' ') ) {
308 ind++;
309 }
310 last = ind + 1;
311 if ( ind >= l ) {
312 break;
313 }
314
315 boolean quoted = false;
316 boolean list = false;
317 if ( ch[ind] == '\'' ) {
318 // The value is quoted
319 quoted = true;
320 ind++;
321 while( (last < l) && (ch[last] != '\'') ) {
322 last++;
323 }
324 } else if ( ch[ind] == '(' ) {
325 // The value is a list
326 list = true;
327 ind++;
328 while( (last < l) && (ch[last] != ')') ) {
329 last++;
330 }
331 } else {
332 // The value is not quoted
333 while( (last < l) && (ch[last] != ' ') ) {
334 last++;
335 }
336 }
337 if ( (ind < last) && (last <= l) ) {
338 if ( list ) {
339 Vector<String> v = new Vector<>();
340 if ( ch[ind] == ' ' ) {
341 ind++;
342 }
343 val = new String( ch, ind, last-ind-1 );
344 // Is this a quoted list? If so, use ' as delimiter,
345 // otherwise use ' '. The space between quoted
346 // values will be returned as tokens containing only
347 // white space. White space is not valid in a list
348 // value, so we just remove all tokens containing
349 // only white space.
350 String delim = (val.indexOf( '\'' ) >= 0) ? "'" : " ";
351 StringTokenizer st = new StringTokenizer( val, delim );
352 while ( st.hasMoreTokens() ) {
353 String tok = st.nextToken().trim();
354 if ( (tok.length() > 0) && !tok.equals( "$" ) ) {
355 v.addElement( tok );
356 }
357 }
358 properties.put( s, v );
359 } else {
360 val = new String( ch, ind, last-ind );
361 if ( s.equals( "NAME" ) ) {
362 names = new String[] { val };
363 } else if ( s.equals( "DESC" ) ) {
364 description = val;
365 } else {
366 properties.put( s, val );
367 }
368 if ( quoted ) {
369 last++;
370 }
371 }
372 }
373 ind = last + 1;
374 }
375 if ( names == null ) {
376 names = getQualifier( "NAME" );
377 if ( names == null ) {
378 names = new String[0];
379 }
380 }
381 }
382
383 /**
384 * Gets the attribute name for a schema element
385 *
386 * @return The attribute name of the element
387 */
388 String getAttributeName() {
389 return null;
390 }
391
392 /**
393 * Formats a String in the format defined in X.501 (see
394 * <A HREF="http://ds.internic.net/rfc/rfc2252.txt"
395 * >RFC 2252, Lightweight Directory Access Protocol
396 * (v3): Attribute Syntax Definitions</A>
397 * for a description of this format).
398 * This is the format that LDAP servers and clients use to exchange
399 * schema information. For example, when
400 * you search an LDAP server for its schema, the server returns an entry
401 * with the attributes "objectclasses" and "attributetypes". The
402 * values of the "attributetypes" attribute are attribute type
403 * descriptions in this format.
404 * <P>
405 * @param quotingBug <CODE>true</CODE> if single quotes are to be
406 * supplied around the SYNTAX and SUP value
407 * @return a formatted String for defining a schema element.
408 */
409 String getValue() {
410 return getValue( false );
411 }
412
413 String getValue( boolean quotingBug ) {
414 return null;
415 }
416
417 /**
418 * Prepares the initial common part of a schema element value in
419 * RFC 2252 format for submitting to a server
420 *
421 * @return the OID, name, description, and possibly OBSOLETE
422 * fields of a schema element definition.
423 */
424 String getValuePrefix() {
425 String s = "( " + oid + ' ';
426 s += getNameField();
427 if ( description != null ) {
428 s += "DESC \'" + description + "\' ";
429 }
430 if ( isObsolete() ) {
431 s += OBSOLETE + ' ';
432 }
433 return s;
434 }
435
436 String getNameField() {
437 String s = "";
438 if ( (names != null) && (names.length > 0) ) {
439 s += "NAME ";
440 if ( names.length > 1 ) {
441 s += "( ";
442 }
443 for( int i = 0; i < names.length; i++ ) {
444 s += '\'' + names[i] + "\' ";
445 }
446 if ( names.length > 1 ) {
447 s += ") ";
448 }
449 }
450 return s;
451 }
452
453 /**
454 * Gets qualifiers which may or may not be present
455 *
456
457 * @param names list of qualifiers to look up
458 * @return String in RFC 2252 format containing any values
459 * found, not terminated with ' '.
460 */
461 protected String getOptionalValues( String[] names ) {
462 String s = "";
463 for( int i = 0; i < names.length; i++ ) {
464 String[] vals = getQualifier( names[i] );
465 if ( (vals != null) && (vals.length > 0) ) {
466 s += names[i] + ' ' + vals[0];
467 }
468 }
469 return s;
470 }
471
472 /**
473 * Gets any qualifiers marked as custom (starting with "X-")
474 *
475 * @return string in RFC 2252 format, without a terminating
476 * ' '.
477 */
478 protected String getCustomValues() {
479 String s = "";
480 Enumeration<String> en = properties.keys();
481 while( en.hasMoreElements() ) {
482 String key = en.nextElement();
483 if ( !key.startsWith( "X-" ) ) {
484 continue;
485 }
486 s += getValue( key, true, false ) + ' ';
487 }
488 // Strip trailing ' '
489 if ( (s.length() > 0) && (s.charAt( s.length() - 1 ) == ' ') ) {
490 s = s.substring( 0, s.length() - 1 );
491 }
492 return s;
493 }
494
495 /**
496 * Gets a qualifier's value or values, if present, and formats
497 * the String according to RFC 2252
498 *
499 * @param key the qualifier to get
500 * @param doQuote <CODE>true</CODE> if values should be enveloped
501 * with single quotes
502 * @param doDollar <CODE>true</CODE> if a list of values should use
503 * " $ " as separator; that is true for object class attribute lists
504 * @return String in RFC 2252 format, without a terminating
505 * ' '.
506 */
507 String getValue( String key, boolean doQuote, boolean doDollar ) {
508 String s = "";
509 Object o = properties.get( key );
510 if ( o == null ) {
511 return s;
512 }
513 if ( o instanceof String ) {
514 if ( ((String)o).length() > 0 ) {
515 s += key + ' ';
516 if ( doQuote ) {
517 s += '\'';
518 }
519 s += (String)o;
520 if ( doQuote ) {
521 s += '\'';
522 }
523 }
524 } else {
525 s += key + " ( ";
526 Vector<String> v = (Vector<String>)o;
527 for( int i = 0; i < v.size(); i++ ) {
528 if ( doQuote ) {
529 s += '\'';
530 }
531 s += v.elementAt(i);
532 if ( doQuote ) {
533 s += '\'';
534 }
535 s += ' ';
536 if ( doDollar && (i < (v.size() - 1)) ) {
537 s += "$ ";
538 }
539 }
540 s += ')';
541 }
542
543 return s;
544 }
545
546 /**
547 * Gets a qualifier's value or values, if present, and format
548 * the String according to RFC 2252.
549 *
550 * @param key the qualifier to get
551 * @param doQuote <CODE>true</CODE> if values should be enveloped
552 * with single quotes
553 * @return String in RFC 2252 format, without a terminating
554 * ' '.
555 */
556 String getValue( String key, boolean doQuote ) {
557 return getValue( key, doQuote, true );
558 }
559
560 /**
561 * Creates a string for use in toString with any qualifiers of the element.
562 *
563 * @param ignore any qualifiers to NOT include
564 * @return a String with any known qualifiers.
565 */
566 String getQualifierString( String[] ignore ) {
567 Hashtable<String, Object> toIgnore = null;
568 if ( ignore != null ) {
569 toIgnore = new Hashtable<>();
570 for( int i = 0; i < ignore.length; i++ ) {
571 toIgnore.put( ignore[i], ignore[i] );
572 }
573 }
574 String s = "";
575 Enumeration<String> en = getQualifierNames();
576 while( en.hasMoreElements() ) {
577 String qualifier = en.nextElement();
578 if ( (toIgnore != null) && toIgnore.containsKey( qualifier ) ) {
579 continue;
580 }
581 s += "; " + qualifier;
582 String[] vals = getQualifier( qualifier );
583 if ( vals == null ) {
584 s += ' ';
585 continue;
586 }
587 s += ": ";
588 for( int i = 0; i < vals.length; i++ ) {
589 s += vals[i] + ' ';
590 }
591 }
592 // Strip trailing ' '
593 if ( (s.length() > 0) && (s.charAt( s.length() - 1 ) == ' ') ) {
594 s = s.substring( 0, s.length() - 1 );
595 }
596 return s;
597 }
598
599 // Constants for known syntax types
600 public static final int unknown = 0;
601 public static final int cis = 1;
602 public static final int binary = 2;
603 public static final int telephone = 3;
604 public static final int ces = 4;
605 public static final int dn = 5;
606 public static final int integer = 6;
607
608 protected static final String cisString =
609 "1.3.6.1.4.1.1466.115.121.1.15";
610 protected static final String binaryString =
611 "1.3.6.1.4.1.1466.115.121.1.5";
612 protected static final String telephoneString =
613 "1.3.6.1.4.1.1466.115.121.1.50";
614 protected static final String cesString =
615 "1.3.6.1.4.1.1466.115.121.1.26";
616 protected static final String intString =
617 "1.3.6.1.4.1.1466.115.121.1.27";
618 protected static final String dnString =
619 "1.3.6.1.4.1.1466.115.121.1.12";
620 // Predefined qualifiers which apply to any schema element type
621 public static final String OBSOLETE = "OBSOLETE";
622 public static final String SUPERIOR = "SUP";
623
624 // Predefined qualifiers
625 public static final String SYNTAX = "SYNTAX";
626
627 // Properties which are common to all schema elements
628 protected String oid = null;
629 protected String description = "";
630 protected String attrName = null;
631 protected String rawValue = null;
632 protected String[] names = new String[0];
633 // Additional qualifiers
634 protected Hashtable<String, Object> properties = null;
635 // Qualifiers known to not have values
636 static protected Hashtable<String, String> novalsTable = new Hashtable<>();
637 }