Debian Codegraph @

texk/tex4htk/java/HtSpk.java

raw
1 package tex4ht;
2 import org.w3c.dom.*;
3 public class HtSpk {
4 public static void fracLevel(Node dom) {
5 setFracLevel(dom.getFirstChild(), 0);
6 }
7 private static int setFracLevel(Node node, int cont) {
8 int level = 0;
9 String clName = null;
10 if (node.hasChildNodes()) {
11 if (node.hasAttributes()) {
12 Node cl = node.getAttributes().getNamedItem("class");
13 if (cl != null) { clName = cl.getNodeValue(); }
14 }
15 NodeList children = node.getChildNodes();
16 int max = 0;
17 for (int i = 0; i < children.getLength(); i++) {
18 Node child = children.item(i);
19 if (child.getNodeType() == Node.ELEMENT_NODE) {
20 int d = setFracLevel(child,
21 (clName != null) &&
22 clName.equals("continuous-mfrac")?
23 2 :
24 ((clName != null) &&
25 clName.equals("continuous-mfrac")?
26 (cont-1) : cont)
27 );
28 if (d > max) { max = d; }
29 } }
30 level += max;
31
32 if( clName != null ){
33 if( clName.equals("msub") || clName.equals("msup") ||
34 clName.equals("msubsup")
35 ) {
36 return 0;
37 }
38
39 if( clName.equals("continuous-mfrac") ) {
40 if( cont > 0 ){
41 Node child = node.getLastChild();
42 if (child.getNodeType() == Node.ELEMENT_NODE) {
43 Node cls = child.getAttributes() .getNamedItem("class");
44 if (cls != null) {
45 String clsName = cls.getNodeValue();
46 if ( clsName.equals("begin-end")) {
47 node.removeChild( child );
48 } } }
49
50 } else { for (int i = 0; i < children.getLength(); i++) {
51 Node child = children.item(i);
52 if (child.getNodeType() == Node.ELEMENT_NODE) {
53 Node cls = child.getAttributes()
54 .getNamedItem("class");
55 if (cls != null) {
56 String clsName = cls.getNodeValue();
57 if ( clsName.equals("begin-end")) {
58 child = child.getFirstChild();
59 String s = child.getNodeValue();
60 s = s.replaceFirst("begin", "begin continued");
61 s = s.replaceFirst("end", "end continued");
62 ((org.w3c.dom.Text) child).setData(s);
63 } } } }
64 }
65 return 0;
66 }
67
68 if (clName.equals("mfrac")) {
69 if( cont > 0 ){
70 Node child = node.getLastChild();
71 if (child.getNodeType() == Node.ELEMENT_NODE) {
72 Node cls = child.getAttributes() .getNamedItem("class");
73 if (cls != null) {
74 String clsName = cls.getNodeValue();
75 if ( clsName.equals("begin-end")) {
76 node.removeChild( child );
77 } } }
78
79 } else if( level > 0 ){
80 for (int i = 0; i < children.getLength(); i++) {
81 Node child = children.item(i);
82 if (child.getNodeType() == Node.ELEMENT_NODE) {
83 Node cls = child.getAttributes()
84 .getNamedItem("class");
85 if (cls != null) {
86 String clsName = cls.getNodeValue();
87 if (clsName.equals("begin-end")) {
88 child = child.getFirstChild();
89 String s = child.getNodeValue();
90 String bg = "", ov = "", en = "";
91 for(int j=0; j<level; j++){
92 bg += " begin "; ov += " over "; en += " end ";
93 }
94 s = s.replaceFirst("begin", bg + "begin");
95 s = s.replaceFirst("over", ov + "over");
96 s = s.replaceFirst("end", en + "end");
97 ((org.w3c.dom.Text) child).setData(s);
98 } } } }
99
100 }
101 level++;
102 } } }
103 return level;
104 }
105
106 public static void scriptLevel(Node dom) {
107 setScriptLevel(dom.getFirstChild(), "");
108 }
109 private static void setScriptLevel(Node node, String prefix) {
110 String clName = null;
111 if (node.hasChildNodes()) {
112 if (node.hasAttributes()) {
113 Node cl = node.getAttributes().getNamedItem("class");
114 if (cl != null) {
115 clName = cl.getNodeValue();
116 if( clName.equals("mrow-sub")
117 ||
118 clName.equals("mrow-super")
119 ){
120 if( !prefix.equals("") ){
121 Node child = node.getFirstChild();
122 if( (child.getNodeType() == Node.ELEMENT_NODE)
123 &&
124 child.hasAttributes()
125 ){
126 Node cls = child.getAttributes().getNamedItem("class");
127 if (cls != null) {
128 String clsName = cls.getNodeValue();
129 if ( clsName.equals("begin-script")
130 ||
131 clsName.equals("mid-script")
132 ) {
133 child = child.getFirstChild();
134 String s = child.getNodeValue();
135 ((org.w3c.dom.Text) child).setData( prefix + s );
136 } } }
137
138 }
139 if( clName.equals( "mrow-sub" ) ){ prefix += " sub "; }
140 else
141 if( clName.equals( "mrow-super" ) ){ prefix += " super "; }
142
143 } else if(
144 clName.equals("msqrt")
145 ){ prefix = ""; }
146 } }
147 NodeList children = node.getChildNodes();
148 for (int i = 0; i < children.getLength(); i++) {
149 Node child = children.item(i);
150 if (child.getNodeType() == Node.ELEMENT_NODE) {
151 setScriptLevel(child, prefix);
152 } }
153
154 } }
155
156 public static void rootLevel(Node dom) {
157 setRootLevel(dom.getFirstChild());
158 }
159 private static int setRootLevel( Node node ){
160 int level = 0;
161 String clName = null;
162 if (node.hasChildNodes()) {
163 if (node.hasAttributes()) {
164 Node cl = node.getAttributes().getNamedItem("class");
165 if (cl != null) { clName = cl.getNodeValue(); }
166 }
167 NodeList children = node.getChildNodes();
168 int max = 0;
169 for (int i = 0; i < children.getLength(); i++) {
170 Node child = children.item(i);
171 if (child.getNodeType() == Node.ELEMENT_NODE) {
172 int d = setRootLevel(child);
173 if( d > max ){ max = d; }
174 } }
175 level += max;
176
177 if( clName != null ){
178 if( clName.equals("msub") || clName.equals("msup") ||
179 clName.equals("msubsup")
180 ) {
181 return 0;
182 }
183
184 if( clName.equals("msqrt") || clName.equals("root") ){
185 for (int i = 0; i < children.getLength(); i++) {
186 Node child = children.item(i);
187 if (child.getNodeType() == Node.ELEMENT_NODE) {
188 Node cls = child.getAttributes()
189 .getNamedItem("class");
190 if (cls != null) {
191 String clsName = cls.getNodeValue();
192 if( clsName.equals("begin-root")
193 || clsName.equals("mid-root")
194 || clsName.equals("end-root")
195 ){
196 child = child.getFirstChild();
197 String s = child.getNodeValue();
198 String nested = "";
199 for(int j=0; j<level; j++){
200 nested += " nested ";
201 }
202 ((org.w3c.dom.Text) child).setData( nested + s);
203 } } } }
204
205 level++;
206 } } }
207 return level;
208 }
209
210 }
211