Index: swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParser.java =================================================================== --- swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParser.java (revision 676572) +++ swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParser.java Mon Jul 14 17:00:46 EEST 2008 @@ -2,13 +2,22 @@ package org.apache.harmony.x.swing.text.rtf; import java.io.*; -import javax.swing.text.Document; +import java.util.Stack; import javax.swing.text.DefaultStyledDocument; public class RTFParser implements RTFParserConstants { - private static RTFParserHandler handler; + private RTFHandler handler; + private String encoding = RTFEncodings.DEFAULT_ENCODING; + + private int ucValue = 1; + /** + * This stack contains only UC values as a scope parameter. + * It could be modified to contain additional parameters. + */ + private Stack scopeStack = new Stack(); + public static void main(String args[]) throws Exception { InputStream in; @@ -18,16 +27,27 @@ in = System.in; RTFParser parser = new RTFParser(in); - parser.parse(new DefaultStyledDocument(), 0); + RTFHandler handler = new DocumentRTFHandler(new DefaultStyledDocument(), 0); + parser.parse(handler); } - static final public void parse(Document doc, int position) throws ParseException { - handler = new RTFParserHandler(doc, position); + private byte[] getBytes(String str) { + char[] chars = str.toCharArray(); + byte[] bytes = new byte[chars.length]; + for (int i = 0; i < chars.length; i++) + bytes[i] = (byte) chars[i]; + return bytes; + } + + final public void parse(RTFHandler handler) throws ParseException { + if (handler == null) + {if (true) throw new NullPointerException("Parameter handler cannot be null");} + this.handler = handler; file(); jj_consume_token(0); } - static final private int parameter() throws ParseException { + final private int parameter() throws ParseException { Token param = null; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case PARAM: @@ -37,11 +57,11 @@ jj_la1[0] = jj_gen; ; } - {if (true) return param == null ? -1 : Integer.parseInt(param.image);} + {if (true) return param == null ? -1 : Integer.parseInt(param.image.trim());} throw new Error("Missing return statement in function"); } - static final private void unknownControlWord() throws ParseException { + final private void unknownControlWord() throws ParseException { jj_consume_token(CONTROL_WORD); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case PARAM: @@ -54,175 +74,335 @@ } /** - * Catches all unhandled control symbols. + * Catches all unhandled control symbols. */ - static final public void unknownControlSymbol() throws ParseException { + final public void unknownControlSymbol() throws ParseException { jj_consume_token(CONTROL_SYMBOL); } - static final public void text() throws ParseException { - Token text; - text = jj_consume_token(TEXT); - handler.addText(text.image); - } - - static final public void file() throws ParseException { +/** + * Group which starts with "{\*" and describes destination, currently this part + * is ignored. + */ + final public void ignoredDestination() throws ParseException { jj_consume_token(OPEN_BRACE); - header(); - document(); + jj_consume_token(IGNORED_DESTINATION); + ignoredBlock(); jj_consume_token(CLOSE_BRACE); } - static final public void header() throws ParseException { + final public String parseText() throws ParseException { + byte[] bytes; + String text; + ByteArrayOutputStream textBytes = new ByteArrayOutputStream(); + StringBuffer parsedText = new StringBuffer(); + label_1: + while (true) { + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case HEX_CHAR: + case TEXT: + label_2: + while (true) { + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case HEX_CHAR: + bytes = parseHexBytes(); + break; + case TEXT: + bytes = parseTextBytes(); + break; + default: + jj_la1[2] = jj_gen; + jj_consume_token(-1); + throw new ParseException(); + } + try { + textBytes.write(bytes); + } catch (IOException e) {} + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case HEX_CHAR: + case TEXT: + ; + break; + default: + jj_la1[3] = jj_gen; + break label_2; + } + } + // Decoding collected characters using specified encoding + try { + parsedText.append(textBytes.toString(encoding)); + textBytes.reset(); + } + catch (UnsupportedEncodingException e) { + {if (true) throw new ParseException("Unsupported encoding");} + } + break; + case ESCAPED_OPEN_BRACE: + case ESCAPED_CLOSE_BRACE: + case ESCAPED_BACKSLASH: + case U: + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case ESCAPED_OPEN_BRACE: + case ESCAPED_CLOSE_BRACE: + case ESCAPED_BACKSLASH: + text = parseSpecialCharacter(); + break; + case U: + text = parseUnicodeText(); + break; + default: + jj_la1[4] = jj_gen; + jj_consume_token(-1); + throw new ParseException(); + } + parsedText.append(text); + break; + default: + jj_la1[5] = jj_gen; + jj_consume_token(-1); + throw new ParseException(); + } + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case ESCAPED_OPEN_BRACE: + case ESCAPED_CLOSE_BRACE: + case ESCAPED_BACKSLASH: + case HEX_CHAR: + case TEXT: + case U: + ; + break; + default: + jj_la1[6] = jj_gen; + break label_1; + } + } + {if (true) return parsedText.toString();} + throw new Error("Missing return statement in function"); + } + + final public byte[] parseHexBytes() throws ParseException { + Token token; + byte[] result = new byte[1]; + token = jj_consume_token(HEX_CHAR); + result[0] = (byte) Integer.parseInt(token.image.substring(2), 16); + {if (true) return result;} + throw new Error("Missing return statement in function"); + } + + final public byte[] parseTextBytes() throws ParseException { + Token token; + token = jj_consume_token(TEXT); + {if (true) return getBytes(token.image);} + throw new Error("Missing return statement in function"); + } + + final public String parseSpecialCharacter() throws ParseException { + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case ESCAPED_OPEN_BRACE: + jj_consume_token(ESCAPED_OPEN_BRACE); + {if (true) return "{";} + break; + case ESCAPED_CLOSE_BRACE: + jj_consume_token(ESCAPED_CLOSE_BRACE); + {if (true) return "}";} + break; + case ESCAPED_BACKSLASH: + jj_consume_token(ESCAPED_BACKSLASH); + {if (true) return "\\";} + break; + default: + jj_la1[7] = jj_gen; + jj_consume_token(-1); + throw new ParseException(); + } + throw new Error("Missing return statement in function"); + } + + final public String parseUnicodeText() throws ParseException { + int param; + StringBuffer unicodeBuffer = new StringBuffer(); + label_3: + while (true) { + jj_consume_token(U); + param = parameter(); + if (param < 0) + param += 65536; + + unicodeBuffer.append((char) param); + skipAfterUnicode(ucValue); + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case U: + ; + break; + default: + jj_la1[8] = jj_gen; + break label_3; + } + } + {if (true) return unicodeBuffer.toString();} + throw new Error("Missing return statement in function"); + } + + final public void file() throws ParseException { + jj_consume_token(OPEN_BRACE); jj_consume_token(RTF); switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case PARAM: jj_consume_token(PARAM); break; default: - jj_la1[2] = jj_gen; + jj_la1[9] = jj_gen; ; } + document(); + jj_consume_token(CLOSE_BRACE); } - static final public void document() throws ParseException { - label_1: + final public void document() throws ParseException { + boolean skipped; + String text; + label_4: while (true) { + if (jj_2_1(2)) { + header(); + } else { - switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { - case B: - case I: - case UL: + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case B: + case I: + case UL: + case PAR: - paragraph(); - break; - default: + paragraph(); + break; + default: - jj_la1[3] = jj_gen; - if (jj_2_1(2147483647)) { - fonttbl(); - } else if (jj_2_2(2147483647)) { - stylesheet(); - } else if (jj_2_3(2147483647)) { + jj_la1[10] = jj_gen; + if (jj_2_2(2147483647)) { - info(); + info(); - } else if (jj_2_4(2147483647)) { + } else if (jj_2_3(2147483647)) { - ignoredDestination(); - } else { - switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { - case OPEN_BRACE: + ignoredDestination(); + } else { + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case OPEN_BRACE: - documentBlock(); + documentGroup(); - break; - case CONTROL_WORD: - unknownControlWord(); - break; - case CONTROL_SYMBOL: - unknownControlSymbol(); - break; + break; + case CONTROL_WORD: + unknownControlWord(); + break; + case CONTROL_SYMBOL: + unknownControlSymbol(); + break; + case ESCAPED_OPEN_BRACE: + case ESCAPED_CLOSE_BRACE: + case ESCAPED_BACKSLASH: + case HEX_CHAR: - case TEXT: + case TEXT: - text(); + case U: + text = parseText(); + handler.addText(text); - break; - default: + break; + default: - jj_la1[4] = jj_gen; - jj_consume_token(-1); - throw new ParseException(); + jj_la1[11] = jj_gen; + skipped = handleUnexpectedControlWord(); + if (!skipped) {if (true) return;} - } - } - } + } + } + } - switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { - case OPEN_BRACE: - case TEXT: - case B: - case I: - case UL: - case CONTROL_WORD: - case CONTROL_SYMBOL: + } - ; + ; - break; - default: - jj_la1[5] = jj_gen; - break label_1; - } - } + } + } - } /** - * A group. + * A group. */ - static final public void documentBlock() throws ParseException { + final public void documentGroup() throws ParseException { + startGroup(); + document(); + endGroup(); + } + + final public void startGroup() throws ParseException { jj_consume_token(OPEN_BRACE); + scopeStack.push(ucValue); - handler.startGroup(); + handler.startGroup(); - document(); + } + + final public void endGroup() throws ParseException { jj_consume_token(CLOSE_BRACE); + ucValue = (Integer) scopeStack.pop(); - handler.endGroup(); + handler.endGroup(); } -/** - * Ignored block of RTF file, currently is using to ignore unknown parts - * of file. - */ - static final public void ignoredBlock() throws ParseException { - label_2: - while (true) { - if (jj_2_5(2147483647)) { - ignoredDestination(); + final public void header() throws ParseException { + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case ANSI: + case MAC: + case PC: + case PCA: + case ANSICPG: + characterSet(); + break; + default: + jj_la1[12] = jj_gen; + if (jj_2_4(2147483647)) { + fonttbl(); + } else if (jj_2_5(2147483647)) { + colortbl(); + } else if (jj_2_6(2147483647)) { + stylesheet(); } else { + jj_consume_token(-1); + throw new ParseException(); + } + } + } + + final public void characterSet() throws ParseException { + int param; - switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { - case B: - case I: - case UL: - characterFormat(); + case ANSI: + jj_consume_token(ANSI); + encoding = "Cp1252"; - break; + break; - case CONTROL_WORD: - unknownControlWord(); + case MAC: + jj_consume_token(MAC); + encoding = "MacRoman"; - break; + break; - case CONTROL_SYMBOL: - unknownControlSymbol(); + case PC: + jj_consume_token(PC); + encoding = "Cp437"; - break; + break; - case TEXT: - jj_consume_token(TEXT); + case PCA: + jj_consume_token(PCA); + encoding = "Cp850"; - break; + break; - case OPEN_BRACE: - jj_consume_token(OPEN_BRACE); - ignoredBlock(); - jj_consume_token(CLOSE_BRACE); + case ANSICPG: + jj_consume_token(ANSICPG); + param = parameter(); + encoding = RTFEncodings.getEncoding(param); - break; - default: + break; + default: - jj_la1[6] = jj_gen; + jj_la1[13] = jj_gen; - jj_consume_token(-1); - throw new ParseException(); - } - } + jj_consume_token(-1); + throw new ParseException(); + } + } - switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { - case OPEN_BRACE: - case TEXT: - case B: - case I: - case UL: - case CONTROL_WORD: - case CONTROL_SYMBOL: - ; - break; - default: - jj_la1[7] = jj_gen; - break label_2; - } - } - } /** - * Group which starts with "{\*" and describes destination, currently this part - * is ignored. + * Part which describes font table group. */ - static final public void ignoredDestination() throws ParseException { + final public void fonttbl() throws ParseException { jj_consume_token(OPEN_BRACE); - jj_consume_token(IGNORED_DESTINATION); + jj_consume_token(FONTTBL); ignoredBlock(); jj_consume_token(CLOSE_BRACE); } /** - * Part which describes font table group. + * Part which describes color table group. */ - static final public void fonttbl() throws ParseException { + final public void colortbl() throws ParseException { jj_consume_token(OPEN_BRACE); - jj_consume_token(FONTTBL); + jj_consume_token(COLORTBL); ignoredBlock(); jj_consume_token(CLOSE_BRACE); } @@ -230,7 +410,7 @@ /** * Part which describes the style sheet group. */ - static final public void stylesheet() throws ParseException { + final public void stylesheet() throws ParseException { jj_consume_token(OPEN_BRACE); jj_consume_token(STYLESHEET); ignoredBlock(); @@ -240,18 +420,32 @@ /** * Part which describes the information group inside document area. */ - static final public void info() throws ParseException { + final public void info() throws ParseException { jj_consume_token(OPEN_BRACE); jj_consume_token(INFO); ignoredBlock(); jj_consume_token(CLOSE_BRACE); } - static final public void paragraph() throws ParseException { + final public void paragraph() throws ParseException { + switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { + case B: + case I: + case UL: - characterFormat(); + characterFormat(); + break; + case PAR: + jj_consume_token(PAR); + handler.newParagraph(); + break; + default: + jj_la1[14] = jj_gen; + jj_consume_token(-1); + throw new ParseException(); - } + } + } - static final public void characterFormat() throws ParseException { + final public void characterFormat() throws ParseException { int param; switch ((jj_ntk==-1)?jj_ntk():jj_ntk) { case B: @@ -270,172 +464,343 @@ handler.setUnderline(param != 0); break; default: - jj_la1[8] = jj_gen; + jj_la1[15] = jj_gen; jj_consume_token(-1); throw new ParseException(); } } - static final private boolean jj_2_1(int xla) { + private void ignoredBlock() throws ParseException { + Token token; + int nesting = 1; + while (true) { + token = getToken(1); + if (token.kind == OPEN_BRACE) + nesting++; + else if (token.kind == CLOSE_BRACE) { + nesting--; + if (nesting == 0) + return; + } + getNextToken(); + } + } + + private boolean handleUnexpectedControlWord() throws ParseException { + Token token = getToken(1); + + if (token.kind == UC) { + getNextToken(); + token = getToken(1); + if (token.kind == PARAM) { + ucValue = Integer.parseInt(token.image); + getNextToken(); + } + return true; + } + + String text = token.image; + if (text.matches("\\\\[a-zA-Z]+")) { + getNextToken(); + token = getToken(1); + if (token.kind == PARAM) + getNextToken(); + return true; + } + + return false; + } + + private void skipAfterUnicode(int n) throws ParseException { + while (n-- > 0) { + Token token = getToken(1); + // If token is a control word or a control symbol + if (token.image.startsWith("\\")) { + getNextToken(); + token = getToken(1); + // If control word has parameter + if (token.kind == PARAM) + getNextToken(); + //TODO: Need to skip \bin data + } + else if (token.kind == HEX_CHAR) + getNextToken(); + else if (token.kind == TEXT) { + String text = token.image; + if (text.length() <= n + 1) { + n -= text.length() - 1; + getNextToken(); + } + else { + token.image = text.substring(n + 1); + return; + } + } + else throw new ParseException("Wrong token " + token + " while skipping data after Unicode character"); + } + } + + final private boolean jj_2_1(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_1(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(0, xla); } } - static final private boolean jj_2_2(int xla) { + final private boolean jj_2_2(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_2(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(1, xla); } } - static final private boolean jj_2_3(int xla) { + final private boolean jj_2_3(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_3(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(2, xla); } } - static final private boolean jj_2_4(int xla) { + final private boolean jj_2_4(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_4(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(3, xla); } } - static final private boolean jj_2_5(int xla) { + final private boolean jj_2_5(int xla) { jj_la = xla; jj_lastpos = jj_scanpos = token; try { return !jj_3_5(); } catch(LookaheadSuccess ls) { return true; } finally { jj_save(4, xla); } } - static final private boolean jj_3_3() { + final private boolean jj_2_6(int xla) { + jj_la = xla; jj_lastpos = jj_scanpos = token; + try { return !jj_3_6(); } + catch(LookaheadSuccess ls) { return true; } + finally { jj_save(5, xla); } + } + + final private boolean jj_3R_18() { + if (jj_scan_token(ANSICPG)) return true; + if (jj_3R_19()) return true; + return false; + } + + final private boolean jj_3R_17() { + if (jj_scan_token(PCA)) return true; + return false; + } + + final private boolean jj_3_3() { if (jj_scan_token(OPEN_BRACE)) return true; + if (jj_scan_token(IGNORED_DESTINATION)) return true; + return false; + } + + final private boolean jj_3R_16() { + if (jj_scan_token(PC)) return true; + return false; + } + + final private boolean jj_3_2() { + if (jj_scan_token(OPEN_BRACE)) return true; if (jj_scan_token(INFO)) return true; return false; } - static final private boolean jj_3_2() { + final private boolean jj_3_6() { if (jj_scan_token(OPEN_BRACE)) return true; if (jj_scan_token(STYLESHEET)) return true; return false; } - static final private boolean jj_3_5() { + final private boolean jj_3R_15() { + if (jj_scan_token(MAC)) return true; + return false; + } + + final private boolean jj_3R_10() { + Token xsp; + xsp = jj_scanpos; + if (jj_3R_14()) { + jj_scanpos = xsp; + if (jj_3R_15()) { + jj_scanpos = xsp; + if (jj_3R_16()) { + jj_scanpos = xsp; + if (jj_3R_17()) { + jj_scanpos = xsp; + if (jj_3R_18()) return true; + } + } + } + } + return false; + } + + final private boolean jj_3R_14() { + if (jj_scan_token(ANSI)) return true; + return false; + } + + final private boolean jj_3_5() { if (jj_scan_token(OPEN_BRACE)) return true; - if (jj_scan_token(IGNORED_DESTINATION)) return true; + if (jj_scan_token(COLORTBL)) return true; return false; } - static final private boolean jj_3_1() { + final private boolean jj_3_4() { if (jj_scan_token(OPEN_BRACE)) return true; if (jj_scan_token(FONTTBL)) return true; return false; } - static final private boolean jj_3_4() { + final private boolean jj_3R_9() { + if (jj_3R_13()) return true; + return false; + } + + final private boolean jj_3R_19() { + Token xsp; + xsp = jj_scanpos; + if (jj_scan_token(38)) jj_scanpos = xsp; + return false; + } + + final private boolean jj_3R_8() { + if (jj_3R_12()) return true; + return false; + } + + final private boolean jj_3_1() { + if (jj_3R_5()) return true; + return false; + } + + final private boolean jj_3R_7() { + if (jj_3R_11()) return true; + return false; + } + + final private boolean jj_3R_13() { if (jj_scan_token(OPEN_BRACE)) return true; - if (jj_scan_token(IGNORED_DESTINATION)) return true; + if (jj_scan_token(STYLESHEET)) return true; return false; } - static private boolean jj_initialized_once = false; - static public RTFParserTokenManager token_source; - static SimpleCharStream jj_input_stream; - static public Token token, jj_nt; - static private int jj_ntk; - static private Token jj_scanpos, jj_lastpos; - static private int jj_la; - static public boolean lookingAhead = false; - static private boolean jj_semLA; - static private int jj_gen; - static final private int[] jj_la1 = new int[9]; + final private boolean jj_3R_6() { + if (jj_3R_10()) return true; + return false; + } + + final private boolean jj_3R_5() { + Token xsp; + xsp = jj_scanpos; + if (jj_3R_6()) { + jj_scanpos = xsp; + if (jj_3R_7()) { + jj_scanpos = xsp; + if (jj_3R_8()) { + jj_scanpos = xsp; + if (jj_3R_9()) return true; + } + } + } + return false; + } + + final private boolean jj_3R_12() { + if (jj_scan_token(OPEN_BRACE)) return true; + if (jj_scan_token(COLORTBL)) return true; + return false; + } + + final private boolean jj_3R_11() { + if (jj_scan_token(OPEN_BRACE)) return true; + if (jj_scan_token(FONTTBL)) return true; + return false; + } + + public RTFParserTokenManager token_source; + SimpleCharStream jj_input_stream; + public Token token, jj_nt; + private int jj_ntk; + private Token jj_scanpos, jj_lastpos; + private int jj_la; + public boolean lookingAhead = false; + private boolean jj_semLA; + private int jj_gen; + final private int[] jj_la1 = new int[16]; static private int[] jj_la1_0; + static private int[] jj_la1_1; static { jj_la1_0(); + jj_la1_1(); } private static void jj_la1_0() { - jj_la1_0 = new int[] {0x400000,0x400000,0x400000,0x70000,0x3000a0,0x3700a0,0x3700a0,0x3700a0,0x70000,}; + jj_la1_0 = new int[] {0x0,0x0,0x4400,0x4400,0x40000e0,0x40044e0,0x40044e0,0xe0,0x4000000,0x0,0x0,0x40055e0,0x1f00000,0x1f00000,0x0,0x0,}; } - static final private JJCalls[] jj_2_rtns = new JJCalls[5]; - static private boolean jj_rescan = false; - static private int jj_gc = 0; + private static void jj_la1_1() { + jj_la1_1 = new int[] {0x40,0x40,0x0,0x0,0x0,0x0,0x0,0x0,0x0,0x40,0x1e,0x20,0x0,0x0,0x1e,0xe,}; + } + final private JJCalls[] jj_2_rtns = new JJCalls[6]; + private boolean jj_rescan = false; + private int jj_gc = 0; public RTFParser(java.io.InputStream stream) { this(stream, null); } public RTFParser(java.io.InputStream stream, String encoding) { - if (jj_initialized_once) { - System.out.println("ERROR: Second call to constructor of static parser. You must"); - System.out.println(" either use ReInit() or set the JavaCC option STATIC to false"); - System.out.println(" during parser generation."); - throw new Error(); - } - jj_initialized_once = true; try { jj_input_stream = new SimpleCharStream(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } token_source = new RTFParserTokenManager(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 9; i++) jj_la1[i] = -1; + for (int i = 0; i < 16; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } - static public void ReInit(java.io.InputStream stream) { + public void ReInit(java.io.InputStream stream) { ReInit(stream, null); } - static public void ReInit(java.io.InputStream stream, String encoding) { + public void ReInit(java.io.InputStream stream, String encoding) { try { jj_input_stream.ReInit(stream, encoding, 1, 1); } catch(java.io.UnsupportedEncodingException e) { throw new RuntimeException(e); } token_source.ReInit(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 9; i++) jj_la1[i] = -1; + for (int i = 0; i < 16; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } public RTFParser(java.io.Reader stream) { - if (jj_initialized_once) { - System.out.println("ERROR: Second call to constructor of static parser. You must"); - System.out.println(" either use ReInit() or set the JavaCC option STATIC to false"); - System.out.println(" during parser generation."); - throw new Error(); - } - jj_initialized_once = true; jj_input_stream = new SimpleCharStream(stream, 1, 1); token_source = new RTFParserTokenManager(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 9; i++) jj_la1[i] = -1; + for (int i = 0; i < 16; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } - static public void ReInit(java.io.Reader stream) { + public void ReInit(java.io.Reader stream) { jj_input_stream.ReInit(stream, 1, 1); token_source.ReInit(jj_input_stream); token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 9; i++) jj_la1[i] = -1; + for (int i = 0; i < 16; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } public RTFParser(RTFParserTokenManager tm) { - if (jj_initialized_once) { - System.out.println("ERROR: Second call to constructor of static parser. You must"); - System.out.println(" either use ReInit() or set the JavaCC option STATIC to false"); - System.out.println(" during parser generation."); - throw new Error(); - } - jj_initialized_once = true; token_source = tm; token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 9; i++) jj_la1[i] = -1; + for (int i = 0; i < 16; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } @@ -444,11 +809,11 @@ token = new Token(); jj_ntk = -1; jj_gen = 0; - for (int i = 0; i < 9; i++) jj_la1[i] = -1; + for (int i = 0; i < 16; i++) jj_la1[i] = -1; for (int i = 0; i < jj_2_rtns.length; i++) jj_2_rtns[i] = new JJCalls(); } - static final private Token jj_consume_token(int kind) throws ParseException { + final private Token jj_consume_token(int kind) throws ParseException { Token oldToken; if ((oldToken = token).next != null) token = token.next; else token = token.next = token_source.getNextToken(); @@ -473,8 +838,8 @@ } static private final class LookaheadSuccess extends java.lang.Error { } - static final private LookaheadSuccess jj_ls = new LookaheadSuccess(); - static final private boolean jj_scan_token(int kind) { + final private LookaheadSuccess jj_ls = new LookaheadSuccess(); + final private boolean jj_scan_token(int kind) { if (jj_scanpos == jj_lastpos) { jj_la--; if (jj_scanpos.next == null) { @@ -495,7 +860,7 @@ return false; } - static final public Token getNextToken() { + final public Token getNextToken() { if (token.next != null) token = token.next; else token = token.next = token_source.getNextToken(); jj_ntk = -1; @@ -503,7 +868,7 @@ return token; } - static final public Token getToken(int index) { + final public Token getToken(int index) { Token t = lookingAhead ? jj_scanpos : token; for (int i = 0; i < index; i++) { if (t.next != null) t = t.next; @@ -512,20 +877,20 @@ return t; } - static final private int jj_ntk() { + final private int jj_ntk() { if ((jj_nt=token.next) == null) return (jj_ntk = (token.next=token_source.getNextToken()).kind); else return (jj_ntk = jj_nt.kind); } - static private java.util.Vector jj_expentries = new java.util.Vector(); - static private int[] jj_expentry; - static private int jj_kind = -1; - static private int[] jj_lasttokens = new int[100]; - static private int jj_endpos; + private java.util.Vector jj_expentries = new java.util.Vector(); + private int[] jj_expentry; + private int jj_kind = -1; + private int[] jj_lasttokens = new int[100]; + private int jj_endpos; - static private void jj_add_error_token(int kind, int pos) { + private void jj_add_error_token(int kind, int pos) { if (pos >= 100) return; if (pos == jj_endpos + 1) { jj_lasttokens[jj_endpos++] = kind; @@ -553,26 +918,29 @@ } } - static public ParseException generateParseException() { + public ParseException generateParseException() { jj_expentries.removeAllElements(); - boolean[] la1tokens = new boolean[24]; - for (int i = 0; i < 24; i++) { + boolean[] la1tokens = new boolean[40]; + for (int i = 0; i < 40; i++) { la1tokens[i] = false; } if (jj_kind >= 0) { la1tokens[jj_kind] = true; jj_kind = -1; } - for (int i = 0; i < 9; i++) { + for (int i = 0; i < 16; i++) { if (jj_la1[i] == jj_gen) { for (int j = 0; j < 32; j++) { if ((jj_la1_0[i] & (1< jj_gen) { if (p.next == null) { p = p.next = new JJCalls(); break; } Index: swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParserTokenManager.java =================================================================== --- swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParserTokenManager.java (revision 676572) +++ swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParserTokenManager.java Mon Jul 14 17:00:46 EEST 2008 @@ -1,79 +1,114 @@ /* Generated By:JavaCC: Do not edit this line. RTFParserTokenManager.java */ package org.apache.harmony.x.swing.text.rtf; import java.io.*; -import javax.swing.text.Document; +import java.util.Stack; import javax.swing.text.DefaultStyledDocument; public class RTFParserTokenManager implements RTFParserConstants { - public static java.io.PrintStream debugStream = System.out; - public static void setDebugStream(java.io.PrintStream ds) { debugStream = ds; } -private static final int jjStopStringLiteralDfa_1(int pos, long active0) + public java.io.PrintStream debugStream = System.out; + public void setDebugStream(java.io.PrintStream ds) { debugStream = ds; } +private final int jjStopStringLiteralDfa_0(int pos, long active0) { switch (pos) { + case 0: + if ((active0 & 0x8f0L) != 0L) + return 1; + return -1; default : return -1; } } -private static final int jjStartNfa_1(int pos, long active0) +private final int jjStartNfa_0(int pos, long active0) { - return jjMoveNfa_1(jjStopStringLiteralDfa_1(pos, active0), pos + 1); + return jjMoveNfa_0(jjStopStringLiteralDfa_0(pos, active0), pos + 1); } -static private final int jjStopAtPos(int pos, int kind) +private final int jjStopAtPos(int pos, int kind) { jjmatchedKind = kind; jjmatchedPos = pos; return pos + 1; } -static private final int jjStartNfaWithStates_1(int pos, int kind, int state) +private final int jjStartNfaWithStates_0(int pos, int kind, int state) { jjmatchedKind = kind; jjmatchedPos = pos; try { curChar = input_stream.readChar(); } catch(java.io.IOException e) { return pos + 1; } - return jjMoveNfa_1(state, pos + 1); + return jjMoveNfa_0(state, pos + 1); } -static private final int jjMoveStringLiteralDfa0_1() +private final int jjMoveStringLiteralDfa0_0() { switch(curChar) { case 92: - return jjStopAtPos(0, 1); + jjmatchedKind = 11; + return jjMoveStringLiteralDfa1_0(0xf0L); case 123: - return jjStopAtPos(0, 5); + return jjStopAtPos(0, 12); case 125: - return jjStopAtPos(0, 6); + return jjStopAtPos(0, 13); default : - return jjMoveNfa_1(0, 0); + return jjMoveNfa_0(0, 0); } } -static private final void jjCheckNAdd(int state) +private final int jjMoveStringLiteralDfa1_0(long active0) { + try { curChar = input_stream.readChar(); } + catch(java.io.IOException e) { + jjStopStringLiteralDfa_0(0, active0); + return 1; + } + switch(curChar) + { + case 42: + if ((active0 & 0x10L) != 0L) + return jjStopAtPos(1, 4); + break; + case 92: + if ((active0 & 0x80L) != 0L) + return jjStopAtPos(1, 7); + break; + case 123: + if ((active0 & 0x20L) != 0L) + return jjStopAtPos(1, 5); + break; + case 125: + if ((active0 & 0x40L) != 0L) + return jjStopAtPos(1, 6); + break; + default : + break; + } + return jjStartNfa_0(0, active0); +} +private final void jjCheckNAdd(int state) +{ if (jjrounds[state] != jjround) { jjstateSet[jjnewStateCnt++] = state; jjrounds[state] = jjround; } } -static private final void jjAddStates(int start, int end) +private final void jjAddStates(int start, int end) { do { jjstateSet[jjnewStateCnt++] = jjnextStates[start]; } while (start++ != end); } -static private final void jjCheckNAddTwoStates(int state1, int state2) +private final void jjCheckNAddTwoStates(int state1, int state2) { jjCheckNAdd(state1); jjCheckNAdd(state2); } -static private final void jjCheckNAddStates(int start, int end) +private final void jjCheckNAddStates(int start, int end) { do { jjCheckNAdd(jjnextStates[start]); } while (start++ != end); } -static private final void jjCheckNAddStates(int start) +private final void jjCheckNAddStates(int start) { jjCheckNAdd(jjnextStates[start]); jjCheckNAdd(jjnextStates[start + 1]); @@ -81,11 +116,11 @@ static final long[] jjbitVec0 = { 0x0L, 0x0L, 0xffffffffffffffffL, 0xffffffffffffffffL }; -static private final int jjMoveNfa_1(int startState, int curPos) +private final int jjMoveNfa_0(int startState, int curPos) { int[] nextStates; int startsAt = 0; - jjnewStateCnt = 1; + jjnewStateCnt = 7; int i = 1; jjstateSet[0] = startState; int j, kind = 0x7fffffff; @@ -101,11 +136,34 @@ switch(jjstateSet[--i]) { case 0: + case 6: if ((0xffffffffffffd9ffL & l) == 0L) break; - kind = 7; - jjstateSet[jjnewStateCnt++] = 0; + if (kind > 14) + kind = 14; + jjCheckNAdd(6); break; + case 1: + if ((0xfc00fffeffffd9ffL & l) != 0L) + { + if (kind > 8) + kind = 8; + } + if (curChar == 39) + jjstateSet[jjnewStateCnt++] = 3; + break; + case 2: + if (curChar == 39) + jjstateSet[jjnewStateCnt++] = 3; + break; + case 3: + if ((0x3ff000000000000L & l) != 0L) + jjstateSet[jjnewStateCnt++] = 4; + break; + case 4: + if ((0x3ff000000000000L & l) != 0L && kind > 10) + kind = 10; + break; default : break; } } while(i != startsAt); @@ -118,10 +176,39 @@ switch(jjstateSet[--i]) { case 0: + if ((0xd7ffffffefffffffL & l) != 0L) + { + if (kind > 14) + kind = 14; + jjCheckNAdd(6); + } + else if (curChar == 92) + jjstateSet[jjnewStateCnt++] = 2; + if (curChar == 92) + jjstateSet[jjnewStateCnt++] = 1; + break; + case 1: + if ((0xd0000001f8000001L & l) != 0L && kind > 8) + kind = 8; + break; + case 3: + if ((0x7e0000007eL & l) != 0L) + jjstateSet[jjnewStateCnt++] = 4; + break; + case 4: + if ((0x7e0000007eL & l) != 0L && kind > 10) + kind = 10; + break; + case 5: + if (curChar == 92) + jjstateSet[jjnewStateCnt++] = 2; + break; + case 6: if ((0xd7ffffffefffffffL & l) == 0L) break; - kind = 7; - jjstateSet[jjnewStateCnt++] = 0; + if (kind > 14) + kind = 14; + jjCheckNAdd(6); break; default : break; } @@ -136,12 +223,17 @@ switch(jjstateSet[--i]) { case 0: + case 6: if ((jjbitVec0[i2] & l2) == 0L) break; - if (kind > 7) - kind = 7; - jjstateSet[jjnewStateCnt++] = 0; + if (kind > 14) + kind = 14; + jjCheckNAdd(6); break; + case 1: + if ((jjbitVec0[i2] & l2) != 0L && kind > 8) + kind = 8; + break; default : break; } } while(i != startsAt); @@ -153,345 +245,439 @@ kind = 0x7fffffff; } ++curPos; - if ((i = jjnewStateCnt) == (startsAt = 1 - (jjnewStateCnt = startsAt))) + if ((i = jjnewStateCnt) == (startsAt = 7 - (jjnewStateCnt = startsAt))) return curPos; try { curChar = input_stream.readChar(); } catch(java.io.IOException e) { return curPos; } } } -private static final int jjStopStringLiteralDfa_0(int pos, long active0) +private final int jjStopStringLiteralDfa_1(int pos, long active0) { switch (pos) { case 0: - if ((active0 & 0x38000L) != 0L) - return 0; - if ((active0 & 0x47000L) != 0L) + if ((active0 & 0x10e1f80000L) != 0L) { - jjmatchedKind = 20; - return 0; + jjmatchedKind = 37; + return 6; } + if ((active0 & 0xf1e000000L) != 0L) + return 6; + if ((active0 & 0x8f0L) != 0L) + return 1; return -1; case 1: - if ((active0 & 0x40000L) != 0L) - return 0; - if ((active0 & 0xf000L) != 0L) + if ((active0 & 0x812c00000L) != 0L) + return 6; + if ((active0 & 0x11e9380000L) != 0L) { - jjmatchedKind = 20; + if (jjmatchedPos != 1) + { + jjmatchedKind = 37; - jjmatchedPos = 1; + jjmatchedPos = 1; - return 0; - } + } + return 6; + } return -1; case 2: - if ((active0 & 0x1000L) != 0L) - return 0; - if ((active0 & 0xe000L) != 0L) + if ((active0 & 0x1e1100000L) != 0L) { - jjmatchedKind = 20; + jjmatchedKind = 37; jjmatchedPos = 2; - return 0; + return 6; } + if ((active0 & 0x1008a80000L) != 0L) + return 6; return -1; case 3: - if ((active0 & 0x8000L) != 0L) - return 0; - if ((active0 & 0x6000L) != 0L) + if ((active0 & 0xe0000000L) != 0L) { - jjmatchedKind = 20; + if (jjmatchedPos != 3) + { + jjmatchedKind = 37; - jjmatchedPos = 3; + jjmatchedPos = 3; - return 0; - } + } + return 6; + } + if ((active0 & 0x101100000L) != 0L) + return 6; return -1; case 4: - if ((active0 & 0x6000L) != 0L) + if ((active0 & 0xe1000000L) != 0L) { - jjmatchedKind = 20; + jjmatchedKind = 37; jjmatchedPos = 4; - return 0; + return 6; } return -1; case 5: - if ((active0 & 0x6000L) != 0L) + if ((active0 & 0xe1000000L) != 0L) { - jjmatchedKind = 20; + jjmatchedKind = 37; jjmatchedPos = 5; - return 0; + return 6; } return -1; case 6: - if ((active0 & 0x4000L) != 0L) + if ((active0 & 0x21000000L) != 0L) + return 6; + if ((active0 & 0xc0000000L) != 0L) { - jjmatchedKind = 20; + jjmatchedKind = 37; jjmatchedPos = 6; - return 0; + return 6; } - if ((active0 & 0x2000L) != 0L) - return 0; return -1; case 7: - if ((active0 & 0x4000L) != 0L) + if ((active0 & 0x40000000L) != 0L) + return 6; + if ((active0 & 0x80000000L) != 0L) { - jjmatchedKind = 20; + jjmatchedKind = 37; jjmatchedPos = 7; - return 0; + return 6; } return -1; case 8: - if ((active0 & 0x4000L) != 0L) + if ((active0 & 0x80000000L) != 0L) { - jjmatchedKind = 20; + jjmatchedKind = 37; jjmatchedPos = 8; - return 0; + return 6; } return -1; default : return -1; } } -private static final int jjStartNfa_0(int pos, long active0) +private final int jjStartNfa_1(int pos, long active0) { - return jjMoveNfa_0(jjStopStringLiteralDfa_0(pos, active0), pos + 1); + return jjMoveNfa_1(jjStopStringLiteralDfa_1(pos, active0), pos + 1); } -static private final int jjStartNfaWithStates_0(int pos, int kind, int state) +private final int jjStartNfaWithStates_1(int pos, int kind, int state) { jjmatchedKind = kind; jjmatchedPos = pos; try { curChar = input_stream.readChar(); } catch(java.io.IOException e) { return pos + 1; } - return jjMoveNfa_0(state, pos + 1); + return jjMoveNfa_1(state, pos + 1); } -static private final int jjMoveStringLiteralDfa0_0() +private final int jjMoveStringLiteralDfa0_1() { switch(curChar) { case 9: - return jjStopAtPos(0, 11); + return jjStopAtPos(0, 18); case 10: - return jjStopAtPos(0, 9); + return jjStopAtPos(0, 16); case 13: - return jjStopAtPos(0, 10); + return jjStopAtPos(0, 17); case 32: - return jjStopAtPos(0, 8); - case 42: - return jjStopAtPos(0, 19); + return jjStopAtPos(0, 15); case 92: - return jjStopAtPos(0, 1); + jjmatchedKind = 11; + return jjMoveStringLiteralDfa1_1(0xf0L); + case 97: + return jjMoveStringLiteralDfa1_1(0x1100000L); case 98: - return jjStartNfaWithStates_0(0, 16, 0); + return jjStartNfaWithStates_1(0, 33, 6); + case 99: + return jjMoveStringLiteralDfa1_1(0x40000000L); case 102: - return jjMoveStringLiteralDfa1_0(0x2000L); + return jjMoveStringLiteralDfa1_1(0x20000000L); case 105: - jjmatchedKind = 17; - return jjMoveStringLiteralDfa1_0(0x8000L); + jjmatchedKind = 34; + return jjMoveStringLiteralDfa1_1(0x100000000L); + case 109: + return jjMoveStringLiteralDfa1_1(0x200000L); + case 112: + return jjMoveStringLiteralDfa1_1(0x1000c00000L); case 114: - return jjMoveStringLiteralDfa1_0(0x1000L); + return jjMoveStringLiteralDfa1_1(0x80000L); case 115: - return jjMoveStringLiteralDfa1_0(0x4000L); + return jjMoveStringLiteralDfa1_1(0x80000000L); case 117: - return jjMoveStringLiteralDfa1_0(0x40000L); + jjmatchedKind = 26; + return jjMoveStringLiteralDfa1_1(0x81a000000L); case 123: - return jjStopAtPos(0, 5); + return jjStopAtPos(0, 12); case 125: - return jjStopAtPos(0, 6); + return jjStopAtPos(0, 13); default : - return jjMoveNfa_0(1, 0); + return jjMoveNfa_1(0, 0); } } -static private final int jjMoveStringLiteralDfa1_0(long active0) +private final int jjMoveStringLiteralDfa1_1(long active0) { try { curChar = input_stream.readChar(); } catch(java.io.IOException e) { - jjStopStringLiteralDfa_0(0, active0); + jjStopStringLiteralDfa_1(0, active0); return 1; } switch(curChar) { + case 42: + if ((active0 & 0x10L) != 0L) + return jjStopAtPos(1, 4); + break; + case 92: + if ((active0 & 0x80L) != 0L) + return jjStopAtPos(1, 7); + break; + case 97: + return jjMoveStringLiteralDfa2_1(active0, 0x1000200000L); + case 99: + if ((active0 & 0x400000L) != 0L) + { + jjmatchedKind = 22; + jjmatchedPos = 1; + } + else if ((active0 & 0x2000000L) != 0L) + return jjStartNfaWithStates_1(1, 25, 6); + return jjMoveStringLiteralDfa2_1(active0, 0x800000L); + case 100: + if ((active0 & 0x10000000L) != 0L) + return jjStartNfaWithStates_1(1, 28, 6); + break; case 108: - if ((active0 & 0x40000L) != 0L) - return jjStartNfaWithStates_0(1, 18, 0); + if ((active0 & 0x800000000L) != 0L) + return jjStartNfaWithStates_1(1, 35, 6); break; case 110: - return jjMoveStringLiteralDfa2_0(active0, 0x8000L); + return jjMoveStringLiteralDfa2_1(active0, 0x101100000L); case 111: - return jjMoveStringLiteralDfa2_0(active0, 0x2000L); + return jjMoveStringLiteralDfa2_1(active0, 0x60000000L); + case 112: + return jjMoveStringLiteralDfa2_1(active0, 0x8000000L); case 116: - return jjMoveStringLiteralDfa2_0(active0, 0x5000L); + return jjMoveStringLiteralDfa2_1(active0, 0x80080000L); + case 123: + if ((active0 & 0x20L) != 0L) + return jjStopAtPos(1, 5); + break; + case 125: + if ((active0 & 0x40L) != 0L) + return jjStopAtPos(1, 6); + break; default : break; } - return jjStartNfa_0(0, active0); + return jjStartNfa_1(0, active0); } -static private final int jjMoveStringLiteralDfa2_0(long old0, long active0) +private final int jjMoveStringLiteralDfa2_1(long old0, long active0) { if (((active0 &= old0)) == 0L) - return jjStartNfa_0(0, old0); + return jjStartNfa_1(0, old0); try { curChar = input_stream.readChar(); } catch(java.io.IOException e) { - jjStopStringLiteralDfa_0(1, active0); + jjStopStringLiteralDfa_1(1, active0); return 2; } switch(curChar) { + case 97: + if ((active0 & 0x800000L) != 0L) + return jjStartNfaWithStates_1(2, 23, 6); + break; + case 99: + if ((active0 & 0x200000L) != 0L) + return jjStartNfaWithStates_1(2, 21, 6); + break; case 102: - if ((active0 & 0x1000L) != 0L) - return jjStartNfaWithStates_0(2, 12, 0); - return jjMoveStringLiteralDfa3_0(active0, 0x8000L); + if ((active0 & 0x80000L) != 0L) + return jjStartNfaWithStates_1(2, 19, 6); + return jjMoveStringLiteralDfa3_1(active0, 0x100000000L); + case 108: + return jjMoveStringLiteralDfa3_1(active0, 0x40000000L); case 110: - return jjMoveStringLiteralDfa3_0(active0, 0x2000L); + return jjMoveStringLiteralDfa3_1(active0, 0x20000000L); + case 114: + if ((active0 & 0x8000000L) != 0L) + return jjStartNfaWithStates_1(2, 27, 6); + else if ((active0 & 0x1000000000L) != 0L) + return jjStartNfaWithStates_1(2, 36, 6); + break; + case 115: + return jjMoveStringLiteralDfa3_1(active0, 0x1100000L); case 121: - return jjMoveStringLiteralDfa3_0(active0, 0x4000L); + return jjMoveStringLiteralDfa3_1(active0, 0x80000000L); default : break; } - return jjStartNfa_0(1, active0); + return jjStartNfa_1(1, active0); } -static private final int jjMoveStringLiteralDfa3_0(long old0, long active0) +private final int jjMoveStringLiteralDfa3_1(long old0, long active0) { if (((active0 &= old0)) == 0L) - return jjStartNfa_0(1, old0); + return jjStartNfa_1(1, old0); try { curChar = input_stream.readChar(); } catch(java.io.IOException e) { - jjStopStringLiteralDfa_0(2, active0); + jjStopStringLiteralDfa_1(2, active0); return 3; } switch(curChar) { + case 105: + if ((active0 & 0x100000L) != 0L) + { + jjmatchedKind = 20; + jjmatchedPos = 3; + } + return jjMoveStringLiteralDfa4_1(active0, 0x1000000L); case 108: - return jjMoveStringLiteralDfa4_0(active0, 0x4000L); + return jjMoveStringLiteralDfa4_1(active0, 0x80000000L); case 111: - if ((active0 & 0x8000L) != 0L) - return jjStartNfaWithStates_0(3, 15, 0); - break; + if ((active0 & 0x100000000L) != 0L) + return jjStartNfaWithStates_1(3, 32, 6); + return jjMoveStringLiteralDfa4_1(active0, 0x40000000L); case 116: - return jjMoveStringLiteralDfa4_0(active0, 0x2000L); + return jjMoveStringLiteralDfa4_1(active0, 0x20000000L); default : break; } - return jjStartNfa_0(2, active0); + return jjStartNfa_1(2, active0); } -static private final int jjMoveStringLiteralDfa4_0(long old0, long active0) +private final int jjMoveStringLiteralDfa4_1(long old0, long active0) { if (((active0 &= old0)) == 0L) - return jjStartNfa_0(2, old0); + return jjStartNfa_1(2, old0); try { curChar = input_stream.readChar(); } catch(java.io.IOException e) { - jjStopStringLiteralDfa_0(3, active0); + jjStopStringLiteralDfa_1(3, active0); return 4; } switch(curChar) { + case 99: + return jjMoveStringLiteralDfa5_1(active0, 0x1000000L); case 101: - return jjMoveStringLiteralDfa5_0(active0, 0x4000L); + return jjMoveStringLiteralDfa5_1(active0, 0x80000000L); + case 114: + return jjMoveStringLiteralDfa5_1(active0, 0x40000000L); case 116: - return jjMoveStringLiteralDfa5_0(active0, 0x2000L); + return jjMoveStringLiteralDfa5_1(active0, 0x20000000L); default : break; } - return jjStartNfa_0(3, active0); + return jjStartNfa_1(3, active0); } -static private final int jjMoveStringLiteralDfa5_0(long old0, long active0) +private final int jjMoveStringLiteralDfa5_1(long old0, long active0) { if (((active0 &= old0)) == 0L) - return jjStartNfa_0(3, old0); + return jjStartNfa_1(3, old0); try { curChar = input_stream.readChar(); } catch(java.io.IOException e) { - jjStopStringLiteralDfa_0(4, active0); + jjStopStringLiteralDfa_1(4, active0); return 5; } switch(curChar) { case 98: - return jjMoveStringLiteralDfa6_0(active0, 0x2000L); + return jjMoveStringLiteralDfa6_1(active0, 0x20000000L); + case 112: + return jjMoveStringLiteralDfa6_1(active0, 0x1000000L); case 115: - return jjMoveStringLiteralDfa6_0(active0, 0x4000L); + return jjMoveStringLiteralDfa6_1(active0, 0x80000000L); + case 116: + return jjMoveStringLiteralDfa6_1(active0, 0x40000000L); default : break; } - return jjStartNfa_0(4, active0); + return jjStartNfa_1(4, active0); } -static private final int jjMoveStringLiteralDfa6_0(long old0, long active0) +private final int jjMoveStringLiteralDfa6_1(long old0, long active0) { if (((active0 &= old0)) == 0L) - return jjStartNfa_0(4, old0); + return jjStartNfa_1(4, old0); try { curChar = input_stream.readChar(); } catch(java.io.IOException e) { - jjStopStringLiteralDfa_0(5, active0); + jjStopStringLiteralDfa_1(5, active0); return 6; } switch(curChar) { + case 98: + return jjMoveStringLiteralDfa7_1(active0, 0x40000000L); + case 103: + if ((active0 & 0x1000000L) != 0L) + return jjStartNfaWithStates_1(6, 24, 6); + break; case 104: - return jjMoveStringLiteralDfa7_0(active0, 0x4000L); + return jjMoveStringLiteralDfa7_1(active0, 0x80000000L); case 108: - if ((active0 & 0x2000L) != 0L) - return jjStartNfaWithStates_0(6, 13, 0); + if ((active0 & 0x20000000L) != 0L) + return jjStartNfaWithStates_1(6, 29, 6); break; default : break; } - return jjStartNfa_0(5, active0); + return jjStartNfa_1(5, active0); } -static private final int jjMoveStringLiteralDfa7_0(long old0, long active0) +private final int jjMoveStringLiteralDfa7_1(long old0, long active0) { if (((active0 &= old0)) == 0L) - return jjStartNfa_0(5, old0); + return jjStartNfa_1(5, old0); try { curChar = input_stream.readChar(); } catch(java.io.IOException e) { - jjStopStringLiteralDfa_0(6, active0); + jjStopStringLiteralDfa_1(6, active0); return 7; } switch(curChar) { case 101: - return jjMoveStringLiteralDfa8_0(active0, 0x4000L); + return jjMoveStringLiteralDfa8_1(active0, 0x80000000L); + case 108: + if ((active0 & 0x40000000L) != 0L) + return jjStartNfaWithStates_1(7, 30, 6); + break; default : break; } - return jjStartNfa_0(6, active0); + return jjStartNfa_1(6, active0); } -static private final int jjMoveStringLiteralDfa8_0(long old0, long active0) +private final int jjMoveStringLiteralDfa8_1(long old0, long active0) { if (((active0 &= old0)) == 0L) - return jjStartNfa_0(6, old0); + return jjStartNfa_1(6, old0); try { curChar = input_stream.readChar(); } catch(java.io.IOException e) { - jjStopStringLiteralDfa_0(7, active0); + jjStopStringLiteralDfa_1(7, active0); return 8; } switch(curChar) { case 101: - return jjMoveStringLiteralDfa9_0(active0, 0x4000L); + return jjMoveStringLiteralDfa9_1(active0, 0x80000000L); default : break; } - return jjStartNfa_0(7, active0); + return jjStartNfa_1(7, active0); } -static private final int jjMoveStringLiteralDfa9_0(long old0, long active0) +private final int jjMoveStringLiteralDfa9_1(long old0, long active0) { if (((active0 &= old0)) == 0L) - return jjStartNfa_0(7, old0); + return jjStartNfa_1(7, old0); try { curChar = input_stream.readChar(); } catch(java.io.IOException e) { - jjStopStringLiteralDfa_0(8, active0); + jjStopStringLiteralDfa_1(8, active0); return 9; } switch(curChar) { case 116: - if ((active0 & 0x4000L) != 0L) - return jjStartNfaWithStates_0(9, 14, 0); + if ((active0 & 0x80000000L) != 0L) + return jjStartNfaWithStates_1(9, 31, 6); break; default : break; } - return jjStartNfa_0(8, active0); + return jjStartNfa_1(8, active0); } -static private final int jjMoveNfa_0(int startState, int curPos) +private final int jjMoveNfa_1(int startState, int curPos) { int[] nextStates; int startsAt = 0; - jjnewStateCnt = 4; + jjnewStateCnt = 10; int i = 1; jjstateSet[0] = startState; int j, kind = 0x7fffffff; @@ -506,31 +692,52 @@ { switch(jjstateSet[--i]) { + case 0: + if ((0x3ff000000000000L & l) != 0L) + { + if (kind > 38) + kind = 38; + jjCheckNAddTwoStates(8, 9); + } + else if (curChar == 45) + jjCheckNAdd(8); + break; case 1: if ((0xfc00fffeffffd9ffL & l) != 0L) { - if (kind > 21) - kind = 21; + if (kind > 8) + kind = 8; } - else if ((0x3ff000000000000L & l) != 0L) - { - if (kind > 22) - kind = 22; - jjCheckNAdd(3); - } - if (curChar == 45) - jjCheckNAdd(3); + if (curChar == 39) + jjstateSet[jjnewStateCnt++] = 3; break; case 2: - if (curChar == 45) - jjCheckNAdd(3); + if (curChar == 39) + jjstateSet[jjnewStateCnt++] = 3; break; case 3: + if ((0x3ff000000000000L & l) != 0L) + jjstateSet[jjnewStateCnt++] = 4; + break; + case 4: + if ((0x3ff000000000000L & l) != 0L && kind > 10) + kind = 10; + break; + case 7: + if (curChar == 45) + jjCheckNAdd(8); + break; + case 8: if ((0x3ff000000000000L & l) == 0L) break; - kind = 22; - jjCheckNAdd(3); + if (kind > 38) + kind = 38; + jjCheckNAddTwoStates(8, 9); break; + case 9: + if (curChar == 32) + kind = 38; + break; default : break; } } while(i != startsAt); @@ -542,24 +749,40 @@ { switch(jjstateSet[--i]) { - case 1: + case 0: if ((0x7fffffe07fffffeL & l) != 0L) { - if (kind > 20) - kind = 20; - jjCheckNAdd(0); + if (kind > 37) + kind = 37; + jjCheckNAdd(6); } - else if ((0xd0000001e8000001L & l) != 0L) - { - if (kind > 21) - kind = 21; - } + else if (curChar == 92) + jjstateSet[jjnewStateCnt++] = 2; + if (curChar == 92) + jjstateSet[jjnewStateCnt++] = 1; break; - case 0: + case 1: + if ((0xd0000001f8000001L & l) != 0L && kind > 8) + kind = 8; + break; + case 3: + if ((0x7e0000007eL & l) != 0L) + jjstateSet[jjnewStateCnt++] = 4; + break; + case 4: + if ((0x7e0000007eL & l) != 0L && kind > 10) + kind = 10; + break; + case 5: + if (curChar == 92) + jjstateSet[jjnewStateCnt++] = 2; + break; + case 6: if ((0x7fffffe07fffffeL & l) == 0L) break; - kind = 20; - jjCheckNAdd(0); + if (kind > 37) + kind = 37; + jjCheckNAdd(6); break; default : break; } @@ -574,8 +797,8 @@ switch(jjstateSet[--i]) { case 1: - if ((jjbitVec0[i2] & l2) != 0L && kind > 21) - kind = 21; + if ((jjbitVec0[i2] & l2) != 0L && kind > 8) + kind = 8; break; default : break; } @@ -588,7 +811,7 @@ kind = 0x7fffffff; } ++curPos; - if ((i = jjnewStateCnt) == (startsAt = 4 - (jjnewStateCnt = startsAt))) + if ((i = jjnewStateCnt) == (startsAt = 10 - (jjnewStateCnt = startsAt))) return curPos; try { curChar = input_stream.readChar(); } catch(java.io.IOException e) { return curPos; } @@ -597,57 +820,59 @@ static final int[] jjnextStates = { }; public static final String[] jjstrLiteralImages = { -null, null, null, null, null, null, null, null, null, null, null, null, null, -null, null, null, null, null, null, null, null, null, null, null, }; +"", null, null, null, null, null, null, null, null, null, null, null, null, +null, null, null, null, null, null, null, null, null, null, null, null, null, null, +null, null, null, null, null, null, null, null, null, null, null, null, null, }; public static final String[] lexStateNames = { - "IN_CONTROL", "DEFAULT", + "IN_CONTROL_WORD", }; public static final int[] jjnewLexState = { - -1, 0, -1, -1, -1, 1, 1, -1, 1, 1, 1, 1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 1, + -1, -1, -1, -1, 0, 0, 0, 0, 0, -1, 0, 1, 0, 0, -1, 0, 0, 0, 0, -1, -1, -1, -1, -1, -1, + -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, 0, 0, }; static final long[] jjtoToken = { - 0x7ff0e1L, + 0x7ffff875f1L, }; static final long[] jjtoSkip = { - 0x800f1cL, + 0x800007800eL, }; static final long[] jjtoMore = { - 0x2L, + 0x800L, }; -static protected SimpleCharStream input_stream; -static private final int[] jjrounds = new int[4]; -static private final int[] jjstateSet = new int[8]; -static protected char curChar; +protected SimpleCharStream input_stream; +private final int[] jjrounds = new int[10]; +private final int[] jjstateSet = new int[20]; +protected char curChar; public RTFParserTokenManager(SimpleCharStream stream){ - if (input_stream != null) - throw new TokenMgrError("ERROR: Second call to constructor of static lexer. You must use ReInit() to initialize the static variables.", TokenMgrError.STATIC_LEXER_ERROR); + if (SimpleCharStream.staticFlag) + throw new Error("ERROR: Cannot use a static CharStream class with a non-static lexical analyzer."); input_stream = stream; } public RTFParserTokenManager(SimpleCharStream stream, int lexState){ this(stream); SwitchTo(lexState); } -static public void ReInit(SimpleCharStream stream) +public void ReInit(SimpleCharStream stream) { jjmatchedPos = jjnewStateCnt = 0; curLexState = defaultLexState; input_stream = stream; ReInitRounds(); } -static private final void ReInitRounds() +private final void ReInitRounds() { int i; jjround = 0x80000001; - for (i = 4; i-- > 0;) + for (i = 10; i-- > 0;) jjrounds[i] = 0x80000000; } -static public void ReInit(SimpleCharStream stream, int lexState) +public void ReInit(SimpleCharStream stream, int lexState) { ReInit(stream); SwitchTo(lexState); } -static public void SwitchTo(int lexState) +public void SwitchTo(int lexState) { if (lexState >= 2 || lexState < 0) throw new TokenMgrError("Error: Ignoring invalid lexical state : " + lexState + ". State unchanged.", TokenMgrError.INVALID_LEXICAL_STATE); @@ -655,7 +880,7 @@ curLexState = lexState; } -static protected Token jjFillToken() +protected Token jjFillToken() { Token t = Token.newToken(jjmatchedKind); t.kind = jjmatchedKind; @@ -668,14 +893,14 @@ return t; } -static int curLexState = 1; -static int defaultLexState = 1; -static int jjnewStateCnt; -static int jjround; -static int jjmatchedPos; -static int jjmatchedKind; +int curLexState = 0; +int defaultLexState = 0; +int jjnewStateCnt; +int jjround; +int jjmatchedPos; +int jjmatchedKind; -public static Token getNextToken() +public Token getNextToken() { int kind; Token specialToken = null; @@ -701,15 +926,6 @@ switch(curLexState) { case 0: - jjmatchedKind = 0x7fffffff; - jjmatchedPos = 0; - curPos = jjMoveStringLiteralDfa0_0(); - if (jjmatchedPos == 0 && jjmatchedKind > 23) - { - jjmatchedKind = 23; - } - break; - case 1: try { input_stream.backup(0); while (curChar <= 13 && (0x2600L & (1L << curChar)) != 0L) curChar = input_stream.BeginToken(); @@ -717,7 +933,16 @@ catch (java.io.IOException e1) { continue EOFLoop; } jjmatchedKind = 0x7fffffff; jjmatchedPos = 0; + curPos = jjMoveStringLiteralDfa0_0(); + break; + case 1: + jjmatchedKind = 0x7fffffff; + jjmatchedPos = 0; curPos = jjMoveStringLiteralDfa0_1(); + if (jjmatchedPos == 0 && jjmatchedKind > 39) + { + jjmatchedKind = 39; + } break; } if (jjmatchedKind != 0x7fffffff) Index: swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParserHandler.java =================================================================== --- swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParserHandler.java (revision 676572) +++ swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParserHandler.java (revision 676572) @@ -1,83 +0,0 @@ -/* - * Licensed to the Apache Software Foundation (ASF) under one or more - * contributor license agreements. See the NOTICE file distributed with - * this work for additional information regarding copyright ownership. - * The ASF licenses this file to You under the Apache License, Version 2.0 - * (the "License"); you may not use this file except in compliance with - * the License. You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ -package org.apache.harmony.x.swing.text.rtf; - -import javax.swing.text.*; -import java.util.Stack; - -/** - * @author ayzen - */ -public class RTFParserHandler { - - private StyledDocument sdoc; - private Document doc; - - private int offset; - - private Stack stylesStack; - private MutableAttributeSet currentStyle; - - public RTFParserHandler(Document doc, int position) { - stylesStack = new Stack(); - currentStyle = new SimpleAttributeSet(); - StyleConstants.setBidiLevel(currentStyle, 1); - - if (doc instanceof StyledDocument) - sdoc = (StyledDocument) doc; - else - this.doc = doc; - - offset = position; - } - - public void addText(String text) { - try { - if (sdoc != null) - sdoc.insertString(offset, text, currentStyle); - else - doc.insertString(offset, text, null); - - offset += text.length(); - } - catch (BadLocationException e) { - //todo: throw it to RTFEditorKit? - } - } - - public void startGroup() { - stylesStack.push(currentStyle); - currentStyle = new SimpleAttributeSet(currentStyle); - } - - public void endGroup() { - currentStyle = stylesStack.pop(); - } - - public void setBold(boolean enable) { - StyleConstants.setBold(currentStyle, enable); - } - - public void setItalic(boolean enable) { - StyleConstants.setItalic(currentStyle, enable); - } - - public void setUnderline(boolean enable) { - StyleConstants.setUnderline(currentStyle, enable); - } - -} Index: swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFHandler.java =================================================================== --- swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFHandler.java Mon Jul 14 16:35:22 EEST 2008 +++ swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFHandler.java Mon Jul 14 16:35:22 EEST 2008 @@ -0,0 +1,39 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.x.swing.text.rtf; + +/** + * @author Aleksey Lagoshin + */ +public interface RTFHandler { + + public void startGroup(); + + public void endGroup(); + + public void addText(String text); + + public void newParagraph(); + + public void setBold(boolean enable); + + public void setItalic(boolean enable); + + public void setUnderline(boolean enable); + +} Index: swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParserConstants.java =================================================================== --- swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParserConstants.java (revision 676572) +++ swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParserConstants.java Mon Jul 14 17:00:46 EEST 2008 @@ -4,31 +4,54 @@ public interface RTFParserConstants { int EOF = 0; - int BACKSLASH = 1; - int OPEN_BRACE = 5; - int CLOSE_BRACE = 6; - int TEXT = 7; - int RTF = 12; - int FONTTBL = 13; - int STYLESHEET = 14; - int INFO = 15; - int B = 16; - int I = 17; - int UL = 18; - int IGNORED_DESTINATION = 19; - int CONTROL_WORD = 20; - int CONTROL_SYMBOL = 21; - int PARAM = 22; + int IGNORED_DESTINATION = 4; + int ESCAPED_OPEN_BRACE = 5; + int ESCAPED_CLOSE_BRACE = 6; + int ESCAPED_BACKSLASH = 7; + int CONTROL_SYMBOL = 8; + int HEX_DIGIT = 9; + int HEX_CHAR = 10; + int BACKSLASH = 11; + int OPEN_BRACE = 12; + int CLOSE_BRACE = 13; + int TEXT = 14; + int RTF = 19; + int ANSI = 20; + int MAC = 21; + int PC = 22; + int PCA = 23; + int ANSICPG = 24; + int UC = 25; + int U = 26; + int UPR = 27; + int UD = 28; + int FONTTBL = 29; + int COLORTBL = 30; + int STYLESHEET = 31; + int INFO = 32; + int B = 33; + int I = 34; + int UL = 35; + int PAR = 36; + int CONTROL_WORD = 37; + int PARAM = 38; - int IN_CONTROL = 0; - int DEFAULT = 1; + int DEFAULT = 0; + int IN_CONTROL_WORD = 1; String[] tokenImage = { "", - "\"\\\\\"", "\"\\n\"", "\"\\r\"", "\"\\t\"", + "\"\\\\*\"", + "\"\\\\{\"", + "\"\\\\}\"", + "\"\\\\\\\\\"", + "", + "", + "", + "\"\\\\\"", "\"{\"", "\"}\"", "", @@ -37,17 +60,26 @@ "\"\\r\"", "\"\\t\"", "\"rtf\"", + "\"ansi\"", + "\"mac\"", + "\"pc\"", + "\"pca\"", + "\"ansicpg\"", + "\"uc\"", + "\"u\"", + "\"upr\"", + "\"ud\"", "\"fonttbl\"", + "\"colortbl\"", "\"stylesheet\"", "\"info\"", "\"b\"", "\"i\"", "\"ul\"", - "\"*\"", + "\"par\"", "", - "", "", - "", + "", }; } Index: swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/DocumentRTFHandler.java =================================================================== --- swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/DocumentRTFHandler.java Mon Jul 14 16:37:03 EEST 2008 +++ swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/DocumentRTFHandler.java Mon Jul 14 16:37:03 EEST 2008 @@ -0,0 +1,89 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.x.swing.text.rtf; + +import javax.swing.text.*; +import java.util.Stack; + +/** + * @author Aleksey Lagoshin + */ +public class DocumentRTFHandler implements RTFHandler { + + private StyledDocument sdoc; + private Document doc; + + private int offset; + + private Stack stylesStack; + private MutableAttributeSet currentStyle; + + public DocumentRTFHandler(Document doc, int position) { + stylesStack = new Stack(); + currentStyle = new SimpleAttributeSet(); + + if (doc instanceof StyledDocument) + sdoc = (StyledDocument) doc; + else + this.doc = doc; + + offset = position; + } + + public void startGroup() { + stylesStack.push(currentStyle); + currentStyle = new SimpleAttributeSet(currentStyle); + } + + public void endGroup() { + currentStyle = stylesStack.pop(); + } + + public void addText(String text) { + try { + if (sdoc != null) + sdoc.insertString(offset, text, currentStyle); + else + doc.insertString(offset, text, null); + + offset += text.length(); + } + catch (BadLocationException e) { + //todo: throw it to RTFEditorKit? + } + } + + public void newParagraph() { + addText("\n"); + } + + public void setBold(boolean enable) { + StyleConstants.setBold(currentStyle, enable); + } + + public void setItalic(boolean enable) { + StyleConstants.setItalic(currentStyle, enable); + } + + public void setUnderline(boolean enable) { + StyleConstants.setUnderline(currentStyle, enable); + } + +} + + Index: swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/SimpleCharStream.java =================================================================== --- swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/SimpleCharStream.java (revision 676572) +++ swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/SimpleCharStream.java Mon Jul 14 17:00:46 EEST 2008 @@ -8,32 +8,32 @@ public class SimpleCharStream { - public static final boolean staticFlag = true; - static int bufsize; - static int available; - static int tokenBegin; - static public int bufpos = -1; - static protected int bufline[]; - static protected int bufcolumn[]; + public static final boolean staticFlag = false; + int bufsize; + int available; + int tokenBegin; + public int bufpos = -1; + protected int bufline[]; + protected int bufcolumn[]; - static protected int column = 0; - static protected int line = 1; + protected int column = 0; + protected int line = 1; - static protected boolean prevCharIsCR = false; - static protected boolean prevCharIsLF = false; + protected boolean prevCharIsCR = false; + protected boolean prevCharIsLF = false; - static protected java.io.Reader inputStream; + protected java.io.Reader inputStream; - static protected char[] buffer; - static protected int maxNextCharInd = 0; - static protected int inBuf = 0; - static protected int tabSize = 8; + protected char[] buffer; + protected int maxNextCharInd = 0; + protected int inBuf = 0; + protected int tabSize = 8; - static protected void setTabSize(int i) { tabSize = i; } - static protected int getTabSize(int i) { return tabSize; } + protected void setTabSize(int i) { tabSize = i; } + protected int getTabSize(int i) { return tabSize; } - static protected void ExpandBuff(boolean wrapAround) + protected void ExpandBuff(boolean wrapAround) { char[] newbuffer = new char[bufsize + 2048]; int newbufline[] = new int[bufsize + 2048]; @@ -83,7 +83,7 @@ tokenBegin = 0; } - static protected void FillBuff() throws java.io.IOException + protected void FillBuff() throws java.io.IOException { if (maxNextCharInd == available) { @@ -128,7 +128,7 @@ } } - static public char BeginToken() throws java.io.IOException + public char BeginToken() throws java.io.IOException { tokenBegin = -1; char c = readChar(); @@ -137,7 +137,7 @@ return c; } - static protected void UpdateLineColumn(char c) + protected void UpdateLineColumn(char c) { column++; @@ -177,7 +177,7 @@ bufcolumn[bufpos] = column; } - static public char readChar() throws java.io.IOException + public char readChar() throws java.io.IOException { if (inBuf > 0) { @@ -203,7 +203,7 @@ * @see #getEndColumn */ - static public int getColumn() { + public int getColumn() { return bufcolumn[bufpos]; } @@ -212,27 +212,27 @@ * @see #getEndLine */ - static public int getLine() { + public int getLine() { return bufline[bufpos]; } - static public int getEndColumn() { + public int getEndColumn() { return bufcolumn[bufpos]; } - static public int getEndLine() { + public int getEndLine() { return bufline[bufpos]; } - static public int getBeginColumn() { + public int getBeginColumn() { return bufcolumn[tokenBegin]; } - static public int getBeginLine() { + public int getBeginLine() { return bufline[tokenBegin]; } - static public void backup(int amount) { + public void backup(int amount) { inBuf += amount; if ((bufpos -= amount) < 0) @@ -242,10 +242,6 @@ public SimpleCharStream(java.io.Reader dstream, int startline, int startcolumn, int buffersize) { - if (inputStream != null) - throw new Error("\n ERROR: Second call to the constructor of a static SimpleCharStream. You must\n" + - " either use ReInit() or set the JavaCC option STATIC to false\n" + - " during the generation of this class."); inputStream = dstream; line = startline; column = startcolumn - 1; @@ -360,7 +356,7 @@ { ReInit(dstream, startline, startcolumn, 4096); } - static public String GetImage() + public String GetImage() { if (bufpos >= tokenBegin) return new String(buffer, tokenBegin, bufpos - tokenBegin + 1); @@ -369,7 +365,7 @@ new String(buffer, 0, bufpos + 1); } - static public char[] GetSuffix(int len) + public char[] GetSuffix(int len) { char[] ret = new char[len]; @@ -385,7 +381,7 @@ return ret; } - static public void Done() + public void Done() { buffer = null; bufline = null; @@ -395,7 +391,7 @@ /** * Method to adjust line and column numbers for the start of a token. */ - static public void adjustBeginLineColumn(int newLine, int newCol) + public void adjustBeginLineColumn(int newLine, int newCol) { int start = tokenBegin; int len; Index: swing/src/main/java/common/javax/swing/text/rtf/RTFEditorKit.java =================================================================== --- swing/src/main/java/common/javax/swing/text/rtf/RTFEditorKit.java (revision 676572) +++ swing/src/main/java/common/javax/swing/text/rtf/RTFEditorKit.java Mon Jul 14 17:09:12 EEST 2008 @@ -14,10 +14,13 @@ * See the License for the specific language governing permissions and * limitations under the License. */ + package javax.swing.text.rtf; import org.apache.harmony.x.swing.text.rtf.RTFParser; import org.apache.harmony.x.swing.text.rtf.ParseException; +import org.apache.harmony.x.swing.text.rtf.RTFHandler; +import org.apache.harmony.x.swing.text.rtf.DocumentRTFHandler; import javax.swing.text.StyledEditorKit; import javax.swing.text.Document; @@ -25,7 +28,7 @@ import java.io.*; /** - * @author ayzen + * @author Aleksey Lagoshin */ public class RTFEditorKit extends StyledEditorKit { @@ -40,7 +43,8 @@ public void read(InputStream in, Document doc, int pos) throws IOException, BadLocationException { RTFParser parser = new RTFParser(in); try { - parser.parse(doc, pos); + RTFHandler handler = new DocumentRTFHandler(doc, pos); + parser.parse(handler); } catch (ParseException e) { IOException ioex = new IOException(e.toString()); @@ -50,16 +54,26 @@ } } - public void read(Reader in, Document doc, int pos) { + public void read(Reader in, Document doc, int pos) throws IOException, BadLocationException { + RTFParser parser = new RTFParser(in); + try { + RTFHandler handler = new DocumentRTFHandler(doc, pos); + parser.parse(handler); + } + catch (ParseException e) { + IOException ioex = new IOException(e.toString()); + ioex.initCause(e); + throw ioex; - } + } + } public void write(OutputStream out, Document doc, int pos, int len) { - + } public void write(Writer out, Document doc, int pos, int len) { } -} +} \ No newline at end of file Index: swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFEncodings.java =================================================================== --- swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFEncodings.java Mon Jul 14 17:12:25 EEST 2008 +++ swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFEncodings.java Mon Jul 14 17:12:25 EEST 2008 @@ -0,0 +1,94 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package org.apache.harmony.x.swing.text.rtf; + +/** + * This class contains methods for converting RTF code pages to Java encodings and vice versa. + * + * @author Aleksey Lagoshin + */ +public class RTFEncodings { + + /** + * The default encoding for RTF files, it is also used for unsupported code pages. + */ + public static final String DEFAULT_ENCODING = "Cp1252"; + + /** + * Returns an encoding name for Java corresponding to parsed code page. + * For all unsupported code pages this method returns the default encoding + * (see this page) + * + * @param rtfCodePage code page parsed from RTF file + * @return an encoding name for Java + */ + public static String getEncoding(int rtfCodePage) { + switch (rtfCodePage) { + case 437: return "Cp437"; // United States IBM + case 708: return "ISO8859_6"; // Arabic (ASMO 708) + case 709: // Arabic (ASMO 449+, BCON V4) + case 710: // Arabic (transparent Arabic) + case 711: // Arabic (Nafitha Enhanced) + case 720: return DEFAULT_ENCODING; // Arabic (transparent ASMO) + case 819: return "ISO8859_1"; // Windows 3.1 (United States and Western Europe) + case 850: return "Cp850"; // IBM multilingual + case 852: return "Cp852"; // Eastern European + case 860: return "Cp860"; // Portuguese + case 862: return "Cp862"; // Hebrew + case 863: return "Cp863"; // French Canadian + case 864: return "Cp864"; // Arabic + case 865: return "Cp865"; // Norwegian + case 866: return "Cp866"; // Soviet Union + case 874: return "MS874"; // Thai + case 932: return "MS932"; // Japanese + case 936: return "MS936"; // Simplified Chinese + case 949: return "MS949"; // Korean + case 950: return "MS950"; // Traditional Chinese + case 1250: return "Cp1250"; // Eastern European + case 1251: return "Cp1251"; // Cyrillic + case 1252: return "Cp1252"; // Western European + case 1253: return "Cp1253"; // Greek + case 1254: return "Cp1255"; // Turkish + case 1255: return "Cp1255"; // Hebrew + case 1256: return "Cp1256"; // Arabic + case 1257: return "Cp1257"; // Baltic + case 1258: return "Cp1258"; // Vietnamese + case 1361: return "x-Johab"; // Johab + case 10000: return "MacRoman"; // MAC Roman + case 10001: return "SJIS"; // MAC Japan + case 10004: return "MacArabic"; // MAC Arabic + case 10005: return "MacHebrew"; // MAC Hebrew + case 10006: return "MacGreek"; // MAC Greek + case 10007: return "MacCyrillic"; // MAC Cyrillic + case 10029: return "MacCentralEurope"; // MAC Latin2 + case 10081: return "MacTurkish"; // MAC Turkish + case 57002: return "ISCII91"; // Devanagari + case 57003: // Bengali + case 57004: // Tamil + case 57005: // Telugu + case 57006: // Assamese + case 57007: // Oriya + case 57008: // Kannada + case 57009: // Malayalam + case 57010: // Gujarati + case 57011: // Punjabi + default: return DEFAULT_ENCODING; + } + } + +} Index: swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParser.jj =================================================================== --- swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParser.jj (revision 676572) +++ swing/src/main/java/common/org/apache/harmony/x/swing/text/rtf/RTFParser.jj Mon Jul 14 16:39:31 EEST 2008 @@ -1,36 +1,46 @@ /* - Licensed to the Apache Software Foundation (ASF) under one or more - contributor license agreements. See the NOTICE file distributed with - this work for additional information regarding copyright ownership. - The ASF licenses this file to You under the Apache License, Version 2.0 - (the "License"); you may not use this file except in compliance with - the License. You may obtain a copy of the License at - - http://www.apache.org/licenses/LICENSE-2.0 - - Unless required by applicable law or agreed to in writing, software - distributed under the License is distributed on an "AS IS" BASIS, - WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - See the License for the specific language governing permissions and - limitations under the License. + * Licensed to the Apache Software Foundation (ASF) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * The ASF licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. -*/ + */ -//options { +options { // DEBUG_PARSER = true; -// DEBUG_TOKEN_MANAGER=true; +// DEBUG_TOKEN_MANAGER = true; -//} + STATIC = false; +} PARSER_BEGIN(RTFParser) package org.apache.harmony.x.swing.text.rtf; import java.io.*; -import javax.swing.text.Document; +import java.util.Stack; import javax.swing.text.DefaultStyledDocument; public class RTFParser { - private static RTFParserHandler handler; + private RTFHandler handler; + private String encoding = RTFEncodings.DEFAULT_ENCODING; + + private int ucValue = 1; + /** + * This stack contains only UC values as a scope parameter. + * It could be modified to contain additional parameters. + */ + private Stack scopeStack = new Stack(); + public static void main(String args[]) throws Exception { InputStream in; @@ -38,50 +48,51 @@ in = new FileInputStream(args[0]); else in = System.in; - + RTFParser parser = new RTFParser(in); - parser.parse(new DefaultStyledDocument(), 0); + RTFHandler handler = new DocumentRTFHandler(new DefaultStyledDocument(), 0); + parser.parse(handler); } - + + private byte[] getBytes(String str) { + char[] chars = str.toCharArray(); + byte[] bytes = new byte[chars.length]; + for (int i = 0; i < chars.length; i++) + bytes[i] = (byte) chars[i]; + return bytes; -} + } +} + PARSER_END(RTFParser) -/*<*> MORE : + +SKIP : { - "\\" : InControlWord + "\n" +| "\r" +| "\t" } - TOKEN : +<*> +TOKEN: { - > {System.out.println("rtf");} : DEFAULT + : DEFAULT +| : DEFAULT +| : DEFAULT +| : DEFAULT +| : DEFAULT +| <#HEX_DIGIT: ["0"-"9","a"-"f","A"-"F"]> +| > : DEFAULT } - ( - documentBlock() - TOKEN : -{ - {System.out.println("param");} -} - MORE: -{ - <~[]> : DEFAULT -}*/ - <*> MORE: { - : IN_CONTROL + : IN_CONTROL_WORD } - -SKIP : -{ - "\n" -| "\r" -| "\t" -} - <*> TOKEN: { @@ -92,10 +103,10 @@ TOKEN: { - + } - + SKIP: { " " : DEFAULT @@ -104,54 +115,47 @@ | "\t" : DEFAULT } - + TOKEN: { +| +| +| +| +| +| +| +| +| | +| | -| +| | | | //| //| -//| -} +| - -TOKEN: -{ - +| +| : DEFAULT } - -TOKEN: -{ - -| -} - - -TOKEN: -{ - -} - - + SKIP: { <~[]> : DEFAULT } - - -public void parse(Document doc, int position) : +public void parse(RTFHandler handler) : {} { { - handler = new RTFParserHandler(doc, position); + if (handler == null) + throw new NullPointerException("Parameter handler cannot be null"); + this.handler = handler; } file() } @@ -161,14 +165,14 @@ Token param = null; } { - [param=] + [param=] { - return param == null ? -1 : Integer.parseInt(param.image); + return param == null ? -1 : Integer.parseInt(param.image.trim()); } } /** - * Catches all unhandled control words. + * Catches all unhandled control words. */ private void unknownControlWord() : {} @@ -177,96 +181,199 @@ } /** - * Catches all unhandled control symbols. + * Catches all unhandled control symbols. */ -void unknownControlSymbol() : +void unknownControlSymbol() : {} { } -void text() : +/** + * Group which starts with "{\*" and describes destination, currently this part + * is ignored. + */ +void ignoredDestination() : +{} { - Token text; + ignoredBlock() } + +String parseText() : { - text= + byte[] bytes; + String text; + ByteArrayOutputStream textBytes = new ByteArrayOutputStream(); + StringBuffer parsedText = new StringBuffer(); +} - { +{ - handler.addText(text.image); + ( + ( + // Here we collecting all the characters which should be decoded + // using specified encoding + ( + bytes = parseHexBytes() + | bytes = parseTextBytes() + ) { + try { + textBytes.write(bytes); + } catch (IOException e) {} - } + } + )+ + { + // Decoding collected characters using specified encoding + try { + parsedText.append(textBytes.toString(encoding)); + textBytes.reset(); -} + } + catch (UnsupportedEncodingException e) { + throw new ParseException("Unsupported encoding"); + } + } + // Appending characters which don't require decoding to parsed text + | ( + text = parseSpecialCharacter() + | text = parseUnicodeText() + ) { parsedText.append(text); } + )+ + { return parsedText.toString(); } +} -void file() : +byte[] parseHexBytes() : +{ + Token token; + byte[] result = new byte[1]; +} +{ + token = + { + result[0] = (byte) Integer.parseInt(token.image.substring(2), 16); + return result; + } +} + +byte[] parseTextBytes() : +{ + Token token; +} +{ + token = + { + return getBytes(token.image); + } +} + +String parseSpecialCharacter() : {} { - header() document() + { return "{"; } +| { return "}"; } +| { return "\\"; } } -void header() : +String parseUnicodeText() : +{ + int param; + StringBuffer unicodeBuffer = new StringBuffer(); +} +{ + ( + param = parameter() + { + if (param < 0) + param += 65536; + + unicodeBuffer.append((char) param); + } + skipAfterUnicode(ucValue) + )+ + { return unicodeBuffer.toString(); } +} + +void file() : {} { - [] + [] document() } void document() : -{} { + boolean skipped; + String text; +} +{ ( - paragraph() - | LOOKAHEAD( ) - fonttbl() - | LOOKAHEAD( ) - stylesheet() + LOOKAHEAD(2) + header() + | paragraph() | LOOKAHEAD( ) info() | LOOKAHEAD( ) - ignoredDestination() + ignoredDestination() - | documentBlock() + | documentGroup() - | unknownControlWord() - | unknownControlSymbol() + | unknownControlWord() + | unknownControlSymbol() - | text() + | text = parseText() { handler.addText(text); } + | skipped = handleUnexpectedControlWord() { if (!skipped) return; } )+ } /** - * A group. + * A group. */ -void documentBlock() : +void documentGroup() : {} { - { handler.startGroup(); } - document() - { handler.endGroup(); } + startGroup() document() endGroup() } -/** - * Ignored block of RTF file, currently is using to ignore unknown parts - * of file. - */ -void ignoredBlock() : +void startGroup() : {} { - ( - LOOKAHEAD( ) - ignoredDestination() - | characterFormat() //TODO: Need to find a way to ignore control words declared as TOKENS - | unknownControlWord() - | unknownControlSymbol() - | - | ignoredBlock() - )+ + + { + scopeStack.push(ucValue); + handler.startGroup(); -} + } +} -/** - * Group which starts with "{\*" and describes destination, currently this part - * is ignored. - */ -void ignoredDestination() : +void endGroup() : {} { - ignoredBlock() + + { + ucValue = (Integer) scopeStack.pop(); + handler.endGroup(); -} + } +} +void header() : +{} +{ + characterSet() +| LOOKAHEAD( ) + fonttbl() +| LOOKAHEAD( ) + colortbl() +| LOOKAHEAD( ) + stylesheet() +} + +void characterSet() : +{ + int param; +} +{ + { encoding = "Cp1252"; } +| { encoding = "MacRoman"; } +| { encoding = "Cp437"; } +| { encoding = "Cp850"; } +| param = parameter() + { + encoding = RTFEncodings.getEncoding(param); + } +} + /** * Part which describes font table group. */ @@ -277,6 +384,15 @@ } /** + * Part which describes color table group. + */ +void colortbl() : +{} +{ + ignoredBlock() +} + +/** * Part which describes the style sheet group. */ void stylesheet() : @@ -298,10 +414,11 @@ {} { characterFormat() +| { handler.newParagraph(); } } void characterFormat() : -{ +{ int param; } { @@ -309,3 +426,90 @@ | param = parameter() { handler.setItalic(param != 0); } |
    param = parameter() { handler.setUnderline(param != 0); } } + +JAVACODE +/** + * Ignoring a block of RTF file enclosed in braces, it is using + * to ignore unknown or unnecessary parts of file. + */ +private void ignoredBlock() { + Token token; + int nesting = 1; + while (true) { + token = getToken(1); + if (token.kind == OPEN_BRACE) + nesting++; + else if (token.kind == CLOSE_BRACE) { + nesting--; + if (nesting == 0) + return; + } + getNextToken(); + } +} + +JAVACODE +/** + * This function skips an unexpected control word defined as token + * and handle control words which could appear anywhere in a file. + * + * @return true if control was skipped or handled + */ +private boolean handleUnexpectedControlWord() { + Token token = getToken(1); + + if (token.kind == UC) { + getNextToken(); + token = getToken(1); + if (token.kind == PARAM) { + ucValue = Integer.parseInt(token.image); + getNextToken(); + } + return true; + } + + String text = token.image; + if (text.matches("\\\\[a-zA-Z]+")) { + getNextToken(); + token = getToken(1); + if (token.kind == PARAM) + getNextToken(); + return true; + } + + return false; +} + +JAVACODE +/** + * This function skips n characters and/or control words and/or + * control symbols after Unicode character. + */ +private void skipAfterUnicode(int n) { + while (n-- > 0) { + Token token = getToken(1); + // If token is a control word or a control symbol + if (token.image.startsWith("\\")) { + getNextToken(); + token = getToken(1); + // If control word has parameter + if (token.kind == PARAM) + getNextToken(); + //TODO: Need to skip \bin data + } + else if (token.kind == HEX_CHAR) + getNextToken(); + else if (token.kind == TEXT) { + String text = token.image; + if (text.length() <= n + 1) { + n -= text.length() - 1; + getNextToken(); + } + else { + token.image = text.substring(n + 1); + return; + } + } + else throw new ParseException("Wrong token " + token + " while skipping data after Unicode character"); + } +} \ No newline at end of file