/** * Insert the text of the file into the result tree * * <p>Processing this element inserts the contents of the URL named by the href attribute into the * result tree as plain text. */ public void process(Context context) throws TransformerException { Outputter out = context.getOutputter(); String hrefAtt = getAttribute("href"); Expression hrefExpr = makeAttributeValueTemplate(hrefAtt); String href = hrefExpr.evaluateAsString(context); URL fileURL = null; try { try { fileURL = new URL(href); } catch (MalformedURLException e1) { try { fileURL = new URL("file:" + href); } catch (MalformedURLException e2) { System.out.println("Cannot open " + href); return; } } InputStreamReader isr = new InputStreamReader(fileURL.openStream()); BufferedReader is = new BufferedReader(isr); char chars[] = new char[4096]; int len = 0; while ((len = is.read(chars)) > 0) { out.writeContent(chars, 0, len); } is.close(); } catch (Exception e) { System.out.println("Cannot read " + href); } }
/** * Insert the text of the file into the result tree * * <p>Processing this element inserts the contents of the URL named by the href attribute into the * result tree as plain text. * * <p>Optional encoding attribute can specify encoding of resource. If not specified default * system encoding is used. */ public void process(Context context) throws TransformerException { Outputter out = context.getOutputter(); String hrefAtt = getAttribute("href"); Expression hrefExpr = makeAttributeValueTemplate(hrefAtt); String href = hrefExpr.evaluateAsString(context); String encodingAtt = getAttribute("encoding"); Expression encodingExpr = makeAttributeValueTemplate(encodingAtt); String encoding = encodingExpr.evaluateAsString(context); String baseURI = context.getContextNodeInfo().getBaseURI(); URIResolver resolver = context.getController().getURIResolver(); if (resolver != null) { Source source = resolver.resolve(href, baseURI); href = source.getSystemId(); } URL baseURL = null; URL fileURL = null; try { baseURL = new URL(baseURI); } catch (MalformedURLException e0) { // what the!? baseURL = null; } try { try { fileURL = new URL(baseURL, href); } catch (MalformedURLException e1) { try { fileURL = new URL(baseURL, "file:" + href); } catch (MalformedURLException e2) { System.out.println("Cannot open " + href); return; } } InputStreamReader isr = null; if (encoding.equals("") == true) isr = new InputStreamReader(fileURL.openStream()); else isr = new InputStreamReader(fileURL.openStream(), encoding); BufferedReader is = new BufferedReader(isr); final int BUFFER_SIZE = 4096; char chars[] = new char[BUFFER_SIZE]; char nchars[] = new char[BUFFER_SIZE]; int len = 0; int i = 0; int carry = -1; while ((len = is.read(chars)) > 0) { // various new lines are normalized to LF to prevent blank lines // between lines int nlen = 0; for (i = 0; i < len; i++) { // is current char CR? if (chars[i] == '\r') { if (i < (len - 1)) { // skip it if next char is LF if (chars[i + 1] == '\n') continue; // single CR -> LF to normalize MAC line endings nchars[nlen] = '\n'; nlen++; continue; } else { // if CR is last char of buffer we must look ahead carry = is.read(); nchars[nlen] = '\n'; nlen++; if (carry == '\n') { carry = -1; } break; } } nchars[nlen] = chars[i]; nlen++; } out.writeContent(nchars, 0, nlen); // handle look aheaded character if (carry != -1) out.writeContent(String.valueOf((char) carry)); carry = -1; } is.close(); } catch (Exception e) { System.out.println("Cannot read " + href); } }