1 /*
2 * Copyright (c) 1999, 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.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 */
23
24
25 /**
26 * Plain text file handler
27 *
28 * This class provides an example of a a replacement content handler for
29 * the text/plain content type. It reads the content of the URL, and prepends
30 * an additional message at the beginning.
31 *
32 * Note that the only restrictions on the package/class names are:
33 * 1) the package must end in the major type of the content type (such as
34 * text, image, application, etc).
35 * 2) the class name must be named with the subtype of the content type (for
36 * content type "text/plain", this would be "plain" as in this example; for
37 * content type "image/gif", the class name would be "gif", and the package
38 * name must end with ".image".
39 * 3) the class must be a subclass of ContentHandler.
40 * 4) It must define the getContent function.
41 */
42 package COM.foo.content.text;
43
44 import java.net.ContentHandler;
45 import java.io.InputStream;
46 import java.net.URLConnection;
47 import java.io.IOException;
48
debug(String msg)49 public class plain extends ContentHandler {
50 /**
51 * Returns one of several object types (this set may change in future
52 * versions):
53 * 1) instance of Thread:
54 * Invoke the thread to launch an external viewer.
55 * 2) instance of InputStream:
56 * Bring up the "Save to disk" dialog page to allow the content
57 * to be saved to disk.
58 * 3) instance of InputStreamImageSource:
59 * Load the image into HotJava in an image viewer page.
60 * 4) instance of String:
61 * Go to a new page with the string as the plain text content
62 * of that page.
63 */
64 public Object getContent(URLConnection uc) {
65 try {
66 InputStream is = uc.getInputStream();
67 StringBuffer sb = new StringBuffer();
68 int c;
69
70 sb.append("[Content of " + uc.getURL() + "]\n\n");
71 sb.append("[This opening message brought to you by your plain/text\n");
72 sb.append("content handler. To remove this content handler, delete the\n");
73 sb.append("COM.foo.content.text directory from your class path and\n");
74 sb.append("the java.content.handler.pkgs property from your HotJava\n");
75 sb.append("properties file.]\n");
76 sb.append("----------------------------------------------------------------\n\n");
77
78 // Read the characters from the source, accumulate them into the string buffer.
79 // (Not the most efficient, but simplest for this example.)
80 while ((c = is.read()) >= 0) {
81 sb.append((char)c);
82 }
83
84 // Tidy up
85 is.close();
86
87 // Return the resulting string to our client (we're case 4 above)
88 return sb.toString();
89 } catch (IOException e) {
90 // For any exception, just return an indication of what went wrong.
91 return "Problem reading document: " + uc.getURL();
92 }
93 }
94 }
95