KickJava   Java API By Example, From Geeks To Geeks.

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


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

3 package com.nwalsh.xalan;
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 org.xml.sax.SAXException JavaDoc;
14 import org.xml.sax.AttributeList JavaDoc;
15 import org.xml.sax.ContentHandler JavaDoc;
16
17 import org.w3c.dom.*;
18 import org.apache.xerces.dom.*;
19
20 import org.apache.xpath.objects.XObject;
21 import org.apache.xpath.XPath;
22 import org.apache.xpath.NodeSet;
23 import org.apache.xalan.extensions.XSLProcessorContext;
24 import org.apache.xalan.transformer.TransformerImpl;
25 import org.apache.xalan.templates.StylesheetRoot;
26 import org.apache.xalan.templates.ElemExtensionCall;
27 import org.apache.xalan.templates.OutputProperties;
28 import org.apache.xalan.res.XSLTErrorResources;
29
30 import javax.xml.transform.stream.StreamResult JavaDoc;
31 import javax.xml.transform.TransformerException JavaDoc;
32 import javax.xml.transform.URIResolver JavaDoc;
33 import javax.xml.transform.Source JavaDoc;
34
35 /**
36  * <p>Xalan extension element for inserting text
37  *
38  * <p>$Id: Text.java,v 1.2 2004/10/29 13:16:16 nwalsh Exp $</p>
39  *
40  * <p>Copyright (C) 2001 Norman Walsh.</p>
41  *
42  * <p>This class provides a
43  * <a HREF="http://xml.apache.org/xalan/">Xalan</a>
44  * extension element for inserting text into a result tree.</p>
45  *
46  * <p><b>Change Log:</b></p>
47  * <dl>
48  * <dt>1.0</dt>
49  * <dd><p>Initial release.</p></dd>
50  * </dl>
51  *
52  * @author Norman Walsh
53  * <a HREF="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
54  *
55  * @version $Id: Text.java,v 1.2 2004/10/29 13:16:16 nwalsh Exp $
56  *
57  */

58 public class Text {
59   /**
60    * <p>Constructor for Text</p>
61    *
62    * <p>Does nothing.</p>
63    */

64   public Text() {
65   }
66
67   public String JavaDoc insertfile(XSLProcessorContext context,
68                  ElemExtensionCall elem)
69     throws MalformedURLException JavaDoc,
70            FileNotFoundException JavaDoc,
71            IOException JavaDoc,
72        TransformerException JavaDoc {
73     String JavaDoc href = getFilename(context, elem);
74     String JavaDoc encoding = getEncoding(context, elem);
75
76     String JavaDoc baseURI = context.getTransformer().getBaseURLOfSource();
77     URIResolver JavaDoc resolver = context.getTransformer().getURIResolver();
78
79     if (resolver != null) {
80       Source JavaDoc source = resolver.resolve(href, baseURI);
81       href = source.getSystemId();
82     }
83
84     URL JavaDoc baseURL = null;
85     URL JavaDoc fileURL = null;
86
87     try {
88       baseURL = new URL JavaDoc(baseURI);
89     } catch (MalformedURLException JavaDoc e1) {
90       try {
91     baseURL = new URL JavaDoc("file:" + baseURI);
92       } catch (MalformedURLException JavaDoc e2) {
93     System.out.println("Cannot find base URI for " + baseURI);
94     baseURL = null;
95       }
96     }
97
98     String JavaDoc text = "";
99
100     try {
101       try {
102         fileURL = new URL JavaDoc(baseURL, href);
103       } catch (MalformedURLException JavaDoc e1) {
104         try {
105           fileURL = new URL JavaDoc(baseURL, "file:" + href);
106         } catch (MalformedURLException JavaDoc e2) {
107           System.out.println("Cannot open " + href);
108           return "";
109         }
110       }
111
112       InputStreamReader JavaDoc isr = null;
113       if (encoding.equals("") == true)
114         isr = new InputStreamReader JavaDoc(fileURL.openStream());
115       else
116         isr = new InputStreamReader JavaDoc(fileURL.openStream(), encoding);
117
118       BufferedReader JavaDoc is = new BufferedReader JavaDoc(isr);
119
120       final int BUFFER_SIZE = 4096;
121       char chars[] = new char[BUFFER_SIZE];
122       char nchars[] = new char[BUFFER_SIZE];
123       int len = 0;
124       int i = 0;
125       int carry = -1;
126
127       while ((len = is.read(chars)) > 0) {
128         // various new lines are normalized to LF to prevent blank lines
129
// between lines
130

131         int nlen = 0;
132         for (i=0; i<len; i++) {
133           // is current char CR?
134
if (chars[i] == '\r') {
135             if (i < (len - 1)) {
136               // skip it if next char is LF
137
if (chars[i+1] == '\n') continue;
138               // single CR -> LF to normalize MAC line endings
139
nchars[nlen] = '\n';
140               nlen++;
141               continue;
142             } else {
143               // if CR is last char of buffer we must look ahead
144
carry = is.read();
145               nchars[nlen] = '\n';
146               nlen++;
147               if (carry == '\n') {
148                 carry = -1;
149               }
150               break;
151             }
152           }
153           nchars[nlen] = chars[i];
154           nlen++;
155         }
156
157     text += String.valueOf(nchars, 0, nlen);
158
159         // handle look aheaded character
160
if (carry != -1) text += String.valueOf((char)carry);
161         carry = -1;
162       }
163       is.close();
164     } catch (Exception JavaDoc e) {
165       System.out.println("Cannot read " + href);
166     }
167
168     return text;
169   }
170
171   private String JavaDoc getFilename(XSLProcessorContext context, ElemExtensionCall elem)
172     throws java.net.MalformedURLException JavaDoc,
173        java.io.FileNotFoundException JavaDoc,
174        java.io.IOException JavaDoc,
175        javax.xml.transform.TransformerException JavaDoc {
176
177     String JavaDoc fileName;
178
179     fileName = ((ElemExtensionCall)elem).getAttribute ("href",
180                                context.getContextNode(),
181                                context.getTransformer());
182
183     if(fileName == null) {
184       context.getTransformer().getMsgMgr().error(elem,
185                          "No 'href' on text, or not a filename");
186     }
187
188     return fileName;
189   }
190
191   private String JavaDoc getEncoding(XSLProcessorContext context, ElemExtensionCall elem)
192     throws java.net.MalformedURLException JavaDoc,
193        java.io.FileNotFoundException JavaDoc,
194        java.io.IOException JavaDoc,
195        javax.xml.transform.TransformerException JavaDoc {
196
197     String JavaDoc encoding;
198
199     encoding = ((ElemExtensionCall)elem).getAttribute ("encoding",
200                                context.getContextNode(),
201                                context.getTransformer());
202
203     if (encoding == null) {
204       return "";
205     } else {
206       return encoding;
207     }
208   }
209 }
210
Popular Tags