001: // Text - Xalan extension element for inserting text
002:
003: package com.nwalsh.xalan;
004:
005: import java.io.BufferedReader;
006: import java.io.InputStreamReader;
007: import java.io.InputStream;
008: import java.io.IOException;
009: import java.io.FileNotFoundException;
010: import java.net.URL;
011: import java.net.MalformedURLException;
012:
013: import org.xml.sax.SAXException;
014: import org.xml.sax.AttributeList;
015: import org.xml.sax.ContentHandler;
016:
017: import org.w3c.dom.*;
018: import org.apache.xerces.dom.*;
019:
020: import org.apache.xpath.objects.XObject;
021: import org.apache.xpath.XPath;
022: import org.apache.xpath.NodeSet;
023: import org.apache.xalan.extensions.XSLProcessorContext;
024: import org.apache.xalan.transformer.TransformerImpl;
025: import org.apache.xalan.templates.StylesheetRoot;
026: import org.apache.xalan.templates.ElemExtensionCall;
027: import org.apache.xalan.templates.OutputProperties;
028: import org.apache.xalan.res.XSLTErrorResources;
029:
030: import javax.xml.transform.stream.StreamResult;
031: import javax.xml.transform.TransformerException;
032: import javax.xml.transform.URIResolver;
033: import javax.xml.transform.Source;
034:
035: /**
036: * <p>Xalan extension element for inserting text
037: *
038: * <p>$Id: Text.java,v 1.1 2006/05/31 17:21:20 mbatchelor Exp $</p>
039: *
040: * <p>Copyright (C) 2001 Norman Walsh.</p>
041: *
042: * <p>This class provides a
043: * <a href="http://xml.apache.org/xalan/">Xalan</a>
044: * extension element for inserting text into a result tree.</p>
045: *
046: * <p><b>Change Log:</b></p>
047: * <dl>
048: * <dt>1.0</dt>
049: * <dd><p>Initial release.</p></dd>
050: * </dl>
051: *
052: * @author Norman Walsh
053: * <a href="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
054: *
055: * @version $Id: Text.java,v 1.1 2006/05/31 17:21:20 mbatchelor Exp $
056: *
057: */
058: public class Text {
059: /**
060: * <p>Constructor for Text</p>
061: *
062: * <p>Does nothing.</p>
063: */
064: public Text() {
065: }
066:
067: public String insertfile(XSLProcessorContext context,
068: ElemExtensionCall elem) throws MalformedURLException,
069: FileNotFoundException, IOException, TransformerException {
070: String href = getFilename(context, elem);
071: String encoding = getEncoding(context, elem);
072:
073: String baseURI = context.getTransformer().getBaseURLOfSource();
074: URIResolver resolver = context.getTransformer()
075: .getURIResolver();
076:
077: if (resolver != null) {
078: Source source = resolver.resolve(href, baseURI);
079: href = source.getSystemId();
080: }
081:
082: URL baseURL = null;
083: URL fileURL = null;
084:
085: try {
086: baseURL = new URL(baseURI);
087: } catch (MalformedURLException e1) {
088: try {
089: baseURL = new URL("file:" + baseURI);
090: } catch (MalformedURLException e2) {
091: System.out.println("Cannot find base URI for "
092: + baseURI);
093: baseURL = null;
094: }
095: }
096:
097: String text = "";
098:
099: try {
100: try {
101: fileURL = new URL(baseURL, href);
102: } catch (MalformedURLException e1) {
103: try {
104: fileURL = new URL(baseURL, "file:" + href);
105: } catch (MalformedURLException e2) {
106: System.out.println("Cannot open " + href);
107: return "";
108: }
109: }
110:
111: InputStreamReader isr = null;
112: if (encoding.equals("") == true)
113: isr = new InputStreamReader(fileURL.openStream());
114: else
115: isr = new InputStreamReader(fileURL.openStream(),
116: encoding);
117:
118: BufferedReader is = new BufferedReader(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')
138: continue;
139: // single CR -> LF to normalize MAC line endings
140: nchars[nlen] = '\n';
141: nlen++;
142: continue;
143: } else {
144: // if CR is last char of buffer we must look ahead
145: carry = is.read();
146: nchars[nlen] = '\n';
147: nlen++;
148: if (carry == '\n') {
149: carry = -1;
150: }
151: break;
152: }
153: }
154: nchars[nlen] = chars[i];
155: nlen++;
156: }
157:
158: text += String.valueOf(nchars, 0, nlen);
159:
160: // handle look aheaded character
161: if (carry != -1)
162: text += String.valueOf((char) carry);
163: carry = -1;
164: }
165: is.close();
166: } catch (Exception e) {
167: System.out.println("Cannot read " + href);
168: }
169:
170: return text;
171: }
172:
173: private String getFilename(XSLProcessorContext context,
174: ElemExtensionCall elem)
175: throws java.net.MalformedURLException,
176: java.io.FileNotFoundException, java.io.IOException,
177: javax.xml.transform.TransformerException {
178:
179: String fileName;
180:
181: fileName = ((ElemExtensionCall) elem).getAttribute("href",
182: context.getContextNode(), context.getTransformer());
183:
184: if (fileName == null) {
185: context.getTransformer().getMsgMgr().error(elem,
186: "No 'href' on text, or not a filename");
187: }
188:
189: return fileName;
190: }
191:
192: private String getEncoding(XSLProcessorContext context,
193: ElemExtensionCall elem)
194: throws java.net.MalformedURLException,
195: java.io.FileNotFoundException, java.io.IOException,
196: javax.xml.transform.TransformerException {
197:
198: String encoding;
199:
200: encoding = ((ElemExtensionCall) elem).getAttribute("encoding",
201: context.getContextNode(), context.getTransformer());
202:
203: if (encoding == null) {
204: return "";
205: } else {
206: return encoding;
207: }
208: }
209: }
|