/* * Copyright (c) 1999, 2020, 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. * * 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.
*/
/* * (C) Copyright IBM Corp. 1999 - All Rights Reserved * * The original version of this source code and documentation is * copyrighted and owned by IBM. These materials are provided * under terms of a License Agreement between IBM and Sun. * This technology is protected by multiple US and International * patents. This notice and attribution to IBM may not be removed.
*/ publicclass ConverterTest extends TestFmwk { publicstaticvoid main(String[] args) throws Exception { new ConverterTest().run(args);
}
privatestaticclass Parameter { publicfinal String value; publicfinalboolean statefull; privatefinal Vector nativeValues = new Vector(); privatefinal Vector unicodeValues = new Vector();
public Parameter(final String param) throws IOException { finalint ndx = param.indexOf(":"); if (ndx >= 0) {
value = param.substring(0, ndx);
} else {
value = param;
} final String args = (ndx < 0) ? "" : param.substring(ndx+1); boolean isStatefull = false; for (int i = 0; i < args.length(); i++) { finalchar flag = args.charAt(i); switch (flag) { case's':
isStatefull = true; break; default:
}
}
final String fileName = value+".b2c"; final FileReader f = new FileReader(new File(System.getProperty("test.src", "."), fileName)); final BufferedReader in = new BufferedReader(f);
String line = in.readLine(); while (line != null) { if (line.startsWith("#")) { //ignore all comments except ones that indicate this is a //statefull conversion. if (line.indexOf("STATEFULL") > 0) {
isStatefull = true;
}
} else { final StringTokenizer tokenizer = new StringTokenizer(line);
String key = tokenizer.nextToken();
String value = tokenizer.nextToken(); if (key.startsWith("0x")) key = key.substring(2); if (value.startsWith("0x")) value = value.substring(2);
finalchar c = (char)Integer.parseInt(value, 16); final String unicodeValue = String.valueOf(c);
publicvoid getMapping(final Vector keys, final Vector values, finalboolean toUnicode) throws IOException { final Hashtable temp = new Hashtable(); for (int i = nativeValues.size() - 1; i >= 0; --i) { finalbyte[] key = (byte[])nativeValues.elementAt(i); final String value = (String)unicodeValues.elementAt(i);
if (toUnicode) { final String keyString = printable(key); if (temp.get(keyString) == null) {
temp.put(keyString, keyString);
keys.addElement(key);
values.addElement(value);
}
} else { if (temp.get(value) == null) {
temp.put(value, value);
keys.addElement(value);
values.addElement(key);
}
}
}
}
}
publicvoid checkPages(String[] args) { for (int j = 0; j < args.length; j++) {
logln("Checking converter: "+args[j]); boolean err = false; try { final Parameter param = new Parameter(args[j]);
final Vector keys = new Vector(); final Vector values = new Vector();
param.getMapping(keys, values, true); for (int i = 0; i < keys.size(); i++) { finalbyte[] key = (byte[])keys.elementAt(i); final String value = (String)values.elementAt(i); try { final String actualValue = new String(key, param.value); if (!value.equals(actualValue)) {
logln(printable(key)+" ==> "+printable(value)+" produced "+printable(actualValue));
err = true;
}
} catch (UnsupportedEncodingException e) {
logln(param.value+" encoding not supported: "+e);
err = true; break;
}
}
keys.removeAllElements();
values.removeAllElements();
param.getMapping(keys, values, false); for (int i = 0; i < keys.size(); i++) { final String key = (String)keys.elementAt(i); finalbyte[] value = (byte[])values.elementAt(i); try { finalbyte[] actualValue = key.getBytes(param.value); boolean diff = false; if (value.length != actualValue.length) {
logln(printable(key)+" ==> "+printable(value)+" produced "+printable(actualValue));
err = true;
} else { for (int k = 0; k < value.length; k++) { if (value[k] != actualValue[k]) {
logln(printable(key)+" ==> "+printable(value)+" produced "+printable(actualValue));
err = true; break;
}
}
}
} catch (UnsupportedEncodingException e) {
logln(param.value+" encoding not supported: "+e);
err = true; break;
}
}
} catch (IOException e) {
logln("Could not load table: "+e);
err = true;
} if (err) {
errln("Error in converter: "+args[j]);
} else {
logln(" passed.");
}
}
}
protectedstatic String printable(String c) { final StringBuffer buffer = new StringBuffer(); for (int i = 0; i < c.length(); i++) {
buffer.append(printable(c.charAt(i)));
} return buffer.toString();
}
protectedstatic String printable(byte c) { final StringBuffer buffer = new StringBuffer("0x"); finalint value = ((int)c) & 0x00FF;
buffer.append(HEX_DIGIT[(value & 0x00F0) >> 4]);
buffer.append(HEX_DIGIT[(value & 0x000F)]); return buffer.toString();
}
protectedstatic String printable(byte[] c) { final StringBuffer buffer = new StringBuffer("["); for (int i = 0; i < c.length; i++) { finalint value = ((int)c[i]) & 0x00FF;
buffer.append(HEX_DIGIT[(value & 0x00F0) >> 4]);
buffer.append(HEX_DIGIT[(value & 0x000F)]);
buffer.append(" ");
}
buffer.append("]"); return buffer.toString();
}
protectedstatic String printable(char[] c) { final StringBuffer buffer = new StringBuffer("["); for (int i = 0; i < c.length; i++) {
buffer.append(printable(c[i]));
}
buffer.append("]"); return buffer.toString();
}
protectedstatic String printable(char c) { final StringBuffer buffer = new StringBuffer("\\u"); finalint value = ((int)c) & 0xFFFF;
buffer.append(HEX_DIGIT[(value & 0xF000) >> 12]);
buffer.append(HEX_DIGIT[(value & 0x0F00) >> 8]);
buffer.append(HEX_DIGIT[(value & 0x00F0) >> 4]);
buffer.append(HEX_DIGIT[(value & 0x000F)]); return buffer.toString();
}
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.