1 3 package com.nwalsh.saxon; 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 javax.xml.transform.TransformerException ; 14 import javax.xml.transform.TransformerConfigurationException ; 15 import javax.xml.transform.URIResolver ; 16 import javax.xml.transform.Source ; 17 18 import com.icl.saxon.Context; 19 import com.icl.saxon.style.StyleElement; 20 import com.icl.saxon.output.Outputter; 21 import com.icl.saxon.expr.Expression; 22 23 import org.xml.sax.AttributeList ; 24 25 48 public class Text extends StyleElement { 49 54 public Text() { 55 } 56 57 64 public boolean isInstruction() { 65 return true; 66 } 67 68 75 public boolean mayContainTemplateBody() { 76 return true; 77 } 78 79 84 public void prepareAttributes() throws TransformerConfigurationException { 85 String fnAtt = getAttribute("href"); 87 if (fnAtt == null) { 88 reportAbsence("href"); 89 } 90 } 91 92 93 public void validate() throws TransformerConfigurationException { 94 checkWithinTemplate(); 95 } 96 97 107 public void process( Context context ) throws TransformerException { 108 Outputter out = context.getOutputter(); 109 110 String hrefAtt = getAttribute("href"); 111 Expression hrefExpr = makeAttributeValueTemplate(hrefAtt); 112 String href = hrefExpr.evaluateAsString(context); 113 114 String encodingAtt = getAttribute("encoding"); 115 Expression encodingExpr = makeAttributeValueTemplate(encodingAtt); 116 String encoding = encodingExpr.evaluateAsString(context); 117 118 String baseURI = context.getContextNodeInfo().getBaseURI(); 119 120 URIResolver resolver = context.getController().getURIResolver(); 121 122 if (resolver != null) { 123 Source source = resolver.resolve(href, baseURI); 124 href = source.getSystemId(); 125 } 126 127 URL baseURL = null; 128 URL fileURL = null; 129 130 try { 131 baseURL = new URL (baseURI); 132 } catch (MalformedURLException e0) { 133 baseURL = null; 135 } 136 137 try { 138 try { 139 fileURL = new URL (baseURL, href); 140 } catch (MalformedURLException e1) { 141 try { 142 fileURL = new URL (baseURL, "file:" + href); 143 } catch (MalformedURLException e2) { 144 System.out.println("Cannot open " + href); 145 return; 146 } 147 } 148 149 InputStreamReader isr = null; 150 if (encoding.equals("") == true) 151 isr = new InputStreamReader (fileURL.openStream()); 152 else 153 isr = new InputStreamReader (fileURL.openStream(), encoding); 154 155 BufferedReader is = new BufferedReader (isr); 156 157 final int BUFFER_SIZE = 4096; 158 char chars[] = new char[BUFFER_SIZE]; 159 char nchars[] = new char[BUFFER_SIZE]; 160 int len = 0; 161 int i = 0; 162 int carry = -1; 163 164 while ((len = is.read(chars)) > 0) { 165 168 int nlen = 0; 169 for (i=0; i<len; i++) { 170 if (chars[i] == '\r') { 172 if (i < (len - 1)) { 173 if (chars[i+1] == '\n') continue; 175 nchars[nlen] = '\n'; 177 nlen++; 178 continue; 179 } else { 180 carry = is.read(); 182 nchars[nlen] = '\n'; 183 nlen++; 184 if (carry == '\n') { 185 carry = -1; 186 } 187 break; 188 } 189 } 190 nchars[nlen] = chars[i]; 191 nlen++; 192 } 193 out.writeContent(nchars, 0, nlen); 194 if (carry != -1) out.writeContent(String.valueOf((char)carry)); 196 carry = -1; 197 } 198 is.close(); 199 } catch (Exception e) { 200 System.out.println("Cannot read " + href); 201 } 202 } 203 } 204 | Popular Tags |