KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > derbyTesting > functionTests > util > Formatters


1 /*
2
3    Derby - Class org.apache.derbyTesting.functionTests.util.Formatters
4
5    Licensed to the Apache Software Foundation (ASF) under one or more
6    contributor license agreements. See the NOTICE file distributed with
7    this work for additional information regarding copyright ownership.
8    The ASF licenses this file to You under the Apache License, Version 2.0
9    (the "License"); you may not use this file except in compliance with
10    the License. You may obtain a copy of the License at
11
12       http://www.apache.org/licenses/LICENSE-2.0
13
14    Unless required by applicable law or agreed to in writing, software
15    distributed under the License is distributed on an "AS IS" BASIS,
16    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17    See the License for the specific language governing permissions and
18    limitations under the License.
19
20  */

21
22 package org.apache.derbyTesting.functionTests.util;
23
24
25 public class Formatters {
26
27     static final char[] hexDigits = { '0', '1', '2', '3',
28                                       '4', '5', '6', '7',
29                                       '8', '9', 'A', 'B',
30                                       'C', 'D', 'E', 'F' };
31
32     /** This method converts the non-ASCII characters in the input
33      * parameter to unicode escape sequences.
34      * @param in String to format
35      * @return String containing unicode escape sequences for non-ASCII chars
36      */

37     public static String JavaDoc format(String JavaDoc in) {
38         if (in == null)
39             return null;
40
41         StringBuffer JavaDoc out = new StringBuffer JavaDoc(in.length());
42         char hexValue[] = new char[4];
43
44         for (int i = 0; i < in.length(); i++) {
45             char inChar = in.charAt(i);
46
47             if (inChar < 128) {
48                 out.append(inChar);
49             } else {
50                 out.append("\\u");
51
52                 int number = (int) inChar;
53
54                 int digit = number % 16;
55
56                 hexValue[3] = hexDigits[digit];
57
58                 number /= 16;
59
60                 digit = number % 16;
61
62                 hexValue[2] = hexDigits[digit];
63
64                 number /= 16;
65
66                 digit = number %16;
67
68                 hexValue[1] = hexDigits[digit];
69
70                 number /= 16;
71
72                 digit = number % 16;
73
74                 hexValue[0] = hexDigits[digit];
75
76                 out.append(hexValue);
77             }
78         }
79
80         return out.toString();
81     }
82
83
84     /**
85      * repeatChar is used to create strings of varying lengths.
86      * called from various tests to test edge cases and such.
87      *
88      * @param c character to repeat
89      * @param repeatCount Number of times to repeat character
90      * @return String of repeatCount characters c
91      */

92    public static String JavaDoc repeatChar(String JavaDoc c, int repeatCount)
93    {
94        char ch = c.charAt(0);
95
96        char[] chArray = new char[repeatCount];
97        for (int i = 0; i < repeatCount; i++)
98        {
99            chArray[i] = ch;
100        }
101
102        return new String JavaDoc(chArray);
103
104    }
105
106     /**
107      * Pads out a string to the specified size
108      *
109      * @param oldValue value to be padded
110      * @param size size of resulting string
111      * @return oldValue padded with spaces to the specified size
112      */

113     public static String JavaDoc padString(String JavaDoc oldValue, int size)
114     {
115         String JavaDoc newValue = oldValue;
116         if (newValue != null)
117         {
118             char [] newCharArr = new char[size];
119             oldValue.getChars(0,oldValue.length(),newCharArr,0);
120             java.util.Arrays.fill(newCharArr,oldValue.length(),
121                                   newCharArr.length -1, ' ');
122             newValue = new String JavaDoc (newCharArr);
123         }
124             
125         return newValue;
126     }
127
128 }
129
Popular Tags