📄 verbatim.java
字号:
if (pos > 0) { rtf.characters(chars, 0, pos); } } else if (node.getNodeType() == Node.COMMENT_NODE) { String text = node.getNodeValue(); char chars[] = text.toCharArray(); rtf.comment(chars, 0, text.length()); } else if (node.getNodeType() == Node.PROCESSING_INSTRUCTION_NODE) { rtf.processingInstruction(node.getNodeName(), node.getNodeValue()); } else { System.out.println("Warning: unexpected node type in lineNumberFragment"); } if (node.getNodeType() == Node.ELEMENT_NODE) { String ns = node.getNamespaceURI(); String localName = node.getLocalName(); String name = ((Element) node).getTagName(); rtf.endElement(ns, localName, name); elementStack.pop(); } } catch (SAXException e) { System.out.println("SAX Exception in lineNumberFragment"); } } /** * <p>Add a formatted line number to the result tree fragment.</p> * * <p>This method examines the global parameters that control line * number presentation (modulus, width, and separator) and adds * the appropriate text to the result tree fragment.</p> * * @param rtf The resulting verbatim environment with numbered lines. * @param lineNumber The number of the current line. */ private void formatLineNumber(DOMBuilder rtf, int lineNumber) { char ch = 160; String lno = ""; if (lineNumber == 1 || (modulus >= 1 && (lineNumber % modulus == 0))) { lno = "" + lineNumber; } while (lno.length() < width) { lno = ch + lno; } lno += separator; char chars[] = lno.toCharArray(); try { rtf.characters(chars, 0, lno.length()); } catch (SAXException e) { System.out.println("SAX Exception in formatLineNumber"); } } /** * <p>Insert text callouts into a verbatim environment.</p> * * <p>This method examines the <tt>areaset</tt> and <tt>area</tt> elements * in the supplied <tt>areaspec</tt> and decorates the supplied * result tree fragment with appropriate callout markers.</p> * * <p>If a <tt>label</tt> attribute is supplied on an <tt>area</tt>, * its content will be used for the label, otherwise the callout * number will be used, surrounded by parenthesis. Callouts are * numbered in document order. All of the <tt>area</tt>s in an * <tt>areaset</tt> get the same number.</p> * * <p>Only the <tt>linecolumn</tt> and <tt>linerange</tt> units are * supported. If no unit is specifed, <tt>linecolumn</tt> is assumed. * If only a line is specified, the callout decoration appears in * the defaultColumn. Lines will be padded with blanks to reach the * necessary column, but callouts that are located beyond the last * line of the verbatim environment will be ignored.</p> * * <p>Callouts are inserted before the character at the line/column * where they are to occur.</p> * * @param areaspecNodeSet The source node set that contains the areaspec. * @param xalanRTF The result tree fragment of the verbatim environment. * @param defaultColumn The column for callouts that specify only a line. * * @return The modified result tree fragment. */ /** * <p>Insert graphical callouts into a verbatim environment.</p> * * <p>This method examines the <tt>areaset</tt> and <tt>area</tt> elements * in the supplied <tt>areaspec</tt> and decorates the supplied * result tree fragment with appropriate callout markers.</p> * * <p>If a <tt>label</tt> attribute is supplied on an <tt>area</tt>, * its content will be used for the label, otherwise the callout * number will be used. Callouts are * numbered in document order. All of the <tt>area</tt>s in an * <tt>areaset</tt> get the same number.</p> * * <p>If the callout number is not greater than <tt>gMax</tt>, the * callout generated will be:</p> * * <pre> * <img src="$gPath/conumber$gExt" alt="conumber"> * </pre> * * <p>Otherwise, it will be the callout number surrounded by * parenthesis.</p> * * <p>Only the <tt>linecolumn</tt> and <tt>linerange</tt> units are * supported. If no unit is specifed, <tt>linecolumn</tt> is assumed. * If only a line is specified, the callout decoration appears in * the defaultColumn. Lines will be padded with blanks to reach the * necessary column, but callouts that are located beyond the last * line of the verbatim environment will be ignored.</p> * * <p>Callouts are inserted before the character at the line/column * where they are to occur.</p> * * @param areaspecNodeSet The source node set that contains the areaspec. * @param xalanRTF The result tree fragment of the verbatim environment. * @param defaultColumn The column for callouts that specify only a line. * @param gPath The path to use for callout graphics. * @param gExt The extension to use for callout graphics. * @param gMax The largest number that can be represented as a graphic. * @param useFO Should fo:external-graphics be produced, as opposed to * HTML imgs. This is bogus, the extension should figure it out, but I * haven't figured out how to do that yet. * * @return The modified result tree fragment. */ public DocumentFragment insertCallouts (ExpressionContext context, NodeIterator areaspecNodeSet, NodeIterator xalanNI) { String type = Params.getString(context, "stylesheet.result.type"); boolean useFO = type.equals("fo"); int defaultColumn = Params.getInt(context, "callout.defaultcolumn"); if (Params.getBoolean(context, "callout.graphics")) { String gPath = Params.getString(context, "callout.graphics.path"); String gExt = Params.getString(context, "callout.graphics.extension"); int gMax = Params.getInt(context, "callout.graphics.number.limit"); return insertGraphicCallouts(areaspecNodeSet, xalanNI, defaultColumn, gPath, gExt, gMax, useFO); } else if (Params.getBoolean(context, "callout.unicode")) { int uStart = Params.getInt(context, "callout.unicode.start.character"); int uMax = Params.getInt(context, "callout.unicode.number.limit"); return insertUnicodeCallouts(areaspecNodeSet, xalanNI, defaultColumn, uStart, uMax, useFO); } else if (Params.getBoolean(context, "callout.dingbats")) { int dMax = 10; return insertDingbatCallouts(areaspecNodeSet, xalanNI, defaultColumn, dMax, useFO); } else { return insertTextCallouts(areaspecNodeSet, xalanNI, defaultColumn, useFO); } } public DocumentFragment insertGraphicCallouts (NodeIterator areaspecNodeSet, NodeIterator xalanNI, int defaultColumn, String gPath, String gExt, int gMax, boolean useFO) { FormatGraphicCallout fgc = new FormatGraphicCallout(gPath,gExt,gMax,useFO); return insertCallouts(areaspecNodeSet, xalanNI, defaultColumn, fgc); } public DocumentFragment insertUnicodeCallouts (NodeIterator areaspecNodeSet, NodeIterator xalanNI, int defaultColumn, int uStart, int uMax, boolean useFO) { FormatUnicodeCallout fuc = new FormatUnicodeCallout(uStart, uMax, useFO); return insertCallouts(areaspecNodeSet, xalanNI, defaultColumn, fuc); } public DocumentFragment insertDingbatCallouts (NodeIterator areaspecNodeSet, NodeIterator xalanNI, int defaultColumn, int gMax, boolean useFO) { FormatDingbatCallout fdc = new FormatDingbatCallout(gMax,useFO); return insertCallouts(areaspecNodeSet, xalanNI, defaultColumn, fdc); } public DocumentFragment insertTextCallouts (NodeIterator areaspecNodeSet, NodeIterator xalanNI, int defaultColumn, boolean useFO) { FormatTextCallout ftc = new FormatTextCallout(useFO); return insertCallouts(areaspecNodeSet, xalanNI, defaultColumn, ftc); } public DocumentFragment insertCallouts (NodeIterator areaspecNodeSet, NodeIterator xalanNI, int defaultColumn, FormatCallout fCallout) { DocumentFragment xalanRTF = (DocumentFragment) xalanNI.nextNode(); callout = new Callout[10]; calloutCount = 0; calloutPos = 0; lineNumber = 1; colNumber = 1; // First we walk through the areaspec to calculate the position // of the callouts // <areaspec> // <areaset id="ex.plco.const" coords=""> // <area id="ex.plco.c1" coords="4"/> // <area id="ex.plco.c2" coords="8"/> // </areaset> // <area id="ex.plco.ret" coords="12"/> // <area id="ex.plco.dest" coords="12"/> // </areaspec> int pos = 0; int coNum = 0; boolean inAreaSet = false; Node node = areaspecNodeSet.nextNode(); node = node.getFirstChild(); while (node != null) { if (node.getNodeType() == Node.ELEMENT_NODE) { if (node.getNodeName().equals("areaset")) { coNum++; Node area = node.getFirstChild(); while (area != null) { if (area.getNodeType() == Node.ELEMENT_NODE) { if (area.getNodeName().equals("area")) { addCallout(coNum, area, defaultColumn); } else { System.out.println("Unexpected element in areaset: " + area.getNodeName()); } } area = area.getNextSibling(); } } else if (node.getNodeName().equalsIgnoreCase("area")) { coNum++; addCallout(coNum, node, defaultColumn); } else { System.out.println("Unexpected element in areaspec: " + node.getNodeName()); } } node = node.getNextSibling(); } // Now sort them java.util.Arrays.sort(callout, 0, calloutCount); DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = null; try { docBuilder = docFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { System.out.println("PCE 2!"); return xalanRTF; } Document doc = docBuilder.newDocument(); DocumentFragment df = doc.createDocumentFragment(); DOMBuilder db = new DOMBuilder(doc, df); elementStack = new Stack(); calloutFragment(db, xalanRTF, fCallout); return df; } /** * <p>Build a FragmentValue with callout decorations.</p> * * <p>This is the method that actually does the work of adding * callouts to a verbatim environment. It recursively walks through a * tree of nodes, copying the structure into the rtf. Text nodes * are examined for the position of callouts as described by the * global callout parameters.</p> * * <p>When called, rtf should be an empty FragmentValue and node * should be the first child of the result tree fragment that contains * the existing, formatted verbatim text.</p> * * @param rtf The resulting verbatim environment with numbered lines. * @param node The root of the tree to copy. */ private void calloutFragment(DOMBuilder rtf, Node node, FormatCallout fCallout) { try { if (node.getNodeType() == Node.DOCUMENT_FRAGMENT_NODE || node.getNodeType() == Node.DOCUMENT_NODE) { Node child = node.getFirstChild(); while (child != null) { calloutFragment(rtf, child, fCallout); child = child.getNextSibling(); } } else if (node.getNodeType() == Node.ELEMENT_NODE) { String ns = node.getNamespaceURI(); String localName = node.getLocalName(); String name = ((Element) node).getTagName(); rtf.startElement(ns, localName, name, copyAttributes((Element) node));
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -