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