KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > nwalsh > saxon > Text


1 // Text - Saxon extension element for inserting text
2

3 package com.nwalsh.saxon;
4
5 import java.io.BufferedReader JavaDoc;
6 import java.io.InputStreamReader JavaDoc;
7 import java.io.InputStream JavaDoc;
8 import java.io.IOException JavaDoc;
9 import java.io.FileNotFoundException JavaDoc;
10 import java.net.URL JavaDoc;
11 import java.net.MalformedURLException JavaDoc;
12
13 import javax.xml.transform.TransformerException JavaDoc;
14 import javax.xml.transform.TransformerConfigurationException JavaDoc;
15 import javax.xml.transform.URIResolver JavaDoc;
16 import javax.xml.transform.Source JavaDoc;
17
18 import com.icl.saxon.Context;
19 import com.icl.saxon.style.StyleElement;
20 import com.icl.saxon.output.Outputter;
21 import com.icl.saxon.expr.Expression;
22
23 import org.xml.sax.AttributeList JavaDoc;
24
25 /**
26  * <p>Saxon extension element for inserting text
27  *
28  * <p>$Id: Text.java,v 1.4 2004/10/29 12:44:51 nwalsh Exp $</p>
29  *
30  * <p>Copyright (C) 2000 Norman Walsh.</p>
31  *
32  * <p>This class provides a
33  * <a HREF="http://users.iclway.co.uk/mhkay/saxon/">Saxon</a>
34  * extension element for inserting text into a result tree.</p>
35  *
36  * <p><b>Change Log:</b></p>
37  * <dl>
38  * <dt>1.0</dt>
39  * <dd><p>Initial release.</p></dd>
40  * </dl>
41  *
42  * @author Norman Walsh
43  * <a HREF="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
44  *
45  * @version $Id: Text.java,v 1.4 2004/10/29 12:44:51 nwalsh Exp $
46  *
47  */

48 public class Text extends StyleElement {
49   /**
50    * <p>Constructor for Text</p>
51    *
52    * <p>Does nothing.</p>
53    */

54   public Text() {
55   }
56
57   /**
58    * <p>Is this element an instruction?</p>
59    *
60    * <p>Yes, it is.</p>
61    *
62    * @return true
63    */

64   public boolean isInstruction() {
65     return true;
66   }
67
68     /**
69     * <p>Can this element contain a template-body?</p>
70     *
71     * <p>Yes, it can, but only so that it can contain xsl:fallback.</p>
72     *
73     * @return true
74     */

75   public boolean mayContainTemplateBody() {
76     return true;
77   }
78
79   /**
80    * <p>Validate the arguments</p>
81    *
82    * <p>The element must have an href attribute.</p>
83    */

84   public void prepareAttributes() throws TransformerConfigurationException JavaDoc {
85     // Get mandatory href attribute
86
String JavaDoc fnAtt = getAttribute("href");
87     if (fnAtt == null) {
88       reportAbsence("href");
89     }
90   }
91
92   /** Validate that the element occurs in a reasonable place. */
93   public void validate() throws TransformerConfigurationException JavaDoc {
94     checkWithinTemplate();
95   }
96
97   /**
98    * <p>Insert the text of the file into the result tree</p>
99    *
100    * <p>Processing this element inserts the contents of the URL named
101    * by the href attribute into the result tree as plain text.</p>
102    *
103    * <p>Optional encoding attribute can specify encoding of resource.
104    * If not specified default system encoding is used.</p>
105    *
106    */

107   public void process( Context context ) throws TransformerException JavaDoc {
108     Outputter out = context.getOutputter();
109
110     String JavaDoc hrefAtt = getAttribute("href");
111     Expression hrefExpr = makeAttributeValueTemplate(hrefAtt);
112     String JavaDoc href = hrefExpr.evaluateAsString(context);
113
114     String JavaDoc encodingAtt = getAttribute("encoding");
115     Expression encodingExpr = makeAttributeValueTemplate(encodingAtt);
116     String JavaDoc encoding = encodingExpr.evaluateAsString(context);
117
118     String JavaDoc baseURI = context.getContextNodeInfo().getBaseURI();
119
120     URIResolver JavaDoc resolver = context.getController().getURIResolver();
121
122     if (resolver != null) {
123       Source JavaDoc source = resolver.resolve(href, baseURI);
124       href = source.getSystemId();
125     }
126
127     URL JavaDoc baseURL = null;
128     URL JavaDoc fileURL = null;
129
130     try {
131       baseURL = new URL JavaDoc(baseURI);
132     } catch (MalformedURLException JavaDoc e0) {
133       // what the!?
134
baseURL = null;
135     }
136
137     try {
138       try {
139         fileURL = new URL JavaDoc(baseURL, href);
140       } catch (MalformedURLException JavaDoc e1) {
141         try {
142           fileURL = new URL JavaDoc(baseURL, "file:" + href);
143         } catch (MalformedURLException JavaDoc e2) {
144           System.out.println("Cannot open " + href);
145           return;
146         }
147       }
148
149       InputStreamReader JavaDoc isr = null;
150       if (encoding.equals("") == true)
151         isr = new InputStreamReader JavaDoc(fileURL.openStream());
152       else
153         isr = new InputStreamReader JavaDoc(fileURL.openStream(), encoding);
154
155       BufferedReader JavaDoc is = new BufferedReader JavaDoc(isr);
156
157       final int BUFFER_SIZE = 4096;
158       char chars[] = new char[BUFFER_SIZE];
159       char nchars[] = new char[BUFFER_SIZE];
160       int len = 0;
161       int i = 0;
162       int carry = -1;
163
164       while ((len = is.read(chars)) > 0) {
165         // various new lines are normalized to LF to prevent blank lines
166
// between lines
167

168         int nlen = 0;
169         for (i=0; i<len; i++) {
170           // is current char CR?
171
if (chars[i] == '\r') {
172             if (i < (len - 1)) {
173               // skip it if next char is LF
174
if (chars[i+1] == '\n') continue;
175               // single CR -> LF to normalize MAC line endings
176
nchars[nlen] = '\n';
177               nlen++;
178               continue;
179             } else {
180               // if CR is last char of buffer we must look ahead
181
carry = is.read();
182               nchars[nlen] = '\n';
183               nlen++;
184               if (carry == '\n') {
185                 carry = -1;
186               }
187               break;
188             }
189           }
190           nchars[nlen] = chars[i];
191           nlen++;
192         }
193         out.writeContent(nchars, 0, nlen);
194         // handle look aheaded character
195
if (carry != -1) out.writeContent(String.valueOf((char)carry));
196         carry = -1;
197       }
198       is.close();
199     } catch (Exception JavaDoc e) {
200       System.out.println("Cannot read " + href);
201     }
202   }
203 }
204
Popular Tags