1 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 import javax.xml.transform.URIResolver ; 33 import javax.xml.transform.Source ; 34 35 58 public class Text { 59 64 public Text() { 65 } 66 67 public String insertfile(XSLProcessorContext context, 68 ElemExtensionCall elem) 69 throws MalformedURLException , 70 FileNotFoundException , 71 IOException , 72 TransformerException { 73 String href = getFilename(context, elem); 74 String encoding = getEncoding(context, elem); 75 76 String baseURI = context.getTransformer().getBaseURLOfSource(); 77 URIResolver resolver = context.getTransformer().getURIResolver(); 78 79 if (resolver != null) { 80 Source source = resolver.resolve(href, baseURI); 81 href = source.getSystemId(); 82 } 83 84 URL baseURL = null; 85 URL fileURL = null; 86 87 try { 88 baseURL = new URL (baseURI); 89 } catch (MalformedURLException e1) { 90 try { 91 baseURL = new URL ("file:" + baseURI); 92 } catch (MalformedURLException e2) { 93 System.out.println("Cannot find base URI for " + baseURI); 94 baseURL = null; 95 } 96 } 97 98 String text = ""; 99 100 try { 101 try { 102 fileURL = new URL (baseURL, href); 103 } catch (MalformedURLException e1) { 104 try { 105 fileURL = new URL (baseURL, "file:" + href); 106 } catch (MalformedURLException e2) { 107 System.out.println("Cannot open " + href); 108 return ""; 109 } 110 } 111 112 InputStreamReader isr = null; 113 if (encoding.equals("") == true) 114 isr = new InputStreamReader (fileURL.openStream()); 115 else 116 isr = new InputStreamReader (fileURL.openStream(), encoding); 117 118 BufferedReader is = new BufferedReader (isr); 119 120 final int BUFFER_SIZE = 4096; 121 char chars[] = new char[BUFFER_SIZE]; 122 char nchars[] = new char[BUFFER_SIZE]; 123 int len = 0; 124 int i = 0; 125 int carry = -1; 126 127 while ((len = is.read(chars)) > 0) { 128 131 int nlen = 0; 132 for (i=0; i<len; i++) { 133 if (chars[i] == '\r') { 135 if (i < (len - 1)) { 136 if (chars[i+1] == '\n') continue; 138 nchars[nlen] = '\n'; 140 nlen++; 141 continue; 142 } else { 143 carry = is.read(); 145 nchars[nlen] = '\n'; 146 nlen++; 147 if (carry == '\n') { 148 carry = -1; 149 } 150 break; 151 } 152 } 153 nchars[nlen] = chars[i]; 154 nlen++; 155 } 156 157 text += String.valueOf(nchars, 0, nlen); 158 159 if (carry != -1) text += String.valueOf((char)carry); 161 carry = -1; 162 } 163 is.close(); 164 } catch (Exception e) { 165 System.out.println("Cannot read " + href); 166 } 167 168 return text; 169 } 170 171 private String getFilename(XSLProcessorContext context, ElemExtensionCall elem) 172 throws java.net.MalformedURLException , 173 java.io.FileNotFoundException , 174 java.io.IOException , 175 javax.xml.transform.TransformerException { 176 177 String fileName; 178 179 fileName = ((ElemExtensionCall)elem).getAttribute ("href", 180 context.getContextNode(), 181 context.getTransformer()); 182 183 if(fileName == null) { 184 context.getTransformer().getMsgMgr().error(elem, 185 "No 'href' on text, or not a filename"); 186 } 187 188 return fileName; 189 } 190 191 private String getEncoding(XSLProcessorContext context, ElemExtensionCall elem) 192 throws java.net.MalformedURLException , 193 java.io.FileNotFoundException , 194 java.io.IOException , 195 javax.xml.transform.TransformerException { 196 197 String encoding; 198 199 encoding = ((ElemExtensionCall)elem).getAttribute ("encoding", 200 context.getContextNode(), 201 context.getTransformer()); 202 203 if (encoding == null) { 204 return ""; 205 } else { 206 return encoding; 207 } 208 } 209 } 210 | Popular Tags |