📄 verbatim.java
字号:
Node child = node.getFirstChild(); while (child != null) { calloutFragment(rtf, child, fCallout); child = child.getNextSibling(); } } else if (node.getNodeType() == Node.TEXT_NODE) { String text = node.getNodeValue(); char chars[] = text.toCharArray(); int pos = 0; for (int count = 0; count < text.length(); count++) { if (calloutPos < calloutCount && callout[calloutPos].getLine() == lineNumber && callout[calloutPos].getColumn() == colNumber) { if (pos > 0) { rtf.characters(chars, 0, pos); pos = 0; } closeOpenElements(rtf); while (calloutPos < calloutCount && callout[calloutPos].getLine() == lineNumber && callout[calloutPos].getColumn() == colNumber) { fCallout.formatCallout(rtf, callout[calloutPos]); calloutPos++; } openClosedElements(rtf); } if (text.charAt(count) == '\n') { // What if we need to pad this line? if (calloutPos < calloutCount && callout[calloutPos].getLine() == lineNumber && callout[calloutPos].getColumn() > colNumber) { if (pos > 0) { rtf.characters(chars, 0, pos); pos = 0; } closeOpenElements(rtf); while (calloutPos < calloutCount && callout[calloutPos].getLine() == lineNumber && callout[calloutPos].getColumn() > colNumber) { formatPad(rtf, callout[calloutPos].getColumn() - colNumber); colNumber = callout[calloutPos].getColumn(); while (calloutPos < calloutCount && callout[calloutPos].getLine() == lineNumber && callout[calloutPos].getColumn() == colNumber) { fCallout.formatCallout(rtf, callout[calloutPos]); calloutPos++; } } openClosedElements(rtf); } lineNumber++; colNumber = 1; } else { colNumber++; } chars[pos++] = text.charAt(count); } 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 calloutFragment: " + node.getNodeType() + ": " + node.getNodeName()); } 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(); } else { // nop } } catch (SAXException e) { System.out.println("SAX Exception in calloutFragment"); } } /** * <p>Add a callout to the global callout array</p> * * <p>This method examines a callout <tt>area</tt> and adds it to * the global callout array if it can be interpreted.</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 <tt>defaultColumn</tt>.</p> * * @param coNum The callout number. * @param node The <tt>area</tt>. * @param defaultColumn The default column for callouts. */ private void addCallout (int coNum, Node node, int defaultColumn) { Element area = (Element) node; String units = area.getAttribute("units"); String otherUnits = area.getAttribute("otherunits"); String coords = area.getAttribute("coords"); int type = 0; String otherType = null; if ("".equals(units) || units.equals("linecolumn")) { type = Callout.LINE_COLUMN; // the default } else if (units.equals("linerange")) { type = Callout.LINE_RANGE; } else if (units.equals("linecolumnpair")) { type = Callout.LINE_COLUMN_PAIR; } else if (units.equals("calspair")) { type = Callout.CALS_PAIR; } else { type = Callout.OTHER; otherType = otherUnits; } if (type != Callout.LINE_COLUMN && type != Callout.LINE_RANGE) { System.out.println("Only linecolumn and linerange units are supported"); return; } if (coords == null) { System.out.println("Coords must be specified"); return; } // Now let's see if we can interpret the coordinates... StringTokenizer st = new StringTokenizer(coords); int tokenCount = 0; int c1 = 0; int c2 = 0; while (st.hasMoreTokens()) { tokenCount++; if (tokenCount > 2) { System.out.println("Unparseable coordinates"); return; } try { String token = st.nextToken(); int coord = Integer.parseInt(token); c2 = coord; if (tokenCount == 1) { c1 = coord; } } catch (NumberFormatException e) { System.out.println("Unparseable coordinate"); return; } } // Make sure we aren't going to blow past the end of our array if (calloutCount == callout.length) { Callout bigger[] = new Callout[calloutCount+10]; for (int count = 0; count < callout.length; count++) { bigger[count] = callout[count]; } callout = bigger; } // Ok, add the callout if (tokenCount == 2) { if (type == Callout.LINE_RANGE) { for (int count = c1; count <= c2; count++) { callout[calloutCount++] = new Callout(coNum, area, count, defaultColumn, type); } } else { // assume linecolumn callout[calloutCount++] = new Callout(coNum, area, c1, c2, type); } } else { // if there's only one number, assume it's the line callout[calloutCount++] = new Callout(coNum, area, c1, defaultColumn, type); } } /** * <p>Add blanks to the result tree fragment.</p> * * <p>This method adds <tt>numBlanks</tt> to the result tree fragment. * It's used to pad lines when callouts occur after the last existing * characater in a line.</p> * * @param rtf The resulting verbatim environment with numbered lines. * @param numBlanks The number of blanks to add. */ private void formatPad(DOMBuilder rtf, int numBlanks) { char chars[] = new char[numBlanks]; for (int count = 0; count < numBlanks; count++) { chars[count] = ' '; } try { rtf.characters(chars, 0, numBlanks); } catch (SAXException e) { System.out.println("SAX Exception in formatCallout"); } } private void closeOpenElements(DOMBuilder rtf) throws SAXException { // Close all the open elements... tempStack = new Stack(); while (!elementStack.empty()) { Node elem = (Node) elementStack.pop(); String ns = elem.getNamespaceURI(); String localName = elem.getLocalName(); String name = ((Element) elem).getTagName(); // If this is the bottom of the stack and it's an fo:block // or an HTML pre or div, don't duplicate it... if (elementStack.empty() && (((ns != null) && ns.equals(foURI) && localName.equals("block")) || (((ns == null) && localName.equalsIgnoreCase("pre")) || ((ns != null) && ns.equals(xhURI) && localName.equals("pre"))) || (((ns == null) && localName.equalsIgnoreCase("div")) || ((ns != null) && ns.equals(xhURI) && localName.equals("div"))))) { elementStack.push(elem); break; } else { rtf.endElement(ns, localName, name); tempStack.push(elem); } } } private void openClosedElements(DOMBuilder rtf) throws SAXException { // Now "reopen" the elements that we closed... while (!tempStack.empty()) { Node elem = (Node) tempStack.pop(); String ns = elem.getNamespaceURI(); String localName = elem.getLocalName(); String name = ((Element) elem).getTagName(); NamedNodeMap domAttr = elem.getAttributes(); AttributesImpl attr = new AttributesImpl(); for (int acount = 0; acount < domAttr.getLength(); acount++) { Node a = domAttr.item(acount); if (((ns == null || ns == "http://www.w3.org/1999/xhtml") && localName.equalsIgnoreCase("a")) || (a.getLocalName().equalsIgnoreCase("id"))) { // skip this attribute } else { attr.addAttribute(a.getNamespaceURI(), a.getLocalName(), a.getNodeName(), "CDATA", a.getNodeValue()); } } rtf.startElement(ns, localName, name, attr); elementStack.push(elem); } tempStack = null; } private Attributes copyAttributes(Element node) { AttributesImpl attrs = new AttributesImpl(); NamedNodeMap nnm = node.getAttributes(); for (int count = 0; count < nnm.getLength(); count++) { Attr attr = (Attr) nnm.item(count); String name = attr.getName(); if (name.startsWith("xmlns:") || name.equals("xmlns")) { // Skip it; (don't ya just love it!!) } else { attrs.addAttribute(attr.getNamespaceURI(), attr.getName(), attr.getName(), "CDATA", attr.getValue()); } } return attrs; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -