0001: /*
0002: *******************************************************************************
0003: * Copyright (C) 1996-2005, International Business Machines Corporation and *
0004: * others. All Rights Reserved. *
0005: *******************************************************************************
0006: */
0007: package com.ibm.icu.dev.demo.translit;
0008:
0009: import java.awt.*;
0010: import java.awt.event.*;
0011: import java.util.*;
0012: import java.text.CharacterIterator;
0013:
0014: import com.ibm.icu.dev.test.util.Differ;
0015: import com.ibm.icu.lang.*;
0016: import com.ibm.icu.text.*;
0017:
0018: import java.io.*;
0019:
0020: /**
0021: * A frame that allows the user to experiment with keyboard
0022: * transliteration. This class has a main() method so it can be run
0023: * as an application. The frame contains an editable text component
0024: * and uses keyboard transliteration to process keyboard events.
0025: *
0026: * <p>Copyright (c) IBM Corporation 1999. All rights reserved.
0027: *
0028: * @author Alan Liu
0029: */
0030: public class Demo extends Frame {
0031:
0032: static final boolean DEBUG = false;
0033: static final String START_TEXT = "(cut,\u03BA\u03C5\u03C4,\u05D0,\u30AF\u30C8,\u4E80,\u091A\u0941\u0924\u094D)";
0034:
0035: Transliterator translit = null;
0036: String fontName = "Arial Unicode MS";
0037: int fontSize = 18;
0038:
0039: /*
0040: boolean compound = false;
0041: Transliterator[] compoundTranslit = new Transliterator[MAX_COMPOUND];
0042: static final int MAX_COMPOUND = 128;
0043: int compoundCount = 0;
0044: */
0045:
0046: TransliteratingTextComponent text = null;
0047:
0048: Menu translitMenu;
0049: CheckboxMenuItem translitItem;
0050: CheckboxMenuItem noTranslitItem;
0051:
0052: static final String NO_TRANSLITERATOR = "None";
0053:
0054: private static final String COPYRIGHT = "\u00A9 IBM Corporation 1999. All rights reserved.";
0055:
0056: public static void main(String[] args) {
0057: Frame f = new Demo(600, 200);
0058: f.addWindowListener(new WindowAdapter() {
0059: public void windowClosing(WindowEvent e) {
0060: System.exit(0);
0061: }
0062: });
0063: f.setVisible(true);
0064: }
0065:
0066: public Demo(int width, int height) {
0067: super ("Transliteration Demo");
0068:
0069: initMenus();
0070:
0071: addWindowListener(new WindowAdapter() {
0072: public void windowClosing(WindowEvent e) {
0073: handleClose();
0074: }
0075: });
0076:
0077: text = new TransliteratingTextComponent();
0078: Font font = new Font(fontName, Font.PLAIN, fontSize);
0079: text.setFont(font);
0080: text.setSize(width, height);
0081: text.setVisible(true);
0082: text.setText(START_TEXT);
0083: add(text);
0084:
0085: setSize(width, height);
0086: setTransliterator("Latin-Greek", null);
0087: }
0088:
0089: private void initMenus() {
0090: MenuBar mbar;
0091: Menu menu;
0092: MenuItem mitem;
0093: //CheckboxMenuItem citem;
0094:
0095: setMenuBar(mbar = new MenuBar());
0096: mbar.add(menu = new Menu("File"));
0097: menu.add(mitem = new MenuItem("Quit"));
0098: mitem.addActionListener(new ActionListener() {
0099: public void actionPerformed(ActionEvent e) {
0100: handleClose();
0101: }
0102: });
0103: /*
0104: final ItemListener setTransliteratorListener = new ItemListener() {
0105: public void itemStateChanged(ItemEvent e) {
0106: CheckboxMenuItem item = (CheckboxMenuItem) e.getSource();
0107: if (e.getStateChange() == ItemEvent.DESELECTED) {
0108: // Don't let the current transliterator be deselected.
0109: // Just reselect it.
0110: item.setState(true);
0111: } else if (compound) {
0112: // Adding an item to a compound transliterator
0113: handleAddToCompound(item.getLabel());
0114: } else if (item != translitItem) {
0115: // Deselect previous choice. Don't need to call
0116: // setState(true) on new choice.
0117: translitItem.setState(false);
0118: translitItem = item;
0119: handleSetTransliterator(item.getLabel());
0120: }
0121: }
0122: };
0123: */
0124: /*
0125: translitMenu.add(translitItem = noTranslitItem =
0126: new CheckboxMenuItem(NO_TRANSLITERATOR, true));
0127: noTranslitItem.addItemListener(new ItemListener() {
0128: public void itemStateChanged(ItemEvent e) {
0129: // Can't uncheck None -- any action here sets None to true
0130: setNoTransliterator();
0131: }
0132: });
0133:
0134: translitMenu.addSeparator();
0135: */
0136:
0137: /*
0138: translitMenu.add(citem = new CheckboxMenuItem("Compound"));
0139: citem.addItemListener(new ItemListener() {
0140: public void itemStateChanged(ItemEvent e) {
0141: CheckboxMenuItem item = (CheckboxMenuItem) e.getSource();
0142: if (e.getStateChange() == ItemEvent.DESELECTED) {
0143: // If compound gets deselected, then select NONE
0144: setNoTransliterator();
0145: } else if (!compound) {
0146: // Switching from non-compound to compound
0147: translitItem.setState(false);
0148: translitItem = item;
0149: translit = null;
0150: compound = true;
0151: compoundCount = 0;
0152: for (int i=0; i<MAX_COMPOUND; ++i) {
0153: compoundTranslit[i] = null;
0154: }
0155: }
0156: }
0157: });
0158:
0159: translitMenu.addSeparator();
0160: */
0161:
0162: /*
0163: for (Enumeration e=getSystemTransliteratorNames().elements();
0164: e.hasMoreElements(); ) {
0165: String s = (String) e.nextElement();
0166: translitMenu.add(citem = new CheckboxMenuItem(s));
0167: citem.addItemListener(setTransliteratorListener);
0168: }
0169: */
0170:
0171: Menu fontMenu = new Menu("Font");
0172: String[] fonts = GraphicsEnvironment
0173: .getLocalGraphicsEnvironment()
0174: .getAvailableFontFamilyNames();
0175: for (int i = 0; i < fonts.length; ++i) {
0176: MenuItem mItem = new MenuItem(fonts[i]);
0177: mItem.addActionListener(new FontActionListener(fonts[i]));
0178: fontMenu.add(mItem);
0179: }
0180: mbar.add(fontMenu);
0181:
0182: Menu sizeMenu = new Menu("Size");
0183: int[] sizes = { 9, 10, 12, 14, 18, 24, 36, 48, 72 };
0184: for (int i = 0; i < sizes.length; ++i) {
0185: MenuItem mItem = new MenuItem("" + sizes[i]);
0186: mItem.addActionListener(new SizeActionListener(sizes[i]));
0187: sizeMenu.add(mItem);
0188: }
0189: mbar.add(sizeMenu);
0190:
0191: translit = null;
0192:
0193: mbar.add(translitMenu = new Menu("Transliterator"));
0194:
0195: translitMenu.add(convertSelectionItem = new MenuItem(
0196: "Transliterate", new MenuShortcut(KeyEvent.VK_K)));
0197: convertSelectionItem.addActionListener(new ActionListener() {
0198: public void actionPerformed(ActionEvent e) {
0199: handleBatchTransliterate(translit);
0200: }
0201: });
0202:
0203: translitMenu.add(swapSelectionItem = new MenuItem("Reverse",
0204: new MenuShortcut(KeyEvent.VK_S)));
0205: swapSelectionItem.addActionListener(new ActionListener() {
0206: public void actionPerformed(ActionEvent e) {
0207: Transliterator inv;
0208: try {
0209: inv = translit.getInverse();
0210: } catch (Exception x) {
0211: inv = Transliterator.getInstance("null");
0212: }
0213: setTransliterator(inv.getID(), null);
0214: }
0215: });
0216:
0217: translitMenu
0218: .add(convertTypingItem = new MenuItem(
0219: "No Typing Conversion", new MenuShortcut(
0220: KeyEvent.VK_T)));
0221: convertTypingItem.addActionListener(new ActionListener() {
0222: public void actionPerformed(ActionEvent e) {
0223: if (!transliterateTyping) {
0224: text.setTransliterator(translit);
0225: convertTypingItem.setLabel("No Typing Conversion");
0226: } else {
0227: text.flush();
0228: text.setTransliterator(null);
0229: convertTypingItem.setLabel("Convert Typing");
0230: }
0231: transliterateTyping = !transliterateTyping;
0232: }
0233: });
0234:
0235: translitMenu.add(historyMenu = new Menu("Recent"));
0236:
0237: helpDialog = new InfoDialog(
0238: this ,
0239: "Simple Demo",
0240: "Instructions",
0241: "CTL A, X, C, V have customary meanings.\n"
0242: + "Arrow keys, delete and backspace work.\n"
0243: + "To get a character from its control point, type the hex, then hit CTL Q");
0244: helpDialog.getArea().setEditable(false);
0245:
0246: Menu helpMenu;
0247: mbar.add(helpMenu = new Menu("Extras"));
0248: helpMenu.add(mitem = new MenuItem("Help"));
0249: mitem.addActionListener(new ActionListener() {
0250: public void actionPerformed(ActionEvent e) {
0251: helpDialog.show();
0252: }
0253: });
0254:
0255: hexDialog = new InfoDialog(this , "Hex Entry",
0256: "Use U+..., \\u..., \\x{...}, or &#x...;", "\\u00E1");
0257: Button button = new Button("Insert");
0258: button.addActionListener(new ActionListener() {
0259: public void actionPerformed(ActionEvent e) {
0260: String hexValue = hexDialog.getArea().getText();
0261: text.insertText(fromHex.transliterate(hexValue));
0262: }
0263: });
0264: hexDialog.getBottom().add(button);
0265:
0266: helpMenu.add(mitem = new MenuItem("Hex...", new MenuShortcut(
0267: KeyEvent.VK_H)));
0268: mitem.addActionListener(new ActionListener() {
0269: public void actionPerformed(ActionEvent e) {
0270: hexDialog.show();
0271: }
0272: });
0273:
0274: // Compound Transliterator
0275:
0276: compoundDialog = new InfoDialog(this ,
0277: "Compound Transliterator", "", "[^\\u0000-\\u00FF] hex");
0278: button = new Button("Set");
0279: button.addActionListener(new ActionListener() {
0280: public void actionPerformed(ActionEvent e) {
0281: String compound = "";
0282: try {
0283: compound = compoundDialog.getArea().getText();
0284: setTransliterator(compound, null);
0285: } catch (RuntimeException ex) {
0286: compoundDialog.getArea().setText(
0287: compound + "\n" + ex.getMessage());
0288: }
0289: }
0290: });
0291: compoundDialog.getBottom().add(button);
0292:
0293: translitMenu.add(mitem = new MenuItem("Multiple...",
0294: new MenuShortcut(KeyEvent.VK_M)));
0295: mitem.addActionListener(new ActionListener() {
0296: public void actionPerformed(ActionEvent e) {
0297: compoundDialog.show();
0298: }
0299: });
0300:
0301: // RuleBased Transliterator
0302:
0303: rulesDialog = new InfoDialog(
0304: this ,
0305: "Rule-Based Transliterator",
0306: "",
0307: "([A-Z]) > &Hex($1) &Name($1);\r\n"
0308: + "&Hex-Any($1) < ('\\' [uU] [a-fA-F0-9]*);\r\n"
0309: + "&Name-Any($1) < ('{' [^\\}]* '}');");
0310: button = new Button("Set");
0311: button.addActionListener(new ActionListener() {
0312: public void actionPerformed(ActionEvent e) {
0313: String compound = "";
0314: try {
0315: compound = rulesDialog.getArea().getText();
0316: String id = ruleId.getText();
0317: setTransliterator(compound, id);
0318: } catch (RuntimeException ex) {
0319: rulesDialog.getArea().setText(
0320: compound + "\n#" + ex.getMessage());
0321: }
0322: }
0323: });
0324: rulesDialog.getBottom().add(button);
0325: ruleId = new TextField("test1", 20);
0326: Label temp = new Label(" Name:");
0327: rulesDialog.getBottom().add(temp);
0328: rulesDialog.getBottom().add(ruleId);
0329:
0330: translitMenu.add(mitem = new MenuItem("From Rules...",
0331: new MenuShortcut(KeyEvent.VK_R)));
0332: mitem.addActionListener(new ActionListener() {
0333: public void actionPerformed(ActionEvent e) {
0334: rulesDialog.show();
0335: }
0336: });
0337:
0338: translitMenu.add(mitem = new MenuItem("From File...",
0339: new MenuShortcut(KeyEvent.VK_F)));
0340: mitem.addActionListener(new FileListener(this , RULE_FILE));
0341:
0342: translitMenu.add(mitem = new MenuItem("Test File..."));
0343: mitem.addActionListener(new FileListener(this , TEST_FILE));
0344:
0345: // Flesh out the menu with the installed transliterators
0346:
0347: translitMenu.addSeparator();
0348:
0349: Iterator sources = add(new TreeSet(),
0350: Transliterator.getAvailableSources()).iterator();
0351: while (sources.hasNext()) {
0352: String source = (String) sources.next();
0353: Iterator targets = add(new TreeSet(),
0354: Transliterator.getAvailableTargets(source))
0355: .iterator();
0356: Menu targetMenu = new Menu(source);
0357: while (targets.hasNext()) {
0358: String target = (String) targets.next();
0359: Set variantSet = add(new TreeSet(), Transliterator
0360: .getAvailableVariants(source, target));
0361: if (variantSet.size() < 2) {
0362: mitem = new MenuItem(target);
0363: mitem
0364: .addActionListener(new TransliterationListener(
0365: source + "-" + target));
0366: targetMenu.add(mitem);
0367: } else {
0368: Iterator variants = variantSet.iterator();
0369: Menu variantMenu = new Menu(target);
0370: while (variants.hasNext()) {
0371: String variant = (String) variants.next();
0372: String menuName = variant.length() == 0 ? "<default>"
0373: : variant;
0374: //System.out.println("<" + source + "-" + target + "/" + variant + ">, <" + menuName + ">");
0375: mitem = new MenuItem(menuName);
0376: mitem
0377: .addActionListener(new TransliterationListener(
0378: source + "-" + target + "/"
0379: + variant));
0380: variantMenu.add(mitem);
0381: }
0382: targetMenu.add(variantMenu);
0383: }
0384: }
0385: translitMenu.add(targetMenu);
0386: }
0387:
0388: }
0389:
0390: static final int RULE_FILE = 0, TEST_FILE = 1;
0391:
0392: //
0393: static class FileListener implements ActionListener {
0394: Demo frame;
0395: int choice;
0396:
0397: FileListener(Demo frame, int choice) {
0398: this .frame = frame;
0399: this .choice = choice;
0400: }
0401:
0402: public void actionPerformed(ActionEvent e) {
0403: String id = frame.translit.getID();
0404: int slashPos = id.indexOf('/');
0405: String variant = "";
0406: if (slashPos >= 0) {
0407: variant = "_" + id.substring(slashPos + 1);
0408: id = id.substring(0, slashPos);
0409: }
0410:
0411: FileDialog fileDialog = new FileDialog(frame, "Input File");
0412: fileDialog.setFile("Test_" + id + ".txt");
0413: fileDialog.show();
0414: String fileName = fileDialog.getFile();
0415: String fileDirectory = fileDialog.getDirectory();
0416: if (fileName != null) {
0417: try {
0418: File f = new File(fileDirectory, fileName);
0419: if (choice == RULE_FILE) {
0420:
0421: // read stuff into buffer
0422:
0423: StringBuffer buffer = new StringBuffer();
0424: FileInputStream fis = new FileInputStream(f);
0425: InputStreamReader isr = new InputStreamReader(
0426: fis, "UTF8");
0427: BufferedReader br = new BufferedReader(isr,
0428: 32 * 1024);
0429: while (true) {
0430: String line = br.readLine();
0431: if (line == null)
0432: break;
0433: if (line.length() > 0
0434: && line.charAt(0) == '\uFEFF')
0435: line = line.substring(1); // strip BOM
0436: buffer.append('\n');
0437: buffer.append(line);
0438: }
0439: br.close();
0440:
0441: // Transform file name into id
0442: if (fileName.startsWith("Transliterator_")) {
0443: fileName = fileName
0444: .substring("Transliterator_"
0445: .length());
0446: }
0447: int pos = fileName.indexOf('_');
0448: if (pos < 0) {
0449: id = fileName;
0450: } else {
0451: id = fileName.substring(0, pos) + "-";
0452: int pos2 = fileName.indexOf('_', pos + 1);
0453: if (pos2 < 0) {
0454: id += fileName.substring(pos + 1);
0455: } else {
0456: id += fileName.substring(pos + 1, pos2)
0457: + "/"
0458: + fileName.substring(pos2 + 1);
0459: }
0460: }
0461: pos = id.lastIndexOf('.');
0462: if (pos >= 0)
0463: id = id.substring(0, pos);
0464:
0465: // Now set
0466:
0467: frame.setTransliterator(buffer.toString(), id);
0468: } else if (choice == TEST_FILE) {
0469: genTestFile(f, frame.translit, variant);
0470: }
0471: } catch (Exception e2) {
0472: e2.printStackTrace();
0473: System.out.println("Problem opening/reading: "
0474: + fileDirectory + ", " + fileName);
0475: }
0476: }
0477: fileDialog.dispose();
0478: }
0479: }
0480:
0481: boolean transliterateTyping = true;
0482: Transliterator fromHex = Transliterator.getInstance("Hex-Any");
0483: InfoDialog helpDialog;
0484: InfoDialog hexDialog;
0485: InfoDialog compoundDialog;
0486: InfoDialog rulesDialog;
0487: TextField ruleId;
0488: MenuItem convertSelectionItem = null;
0489: MenuItem swapSelectionItem = null;
0490: MenuItem convertTypingItem = null;
0491: Menu historyMenu;
0492: Map historyMap = new HashMap();
0493: Set historySet = new TreeSet(new Comparator() {
0494: public int compare(Object a, Object b) {
0495: MenuItem aa = (MenuItem) a;
0496: MenuItem bb = (MenuItem) b;
0497: return aa.getLabel().compareTo(bb.getLabel());
0498: }
0499: });
0500:
0501: // ADD Factory since otherwise getInverse blows out
0502: static class DummyFactory implements Transliterator.Factory {
0503: static DummyFactory singleton = new DummyFactory();
0504: static HashMap m = new HashMap();
0505:
0506: // Since Transliterators are immutable, we don't have to clone on set & get
0507: static void add(String ID, Transliterator t) {
0508: m.put(ID, t);
0509: System.out.println("Registering: " + ID + ", "
0510: + t.toRules(true));
0511: Transliterator.registerFactory(ID, singleton);
0512: }
0513:
0514: public Transliterator getInstance(String ID) {
0515: return (Transliterator) m.get(ID);
0516: }
0517: }
0518:
0519: static void printBreaks(int num, String testSource, BreakIterator bi) {
0520: String result = "";
0521: int lastPos = 0;
0522: while (true) {
0523: int pos = bi.next();
0524: if (pos == BreakIterator.DONE)
0525: break;
0526: result += testSource.substring(lastPos, pos) + "&";
0527: lastPos = pos;
0528: System.out.println(pos);
0529: }
0530: System.out.println("Test" + num + ": " + result);
0531: }
0532:
0533: static void printIteration(int num, String testSource,
0534: CharacterIterator ci) {
0535: String result = "";
0536: while (true) {
0537: char ch = ci.next();
0538: if (ch == CharacterIterator.DONE)
0539: break;
0540: result += ch + "(" + ci.getIndex() + ")";
0541: }
0542: System.out.println("Test" + num + ": " + result);
0543: }
0544:
0545: static void printSources() {
0546: String[] list = { "Latin-ThaiLogical", "ThaiLogical-Latin",
0547: "Thai-ThaiLogical", "ThaiLogical-Thai" };
0548: UnicodeSet all = new UnicodeSet();
0549: for (int i = 0; i < list.length; ++i) {
0550: Transliterator tr = Transliterator.getInstance(list[i]);
0551: UnicodeSet src = tr.getSourceSet();
0552: System.out.println(list[i] + ": " + src.toPattern(true));
0553: all.addAll(src);
0554: }
0555: System.out.println("All: " + all.toPattern(true));
0556: UnicodeSet rem = new UnicodeSet("[[:latin:][:thai:]]");
0557: System.out.println("missing from [:latin:][:thai:]: "
0558: + all.removeAll(rem).toPattern(true));
0559: }
0560:
0561: // 200E;LEFT-TO-RIGHT MARK;Cf;0;L;;;;;N;;;;;
0562:
0563: static Transliterator title = Transliterator.getInstance("title");
0564: static String hexAndNameRules = " ([:c:]) > \\u200E &hex/unicode($1) ' ( ) ' &name($1) \\u200E ' ';"
0565: + "([:mark:]) > \\u200E &hex/unicode($1) ' ( ' \\u200E \u25CC $1 \\u200E ' ) ' &name($1) \\u200E ' ';"
0566: + "(.) > \\u200E &hex/unicode($1) ' ( ' \\u200E $1 \\u200E ' ) ' &name($1) ' ' \\u200E;";
0567:
0568: static Transliterator hexAndName = Transliterator.createFromRules(
0569: "any-hexAndName", hexAndNameRules, Transliterator.FORWARD);
0570:
0571: //static Transliterator upper = Transliterator.getInstance("upper");
0572:
0573: static final byte NONE = 0, TITLEWORD = 1, TITLELINE = 2;
0574:
0575: static void genTestFile(File sourceFile, Transliterator translit,
0576: String variant) {
0577: try {
0578:
0579: System.out.println("Reading: "
0580: + sourceFile.getCanonicalPath());
0581: BufferedReader in = new BufferedReader(
0582: new InputStreamReader(new FileInputStream(
0583: sourceFile), "UTF-8"));
0584: String targetFile = sourceFile.getCanonicalPath();
0585: int dotPos = targetFile.lastIndexOf('.');
0586: if (dotPos >= 0)
0587: targetFile = targetFile.substring(0, dotPos);
0588: targetFile += variant;
0589:
0590: File outFile = new File(targetFile + ".html");
0591: System.out
0592: .println("Writing: " + outFile.getCanonicalPath());
0593:
0594: PrintWriter out = new PrintWriter(new BufferedWriter(
0595: new OutputStreamWriter(
0596: new FileOutputStream(outFile), "UTF-8")));
0597:
0598: String direction = "";
0599: String id = translit.getID();
0600: if (id.indexOf("Arabic") >= 0 || id.indexOf("Hebrew") >= 0) {
0601: direction = " direction: rtl;";
0602: }
0603: boolean testRoundTrip = true;
0604: boolean generateSets = true;
0605: if (id.startsWith("Han-") || id.startsWith("ja-")) {
0606: testRoundTrip = false;
0607: generateSets = false;
0608: }
0609: out
0610: .println("<head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
0611: out.println("<style><!--");
0612: out
0613: .println("td, th { vertical-align: top; border: 1px solid black }");
0614: out.println("td.s { background-color: #EEEEEE;"
0615: + direction + " }");
0616: out.println("td.r { background-color: #CCCCCC;"
0617: + direction + " }");
0618: out.println("td.n { background-color: #FFFFCC; }");
0619: out.println("td.title { border: 0px solid black}");
0620: out.println("span.d { background-color: #FF6666 }");
0621: out.println("span.r { background-color: #66FF66 }");
0622:
0623: out
0624: .println("body { font-family: 'Arial Unicode MS', 'Lucida Sans Unicode', Arial, sans-serif; margin: 5 }");
0625: out.println("--></style>");
0626: out.println("<title>" + id
0627: + " Transliteration Check</title></head>");
0628: out
0629: .println("<body bgcolor='#FFFFFF'><p>See <a href='Test_Instructions.html'>Test_Instructions.html</a> for details.</p>");
0630: out.println("<table>");
0631:
0632: //out.println("<tr><th width='33%'>Thai</th><th width='33%'>Latin</th><th width='33%'>Thai</th></tr>");
0633:
0634: Transliterator tl = translit;
0635: Transliterator lt = tl.getInverse();
0636:
0637: Transliterator ltFilter = tl.getInverse();
0638: ltFilter.setFilter(new UnicodeSet("[:^Lu:]"));
0639: Transliterator tlFilter = lt.getInverse();
0640: tlFilter.setFilter(new UnicodeSet("[:^Lu:]"));
0641:
0642: //Transliterator.getInstance("[:^Lu:]" + lt.getID());
0643:
0644: BreakIterator sentenceBreak = BreakIterator
0645: .getSentenceInstance();
0646:
0647: byte titleSetting = TITLELINE;
0648: boolean upperfilter = false;
0649: boolean first = true;
0650: while (true) {
0651: String line = in.readLine();
0652: if (line == null)
0653: break;
0654: line = line.trim();
0655: if (line.length() == 0)
0656: continue;
0657: if (line.charAt(0) == '\uFEFF')
0658: line = line.substring(1); // remove BOM
0659:
0660: if (line.charAt(0) == '#')
0661: continue; // comments
0662:
0663: if (line.equals("@TITLECASE@")) {
0664: titleSetting = TITLEWORD;
0665: out
0666: .println("<tr><td colspan='2' class='title'><b>Names</b></td></tr>");
0667: continue;
0668: } else if (line.equals("@UPPERFILTER@")) {
0669: upperfilter = true;
0670: continue;
0671: } else if (line.startsWith("@SET")) {
0672: UnicodeSet s = new UnicodeSet(line.substring(4)
0673: .trim());
0674: out
0675: .println("<tr><td colspan='2' class='title'><b>Characters</b></td></tr>");
0676: UnicodeSetIterator it = new UnicodeSetIterator(s);
0677: while (it.next()) {
0678: addSentenceToTable(
0679: out,
0680: it.codepoint != UnicodeSetIterator.IS_STRING ? UTF16
0681: .valueOf(it.codepoint)
0682: : it.string, NONE, true,
0683: testRoundTrip, first, tl, lt);
0684: }
0685: continue;
0686: }
0687:
0688: sentenceBreak.setText(line);
0689: int start = 0;
0690: while (true) {
0691: int end = sentenceBreak.next();
0692: if (end == BreakIterator.DONE)
0693: break;
0694: String coreSentence = line.substring(start, end);
0695: //System.out.println("Core: " + hex.transliterate(coreSentence));
0696: end = start;
0697:
0698: int oldPos = 0;
0699: while (oldPos < coreSentence.length()) {
0700: // hack, because sentence doesn't seem to be working right
0701: int pos = coreSentence.indexOf(". ", oldPos);
0702: if (pos < 0)
0703: pos = coreSentence.length();
0704: else
0705: pos = pos + 2;
0706: int pos2 = coreSentence.indexOf('\u3002',
0707: oldPos);
0708: if (pos2 < 0)
0709: pos2 = coreSentence.length();
0710: else
0711: pos2 = pos2 + 1;
0712: if (pos > pos2)
0713: pos = pos2;
0714: String sentence = coreSentence.substring(
0715: oldPos, pos).trim();
0716: //System.out.println("Sentence: " + hex.transliterate(coreSentence));
0717: oldPos = pos;
0718:
0719: addSentenceToTable(out, sentence, titleSetting,
0720: false, testRoundTrip, first, tl, lt);
0721:
0722: first = false;
0723: }
0724: }
0725: }
0726: out.println("</table></body>");
0727: out.close();
0728:
0729: // Now write the source/target sets
0730: if (generateSets) {
0731: outFile = new File(targetFile + "_Sets.html");
0732: System.out.println("Writing: "
0733: + outFile.getCanonicalPath());
0734:
0735: out = new PrintWriter(new BufferedWriter(
0736: new OutputStreamWriter(new FileOutputStream(
0737: outFile), "UTF-8")));
0738: out
0739: .println("<head><meta http-equiv='Content-Type' content='text/html; charset=utf-8'>");
0740: out.println("<style><!--");
0741: out
0742: .println("body { font-family: 'Arial Unicode MS', 'Lucida Sans Unicode', Arial, sans-serif; margin: 5 }");
0743: out.println("--></style>");
0744: out.println("<title>" + id
0745: + " Transliteration Sets</title></head>");
0746: out.println("<body bgcolor='#FFFFFF'>");
0747:
0748: int dashPos = id.indexOf('-');
0749: int slashPos = id.indexOf('/');
0750: if (slashPos < 0)
0751: slashPos = id.length();
0752: UnicodeSet sourceSuper = null;
0753: try {
0754: String temp = id.substring(0, dashPos);
0755: if (temp.equals("ja"))
0756: sourceSuper = new UnicodeSet(
0757: "[[:Han:][:hiragana:][:katakana:]]");
0758: else
0759: sourceSuper = new UnicodeSet("[[:" + temp
0760: + ":][:Mn:][:Me:]]");
0761: } catch (Exception e) {
0762: }
0763:
0764: UnicodeSet targetSuper = null;
0765: try {
0766: targetSuper = new UnicodeSet("[[:"
0767: + id.substring(dashPos + 1, slashPos)
0768: + ":][:Mn:][:Me:]]");
0769: } catch (Exception e) {
0770: }
0771:
0772: int nfdStyle = CLOSE_CASE | CLOSE_FLATTEN
0773: | CLOSE_CANONICAL;
0774: int nfkdStyle = nfdStyle | CLOSE_COMPATIBILITY;
0775: out.println("<ul>");
0776: out.println("<p><b>None</b></p>");
0777: showSets(out, translit, lt, null, null, 0);
0778: out.println("<p><b>NFD</b></p>");
0779: showSets(out, translit, lt, sourceSuper, targetSuper,
0780: nfdStyle);
0781: out.println("<p><b>NFKD</b></p>");
0782: showSets(out, translit, lt, sourceSuper, targetSuper,
0783: nfkdStyle);
0784: out.println("</ul></body>");
0785: out.close();
0786: }
0787: System.out.println("Done Writing");
0788: } catch (Exception e) {
0789: e.printStackTrace();
0790: }
0791: }
0792:
0793: static void addSentenceToTable(PrintWriter out, String sentence,
0794: byte titleSetting, boolean addName, boolean testRoundTrip,
0795: boolean first, Transliterator tl, Transliterator lt) {
0796: if (sentence.length() == 0)
0797: return; // skip empty lines
0798:
0799: String originalShow = sentence;
0800: String latin;
0801: latin = tl.transliterate(saveAscii.transliterate(sentence));
0802:
0803: String latinShow = latin;
0804: if (titleSetting == TITLEWORD) {
0805: latinShow = title.transliterate(latin);
0806: } else if (titleSetting == TITLELINE) {
0807: latinShow = titlecaseFirstWord(latinShow);
0808: }
0809: latinShow = restoreAscii.transliterate(latinShow);
0810:
0811: String reverse;
0812: reverse = restoreAscii.transliterate(lt.transliterate(latin));
0813:
0814: String NFKDSentence = Normalizer.normalize(sentence,
0815: Normalizer.NFKD);
0816: String NFKDLatin = Normalizer.normalize(latin, Normalizer.NFKD);
0817: String NFKDReverse = Normalizer.normalize(reverse,
0818: Normalizer.NFKD);
0819:
0820: if (latinShow.length() == 0) {
0821: latinShow = "<i>empty</i>";
0822: } else if (NFKDSentence.equals(NFKDLatin)) {
0823: latinShow = "<span class='r'>" + latinShow + "</span>";
0824: }
0825: String reverseShow = reverse;
0826:
0827: if (testRoundTrip && !NFKDReverse.equals(NFKDSentence)) {
0828: int minLen = reverse.length();
0829: if (minLen > sentence.length())
0830: minLen = sentence.length();
0831: int i;
0832: for (i = 0; i < minLen; ++i) {
0833: if (reverse.charAt(i) != sentence.charAt(i))
0834: break;
0835: }
0836: //originalShow = sentence.substring(0,i) + "<span class='d'>" + sentence.substring(i) + "</span>";
0837: reverseShow = reverseShow.length() == 0 ? "<i>empty</i>"
0838: //: reverse.substring(0,i) + "<span class='d'>" + reverse.substring(i) + "</span>";
0839: : showDifference(sentence, reverse);
0840: out.println("<tr><td class='s'"
0841: + (first ? " width='50%'>" : ">") + originalShow
0842: + "</td><td rowSpan='2'>" + latinShow
0843: + "</td></tr><tr><td class='r'>" + reverseShow
0844: + "</td></tr>");
0845: } else {
0846: out.println("<tr><td class='s'"
0847: + (first ? " width='50%'>" : ">") + originalShow
0848: + "</td><td>" + latinShow + "</td></tr>");
0849: }
0850: if (addName) {
0851: latinShow = hexAndName.transliterate(latin);
0852: if (latinShow.length() == 0)
0853: latinShow = "<i>empty</i>";
0854: originalShow = hexAndName.transliterate(sentence);
0855: if (originalShow.length() == 0)
0856: originalShow = "<i>empty</i>";
0857:
0858: out.println("<tr><td class='n'>" + originalShow
0859: + "</td><td class='n'>" + latinShow + "</td></tr>");
0860: }
0861: out.println("<tr><td></td></tr>");
0862:
0863: }
0864:
0865: static String showDifference(String as, String bs) {
0866: Differ differ = new Differ(300, 3);
0867: StringBuffer out = new StringBuffer();
0868: int max = as.length();
0869: if (max < bs.length())
0870: max = bs.length();
0871: for (int j = 0; j <= max; ++j) {
0872: if (j < as.length())
0873: differ.addA(as.substring(j, j + 1));
0874: if (j < bs.length())
0875: differ.addB(bs.substring(j, j + 1));
0876: differ.checkMatch(j == max);
0877:
0878: if (differ.getACount() != 0 || differ.getBCount() != 0) {
0879: out.append("...");
0880: if (differ.getACount() != 0) {
0881: out.append("<span class='r'>");
0882: for (int i = 0; i < differ.getACount(); ++i) {
0883: out.append(differ.getA(i));
0884: }
0885: out.append("</span>");
0886: }
0887: if (differ.getBCount() != 0) {
0888: out.append("<span class='d'>");
0889: for (int i = 0; i < differ.getBCount(); ++i) {
0890: out.append(differ.getB(i));
0891: }
0892: out.append("</span>");
0893: }
0894: out.append("...");
0895: }
0896: }
0897: return out.toString();
0898: }
0899:
0900: static void showSets(PrintWriter out, Transliterator translit,
0901: Transliterator inverse, UnicodeSet sourceSuper,
0902: UnicodeSet targetSuper, int options) {
0903: out.println("<li>Source Set:<ul><li>"
0904: + toPattern(closeUnicodeSet(translit.getSourceSet(),
0905: options), sourceSuper) + "</li></ul></li>");
0906: out.println("<li>Reverse Target Set:<ul><li>"
0907: + toPattern(closeUnicodeSet(inverse.getTargetSet(),
0908: options), sourceSuper) + "</li></ul></li>");
0909: out.println("<li>Target Set:<ul><li>"
0910: + toPattern(closeUnicodeSet(translit.getTargetSet(),
0911: options), targetSuper) + "</li></ul></li>");
0912: out.println("<li>Reverse Source Set:<ul><li>"
0913: + toPattern(closeUnicodeSet(inverse.getSourceSet(),
0914: options), targetSuper) + "</li></ul></li>");
0915: }
0916:
0917: static final int CLOSE_CASE = 1, CLOSE_FLATTEN = 2,
0918: CLOSE_CANONICAL = 4, CLOSE_COMPATIBILITY = 8;
0919:
0920: static UnicodeSet closeUnicodeSet(UnicodeSet source, int options) {
0921: if (options == 0)
0922: return source;
0923:
0924: UnicodeSetIterator it = new UnicodeSetIterator(source);
0925: UnicodeSet additions = new UnicodeSet(); // to avoid messing up iterator
0926: UnicodeSet removals = new UnicodeSet(); // to avoid messing up iterator
0927: String base;
0928: int cp;
0929:
0930: // Add all case equivalents
0931: if ((options & CLOSE_CASE) != 0) {
0932: while (it.next()) {
0933: cp = it.codepoint;
0934: if (cp == UnicodeSetIterator.IS_STRING)
0935: continue;
0936: int type = UCharacter.getType(cp);
0937: if (type == Character.UPPERCASE_LETTER
0938: || type == Character.LOWERCASE_LETTER
0939: || type == Character.TITLECASE_LETTER) {
0940: additions.add(UCharacter.toLowerCase(UTF16
0941: .valueOf(cp)));
0942: additions.add(UCharacter.toUpperCase(UTF16
0943: .valueOf(cp)));
0944: }
0945: }
0946: source.addAll(additions);
0947: }
0948:
0949: // Add the canonical closure of all strings and characters in source
0950: if ((options & CLOSE_CANONICAL) != 0) {
0951: it.reset();
0952: additions.clear();
0953: CanonicalIterator ci = new CanonicalIterator(".");
0954: while (it.next()) {
0955: if (it.codepoint == UnicodeSetIterator.IS_STRING)
0956: base = it.string;
0957: else
0958: base = UTF16.valueOf(it.codepoint);
0959: ci.setSource(base);
0960: while (true) {
0961: String trial = ci.next();
0962: if (trial == null)
0963: break;
0964: if (trial.equals(base))
0965: continue;
0966: additions.add(trial);
0967: }
0968: }
0969: source.addAll(additions);
0970: }
0971:
0972: // flatten strings
0973: if ((options & CLOSE_FLATTEN) != 0) {
0974: it.reset();
0975: additions.clear();
0976: while (it.next()) {
0977: if (it.codepoint != UnicodeSetIterator.IS_STRING)
0978: continue;
0979: additions.addAll(it.string);
0980: removals.add(it.string);
0981: //System.out.println("flattening '" + hex.transliterate(it.string) + "'");
0982: }
0983: source.addAll(additions);
0984: source.removeAll(removals);
0985: }
0986:
0987: // Now add decompositions of characters in source
0988: if ((options & CLOSE_COMPATIBILITY) != 0) {
0989: it.reset(source);
0990: additions.clear();
0991: while (it.next()) {
0992: if (it.codepoint == UnicodeSetIterator.IS_STRING)
0993: base = it.string;
0994: else
0995: base = UTF16.valueOf(it.codepoint);
0996: if (Normalizer.isNormalized(base, Normalizer.NFKD, 0))
0997: continue;
0998: String decomp = Normalizer.normalize(base,
0999: Normalizer.NFKD);
1000: additions.add(decomp);
1001: }
1002: source.addAll(additions);
1003:
1004: // Now add any other character that decomposes to a character in source
1005: for (cp = 0; cp < 0x10FFFF; ++cp) {
1006: if (!UCharacter.isDefined(cp))
1007: continue;
1008: if (Normalizer.isNormalized(cp, Normalizer.NFKD, 0))
1009: continue;
1010: if (source.contains(cp))
1011: continue;
1012:
1013: String decomp = Normalizer.normalize(cp,
1014: Normalizer.NFKD);
1015: if (source.containsAll(decomp)) {
1016: // System.out.println("Adding: " + Integer.toString(cp,16) + " " + UCharacter.getName(cp));
1017: source.add(cp);
1018: }
1019: }
1020: }
1021:
1022: return source;
1023: }
1024:
1025: static String toPattern(UnicodeSet source, UnicodeSet super set) {
1026: if (super set != null) {
1027: source.removeAll(super set);
1028: return "[" + super set.toPattern(true) + " "
1029: + source.toPattern(true) + "]";
1030: }
1031: return source.toPattern(true);
1032: }
1033:
1034: static BreakIterator bi = BreakIterator.getWordInstance();
1035:
1036: static String titlecaseFirstWord(String line) {
1037: // search for first word with letters. If the first letter is lower, then titlecase it.
1038: bi.setText(line);
1039: int start = 0;
1040: while (true) {
1041: int end = bi.next();
1042: if (end == BreakIterator.DONE)
1043: break;
1044: int firstLetterType = getFirstLetterType(line, start, end);
1045: if (firstLetterType != Character.UNASSIGNED) {
1046: if (firstLetterType != Character.LOWERCASE_LETTER)
1047: break;
1048: line = line.substring(0, start)
1049: + UCharacter.toTitleCase(line.substring(start,
1050: end), bi) + line.substring(end);
1051: break;
1052: }
1053: end = start;
1054: }
1055: return line;
1056: }
1057:
1058: static final int LETTER_MASK = (1 << Character.UPPERCASE_LETTER)
1059: | (1 << Character.LOWERCASE_LETTER)
1060: | (1 << Character.TITLECASE_LETTER)
1061: | (1 << Character.MODIFIER_LETTER)
1062: | (1 << Character.OTHER_LETTER);
1063:
1064: static int getFirstLetterType(String line, int start, int end) {
1065: int cp;
1066: for (int i = start; i < end; i += UTF16.getCharCount(cp)) {
1067: cp = UTF16.charAt(line, i);
1068: int type = UCharacter.getType(cp);
1069: if (((1 << type) & LETTER_MASK) != 0)
1070: return type;
1071: }
1072: return Character.UNASSIGNED;
1073: }
1074:
1075: static void printNames(UnicodeSet s, String targetFile) {
1076: try {
1077: File outFile = new File(targetFile);
1078: System.out
1079: .println("Writing: " + outFile.getCanonicalPath());
1080:
1081: PrintWriter out = new PrintWriter(new BufferedWriter(
1082: new OutputStreamWriter(
1083: new FileOutputStream(outFile), "UTF-8")));
1084: UnicodeSet main = new UnicodeSet();
1085:
1086: UnicodeSet others = new UnicodeSet();
1087: UnicodeSetIterator it = new UnicodeSetIterator(s);
1088: while (it.next()) {
1089: if (!UCharacter.isDefined(it.codepoint))
1090: continue;
1091: if (!Normalizer.isNormalized(it.codepoint,
1092: Normalizer.NFD, 0)) {
1093: String decomp = Normalizer.normalize(it.codepoint,
1094: Normalizer.NFD);
1095: others.addAll(decomp);
1096: continue;
1097: }
1098: out.println(" " + UTF16.valueOf(it.codepoint)
1099: + " <> XXX # "
1100: + UCharacter.getName(it.codepoint));
1101: main.add(it.codepoint);
1102: }
1103:
1104: if (others.size() != 0) {
1105: out.println("Decomposed characters found above: ");
1106: others.removeAll(main);
1107: it.reset(others);
1108: while (it.next()) {
1109: out.println(" " + UTF16.valueOf(it.codepoint)
1110: + " <> XXX # "
1111: + UCharacter.getName(it.codepoint));
1112: }
1113: }
1114:
1115: out.close();
1116: System.out.println("Done Writing");
1117: } catch (Exception e) {
1118: e.printStackTrace();
1119: }
1120: }
1121:
1122: static Transliterator hex = Transliterator
1123: .getInstance("[^\\u0020-\\u007E] hex");
1124: static final String saveRules = "A <> \uEA41; B <> \uEA42; C <> \uEA43; D <> \uEA44; E <> \uEA45; F <> \uEA46; G <> \uEA47; H <> \uEA48; I <> \uEA49; "
1125: + "J <> \uEA4A; K <> \uEA4B; L <> \uEA4C; M <> \uEA4D; N <> \uEA4E; O <> \uEA4F; P <> \uEA50; Q <> \uEA51; R <> \uEA52; "
1126: + "S <> \uEA53; T <> \uEA54; U <> \uEA55; V <> \uEA56; W <> \uEA57; X <> \uEA58; Y <> \uEA59; Z <> \uEA5A; "
1127: + "a <> \uEA61; b <> \uEA62; c <> \uEA63; d <> \uEA64; e <> \uEA65; f <> \uEA66; g <> \uEA67; h <> \uEA68; i <> \uEA69; "
1128: + "j <> \uEA6A; k <> \uEA6B; l <> \uEA6C; m <> \uEA6D; n <> \uEA6E; o <> \uEA6F; p <> \uEA70; q <> \uEA71; r <> \uEA72; "
1129: + "s <> \uEA73; t <> \uEA74; u <> \uEA75; v <> \uEA76; w <> \uEA77; x <> \uEA78; y <> \uEA79; z <> \uEA7A;";
1130:
1131: static Transliterator saveAscii = Transliterator.createFromRules(
1132: "ascii-saved", saveRules, Transliterator.FORWARD);
1133: static Transliterator restoreAscii = Transliterator
1134: .createFromRules("ascii-saved", saveRules,
1135: Transliterator.REVERSE);
1136:
1137: static {
1138:
1139: if (false) {
1140:
1141: for (char i = 'A'; i <= 'z'; ++i) {
1142: System.out.print(i
1143: + " <> "
1144: + hex.transliterate(String
1145: .valueOf((char) (0xEA00 + i))) + "; ");
1146: }
1147:
1148: UnicodeSet x = new UnicodeSet("[[:^ccc=0:]&[:^ccc=230:]]");
1149: x = x.complement();
1150: x = x.complement();
1151: System.out.println("Test: " + x.toPattern(true));
1152:
1153: Transliterator y = Transliterator
1154: .createFromRules(
1155: "xxx",
1156: "$notAbove = [[:^ccc=0:]&[:^ccc=230:]]; u ($notAbove*) \u0308 > XXX | $1; ",
1157: Transliterator.FORWARD);
1158:
1159: String[] testList = { "u\u0308", "u\u0316\u0308",
1160: "u\u0308\u0316", "u\u0301\u0308", "u\u0308\u0301" };
1161: for (int i = 0; i < testList.length; ++i) {
1162: String yy = y.transliterate(testList[i]);
1163: System.out.println(hex.transliterate(testList[i])
1164: + " => " + hex.transliterate(yy));
1165: }
1166:
1167: //printNames(new UnicodeSet("[\u0600-\u06FF]"), "Arabic-Latin.txt");
1168:
1169: /*
1170: BreakTransliterator.register();
1171:
1172: BreakTransliterator testTrans = new BreakTransliterator("Any-XXX", null, null, "$");
1173: String testSource = "The Quick: Brown fox--jumped.";
1174: BreakIterator bi = testTrans.getBreakIterator();
1175: bi.setText(new StringCharacterIterator(testSource));
1176: printBreaks(0, testSource, bi);
1177: //bi.setText(UCharacterIterator.getInstance(testSource));
1178: //printBreaks(1, testSource, bi);
1179:
1180: printIteration(2, testSource, new StringCharacterIterator(testSource));
1181: //printIteration(3, testSource, UCharacterIterator.getInstance(testSource));
1182:
1183:
1184:
1185: String test = testTrans.transliterate(testSource);
1186: System.out.println("Test3: " + test);
1187: DummyFactory.add(testTrans.getID(), testTrans);
1188: */
1189:
1190: // AnyTransliterator.ScriptRunIterator.registerAnyToScript();
1191: AnyTransliterator at = new AnyTransliterator("Greek", null);
1192: at.transliterate("(cat,\u03b1,\u0915)");
1193: DummyFactory.add(at.getID(), at);
1194:
1195: at = new AnyTransliterator("Devanagari", null);
1196: at.transliterate("(cat,\u03b1,\u0915)");
1197: DummyFactory.add(at.getID(), at);
1198:
1199: at = new AnyTransliterator("Latin", null);
1200: at.transliterate("(cat,\u03b1,\u0915)");
1201: DummyFactory.add(at.getID(), at);
1202:
1203: DummyFactory
1204: .add(
1205: "Any-gif",
1206: Transliterator
1207: .createFromRules(
1208: "gif",
1209: "'\\'u(..)(..) > '<img src=\"http://www.unicode.org/gifs/24/' $1 '/U' $1$2 '.gif\">';",
1210: Transliterator.FORWARD));
1211: DummyFactory.add("gif-Any", Transliterator
1212: .getInstance("Any-Null"));
1213:
1214: DummyFactory.add("Any-RemoveCurly", Transliterator
1215: .createFromRules("RemoveCurly", "[\\{\\}] > ;",
1216: Transliterator.FORWARD));
1217: DummyFactory.add("RemoveCurly-Any", Transliterator
1218: .getInstance("Any-Null"));
1219:
1220: System.out.println("Trying &hex");
1221: Transliterator t = Transliterator.createFromRules("hex2",
1222: "(.) > &hex($1);", Transliterator.FORWARD);
1223: System.out.println("Registering");
1224: DummyFactory.add("Any-hex2", t);
1225:
1226: System.out.println("Trying &gif");
1227: t = Transliterator.createFromRules("gif2",
1228: "(.) > &any-gif($1);", Transliterator.FORWARD);
1229: System.out.println("Registering");
1230: DummyFactory.add("Any-gif2", t);
1231: }
1232: }
1233:
1234: void setTransliterator(String name, String id) {
1235: if (DEBUG)
1236: System.out.println("Got: " + name);
1237: if (id == null) {
1238: translit = Transliterator.getInstance(name);
1239: } else {
1240: String reverseId = "";
1241: int pos = id.indexOf('-');
1242: if (pos < 0) {
1243: reverseId = id + "-Any";
1244: id = "Any-" + id;
1245: } else {
1246: int pos2 = id.indexOf("/", pos);
1247: if (pos2 < 0) {
1248: reverseId = id.substring(pos + 1) + "-"
1249: + id.substring(0, pos);
1250: } else {
1251: reverseId = id.substring(pos + 1, pos2) + "-"
1252: + id.substring(0, pos) + id.substring(pos2);
1253: }
1254: }
1255:
1256: translit = Transliterator.createFromRules(id, name,
1257: Transliterator.FORWARD);
1258: if (DEBUG) {
1259: System.out.println("***Forward Rules");
1260: System.out.println(translit.toRules(true));
1261: System.out.println("***Source Set");
1262: System.out.println(translit.getSourceSet().toPattern(
1263: true));
1264: }
1265: System.out.println("***Target Set");
1266: UnicodeSet target = translit.getTargetSet();
1267: System.out.println(target.toPattern(true));
1268: UnicodeSet rest = new UnicodeSet("[a-z]").removeAll(target);
1269: System.out.println("***ASCII - Target Set");
1270: System.out.println(rest.toPattern(true));
1271:
1272: DummyFactory.add(id, translit);
1273:
1274: Transliterator translit2 = Transliterator.createFromRules(
1275: reverseId, name, Transliterator.REVERSE);
1276: if (DEBUG) {
1277: System.out.println("***Backward Rules");
1278: System.out.println(translit2.toRules(true));
1279: }
1280: DummyFactory.add(reverseId, translit2);
1281:
1282: Transliterator rev = translit.getInverse();
1283: if (DEBUG)
1284: System.out.println("***Inverse Rules");
1285: if (DEBUG)
1286: System.out.println(rev.toRules(true));
1287:
1288: }
1289: text.flush();
1290: text.setTransliterator(translit);
1291: convertSelectionItem.setLabel(Transliterator
1292: .getDisplayName(translit.getID()));
1293:
1294: addHistory(translit);
1295:
1296: Transliterator inv;
1297: try {
1298: inv = translit.getInverse();
1299: } catch (Exception ex) {
1300: inv = null;
1301: }
1302: if (inv != null) {
1303: addHistory(inv);
1304: swapSelectionItem.setEnabled(true);
1305: } else {
1306: swapSelectionItem.setEnabled(false);
1307: }
1308: System.out.println("Set transliterator: " + translit.getID()
1309: + (inv == null ? " and " + inv.getID() : ""));
1310: }
1311:
1312: void addHistory(Transliterator translit) {
1313: String name = translit.getID();
1314: MenuItem cmi = (MenuItem) historyMap.get(name);
1315: if (cmi == null) {
1316: cmi = new MenuItem(Transliterator.getDisplayName(name));
1317: cmi.addActionListener(new TransliterationListener(name));
1318: historyMap.put(name, cmi);
1319: historySet.add(cmi);
1320: historyMenu.removeAll();
1321: Iterator it = historySet.iterator();
1322: while (it.hasNext()) {
1323: historyMenu.add((MenuItem) it.next());
1324: }
1325: }
1326: }
1327:
1328: class TransliterationListener implements ActionListener,
1329: ItemListener {
1330: String name;
1331:
1332: public TransliterationListener(String name) {
1333: this .name = name;
1334: }
1335:
1336: public void actionPerformed(ActionEvent e) {
1337: setTransliterator(name, null);
1338: }
1339:
1340: public void itemStateChanged(ItemEvent e) {
1341: if (e.getStateChange() == ItemEvent.SELECTED) {
1342: setTransliterator(name, null);
1343: } else {
1344: setTransliterator("Any-Null", null);
1345: }
1346: }
1347: }
1348:
1349: class FontActionListener implements ActionListener {
1350: String name;
1351:
1352: public FontActionListener(String name) {
1353: this .name = name;
1354: }
1355:
1356: public void actionPerformed(ActionEvent e) {
1357: if (DEBUG)
1358: System.out.println("Font: " + name);
1359: fontName = name;
1360: text.setFont(new Font(fontName, Font.PLAIN, fontSize));
1361: }
1362: }
1363:
1364: class SizeActionListener implements ActionListener {
1365: int size;
1366:
1367: public SizeActionListener(int size) {
1368: this .size = size;
1369: }
1370:
1371: public void actionPerformed(ActionEvent e) {
1372: if (DEBUG)
1373: System.out.println("Size: " + size);
1374: fontSize = size;
1375: text.setFont(new Font(fontName, Font.PLAIN, fontSize));
1376: }
1377: }
1378:
1379: Set add(Set s, Enumeration enumeration) {
1380: while (enumeration.hasMoreElements()) {
1381: s.add(enumeration.nextElement());
1382: }
1383: return s;
1384: }
1385:
1386: /**
1387: * Get a sorted list of the system transliterators.
1388: */
1389: /*
1390: private static Vector getSystemTransliteratorNames() {
1391: Vector v = new Vector();
1392: for (Enumeration e=Transliterator.getAvailableIDs();
1393: e.hasMoreElements(); ) {
1394: v.addElement(e.nextElement());
1395: }
1396: // Insertion sort, O(n^2) acceptable for small n
1397: for (int i=0; i<(v.size()-1); ++i) {
1398: String a = (String) v.elementAt(i);
1399: for (int j=i+1; j<v.size(); ++j) {
1400: String b = (String) v.elementAt(j);
1401: if (a.compareTo(b) > 0) {
1402: v.setElementAt(b, i);
1403: v.setElementAt(a, j);
1404: a = b;
1405: }
1406: }
1407: }
1408: return v;
1409: }
1410: */
1411:
1412: /*
1413: private void setNoTransliterator() {
1414: translitItem = noTranslitItem;
1415: noTranslitItem.setState(true);
1416: handleSetTransliterator(noTranslitItem.getLabel());
1417: compound = false;
1418: for (int i=0; i<translitMenu.getItemCount(); ++i) {
1419: MenuItem it = translitMenu.getItem(i);
1420: if (it != noTranslitItem && it instanceof CheckboxMenuItem) {
1421: ((CheckboxMenuItem) it).setState(false);
1422: }
1423: }
1424: }
1425: */
1426: /*
1427: private void handleAddToCompound(String name) {
1428: if (compoundCount < MAX_COMPOUND) {
1429: compoundTranslit[compoundCount] = decodeTranslitItem(name);
1430: ++compoundCount;
1431: Transliterator t[] = new Transliterator[compoundCount];
1432: System.arraycopy(compoundTranslit, 0, t, 0, compoundCount);
1433: translit = new CompoundTransliterator(t);
1434: text.setTransliterator(translit);
1435: }
1436: }
1437: */
1438: /*
1439: private void handleSetTransliterator(String name) {
1440: translit = decodeTranslitItem(name);
1441: text.setTransliterator(translit);
1442: }
1443: */
1444:
1445: /**
1446: * Decode a menu item that looks like <translit name>.
1447: */
1448: /*
1449: private static Transliterator decodeTranslitItem(String name) {
1450: return (name.equals(NO_TRANSLITERATOR))
1451: ? null : Transliterator.getInstance(name);
1452: }
1453: */
1454:
1455: private void handleBatchTransliterate(Transliterator translit) {
1456: if (translit == null) {
1457: return;
1458: }
1459:
1460: int start = text.getSelectionStart();
1461: int end = text.getSelectionEnd();
1462: ReplaceableString s = new ReplaceableString(text.getText()
1463: .substring(start, end));
1464:
1465: StringBuffer log = null;
1466: if (DEBUG) {
1467: log = new StringBuffer();
1468: log.append('"' + s.toString() + "\" (start " + start
1469: + ", end " + end + ") -> \"");
1470: }
1471:
1472: translit.transliterate(s);
1473: String str = s.toString();
1474:
1475: if (DEBUG) {
1476: log.append(str + "\"");
1477: System.out.println("Batch " + translit.getID() + ": "
1478: + log.toString());
1479: }
1480:
1481: text.replaceRange(str, start, end);
1482: text.select(start, start + str.length());
1483: }
1484:
1485: private void handleClose() {
1486: helpDialog.dispose();
1487: dispose();
1488: }
1489:
1490: /*
1491: class InfoDialog extends Dialog {
1492: protected Button button;
1493: protected TextArea area;
1494: protected Dialog me;
1495: protected Panel bottom;
1496:
1497: public TextArea getArea() {
1498: return area;
1499: }
1500:
1501: public Panel getBottom() {
1502: return bottom;
1503: }
1504:
1505: InfoDialog(Frame parent, String title, String label, String message) {
1506: super(parent, title, false);
1507: me = this;
1508: this.setLayout(new BorderLayout());
1509: if (label.length() != 0) {
1510: this.add("North", new Label(label));
1511: }
1512:
1513: area = new TextArea(message, 8, 80, TextArea.SCROLLBARS_VERTICAL_ONLY);
1514: this.add("Center", area);
1515:
1516: button = new Button("Hide");
1517: button.addActionListener(new ActionListener() {
1518: public void actionPerformed(ActionEvent e) {
1519: me.hide();
1520: }
1521: });
1522: bottom = new Panel();
1523: bottom.setLayout(new FlowLayout(FlowLayout.CENTER, 0, 0));
1524: bottom.add(button);
1525: this.add("South", bottom);
1526: this.pack();
1527: addWindowListener(new WindowAdapter() {
1528: public void windowClosing(WindowEvent e) {
1529: me.hide();
1530: }
1531: });
1532: }
1533: }
1534: */
1535: }
|