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.AttributeList;
015: import org.xml.sax.ContentHandler;
016:
017: import org.w3c.dom.*;
018: import org.apache.xerces.dom.*;
019:
020: import org.apache.xpath.objects.XObject;
021: import org.apache.xpath.XPath;
022: import org.apache.xpath.NodeSet;
023: import org.apache.xalan.extensions.XSLProcessorContext;
024: import org.apache.xalan.transformer.TransformerImpl;
025: import org.apache.xalan.templates.StylesheetRoot;
026: import org.apache.xalan.templates.ElemExtensionCall;
027: import org.apache.xalan.templates.OutputProperties;
028: import org.apache.xalan.res.XSLTErrorResources;
029:
030: import javax.xml.transform.stream.StreamResult;
031: import javax.xml.transform.TransformerException;
032:
033: /**
034: * <p>Xalan extension element for inserting text
035: *
036: * <p>$Id: Text.java,v 1.4 2005-08-30 08:14:58 draganr Exp $</p>
037: *
038: * <p>Copyright (C) 2001 Norman Walsh.</p>
039: *
040: * <p>This class provides a
041: * <a href="http://xml.apache.org/xalan/">Xalan</a>
042: * extension element for inserting text into a result tree.</p>
043: *
044: * <p><b>Change Log:</b></p>
045: * <dl>
046: * <dt>1.0</dt>
047: * <dd><p>Initial release.</p></dd>
048: * </dl>
049: *
050: * @author Norman Walsh
051: * <a href="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
052: *
053: * @version $Id: Text.java,v 1.4 2005-08-30 08:14:58 draganr Exp $
054: *
055: */
056: public class Text {
057: /**
058: * <p>Constructor for Text</p>
059: *
060: * <p>Does nothing.</p>
061: */
062: public Text() {
063: }
064:
065: public NodeList insertfile(XSLProcessorContext context,
066: ElemExtensionCall elem) throws MalformedURLException,
067: FileNotFoundException, IOException, TransformerException {
068: String href = getFilename(context, elem);
069:
070: NodeSet textNodes = new NodeSet();
071: Document textDoc = DOMImplementationImpl.getDOMImplementation()
072: .createDocument(null, "tmpDoc", null);
073:
074: URL fileURL = null;
075:
076: try {
077: try {
078: fileURL = new URL(href);
079: } catch (MalformedURLException e1) {
080: try {
081: fileURL = new URL("file:" + href);
082: } catch (MalformedURLException e2) {
083: System.out.println("Cannot open " + href);
084: return null;
085: }
086: }
087:
088: InputStreamReader isr = new InputStreamReader(fileURL
089: .openStream());
090: BufferedReader is = new BufferedReader(isr);
091:
092: char chars[] = new char[4096];
093: int len = 0;
094: while ((len = is.read(chars)) > 0) {
095: String s = new String(chars, 0, len);
096: // Does it matter that this produces multiple, adjacent text
097: // nodes? I don't think so...
098: textNodes.addNode(textDoc.createTextNode(s));
099: }
100: is.close();
101: } catch (Exception e) {
102: System.out.println("Cannot read " + href);
103: }
104:
105: return textNodes;
106: }
107:
108: private String getFilename(XSLProcessorContext context,
109: ElemExtensionCall elem)
110: throws java.net.MalformedURLException,
111: java.io.FileNotFoundException, java.io.IOException,
112: javax.xml.transform.TransformerException {
113:
114: String fileName;
115:
116: fileName = ((ElemExtensionCall) elem).getAttribute("href",
117: context.getContextNode(), context.getTransformer());
118:
119: if (fileName == null) {
120: context.getTransformer().getMsgMgr().error(elem,
121: "No 'href' on text, or not a filename");
122: }
123:
124: return fileName;
125: }
126: }
|