/** * Unwrap links * * @param context The current stylesheet context. * @param rtf_ns The result tree fragment of the verbatim environment. * @return The modified result tree fragment. */ public static NodeSetValue unwrapLinks(Context context, NodeSetValue rtf_ns) { FragmentValue rtf = (FragmentValue) rtf_ns; boolean tryAgain = true; setupUnwrapLinks(context); try { Controller controller = context.getController(); NamePool namePool = controller.getNamePool(); while (tryAgain) { UnwrapLinksEmitter ulEmitter = new UnwrapLinksEmitter(controller, namePool, foStylesheet); rtf.replay(ulEmitter); tryAgain = ulEmitter.tryAgain(); rtf = (FragmentValue) ulEmitter.getResultTreeFragment(); } return rtf; } catch (TransformerException e) { // This "can't" happen. System.out.println("Transformer Exception in unwrapLinks"); return rtf; } }
/** * 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); } }
public String evaluateAsString(Context context) throws XPathException { /* 43*/ if (getNumberOfArguments() == 1) { /* 44*/ NodeEnumeration nodeenumeration = super.argument[0].enumerate(context, true); /* 45*/ if (nodeenumeration.hasMoreElements()) /* 46*/ return nodeenumeration.nextElement().getLocalName(); /* 48*/ else /* 48*/ return ""; } else { /* 51*/ return context.getContextNodeInfo().getLocalName(); } }
/** * 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); } }