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