📄 table.java
字号:
while (child != null) { if (child.getNodeType() == Node.ELEMENT_NODE && (child.getNodeName().equals("col") || (child.getNamespaceURI().equals(foURI) && child.getLocalName().equals("table-column")))) { Element col = (Element) child; columns[colnum] = col; if (foStylesheet) { if ("".equals(col.getAttribute("column-width"))) { widths[colnum] = "1*"; } else { widths[colnum] = col.getAttribute("column-width"); } } else { if ("".equals(col.getAttribute("width"))) { widths[colnum] = "1*"; } else { widths[colnum] = col.getAttribute("width"); } } colnum++; } child = child.getNextSibling(); } float relTotal = 0; float relParts[] = new float[numColumns]; float absTotal = 0; float absParts[] = new float[numColumns]; for (int count = 0; count < numColumns; count++) { String width = widths[count]; int pos = width.indexOf("*"); if (pos >= 0) { String relPart = width.substring(0, pos); String absPart = width.substring(pos+1); try { float rel = Float.parseFloat(relPart); relTotal += rel; relParts[count] = rel; } catch (NumberFormatException e) { System.out.println(relPart + " is not a valid relative unit."); } int pixels = 0; if (absPart != null && !absPart.equals("")) { pixels = convertLength(absPart); } absTotal += pixels; absParts[count] = pixels; } else { relParts[count] = 0; int pixels = 0; if (width != null && !width.equals("")) { pixels = convertLength(width); } absTotal += pixels; absParts[count] = pixels; } } // Ok, now we have the relative widths and absolute widths in // two parallel arrays. // // - If there are no relative widths, output the absolute widths // - If there are no absolute widths, output the relative widths // - If there are a mixture of relative and absolute widths, // - If the table width is absolute, turn these all into absolute // widths. // - If the table width is relative, turn these all into absolute // widths in the nominalWidth and then turn them back into // percentages. if (relTotal == 0) { for (int count = 0; count < numColumns; count++) { Float f = new Float(absParts[count]); if (foStylesheet) { int pixels = f.intValue(); float inches = (float) pixels / pixelsPerInch; widths[count] = inches + "in"; } else { widths[count] = Integer.toString(f.intValue()); } } } else if (absTotal == 0) { for (int count = 0; count < numColumns; count++) { float rel = relParts[count] / relTotal * 100; Float f = new Float(rel); widths[count] = Integer.toString(f.intValue()); } widths = correctRoundingError(widths); } else { int pixelWidth = nominalWidth; if (tableWidth.indexOf("%") <= 0) { pixelWidth = convertLength(tableWidth); } if (pixelWidth <= absTotal) { System.out.println("Table is wider than table width."); } else { pixelWidth -= absTotal; } absTotal = 0; for (int count = 0; count < numColumns; count++) { float rel = relParts[count] / relTotal * pixelWidth; relParts[count] = rel + absParts[count]; absTotal += rel + absParts[count]; } if (tableWidth.indexOf("%") <= 0) { for (int count = 0; count < numColumns; count++) { Float f = new Float(relParts[count]); if (foStylesheet) { int pixels = f.intValue(); float inches = (float) pixels / pixelsPerInch; widths[count] = inches + "in"; } else { widths[count] = Integer.toString(f.intValue()); } } } else { for (int count = 0; count < numColumns; count++) { float rel = relParts[count] / absTotal * 100; Float f = new Float(rel); widths[count] = Integer.toString(f.intValue()); } widths = correctRoundingError(widths); } } // Now rebuild the colgroup with the right widths DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = null; try { docBuilder = docFactory.newDocumentBuilder(); } catch (ParserConfigurationException e) { System.out.println("PCE!"); return xalanRTF; } Document doc = docBuilder.newDocument(); DocumentFragment df = doc.createDocumentFragment(); DOMBuilder rtf = new DOMBuilder(doc, df); try { String ns = colgroup.getNamespaceURI(); String localName = colgroup.getLocalName(); String name = colgroup.getTagName(); if (colgroup.getLocalName().equals("colgroup")) { rtf.startElement(ns, localName, name, copyAttributes(colgroup)); } for (colnum = 0; colnum < numColumns; colnum++) { Element col = columns[colnum]; NamedNodeMap domAttr = col.getAttributes(); AttributesImpl attr = new AttributesImpl(); for (int acount = 0; acount < domAttr.getLength(); acount++) { Node a = domAttr.item(acount); String a_ns = a.getNamespaceURI(); String a_localName = a.getLocalName(); if ((foStylesheet && !a_localName.equals("column-width")) || !a_localName.equalsIgnoreCase("width")) { attr.addAttribute(a.getNamespaceURI(), a.getLocalName(), a.getNodeName(), "CDATA", a.getNodeValue()); } } if (foStylesheet) { attr.addAttribute("", "column-width", "column-width", "CDATA", widths[colnum]); } else { attr.addAttribute("", "width", "width", "CDATA", widths[colnum]); } rtf.startElement(col.getNamespaceURI(), col.getLocalName(), col.getTagName(), attr); rtf.endElement(col.getNamespaceURI(), col.getLocalName(), col.getTagName()); } if (colgroup.getLocalName().equals("colgroup")) { rtf.endElement(ns, localName, name); } } catch (SAXException se) { System.out.println("SE!"); return xalanRTF; } return df; } 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; } /** * Correct rounding errors introduced in calculating the width of each * column. Make sure they sum to 100% in the end. */ protected String[] correctRoundingError(String widths[]) { int totalWidth = 0; for (int count = 0; count < widths.length; count++) { try { int width = Integer.parseInt(widths[count]); totalWidth += width; } catch (NumberFormatException nfe) { // nop; "can't happen" } } float totalError = 100 - totalWidth; float columnError = totalError / widths.length; float error = 0; for (int count = 0; count < widths.length; count++) { try { int width = Integer.parseInt(widths[count]); error = error + columnError; if (error >= 1.0) { int adj = (int) Math.round(Math.floor(error)); error = error - (float) Math.floor(error); width = width + adj; widths[count] = Integer.toString(width) + "%"; } else { widths[count] = Integer.toString(width) + "%"; } } catch (NumberFormatException nfe) { // nop; "can't happen" } } return widths; }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -