From aedef4b44d4577fd13cef042194923c68483fbd6 Mon Sep 17 00:00:00 2001 From: dchandler Date: Sun, 29 Jun 2003 21:31:48 +0000 Subject: [PATCH] An error now appears if you try to convert from format A to format B but no glyphs in format A appear. In this case, it is likely that you meant to convert a different file or do a different conversion. --- source/org/thdl/tib/input/ConverterGUI.java | 6 ++ source/org/thdl/tib/input/DuffPane.java | 12 ++-- source/org/thdl/tib/input/Jskad.java | 57 ++++++++++++++----- .../org/thdl/tib/input/TibetanConverter.java | 20 +++++-- source/org/thdl/tib/text/TibetanDocument.java | 43 ++++++++++---- 5 files changed, 105 insertions(+), 33 deletions(-) diff --git a/source/org/thdl/tib/input/ConverterGUI.java b/source/org/thdl/tib/input/ConverterGUI.java index 7ccf5b3..69051c9 100644 --- a/source/org/thdl/tib/input/ConverterGUI.java +++ b/source/org/thdl/tib/input/ConverterGUI.java @@ -67,6 +67,12 @@ public class ConverterGUI implements FontConversion, FontConverterConstants { "Conversion failed", JOptionPane.ERROR_MESSAGE); return false; + } else if (43 == returnCode) { + JOptionPane.showMessageDialog(cd, + "Though an output file has been created, this conversion did nothing.\nDid you choose the correct original file?\nDid you choose the correct type of conversion?", + "Nothing to do", + JOptionPane.ERROR_MESSAGE); + return false; } else if (0 != returnCode) { JOptionPane.showMessageDialog(cd, "The conversion failed with code " + returnCode + "; please e-mail\ndchandler@users.sourceforge.net to learn what that means if\nyou can't find out from the output.", diff --git a/source/org/thdl/tib/input/DuffPane.java b/source/org/thdl/tib/input/DuffPane.java index d02fc1e..3d943ca 100644 --- a/source/org/thdl/tib/input/DuffPane.java +++ b/source/org/thdl/tib/input/DuffPane.java @@ -1489,8 +1489,8 @@ public void paste(int offset) { public void toWylie() { int start = getSelectionStart(); int end = getSelectionEnd(); - - toWylie(start, end); + long n[] = new long[] { 0 }; + toWylie(start, end, n); } /** @@ -1499,8 +1499,11 @@ public void paste(int offset) { * * @param start the point from which to begin converting to Wylie * @param end the point at which to stop converting to Wylie -*/ - public void toWylie(int start, int end) { +* @param numAttemptedReplacements an array that contains one element; +* this first element will be, upon exit, incremented by the number of +* TMW glyphs that we encountered and attempted to convert to Wylie */ + public void toWylie(int start, int end, + long numAttemptedReplacements[]) { if (start == end) return; @@ -1529,6 +1532,7 @@ public void paste(int offset) { } else { char ch = getTibDoc().getText(i,1).charAt(0); dcs.add(new DuffCode(fontNum, ch)); + ++numAttemptedReplacements[0]; } i++; diff --git a/source/org/thdl/tib/input/Jskad.java b/source/org/thdl/tib/input/Jskad.java index d1d411c..01a2625 100644 --- a/source/org/thdl/tib/input/Jskad.java +++ b/source/org/thdl/tib/input/Jskad.java @@ -374,8 +374,10 @@ public class Jskad extends JPanel implements DocumentListener { toTMItem.addActionListener(new ThdlActionListener() { public void theRealActionPerformed(ActionEvent e) { StringBuffer errors = new StringBuffer(); + long numAttemptedReplacements[] = new long[] { 0 }; boolean errorReturn - = ((TibetanDocument)dp.getDocument()).convertToTM(0, -1, errors); // entire document + = ((TibetanDocument)dp.getDocument()).convertToTM(0, -1, errors, + numAttemptedReplacements); // entire document if (errorReturn) { JOptionPane.showMessageDialog(Jskad.this, "At least one error occurred while converting Tibetan Machine Web\nto Tibetan Machine. Your document is mostly converted,\nexcept for the following glyphs, which you should replace manually\nbefore retrying:\n" @@ -383,9 +385,17 @@ public class Jskad extends JPanel implements DocumentListener { "TMW to TM Errors", JOptionPane.PLAIN_MESSAGE); } else { - JOptionPane.showMessageDialog(Jskad.this, - "Converting Tibetan Machine Web to Tibetan Machine met with perfect success.", - "Success", JOptionPane.PLAIN_MESSAGE); + if (numAttemptedReplacements[0] > 0) { + JOptionPane.showMessageDialog(Jskad.this, + "Converting Tibetan Machine Web to Tibetan Machine met with perfect success.", + "Success", + JOptionPane.PLAIN_MESSAGE); + } else { + JOptionPane.showMessageDialog(Jskad.this, + "No Tibetan Machine Web was found, so nothing was converted.", + "Nothing to do", + JOptionPane.ERROR_MESSAGE); + } } } }); @@ -394,17 +404,27 @@ public class Jskad extends JPanel implements DocumentListener { toTMWItem.addActionListener(new ThdlActionListener() { public void theRealActionPerformed(ActionEvent e) { StringBuffer errors = new StringBuffer(); + long numAttemptedReplacements[] = new long[] { 0 }; boolean errorReturn - = ((TibetanDocument)dp.getDocument()).convertToTMW(0, -1, errors); // entire document + = ((TibetanDocument)dp.getDocument()).convertToTMW(0, -1, errors, + numAttemptedReplacements); // entire document if (errorReturn) { JOptionPane.showMessageDialog(Jskad.this, "At least one error occurred while converting Tibetan Machine\nto Tibetan Machine Web. Your document is mostly converted,\nexcept for the following glyphs, which you should replace manually\nbefore retrying:\n" + errors.toString(), "TM to TMW Errors", JOptionPane.PLAIN_MESSAGE); } else { - JOptionPane.showMessageDialog(Jskad.this, - "Converting Tibetan Machine to Tibetan Machine Web met with perfect success.", - "Success", JOptionPane.PLAIN_MESSAGE); + if (numAttemptedReplacements[0] > 0) { + JOptionPane.showMessageDialog(Jskad.this, + "Converting Tibetan Machine to Tibetan Machine Web met with perfect success.", + "Success", + JOptionPane.PLAIN_MESSAGE); + } else { + JOptionPane.showMessageDialog(Jskad.this, + "No Tibetan Machine was found, so nothing was converted.", + "Nothing to do", + JOptionPane.ERROR_MESSAGE); + } } } }); @@ -413,18 +433,28 @@ public class Jskad extends JPanel implements DocumentListener { toUnicodeItem.addActionListener(new ThdlActionListener() { public void theRealActionPerformed(ActionEvent e) { StringBuffer errors = new StringBuffer(); + long numAttemptedReplacements[] = new long[] { 0 }; boolean errorReturn = ((TibetanDocument)dp.getDocument()).convertToUnicode(0, -1, errors, - ThdlOptions.getStringOption("thdl.tmw.to.unicode.font").intern()); // entire document + ThdlOptions.getStringOption("thdl.tmw.to.unicode.font").intern(), + numAttemptedReplacements); // entire document if (errorReturn) { JOptionPane.showMessageDialog(Jskad.this, "At least one error occurred while converting Tibetan Machine Web\nto Unicode. Your document is mostly converted,\nexcept for the following glyphs, which you should replace manually\nbefore retrying:\n" + errors.toString(), "TMW to Unicode Errors", JOptionPane.PLAIN_MESSAGE); } else { - JOptionPane.showMessageDialog(Jskad.this, - "Converting Tibetan Machine Web to Unicode met with perfect success.", - "Success", JOptionPane.PLAIN_MESSAGE); + if (numAttemptedReplacements[0] > 0) { + JOptionPane.showMessageDialog(Jskad.this, + "Converting Tibetan Machine Web to Unicode met with perfect success.", + "Success", + JOptionPane.PLAIN_MESSAGE); + } else { + JOptionPane.showMessageDialog(Jskad.this, + "No Tibetan Machine Web was found, so nothing was converted.", + "Nothing to do", + JOptionPane.ERROR_MESSAGE); + } } } }); @@ -1032,7 +1062,8 @@ public class Jskad extends JPanel implements DocumentListener { private void toWylie() { Jskad.this.setCursor(Cursor.getPredefinedCursor(Cursor.WAIT_CURSOR)); - dp.toWylie(dp.getSelectionStart(), dp.getSelectionEnd()); + long n[] = new long[] { 0 }; + dp.toWylie(dp.getSelectionStart(), dp.getSelectionEnd(), n); Jskad.this.setCursor(Cursor.getPredefinedCursor(Cursor.DEFAULT_CURSOR)); } diff --git a/source/org/thdl/tib/input/TibetanConverter.java b/source/org/thdl/tib/input/TibetanConverter.java index 8084435..cc75d54 100644 --- a/source/org/thdl/tib/input/TibetanConverter.java +++ b/source/org/thdl/tib/input/TibetanConverter.java @@ -117,13 +117,14 @@ public class TibetanConverter implements FontConverterConstants { out.println(" result to standard output (after dealing with the curly brace problem if"); out.println(" the input is TibetanMachineWeb). Exit code is zero on success, 42 if some"); out.println(" glyphs couldn't be converted (in which case the output is just those glyphs),"); + out.println(" 43 if not even one glyph found was eligible for this conversion, which means"); + out.println(" that you probably selected the wrong conversion or the wrong document, or "); out.println(" nonzero otherwise."); out.println(""); out.println(" You may find it helpful to use `--find-some-non-tmw' mode (or"); out.println(" `--find-some-non-tm' mode for Tibetan Machine input) before doing a"); out.println(" conversion so that you have confidence in the conversion's correctness."); // DLC add Wylie->TMW mode. - // DLC give error if you have a TM file and try TMW->Unicode. return 77; } if (args[0].equals("--version") || args[0].equals("-v")) { @@ -237,30 +238,34 @@ public class TibetanConverter implements FontConverterConstants { ((TibetanDocument)dp.getDocument()).replaceTahomaCurlyBracesAndBackslashes(0, -1); if (debug) System.err.println("End : solving curly brace problem"); } - + int exitCode = 0; ThdlDebug.verify(((TMW_TO_TM == ct) ? 1 : 0) + ((TMW_TO_UNI == ct) ? 1 : 0) + ((TM_TO_TMW == ct) ? 1 : 0) + ((TMW_TO_WYLIE == ct) ? 1 : 0) == 1); + long numAttemptedReplacements[] = new long[] { 0 }; if (TMW_TO_WYLIE == ct) { // Convert to THDL Wylie: - dp.toWylie(0, dp.getDocument().getLength()); + dp.toWylie(0, dp.getDocument().getLength(), + numAttemptedReplacements); } else if (TMW_TO_UNI == ct) { StringBuffer errors = new StringBuffer(); // Convert to Unicode: if (((TibetanDocument)dp.getDocument()).convertToUnicode(0, dp.getDocument().getLength(), errors, - ThdlOptions.getStringOption("thdl.tmw.to.unicode.font").intern())) { + ThdlOptions.getStringOption("thdl.tmw.to.unicode.font").intern(), + numAttemptedReplacements)) { System.err.println(errors); exitCode = 42; } } else if (TM_TO_TMW == ct) { StringBuffer errors = new StringBuffer(); // Convert to TibetanMachineWeb: - if (((TibetanDocument)dp.getDocument()).convertToTMW(0, dp.getDocument().getLength(), errors)) { + if (((TibetanDocument)dp.getDocument()).convertToTMW(0, dp.getDocument().getLength(), errors, + numAttemptedReplacements)) { System.err.println(errors); exitCode = 42; } @@ -268,7 +273,8 @@ public class TibetanConverter implements FontConverterConstants { ThdlDebug.verify(TMW_TO_TM == ct); StringBuffer errors = new StringBuffer(); // Convert to TibetanMachine: - if (((TibetanDocument)dp.getDocument()).convertToTM(0, dp.getDocument().getLength(), errors)) { + if (((TibetanDocument)dp.getDocument()).convertToTM(0, dp.getDocument().getLength(), errors, + numAttemptedReplacements)) { System.err.println(errors); exitCode = 42; } @@ -282,6 +288,8 @@ public class TibetanConverter implements FontConverterConstants { } if (out.checkError()) exitCode = 41; + if (numAttemptedReplacements[0] < 1) + exitCode = 43; return exitCode; } diff --git a/source/org/thdl/tib/text/TibetanDocument.java b/source/org/thdl/tib/text/TibetanDocument.java index 2526bfb..8ccf5c8 100644 --- a/source/org/thdl/tib/text/TibetanDocument.java +++ b/source/org/thdl/tib/text/TibetanDocument.java @@ -10,7 +10,7 @@ License for the specific terms governing rights and limitations under the License. The Initial Developer of this software is the Tibetan and Himalayan Digital -Library (THDL). Portions created by the THDL are Copyright 2001 THDL. +Library (THDL). Portions created by the THDL are Copyright 2001-2003 THDL. All Rights Reserved. Contributor(s): ______________________________________. @@ -523,9 +523,15 @@ public class TibetanDocument extends DefaultStyledDocument { was encountered @param errors if non-null, then notes about all exceptional cases will be appended to this StringBuffer + @param numAttemptedReplacements an array that contains one + element; this first element will be, upon exit, incremented by + the number of TMW glyphs that we encountered and attempted to + convert to TM */ - public boolean convertToTM(int begin, int end, StringBuffer errors) { - return convertHelper(begin, end, true, false, errors, null); + public boolean convertToTM(int begin, int end, StringBuffer errors, + long numAttemptedReplacements[]) { + return convertHelper(begin, end, true, false, errors, null, + numAttemptedReplacements); } /** Converts all TibetanMachine glyphs in the document to @@ -539,9 +545,15 @@ public class TibetanDocument extends DefaultStyledDocument { was encountered @param errors if non-null, then notes about all exceptional cases will be appended to this StringBuffer + @param numAttemptedReplacements an array that contains one + element; this first element will be, upon exit, incremented by + the number of TM glyphs that we encountered and attempted to + convert to TMW */ - public boolean convertToTMW(int begin, int end, StringBuffer errors) { - return convertHelper(begin, end, false, false, errors, null); + public boolean convertToTMW(int begin, int end, StringBuffer errors, + long numAttemptedReplacements[]) { + return convertHelper(begin, end, false, false, errors, null, + numAttemptedReplacements); } /** Converts all TibetanMachineWeb glyphs in the document to @@ -557,10 +569,16 @@ public class TibetanDocument extends DefaultStyledDocument { cases will be appended to this StringBuffer @param unicodeFont the name of the Unicode font to use; defaults to Ximalaya if null + @param numAttemptedReplacements an array that contains one + element; this first element will be, upon exit, incremented by + the number of TMW glyphs that we encountered and attempted to + convert to Unicode */ public boolean convertToUnicode(int begin, int end, StringBuffer errors, - String unicodeFont) { - return convertHelper(begin, end, false, true, errors, unicodeFont); + String unicodeFont, + long numAttemptedReplacements[]) { + return convertHelper(begin, end, false, true, errors, unicodeFont, + numAttemptedReplacements); } /** For debugging only. Start with an empty document, and call @@ -708,7 +726,8 @@ public class TibetanDocument extends DefaultStyledDocument { @see #convertToTM(int,int,StringBuffer) */ private boolean convertHelper(int begin, int end, boolean toTM, boolean toUnicode, StringBuffer errors, - String unicodeFont) { + String unicodeFont, + long numAttemptedReplacements[]) { // To preserve formatting, we go paragraph by paragraph. // Use positions, not offsets, because our work on paragraph K @@ -730,6 +749,7 @@ public class TibetanDocument extends DefaultStyledDocument { boolean warn = false; int lastTimeWeExamined = -1; // must be -1 boolean noMore = false; + while (!noMore && lastTimeWeExamined != ceh.lastOffsetExamined) { lastTimeWeExamined = ceh.lastOffsetExamined; @@ -745,7 +765,8 @@ public class TibetanDocument extends DefaultStyledDocument { ? finalEndPos.getOffset() : p_end), toTM, toUnicode, errors, ceh, - unicodeFont); + unicodeFont, + numAttemptedReplacements); } if (!ceh.errorReturn && pl != getParagraphs(begin, finalEndPos.getOffset()).length) { @@ -829,7 +850,8 @@ public class TibetanDocument extends DefaultStyledDocument { private void convertHelperHelper(int begin, int end, boolean toTM, boolean toUnicode, StringBuffer errors, ConversionErrorHelper ceh, - String unicodeFont) { + String unicodeFont, + long numAttemptedReplacements[]) { final boolean debug = false; if (debug) System.out.println("cHH: [" + begin + ", " + end + ")"); @@ -880,6 +902,7 @@ public class TibetanDocument extends DefaultStyledDocument { : TibetanMachineWeb.getTMFontNumber(fontName)); if (0 != fontNum) { + ++numAttemptedReplacements[0]; // SPEED_FIXME: determining font size might be slow, allow an override. int fontSize = tibetanFontSize;