Debian Codegraph @

src/com/nwalsh/saxon/Text.java

raw symbols SCIP
1 // Text - Saxon extension element for inserting text
2
3 package com.nwalsh.saxon;
4
5 import java.io.BufferedReader;
6 import java.io.InputStreamReader;
7 import java.io.InputStream;
8 import java.io.IOException;
9 import java.io.FileNotFoundException;
10 import java.net.URL;
11 import java.net.MalformedURLException;
12
13 import javax.xml.transform.TransformerException;
14 import javax.xml.transform.TransformerConfigurationException;
15 import javax.xml.transform.URIResolver;
16 import javax.xml.transform.Source;
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;
24
25 /**
26 * <p>Saxon extension element for inserting text
27 *
28 * <p>$Id: Text.java 5907 2006-04-27 08:26:47Z xmldoc $</p>
29 *
30 * <p>Copyright (C) 2000 Norman Walsh.</p>
31 *
32 * <p>This class provides a
33 * <a href="http://saxon.sourceforge.net/">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:[email protected]">[email protected]</a>
44 *
45 * @version $Id: Text.java 5907 2006-04-27 08:26:47Z xmldoc $
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 {
85 // Get mandatory href attribute
86 String 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 {
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 {
108 Outputter out = context.getOutputter();
109
110 String hrefAtt = getAttribute("href");
111 Expression hrefExpr = makeAttributeValueTemplate(hrefAtt);
112 String href = hrefExpr.evaluateAsString(context);
113
114 String encodingAtt = getAttribute("encoding");
115 Expression encodingExpr = makeAttributeValueTemplate(encodingAtt);
116 String encoding = encodingExpr.evaluateAsString(context);
117
118 String baseURI = context.getContextNodeInfo().getBaseURI();
119
120 URIResolver resolver = context.getController().getURIResolver();
121
122 if (resolver != null) {
123 Source source = resolver.resolve(href, baseURI);
124 href = source.getSystemId();
125 }
126
127 URL baseURL = null;
128 URL fileURL = null;
129
130 try {
131 baseURL = new URL(baseURI);
132 } catch (MalformedURLException e0) {
133 // what the!?
134 baseURL = null;
135 }
136
137 try {
138 try {
139 fileURL = new URL(baseURL, href);
140 } catch (MalformedURLException e1) {
141 try {
142 fileURL = new URL(baseURL, "file:" + href);
143 } catch (MalformedURLException e2) {
144 System.out.println("Cannot open " + href);
145 return;
146 }
147 }
148
149 InputStreamReader isr = null;
150 if (encoding.equals("") == true)
151 isr = new InputStreamReader(fileURL.openStream());
152 else
153 isr = new InputStreamReader(fileURL.openStream(), encoding);
154
155 BufferedReader is = new BufferedReader(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 e) {
200 System.out.println("Cannot read " + href);
201 }
202 }
203 }