jdk/src/share/classes/javax/swing/text/rtf/RTFReader.java
author sherman
Tue, 30 Aug 2011 11:53:11 -0700
changeset 10419 12c063b39232
parent 5506 202f599c92aa
child 22567 5816a47fa4dd
permissions -rw-r--r--
7084245: Update usages of InternalError to use exception chaining Summary: to use new InternalError constructor with cause chainning Reviewed-by: alanb, ksrini, xuelei, neugens Contributed-by: sebastian.sickelmann@gmx.de
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     1
/*
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     2
 * Copyright (c) 1997, 2008, Oracle and/or its affiliates. All rights reserved.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
90ce3da70b43 Initial load
duke
parents:
diff changeset
     4
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
90ce3da70b43 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     7
 * published by the Free Software Foundation.  Oracle designates this
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
     8
 * particular file as subject to the "Classpath" exception as provided
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
     9
 * by Oracle in the LICENSE file that accompanied this code.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    10
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    11
 * This code is distributed in the hope that it will be useful, but WITHOUT
90ce3da70b43 Initial load
duke
parents:
diff changeset
    12
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
90ce3da70b43 Initial load
duke
parents:
diff changeset
    13
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
90ce3da70b43 Initial load
duke
parents:
diff changeset
    14
 * version 2 for more details (a copy is included in the LICENSE file that
90ce3da70b43 Initial load
duke
parents:
diff changeset
    15
 * accompanied this code).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    16
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    17
 * You should have received a copy of the GNU General Public License version
90ce3da70b43 Initial load
duke
parents:
diff changeset
    18
 * 2 along with this work; if not, write to the Free Software Foundation,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    19
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    20
 *
5506
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    22
 * or visit www.oracle.com if you need additional information or have any
202f599c92aa 6943119: Rebrand source copyright notices
ohair
parents: 1639
diff changeset
    23
 * questions.
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    24
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    25
package javax.swing.text.rtf;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    26
90ce3da70b43 Initial load
duke
parents:
diff changeset
    27
import java.lang.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    28
import java.util.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    29
import java.io.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    30
import java.awt.Font;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    31
import java.awt.Color;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    32
90ce3da70b43 Initial load
duke
parents:
diff changeset
    33
import javax.swing.text.*;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    34
90ce3da70b43 Initial load
duke
parents:
diff changeset
    35
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
    36
 * Takes a sequence of RTF tokens and text and appends the text
90ce3da70b43 Initial load
duke
parents:
diff changeset
    37
 * described by the RTF to a <code>StyledDocument</code> (the <em>target</em>).
90ce3da70b43 Initial load
duke
parents:
diff changeset
    38
 * The RTF is lexed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    39
 * from the character stream by the <code>RTFParser</code> which is this class's
90ce3da70b43 Initial load
duke
parents:
diff changeset
    40
 * superclass.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    41
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    42
 * This class is an indirect subclass of OutputStream. It must be closed
90ce3da70b43 Initial load
duke
parents:
diff changeset
    43
 * in order to guarantee that all of the text has been sent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
    44
 * the text acceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    45
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
    46
 *   @see RTFParser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    47
 *   @see java.io.OutputStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
    48
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    49
class RTFReader extends RTFParser
90ce3da70b43 Initial load
duke
parents:
diff changeset
    50
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
    51
  /** The object to which the parsed text is sent. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    52
  StyledDocument target;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    53
90ce3da70b43 Initial load
duke
parents:
diff changeset
    54
  /** Miscellaneous information about the parser's state. This
90ce3da70b43 Initial load
duke
parents:
diff changeset
    55
   *  dictionary is saved and restored when an RTF group begins
90ce3da70b43 Initial load
duke
parents:
diff changeset
    56
   *  or ends. */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    57
  Dictionary<Object, Object> parserState;   /* Current parser state */
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    58
  /** This is the "dst" item from parserState. rtfDestination
90ce3da70b43 Initial load
duke
parents:
diff changeset
    59
   *  is the current rtf destination. It is cached in an instance
90ce3da70b43 Initial load
duke
parents:
diff changeset
    60
   *  variable for speed. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    61
  Destination rtfDestination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    62
  /** This holds the current document attributes. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    63
  MutableAttributeSet documentAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    64
90ce3da70b43 Initial load
duke
parents:
diff changeset
    65
  /** This Dictionary maps Integer font numbers to String font names. */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    66
  Dictionary<Integer, String> fontTable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    67
  /** This array maps color indices to Color objects. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    68
  Color[] colorTable;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    69
  /** This array maps character style numbers to Style objects. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    70
  Style[] characterStyles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    71
  /** This array maps paragraph style numbers to Style objects. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    72
  Style[] paragraphStyles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    73
  /** This array maps section style numbers to Style objects. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    74
  Style[] sectionStyles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    75
90ce3da70b43 Initial load
duke
parents:
diff changeset
    76
  /** This is the RTF version number, extracted from the \rtf keyword.
90ce3da70b43 Initial load
duke
parents:
diff changeset
    77
   *  The version information is currently not used. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    78
  int rtfversion;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    79
90ce3da70b43 Initial load
duke
parents:
diff changeset
    80
  /** <code>true</code> to indicate that if the next keyword is unknown,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    81
   *  the containing group should be ignored. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    82
  boolean ignoreGroupIfUnknownKeyword;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    83
90ce3da70b43 Initial load
duke
parents:
diff changeset
    84
  /** The parameter of the most recently parsed \\ucN keyword,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    85
   *  used for skipping alternative representations after a
90ce3da70b43 Initial load
duke
parents:
diff changeset
    86
   *  Unicode character. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    87
  int skippingCharacters;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    88
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    89
  static private Dictionary<String, RTFAttribute> straightforwardAttributes;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
    90
  static {
90ce3da70b43 Initial load
duke
parents:
diff changeset
    91
      straightforwardAttributes = RTFAttributes.attributesByKeyword();
90ce3da70b43 Initial load
duke
parents:
diff changeset
    92
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
    93
90ce3da70b43 Initial load
duke
parents:
diff changeset
    94
  private MockAttributeSet mockery;
90ce3da70b43 Initial load
duke
parents:
diff changeset
    95
90ce3da70b43 Initial load
duke
parents:
diff changeset
    96
  /* this should be final, but there's a bug in javac... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
    97
  /** textKeywords maps RTF keywords to single-character strings,
90ce3da70b43 Initial load
duke
parents:
diff changeset
    98
   *  for those keywords which simply insert some text. */
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
    99
  static Dictionary<String, String> textKeywords = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   100
  static {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   101
      textKeywords = new Hashtable<String, String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   102
      textKeywords.put("\\",         "\\");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   103
      textKeywords.put("{",          "{");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   104
      textKeywords.put("}",          "}");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   105
      textKeywords.put(" ",          "\u00A0");  /* not in the spec... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   106
      textKeywords.put("~",          "\u00A0");  /* nonbreaking space */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   107
      textKeywords.put("_",          "\u2011");  /* nonbreaking hyphen */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   108
      textKeywords.put("bullet",     "\u2022");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   109
      textKeywords.put("emdash",     "\u2014");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   110
      textKeywords.put("emspace",    "\u2003");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   111
      textKeywords.put("endash",     "\u2013");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   112
      textKeywords.put("enspace",    "\u2002");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   113
      textKeywords.put("ldblquote",  "\u201C");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   114
      textKeywords.put("lquote",     "\u2018");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   115
      textKeywords.put("ltrmark",    "\u200E");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   116
      textKeywords.put("rdblquote",  "\u201D");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   117
      textKeywords.put("rquote",     "\u2019");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   118
      textKeywords.put("rtlmark",    "\u200F");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   119
      textKeywords.put("tab",        "\u0009");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   120
      textKeywords.put("zwj",        "\u200D");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   121
      textKeywords.put("zwnj",       "\u200C");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   122
90ce3da70b43 Initial load
duke
parents:
diff changeset
   123
      /* There is no Unicode equivalent to an optional hyphen, as far as
90ce3da70b43 Initial load
duke
parents:
diff changeset
   124
         I can tell. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   125
      textKeywords.put("-",          "\u2027");  /* TODO: optional hyphen */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   126
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   127
90ce3da70b43 Initial load
duke
parents:
diff changeset
   128
  /* some entries in parserState */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   129
  static final String TabAlignmentKey = "tab_alignment";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   130
  static final String TabLeaderKey = "tab_leader";
90ce3da70b43 Initial load
duke
parents:
diff changeset
   131
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   132
  static Dictionary<String, char[]> characterSets;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   133
  static boolean useNeXTForAnsi = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   134
  static {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   135
      characterSets = new Hashtable<String, char[]>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   136
  }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   137
90ce3da70b43 Initial load
duke
parents:
diff changeset
   138
/* TODO: per-font font encodings ( \fcharset control word ) ? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   139
90ce3da70b43 Initial load
duke
parents:
diff changeset
   140
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   141
 * Creates a new RTFReader instance. Text will be sent to
90ce3da70b43 Initial load
duke
parents:
diff changeset
   142
 * the specified TextAcceptor.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   143
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   144
 * @param destination The TextAcceptor which is to receive the text.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   145
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   146
public RTFReader(StyledDocument destination)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   147
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   148
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   149
90ce3da70b43 Initial load
duke
parents:
diff changeset
   150
    target = destination;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   151
    parserState = new Hashtable<Object, Object>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   152
    fontTable = new Hashtable<Integer, String>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   153
90ce3da70b43 Initial load
duke
parents:
diff changeset
   154
    rtfversion = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   155
90ce3da70b43 Initial load
duke
parents:
diff changeset
   156
    mockery = new MockAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   157
    documentAttributes = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   158
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   159
90ce3da70b43 Initial load
duke
parents:
diff changeset
   160
/** Called when the RTFParser encounters a bin keyword in the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   161
 *  RTF stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   162
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   163
 *  @see RTFParser
90ce3da70b43 Initial load
duke
parents:
diff changeset
   164
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   165
public void handleBinaryBlob(byte[] data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   166
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   167
    if (skippingCharacters > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   168
        /* a blob only counts as one character for skipping purposes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   169
        skippingCharacters --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   170
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   171
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   172
90ce3da70b43 Initial load
duke
parents:
diff changeset
   173
    /* someday, someone will want to do something with blobs */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   174
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   175
90ce3da70b43 Initial load
duke
parents:
diff changeset
   176
90ce3da70b43 Initial load
duke
parents:
diff changeset
   177
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   178
 * Handles any pure text (containing no control characters) in the input
90ce3da70b43 Initial load
duke
parents:
diff changeset
   179
 * stream. Called by the superclass. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   180
public void handleText(String text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   181
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   182
    if (skippingCharacters > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   183
        if (skippingCharacters >= text.length()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   184
            skippingCharacters -= text.length();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   185
            return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   186
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   187
            text = text.substring(skippingCharacters);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   188
            skippingCharacters = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   189
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   190
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   191
90ce3da70b43 Initial load
duke
parents:
diff changeset
   192
    if (rtfDestination != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   193
        rtfDestination.handleText(text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   194
        return;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   195
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   196
90ce3da70b43 Initial load
duke
parents:
diff changeset
   197
    warning("Text with no destination. oops.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   198
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   199
90ce3da70b43 Initial load
duke
parents:
diff changeset
   200
/** The default color for text which has no specified color. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   201
Color defaultColor()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   202
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   203
    return Color.black;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   204
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   205
90ce3da70b43 Initial load
duke
parents:
diff changeset
   206
/** Called by the superclass when a new RTF group is begun.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   207
 *  This implementation saves the current <code>parserState</code>, and gives
90ce3da70b43 Initial load
duke
parents:
diff changeset
   208
 *  the current destination a chance to save its own state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   209
 * @see RTFParser#begingroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   210
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   211
public void begingroup()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   212
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   213
    if (skippingCharacters > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   214
        /* TODO this indicates an error in the RTF. Log it? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   215
        skippingCharacters = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   216
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   217
90ce3da70b43 Initial load
duke
parents:
diff changeset
   218
    /* we do this little dance to avoid cloning the entire state stack and
90ce3da70b43 Initial load
duke
parents:
diff changeset
   219
       immediately throwing it away. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   220
    Object oldSaveState = parserState.get("_savedState");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   221
    if (oldSaveState != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   222
        parserState.remove("_savedState");
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   223
    Dictionary<String, Object> saveState = (Dictionary<String, Object>)((Hashtable)parserState).clone();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   224
    if (oldSaveState != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   225
        saveState.put("_savedState", oldSaveState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   226
    parserState.put("_savedState", saveState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   227
90ce3da70b43 Initial load
duke
parents:
diff changeset
   228
    if (rtfDestination != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   229
        rtfDestination.begingroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   230
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   231
90ce3da70b43 Initial load
duke
parents:
diff changeset
   232
/** Called by the superclass when the current RTF group is closed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   233
 *  This restores the parserState saved by <code>begingroup()</code>
90ce3da70b43 Initial load
duke
parents:
diff changeset
   234
 *  as well as invoking the endgroup method of the current
90ce3da70b43 Initial load
duke
parents:
diff changeset
   235
 *  destination.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   236
 * @see RTFParser#endgroup
90ce3da70b43 Initial load
duke
parents:
diff changeset
   237
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   238
public void endgroup()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   239
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   240
    if (skippingCharacters > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   241
        /* NB this indicates an error in the RTF. Log it? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   242
        skippingCharacters = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   243
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   244
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   245
    Dictionary<Object, Object> restoredState = (Dictionary<Object, Object>)parserState.get("_savedState");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   246
    Destination restoredDestination = (Destination)restoredState.get("dst");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   247
    if (restoredDestination != rtfDestination) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   248
        rtfDestination.close(); /* allow the destination to clean up */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   249
        rtfDestination = restoredDestination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   250
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   251
    Dictionary oldParserState = parserState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   252
    parserState = restoredState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   253
    if (rtfDestination != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   254
        rtfDestination.endgroup(oldParserState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   255
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   256
90ce3da70b43 Initial load
duke
parents:
diff changeset
   257
protected void setRTFDestination(Destination newDestination)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   258
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   259
    /* Check that setting the destination won't close the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   260
       current destination (should never happen) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   261
    Dictionary previousState = (Dictionary)parserState.get("_savedState");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   262
    if (previousState != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   263
        if (rtfDestination != previousState.get("dst")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   264
            warning("Warning, RTF destination overridden, invalid RTF.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   265
            rtfDestination.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   266
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   267
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   268
    rtfDestination = newDestination;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   269
    parserState.put("dst", rtfDestination);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   270
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   271
90ce3da70b43 Initial load
duke
parents:
diff changeset
   272
/** Called by the user when there is no more input (<i>i.e.</i>,
90ce3da70b43 Initial load
duke
parents:
diff changeset
   273
 * at the end of the RTF file.)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   274
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   275
 * @see OutputStream#close
90ce3da70b43 Initial load
duke
parents:
diff changeset
   276
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   277
public void close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   278
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   279
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   280
    Enumeration docProps = documentAttributes.getAttributeNames();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   281
    while(docProps.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   282
        Object propName = docProps.nextElement();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   283
        target.putProperty(propName,
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   284
                           documentAttributes.getAttribute(propName));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   285
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   286
90ce3da70b43 Initial load
duke
parents:
diff changeset
   287
    /* RTFParser should have ensured that all our groups are closed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   288
90ce3da70b43 Initial load
duke
parents:
diff changeset
   289
    warning("RTF filter done.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   290
90ce3da70b43 Initial load
duke
parents:
diff changeset
   291
    super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   292
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   293
90ce3da70b43 Initial load
duke
parents:
diff changeset
   294
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   295
 * Handles a parameterless RTF keyword. This is called by the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   296
 * (RTFParser) when a keyword is found in the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   297
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   298
 * @returns <code>true</code> if the keyword is recognized and handled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   299
 *          <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   300
 * @see RTFParser#handleKeyword
90ce3da70b43 Initial load
duke
parents:
diff changeset
   301
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   302
public boolean handleKeyword(String keyword)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   303
{
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   304
    String item;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   305
    boolean ignoreGroupIfUnknownKeywordSave = ignoreGroupIfUnknownKeyword;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   306
90ce3da70b43 Initial load
duke
parents:
diff changeset
   307
    if (skippingCharacters > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   308
        skippingCharacters --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   309
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   310
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   311
90ce3da70b43 Initial load
duke
parents:
diff changeset
   312
    ignoreGroupIfUnknownKeyword = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   313
90ce3da70b43 Initial load
duke
parents:
diff changeset
   314
    if ((item = textKeywords.get(keyword)) != null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   315
        handleText(item);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   316
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   317
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   318
90ce3da70b43 Initial load
duke
parents:
diff changeset
   319
    if (keyword.equals("fonttbl")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   320
        setRTFDestination(new FonttblDestination());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   321
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   322
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   323
90ce3da70b43 Initial load
duke
parents:
diff changeset
   324
    if (keyword.equals("colortbl")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   325
        setRTFDestination(new ColortblDestination());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   326
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   327
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   328
90ce3da70b43 Initial load
duke
parents:
diff changeset
   329
    if (keyword.equals("stylesheet")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   330
        setRTFDestination(new StylesheetDestination());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   331
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   332
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   333
90ce3da70b43 Initial load
duke
parents:
diff changeset
   334
    if (keyword.equals("info")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   335
        setRTFDestination(new InfoDestination());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   336
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   337
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   338
90ce3da70b43 Initial load
duke
parents:
diff changeset
   339
    if (keyword.equals("mac")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   340
        setCharacterSet("mac");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   341
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   342
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   343
90ce3da70b43 Initial load
duke
parents:
diff changeset
   344
    if (keyword.equals("ansi")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   345
        if (useNeXTForAnsi)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   346
            setCharacterSet("NeXT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   347
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   348
            setCharacterSet("ansi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   349
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   350
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   351
90ce3da70b43 Initial load
duke
parents:
diff changeset
   352
    if (keyword.equals("next")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   353
        setCharacterSet("NeXT");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   354
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   355
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   356
90ce3da70b43 Initial load
duke
parents:
diff changeset
   357
    if (keyword.equals("pc")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   358
        setCharacterSet("cpg437"); /* IBM Code Page 437 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   359
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   360
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   361
90ce3da70b43 Initial load
duke
parents:
diff changeset
   362
    if (keyword.equals("pca")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   363
        setCharacterSet("cpg850"); /* IBM Code Page 850 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   364
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   365
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   366
90ce3da70b43 Initial load
duke
parents:
diff changeset
   367
    if (keyword.equals("*")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   368
        ignoreGroupIfUnknownKeyword = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   369
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   370
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   371
90ce3da70b43 Initial load
duke
parents:
diff changeset
   372
    if (rtfDestination != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   373
        if(rtfDestination.handleKeyword(keyword))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   374
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   375
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   376
90ce3da70b43 Initial load
duke
parents:
diff changeset
   377
    /* this point is reached only if the keyword is unrecognized */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   378
90ce3da70b43 Initial load
duke
parents:
diff changeset
   379
    /* other destinations we don't understand and therefore ignore */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   380
    if (keyword.equals("aftncn") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   381
        keyword.equals("aftnsep") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   382
        keyword.equals("aftnsepc") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   383
        keyword.equals("annotation") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   384
        keyword.equals("atnauthor") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   385
        keyword.equals("atnicn") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   386
        keyword.equals("atnid") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   387
        keyword.equals("atnref") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   388
        keyword.equals("atntime") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   389
        keyword.equals("atrfend") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   390
        keyword.equals("atrfstart") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   391
        keyword.equals("bkmkend") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   392
        keyword.equals("bkmkstart") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   393
        keyword.equals("datafield") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   394
        keyword.equals("do") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   395
        keyword.equals("dptxbxtext") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   396
        keyword.equals("falt") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   397
        keyword.equals("field") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   398
        keyword.equals("file") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   399
        keyword.equals("filetbl") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   400
        keyword.equals("fname") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   401
        keyword.equals("fontemb") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   402
        keyword.equals("fontfile") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   403
        keyword.equals("footer") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   404
        keyword.equals("footerf") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   405
        keyword.equals("footerl") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   406
        keyword.equals("footerr") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   407
        keyword.equals("footnote") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   408
        keyword.equals("ftncn") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   409
        keyword.equals("ftnsep") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   410
        keyword.equals("ftnsepc") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   411
        keyword.equals("header") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   412
        keyword.equals("headerf") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   413
        keyword.equals("headerl") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   414
        keyword.equals("headerr") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   415
        keyword.equals("keycode") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   416
        keyword.equals("nextfile") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   417
        keyword.equals("object") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   418
        keyword.equals("pict") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   419
        keyword.equals("pn") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   420
        keyword.equals("pnseclvl") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   421
        keyword.equals("pntxtb") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   422
        keyword.equals("pntxta") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   423
        keyword.equals("revtbl") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   424
        keyword.equals("rxe") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   425
        keyword.equals("tc") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   426
        keyword.equals("template") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   427
        keyword.equals("txe") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   428
        keyword.equals("xe")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   429
        ignoreGroupIfUnknownKeywordSave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   430
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   431
90ce3da70b43 Initial load
duke
parents:
diff changeset
   432
    if (ignoreGroupIfUnknownKeywordSave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   433
        setRTFDestination(new DiscardingDestination());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   434
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   435
90ce3da70b43 Initial load
duke
parents:
diff changeset
   436
    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   437
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   438
90ce3da70b43 Initial load
duke
parents:
diff changeset
   439
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   440
 * Handles an RTF keyword and its integer parameter.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   441
 * This is called by the superclass
90ce3da70b43 Initial load
duke
parents:
diff changeset
   442
 * (RTFParser) when a keyword is found in the input stream.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   443
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   444
 * @returns <code>true</code> if the keyword is recognized and handled;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   445
 *          <code>false</code> otherwise
90ce3da70b43 Initial load
duke
parents:
diff changeset
   446
 * @see RTFParser#handleKeyword
90ce3da70b43 Initial load
duke
parents:
diff changeset
   447
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   448
public boolean handleKeyword(String keyword, int parameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   449
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   450
    boolean ignoreGroupIfUnknownKeywordSave = ignoreGroupIfUnknownKeyword;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   451
90ce3da70b43 Initial load
duke
parents:
diff changeset
   452
    if (skippingCharacters > 0) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   453
        skippingCharacters --;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   454
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   455
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   456
90ce3da70b43 Initial load
duke
parents:
diff changeset
   457
    ignoreGroupIfUnknownKeyword = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   458
90ce3da70b43 Initial load
duke
parents:
diff changeset
   459
    if (keyword.equals("uc")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   460
        /* count of characters to skip after a unicode character */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   461
        parserState.put("UnicodeSkip", Integer.valueOf(parameter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   462
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   463
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   464
    if (keyword.equals("u")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   465
        if (parameter < 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   466
            parameter = parameter + 65536;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   467
        handleText((char)parameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   468
        Number skip = (Number)(parserState.get("UnicodeSkip"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   469
        if (skip != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   470
            skippingCharacters = skip.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   471
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   472
            skippingCharacters = 1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   473
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   474
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   475
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   476
90ce3da70b43 Initial load
duke
parents:
diff changeset
   477
    if (keyword.equals("rtf")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   478
        rtfversion = parameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   479
        setRTFDestination(new DocumentDestination());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   480
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   482
90ce3da70b43 Initial load
duke
parents:
diff changeset
   483
    if (keyword.startsWith("NeXT") ||
90ce3da70b43 Initial load
duke
parents:
diff changeset
   484
        keyword.equals("private"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   485
        ignoreGroupIfUnknownKeywordSave = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   486
90ce3da70b43 Initial load
duke
parents:
diff changeset
   487
    if (rtfDestination != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   488
        if(rtfDestination.handleKeyword(keyword, parameter))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   489
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   490
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   491
90ce3da70b43 Initial load
duke
parents:
diff changeset
   492
    /* this point is reached only if the keyword is unrecognized */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   493
90ce3da70b43 Initial load
duke
parents:
diff changeset
   494
    if (ignoreGroupIfUnknownKeywordSave) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   495
        setRTFDestination(new DiscardingDestination());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   496
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   497
90ce3da70b43 Initial load
duke
parents:
diff changeset
   498
    return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   499
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   500
90ce3da70b43 Initial load
duke
parents:
diff changeset
   501
private void setTargetAttribute(String name, Object value)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   502
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   503
//    target.changeAttributes(new LFDictionary(LFArray.arrayWithObject(value), LFArray.arrayWithObject(name)));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   504
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   505
90ce3da70b43 Initial load
duke
parents:
diff changeset
   506
/**
90ce3da70b43 Initial load
duke
parents:
diff changeset
   507
 * setCharacterSet sets the current translation table to correspond with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   508
 * the named character set. The character set is loaded if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   509
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   510
 * @see AbstractFilter
90ce3da70b43 Initial load
duke
parents:
diff changeset
   511
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   512
public void setCharacterSet(String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   513
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   514
    Object set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   515
90ce3da70b43 Initial load
duke
parents:
diff changeset
   516
    try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   517
        set = getCharacterSet(name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   518
    } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   519
        warning("Exception loading RTF character set \"" + name + "\": " + e);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   520
        set = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   521
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   522
90ce3da70b43 Initial load
duke
parents:
diff changeset
   523
    if (set != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   524
        translationTable = (char[])set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   525
    } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   526
        warning("Unknown RTF character set \"" + name + "\"");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   527
        if (!name.equals("ansi")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   528
            try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   529
                translationTable = (char[])getCharacterSet("ansi");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   530
            } catch (IOException e) {
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
   531
                throw new InternalError("RTFReader: Unable to find character set resources (" + e + ")", e);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   532
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   533
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   534
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   535
90ce3da70b43 Initial load
duke
parents:
diff changeset
   536
    setTargetAttribute(Constants.RTFCharacterSet, name);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   537
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   538
90ce3da70b43 Initial load
duke
parents:
diff changeset
   539
/** Adds a character set to the RTFReader's list
90ce3da70b43 Initial load
duke
parents:
diff changeset
   540
 *  of known character sets */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   541
public static void
90ce3da70b43 Initial load
duke
parents:
diff changeset
   542
defineCharacterSet(String name, char[] table)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   543
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   544
    if (table.length < 256)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   545
        throw new IllegalArgumentException("Translation table must have 256 entries.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   546
    characterSets.put(name, table);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   547
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   548
90ce3da70b43 Initial load
duke
parents:
diff changeset
   549
/** Looks up a named character set. A character set is a 256-entry
90ce3da70b43 Initial load
duke
parents:
diff changeset
   550
 *  array of characters, mapping unsigned byte values to their Unicode
90ce3da70b43 Initial load
duke
parents:
diff changeset
   551
 *  equivalents. The character set is loaded if necessary.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   552
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   553
 *  @returns the character set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   554
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   555
public static Object
90ce3da70b43 Initial load
duke
parents:
diff changeset
   556
getCharacterSet(final String name)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   557
    throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   558
{
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   559
    char[] set = characterSets.get(name);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   560
    if (set == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   561
      InputStream charsetStream;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   562
      charsetStream = java.security.AccessController.
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   563
              doPrivileged(new java.security.PrivilegedAction<InputStream>() {
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   564
          public InputStream run() {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   565
              return RTFReader.class.getResourceAsStream
90ce3da70b43 Initial load
duke
parents:
diff changeset
   566
                                     ("charsets/" + name + ".txt");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   567
          }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   568
      });
90ce3da70b43 Initial load
duke
parents:
diff changeset
   569
      set = readCharset(charsetStream);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   570
      defineCharacterSet(name, set);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   571
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   572
    return set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   573
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   574
90ce3da70b43 Initial load
duke
parents:
diff changeset
   575
/** Parses a character set from an InputStream. The character set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   576
 * must contain 256 decimal integers, separated by whitespace, with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   577
 * no punctuation. B- and C- style comments are allowed.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   578
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   579
 * @returns the newly read character set
90ce3da70b43 Initial load
duke
parents:
diff changeset
   580
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   581
static char[] readCharset(InputStream strm)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   582
     throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   583
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   584
    char[] values = new char[256];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   585
    int i;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   586
    StreamTokenizer in = new StreamTokenizer(new BufferedReader(
90ce3da70b43 Initial load
duke
parents:
diff changeset
   587
            new InputStreamReader(strm, "ISO-8859-1")));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   588
90ce3da70b43 Initial load
duke
parents:
diff changeset
   589
    in.eolIsSignificant(false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   590
    in.commentChar('#');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   591
    in.slashSlashComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   592
    in.slashStarComments(true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   593
90ce3da70b43 Initial load
duke
parents:
diff changeset
   594
    i = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   595
    while (i < 256) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   596
        int ttype;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   597
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   598
            ttype = in.nextToken();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   599
        } catch (Exception e) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   600
            throw new IOException("Unable to read from character set file (" + e + ")");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   601
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   602
        if (ttype != in.TT_NUMBER) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   603
//          System.out.println("Bad token: type=" + ttype + " tok=" + in.sval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   604
            throw new IOException("Unexpected token in character set file");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   605
//          continue;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   606
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   607
        values[i] = (char)(in.nval);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   608
        i++;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   609
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   610
90ce3da70b43 Initial load
duke
parents:
diff changeset
   611
    return values;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   612
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   613
90ce3da70b43 Initial load
duke
parents:
diff changeset
   614
static char[] readCharset(java.net.URL href)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   615
     throws IOException
90ce3da70b43 Initial load
duke
parents:
diff changeset
   616
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   617
    return readCharset(href.openStream());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   618
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   619
90ce3da70b43 Initial load
duke
parents:
diff changeset
   620
/** An interface (could be an entirely abstract class) describing
90ce3da70b43 Initial load
duke
parents:
diff changeset
   621
 *  a destination. The RTF reader always has a current destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   622
 *  which is where text is sent.
90ce3da70b43 Initial load
duke
parents:
diff changeset
   623
 *
90ce3da70b43 Initial load
duke
parents:
diff changeset
   624
 *  @see RTFReader
90ce3da70b43 Initial load
duke
parents:
diff changeset
   625
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   626
interface Destination {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   627
    void handleBinaryBlob(byte[] data);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   628
    void handleText(String text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   629
    boolean handleKeyword(String keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   630
    boolean handleKeyword(String keyword, int parameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   631
90ce3da70b43 Initial load
duke
parents:
diff changeset
   632
    void begingroup();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   633
    void endgroup(Dictionary oldState);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   634
90ce3da70b43 Initial load
duke
parents:
diff changeset
   635
    void close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   636
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   637
90ce3da70b43 Initial load
duke
parents:
diff changeset
   638
/** This data-sink class is used to implement ignored destinations
90ce3da70b43 Initial load
duke
parents:
diff changeset
   639
 *  (e.g. {\*\blegga blah blah blah} )
90ce3da70b43 Initial load
duke
parents:
diff changeset
   640
 *  It accepts all keywords and text but does nothing with them. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   641
class DiscardingDestination implements Destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   642
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   643
    public void handleBinaryBlob(byte[] data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   644
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   645
        /* Discard binary blobs. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   646
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   647
90ce3da70b43 Initial load
duke
parents:
diff changeset
   648
    public void handleText(String text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   649
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   650
        /* Discard text. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   651
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   652
90ce3da70b43 Initial load
duke
parents:
diff changeset
   653
    public boolean handleKeyword(String text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   654
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   655
        /* Accept and discard keywords. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   656
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   657
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   658
90ce3da70b43 Initial load
duke
parents:
diff changeset
   659
    public boolean handleKeyword(String text, int parameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   660
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   661
        /* Accept and discard parameterized keywords. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   662
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   663
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   664
90ce3da70b43 Initial load
duke
parents:
diff changeset
   665
    public void begingroup()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   666
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   667
        /* Ignore groups --- the RTFReader will keep track of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   668
           current group level as necessary */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   669
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   670
90ce3da70b43 Initial load
duke
parents:
diff changeset
   671
    public void endgroup(Dictionary oldState)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   672
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   673
        /* Ignore groups */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   674
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   675
90ce3da70b43 Initial load
duke
parents:
diff changeset
   676
    public void close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   677
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   678
        /* No end-of-destination cleanup needed */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   679
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   680
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   681
90ce3da70b43 Initial load
duke
parents:
diff changeset
   682
/** Reads the fonttbl group, inserting fonts into the RTFReader's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   683
 *  fontTable dictionary. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   684
class FonttblDestination implements Destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   685
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   686
    int nextFontNumber;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   687
    Integer fontNumberKey = null;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   688
    String nextFontFamily;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   689
90ce3da70b43 Initial load
duke
parents:
diff changeset
   690
    public void handleBinaryBlob(byte[] data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   691
    { /* Discard binary blobs. */ }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   692
90ce3da70b43 Initial load
duke
parents:
diff changeset
   693
    public void handleText(String text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   694
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   695
        int semicolon = text.indexOf(';');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   696
        String fontName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   697
90ce3da70b43 Initial load
duke
parents:
diff changeset
   698
        if (semicolon > -1)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   699
            fontName = text.substring(0, semicolon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   700
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   701
            fontName = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   702
90ce3da70b43 Initial load
duke
parents:
diff changeset
   703
90ce3da70b43 Initial load
duke
parents:
diff changeset
   704
        /* TODO: do something with the font family. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   705
90ce3da70b43 Initial load
duke
parents:
diff changeset
   706
        if (nextFontNumber == -1
90ce3da70b43 Initial load
duke
parents:
diff changeset
   707
            && fontNumberKey != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   708
            //font name might be broken across multiple calls
90ce3da70b43 Initial load
duke
parents:
diff changeset
   709
            fontName = fontTable.get(fontNumberKey) + fontName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   710
        } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   711
            fontNumberKey = Integer.valueOf(nextFontNumber);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   712
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   713
        fontTable.put(fontNumberKey, fontName);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   714
90ce3da70b43 Initial load
duke
parents:
diff changeset
   715
        nextFontNumber = -1;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   716
        nextFontFamily = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   717
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   718
90ce3da70b43 Initial load
duke
parents:
diff changeset
   719
    public boolean handleKeyword(String keyword)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   720
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   721
        if (keyword.charAt(0) == 'f') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   722
            nextFontFamily = keyword.substring(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   723
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   724
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   725
90ce3da70b43 Initial load
duke
parents:
diff changeset
   726
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   727
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   728
90ce3da70b43 Initial load
duke
parents:
diff changeset
   729
    public boolean handleKeyword(String keyword, int parameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   730
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   731
        if (keyword.equals("f")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   732
            nextFontNumber = parameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   733
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   734
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   735
90ce3da70b43 Initial load
duke
parents:
diff changeset
   736
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   737
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   738
90ce3da70b43 Initial load
duke
parents:
diff changeset
   739
    /* Groups are irrelevant. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   740
    public void begingroup() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   741
    public void endgroup(Dictionary oldState) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   742
90ce3da70b43 Initial load
duke
parents:
diff changeset
   743
    /* currently, the only thing we do when the font table ends is
90ce3da70b43 Initial load
duke
parents:
diff changeset
   744
       dump its contents to the debugging log. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   745
    public void close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   746
    {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   747
        Enumeration<Integer> nums = fontTable.keys();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   748
        warning("Done reading font table.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   749
        while(nums.hasMoreElements()) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   750
            Integer num = nums.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   751
            warning("Number " + num + ": " + fontTable.get(num));
90ce3da70b43 Initial load
duke
parents:
diff changeset
   752
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   753
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   754
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   755
90ce3da70b43 Initial load
duke
parents:
diff changeset
   756
/** Reads the colortbl group. Upon end-of-group, the RTFReader's
90ce3da70b43 Initial load
duke
parents:
diff changeset
   757
 *  color table is set to an array containing the read colors. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   758
class ColortblDestination implements Destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   759
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
   760
    int red, green, blue;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   761
    Vector<Color> proTemTable;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   762
90ce3da70b43 Initial load
duke
parents:
diff changeset
   763
    public ColortblDestination()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   764
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   765
        red = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   766
        green = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   767
        blue = 0;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   768
        proTemTable = new Vector<Color>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   769
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   770
90ce3da70b43 Initial load
duke
parents:
diff changeset
   771
    public void handleText(String text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   772
    {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   773
        int index;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   774
90ce3da70b43 Initial load
duke
parents:
diff changeset
   775
        for (index = 0; index < text.length(); index ++) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   776
            if (text.charAt(index) == ';') {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   777
                Color newColor;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   778
                newColor = new Color(red, green, blue);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   779
                proTemTable.addElement(newColor);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   780
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   781
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   782
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   783
90ce3da70b43 Initial load
duke
parents:
diff changeset
   784
    public void close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   785
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   786
        int count = proTemTable.size();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   787
        warning("Done reading color table, " + count + " entries.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   788
        colorTable = new Color[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   789
        proTemTable.copyInto(colorTable);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   790
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   791
90ce3da70b43 Initial load
duke
parents:
diff changeset
   792
    public boolean handleKeyword(String keyword, int parameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   793
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   794
        if (keyword.equals("red"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   795
            red = parameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   796
        else if (keyword.equals("green"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   797
            green = parameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   798
        else if (keyword.equals("blue"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
   799
            blue = parameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   800
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   801
            return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   802
90ce3da70b43 Initial load
duke
parents:
diff changeset
   803
        return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   804
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   805
90ce3da70b43 Initial load
duke
parents:
diff changeset
   806
    /* Colortbls don't understand any parameterless keywords */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   807
    public boolean handleKeyword(String keyword) { return false; }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   808
90ce3da70b43 Initial load
duke
parents:
diff changeset
   809
    /* Groups are irrelevant. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   810
    public void begingroup() {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   811
    public void endgroup(Dictionary oldState) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   812
90ce3da70b43 Initial load
duke
parents:
diff changeset
   813
    /* Shouldn't see any binary blobs ... */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   814
    public void handleBinaryBlob(byte[] data) {}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   815
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
   816
90ce3da70b43 Initial load
duke
parents:
diff changeset
   817
/** Handles the stylesheet keyword. Styles are read and sorted
90ce3da70b43 Initial load
duke
parents:
diff changeset
   818
 *  into the three style arrays in the RTFReader. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   819
class StylesheetDestination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   820
    extends DiscardingDestination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   821
    implements Destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   822
{
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   823
    Dictionary<Integer, StyleDefiningDestination> definedStyles;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   824
90ce3da70b43 Initial load
duke
parents:
diff changeset
   825
    public StylesheetDestination()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   826
    {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   827
        definedStyles = new Hashtable<Integer, StyleDefiningDestination>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   828
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   829
90ce3da70b43 Initial load
duke
parents:
diff changeset
   830
    public void begingroup()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   831
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   832
        setRTFDestination(new StyleDefiningDestination());
90ce3da70b43 Initial load
duke
parents:
diff changeset
   833
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   834
90ce3da70b43 Initial load
duke
parents:
diff changeset
   835
    public void close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   836
    {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   837
        Vector<Style> chrStyles = new Vector<Style>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   838
        Vector<Style> pgfStyles = new Vector<Style>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   839
        Vector<Style> secStyles = new Vector<Style>();
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   840
        Enumeration<StyleDefiningDestination> styles = definedStyles.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   841
        while(styles.hasMoreElements()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   842
            StyleDefiningDestination style;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   843
            Style defined;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   844
            style = styles.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   845
            defined = style.realize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   846
            warning("Style "+style.number+" ("+style.styleName+"): "+defined);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   847
            String stype = (String)defined.getAttribute(Constants.StyleType);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   848
            Vector<Style> toSet;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   849
            if (stype.equals(Constants.STSection)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   850
                toSet = secStyles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   851
            } else if (stype.equals(Constants.STCharacter)) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   852
                toSet = chrStyles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   853
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   854
                toSet = pgfStyles;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   855
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   856
            if (toSet.size() <= style.number)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   857
                toSet.setSize(style.number + 1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   858
            toSet.setElementAt(defined, style.number);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   859
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   860
        if (!(chrStyles.isEmpty())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   861
            Style[] styleArray = new Style[chrStyles.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   862
            chrStyles.copyInto(styleArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   863
            characterStyles = styleArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   864
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   865
        if (!(pgfStyles.isEmpty())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   866
            Style[] styleArray = new Style[pgfStyles.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   867
            pgfStyles.copyInto(styleArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   868
            paragraphStyles = styleArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   869
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   870
        if (!(secStyles.isEmpty())) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   871
            Style[] styleArray = new Style[secStyles.size()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
   872
            secStyles.copyInto(styleArray);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   873
            sectionStyles = styleArray;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   874
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   875
90ce3da70b43 Initial load
duke
parents:
diff changeset
   876
/* (old debugging code)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   877
        int i, m;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   878
        if (characterStyles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   879
          m = characterStyles.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   880
          for(i=0;i<m;i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   881
            warnings.println("chrStyle["+i+"]="+characterStyles[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   882
        } else warnings.println("No character styles.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   883
        if (paragraphStyles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   884
          m = paragraphStyles.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   885
          for(i=0;i<m;i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   886
            warnings.println("pgfStyle["+i+"]="+paragraphStyles[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   887
        } else warnings.println("No paragraph styles.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   888
        if (sectionStyles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   889
          m = characterStyles.length;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   890
          for(i=0;i<m;i++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   891
            warnings.println("secStyle["+i+"]="+sectionStyles[i]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   892
        } else warnings.println("No section styles.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
   893
*/
90ce3da70b43 Initial load
duke
parents:
diff changeset
   894
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   895
90ce3da70b43 Initial load
duke
parents:
diff changeset
   896
    /** This subclass handles an individual style */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   897
    class StyleDefiningDestination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   898
        extends AttributeTrackingDestination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   899
        implements Destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
   900
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   901
        final int STYLENUMBER_NONE = 222;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   902
        boolean additive;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   903
        boolean characterStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   904
        boolean sectionStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   905
        public String styleName;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   906
        public int number;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   907
        int basedOn;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   908
        int nextStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   909
        boolean hidden;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   910
90ce3da70b43 Initial load
duke
parents:
diff changeset
   911
        Style realizedStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   912
90ce3da70b43 Initial load
duke
parents:
diff changeset
   913
        public StyleDefiningDestination()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   914
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   915
            additive = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   916
            characterStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   917
            sectionStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   918
            styleName = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   919
            number = 0;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   920
            basedOn = STYLENUMBER_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   921
            nextStyle = STYLENUMBER_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   922
            hidden = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   923
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   924
90ce3da70b43 Initial load
duke
parents:
diff changeset
   925
        public void handleText(String text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   926
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   927
            if (styleName != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   928
                styleName = styleName + text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   929
            else
90ce3da70b43 Initial load
duke
parents:
diff changeset
   930
                styleName = text;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   931
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   932
90ce3da70b43 Initial load
duke
parents:
diff changeset
   933
        public void close() {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   934
            int semicolon = (styleName == null) ? 0 : styleName.indexOf(';');
90ce3da70b43 Initial load
duke
parents:
diff changeset
   935
            if (semicolon > 0)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   936
                styleName = styleName.substring(0, semicolon);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   937
            definedStyles.put(Integer.valueOf(number), this);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   938
            super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   939
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   940
90ce3da70b43 Initial load
duke
parents:
diff changeset
   941
        public boolean handleKeyword(String keyword)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   942
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   943
            if (keyword.equals("additive")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   944
                additive = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   945
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   946
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   947
            if (keyword.equals("shidden")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   948
                hidden = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   949
                return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   950
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   951
            return super.handleKeyword(keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   952
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   953
90ce3da70b43 Initial load
duke
parents:
diff changeset
   954
        public boolean handleKeyword(String keyword, int parameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   955
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   956
            if (keyword.equals("s")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   957
                characterStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   958
                sectionStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   959
                number = parameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   960
            } else if (keyword.equals("cs")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   961
                characterStyle = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   962
                sectionStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   963
                number = parameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   964
            } else if (keyword.equals("ds")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   965
                characterStyle = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   966
                sectionStyle = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   967
                number = parameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   968
            } else if (keyword.equals("sbasedon")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   969
                basedOn = parameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   970
            } else if (keyword.equals("snext")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   971
                nextStyle = parameter;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   972
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   973
                return super.handleKeyword(keyword, parameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   974
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   975
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   976
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   977
90ce3da70b43 Initial load
duke
parents:
diff changeset
   978
        public Style realize()
90ce3da70b43 Initial load
duke
parents:
diff changeset
   979
        {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   980
            Style basis = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   981
            Style next = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   982
90ce3da70b43 Initial load
duke
parents:
diff changeset
   983
            if (realizedStyle != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
   984
                return realizedStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
   985
90ce3da70b43 Initial load
duke
parents:
diff changeset
   986
            if (basedOn != STYLENUMBER_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   987
                StyleDefiningDestination styleDest;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
   988
                styleDest = definedStyles.get(Integer.valueOf(basedOn));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
   989
                if (styleDest != null && styleDest != this) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
   990
                    basis = styleDest.realize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
   991
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   992
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
   993
90ce3da70b43 Initial load
duke
parents:
diff changeset
   994
            /* NB: Swing StyleContext doesn't allow distinct styles with
90ce3da70b43 Initial load
duke
parents:
diff changeset
   995
               the same name; RTF apparently does. This may confuse the
90ce3da70b43 Initial load
duke
parents:
diff changeset
   996
               user. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
   997
            realizedStyle = target.addStyle(styleName, basis);
90ce3da70b43 Initial load
duke
parents:
diff changeset
   998
90ce3da70b43 Initial load
duke
parents:
diff changeset
   999
            if (characterStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1000
                realizedStyle.addAttributes(currentTextAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1001
                realizedStyle.addAttribute(Constants.StyleType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1002
                                           Constants.STCharacter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1003
            } else if (sectionStyle) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1004
                realizedStyle.addAttributes(currentSectionAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1005
                realizedStyle.addAttribute(Constants.StyleType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1006
                                           Constants.STSection);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1007
            } else { /* must be a paragraph style */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1008
                realizedStyle.addAttributes(currentParagraphAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1009
                realizedStyle.addAttribute(Constants.StyleType,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1010
                                           Constants.STParagraph);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1011
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1012
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1013
            if (nextStyle != STYLENUMBER_NONE) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1014
                StyleDefiningDestination styleDest;
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1015
                styleDest = definedStyles.get(Integer.valueOf(nextStyle));
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1016
                if (styleDest != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1017
                    next = styleDest.realize();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1018
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1019
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1020
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1021
            if (next != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1022
                realizedStyle.addAttribute(Constants.StyleNext, next);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1023
            realizedStyle.addAttribute(Constants.StyleAdditive,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1024
                                       Boolean.valueOf(additive));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1025
            realizedStyle.addAttribute(Constants.StyleHidden,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1026
                                       Boolean.valueOf(hidden));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1027
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1028
            return realizedStyle;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1029
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1030
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1031
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1032
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1033
/** Handles the info group. Currently no info keywords are recognized
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1034
 *  so this is a subclass of DiscardingDestination. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1035
class InfoDestination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1036
    extends DiscardingDestination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1037
    implements Destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1038
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1039
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1040
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1041
/** RTFReader.TextHandlingDestination is an abstract RTF destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1042
 *  which simply tracks the attributes specified by the RTF control words
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1043
 *  in internal form and can produce acceptable AttributeSets for the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1044
 *  current character, paragraph, and section attributes. It is up
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1045
 *  to the subclasses to determine what is done with the actual text. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1046
abstract class AttributeTrackingDestination implements Destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1047
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1048
    /** This is the "chr" element of parserState, cached for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1049
     *  more efficient use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1050
    MutableAttributeSet characterAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1051
    /** This is the "pgf" element of parserState, cached for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1052
     *  more efficient use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1053
    MutableAttributeSet paragraphAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1054
    /** This is the "sec" element of parserState, cached for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1055
     *  more efficient use */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1056
    MutableAttributeSet sectionAttributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1057
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1058
    public AttributeTrackingDestination()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1059
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1060
        characterAttributes = rootCharacterAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1061
        parserState.put("chr", characterAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1062
        paragraphAttributes = rootParagraphAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1063
        parserState.put("pgf", paragraphAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1064
        sectionAttributes = rootSectionAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1065
        parserState.put("sec", sectionAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1066
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1067
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1068
    abstract public void handleText(String text);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1069
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1070
    public void handleBinaryBlob(byte[] data)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1071
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1072
        /* This should really be in TextHandlingDestination, but
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1073
         * since *nobody* does anything with binary blobs, this
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1074
         * is more convenient. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1075
        warning("Unexpected binary data in RTF file.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1076
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1077
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1078
    public void begingroup()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1079
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1080
        AttributeSet characterParent = currentTextAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1081
        AttributeSet paragraphParent = currentParagraphAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1082
        AttributeSet sectionParent = currentSectionAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1083
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1084
        /* It would probably be more efficient to use the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1085
         * resolver property of the attributes set for
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1086
         * implementing rtf groups,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1087
         * but that's needed for styles. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1088
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1089
        /* update the cached attribute dictionaries */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1090
        characterAttributes = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1091
        characterAttributes.addAttributes(characterParent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1092
        parserState.put("chr", characterAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1093
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1094
        paragraphAttributes = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1095
        paragraphAttributes.addAttributes(paragraphParent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1096
        parserState.put("pgf", paragraphAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1097
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1098
        sectionAttributes = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1099
        sectionAttributes.addAttributes(sectionParent);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1100
        parserState.put("sec", sectionAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1101
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1102
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1103
    public void endgroup(Dictionary oldState)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1104
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1105
        characterAttributes = (MutableAttributeSet)parserState.get("chr");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1106
        paragraphAttributes = (MutableAttributeSet)parserState.get("pgf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1107
        sectionAttributes   = (MutableAttributeSet)parserState.get("sec");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1108
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1109
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1110
    public void close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1111
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1112
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1113
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1114
    public boolean handleKeyword(String keyword)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1115
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1116
        if (keyword.equals("ulnone")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1117
            return handleKeyword("ul", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1118
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1119
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1120
        {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1121
            RTFAttribute attr = straightforwardAttributes.get(keyword);
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1122
            if (attr != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1123
                boolean ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1124
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1125
                switch(attr.domain()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1126
                  case RTFAttribute.D_CHARACTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1127
                    ok = attr.set(characterAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1128
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1129
                  case RTFAttribute.D_PARAGRAPH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1130
                    ok = attr.set(paragraphAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1131
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1132
                  case RTFAttribute.D_SECTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1133
                    ok = attr.set(sectionAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1134
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1135
                  case RTFAttribute.D_META:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1136
                    mockery.backing = parserState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1137
                    ok = attr.set(mockery);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1138
                    mockery.backing = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1139
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1140
                  case RTFAttribute.D_DOCUMENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1141
                    ok = attr.set(documentAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1142
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1143
                  default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1144
                    /* should never happen */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1145
                    ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1146
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1147
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1148
                if (ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1149
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1150
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1151
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1152
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1153
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1154
        if (keyword.equals("plain")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1155
            resetCharacterAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1156
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1157
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1158
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1159
        if (keyword.equals("pard")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1160
            resetParagraphAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1161
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1162
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1163
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1164
        if (keyword.equals("sectd")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1165
            resetSectionAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1166
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1167
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1168
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1169
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1170
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1171
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1172
    public boolean handleKeyword(String keyword, int parameter)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1173
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1174
        boolean booleanParameter = (parameter != 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1175
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1176
        if (keyword.equals("fc"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1177
            keyword = "cf"; /* whatEVER, dude. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1178
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1179
        if (keyword.equals("f")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1180
            parserState.put(keyword, Integer.valueOf(parameter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1181
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1182
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1183
        if (keyword.equals("cf")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1184
            parserState.put(keyword, Integer.valueOf(parameter));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1185
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1186
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1187
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1188
        {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1189
            RTFAttribute attr = straightforwardAttributes.get(keyword);
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1190
            if (attr != null) {
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1191
                boolean ok;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1192
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1193
                switch(attr.domain()) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1194
                  case RTFAttribute.D_CHARACTER:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1195
                    ok = attr.set(characterAttributes, parameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1196
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1197
                  case RTFAttribute.D_PARAGRAPH:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1198
                    ok = attr.set(paragraphAttributes, parameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1199
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1200
                  case RTFAttribute.D_SECTION:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1201
                    ok = attr.set(sectionAttributes, parameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1202
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1203
                  case RTFAttribute.D_META:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1204
                    mockery.backing = parserState;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1205
                    ok = attr.set(mockery, parameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1206
                    mockery.backing = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1207
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1208
                  case RTFAttribute.D_DOCUMENT:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1209
                    ok = attr.set(documentAttributes, parameter);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1210
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1211
                  default:
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1212
                    /* should never happen */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1213
                    ok = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1214
                    break;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1215
                }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1216
                if (ok)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1217
                    return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1218
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1219
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1220
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1221
        if (keyword.equals("fs")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1222
            StyleConstants.setFontSize(characterAttributes, (parameter / 2));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1223
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1224
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1225
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1226
        /* TODO: superscript/subscript */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1227
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1228
        if (keyword.equals("sl")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1229
            if (parameter == 1000) {  /* magic value! */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1230
                characterAttributes.removeAttribute(StyleConstants.LineSpacing);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1231
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1232
                /* TODO: The RTF sl attribute has special meaning if it's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1233
                   negative. Make sure that SwingText has the same special
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1234
                   meaning, or find a way to imitate that. When SwingText
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1235
                   handles this, also recognize the slmult keyword. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1236
                StyleConstants.setLineSpacing(characterAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1237
                                              parameter / 20f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1238
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1239
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1240
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1241
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1242
        /* TODO: Other kinds of underlining */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1243
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1244
        if (keyword.equals("tx") || keyword.equals("tb")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1245
            float tabPosition = parameter / 20f;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1246
            int tabAlignment, tabLeader;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1247
            Number item;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1248
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1249
            tabAlignment = TabStop.ALIGN_LEFT;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1250
            item = (Number)(parserState.get("tab_alignment"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1251
            if (item != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1252
                tabAlignment = item.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1253
            tabLeader = TabStop.LEAD_NONE;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1254
            item = (Number)(parserState.get("tab_leader"));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1255
            if (item != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1256
                tabLeader = item.intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1257
            if (keyword.equals("tb"))
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1258
                tabAlignment = TabStop.ALIGN_BAR;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1259
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1260
            parserState.remove("tab_alignment");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1261
            parserState.remove("tab_leader");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1262
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1263
            TabStop newStop = new TabStop(tabPosition, tabAlignment, tabLeader);
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1264
            Dictionary<Object, Object> tabs;
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1265
            Integer stopCount;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1266
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1267
            tabs = (Dictionary<Object, Object>)parserState.get("_tabs");
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1268
            if (tabs == null) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1269
                tabs = new Hashtable<Object, Object>();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1270
                parserState.put("_tabs", tabs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1271
                stopCount = Integer.valueOf(1);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1272
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1273
                stopCount = (Integer)tabs.get("stop count");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1274
                stopCount = Integer.valueOf(1 + stopCount.intValue());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1275
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1276
            tabs.put(stopCount, newStop);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1277
            tabs.put("stop count", stopCount);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1278
            parserState.remove("_tabs_immutable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1279
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1280
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1281
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1282
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1283
        if (keyword.equals("s") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1284
            paragraphStyles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1285
            parserState.put("paragraphStyle", paragraphStyles[parameter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1286
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1287
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1288
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1289
        if (keyword.equals("cs") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1290
            characterStyles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1291
            parserState.put("characterStyle", characterStyles[parameter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1292
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1293
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1294
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1295
        if (keyword.equals("ds") &&
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1296
            sectionStyles != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1297
            parserState.put("sectionStyle", sectionStyles[parameter]);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1298
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1299
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1300
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1301
        return false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1302
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1303
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1304
    /** Returns a new MutableAttributeSet containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1305
     *  default character attributes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1306
    protected MutableAttributeSet rootCharacterAttributes()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1307
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1308
        MutableAttributeSet set = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1309
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1310
        /* TODO: default font */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1311
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1312
        StyleConstants.setItalic(set, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1313
        StyleConstants.setBold(set, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1314
        StyleConstants.setUnderline(set, false);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1315
        StyleConstants.setForeground(set, defaultColor());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1316
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1317
        return set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1318
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1319
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1320
    /** Returns a new MutableAttributeSet containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1321
     *  default paragraph attributes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1322
    protected MutableAttributeSet rootParagraphAttributes()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1323
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1324
        MutableAttributeSet set = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1325
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1326
        StyleConstants.setLeftIndent(set, 0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1327
        StyleConstants.setRightIndent(set, 0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1328
        StyleConstants.setFirstLineIndent(set, 0f);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1329
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1330
        /* TODO: what should this be, really? */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1331
        set.setResolveParent(target.getStyle(StyleContext.DEFAULT_STYLE));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1332
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1333
        return set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1334
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1335
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1336
    /** Returns a new MutableAttributeSet containing the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1337
     *  default section attributes */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1338
    protected MutableAttributeSet rootSectionAttributes()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1339
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1340
        MutableAttributeSet set = new SimpleAttributeSet();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1341
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1342
        return set;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1343
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1344
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1345
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1346
     * Calculates the current text (character) attributes in a form suitable
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1347
     * for SwingText from the current parser state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1348
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1349
     * @returns a new MutableAttributeSet containing the text attributes.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1350
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1351
    MutableAttributeSet currentTextAttributes()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1352
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1353
        MutableAttributeSet attributes =
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1354
            new SimpleAttributeSet(characterAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1355
        Integer fontnum;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1356
        Integer stateItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1357
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1358
        /* figure out the font name */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1359
        /* TODO: catch exceptions for undefined attributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1360
           bad font indices, etc.? (as it stands, it is the caller's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1361
           job to clean up after corrupt RTF) */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1362
        fontnum = (Integer)parserState.get("f");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1363
        /* note setFontFamily() can not handle a null font */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1364
        String fontFamily;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1365
        if (fontnum != null)
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1366
            fontFamily = fontTable.get(fontnum);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1367
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1368
            fontFamily = null;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1369
        if (fontFamily != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1370
            StyleConstants.setFontFamily(attributes, fontFamily);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1371
        else
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1372
            attributes.removeAttribute(StyleConstants.FontFamily);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1373
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1374
        if (colorTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1375
            stateItem = (Integer)parserState.get("cf");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1376
            if (stateItem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1377
                Color fg = colorTable[stateItem.intValue()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1378
                StyleConstants.setForeground(attributes, fg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1379
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1380
                /* AttributeSet dies if you set a value to null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1381
                attributes.removeAttribute(StyleConstants.Foreground);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1382
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1383
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1384
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1385
        if (colorTable != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1386
            stateItem = (Integer)parserState.get("cb");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1387
            if (stateItem != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1388
                Color bg = colorTable[stateItem.intValue()];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1389
                attributes.addAttribute(StyleConstants.Background,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1390
                                        bg);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1391
            } else {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1392
                /* AttributeSet dies if you set a value to null */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1393
                attributes.removeAttribute(StyleConstants.Background);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1394
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1395
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1396
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1397
        Style characterStyle = (Style)parserState.get("characterStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1398
        if (characterStyle != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1399
            attributes.setResolveParent(characterStyle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1400
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1401
        /* Other attributes are maintained directly in "attributes" */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1402
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1403
        return attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1404
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1405
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1406
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1407
     * Calculates the current paragraph attributes (with keys
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1408
     * as given in StyleConstants) from the current parser state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1409
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1410
     * @returns a newly created MutableAttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1411
     * @see StyleConstants
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1412
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1413
    MutableAttributeSet currentParagraphAttributes()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1414
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1415
        /* NB if there were a mutableCopy() method we should use it */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1416
        MutableAttributeSet bld = new SimpleAttributeSet(paragraphAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1417
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1418
        Integer stateItem;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1419
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1420
        /*** Tab stops ***/
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1421
        TabStop tabs[];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1422
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1423
        tabs = (TabStop[])parserState.get("_tabs_immutable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1424
        if (tabs == null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1425
            Dictionary workingTabs = (Dictionary)parserState.get("_tabs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1426
            if (workingTabs != null) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1427
                int count = ((Integer)workingTabs.get("stop count")).intValue();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1428
                tabs = new TabStop[count];
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1429
                for (int ix = 1; ix <= count; ix ++)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1430
                    tabs[ix-1] = (TabStop)workingTabs.get(Integer.valueOf(ix));
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1431
                parserState.put("_tabs_immutable", tabs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1432
            }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1433
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1434
        if (tabs != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1435
            bld.addAttribute(Constants.Tabs, tabs);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1436
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1437
        Style paragraphStyle = (Style)parserState.get("paragraphStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1438
        if (paragraphStyle != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1439
            bld.setResolveParent(paragraphStyle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1440
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1441
        return bld;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1442
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1443
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1444
    /**
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1445
     * Calculates the current section attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1446
     * from the current parser state.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1447
     *
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1448
     * @returns a newly created MutableAttributeSet.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1449
     */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1450
    public AttributeSet currentSectionAttributes()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1451
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1452
        MutableAttributeSet attributes = new SimpleAttributeSet(sectionAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1453
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1454
        Style sectionStyle = (Style)parserState.get("sectionStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1455
        if (sectionStyle != null)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1456
            attributes.setResolveParent(sectionStyle);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1457
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1458
        return attributes;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1459
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1460
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1461
    /** Resets the filter's internal notion of the current character
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1462
     *  attributes to their default values. Invoked to handle the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1463
     *  \plain keyword. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1464
    protected void resetCharacterAttributes()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1465
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1466
        handleKeyword("f", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1467
        handleKeyword("cf", 0);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1468
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1469
        handleKeyword("fs", 24);  /* 12 pt. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1470
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1471
        Enumeration<RTFAttribute> attributes = straightforwardAttributes.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1472
        while(attributes.hasMoreElements()) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1473
            RTFAttribute attr = attributes.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1474
            if (attr.domain() == RTFAttribute.D_CHARACTER)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1475
                attr.setDefault(characterAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1476
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1477
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1478
        handleKeyword("sl", 1000);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1479
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1480
        parserState.remove("characterStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1481
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1482
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1483
    /** Resets the filter's internal notion of the current paragraph's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1484
     *  attributes to their default values. Invoked to handle the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1485
     *  \pard keyword. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1486
    protected void resetParagraphAttributes()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1487
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1488
        parserState.remove("_tabs");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1489
        parserState.remove("_tabs_immutable");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1490
        parserState.remove("paragraphStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1491
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1492
        StyleConstants.setAlignment(paragraphAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1493
                                    StyleConstants.ALIGN_LEFT);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1494
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1495
        Enumeration<RTFAttribute> attributes = straightforwardAttributes.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1496
        while(attributes.hasMoreElements()) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1497
            RTFAttribute attr = attributes.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1498
            if (attr.domain() == RTFAttribute.D_PARAGRAPH)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1499
                attr.setDefault(characterAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1500
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1501
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1502
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1503
    /** Resets the filter's internal notion of the current section's
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1504
     *  attributes to their default values. Invoked to handle the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1505
     *  \sectd keyword. */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1506
    protected void resetSectionAttributes()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1507
    {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1508
        Enumeration<RTFAttribute> attributes = straightforwardAttributes.elements();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1509
        while(attributes.hasMoreElements()) {
1287
a04aca99c77a 6722802: Code improvement and warnings removing from the javax.swing.text package
rupashka
parents: 2
diff changeset
  1510
            RTFAttribute attr = attributes.nextElement();
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1511
            if (attr.domain() == RTFAttribute.D_SECTION)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1512
                attr.setDefault(characterAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1513
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1514
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1515
        parserState.remove("sectionStyle");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1516
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1517
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1518
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1519
/** RTFReader.TextHandlingDestination provides basic text handling
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1520
 *  functionality. Subclasses must implement: <dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1521
 *  <dt>deliverText()<dd>to handle a run of text with the same
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1522
 *                       attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1523
 *  <dt>finishParagraph()<dd>to end the current paragraph and
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1524
 *                           set the paragraph's attributes
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1525
 *  <dt>endSection()<dd>to end the current section
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1526
 *  </dl>
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1527
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1528
abstract class TextHandlingDestination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1529
    extends AttributeTrackingDestination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1530
    implements Destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1531
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1532
    /** <code>true</code> if the reader has not just finished
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1533
     *  a paragraph; false upon startup */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1534
    boolean inParagraph;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1535
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1536
    public TextHandlingDestination()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1537
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1538
        super();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1539
        inParagraph = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1540
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1541
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1542
    public void handleText(String text)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1543
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1544
        if (! inParagraph)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1545
            beginParagraph();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1546
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1547
        deliverText(text, currentTextAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1548
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1549
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1550
    abstract void deliverText(String text, AttributeSet characterAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1551
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1552
    public void close()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1553
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1554
        if (inParagraph)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1555
            endParagraph();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1556
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1557
        super.close();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1558
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1559
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1560
    public boolean handleKeyword(String keyword)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1561
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1562
        if (keyword.equals("\r") || keyword.equals("\n")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1563
            keyword = "par";
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1564
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1565
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1566
        if (keyword.equals("par")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1567
//          warnings.println("Ending paragraph.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1568
            endParagraph();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1569
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1570
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1571
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1572
        if (keyword.equals("sect")) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1573
//          warnings.println("Ending section.");
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1574
            endSection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1575
            return true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1576
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1577
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1578
        return super.handleKeyword(keyword);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1579
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1580
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1581
    protected void beginParagraph()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1582
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1583
        inParagraph = true;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1584
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1585
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1586
    protected void endParagraph()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1587
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1588
        AttributeSet pgfAttributes = currentParagraphAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1589
        AttributeSet chrAttributes = currentTextAttributes();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1590
        finishParagraph(pgfAttributes, chrAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1591
        inParagraph = false;
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1592
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1593
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1594
    abstract void finishParagraph(AttributeSet pgfA, AttributeSet chrA);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1595
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1596
    abstract void endSection();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1597
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1598
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1599
/** RTFReader.DocumentDestination is a concrete subclass of
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1600
 *  TextHandlingDestination which appends the text to the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1601
 *  StyledDocument given by the <code>target</code> ivar of the
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1602
 *  containing RTFReader.
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1603
 */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1604
class DocumentDestination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1605
    extends TextHandlingDestination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1606
    implements Destination
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1607
{
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1608
    public void deliverText(String text, AttributeSet characterAttributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1609
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1610
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1611
            target.insertString(target.getLength(),
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1612
                                text,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1613
                                currentTextAttributes());
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1614
        } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1615
            /* This shouldn't be able to happen, of course */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1616
            /* TODO is InternalError the correct error to throw? */
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
  1617
            throw new InternalError(ble.getMessage(), ble);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1618
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1619
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1620
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1621
    public void finishParagraph(AttributeSet pgfAttributes,
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1622
                                AttributeSet chrAttributes)
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1623
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1624
        int pgfEndPosition = target.getLength();
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1625
        try {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1626
            target.insertString(pgfEndPosition, "\n", chrAttributes);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1627
            target.setParagraphAttributes(pgfEndPosition, 1, pgfAttributes, true);
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1628
        } catch (BadLocationException ble) {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1629
            /* This shouldn't be able to happen, of course */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1630
            /* TODO is InternalError the correct error to throw? */
10419
12c063b39232 7084245: Update usages of InternalError to use exception chaining
sherman
parents: 5506
diff changeset
  1631
            throw new InternalError(ble.getMessage(), ble);
2
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1632
        }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1633
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1634
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1635
    public void endSection()
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1636
    {
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1637
        /* If we implemented sections, we'd end 'em here */
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1638
    }
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1639
}
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1640
90ce3da70b43 Initial load
duke
parents:
diff changeset
  1641
}