// 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 nsName = new ArrayList(), nsValue = new ArrayList(); Stack nsStack = new Stack(); PrintWriter out = null, log = null; HashMap scripts = null; Method method = null; boolean savemode=false; String code="", match = null; Stack stack = new Stack(); public ScriptsManager( PrintWriter out, HashMap 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; i0; ){ 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 registry = new HashSet(); 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 = ""; 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; i0; ){ 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); } } }