import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.TransformerException;
+import javax.xml.transform.URIResolver;
+import javax.xml.transform.Source;
/**
* <p>Xalan extension element for inserting text
public Text() {
}
- public NodeList insertfile(XSLProcessorContext context,
+ public String insertfile(XSLProcessorContext context,
ElemExtensionCall elem)
throws MalformedURLException,
FileNotFoundException,
IOException,
TransformerException {
String href = getFilename(context, elem);
+ String encoding = getEncoding(context, elem);
- NodeSet textNodes = new NodeSet();
- Document textDoc = DOMImplementationImpl.getDOMImplementation().createDocument(null, "tmpDoc", null);
+ String baseURI = context.getTransformer().getBaseURLOfSource();
+ URIResolver resolver = context.getTransformer().getURIResolver();
+ if (resolver != null) {
+ Source source = resolver.resolve(href, baseURI);
+ href = source.getSystemId();
+ }
+
+ URL baseURL = null;
URL fileURL = null;
try {
+ baseURL = new URL(baseURI);
+ } catch (MalformedURLException e1) {
try {
- fileURL = new URL(href);
+ baseURL = new URL("file:" + baseURI);
+ } catch (MalformedURLException e2) {
+ System.out.println("Cannot find base URI for " + baseURI);
+ baseURL = null;
+ }
+ }
+
+ String text = "";
+
+ try {
+ try {
+ fileURL = new URL(baseURL, href);
} catch (MalformedURLException e1) {
- try {
- fileURL = new URL("file:" + href);
- } catch (MalformedURLException e2) {
- System.out.println("Cannot open " + href);
- return null;
- }
+ try {
+ fileURL = new URL(baseURL, "file:" + href);
+ } catch (MalformedURLException e2) {
+ System.out.println("Cannot open " + href);
+ return "";
+ }
}
- InputStreamReader isr = new InputStreamReader(fileURL.openStream());
+ InputStreamReader isr = null;
+ if (encoding.equals("") == true)
+ isr = new InputStreamReader(fileURL.openStream());
+ else
+ isr = new InputStreamReader(fileURL.openStream(), encoding);
+
BufferedReader is = new BufferedReader(isr);
- char chars[] = new char[4096];
+ final int BUFFER_SIZE = 4096;
+ char chars[] = new char[BUFFER_SIZE];
+ char nchars[] = new char[BUFFER_SIZE];
int len = 0;
+ int i = 0;
+ int carry = -1;
+
while ((len = is.read(chars)) > 0) {
- String s = new String(chars, 0, len);
- // Does it matter that this produces multiple, adjacent text
- // nodes? I don't think so...
- textNodes.addNode(textDoc.createTextNode(s));
+ // various new lines are normalized to LF to prevent blank lines
+ // between lines
+
+ int nlen = 0;
+ for (i=0; i<len; i++) {
+ // is current char CR?
+ if (chars[i] == '\r') {
+ if (i < (len - 1)) {
+ // skip it if next char is LF
+ if (chars[i+1] == '\n') continue;
+ // single CR -> LF to normalize MAC line endings
+ nchars[nlen] = '\n';
+ nlen++;
+ continue;
+ } else {
+ // if CR is last char of buffer we must look ahead
+ carry = is.read();
+ nchars[nlen] = '\n';
+ nlen++;
+ if (carry == '\n') {
+ carry = -1;
+ }
+ break;
+ }
+ }
+ nchars[nlen] = chars[i];
+ nlen++;
+ }
+
+ text += String.valueOf(nchars, 0, nlen);
+
+ // handle look aheaded character
+ if (carry != -1) text += String.valueOf((char)carry);
+ carry = -1;
}
is.close();
} catch (Exception e) {
System.out.println("Cannot read " + href);
}
- return textNodes;
+ return text;
}
private String getFilename(XSLProcessorContext context, ElemExtensionCall elem)
return fileName;
}
+
+ private String getEncoding(XSLProcessorContext context, ElemExtensionCall elem)
+ throws java.net.MalformedURLException,
+ java.io.FileNotFoundException,
+ java.io.IOException,
+ javax.xml.transform.TransformerException {
+
+ String encoding;
+
+ encoding = ((ElemExtensionCall)elem).getAttribute ("encoding",
+ context.getContextNode(),
+ context.getTransformer());
+
+ if (encoding == null) {
+ return "";
+ } else {
+ return encoding;
+ }
+ }
}