Return-Path: Mailing-List: contact tomcat-dev-help@jakarta.apache.org; run by ezmlm Delivered-To: mailing list tomcat-dev@jakarta.apache.org Received: (qmail 5177 invoked by uid 500); 1 Nov 2000 21:39:39 -0000 Delivered-To: apmail-jakarta-tomcat-cvs@apache.org Received: (qmail 5171 invoked by uid 1052); 1 Nov 2000 21:39:38 -0000 Date: 1 Nov 2000 21:39:38 -0000 Message-ID: <20001101213938.5170.qmail@locus.apache.org> From: costin@locus.apache.org To: jakarta-tomcat-cvs@apache.org Subject: cvs commit: jakarta-tomcat/src/share/org/apache/tomcat/util/xml SaxContext.java XmlMapper.java costin 00/11/01 13:39:38 Modified: src/share/org/apache/tomcat/util/xml SaxContext.java XmlMapper.java Log: Added few more helper methods to the xml mapper. - you can define "variables" ( a global Hashtable ) ( that will eliminate the need for intermediary objects ) - shortcuts for common accesses to current and previous object Revision Changes Path 1.2 +38 -7 jakarta-tomcat/src/share/org/apache/tomcat/util/xml/SaxContext.java Index: SaxContext.java =================================================================== RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/xml/SaxContext.java,v retrieving revision 1.1 retrieving revision 1.2 diff -u -r1.1 -r1.2 --- SaxContext.java 2000/02/08 20:13:32 1.1 +++ SaxContext.java 2000/11/01 21:39:35 1.2 @@ -16,32 +16,55 @@ // XXX this interface is not final, but a prototype. /** SAX Context - used to match and perform actions - * provide access to the current stack and XML elements. + * provide access to the current stack and XML elements. + * + * We maintain a stack with all elements and their attributes. + * We also support a stack of objects that can be used as in a + * stack-based programming language. * * @author costin@dnt.ro */ public interface SaxContext { - // -------------------- Access to parsing context + // -------------------- Access to the element stack + /** Body of the last tag. + */ + public String getBody(); + + /** Attributes of the current tag + */ + public AttributeList getCurrentAttributes(); + + /** Current element + */ + public String getCurrentElement(); + + /** Depth of the tag stack. + * XXX getElementDepth() ? */ public int getTagCount(); - /** Access attributes of a particular tag + /** Random access to attributes of a particular element. */ public AttributeList getAttributeList( int pos ); - /** Access a particular tag + /** Random Access a particular parent element + * XXX getElement() is a better name */ public String getTag( int pos ); - /** Body of the last tag - */ - public String getBody(); + // -------------------- Object stack + public void pushObject(Object o); + public Object popObject(); + + public Object currentObject(); + public Object previousObject(); + /** The root object is either set by caller before starting the parse or can be created using the first tag. It is used to set object in @@ -54,6 +77,7 @@ as result of parsing. You can either use the stack ( which is very powerfull construct !), or use the root object and navigation in the result tree. + @deprecated */ public Stack getObjectStack(); @@ -62,4 +86,11 @@ public int getDebug(); public void log( String s ); + + public XmlMapper getMapper(); + + // -------------------- Variables -------------------- + public void setVariable( String s, Object v ); + + public Object getVariable( String s ); } 1.28 +142 -68 jakarta-tomcat/src/share/org/apache/tomcat/util/xml/XmlMapper.java Index: XmlMapper.java =================================================================== RCS file: /home/cvs/jakarta-tomcat/src/share/org/apache/tomcat/util/xml/XmlMapper.java,v retrieving revision 1.27 retrieving revision 1.28 diff -u -r1.27 -r1.28 --- XmlMapper.java 2000/08/10 22:39:24 1.27 +++ XmlMapper.java 2000/11/01 21:39:35 1.28 @@ -32,7 +32,8 @@ */ private Hashtable fileDTDs = new Hashtable(); private Hashtable resDTDs = new Hashtable(); - + private Hashtable variables = new Hashtable(); + // Stack of elements Stack oStack=new Stack(); Object root; @@ -144,6 +145,10 @@ return (AttributeList)attributeStack[pos]; } + public AttributeList getCurrentAttributes() { + return (AttributeList)attributeStack[sp-1]; + } + public int getTagCount() { return sp; } @@ -152,6 +157,10 @@ return tagStack[pos]; } + public String getCurrentElement() { + return tagStack[sp-1]; + } + public String getBody() { return body; } @@ -160,6 +169,25 @@ return oStack; } + public Object popObject() { + return oStack.pop(); + } + + public Object currentObject() { + return oStack.peek(); + } + + public Object previousObject() { + Object o=oStack.pop(); + Object result=oStack.peek(); + oStack.push( o ); + return result; + } + + public void pushObject(Object o) { + oStack.push( o ); + } + public Object getRoot() { return root; } @@ -195,15 +223,29 @@ System.out.println("XmlMapper: " + msg); } + public void setVariable( String name, Object value ) { + if( value==null) + variables.remove(name); + else + variables.put( name, value ); + } + + public Object getVariable( String name ) { + return variables.get( name ); + } + + public XmlMapper getMapper() { + return this; + } + /** read an XML file, construct and return the object hierarchy */ public Object readXml(File xmlFile, Object root) throws Exception { if(root!=null) { - Stack st=this.getObjectStack(); this.root=root; - st.push( root ); + this.pushObject( root ); } try { SAXParser parser=null; @@ -240,9 +282,8 @@ throws Exception { if(root!=null) { - Stack st=this.getObjectStack(); this.root=root; - st.push( root ); + this.pushObject( root ); } SAXParser parser=null; try { @@ -311,8 +352,8 @@ addRule( "xmlmapper:debug", new XmlAction() { public void start(SaxContext ctx) { - int top=ctx.getTagCount()-1; - AttributeList attributes = ctx.getAttributeList( top ); + AttributeList attributes = + ctx.getCurrentAttributes(); String levelS=attributes.getValue("level"); XmlMapper mapper=(XmlMapper)ctx; if( levelS!=null) @@ -325,8 +366,8 @@ new XmlAction() { public void start(SaxContext ctx) { XmlMapper mapper=(XmlMapper)ctx; - int top=ctx.getTagCount()-1; - AttributeList attributes = ctx.getAttributeList( top ); + AttributeList attributes = + ctx.getCurrentAttributes(); String match=attributes.getValue("match"); if(match==null) return; //log String obj=attributes.getValue("object-create"); @@ -480,6 +521,12 @@ return new SetProperties(); } + /** Set a variable varName using the value of an attribute + */ + public XmlAction setVariable( String varName, String attName ) { + return new SetVariable( varName, attName ); + } + /** For the last 2 objects in stack, create a parent-child * and child.childM with parente as parameter */ @@ -573,27 +620,26 @@ } public void start( SaxContext ctx) throws Exception { - Stack st=ctx.getObjectStack(); - int top=ctx.getTagCount()-1; - String tag=ctx.getTag(top); + String tag=ctx.getCurrentElement(); String classN=className; if( attrib!=null) { - AttributeList attributes = ctx.getAttributeList( top ); + AttributeList attributes = ctx.getCurrentAttributes(); if (attributes.getValue(attrib) != null) classN= attributes.getValue(attrib); } Class c=Class.forName( classN ); Object o=c.newInstance(); - st.push(o); - if( ctx.getDebug() > 0 ) ctx.log("new " + attrib + " " + classN + " " + tag + " " + o); + ctx.pushObject(o); + if( ctx.getDebug() > 0 ) + ctx.log("new " + attrib + " " + classN + " " + tag + " " + o); } public void cleanup( SaxContext ctx) { - Stack st=ctx.getObjectStack(); - String tag=ctx.getTag(ctx.getTagCount()-1); - Object o=st.pop(); - if( ctx.getDebug() > 0 ) ctx.log("pop " + tag + " " + o.getClass().getName() + ": " + o); + String tag=ctx.getCurrentElement(); + Object o=ctx.popObject(); + if( ctx.getDebug() > 0 ) ctx.log("pop " + tag + " " + + o.getClass().getName() + ": " + o); } } @@ -601,16 +647,13 @@ /** Set object properties using XML attribute list */ class SetProperties extends XmlAction { - // static Class paramT[]=new Class[] { "String".getClass() }; public SetProperties() { } public void start( SaxContext ctx ) { - Stack st=ctx.getObjectStack(); - Object elem=st.peek(); - int top=ctx.getTagCount()-1; - AttributeList attributes = ctx.getAttributeList( top ); + Object elem=ctx.currentObject(); + AttributeList attributes = ctx.getCurrentAttributes(); for (int i = 0; i < attributes.getLength (); i++) { String type = attributes.getType (i); @@ -623,11 +666,14 @@ } /** Find a method with the right name - If found, call the method ( if param is int or boolean we'll convert value to - the right type before) - that means you can have setDebug(1). + If found, call the method ( if param is int or boolean we'll convert + value to the right type before) - that means you can have setDebug(1). */ - static void setProperty( SaxContext ctx, Object o, String name, String value ) { - if( ctx.getDebug() > 1 ) ctx.log("setProperty(" + o.getClass() + " " + name + "=" + value +")" ); + static void setProperty( SaxContext ctx, Object o, String name, + String value ) { + if( ctx.getDebug() > 1 ) ctx.log("setProperty(" + + o.getClass() + " " + name + "=" + + value +")" ); String setter= "set" +capitalize(name); @@ -661,7 +707,8 @@ try { params[0]=new Integer(value); } catch( NumberFormatException ex ) {ok=false;} - } else if ("java.lang.Boolean".equals( paramType.getName()) || + } else if ("java.lang.Boolean". + equals( paramType.getName()) || "boolean".equals( paramType.getName())) { params[0]=new Boolean(value); } else { @@ -669,7 +716,6 @@ } if( ok ) { - // System.out.println("XXX: " + methods[i] + " " + o + " " + params[0] ); methods[i].invoke( o, params ); return; } @@ -690,13 +736,19 @@ } } catch( SecurityException ex1 ) { - if( ctx.getDebug() > 0 ) ctx.log("SecurityException for " + o.getClass() + " " + name + "=" + value +")" ); + if( ctx.getDebug() > 0 ) + ctx.log("SecurityException for " + o.getClass() + " " + + name + "=" + value +")" ); if( ctx.getDebug() > 1 ) ex1.printStackTrace(); } catch (IllegalAccessException iae) { - if( ctx.getDebug() > 0 ) ctx.log("IllegalAccessException for " + o.getClass() + " " + name + "=" + value +")" ); + if( ctx.getDebug() > 0 ) + ctx.log("IllegalAccessException for " + + o.getClass() + " " + name + "=" + value +")" ); if( ctx.getDebug() > 1 ) iae.printStackTrace(); } catch (InvocationTargetException ie) { - if( ctx.getDebug() > 0 ) ctx.log("InvocationTargetException for " + o.getClass() + " " + name + "=" + value +")" ); + if( ctx.getDebug() > 0 ) + ctx.log("InvocationTargetException for " + o.getClass() + + " " + name + "=" + value +")" ); if( ctx.getDebug() > 1 ) ie.printStackTrace(); } } @@ -729,15 +781,13 @@ } public void end( SaxContext ctx) throws Exception { - Stack st=ctx.getObjectStack(); - - Object obj=st.pop(); - Object parent=st.peek(); - st.push( obj ); // put it back + Object obj=ctx.currentObject(); + Object parent=ctx.previousObject(); String parentC=parent.getClass().getName(); - if( ctx.getDebug() > 0 ) ctx.log("Calling " + obj.getClass().getName() + "." + childM + - " " + parentC); + if( ctx.getDebug() > 0 ) + ctx.log("Calling " + obj.getClass().getName() + "." + childM + + " " + parentC); Class params[]=new Class[1]; if( paramT==null) { @@ -762,14 +812,12 @@ } public void end( SaxContext ctx) throws Exception { - Stack st=ctx.getObjectStack(); + Object obj=ctx.currentObject(); + Object parent=ctx.previousObject(); - Object obj=st.pop(); - Object parent=st.peek(); - st.push( obj ); // put it back - String parentC=parent.getClass().getName(); - if( ctx.getDebug() >0) ctx.log("Calling " + parentC + "." + parentM +" " + obj ); + if( ctx.getDebug() >0) + ctx.log("Calling " + parentC + "." + parentM +" " + obj ); Class params[]=new Class[1]; if( paramT==null) { @@ -784,7 +832,7 @@ /** */ -class MethodSetter extends XmlAction { +class MethodSetter extends XmlAction { String mName; int paramC; String paramTypes[]; @@ -801,24 +849,23 @@ } public void start( SaxContext ctx) { - Stack st=ctx.getObjectStack(); - if(paramC==0) return; String params[]=new String[paramC]; - st.push( params ); + ctx.pushObject( params ); } - static final Class STRING_CLASS="String".getClass(); // XXX is String.CLASS valid in 1.1 ? + static final Class STRING_CLASS="String".getClass(); public void end( SaxContext ctx) throws Exception { - Stack st=ctx.getObjectStack(); - String params[]=null; - if( paramC >0 ) params=(String []) st.pop(); - Object parent=st.peek(); + String params[]=(String [])ctx.popObject(); + Object parent=ctx.currentObject(); + // XXX ??? if( paramC == 0 ) { params=new String[1]; params[0]= ctx.getBody().trim(); - if( ctx.getDebug() > 0 ) ctx.log("" + parent.getClass().getName() + "." + mName + "( " + params[0] + ")"); + if( ctx.getDebug() > 0 ) + ctx.log("" + parent.getClass().getName() + "." + + mName + "( " + params[0] + ")"); } Class paramT[]=new Class[params.length]; @@ -843,7 +890,8 @@ try { m=parent.getClass().getMethod( mName, paramT ); } catch( NoSuchMethodException ex ) { - ctx.log("Can't find method " + mName + " in " + parent + " CLASS " + parent.getClass()); + ctx.log("Can't find method " + mName + " in " + + parent + " CLASS " + parent.getClass()); return; } m.invoke( parent, realParam ); @@ -876,20 +924,15 @@ // If param is an attrib, set it public void start( SaxContext ctx) { if( attrib==null) return; - - Stack st=ctx.getObjectStack(); - String h[]=(String[])st.peek(); - - int top=ctx.getTagCount()-1; - AttributeList attributes = ctx.getAttributeList( top ); + String h[]=(String[])ctx.currentObject(); + AttributeList attributes = ctx.getCurrentAttributes(); h[paramId]= attributes.getValue(attrib); } // If param is the body, set it public void end( SaxContext ctx) { if( attrib!=null) return; - Stack st=ctx.getObjectStack(); - String h[]=(String[])st.peek(); + String h[]=(String[])ctx.currentObject(); h[paramId]= ctx.getBody().trim(); } } @@ -902,8 +945,39 @@ } public void end( SaxContext ctx) { - Stack st=ctx.getObjectStack(); - Object top = st.pop(); - if( ctx.getDebug() > 0 ) ctx.log("Pop " + top.getClass().getName()); + Object top = ctx.popObject(); + if( ctx.getDebug() > 0 ) + ctx.log("Pop " + + ((top==null) ? "null" : + top.getClass().getName())); + } +} + +/** + */ +class SetVariable extends XmlAction { + String varName; + String attributeN; + + public SetVariable(String varName, String attributeN) { + super(); + this.varName=varName; + this.attributeN=attributeN; + } + + public void start( SaxContext ctx) throws Exception { + AttributeList attributes = ctx.getCurrentAttributes(); + ctx.setVariable( varName, + attributes.getValue(attributeN)); + if( ctx.getDebug() > 0 ) + ctx.log("setVariable " + varName + " " + attributeN + " " + + attributes.getValue( attributeN )); + } + + public void cleanup( SaxContext ctx) { + ctx.setVariable( varName, null); + if( ctx.getDebug() > 0 ) + ctx.log("setVariable " + varName + " " + attributeN + " " + + "null"); } }