1 16 package org.outerj.daisy.frontend; 17 18 import org.apache.cocoon.transformation.AbstractTransformer; 19 import org.apache.cocoon.environment.SourceResolver; 20 import org.apache.cocoon.ProcessingException; 21 import org.apache.cocoon.xml.AttributesImpl; 22 import org.apache.avalon.framework.parameters.Parameters; 23 import org.xml.sax.SAXException ; 24 import org.xml.sax.Attributes ; 25 26 import java.util.Map ; 27 import java.util.Stack ; 28 import java.io.IOException ; 29 30 35 public class TableHelperTransformer extends AbstractTransformer { 36 private Stack tableInfoStack; 37 private TableInfo tableInfo; 38 39 public void setup(SourceResolver sourceResolver, Map map, String s, Parameters parameters) throws ProcessingException, SAXException , IOException { 40 tableInfoStack = new Stack (); 41 tableInfo = null; 42 } 43 44 public void startElement(String namespaceURI, String localName, String qName, Attributes attrs) throws SAXException { 45 if (namespaceURI.equals("")) { 46 if (localName.equals("table")) { 47 if (tableInfo != null) 48 tableInfoStack.push(tableInfo); 49 tableInfo = new TableInfo(); 50 tableInfo.columnWidths = attrs.getValue("column-widths"); 51 tableInfo.printColumnWidths = attrs.getValue("print-column-widths"); 52 } else if (tableInfo != null && localName.equals("tr")) { 53 tableInfo.maxColumnsCurrentRow = 0; 54 for (int i = 0; i < tableInfo.activeRowSpans.length; i++) { 55 int rowspan = tableInfo.activeRowSpans[i]; 56 if (rowspan > 0) { 57 tableInfo.maxColumnsCurrentRow++; 58 tableInfo.activeRowSpans[i] = rowspan - 1; 59 } 60 } 61 } else if (tableInfo != null && localName.equals("td") || localName.equals("th")) { 62 String rowspanAttr = attrs.getValue("rowspan"); 63 if (rowspanAttr != null && rowspanAttr.length() > 0) { 64 try { 65 int rowspan = Integer.parseInt(rowspanAttr); 66 if (rowspan > 1) { 67 tableInfo.activeRowSpansCount++; 68 assureActiveRowSpanArrayIsBigEnough(); 69 tableInfo.activeRowSpans[tableInfo.activeRowSpansCount] = rowspan - 1; 70 } 71 } catch (NumberFormatException e) { 72 } 74 } 75 tableInfo.maxColumnsCurrentRow += 1; 76 } 77 } 78 super.startElement(namespaceURI, localName, qName, attrs); 79 } 80 81 public void endElement(String namespaceURI, String localName, String qName) throws SAXException { 82 if (namespaceURI.equals("") && tableInfo != null) { 83 if (localName.equals("table")) { 84 AttributesImpl attrs = new AttributesImpl(); 85 attrs.addCDATAAttribute("maxColumns", String.valueOf(tableInfo.maxColumns)); 86 87 super.startElement("", "computedInfo", "computedInfo", attrs); 88 89 if (tableInfo.columnWidths != null) { 90 super.startElement("", "html", "html", new AttributesImpl()); 91 generateColumnInfo(tableInfo.columnWidths, tableInfo.maxColumns); 92 super.endElement("", "html", "html"); 93 } 94 95 if (tableInfo.printColumnWidths != null) { 96 super.startElement("", "print", "print", new AttributesImpl()); 97 generateColumnInfo(tableInfo.printColumnWidths, tableInfo.maxColumns); 98 super.endElement("", "print", "print"); 99 } 100 101 super.endElement("", "computedInfo", "computedInfo"); 102 103 tableInfo = null; 104 if (tableInfoStack.size() > 0) { 105 tableInfo = (TableInfo)tableInfoStack.pop(); 106 } 107 } else if (localName.equals("tr")) { 108 if (tableInfo.maxColumnsCurrentRow > tableInfo.maxColumns) 109 tableInfo.maxColumns = tableInfo.maxColumnsCurrentRow; 110 } 111 } 112 super.endElement(namespaceURI, localName, qName); 113 } 114 115 private void generateColumnInfo(String columnInfo, int maxColumns) throws SAXException { 116 String [] columnInfos = columnInfo.split(";", maxColumns); 117 for (int i = 0; i < columnInfos.length; i++) { 118 AttributesImpl printColAttrs = new AttributesImpl(); 119 if (columnInfos[i].trim().length() > 0) 120 printColAttrs.addCDATAAttribute("width", columnInfos[i]); 121 super.startElement("", "col", "col", printColAttrs); 122 super.endElement("", "col", "col"); 123 } 124 for (int i = columnInfos.length; i < maxColumns; i++) { 125 super.startElement("", "col", "col", new AttributesImpl()); 126 super.endElement("", "col", "col"); 127 } 128 } 129 130 static class TableInfo { 131 public int maxColumns = 0; 132 public int maxColumnsCurrentRow = 0; 133 public int[] activeRowSpans = new int[5]; 137 public int activeRowSpansCount = -1; 138 public String columnWidths; 139 public String printColumnWidths; 140 } 141 142 private void assureActiveRowSpanArrayIsBigEnough() { 143 if (tableInfo.activeRowSpans.length <= tableInfo.activeRowSpansCount) { 144 int[] newArray = new int[tableInfo.activeRowSpans.length + 10]; 145 System.arraycopy(tableInfo.activeRowSpans, 0, newArray, 0, tableInfo.activeRowSpans.length); 146 tableInfo.activeRowSpans = newArray; 147 } 148 } 149 } 150 | Popular Tags |