KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > lexer > gen > util > UnicodeRanges


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.modules.lexer.gen.util;
21
22 /**
23  * Program that generates Unicode character ranges array
24  * depending on the method being used.
25  *
26  * @author Miloslav Metelka
27  * @version 1.00
28  */

29
30 public class UnicodeRanges {
31
32     public static final int IS_JAVA_IDENTIFIER_START = 1;
33
34     public static final int IS_JAVA_IDENTIFIER_PART = 2;
35
36     public static char[] findRanges(int testedMethod) {
37         char[] ranges = new char[16]; // will grow very likely
38
int rangesCount = 0;
39         int rangeStart = -1;
40         for (int i = 0; i < 65536; i++) {
41             boolean valid = false;
42             switch (testedMethod) {
43                 case IS_JAVA_IDENTIFIER_START:
44                     valid = Character.isJavaIdentifierStart((char)i);
45                     break;
46                     
47                 case IS_JAVA_IDENTIFIER_PART:
48                     valid = Character.isJavaIdentifierPart((char)i);
49                     break;
50                     
51             }
52             
53             // The following code gets rid of post-handling code after for loop
54
if (i == 65535 && valid) {
55                 if (rangeStart < 0) {
56                     rangeStart = i;
57                 }
58                 i++;
59                 valid = false;
60             }
61                 
62             if (valid) {
63                 if (rangeStart < 0) {
64                     rangeStart = i;
65                 }
66                 
67             } else { // not valid
68
if (rangeStart >= 0) {
69                     // Check sufficient space in ranges array
70
if (ranges.length - rangesCount < 2) {
71                         char[] tmp = new char[ranges.length * 2];
72                         System.arraycopy(ranges, 0, tmp, 0, rangesCount);
73                         ranges = tmp;
74                     }
75                     ranges[rangesCount++] = (char)rangeStart;
76                     ranges[rangesCount++] = (char)(i - 1);
77
78                     rangeStart = -1;
79                 }
80             }
81         }
82         
83         if (rangesCount < ranges.length) {
84             char[] tmp = new char[rangesCount];
85             System.arraycopy(ranges, 0, tmp, 0, rangesCount);
86             ranges = tmp;
87         }
88         return ranges;
89     }
90     
91     public static void appendUnicodeChar(StringBuffer JavaDoc sb, char ch, char quoteChar) {
92         String JavaDoc ret = Integer.toHexString(ch);
93         while (ret.length() < 4) {
94             ret = "0" + ret;
95         }
96         sb.append(quoteChar);
97         sb.append("\\u");
98         sb.append(ret);
99         sb.append(quoteChar);
100     }
101     
102     public static void indent(StringBuffer JavaDoc sb, int indent) {
103         while (indent-- > 0) {
104             sb.append(' ');
105         }
106     }
107     
108     protected static String JavaDoc usage() {
109         return "Prints ranges of characters belonging to selected category\n"
110             + "arg0=Tested method:\n"
111             + " 1 - Character.isJavaIdentifierStart()\n"
112             + " 2 - Character.isJavaIdentifierPart()\n"
113             + "arg1=Indentation e.g. 8\n";
114     }
115     
116 }
117
Popular Tags