1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
|
// 2009-01-27-22:19
package xtpipes.util;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
import java.io.*;
import java.lang.reflect.*;
import java.util.HashMap;
import java.util.Stack;
import java.util.ArrayList;
import java.util.HashSet;
import xtpipes.XtpipesUni;
public class ScriptsManager extends DefaultHandler {
boolean inBody = false;
ArrayList<String> nsName = new ArrayList<String>(),
nsValue = new ArrayList<String>();
Stack<Integer> nsStack = new Stack<Integer>();
PrintWriter out = null, log = null;
HashMap<String,Object> scripts = null;
Method method = null;
boolean savemode=false;
String code="", match = null;
Stack<Object[]> stack = new Stack<Object[]>();
public ScriptsManager( PrintWriter out,
HashMap<String,Object> scripts,
Method method,
PrintWriter log, boolean trace ){
this.out = out;
this.log = (log==null)? new PrintWriter( System.err ) : log;
this.scripts = scripts;
this.method = method;
}
public void characters(char[] ch, int start, int length){
add( XtpipesUni.toUni(ch, start, length, "<>&") );
}
public void startElement(String ns, String sName,
String qName, Attributes atts) {
int top = nsName.size();
nsStack.push( new Integer(top) );
String key = (atts==null)?
null
: (qName + "::" + atts.getValue("class"));
boolean flag = (key != null) && scripts.containsKey(key);
if( !flag ){
key = qName;
flag = scripts.containsKey(key);
}
inBody = true;
String s = "<" + qName + "\n";
for(int i=0; i<atts.getLength(); i++ ){
String name = atts.getQName(i),
value = atts.getValue(i);
if( name.startsWith("xmlns") ){
if( (name.length() == 5) || (name.charAt(5) == ':') ){
boolean bool = false;
for(int k=nsName.size(); k>0; ){
k--;
if( ((String) nsName.get(k)) . equals(name) ){
bool = ((String) nsValue.get(k)) . equals(value);
break;
} }
if( !bool ){
nsName.add(name); nsValue.add(value);
} } }
s += " " + name + "=\"" +
XtpipesUni.toUni(value, "<>&\"") + "\"";
}
if( flag ){ HashSet<String> registry = new HashSet<String>();
for(int i=nsName.size(); i>top; ){
i--;
registry.add( (String) nsName.get(i) );
}
for(int i=top; i>0; ){
i--;
String nm = (String) nsName.get(i);
if( ! registry.contains(nm) ){
registry.add( nm );
s += " " + nm + "=\"" +
XtpipesUni.toUni( (String) nsValue.get(i), "<>&\"") + "\"";
} }
}
s += ">" ;
if( flag ){
Object [] state = { new Boolean(savemode), code, match };
stack.push( state );
savemode=true; code=""; match= key;
} else {
Object [] state = { new Boolean(savemode), null, null };
stack.push( state );
}
add( s );
}
public void endElement(String ns, String sName, String qName){
String s = "</" + qName + ">";
add( s );
Object [] state = (Object []) stack.pop();
if( (String) state[1] != null ){
Object parmValues[] = new Object[2];
parmValues[0] = scripts.get( match );
parmValues[1] = code;
try {
s = (String) method.invoke( null, parmValues );
} catch(java.lang.reflect.InvocationTargetException e){
log.println("--- ScriptsManager Error 1 --- " + e.getCause() );
log.flush();
} catch (Exception e){
log.println("--- ScriptsManager Error 2 --- " + e );
log.flush();
}
savemode = ((Boolean) state[0]).booleanValue();
code = (String) state[1];
match = (String) state[2];
int top = ((Integer) nsStack.pop()) . intValue();
for(int i=nsName.size(); i>top; ){
i--;
nsName.remove(i);
nsValue.remove(i);
}
if( !s.equals("") ){
int m = s.indexOf('>');
char [] attrs = s.substring(0,m).toCharArray();
int result = qName.length()+1,
mark = result,
from=-1,
control = 12
;
char delimiter = ' ';
String name="";
for(int i=result; i<m; i++ ){
attrs[result++] = attrs[i];
switch( control ){
case 12
: { if( attrs[i] == '=' ){
name = (new String(attrs,mark,result-mark-1)).trim();
control = 13
;
}
break; }
case 13
: { if( (attrs[i] == '"') || (attrs[i] == '\'') ){
delimiter = attrs[i];
control = 14
;
from = result;
}
break; }
case 14
: { if( attrs[i] == delimiter ){
if( name.startsWith("xmlns")
&& ((name.length() == 5) || (name.charAt(5) == ':')) ){
String value = (new String(attrs,from,result-from-1)).trim();
boolean bool = false;
for(int k=nsName.size(); k>0; ){
k--;
if( ((String) nsName.get(k)) . equals(name) ){
bool = ((String) nsValue.get(k)) . equals(value);
break;
} }
if( bool ){ result = mark; }
}
mark = result;
control = 12
;
}
break; }
} }
s = (new String(attrs,0, Math.min(result,attrs.length)))
+ s.substring(m);
add( s );
}
} else { int top = ((Integer) nsStack.pop()) . intValue();
for(int i=nsName.size(); i>top; ){
i--;
nsName.remove(i);
nsValue.remove(i);
}
}
}
protected void add(String s){
if( savemode ){ code+=s; }
else { out.print(s); }
} }
|