Source code: com/nwalsh/xalan/Text.java
1 // Text - Xalan extension element for inserting text
2
3 package com.nwalsh.xalan;
4
5 import java.io.BufferedReader;
6 import java.io.InputStreamReader;
7 import java.io.InputStream;
8 import java.io.IOException;
9 import java.io.FileNotFoundException;
10 import java.net.URL;
11 import java.net.MalformedURLException;
12
13 import org.xml.sax.SAXException;
14 import org.xml.sax.AttributeList;
15 import org.xml.sax.ContentHandler;
16
17 import org.w3c.dom.*;
18 import org.apache.xerces.dom.*;
19
20 import org.apache.xpath.objects.XObject;
21 import org.apache.xpath.XPath;
22 import org.apache.xpath.NodeSet;
23 import org.apache.xalan.extensions.XSLProcessorContext;
24 import org.apache.xalan.transformer.TransformerImpl;
25 import org.apache.xalan.templates.StylesheetRoot;
26 import org.apache.xalan.templates.ElemExtensionCall;
27 import org.apache.xalan.templates.OutputProperties;
28 import org.apache.xalan.res.XSLTErrorResources;
29
30 import javax.xml.transform.stream.StreamResult;
31 import javax.xml.transform.TransformerException;
32
33 /**
34 * <p>Xalan extension element for inserting text
35 *
36 *
37 * <p>Copyright (C) 2001 Norman Walsh.</p>
38 *
39 * <p>This class provides a
40 * <a href="http://xml.apache.org/xalan/">Xalan</a>
41 * extension element for inserting text into a result tree.</p>
42 *
43 * <p><b>Change Log:</b></p>
44 * <dl>
45 * <dt>1.0</dt>
46 * <dd><p>Initial release.</p></dd>
47 * </dl>
48 *
49 * @author Norman Walsh
50 * <a href="mailto:ndw@nwalsh.com">ndw@nwalsh.com</a>
51 *
52 *
53 */
54 public class Text {
55 /**
56 * <p>Constructor for Text</p>
57 *
58 * <p>Does nothing.</p>
59 */
60 public Text() {
61 }
62
63 public NodeList insertfile(XSLProcessorContext context,
64 ElemExtensionCall elem)
65 throws MalformedURLException,
66 FileNotFoundException,
67 IOException,
68 TransformerException {
69 String href = getFilename(context, elem);
70
71 NodeSet textNodes = new NodeSet();
72 Document textDoc = DOMImplementationImpl.getDOMImplementation().createDocument(null, "tmpDoc", null);
73
74 URL fileURL = null;
75
76 try {
77 try {
78 fileURL = new URL(href);
79 } catch (MalformedURLException e1) {
80 try {
81 fileURL = new URL("file:" + href);
82 } catch (MalformedURLException e2) {
83 System.out.println("Cannot open " + href);
84 return null;
85 }
86 }
87
88 InputStreamReader isr = new InputStreamReader(fileURL.openStream());
89 BufferedReader is = new BufferedReader(isr);
90
91 char chars[] = new char[4096];
92 int len = 0;
93 while ((len = is.read(chars)) > 0) {
94 String s = new String(chars, 0, len);
95 // Does it matter that this produces multiple, adjacent text
96 // nodes? I don't think so...
97 textNodes.addNode(textDoc.createTextNode(s));
98 }
99 is.close();
100 } catch (Exception e) {
101 System.out.println("Cannot read " + href);
102 }
103
104 return textNodes;
105 }
106
107 private String getFilename(XSLProcessorContext context, ElemExtensionCall elem)
108 throws java.net.MalformedURLException,
109 java.io.FileNotFoundException,
110 java.io.IOException,
111 javax.xml.transform.TransformerException {
112
113 String fileName;
114
115 fileName = ((ElemExtensionCall)elem).getAttribute ("href",
116 context.getContextNode(),
117 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 }