001: // Text - Saxon extension element for inserting text
002:
003: package com.nwalsh.saxon;
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: import javax.xml.transform.TransformerException;
013: import javax.xml.transform.TransformerConfigurationException;
014: import com.icl.saxon.*;
015: import com.icl.saxon.style.*;
016: import com.icl.saxon.expr.*;
017: import com.icl.saxon.output.*;
018: import org.xml.sax.AttributeList;
019:
020: /**
021: * <p>Saxon extension element for inserting text
022: *
023: * <p>$Id: Text.java,v 1.4 2005-08-30 08:14:58 draganr Exp $</p>
024: *
025: * <p>Copyright (C) 2000 Norman Walsh.</p>
026: *
027: * <p>This class provides a
028: * <a href="http://users.iclway.co.uk/mhkay/saxon/">Saxon</a>
029: * extension element for inserting text into a result tree.</p>
030: *
031: * <p><b>Change Log:</b></p>
032: * <dl>
033: * <dt>1.0</dt>
034: * <dd><p>Initial release.</p></dd>
035: * </dl>
036: *
037: * @author Norman Walsh
038: * <a href="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
039: *
040: * @version $Id: Text.java,v 1.4 2005-08-30 08:14:58 draganr Exp $
041: *
042: */
043: public class Text extends StyleElement {
044: /**
045: * <p>Constructor for Text</p>
046: *
047: * <p>Does nothing.</p>
048: */
049: public Text() {
050: }
051:
052: /**
053: * <p>Is this element an instruction?</p>
054: *
055: * <p>Yes, it is.</p>
056: *
057: * @return true
058: */
059: public boolean isInstruction() {
060: return true;
061: }
062:
063: /**
064: * <p>Can this element contain a template-body?</p>
065: *
066: * <p>Yes, it can, but only so that it can contain xsl:fallback.</p>
067: *
068: * @return true
069: */
070: public boolean mayContainTemplateBody() {
071: return true;
072: }
073:
074: /**
075: * <p>Validate the arguments</p>
076: *
077: * <p>The element must have an href attribute.</p>
078: */
079: public void prepareAttributes()
080: throws TransformerConfigurationException {
081: // Get mandatory href attribute
082: String fnAtt = getAttribute("href");
083: if (fnAtt == null) {
084: reportAbsence("href");
085: }
086: }
087:
088: /** Validate that the element occurs in a reasonable place. */
089: public void validate() throws TransformerConfigurationException {
090: checkWithinTemplate();
091: }
092:
093: /**
094: * <p>Insert the text of the file into the result tree</p>
095: *
096: * <p>Processing this element inserts the contents of the URL named
097: * by the href attribute into the result tree as plain text.</p>
098: *
099: */
100: public void process(Context context) throws TransformerException {
101: Outputter out = context.getOutputter();
102:
103: String hrefAtt = getAttribute("href");
104: Expression hrefExpr = makeAttributeValueTemplate(hrefAtt);
105: String href = hrefExpr.evaluateAsString(context);
106: URL fileURL = null;
107:
108: try {
109: try {
110: fileURL = new URL(href);
111: } catch (MalformedURLException e1) {
112: try {
113: fileURL = new URL("file:" + href);
114: } catch (MalformedURLException e2) {
115: System.out.println("Cannot open " + href);
116: return;
117: }
118: }
119:
120: InputStreamReader isr = new InputStreamReader(fileURL
121: .openStream());
122: BufferedReader is = new BufferedReader(isr);
123:
124: char chars[] = new char[4096];
125: int len = 0;
126: while ((len = is.read(chars)) > 0) {
127: out.writeContent(chars, 0, len);
128: }
129: is.close();
130: } catch (Exception e) {
131: System.out.println("Cannot read " + href);
132: }
133: }
134: }
|