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