Debian Codegraph @

texk/tex4htk/java/DbUtilities.java

raw
1 package tex4ht;
2 /**********************************************************/
3 /* DbUtilities.java 2008-11-14-02:41 */
4 /* Copyright (C) 2008 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 DbUtilities {
27 public static void cline(Node dom) {
28 Node row, entry, para, nextrow;
29 Node node = dom.getFirstChild();
30 if( node != null ){
31 row = node.getLastChild();
32 while( (row != null)
33 && ( (entry = row.getLastChild()) != null)
34 && ( (para = entry.getFirstChild()) != null)
35 && ( para.getNextSibling() == null)
36 && ( para.getFirstChild() == null)
37 ){
38 node.removeChild(row);
39 row = node.getLastChild();
40 }
41
42 row = node.getFirstChild();
43 while( row != null ){
44 if( (row.getNodeType() == Node.ELEMENT_NODE)
45 && ((Element) row).getAttribute("rowsep").equals("")
46 && !((Element) row).getAttribute("role" ).equals("cline")
47 && ((nextrow = row.getNextSibling()) != null)
48 && (nextrow.getNodeType() == Node.ELEMENT_NODE)
49 && ((Element) nextrow).getAttribute("role" ).equals("cline")
50 ){
51 boolean compatible = true;
52 Node entry1 = row.getFirstChild();
53 Node entry2 = nextrow.getFirstChild();
54 while( true ){
55 if( (entry1 == null) || (entry2 == null) ){ break; }
56 int range;
57 try{
58 range =
59 Integer.parseInt( ((Element) entry1).getAttribute("nameend") )
60 -
61 Integer.parseInt( ((Element) entry1).getAttribute("namest") )
62 +
63 1;
64 } catch( Exception e){ range = 1;}
65 if( range > 1 ){
66 String rowsep = ((Element) entry2).getAttribute("rowsep");
67 while( --range > 0 ){
68 entry2 = entry2.getNextSibling();
69 if( entry2 == null ){
70 compatible = false;
71 break;
72 }
73 String value = ((Element) entry2).getAttribute("rowsep");
74 if( !value.equals( rowsep ) ){
75 compatible = false;
76 break;
77 }
78 }
79 }
80 if( !compatible ){ break; }
81 entry1 = entry1.getNextSibling();
82 entry2 = entry2.getNextSibling();
83 }
84
85 if( compatible ){
86 entry1 = row.getFirstChild();
87 entry2 = nextrow.getFirstChild();
88 while( true ){
89 if( (entry1 == null) || (entry2 == null) ){ break; }
90 int range;
91 try{
92 range =
93 Integer.parseInt( ((Element) entry1).getAttribute("nameend") )
94 -
95 Integer.parseInt( ((Element) entry1).getAttribute("namest") )
96 +
97 1;
98 } catch( Exception e){ range = 1;}
99 ((Element) entry1).setAttribute(
100 "rowsep",
101 ((Element) entry2).getAttribute("rowsep") );
102 while( --range > 0 ){
103 entry2 = entry2.getNextSibling();
104 }
105 entry1 = entry1.getNextSibling();
106 entry2 = entry2.getNextSibling();
107 }
108
109 node.removeChild(nextrow);
110 }
111 }
112 row = row.getNextSibling();
113 }
114
115 }
116 }
117
118 public static void para(Node dom) {
119 Node pNode = dom.getFirstChild();
120 if( pNode.hasChildNodes() ){
121 Node child = pNode.getFirstChild();
122 if( child != null ){
123 if( (child.getNodeType() == Node.TEXT_NODE)
124 &&
125 ((Text) child).getWholeText().trim().equals("")
126 ){
127 pNode.removeChild( child );
128 } } }
129 if( pNode.hasChildNodes() ){
130 Node child = pNode.getLastChild();
131 if( child != null ){
132 if( (child.getNodeType() == Node.TEXT_NODE)
133 &&
134 ((Text) child).getWholeText().trim().equals("")
135 ){
136 pNode.removeChild( child );
137 } } }
138 if( pNode.hasChildNodes() ){
139 Node child = pNode.getFirstChild();
140 if( (child != null)
141 && (child.getNextSibling() == null)
142 && (child.getNodeType() == Node.TEXT_NODE)
143 ){
144 String txt = ((Text) child).getWholeText();
145 String trm = txt.trim();
146 if( !trm.equals(txt) ){
147 ((Text)child).replaceWholeText(trm);
148 } } }
149 }
150
151 }
152