KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > lib > lexer > test > CharRangesDump


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2007 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

19
20 package org.netbeans.lib.lexer.test;
21
22 import java.lang.reflect.Method JavaDoc;
23 import java.util.ArrayList JavaDoc;
24 import java.util.List JavaDoc;
25
26
27 /**
28  * Dump of various character ranges.
29  *
30  * @author Miloslav Metelka
31  */

32 public class CharRangesDump {
33
34     public static void main(String JavaDoc... args) {
35         String JavaDoc methodName = (args[0] != null) ? args[0] : "isWhitespace";
36         new CharRangesDump(new CharacterMethodAcceptor(methodName)).dump();
37     }
38
39     private UnicodeCharAcceptor acceptor;
40
41     private List JavaDoc<Integer JavaDoc> charRanges = new ArrayList JavaDoc<Integer JavaDoc>();
42     
43     public CharRangesDump(UnicodeCharAcceptor acceptor) {
44         if (acceptor == null) {
45             throw new IllegalArgumentException JavaDoc("acceptor must be non-null");
46         }
47         this.acceptor = acceptor;
48         initRanges();
49     }
50     
51     private void initRanges() {
52         int seqStart = -1;
53         for (int i = 0; i <= Character.MAX_CODE_POINT + 1; i++) {
54             if ((i <= Character.MAX_CODE_POINT) && acceptor.isAccepted(i)) {
55                 if (seqStart == -1) {
56                     seqStart = i;
57                 } // sequence already in progress
58
} else { // char not accepted
59
if (seqStart != -1) { // sequence in progress
60
charRanges.add(seqStart);
61                     charRanges.add(i - 1);
62                     seqStart = -1;
63                 }
64             }
65         }
66     }
67     
68     public List JavaDoc<Integer JavaDoc> charRanges() {
69         return charRanges;
70     }
71     
72     public void dump() {
73         StringBuilder JavaDoc sb = new StringBuilder JavaDoc(acceptor.toString());
74         sb.append('\n');
75         for (int i = 0; i < charRanges.size(); i++) {
76             Integer JavaDoc start = charRanges.get(i++);
77             Integer JavaDoc end = charRanges.get(i);
78             if (!start.equals(end)) { // sequence of 2 chars or more
79
sb.append("Sequence: " + charToString(start) + " - "
80                         + charToString(end));
81                 int seqLen = end - start;
82                 if (seqLen >= 2 && seqLen <= 9) {
83                     sb.append(" incl. ");
84                     for (int c = start + 1; c < end; c++) {
85                         if (c != start + 1) {
86                             sb.append(", ");
87                         }
88                         sb.append('\'');
89                         sb.append(LexerTestUtilities.debugText(String.valueOf((char)c)));
90                         sb.append('\'');
91                     }
92                 }
93                 sb.append('\n');
94             } else { // single char only
95
sb.append("Char: " + charToString(start) + '\n');
96             }
97         }
98         
99         System.err.println(sb.toString());
100     }
101     
102     public void dumpAsserts() {
103         StringBuilder JavaDoc sb = new StringBuilder JavaDoc();
104         for (int i = 0; i < charRanges.size(); i++) {
105             Integer JavaDoc start = charRanges.get(i++);
106             Integer JavaDoc end = charRanges.get(i);
107             if (start > 0x7F) { // Only ranges up to 0x7F
108
break;
109             }
110             appendAssert(sb, i - 1, start);
111             appendAssert(sb, i, end);
112         }
113         System.err.println(sb.toString());
114     }
115     
116     private static void appendAssert(StringBuilder JavaDoc sb, int index, Integer JavaDoc value) {
117         sb.append("TestCase.assertEquals(charRanges.get(" + index + ").intValue(), 0x"
118                 + Integer.toString(value.intValue(), 16) + ");\n");
119     }
120     
121     private String JavaDoc charToString(int i) {
122         return "'" + LexerTestUtilities.debugText(String.valueOf((char)i))
123                 + "', " + i + "(0x" + Integer.toString(i, 16) + ")";
124     }
125     
126     public interface UnicodeCharAcceptor {
127
128         boolean isAccepted(int c);
129
130     }
131     
132     public static final class CharacterMethodAcceptor implements UnicodeCharAcceptor {
133         
134         private final Method JavaDoc characterClassMethod;
135         
136         public CharacterMethodAcceptor(String JavaDoc methodName) {
137             try {
138                 characterClassMethod = Character JavaDoc.class.getDeclaredMethod(
139                         methodName, new Class JavaDoc[] { int.class });
140             } catch (Exception JavaDoc e) {
141                 throw new IllegalArgumentException JavaDoc(e);
142             }
143         }
144         
145         public boolean isAccepted(int c) {
146             try {
147                 Object JavaDoc result = characterClassMethod.invoke(null, new Integer JavaDoc(c));
148                 assert (result instanceof Boolean JavaDoc);
149                 return (Boolean.TRUE.equals(result));
150             } catch (Exception JavaDoc e) {
151                 throw new IllegalStateException JavaDoc(e);
152             }
153         }
154         
155         public String JavaDoc toString() {
156             return characterClassMethod.toString();
157         }
158
159     }
160
161 }
162
Popular Tags