001    /*
002     * CDDL HEADER START
003     *
004     * The contents of this file are subject to the terms of the
005     * Common Development and Distribution License, Version 1.0 only
006     * (the "License").  You may not use this file except in compliance
007     * with the License.
008     *
009     * You can obtain a copy of the license at
010     * trunk/opends/resource/legal-notices/OpenDS.LICENSE
011     * or https://OpenDS.dev.java.net/OpenDS.LICENSE.
012     * See the License for the specific language governing permissions
013     * and limitations under the License.
014     *
015     * When distributing Covered Code, include this CDDL HEADER in each
016     * file and include the License file at
017     * trunk/opends/resource/legal-notices/OpenDS.LICENSE.  If applicable,
018     * add the following below this CDDL HEADER, with the fields enclosed
019     * by brackets "[]" replaced with your own identifying information:
020     *      Portions Copyright [yyyy] [name of copyright owner]
021     *
022     * CDDL HEADER END
023     *
024     *
025     *      Copyright 2006-2008 Sun Microsystems, Inc.
026     */
027    package org.opends.server.schema;
028    
029    
030    
031    
032    
033    
034    /**
035     * This class defines utility methods that can be used to determine whether a
036     * character string is printable as defined in X.520 and referenced in RFC 2252.
037     * Printable characters consist of the set of uppercase and lowercase alphabetic
038     * characters, numeric digits, quotation mark, open and close parentheses, plus,
039     * minus, comma, period, slash, colon, question mark, and space.
040     */
041    public class PrintableString
042    {
043    
044    
045    
046      /**
047       * Indicates whether the provided character is a valid printable character.
048       *
049       * @param  c  The character for which to make the determination.
050       *
051       * @return  <CODE>true</CODE> if the provided character is a printable
052       *          character, or <CODE>false</CODE> if not.
053       */
054      public static boolean isPrintableCharacter(char c)
055      {
056        switch (c)
057        {
058          case 'a':
059          case 'b':
060          case 'c':
061          case 'd':
062          case 'e':
063          case 'f':
064          case 'g':
065          case 'h':
066          case 'i':
067          case 'j':
068          case 'k':
069          case 'l':
070          case 'm':
071          case 'n':
072          case 'o':
073          case 'p':
074          case 'q':
075          case 'r':
076          case 's':
077          case 't':
078          case 'u':
079          case 'v':
080          case 'w':
081          case 'x':
082          case 'y':
083          case 'z':
084          case 'A':
085          case 'B':
086          case 'C':
087          case 'D':
088          case 'E':
089          case 'F':
090          case 'G':
091          case 'H':
092          case 'I':
093          case 'J':
094          case 'K':
095          case 'L':
096          case 'M':
097          case 'N':
098          case 'O':
099          case 'P':
100          case 'Q':
101          case 'R':
102          case 'S':
103          case 'T':
104          case 'U':
105          case 'V':
106          case 'W':
107          case 'X':
108          case 'Y':
109          case 'Z':
110          case '0':
111          case '1':
112          case '2':
113          case '3':
114          case '4':
115          case '5':
116          case '6':
117          case '7':
118          case '8':
119          case '9':
120          case '\'':
121          case '(':
122          case ')':
123          case '+':
124          case ',':
125          case '-':
126          case '.':
127          case '=':
128          case '/':
129          case ':':
130          case '?':
131          case ' ':
132            return true;
133          default:
134            return false;
135        }
136      }
137    
138    
139    
140      /**
141       * Indicates whether the provided string is a valid printable string.
142       *
143       * @param  s  The string for which to make the determination.
144       *
145       * @return  <CODE>true</CODE> if the provided string is a printable string, or
146       *          <CODE>false</CODE> if not.
147       */
148      public static boolean isPrintableString(String s)
149      {
150        if (s == null)
151        {
152          return false;
153        }
154    
155        int length = s.length();
156        for (int i=0; i < length; i++)
157        {
158          if (! isPrintableCharacter(s.charAt(i)))
159          {
160            return false;
161          }
162        }
163    
164        return true;
165      }
166    }
167