KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > outerj > daisy > frontend > TableHelperTransformer


1 /*
2  * Copyright 2004 Outerthought bvba and Schaubroeck nv
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */

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 JavaDoc;
24 import org.xml.sax.Attributes JavaDoc;
25
26 import java.util.Map JavaDoc;
27 import java.util.Stack JavaDoc;
28 import java.io.IOException JavaDoc;
29
30 /**
31  * A transformer that counts the maximum number of cells on a row in a table
32  * (taking into account colspan attributes) and inserts that information into
33  * the SAX stream. This seemed a bit too annoying to do in XSL.
34  */

35 public class TableHelperTransformer extends AbstractTransformer {
36     private Stack JavaDoc tableInfoStack;
37     private TableInfo tableInfo;
38
39     public void setup(SourceResolver sourceResolver, Map JavaDoc map, String JavaDoc s, Parameters parameters) throws ProcessingException, SAXException JavaDoc, IOException JavaDoc {
40         tableInfoStack = new Stack JavaDoc();
41         tableInfo = null;
42     }
43
44     public void startElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName, Attributes JavaDoc attrs) throws SAXException JavaDoc {
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 JavaDoc 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 JavaDoc e) {
72                         // invalid number in rowspan attribute: ignore
73
}
74                 }
75                 tableInfo.maxColumnsCurrentRow += 1;
76             }
77         }
78         super.startElement(namespaceURI, localName, qName, attrs);
79     }
80
81     public void endElement(String JavaDoc namespaceURI, String JavaDoc localName, String JavaDoc qName) throws SAXException JavaDoc {
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 JavaDoc columnInfo, int maxColumns) throws SAXException JavaDoc {
116         String JavaDoc[] 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         // Note: the activeRowSpans array only grows, i.e. each new cell with a rowspan adds
134
// an entry to this array. This is not really a problem unless one would have
135
// a really huge table with lots of rowspans.
136
public int[] activeRowSpans = new int[5];
137         public int activeRowSpansCount = -1;
138         public String JavaDoc columnWidths;
139         public String JavaDoc 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