// Text - Saxon extension element for inserting text package com.nwalsh.saxon; import java.io.BufferedReader; import java.io.InputStreamReader; import java.io.InputStream; import java.io.IOException; import java.io.FileNotFoundException; import java.net.URL; import java.net.MalformedURLException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerConfigurationException; import com.icl.saxon.*; import com.icl.saxon.style.*; import com.icl.saxon.expr.*; import com.icl.saxon.output.*; import org.xml.sax.AttributeList; /** *

Saxon extension element for inserting text * *

$Id: Text.java,v 1.3 2004/03/24 09:10:16 kosek Exp $

* *

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 * ndw@nwalsh.com * * @version $Id: Text.java,v 1.3 2004/03/24 09:10:16 kosek Exp $ * */ 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); URL fileURL = null; try { try { fileURL = new URL(href); } catch (MalformedURLException e1) { try { fileURL = new URL("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); // 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); } } }