1 /*
2  * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * This code is free software; you can redistribute it and/or modify it
6  * under the terms of the GNU General Public License version 2 only, as
7  * published by the Free Software Foundation.  Oracle designates this
8  * particular file as subject to the "Classpath" exception as provided
9  * by Oracle in the LICENSE file that accompanied this code.
10  *
11  * This code is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14  * version 2 for more details (a copy is included in the LICENSE file that
15  * accompanied this code).
16  *
17  * You should have received a copy of the GNU General Public License version
18  * 2 along with this work; if not, write to the Free Software Foundation,
19  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20  *
21  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22  * or visit www.oracle.com if you need additional information or have any
23  * questions.
24  */
25 package javax.swing.text.rtf;
26 
27 import java.awt.*;
28 import java.io.*;
29 import java.net.MalformedURLException;
30 import java.net.URL;
31 import javax.swing.Action;
32 import javax.swing.text.*;
33 import javax.swing.*;
34 
35 /**
36  * This is the default implementation of RTF editing
37  * functionality.  The RTF support was not written by the
38  * Swing team.  In the future we hope to improve the support
39  * provided.
40  *
41  * @author  Timothy Prinzing (of this class, not the package!)
42  */
43 @SuppressWarnings("serial") // Same-version serialization only
44 public class RTFEditorKit extends StyledEditorKit {
45 
46     /**
47      * Constructs an RTFEditorKit.
48      */
RTFEditorKit()49     public RTFEditorKit() {
50         super();
51     }
52 
53     /**
54      * Get the MIME type of the data that this
55      * kit represents support for.  This kit supports
56      * the type <code>text/rtf</code>.
57      *
58      * @return the type
59      */
getContentType()60     public String getContentType() {
61         return "text/rtf";
62     }
63 
64     /**
65      * Insert content from the given stream which is expected
66      * to be in a format appropriate for this kind of content
67      * handler.
68      *
69      * @param in  The stream to read from
70      * @param doc The destination for the insertion.
71      * @param pos The location in the document to place the
72      *   content.
73      * @exception IOException on any I/O error
74      * @exception BadLocationException if pos represents an invalid
75      *   location within the document.
76      */
read(InputStream in, Document doc, int pos)77     public void read(InputStream in, Document doc, int pos) throws IOException, BadLocationException {
78 
79         if (doc instanceof StyledDocument) {
80             // PENDING(prinz) this needs to be fixed to
81             // insert to the given position.
82             RTFReader rdr = new RTFReader((StyledDocument) doc);
83             rdr.readFromStream(in);
84             rdr.close();
85         } else {
86             // treat as text/plain
87             super.read(in, doc, pos);
88         }
89     }
90 
91     /**
92      * Write content from a document to the given stream
93      * in a format appropriate for this kind of content handler.
94      *
95      * @param out  The stream to write to
96      * @param doc The source for the write.
97      * @param pos The location in the document to fetch the
98      *   content.
99      * @param len The amount to write out.
100      * @exception IOException on any I/O error
101      * @exception BadLocationException if pos represents an invalid
102      *   location within the document.
103      */
write(OutputStream out, Document doc, int pos, int len)104     public void write(OutputStream out, Document doc, int pos, int len)
105         throws IOException, BadLocationException {
106 
107             // PENDING(prinz) this needs to be fixed to
108             // use the given document range.
109             RTFGenerator.writeDocument(doc, out);
110     }
111 
112     /**
113      * Insert content from the given stream, which will be
114      * treated as plain text.
115      *
116      * @param in  The stream to read from
117      * @param doc The destination for the insertion.
118      * @param pos The location in the document to place the
119      *   content.
120      * @exception IOException on any I/O error
121      * @exception BadLocationException if pos represents an invalid
122      *   location within the document.
123      */
read(Reader in, Document doc, int pos)124     public void read(Reader in, Document doc, int pos)
125         throws IOException, BadLocationException {
126 
127         if (doc instanceof StyledDocument) {
128             RTFReader rdr = new RTFReader((StyledDocument) doc);
129             rdr.readFromReader(in);
130             rdr.close();
131         } else {
132             // treat as text/plain
133             super.read(in, doc, pos);
134         }
135     }
136 
137     /**
138      * Write content from a document to the given stream
139      * as plain text.
140      *
141      * @param out  The stream to write to
142      * @param doc The source for the write.
143      * @param pos The location in the document to fetch the
144      *   content.
145      * @param len The amount to write out.
146      * @exception IOException on any I/O error
147      * @exception BadLocationException if pos represents an invalid
148      *   location within the document.
149      */
write(Writer out, Document doc, int pos, int len)150     public void write(Writer out, Document doc, int pos, int len)
151         throws IOException, BadLocationException {
152 
153         throw new IOException("RTF is an 8-bit format");
154     }
155 
156 }
157