KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > sample > Script


1 /*
2  * (C) Copyright 2002-2004, Andy Clark. All rights reserved.
3  *
4  * This file is distributed under an Apache style license. Please
5  * refer to the LICENSE file for specific details.
6  */

7
8 package sample;
9
10 import java.io.BufferedReader JavaDoc;
11 import java.io.IOException JavaDoc;
12 import java.io.PrintWriter JavaDoc;
13 import java.io.StringReader JavaDoc;
14 import java.io.StringWriter JavaDoc;
15
16 import org.cyberneko.html.HTMLConfiguration;
17 import org.cyberneko.html.filters.DefaultFilter;
18 import org.cyberneko.html.filters.Identity;
19 import org.cyberneko.html.filters.Writer;
20
21 import org.apache.xerces.util.XMLAttributesImpl;
22 import org.apache.xerces.xni.Augmentations;
23 import org.apache.xerces.xni.QName;
24 import org.apache.xerces.xni.XMLAttributes;
25 import org.apache.xerces.xni.XMLLocator;
26 import org.apache.xerces.xni.XMLString;
27 import org.apache.xerces.xni.XNIException;
28 import org.apache.xerces.xni.parser.XMLDocumentFilter;
29 import org.apache.xerces.xni.parser.XMLInputSource;
30
31 /**
32  * This sample demonstrates how to use of the <code>pushInputSource</code>
33  * method of the HTMLConfiguration in order to dynamically insert content
34  * into the HTML stream. The typical use for this functionality is to
35  * insert the result of an embedded script into the HTML document in place
36  * of the script.
37  * <p>
38  * This particular example defines a new script language called "NekoHTML"
39  * script that is a tiny subset of the NSGMLS format. The following table
40  * enumerates the NSGMLS features supported by this script language:
41  * <table border='1' cellspacing='0', cellpadding='3'>
42  * <tr><th>(<i>name</i><td>A start element with the specified <i>name</i>.
43  * <tr><th>"<i>text</i><td>Character content with the specified <i>text</i>.
44  * <tr><th>)<i>name</i><td>An end element with the specified <i>name</i>.
45  * </table>
46  * <p>
47  * In this format, every <i>command</i> is specified on a line by itself.
48  * For example, the following document:
49  * <pre>
50  * &lt;script type='NekoHTML'&gt;
51  * (h1
52  * "Header
53  * )h1
54  * &lt;/script&gt;
55  * </pre>
56  * is equivalent to the following HTML document:
57  * <pre>
58  * &lt;H1&gt;Header&lt;/H1&gt;
59  * </pre>
60  * as seen by document handler registered with the parser, when processed
61  * by this filter.
62  *
63  * @author Andy Clark
64  *
65  * @version $Id: Script.java,v 1.3 2004/02/19 20:00:17 andyc Exp $
66  */

67 public class Script
68     extends DefaultFilter {
69
70     //
71
// Constants
72
//
73

74     /** Augmentations feature identifier. */
75     protected static final String JavaDoc AUGMENTATIONS = "http://cyberneko.org/html/features/augmentations";
76
77     /** Filters property identifier. */
78     protected static final String JavaDoc FILTERS = "http://cyberneko.org/html/properties/filters";
79
80     /** Script type ("text/x-nekoscript"). */
81     protected static final String JavaDoc SCRIPT_TYPE = "text/x-nekoscript";
82
83     //
84
// Data
85
//
86

87     /** The NekoHTML configuration. */
88     protected HTMLConfiguration fConfiguration;
89
90     /** A string buffer to collect the "script". */
91     protected StringBuffer JavaDoc fBuffer;
92     
93     /** The system identifier of the source document. */
94     protected String JavaDoc fSystemId;
95
96     /** The script count. */
97     protected int fScriptCount;
98
99     //
100
// Constructors
101
//
102

103     /** Constructs a script object with the specified configuration. */
104     public Script(HTMLConfiguration config) {
105         fConfiguration = config;
106     } // <init>(HTMLConfiguration)
107

108     //
109
// XMLDocumentHandler methods
110
//
111

112     /** Start document. */
113     public void startDocument(XMLLocator locator, String JavaDoc encoding, Augmentations augs)
114         throws XNIException {
115         fBuffer = null;
116         fSystemId = locator != null ? locator.getLiteralSystemId() : null;
117         fScriptCount = 0;
118         super.startDocument(locator, encoding, augs);
119     } // startDocument(XMLLocator,String,Augmentations)
120

121     /** Start element. */
122     public void startElement(QName element, XMLAttributes attrs, Augmentations augs)
123         throws XNIException {
124         if (element.rawname.equalsIgnoreCase("script") && attrs != null) {
125             String JavaDoc value = attrs.getValue("type");
126             if (value != null && value.equalsIgnoreCase(SCRIPT_TYPE)) {
127                 fBuffer = new StringBuffer JavaDoc();
128                 return;
129             }
130         }
131         super.startElement(element, attrs, augs);
132     } // startElement(QName,XMLAttributes,Augmentations)
133

134     /** Empty element. */
135     public void emptyElement(QName element, XMLAttributes attrs, Augmentations augs)
136         throws XNIException {
137         if (element.rawname.equalsIgnoreCase("script") && attrs != null) {
138             String JavaDoc value = attrs.getValue("type");
139             if (value != null && value.equalsIgnoreCase(SCRIPT_TYPE)) {
140                 return;
141             }
142         }
143         super.emptyElement(element, attrs, augs);
144     } // emptyElement(QName,XMLAttributes,Augmentations)
145

146     /** Characters. */
147     public void characters(XMLString text, Augmentations augs)
148         throws XNIException {
149         if (fBuffer != null) {
150             fBuffer.append(text.ch, text.offset, text.length);
151         }
152         else {
153             super.characters(text, augs);
154         }
155     } // characters(XMLString,Augmentations)
156

157     /** End element. */
158     public void endElement(QName element, Augmentations augs) throws XNIException {
159         if (fBuffer != null) {
160             try {
161                 // run "script" and generate HTML output
162
BufferedReader JavaDoc in = new BufferedReader JavaDoc(new StringReader JavaDoc(fBuffer.toString()));
163                 StringWriter JavaDoc sout = new StringWriter JavaDoc();
164                 PrintWriter JavaDoc out = new PrintWriter JavaDoc(sout);
165                 String JavaDoc line;
166                 while ((line = in.readLine()) != null) {
167                     line.trim();
168                     if (line.length() == 0) {
169                         continue;
170                     }
171                     switch (line.charAt(0)) {
172                         case '(': {
173                             out.print('<');
174                             out.print(line.substring(1));
175                             out.print('>');
176                             break;
177                         }
178                         case '"': {
179                             out.print(line.substring(1));
180                             break;
181                         }
182                         case ')': {
183                             out.print("</");
184                             out.print(line.substring(1));
185                             out.print('>');
186                             break;
187                         }
188                     }
189                 }
190
191                 // push new input source
192
String JavaDoc systemId = fSystemId != null ? fSystemId+'_' : "";
193                 fScriptCount++;
194                 systemId += "script"+fScriptCount;
195                 XMLInputSource source = new XMLInputSource(null, systemId, null,
196                                                            new StringReader JavaDoc(sout.toString()),
197                                                            "UTF-8");
198                 fConfiguration.pushInputSource(source);
199             }
200             catch (IOException JavaDoc e) {
201                 // ignore
202
}
203             finally {
204                 fBuffer = null;
205             }
206         }
207         else {
208             super.endElement(element, augs);
209         }
210     } // endElement(QName,Augmentations)
211

212     //
213
// MAIN
214
//
215

216     /** Main. */
217     public static void main(String JavaDoc[] argv) throws Exception JavaDoc {
218         HTMLConfiguration parser = new HTMLConfiguration();
219         parser.setFeature(AUGMENTATIONS, true);
220         XMLDocumentFilter[] filters = { new Script(parser), new Identity(), new Writer JavaDoc() };
221         parser.setProperty(FILTERS, filters);
222         for (int i = 0; i < argv.length; i++) {
223             parser.parse(new XMLInputSource(null, argv[i], null));
224         }
225     } // main(String[])
226

227 } // class Script
228
Popular Tags