Debian Codegraph @

texk/tex4htk/java/OoUtilities.java

raw
1 package tex4ht;
2 /**********************************************************/
3 /* OoUtilities.java 2009-03-11-03:09 */
4 /* Copyright (C) 2006--2009 Eitan M. Gurari */
5 /* */
6 /* This work may be distributed and/or modified under the */
7 /* conditions of the LaTeX Project Public License, either */
8 /* version 1.3 of this license or (at your option) any */
9 /* later version. The latest version of this license is */
10 /* in */
11 /* http://www.latex-project.org/lppl.txt */
12 /* and version 1.3 or later is part of all distributions */
13 /* of LaTeX version 2003/12/01 or later. */
14 /* */
15 /* This work has the LPPL maintenance status "maintained".*/
16 /* */
17 /* This Current Maintainer of this work */
18 /* is Eitan M. Gurari. */
19 /* */
20 /* [email protected] */
21 /* http://www.cse.ohio-state.edu/~gurari */
22 /**********************************************************/
23
24
25 import org.w3c.dom.*;
26 public class OoUtilities {
27 public static void mtable(Node dom) {
28 Node mtr, mtd, d;
29 int cols = 0;
30 Node node = dom.getFirstChild();
31 if (node.hasChildNodes()) {
32 mtr = node.getLastChild();
33 while( mtr != null){
34 if( mtr.getNodeType() == Node.ELEMENT_NODE ){
35 if (mtr.hasChildNodes()) {
36 mtd = mtr.getLastChild();
37 int count = 0;
38 while( mtd != null){
39 if( mtd.getNodeType() == Node.ELEMENT_NODE ){
40 count++;
41 d = mtd.getFirstChild();
42 if( d != null ){
43 boolean remove = true;
44 for(Node i=d; i!=null; i=i.getNextSibling() ){
45 if( (i.getNodeType() == Node.ELEMENT_NODE)
46 && !i.getNodeName().equals("math:mspace") ) {
47 remove = false; break;
48 } }
49 if( remove ){
50 while( d != null ){
51 mtd.removeChild(d);
52 d = mtd.getFirstChild();
53 } }
54
55 }
56 if( d != null ){
57 if(
58 (d.getNodeType() == Node.TEXT_NODE)
59 && d.getNodeValue().trim().equals("")
60 ){
61 d.getParentNode().removeChild(d);
62 d = null;
63 }
64
65 } }
66 d = mtd;
67 mtd = mtd.getPreviousSibling();
68 if(
69 (d.getNodeType() == Node.TEXT_NODE)
70 && d.getNodeValue().trim().equals("")
71 ){
72 d.getParentNode().removeChild(d);
73 d = null;
74 }
75
76 if( (d != null)
77 && (d.getNodeType() == Node.ELEMENT_NODE) ){
78 if( (d.getNextSibling()==null)
79 && (d.getFirstChild()==null) ){
80 d.getParentNode().removeChild(d);
81 d = null;
82 }
83
84 if( d == null ){ count--; }
85 } }
86 if( count > cols ){ cols = count; }
87 } }
88 d = mtr;
89 mtr = mtr.getPreviousSibling();
90 if(
91 (d.getNodeType() == Node.TEXT_NODE)
92 && d.getNodeValue().trim().equals("")
93 ){
94 d.getParentNode().removeChild(d);
95 d = null;
96 }
97
98 if( d != null ){
99 if( (d.getNextSibling()==null)
100 && (d.getFirstChild()==null) ){
101 d.getParentNode().removeChild(d);
102 d = null;
103 }
104
105 } } }
106
107 if (node.hasChildNodes()) {
108 mtr = node.getFirstChild();
109 while( mtr != null){
110 if( mtr.getNodeType() == Node.ELEMENT_NODE ){
111 int count = 0;
112 if (mtr.hasChildNodes()) {
113 mtd = mtr.getFirstChild();
114 while( mtd != null){
115 if( mtd.getNodeType() == Node.ELEMENT_NODE ){
116 mtr.insertBefore( ((Document) dom).createTextNode("\n"), mtd );
117
118 count++;
119 }
120 mtd = mtd.getNextSibling();
121 }
122 }
123 if( count < cols ){
124 for(int i = count; i < cols; i++){
125 mtr.appendChild( ((Document) dom).createElement("math:mtd") );
126 }
127
128 } }
129 mtr = mtr.getNextSibling();
130 } }
131
132 }
133
134 public static void table(Node dom) {
135 Node tblRow, tblCell, d;
136 // int cols = 0;
137 Node node = dom.getFirstChild();
138
139 tblRow = node.getLastChild();
140 while( (tblRow != null)
141 // && (tblRow.getNodeType() == Node.TEXT_NODE)
142 // && tblRow.getNodeValue().trim().equals("")
143 && tblRow.getTextContent().trim().equals("")
144 ){
145 node.removeChild(tblRow);
146 tblRow = node.getLastChild();
147 }
148
149 if( (tblRow != null) && (tblRow.getPreviousSibling() != null) ){
150 boolean bool = false;
151 if( tblRow.getNodeName().equals("table:table-row")
152 && tblRow.hasAttributes()
153 ){
154 NamedNodeMap attributes = tblRow.getAttributes();
155 Node styleAttr = attributes.getNamedItem( "table:style-name" );
156 String style = (styleAttr==null)? null
157 : styleAttr.getNodeValue();
158 if( (style != null)
159 && ( style.equals("hline-row")
160 || style.equals("cline-row")
161 )
162 ){
163 bool = true;
164 } }
165
166 if( !bool ){
167 tblCell = tblRow.getFirstChild();
168 while( tblCell != null){
169 d = tblCell.getNextSibling();
170 if( justSpace(tblCell) ){ tblRow.removeChild(tblCell); }
171 tblCell = d;
172 }
173
174
175 tblCell = tblRow.getFirstChild();
176 if( (tblCell != null)
177 && (tblCell.getNextSibling() == null)
178 && justSpace(tblCell)
179 ){
180 node.removeChild(tblRow);
181 } } }
182 int n = 0;
183 tblRow = node.getFirstChild();
184 while( tblRow != null ){
185 if(
186 (tblRow.getNodeType() == Node.ELEMENT_NODE)
187 &&
188 tblRow.getNodeName().equals("table:table-row")
189 ){
190 int m = 0;
191 tblCell = tblRow.getFirstChild();
192 while( tblCell != null ){
193 if(
194 (tblCell.getNodeType() == Node.ELEMENT_NODE)
195 &&
196 tblCell.getNodeName().equals("table:table-cell")
197 ){
198 m++;
199 Node child = tblCell.getLastChild();
200 while( child != null ){
201 Node prevChild = child.getPreviousSibling();
202 if(
203 (child.getNodeType() == Node.ELEMENT_NODE)
204 &&
205 child.getNodeName().equals("text:p")
206 ){
207 Node sibling = child.getPreviousSibling();
208 while( (sibling != null)
209 &&
210 (sibling.getNodeType() != Node.ELEMENT_NODE)
211 ){
212 sibling = sibling.getPreviousSibling();
213 }
214 if( sibling == null ){
215 sibling = child.getNextSibling();
216 while( (sibling != null)
217 &&
218 (sibling.getNodeType() != Node.ELEMENT_NODE)
219 ){
220 sibling = sibling.getNextSibling();
221 } }
222 if( (sibling != null)
223 && child.getTextContent().trim().equals("")
224 ){
225 tblCell.removeChild(child);
226 } }
227 child = prevChild;
228 }
229
230 }
231 tblCell = tblCell.getNextSibling();
232 }
233
234 if( m > n ){ n = m; }
235 }
236 tblRow = tblRow.getNextSibling();
237 }
238
239 tblRow = node.getFirstChild();
240 while( tblRow != null ){
241 d = tblRow.getNextSibling();
242 if(
243 (tblRow.getNodeType() == Node.ELEMENT_NODE)
244 &&
245 tblRow.getNodeName().equals("table:table-column")
246 ){
247 n--;
248 if( n < 0 ){
249 tblRow.getParentNode().removeChild(tblRow);
250 } }
251 tblRow = d;
252 }
253
254 }
255
256 static boolean justSpace(Node node){
257 if( node == null ){ return true; }
258 if( node.getNodeType() == Node.TEXT_NODE ){
259 if( !node.getNodeValue().trim().equals("") ){ return false; }
260 } else {
261 if( node.getNodeType() == Node.ELEMENT_NODE ){
262 String nm = node.getNodeName();
263 if(
264 !nm.equals("table:table-cell")
265 && !nm.equals("text:p")
266 ){
267 return false;
268 } }
269 }
270 if(!justSpace( node.getNextSibling() )){ return false; }
271 if(!justSpace( node.getFirstChild() )){ return false; }
272 return true;
273 }
274
275 }
276