/*
* Copyright (c) 2002, 2013, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation. Oracle designates this
* particular file as subject to the "Classpath" exception as provided
* by Oracle in the LICENSE file that accompanied this code.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package org.openjdk.buildtools.generatecharacter;
import java.io.*;
import java.util.*;
import java.lang.*;
/**
* SpecialCaseMap has the responsibility of storing the
* 1:M, locale-sensitive, and context sensitive case mappings
* that occur when uppercasing Unicode 4.0 characters. This class can
* read and parse the SpecialCasing.txt file that contains those mappings.
*
* A single SpecialCaseMap contains the mapping for one character.
*
* @author John O'Conner
*/
public class SpecialCaseMap implements Comparable {
SpecialCaseMap() {
chSource = 0xFFFF;
}
/**
* Read and parse a Unicode special case map file.
*
* @param file a file specifying the Unicode special case mappings
* @return an array of SpecialCaseMap objects, one for each line of the
* special case map data file that could be successfully parsed
*/
public static SpecialCaseMap[] readSpecFile(File file, int plane) throws FileNotFoundException {
ArrayList caseMaps = new ArrayList<>(150);
int count = 0;
BufferedReader f = new BufferedReader(new FileReader(file));
String line = null;
loop:
while(true) {
try {
line = f.readLine();
}
catch (IOException e) { break loop; }
if (line == null) break loop;
SpecialCaseMap item = parse(line.trim());
if (item != null) {
if(item.getCharSource() >> 16 < plane) continue;
if(item.getCharSource() >> 16 > plane) break;
caseMaps.add(item);
++count;
}
}
caseMaps.trimToSize();
SpecialCaseMap[] result = new SpecialCaseMap[caseMaps.size()];
caseMaps.toArray(result);
Arrays.sort(result);
return result;
}
/**
* Given one line of a Unicode special casing data file as a String, parse the line
* and return a SpecialCaseMap object that contains the case mapping.
*
* @param s a line of the Unicode special case map data file to be parsed
* @return a SpecialCaseMap object, or null if the parsing process failed for some reason
*/
public static SpecialCaseMap parse(String s) {
SpecialCaseMap spec = null;
String[] tokens = new String[REQUIRED_FIELDS];
if ( s != null && s.length() != 0 && s.charAt(0) != '#') {
try {
int x = 0, tokenStart = 0, tokenEnd = 0;
for (x=0; x 0xFFFF) {
buff.append(getHighSurrogate(ch));
buff.append(getLowSurrogate(ch));
} else {
buff.append((char)ch);
}
}
char[] map = new char[buff.length()];
buff.getChars(0, buff.length(), map, 0);
return map;
}
static Locale parseLocale(String token) {
return null;
}
static String[] parseContext(String token) {
return null;
}
static int find(int ch, SpecialCaseMap[] map) {
if ((map == null) || (map.length == 0)) {
return -1;
}
int top, bottom, current;
bottom = 0;
top = map.length;
current = top/2;
// invariant: top > current >= bottom && ch >= map.chSource
while (top - bottom > 1) {
if (ch >= map[current].getCharSource()) {
bottom = current;
} else {
top = current;
}
current = (top + bottom) / 2;
}
if (ch == map[current].getCharSource()) return current;
else return -1;
}
/*
* Extracts and returns the high surrogate value from a UTF-32 code point.
* If argument is a BMP character, then it is converted to a char and returned;
* otherwise the high surrogate value is extracted.
* @param codePoint a UTF-32 codePoint with value greater than 0xFFFF.
* @return the high surrogate value that helps create codePoint
; else
* the char representation of codePoint
if it is a BMP character.
* @since 1.5
*/
static char getHighSurrogate(int codePoint) {
char high = (char)codePoint;
if (codePoint > 0xFFFF) {
high = (char)((codePoint - 0x10000)/0x0400 + 0xD800);
}
return high;
}
/*
* Extracts and returns the low surrogate value from a UTF-32 code point.
* If argument is a BMP character, then it is converted to a char and returned;
* otherwise the high surrogate value is extracted.
* @param codePoint a UTF-32 codePoint with value greater than 0xFFFF.
* @return the low surrogate value that helps create codePoint
; else
* the char representation of codePoint
if it is a BMP character.
* @since 1.5
*/
static char getLowSurrogate(int codePoint) {
char low = (char)codePoint;
if(codePoint > 0xFFFF) {
low = (char)((codePoint - 0x10000)%0x0400 + 0xDC00);
}
return low;
}
static String hex6(int n) {
String str = Integer.toHexString(n & 0xFFFFFF).toUpperCase();
return "000000".substring(Math.min(6, str.length())) + str;
}
static String hex6(char[] map){
StringBuffer buff = new StringBuffer();
int x=0;
buff.append(hex6(map[x++]));
while(x otherObject.chSource) {
return 1;
}
else return 0;
}
public boolean equals(Object o1) {
if (this == o1) {
return true;
}
if (o1 == null || !(o1 instanceof SpecialCaseMap)) {
return false;
}
SpecialCaseMap other = (SpecialCaseMap)o1;
boolean bEqual = false;
if (0 == compareTo(other)) {
bEqual = true;
}
return bEqual;
}
public String toString() {
StringBuffer buff = new StringBuffer();
buff.append(hex6(getCharSource()));
buff.append("|" + hex6(lowerCaseMap));
buff.append("|" + hex6(upperCaseMap));
buff.append("|" + hex6(titleCaseMap));
buff.append("|" + context);
return buff.toString();
}
public int hashCode() {
return chSource;
}
public static void main(String[] args) {
SpecialCaseMap[] spec = null;
if (args.length == 2 ) {
try {
File file = new File(args[0]);
int plane = Integer.parseInt(args[1]);
spec = SpecialCaseMap.readSpecFile(file, plane);
System.out.println("SpecialCaseMap[" + spec.length + "]:");
for (int x=0; x