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