mirror of
https://github.com/unicode-org/icu.git
synced 2025-04-08 06:53:45 +00:00
misc fixes
X-SVN-Rev: 14485
This commit is contained in:
parent
d8d1d7863c
commit
62335fd2aa
7 changed files with 410 additions and 134 deletions
|
@ -5,8 +5,8 @@
|
|||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/util/BagFormatter.java,v $
|
||||
* $Date: 2004/02/07 00:59:26 $
|
||||
* $Revision: 1.6 $
|
||||
* $Date: 2004/02/12 00:47:30 $
|
||||
* $Revision: 1.7 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
|
@ -96,15 +96,18 @@ public class BagFormatter {
|
|||
|
||||
UnicodeSet temp = new UnicodeSet(set1).removeAll(set2);
|
||||
pw.println();
|
||||
showSetNames(pw, inOut.format(names), temp);
|
||||
pw.println(inOut.format(names));
|
||||
showSetNames(pw, temp);
|
||||
|
||||
temp = new UnicodeSet(set2).removeAll(set1);
|
||||
pw.println();
|
||||
showSetNames(pw, outIn.format(names), temp);
|
||||
pw.println(outIn.format(names));
|
||||
showSetNames(pw, temp);
|
||||
|
||||
temp = new UnicodeSet(set2).retainAll(set1);
|
||||
pw.println();
|
||||
showSetNames(pw, inIn.format(names), temp);
|
||||
pw.println(inIn.format(names));
|
||||
showSetNames(pw, temp);
|
||||
}
|
||||
|
||||
public void showSetDifferences(
|
||||
|
@ -122,19 +125,22 @@ public class BagFormatter {
|
|||
Collection temp = new HashSet(set1);
|
||||
temp.removeAll(set2);
|
||||
pw.println();
|
||||
showSetNames(pw, inOut.format(names), temp);
|
||||
pw.println(inOut.format(names));
|
||||
showSetNames(pw, temp);
|
||||
|
||||
temp.clear();
|
||||
temp.addAll(set2);
|
||||
temp.removeAll(set1);
|
||||
pw.println();
|
||||
showSetNames(pw, outIn.format(names), temp);
|
||||
pw.println(outIn.format(names));
|
||||
showSetNames(pw, temp);
|
||||
|
||||
temp.clear();
|
||||
temp.addAll(set1);
|
||||
temp.retainAll(set2);
|
||||
pw.println();
|
||||
showSetNames(pw, inIn.format(names), temp);
|
||||
pw.println(inIn.format(names));
|
||||
showSetNames(pw, temp);
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -145,11 +151,10 @@ public class BagFormatter {
|
|||
* @return
|
||||
* @internal
|
||||
*/
|
||||
public String showSetNames(String title, Object c) {
|
||||
public String showSetNames(Object c) {
|
||||
StringWriter buffer = new StringWriter();
|
||||
PrintWriter output = new PrintWriter(buffer);
|
||||
output.println(title);
|
||||
mainVisitor.doAt(c, output);
|
||||
showSetNames(output,c);
|
||||
return buffer.toString();
|
||||
}
|
||||
|
||||
|
@ -161,8 +166,7 @@ public class BagFormatter {
|
|||
* @return
|
||||
* @internal
|
||||
*/
|
||||
public void showSetNames(PrintWriter output, String title, Object c) {
|
||||
output.println(title);
|
||||
public void showSetNames(PrintWriter output, Object c) {
|
||||
mainVisitor.doAt(c, output);
|
||||
output.flush();
|
||||
}
|
||||
|
@ -175,11 +179,11 @@ public class BagFormatter {
|
|||
* @return
|
||||
* @internal
|
||||
*/
|
||||
public void showSetNames(String filename, String title, Object c) throws IOException {
|
||||
public void showSetNames(String filename, Object c) throws IOException {
|
||||
PrintWriter pw = new PrintWriter(
|
||||
new OutputStreamWriter(
|
||||
new FileOutputStream(filename),"utf-8"));
|
||||
showSetNames(log,title,c);
|
||||
showSetNames(log,c);
|
||||
pw.close();
|
||||
}
|
||||
|
||||
|
@ -278,6 +282,7 @@ public class BagFormatter {
|
|||
String result = getName(start, false);
|
||||
if (start == end) return separator + result;
|
||||
String endString = getName(end, false);
|
||||
if (result.length() == 0 && endString.length() == 0) return separator;
|
||||
if (abbreviated) endString = getAbbreviatedName(endString,result,"~");
|
||||
return separator + result + ".." + endString;
|
||||
}
|
||||
|
@ -288,17 +293,19 @@ public class BagFormatter {
|
|||
|
||||
UnicodeLabel nameSource;
|
||||
|
||||
static class NameLabel extends UnicodeLabel {
|
||||
class NameLabel extends UnicodeLabel {
|
||||
UnicodeProperty nameProp;
|
||||
UnicodeProperty name1Prop;
|
||||
UnicodeProperty catProp;
|
||||
//UnicodeProperty shortCatProp;
|
||||
UnicodeSet control;
|
||||
UnicodeSet private_use;
|
||||
UnicodeSet noncharacter;
|
||||
UnicodeSet surrogate;
|
||||
|
||||
NameLabel(UnicodeProperty.Factory source) {
|
||||
nameProp = source.getProperty("Name");
|
||||
name1Prop = source.getProperty("Unicode_1_Name");
|
||||
catProp = source.getProperty("General_Category");
|
||||
//shortCatProp = source.getProperty("General_Category");
|
||||
control = source.getSet("gc=Cc");
|
||||
private_use = source.getSet("gc=Co");
|
||||
surrogate = source.getSet("gc=Cs");
|
||||
noncharacter = source.getSet("noncharactercodepoint=true");
|
||||
}
|
||||
|
||||
public String getValue(int codePoint, boolean isShort) {
|
||||
|
@ -308,13 +315,12 @@ public class BagFormatter {
|
|||
String result = nameProp.getValue(codePoint);
|
||||
if (result != null)
|
||||
return hcp + result;
|
||||
String prop = catProp.getValue(codePoint, true);
|
||||
if (prop.equals("Control")) {
|
||||
result = name1Prop.getValue(codePoint);
|
||||
if (result != null)
|
||||
return hcp + "<" + result + ">";
|
||||
}
|
||||
return hcp + "<reserved>";
|
||||
if (control.contains(codePoint)) return "<control-" + Utility.hex(codePoint, 4) + ">";
|
||||
if (private_use.contains(codePoint)) return "<private use-" + Utility.hex(codePoint, 4) + ">";
|
||||
if (noncharacter.contains(codePoint)) return "<noncharacter-" + Utility.hex(codePoint, 4) + ">";
|
||||
if (surrogate.contains(codePoint)) return "<surrogate-" + Utility.hex(codePoint, 4) + ">";
|
||||
if (suppressReserved) return "";
|
||||
return hcp + "<reserved-" + Utility.hex(codePoint, 4) + ">";
|
||||
}
|
||||
|
||||
}
|
||||
|
@ -341,24 +347,31 @@ public class BagFormatter {
|
|||
private String separator = ",";
|
||||
private String prefix = "[";
|
||||
private String suffix = "]";
|
||||
UnicodeProperty.Factory source;
|
||||
UnicodeLabel labelSource = UnicodeLabel.NULL;
|
||||
UnicodeLabel valueSource = UnicodeLabel.NULL;
|
||||
private UnicodeProperty.Factory source;
|
||||
private UnicodeLabel labelSource;
|
||||
private UnicodeLabel valueSource = UnicodeLabel.NULL;
|
||||
private boolean showCount = true;
|
||||
private boolean suppressReserved = true;
|
||||
|
||||
public BagFormatter setUnicodePropertySource(UnicodeProperty.Factory source) {
|
||||
this.source = source;
|
||||
nameSource = new NameLabel(source);
|
||||
return this;
|
||||
}
|
||||
|
||||
public BagFormatter () {
|
||||
this(null);
|
||||
}
|
||||
|
||||
{
|
||||
setUnicodePropertySource(ICUPropertyFactory.make());
|
||||
public BagFormatter (UnicodeProperty.Factory source) {
|
||||
if (source == null) source = ICUPropertyFactory.make();
|
||||
setUnicodePropertySource(source);
|
||||
Map labelMap = new HashMap();
|
||||
labelMap.put("Lo","L&");
|
||||
//labelMap.put("Lo","L&");
|
||||
labelMap.put("Lu","L&");
|
||||
labelMap.put("Lt","L&");
|
||||
setLabelSource(new UnicodeProperty.FilteredUnicodeProperty(
|
||||
labelMap.put("Ll","L&");
|
||||
setLabelSource(new UnicodeProperty.FilteredProperty(
|
||||
source.getProperty("General_Category"),
|
||||
new UnicodeProperty.MapFilter(labelMap)));
|
||||
}
|
||||
|
@ -418,13 +431,16 @@ public class BagFormatter {
|
|||
|
||||
private static NumberFormat nf =
|
||||
NumberFormat.getIntegerInstance(Locale.ENGLISH);
|
||||
static {
|
||||
nf.setGroupingUsed(false);
|
||||
}
|
||||
|
||||
private String lineSeparator = "\r\n";
|
||||
|
||||
private class MyVisitor extends Visitor {
|
||||
private PrintWriter output;
|
||||
Tabber.MonoTabber myTabber;
|
||||
String commentSeparator = "\t# ";
|
||||
String commentSeparator;
|
||||
|
||||
public void doAt(Object c, PrintWriter output) {
|
||||
this.output = output;
|
||||
|
@ -432,16 +448,18 @@ public class BagFormatter {
|
|||
int valueSize = valueSource.getMaxWidth(shortValue);
|
||||
if (valueSize > 0) valueSize += 2;
|
||||
if (!mergeRanges) {
|
||||
myTabber.add(0,Tabber.LEFT);
|
||||
myTabber.add(6 + valueSize,Tabber.LEFT);
|
||||
myTabber.add(6,Tabber.LEFT); // code
|
||||
if (valueSource != UnicodeProperty.NULL) myTabber.add(2 + valueSize,Tabber.LEFT); // value
|
||||
myTabber.add(2 + labelSource.getMaxWidth(shortLabel),Tabber.LEFT);
|
||||
myTabber.add(4,Tabber.LEFT);
|
||||
if (showLiteral != null) myTabber.add(4,Tabber.LEFT);
|
||||
//myTabber.add(4,Tabber.LEFT);
|
||||
} else {
|
||||
myTabber.add(0,Tabber.LEFT);
|
||||
myTabber.add(15 + valueSize,Tabber.LEFT);
|
||||
myTabber.add(13,Tabber.LEFT);
|
||||
if (valueSource != UnicodeProperty.NULL) myTabber.add(2 + valueSize,Tabber.LEFT); // value
|
||||
myTabber.add(2 + labelSource.getMaxWidth(shortLabel),Tabber.LEFT);
|
||||
myTabber.add(11,Tabber.LEFT);
|
||||
myTabber.add(7,Tabber.LEFT);
|
||||
if (showCount) myTabber.add(8,Tabber.RIGHT);
|
||||
if (showLiteral != null) myTabber.add(4,Tabber.LEFT);
|
||||
//myTabber.add(7,Tabber.LEFT);
|
||||
}
|
||||
commentSeparator = (showCount || showLiteral != null
|
||||
|| labelSource != UnicodeProperty.NULL || nameSource != UnicodeProperty.NULL)
|
||||
|
@ -469,7 +487,7 @@ public class BagFormatter {
|
|||
}
|
||||
|
||||
protected void doAfter(Object container, Object o) {
|
||||
output.print("# Total: " + nf.format(count(container)) + lineSeparator);
|
||||
output.print(lineSeparator + "# Total code points: " + nf.format(count(container)) + lineSeparator);
|
||||
}
|
||||
|
||||
protected void doSimpleAt(Object o) {
|
||||
|
@ -501,11 +519,9 @@ public class BagFormatter {
|
|||
if (!mergeRanges) {
|
||||
for (int cp = usi.codepoint; cp <= usi.codepointEnd; ++cp) {
|
||||
String label = labelSource.getValue(cp, shortLabel);
|
||||
if (label.length() != 0)
|
||||
label += " ";
|
||||
String value = valueSource.getValue(cp, shortValue);
|
||||
if (value.length() != 0) {
|
||||
value = "; " + value;
|
||||
value = "\t; " + value;
|
||||
}
|
||||
output.print(
|
||||
myTabber.process(
|
||||
|
@ -528,13 +544,13 @@ public class BagFormatter {
|
|||
int start = rf.start;
|
||||
int end = rf.limit - 1;
|
||||
String label = rf.label;
|
||||
if (label.length() != 0)
|
||||
label += " ";
|
||||
String value = rf.value;
|
||||
if (value.length() != 0) {
|
||||
value = "; " + value;
|
||||
value = "\t; " + value;
|
||||
}
|
||||
String count = showCount ? "\t["+ nf.format(end - start + 1)+ "]" : "";
|
||||
String count = !showCount ? ""
|
||||
: end == start ? "\t"
|
||||
: "\t["+ nf.format(end - start + 1)+ "]";
|
||||
output.print(
|
||||
myTabber.process(
|
||||
hex(start, end)
|
||||
|
@ -833,6 +849,10 @@ public class BagFormatter {
|
|||
return this;
|
||||
}
|
||||
|
||||
public BagFormatter setValueSource(String label) {
|
||||
return setValueSource(new UnicodeLabel.Constant(label));
|
||||
}
|
||||
|
||||
/**
|
||||
* @return
|
||||
*/
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/util/ICUPropertyFactory.java,v $
|
||||
* $Date: 2004/02/07 00:59:26 $
|
||||
* $Revision: 1.1 $
|
||||
* $Date: 2004/02/12 00:47:30 $
|
||||
* $Revision: 1.2 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
|
@ -34,6 +34,7 @@ import com.ibm.icu.text.Normalizer;
|
|||
import com.ibm.icu.text.UTF16;
|
||||
import com.ibm.icu.text.UnicodeSet;
|
||||
import com.ibm.icu.text.UnicodeSetIterator;
|
||||
import com.ibm.icu.util.VersionInfo;
|
||||
|
||||
|
||||
/**
|
||||
|
@ -68,7 +69,7 @@ public class ICUPropertyFactory extends UnicodeProperty.Factory {
|
|||
|
||||
boolean shownException = false;
|
||||
|
||||
public String getValue(int codePoint) {
|
||||
public String _getValue(int codePoint) {
|
||||
if (propEnum < UProperty.INT_LIMIT) {
|
||||
int enumValue = -1;
|
||||
String value = null;
|
||||
|
@ -121,7 +122,7 @@ public class ICUPropertyFactory extends UnicodeProperty.Factory {
|
|||
return null;
|
||||
}
|
||||
|
||||
public Collection getAvailableValueAliases(Collection result) {
|
||||
public Collection _getAvailableValueAliases(Collection result) {
|
||||
if (result == null) result = new ArrayList();
|
||||
if (propEnum < UProperty.INT_LIMIT) {
|
||||
if (Binary_Extras.isInRange(propEnum)) {
|
||||
|
@ -183,7 +184,7 @@ public class ICUPropertyFactory extends UnicodeProperty.Factory {
|
|||
}
|
||||
}
|
||||
|
||||
public Collection getAliases(Collection result) {
|
||||
public Collection _getAliases(Collection result) {
|
||||
if (result == null) result = new ArrayList();
|
||||
String alias = String_Extras.get(propEnum);
|
||||
if (alias == null) alias = Binary_Extras.get(propEnum);
|
||||
|
@ -206,7 +207,7 @@ public class ICUPropertyFactory extends UnicodeProperty.Factory {
|
|||
return result;
|
||||
}
|
||||
|
||||
public Collection getValueAliases(String valueAlias, Collection result) {
|
||||
public Collection _getValueAliases(String valueAlias, Collection result) {
|
||||
if (result == null) result = new ArrayList();
|
||||
for (int nameChoice = 0; ; ++nameChoice) {
|
||||
String alias = getFixedValueAlias(valueAlias, -1, nameChoice);
|
||||
|
@ -248,6 +249,13 @@ public class ICUPropertyFactory extends UnicodeProperty.Factory {
|
|||
if (propEnum < UProperty.STRING_LIMIT) return UnicodeProperty.STRING;
|
||||
return UnicodeProperty.EXTENDED_STRING;
|
||||
}
|
||||
|
||||
/* (non-Javadoc)
|
||||
* @see com.ibm.icu.dev.test.util.UnicodeProperty#getVersion()
|
||||
*/
|
||||
public String _getVersion() {
|
||||
return VersionInfo.ICU_VERSION.toString();
|
||||
}
|
||||
}
|
||||
|
||||
/*{
|
||||
|
|
|
@ -6,8 +6,8 @@
|
|||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/util/Tabber.java,v $
|
||||
* $Date: 2004/02/07 00:59:26 $
|
||||
* $Revision: 1.3 $
|
||||
* $Date: 2004/02/12 00:47:30 $
|
||||
* $Revision: 1.4 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
|
@ -58,11 +58,21 @@ public abstract class Tabber {
|
|||
private List stops = new ArrayList();
|
||||
private List types = new ArrayList();
|
||||
|
||||
/**
|
||||
* Adds tab stop and how to align the text UP TO that stop
|
||||
* @param tabPos
|
||||
* @param type
|
||||
*/
|
||||
public void addAbsolute(int tabPos, int type) {
|
||||
stops.add(new Integer(tabPos));
|
||||
types.add(new Integer(type));
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds relative tab stop and how to align the text UP TO that stop
|
||||
* @param tabPos
|
||||
* @param type
|
||||
*/
|
||||
public void add(int fieldWidth, byte type) {
|
||||
int last = getStop(stops.size()-1);
|
||||
stops.add(new Integer(last + fieldWidth));
|
||||
|
@ -71,8 +81,14 @@ public abstract class Tabber {
|
|||
|
||||
public int getStop(int fieldNumber) {
|
||||
if (fieldNumber < 0) return 0;
|
||||
if (fieldNumber >= stops.size()) fieldNumber = stops.size() - 1;
|
||||
return ((Integer)stops.get(fieldNumber)).intValue();
|
||||
}
|
||||
public int getType(int fieldNumber) {
|
||||
if (fieldNumber < 0) return LEFT;
|
||||
if (fieldNumber >= stops.size()) return LEFT;
|
||||
return ((Integer)types.get(fieldNumber)).intValue();
|
||||
}
|
||||
/*
|
||||
public String process(String source) {
|
||||
StringBuffer result = new StringBuffer();
|
||||
|
@ -99,12 +115,24 @@ public abstract class Tabber {
|
|||
|
||||
public void process_field(int count, String source, int start, int limit, StringBuffer output) {
|
||||
String piece = source.substring(start, limit);
|
||||
int pos = getStop(count);
|
||||
if (output.length() < pos) {
|
||||
output.append(repeat(" ", pos - output.length()));
|
||||
// TODO fix type
|
||||
} else {
|
||||
output.append(" ");
|
||||
int startPos = getStop(count-1);
|
||||
int endPos = getStop(count) - 1;
|
||||
int type = getType(count);
|
||||
switch (type) {
|
||||
case LEFT:
|
||||
break;
|
||||
case RIGHT:
|
||||
startPos = endPos - piece.length();
|
||||
break;
|
||||
case CENTER:
|
||||
startPos = (startPos + endPos - piece.length() + 1)/2;
|
||||
break;
|
||||
}
|
||||
|
||||
if (output.length() < startPos) {
|
||||
output.append(repeat(" ", startPos - output.length()));
|
||||
} else if (startPos != 0) { // don't do anything on first instance
|
||||
output.append(" "); // otherwise minimum of first space
|
||||
}
|
||||
output.append(piece);
|
||||
}
|
||||
|
|
|
@ -5,8 +5,8 @@
|
|||
*******************************************************************************
|
||||
*
|
||||
* $Source: /xsrl/Nsvn/icu/icu4j/src/com/ibm/icu/dev/test/util/TestBagFormatter.java,v $
|
||||
* $Date: 2004/02/07 00:59:25 $
|
||||
* $Revision: 1.7 $
|
||||
* $Date: 2004/02/12 00:47:30 $
|
||||
* $Revision: 1.8 $
|
||||
*
|
||||
*****************************************************************************************
|
||||
*/
|
||||
|
@ -67,14 +67,17 @@ public class TestBagFormatter {
|
|||
|
||||
BagFormatter bf = new BagFormatter();
|
||||
|
||||
UnicodeSet us = new UnicodeSet("[:numeric_value=2:]");
|
||||
bf.showSetNames(BagFormatter.CONSOLE,"[:numeric_value=2:]", us);
|
||||
UnicodeSet us = new UnicodeSet("[:numeric_value=2:]");
|
||||
BagFormatter.CONSOLE.println("[:numeric_value=2:]");
|
||||
bf.showSetNames(BagFormatter.CONSOLE,us);
|
||||
us = new UnicodeSet("[:numeric_type=numeric:]");
|
||||
bf.showSetNames(BagFormatter.CONSOLE,"[:numeric_type=numeric:]", us);
|
||||
BagFormatter.CONSOLE.println("[:numeric_type=numeric:]");
|
||||
bf.showSetNames(BagFormatter.CONSOLE,us);
|
||||
|
||||
UnicodeProperty.Factory ups = ICUPropertyFactory.make();
|
||||
us = ups.getSet("gc=mn", null, null);
|
||||
bf.showSetNames(bf.CONSOLE,"gc=mn", us);
|
||||
BagFormatter.CONSOLE.println("gc=mn");
|
||||
bf.showSetNames(bf.CONSOLE, us);
|
||||
|
||||
if (true) return;
|
||||
//showNames("Name", ".*MARK.*");
|
||||
|
|
|
@ -27,11 +27,18 @@ public abstract class UnicodeLabel {
|
|||
return "U+" + Utility.hex(codepoint,4);
|
||||
}
|
||||
}
|
||||
private static class Null extends UnicodeLabel {
|
||||
public static class Constant extends UnicodeLabel {
|
||||
private String value;
|
||||
public Constant(String value) {
|
||||
this.value = value;
|
||||
}
|
||||
public String getValue(int codepoint, boolean isShort) {
|
||||
return "";
|
||||
}
|
||||
return value;
|
||||
}
|
||||
public int getMaxWidth(boolean isShort) {
|
||||
return value.length();
|
||||
}
|
||||
}
|
||||
public static final UnicodeLabel NULL = new Null();
|
||||
public static final UnicodeLabel NULL = new Constant("");
|
||||
public static final UnicodeLabel HEX = new Hex();
|
||||
}
|
|
@ -58,8 +58,8 @@ public class UnicodeMap {
|
|||
UnicodeSet set = (UnicodeSet) objectToSet.get(it.next());
|
||||
set.removeAll(codepoints);
|
||||
}
|
||||
missing.removeAll(codepoints);
|
||||
}
|
||||
missing.removeAll(codepoints);
|
||||
UnicodeSet set = (UnicodeSet) objectToSet.get(value);
|
||||
if (set == null) {
|
||||
set = new UnicodeSet();
|
||||
|
@ -68,6 +68,42 @@ public class UnicodeMap {
|
|||
set.addAll(codepoints);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds bunch o' codepoints; otherwise like add.
|
||||
* @param codepoints
|
||||
* @param value
|
||||
* @return this, for chaining
|
||||
*/
|
||||
public UnicodeMap putAll(int startCodePoint, int endCodePoint, Object value) {
|
||||
// TODO optimize
|
||||
return putAll(new UnicodeSet(startCodePoint, endCodePoint), value);
|
||||
}
|
||||
/**
|
||||
* Add all the (main) values from a Unicode property
|
||||
* @param prop
|
||||
* @return
|
||||
*/
|
||||
public UnicodeMap putAll(UnicodeProperty prop) {
|
||||
UnicodeSet temp = new UnicodeSet();
|
||||
Iterator it = prop.getAliases().iterator();
|
||||
while(it.hasNext()) {
|
||||
String value = (String) it.next();
|
||||
temp.clear();
|
||||
putAll(prop.getSet(value,temp), value);
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the currently unmapped Unicode code points to the given value.
|
||||
* @param value
|
||||
* @return
|
||||
*/public UnicodeMap setMissing(Object value) {
|
||||
objectToSet.put(value,missing);
|
||||
missing = new UnicodeSet();
|
||||
return this;
|
||||
}
|
||||
/**
|
||||
* Returns the set associated with a given value. Deposits into
|
||||
* result if it is not null. Remember to clear if you just want
|
||||
|
@ -109,4 +145,18 @@ public class UnicodeMap {
|
|||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
public String toString() {
|
||||
StringBuffer result = new StringBuffer();
|
||||
Iterator it = objectToSet.keySet().iterator();
|
||||
while (it.hasNext()) {
|
||||
Object value = it.next();
|
||||
UnicodeSet set = (UnicodeSet) objectToSet.get(value);
|
||||
result.append(value)
|
||||
.append("=>")
|
||||
.append(set.toPattern(true))
|
||||
.append("\r\n");
|
||||
}
|
||||
return result.toString();
|
||||
}
|
||||
}
|
|
@ -1,5 +1,7 @@
|
|||
package com.ibm.icu.dev.test.util;
|
||||
|
||||
import java.io.PrintWriter;
|
||||
import java.io.StringWriter;
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.Collection;
|
||||
|
@ -18,9 +20,12 @@ import com.ibm.icu.text.UnicodeSetIterator;
|
|||
|
||||
public abstract class UnicodeProperty extends UnicodeLabel {
|
||||
|
||||
public static boolean DEBUG = false;
|
||||
|
||||
private String propertyAlias;
|
||||
private String shortestPropertyAlias = null;
|
||||
private int type;
|
||||
private Map mapToShortName = null;
|
||||
private Map valueToShortValue = null;
|
||||
|
||||
public static final int UNKNOWN = 0,
|
||||
BINARY = 2, EXTENDED_BINARY = 3,
|
||||
|
@ -63,14 +68,48 @@ public abstract class UnicodeProperty extends UnicodeLabel {
|
|||
type = i;
|
||||
}
|
||||
|
||||
public abstract String getValue(int codepoint);
|
||||
public abstract Collection getAliases(Collection result);
|
||||
public abstract Collection getValueAliases(String valueAlias, Collection result);
|
||||
abstract public Collection getAvailableValueAliases(Collection result);
|
||||
public String getVersion() {
|
||||
return _getVersion();
|
||||
}
|
||||
public String getValue(int codepoint) {
|
||||
return _getValue(codepoint);
|
||||
}
|
||||
public Collection getAliases(Collection result) {
|
||||
return _getAliases(result);
|
||||
}
|
||||
public Collection getValueAliases(String valueAlias, Collection result) {
|
||||
result = _getValueAliases(valueAlias, result);
|
||||
if (!result.contains(valueAlias) && type < NUMERIC) {
|
||||
throw new IllegalArgumentException(
|
||||
"Internal error: result doesn't contain " + valueAlias);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public Collection getAvailableValueAliases(Collection result) {
|
||||
return _getAvailableValueAliases(result);
|
||||
}
|
||||
|
||||
protected abstract String _getVersion();
|
||||
protected abstract String _getValue(int codepoint);
|
||||
protected abstract Collection _getAliases(Collection result);
|
||||
protected abstract Collection _getValueAliases(String valueAlias, Collection result);
|
||||
protected abstract Collection _getAvailableValueAliases(Collection result);
|
||||
|
||||
// conveniences
|
||||
public final Collection getAliases() {
|
||||
return _getAliases(null);
|
||||
}
|
||||
public final Collection getValueAliases(String valueAlias) {
|
||||
return _getValueAliases(valueAlias, null);
|
||||
}
|
||||
public final Collection getAvailableValueAliases() {
|
||||
return _getAvailableValueAliases(null);
|
||||
}
|
||||
|
||||
static public class Factory {
|
||||
Map canonicalNames = new TreeMap();
|
||||
Map skeletonNames = new TreeMap();
|
||||
Map propertyCache = new HashMap();
|
||||
|
||||
public final Factory add(UnicodeProperty sp) {
|
||||
canonicalNames.put(sp.getName(), sp);
|
||||
|
@ -107,6 +146,7 @@ public abstract class UnicodeProperty extends UnicodeLabel {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
InverseMatcher inverseMatcher = new InverseMatcher();
|
||||
/**
|
||||
* Format is:
|
||||
* propname ('=' | '!=') propvalue ( '|' propValue )*
|
||||
|
@ -114,18 +154,25 @@ public abstract class UnicodeProperty extends UnicodeLabel {
|
|||
public final UnicodeSet getSet(String propAndValue, Matcher matcher, UnicodeSet result) {
|
||||
int equalPos = propAndValue.indexOf('=');
|
||||
String prop = propAndValue.substring(0,equalPos);
|
||||
String value = propAndValue.substring(equalPos+1);
|
||||
boolean negative = false;
|
||||
if (prop.endsWith("!")) {
|
||||
prop = prop.substring(0,prop.length()-1);
|
||||
negative = true;
|
||||
}
|
||||
prop = prop.trim();
|
||||
String value = propAndValue.substring(equalPos+1);
|
||||
UnicodeProperty up = getProperty(prop);
|
||||
if (matcher != null) {
|
||||
return up.getSet(matcher.set(value), result);
|
||||
if (matcher == null) {
|
||||
matcher = new SimpleMatcher(value,
|
||||
up.getType() >= STRING ? null : new SkeletonComparator());
|
||||
}
|
||||
return up.getSet(value,result);
|
||||
if (negative) {
|
||||
inverseMatcher.set(matcher);
|
||||
matcher = inverseMatcher;
|
||||
}
|
||||
return up.getSet(matcher.set(value), result);
|
||||
}
|
||||
|
||||
public final UnicodeSet getSet(String propAndValue, Matcher matcher) {
|
||||
return getSet(propAndValue, matcher, null);
|
||||
}
|
||||
|
@ -134,12 +181,13 @@ public abstract class UnicodeProperty extends UnicodeLabel {
|
|||
}
|
||||
}
|
||||
|
||||
static class FilteredUnicodeProperty extends UnicodeProperty {
|
||||
UnicodeProperty property;
|
||||
public static class FilteredProperty extends UnicodeProperty {
|
||||
private UnicodeProperty property;
|
||||
protected StringFilter filter;
|
||||
protected UnicodeSetIterator matchIterator = new UnicodeSetIterator(new UnicodeSet(0,0x10FFFF));
|
||||
|
||||
FilteredUnicodeProperty(UnicodeProperty property, StringFilter filter) {
|
||||
protected HashMap backmap;
|
||||
|
||||
public FilteredProperty(UnicodeProperty property, StringFilter filter) {
|
||||
this.property = property;
|
||||
this.filter = filter;
|
||||
}
|
||||
|
@ -153,37 +201,68 @@ public abstract class UnicodeProperty extends UnicodeLabel {
|
|||
return this;
|
||||
}
|
||||
|
||||
public Collection getAvailableValueAliases(Collection result) {
|
||||
return property.getAvailableValueAliases(result);
|
||||
Collection temp = new ArrayList();
|
||||
|
||||
public Collection _getAvailableValueAliases(Collection result) {
|
||||
temp.clear();
|
||||
return filter.addUnique(property.getAvailableValueAliases(temp), result);
|
||||
}
|
||||
|
||||
public Collection getAliases(Collection result) {
|
||||
return property.getAliases(result);
|
||||
public Collection _getAliases(Collection result) {
|
||||
temp.clear();
|
||||
return filter.addUnique(
|
||||
property.getAliases(temp), result);
|
||||
}
|
||||
|
||||
public String getValue(int codepoint) {
|
||||
public String _getValue(int codepoint) {
|
||||
return filter.remap(property.getValue(codepoint));
|
||||
}
|
||||
|
||||
public Collection getValueAliases(
|
||||
String valueAlias,
|
||||
Collection result) {
|
||||
return property.getValueAliases(valueAlias, result);
|
||||
public Collection _getValueAliases(String valueAlias, Collection result) {
|
||||
temp.clear();
|
||||
if (backmap == null) {
|
||||
backmap = new HashMap();
|
||||
temp.clear();
|
||||
Iterator it = property.getAvailableValueAliases(temp).iterator();
|
||||
while (it.hasNext()) {
|
||||
String item = (String) it.next();
|
||||
String mappedItem = filter.remap(item);
|
||||
if (backmap.get(mappedItem) != null) {
|
||||
throw new IllegalArgumentException("Filter makes values collide!");
|
||||
}
|
||||
backmap.put(mappedItem, item);
|
||||
}
|
||||
}
|
||||
return filter.addUnique(
|
||||
property.getValueAliases((String) backmap.get(valueAlias), temp), result);
|
||||
}
|
||||
|
||||
public String _getVersion() {
|
||||
return property.getVersion();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public static class StringFilter implements Cloneable {
|
||||
public String remap(String original) {
|
||||
return original;
|
||||
public static abstract class StringFilter implements Cloneable {
|
||||
public abstract String remap(String original);
|
||||
public final Collection addUnique(Collection source, Collection result) {
|
||||
if (result == null) result = new ArrayList();
|
||||
Iterator it = source.iterator();
|
||||
while (it.hasNext()) {
|
||||
UnicodeProperty.addUnique(
|
||||
remap((String) it.next()), result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
public Object clone() {
|
||||
/*
|
||||
public Object clone() {
|
||||
try {
|
||||
return super.clone();
|
||||
} catch (CloneNotSupportedException e) {
|
||||
throw new InternalError("Should never happen.");
|
||||
}
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
public static class MapFilter extends StringFilter {
|
||||
|
@ -210,6 +289,21 @@ public abstract class UnicodeProperty extends UnicodeLabel {
|
|||
public Matcher set(String pattern);
|
||||
}
|
||||
|
||||
public static class InverseMatcher implements Matcher {
|
||||
Matcher other;
|
||||
public Matcher set(Matcher toInverse) {
|
||||
other = toInverse;
|
||||
return this;
|
||||
}
|
||||
public boolean matches(String value) {
|
||||
return !other.matches(value);
|
||||
}
|
||||
public Matcher set(String pattern) {
|
||||
other.set(pattern);
|
||||
return this;
|
||||
}
|
||||
}
|
||||
|
||||
public static class SimpleMatcher implements Matcher {
|
||||
Comparator comparator;
|
||||
String pattern;
|
||||
|
@ -231,16 +325,23 @@ public abstract class UnicodeProperty extends UnicodeLabel {
|
|||
private String shortAlias;
|
||||
Collection valueAliases = new ArrayList();
|
||||
Map toAlternates = new HashMap();
|
||||
String version;
|
||||
|
||||
protected void set(String alias, String shortAlias, int propertyType, String valueAlias) {
|
||||
set(alias,shortAlias,propertyType,new String[]{valueAlias},null);
|
||||
}
|
||||
|
||||
protected void set(String alias, String shortAlias, int propertyType,
|
||||
String[] valueAliases, String[] alternateValueAliases) {
|
||||
public SimpleProperty setMain(String alias, String shortAlias, int propertyType,
|
||||
String version) {
|
||||
setName(alias);
|
||||
setType(propertyType);
|
||||
this.shortAlias = shortAlias;
|
||||
this.version = version;
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleProperty setValues(String valueAlias) {
|
||||
setValues(new String[]{valueAlias}, null);
|
||||
return this;
|
||||
}
|
||||
|
||||
public SimpleProperty setValues(String[] valueAliases, String[] alternateValueAliases) {
|
||||
this.valueAliases = Arrays.asList((Object[]) valueAliases.clone());
|
||||
|
||||
for (int i = 0; i < valueAliases.length; ++i) {
|
||||
|
@ -249,48 +350,74 @@ public abstract class UnicodeProperty extends UnicodeLabel {
|
|||
if (alternateValueAliases != null) addUnique(alternateValueAliases[i],a);
|
||||
toAlternates.put(valueAliases[i], a);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
protected void set(String alias, String shortAlias, int propertyType,
|
||||
Collection valueAliases) {
|
||||
setName(alias);
|
||||
setType(propertyType);
|
||||
this.shortAlias = shortAlias;
|
||||
this.valueAliases = new ArrayList(valueAliases);
|
||||
public SimpleProperty setValues(Collection valueAliases) {
|
||||
this.valueAliases = new ArrayList(valueAliases);
|
||||
for (Iterator it = this.valueAliases.iterator(); it.hasNext(); ) {
|
||||
Object item = it.next();
|
||||
List list = new ArrayList();
|
||||
list.add(item);
|
||||
toAlternates.put(item, list);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
public Collection getAliases(Collection result) {
|
||||
public Collection _getAliases(Collection result) {
|
||||
if (result == null) result = new ArrayList();
|
||||
addUnique(getName(), result);
|
||||
addUnique(shortAlias, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Collection getValueAliases(String valueAlias, Collection result) {
|
||||
public Collection _getValueAliases(String valueAlias, Collection result) {
|
||||
if (result == null) result = new ArrayList();
|
||||
Collection a = (Collection) toAlternates.get(valueAlias);
|
||||
if (a != null) result.addAll(valueAliases);
|
||||
if (a != null) addAllUnique(a, result);
|
||||
return result;
|
||||
}
|
||||
|
||||
public Collection getAvailableValueAliases(Collection result) {
|
||||
public Collection _getAvailableValueAliases(Collection result) {
|
||||
if (result == null) result = new ArrayList();
|
||||
result.addAll(valueAliases);
|
||||
return result;
|
||||
}
|
||||
|
||||
public String _getVersion() {
|
||||
return version;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
public final String getValue(int codepoint, boolean getShortest) {
|
||||
String result = getValue(codepoint);
|
||||
if (!getShortest || result == null) return result;
|
||||
if (mapToShortName == null) getValueCache();
|
||||
return (String)mapToShortName.get(result);
|
||||
return getShortestValueAlias(result);
|
||||
}
|
||||
|
||||
public final String getShortestValueAlias(String value) {
|
||||
if (valueToShortValue == null) getValueCache();
|
||||
return (String)valueToShortValue.get(value);
|
||||
}
|
||||
|
||||
public final String getShortestAlias() {
|
||||
if (shortestPropertyAlias == null) {
|
||||
shortestPropertyAlias = propertyAlias;
|
||||
for (Iterator it = _getAliases(null).iterator(); it.hasNext();) {
|
||||
String item = (String) it.next();
|
||||
if (item.length() < shortestPropertyAlias.length()) {
|
||||
shortestPropertyAlias = item;
|
||||
}
|
||||
}
|
||||
}
|
||||
return shortestPropertyAlias;
|
||||
}
|
||||
|
||||
private void getValueCache() {
|
||||
maxWidth = 0;
|
||||
mapToShortName = new HashMap();
|
||||
maxValueWidth = 0;
|
||||
maxShortestValueWidth = 0;
|
||||
valueToShortValue = new HashMap();
|
||||
Iterator it = getAvailableValueAliases(null).iterator();
|
||||
while (it.hasNext()) {
|
||||
String value = (String)it.next();
|
||||
|
@ -300,18 +427,28 @@ public abstract class UnicodeProperty extends UnicodeLabel {
|
|||
String other = (String)it2.next();
|
||||
if (shortest.length() > other.length()) shortest = other;
|
||||
}
|
||||
mapToShortName.put(value,shortest);
|
||||
if (shortest.length() > maxWidth) maxWidth = shortest.length();
|
||||
valueToShortValue.put(value,shortest);
|
||||
if (value.length() > maxValueWidth) maxValueWidth = value.length();
|
||||
if (shortest.length() > maxShortestValueWidth) maxShortestValueWidth = shortest.length();
|
||||
}
|
||||
}
|
||||
|
||||
private int maxWidth = -1;
|
||||
private int maxValueWidth = -1;
|
||||
private int maxShortestValueWidth = -1;
|
||||
|
||||
public final int getMaxWidth(boolean getShortest) {
|
||||
if (maxWidth < 0) getValueCache();
|
||||
return maxWidth;
|
||||
if (maxValueWidth < 0) getValueCache();
|
||||
if (getShortest) return maxShortestValueWidth;
|
||||
return maxValueWidth;
|
||||
}
|
||||
|
||||
public final UnicodeSet getSet(String propertyValue) {
|
||||
return getSet(propertyValue,null);
|
||||
}
|
||||
public final UnicodeSet getSet(Matcher matcher) {
|
||||
return getSet(matcher,null);
|
||||
}
|
||||
|
||||
public final UnicodeSet getSet(String propertyValue, UnicodeSet result) {
|
||||
int type = getType();
|
||||
return getSet(new SimpleMatcher(propertyValue,
|
||||
|
@ -332,12 +469,7 @@ public abstract class UnicodeProperty extends UnicodeLabel {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
if (cacheValueToSet == null) {
|
||||
cacheValueToSet = new UnicodeMap();
|
||||
for (int i = 0; i <= 0x10FFFF; ++i) {
|
||||
cacheValueToSet.put(i, getValue(i));
|
||||
}
|
||||
}
|
||||
if (cacheValueToSet == null) cacheValueToSet = _getUnicodeMap();
|
||||
Collection temp = new HashSet(); // to avoid reallocating...
|
||||
Iterator it = cacheValueToSet.getAvailableValues(null).iterator();
|
||||
main:
|
||||
|
@ -356,6 +488,21 @@ public abstract class UnicodeProperty extends UnicodeLabel {
|
|||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
protected UnicodeMap _getUnicodeMap() {
|
||||
UnicodeMap result = new UnicodeMap();
|
||||
for (int i = 0; i <= 0x10FFFF; ++i) {
|
||||
if (DEBUG && i == 0x41) System.out.println(i + "\t" + getValue(i));
|
||||
result.put(i, getValue(i));
|
||||
}
|
||||
if (DEBUG) {
|
||||
System.out.println(getName() + ":\t" + getClass().getName()
|
||||
+ "\t" + getVersion());
|
||||
System.out.println(getStack());
|
||||
System.out.println(result);
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
/*
|
||||
public UnicodeSet getMatchSet(UnicodeSet result) {
|
||||
|
@ -369,6 +516,16 @@ public abstract class UnicodeProperty extends UnicodeLabel {
|
|||
}
|
||||
*/
|
||||
|
||||
public static String getStack() {
|
||||
Exception e = new Exception();
|
||||
StringWriter sw = new StringWriter();
|
||||
PrintWriter pw = new PrintWriter(sw);
|
||||
e.printStackTrace(pw);
|
||||
pw.flush();
|
||||
return "Showing Stack with fake " + sw.getBuffer().toString();
|
||||
}
|
||||
|
||||
|
||||
public static Collection addUnique(Object obj, Collection result) {
|
||||
if (obj != null && !result.contains(obj)) result.add(obj);
|
||||
return result;
|
||||
|
@ -386,6 +543,9 @@ public abstract class UnicodeProperty extends UnicodeLabel {
|
|||
public static class SkeletonComparator implements Comparator {
|
||||
public int compare(Object o1, Object o2) {
|
||||
// TODO optimize
|
||||
if (o1 == o2) return 0;
|
||||
if (o1 == null) return -1;
|
||||
if (o2 == null) return 1;
|
||||
return toSkeleton((String)o1).compareTo(toSkeleton((String)o2));
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue