package org.crosswire.mss.transcript; import java.io.Reader; import java.io.UnsupportedEncodingException; import java.io.File; import java.io.FileReader; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.SAXException; import org.xml.sax.SAXParseException; public class Convert { static char symbolGreekTranslation[] = new char[] { /* 000 (00) */ 0x0000 /*' '*/, 0x0001 /*''*/, 0x0002 /*''*/, 0x0003 /*''*/, /* 004 (04) */ 0x0004 /*''*/, 0x0005 /*''*/, 0x0006 /*''*/, 0x0007 /*''*/, /* 008 (08) */ ' ' /*'backspace? '*/, 0x0009 /*'\t'*/, 0x000a /*\n*/, 0x000b /*' '*/, /* 012 (0c) */ 0x000c /*' '*/, 0x000d /*'\r'*/, 0x000e /*''*/, 0x000f /*''*/, /* 016 (10) */ 0x0010 /*''*/, 0x0011 /*''*/, 0x0012 /*''*/, 0x0013 /*''*/, /* 020 (14) */ 0x0014 /*''*/, 0x0015 /*''*/, 0x0016 /*''*/, 0x0017 /*''*/, /* 024 (18) */ 0x0018 /*''*/, 0x0019 /*''*/, 0x001a /*''*/, 0x001b /*''*/, /* 028 (1c) */ 0x001c /*''*/, 0x001d /*''*/, 0x001e /*''*/, 0x001f /*''*/, /* 032 (20) */ 0x0020 /*' '*/, 0x0021 /*'!'*/, 0x03C2 /*'"'*/, 0x0023 /*'#'*/, /* 036 (24) */ 0x0024 /*'$'*/, 0x0025 /*'%'*/, 0x0026 /*'&'*/, 0x0027 /*'''*/, /* 040 (28) */ 0x0028 /*'('*/, 0x0029 /*')'*/, 0x002a /*'*'*/, 0x002b /*'+'*/, /* 044 (2c) */ 0x002c /*','*/, 0x002d /*'-'*/, 0x002e /*'.'*/, 0x002f /*'/'*/, /* 048 (30) */ 0x0030 /*'0'*/, 0x0031 /*'1'*/, 0x0032 /*'2'*/, 0x0033 /*'3'*/, /* 052 (34) */ 0x0034 /*'4'*/, 0x0035 /*'5'*/, 0x0036 /*'6'*/, 0x0037 /*'7'*/, /* 056 (38) */ 0x0038 /*'8'*/, 0x0039 /*'9'*/, 0x003a /*':'*/, 0x003b /*';'*/, /* 060 (3c) */ 0x003c /*'<'*/, 0x003d /*'='*/, 0x003e /*'>'*/, 0x003f /*'?'*/, /* 064 (40) */ 0x0040 /*'@'*/, 0x0391 /*'A'*/, 0x0392 /*'B'*/, 0x03A7 /*'C'*/, /* 068 (44) */ 0x0394 /*'D'*/, 0x0395 /*'E'*/, 0x0396 /*'F'*/, 0x0393 /*'G'*/, /* 072 (48) */ 0x0397 /*'H'*/, 0x0399 /*'I'*/, 0x004a /*'J'*/, 0x039a /*'K' bug in .py */, /* 076 (4c) */ 0x039b /*'L'*/, 0x039c /*'M'*/, 0x039d /*'N'*/, 0x039f /*'O'*/, /* 080 (50) */ 0x03a0 /*'P'*/, 0x0051 /*'Q'*/, 0x03a1 /*'R'*/, 0x03a3 /*'S'*/, /* 084 (54) */ 0x03a4 /*'T'*/, 0x03a5 /*'U'*/, 0x0384 /*'V'*/, 0x03a9 /*'W'*/, /* 088 (58) */ 0x03a7 /*'X'*/, 0x0059 /*'Y'*/, 0x005a /*'Z'*/, 0x005b /*'['*/, /* 092 (5c) */ 0x005c /*'\'*/, 0x005d /*']'*/, 0x005e /*'^'*/, 0x005f /*'_'*/, /* 096 (60) */ 0x0060 /*'`'*/, 0x03b1 /*'a'*/, 0x03b2 /*'b'*/, 0x03c7 /*'c'*/, /* 100 (64) */ 0x03b4 /*'d'*/, 0x03b5 /*'e'*/, 0x03c6 /*'f'*/, 0x03b3 /*'g'*/, /* 104 (68) */ 0x03b7 /*'h'*/, 0x03b9 /*'i'*/, 0x006a /*'j'*/, 0x03ba /*'k'*/, /* 108 (6c) */ 0x03bb /*'l'*/, 0x03bc /*'m'*/, 0x03bd /*'n'*/, 0x03bf /*'o'*/, /* 112 (70) */ 0x03c0 /*'p'*/, 0x03b8 /*'q'*/, 0x03c1 /*'r'*/, 0x03c3 /*'s'*/, /* 116 (74) */ 0x03c4 /*'t'*/, 0x03c5 /*'u'*/, 0x0076 /*'v'*/, 0x03c9 /*'w'*/, /* 120 (78) */ 0x03be /*'x'*/, 0x03c8 /*'y'*/, 0x03b6 /*'z'*/, 0x007b /*'{'*/, /* 124 (7c) */ 0x007c /*'|'*/, 0x007d /*'}'*/, 0x03c2 /*'~'*/, 0x007f /*''*/, /* 128 (80) */ ' ' /*'�'*/, 0x0081 /*'�'*/, 0x006c /*'�'*/, 0x0083 /*'�'*/, /* 132 (84) */ 0x0084 /*'�'*/, 0x003e /* blockend '�'*/, 0x1fbd /*elusionszeichen'�'*/, 0x0087 /*'�'*/, /* 136 (88) */ 0x0088 /*'�'*/, 0x003b /*; '�'*/, ' ' /*'�'*/, 0x008b /*'�'*/, /* 140 (8c) */ 0x008c /*'�'*/, 0x008d /*'�'*/, 0x008e /*'�'*/, 0x008f /*'�'*/, /* 144 (90) */ 0x0090 /*'�'*/, 0x0091 /*'�'*/, 0x0092 /*'�'*/, 0x0093 /*'�'*/, /* 148 (94) */ 0x0094 /*'�'*/, 0x0095 /*'�'*/, 0x0096 /*'�'*/, 0x0056 /*'�'*/, /* 152 (98) */ 0x0098 /*'�'*/, 0x0099 /*'�'*/, 0x002f /* / '�'*/, 0x009b /*'�'*/, /* 156 (9c) */ 0x009c /*'�'*/, 0x009d /*'�'*/, 0x009e /*'�'*/, 0x009f /*'�'*/, /* 160 (a0) */ 0x00a0 /*'�'*/, 0x00a1 /*'�'*/, 0x00a2 /*'�'*/, 0x00a3 /*'�'*/, /* 164 (a4) */ 0x00a4 /*'�'*/, 0x00a5 /*'�'*/, 0x00a6 /*'�'*/, 0x00a7 /*'�'*/, /* 168 (a8) */ 0x00a8 /*'�'*/, 0x00a9 /*'�'*/, 0x00aa /*'�'*/, 0x0044 /* D '�'*/, /* 172 (ac) */ 0x0046 /* F '�'*/, 0x0047 /* G '�'*/, 0x00ae /*'�'*/, 0x00af /*'�'*/, /* 176 (b0) */ 0x00b0 /*'�'*/, 0x00b1 /*'�'*/, 0x00b2 /*'�'*/, 0x0323 /*unterpunkt'�'*/, /* 180 (b4) */ 0x00b4 /*'�'*/, 0x00b5 /*'�'*/, 0x00b6 /*'�'*/, 0x00b7 /*'�'*/, /* 184 (b8) */ 0x00b8 /*'�'*/, 0x00b9 /*'�'*/, 0x00ba /*'�'*/, 0x005b /* [ '�'*/, /* 188 (bc) */ 0x005d /*]'�'*/, 0x00bd /*'�'*/, 0x00be /*'�'*/, 0x00bf /*'�'*/, /* 192 (c0) */ 0x00c0 /*'�'*/, 0x00B0 /* degree '�'*/, 0x006c /*'�'*/, ' ' /*'�'*/, /* 196 (c4) */ 0x00c4 /*'�'*/, 0x00c5 /*'�'*/, 0x00c6 /*'�'*/, 0x00c7 /*'�'*/, /* 200 (c8) */ 0x00c8 /*'�'*/, 0x00c9 /*'�'*/, 0x00ca /*'�'*/, 0x00cb /*'�'*/, /* 204 (cc) */ 0x00cc /*'�'*/, 0x00cd /*'�'*/, 0x00ce /*'�'*/, 0x00cf /*'�'*/, /* 208 (d0) */ 0x00d0 /*'�'*/, 0x00af /*oberstrich'�'*/, 0x00d2 /*'�'*/, 0x00d3 /*'�'*/, /* 212 (d4) */ 0x00d4 /*'�'*/, 0x00d5 /*'�'*/, 0x00d6 /*'�'*/, 0x0056 /* V '�'*/, /* 216 (d8) */ 0x00d8 /*'�'*/, 0x00d9 /*'�'*/, 0x007c /*|'�'*/, 0x0060 /*`'�'*/, /* 220 (dc) */ 0x00dc /*'�'*/, 0x00dd /*'�'*/, 0x00de /*'�'*/, 0x00df /*'�'*/, /* 224 (e0) */ 0x00e0 /*'�'*/, 0x00e1 /*'�'*/, ' ' /*'�'*/, 0x00e3 /*'�'*/, /* 228 (e4) */ 0x00e4 /*'�'*/, 0x00e5 /*'�'*/, 0x00e6 /*'�'*/, 0x00e7 /*'�'*/, /* 232 (e8) */ 0x00e8 /*'�'*/, 0x00e9 /*'�'*/, 0x00ea /*'�'*/, 0x00eb /*'�'*/, /* 236 (ec) */ 0x0046 /* F '�'*/, 0x00ed /*'�'*/, 0x00ee /*'�'*/, ' ' /*'�'*/, /* 240 (f0) */ 0x00f0 /*'�'*/, 0x00f1 /*'�'*/, 0x00f2 /*'�'*/, 0x00f3 /*'�'*/, /* 244 (f4) */ 0x00f4 /*'�'*/, 0x00f5 /*'�'*/, 0x00f6 /*'�'*/, 0x00f7 /*'�'*/, /* 248 (f8) */ 0x00f8 /*'�'*/, 0x00f9 /*'�'*/, 0x00fa /*'�'*/, 0x00fb /*'�'*/, /* 252 (fc) */ 0x00fc /*'�'*/, 0x00fd /*'�'*/, 0x00fe /*'�'*/, 0x00ff /*'?'*/ }; public static String translate(char c) { return new String(new char[] { (c<= 0xff)?symbolGreekTranslation[c]:c }); } public static String translate(String s) { StringBuffer out = new StringBuffer(); try { byte b[] = s.getBytes("iso8859-1"); for (int i = 0; i < b.length; ++i) { char c = (char)((b[i] < 0)?b[i]+256 : b[i]); out.append((c<= 0xff)?symbolGreekTranslation[c]:c); if (c == 10 || c == 13) { // reset } } } catch (UnsupportedEncodingException e) { e.printStackTrace(); } return out.toString(); } public static StringBuffer processWitness(Reader reader, int book, int chap) throws IOException, SAXException, ParserConfigurationException { StringBuffer retVal = new StringBuffer(); String pertinentContext = "B"+Integer.toString(book)+"K"+Integer.toString(chap)+"V"; // Create a JAXP "parser factory" for creating SAX parsers javax.xml.parsers.SAXParserFactory spf = SAXParserFactory.newInstance(); // Configure the parser factory for the type of parsers we require spf.setValidating(false); // No validation required // Now use the parser factory to create a SAXParser object // Note that SAXParser is a JAXP class, not a SAX class javax.xml.parsers.SAXParser sp = spf.newSAXParser(); // Create a SAX input source for the file argument org.xml.sax.InputSource input = new InputSource(reader); // Give the InputSource an absolute URL for the file, so that // it can resolve relative URLs in a declaration, e.g. // input.setSystemId("file://" + new File(args[0]).getAbsolutePath()); // Create an instance of this class; it defines all the handler methods SAXDemo handler = new SAXDemo(retVal, pertinentContext); // Finally, tell the parser to parse the input and notify the handler sp.parse(input, handler); // Instead of using the SAXParser.parse() method, which is part of the // JAXP API, we could also use the SAX1 API directly. Note the // difference between the JAXP class javax.xml.parsers.SAXParser and // the SAX1 class org.xml.sax.Parser // // org.xml.sax.Parser parser = sp.getParser(); // Get the SAX parser // parser.setDocumentHandler(handler); // Set main handler // parser.setErrorHandler(handler); // Set error handler // parser.parse(input); // Parse! return retVal; } /** * * This class implements the HandlerBase helper class, which means that it * defines all the "callback" methods that the SAX parser will invoke to notify * the application. In this example we override the methods that we require. * * This example uses full package names in places to help keep the JAXP and SAX * APIs distinct. */ static public class SAXDemo extends org.xml.sax.helpers.DefaultHandler { /** The main method sets things up for parsing */ StringBuffer result = new StringBuffer(); String pertinentContext = ""; boolean process = false; public SAXDemo(StringBuffer result, String pertinentContext) { this.result = result; this.pertinentContext = pertinentContext; } StringBuffer accumulator = new StringBuffer(); // Accumulate parsed text // When the parser encounters plain text (not XML elements), it calls // this method, which accumulates them in a string buffer public void characters(char[] buffer, int start, int length) { accumulator.append(buffer, start, length); } // Every time the parser encounters the beginning of a new element, it // calls this method, which resets the string buffer public void startElement (String uri, String localName, String qName, Attributes attributes) throws SAXException { accumulator.setLength(0); // Ready to accumulate new text // If its a servlet tag, look for id attribute if (qName.equals("ab")) { String context = attributes.getValue("xml:id"); process = (context != null) && context.startsWith(pertinentContext); if (process) result.append('\n'); } } public void endElement (String uri, String localName, String qName) throws SAXException { if (process && qName.equals("w")) { result.append(accumulator); result.append(' '); } } /** This method is called when warnings occur */ public void warning(SAXParseException exception) { System.err.println("WARNING: line " + exception.getLineNumber() + ": " + exception.getMessage()); } /** This method is called when errors occur */ public void error(SAXParseException exception) { System.err.println("ERROR: line " + exception.getLineNumber() + ": " + exception.getMessage()); } /** This method is called when non-recoverable errors occur. */ public void fatalError(SAXParseException exception) throws SAXException { System.err.println("FATAL: line " + exception.getLineNumber() + ": " + exception.getMessage()); throw (exception); } } }