
com.nwalsh.saxon.Text Maven / Gradle / Ivy
Show all versions of docbook-xsl-saxon Show documentation
// Text - Saxon extension element for inserting text
package com.nwalsh.saxon;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.MalformedURLException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.URIResolver;
import javax.xml.transform.Source;
import com.icl.saxon.Context;
import com.icl.saxon.style.StyleElement;
import com.icl.saxon.output.Outputter;
import com.icl.saxon.expr.Expression;
/**
* Saxon extension element for inserting text
*
*
$Id: Text.java 8377 2009-03-24 19:28:27Z mzjn $
*
* Copyright (C) 2000 Norman Walsh.
*
* This class provides a
* Saxon
* extension element for inserting text into a result tree.
*
* Change Log:
*
* - 1.0
* Initial release.
*
*
* @author Norman Walsh
* [email protected]
*
* @version $Id: Text.java 8377 2009-03-24 19:28:27Z mzjn $
*
*/
public class Text extends StyleElement {
/**
* Constructor for Text
*
* Does nothing.
*/
public Text() {
}
/**
* Is this element an instruction?
*
* Yes, it is.
*
* @return true
*/
public boolean isInstruction() {
return true;
}
/**
* Can this element contain a template-body?
*
* Yes, it can, but only so that it can contain xsl:fallback.
*
* @return true
*/
public boolean mayContainTemplateBody() {
return true;
}
/**
* Validate the arguments
*
* The element must have an href attribute.
*/
public void prepareAttributes() throws TransformerConfigurationException {
// Get mandatory href attribute
String fnAtt = getAttribute("href");
if (fnAtt == null) {
reportAbsence("href");
}
}
/** Validate that the element occurs in a reasonable place. */
public void validate() throws TransformerConfigurationException {
checkWithinTemplate();
}
/**
* Insert the text of the file into the result tree
*
* Processing this element inserts the contents of the URL named
* by the href attribute into the result tree as plain text.
*
* Optional encoding attribute can specify encoding of resource.
* If not specified default system encoding is used.
*
*/
public void process( Context context ) throws TransformerException {
Outputter out = context.getOutputter();
String hrefAtt = getAttribute("href");
Expression hrefExpr = makeAttributeValueTemplate(hrefAtt);
String href = hrefExpr.evaluateAsString(context);
String encodingAtt = getAttribute("encoding");
Expression encodingExpr = makeAttributeValueTemplate(encodingAtt);
String encoding = encodingExpr.evaluateAsString(context);
String baseURI = context.getContextNodeInfo().getBaseURI();
URIResolver resolver = context.getController().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 e0) {
// what the!?
baseURL = null;
}
try {
try {
fileURL = new URL(baseURL, href);
} catch (MalformedURLException e1) {
try {
fileURL = new URL(baseURL, "file:" + href);
} catch (MalformedURLException e2) {
System.out.println("Cannot open " + href);
return;
}
}
InputStreamReader isr = null;
if (encoding.equals("") == true)
isr = new InputStreamReader(fileURL.openStream());
else
isr = new InputStreamReader(fileURL.openStream(), encoding);
BufferedReader is = new BufferedReader(isr);
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) {
// various new lines are normalized to LF to prevent blank lines
// between lines
int nlen = 0;
for (i=0; i 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++;
}
out.writeContent(nchars, 0, nlen-1);
// handle look aheaded character
if (carry != -1) out.writeContent(String.valueOf((char)carry));
carry = -1;
}
is.close();
} catch (Exception e) {
System.out.println("Cannot read " + href);
}
}
}