📄 xmlmapper.java
字号:
// -------------------- Factories for "common" actions -------------------- // XXX Probably it's better to use the real XmlActions, with new FooAction() /** Create an object using for a matching tag with the given class name */ public XmlAction objectCreate( String classN ) { return new ObjectCreate( classN); } /** Create an object using an attribute value as the class name If no attribute use classN as a default. */ public XmlAction objectCreate( String classN, String attrib ) { return new ObjectCreate( classN, attrib); } /** Set object properties using XML attributes */ public XmlAction setProperties( ) { return new SetProperties(); } /** For the last 2 objects in stack, create a parent-child * and child.childM with parente as parameter */ public XmlAction setParent( String childM ) { return new SetParent( childM ); } /** For the last 2 objects in stack, create a parent-child * and child.childM with parent as parameter */ public XmlAction setParent( String childM, String argType ) { return new SetParent( childM, argType ); } /** For the last 2 objects in stack, create a parent-child * relation by invokeing parent.parentM with the child as parameter * ArgType is the parameter expected by addParent ( null use the current object * type) */ public XmlAction addChild( String parentM, String argType ) { return new AddChild( parentM, argType ); } /** If a tag matches, invoke a method on the current object. Parameters can be extracted from sub-elements of the current tag. */ public XmlAction methodSetter(String method, int paramC) { return new MethodSetter(method, paramC); } /** If a tag matches, invoke a method on the current object. Parameters can be extracted from sub-elements of the current tag. */ public XmlAction methodSetter(String method, int paramC, String paramTypes[]) { return new MethodSetter(method, paramC, paramTypes); } /** Extract the method param from the body of the tag */ public XmlAction methodParam(int ord) { return new MethodParam(ord, null); // use body as value } /** Extract the method param from a tag attribute */ public XmlAction methodParam(int ord, String attrib) { return new MethodParam(ord, attrib); } /** Pop the object stack */ public XmlAction popStack() { return new PopStack(); }}//-------------------- "Core" actions --------------------// XXX XXX XXX Need to move the "standard" actions in individual files/** * Create an object of the specified or override Java class name. */class ObjectCreate extends XmlAction { String className; String attrib; /** * Create an object of the specified class name. * * @param classN Fully qualified name of the Java class to instantiate */ public ObjectCreate(String classN) { className=classN; } /** * Create an object of the specified default class name, unless an * attribute with the specified name is present, in which case the value * of this attribute overrides the default class name. * * @param classN Fully qualified name of the Java class to instantiate * if the specified attribute name is not present * @param attrib Name of the attribute that may contain a fully qualified * name of a Java class that overrides the default */ public ObjectCreate(String classN, String attrib) { className=classN; this.attrib=attrib; } public void start( SaxContext ctx) throws Exception { Stack st=ctx.getObjectStack(); int top=ctx.getTagCount()-1; String tag=ctx.getTag(top); String classN=className; if( attrib!=null) { AttributeList attributes = ctx.getAttributeList( top ); 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); } 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); }}/** 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 ); for (int i = 0; i < attributes.getLength (); i++) { String type = attributes.getType (i); String name=attributes.getName(i); String value=attributes.getValue(i); setProperty( ctx, elem, name, value ); } } /** 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). */ 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); try { Method methods[]=o.getClass().getMethods(); Method setPropertyMethod=null; // First, the ideal case - a setFoo( String ) method for( int i=0; i< methods.length; i++ ) { Class paramT[]=methods[i].getParameterTypes(); if( setter.equals( methods[i].getName() ) && paramT.length == 1 && "java.lang.String".equals( paramT[0].getName())) { methods[i].invoke( o, new Object[] { value } ); return; } } // Try a setFoo ( int ) or ( boolean ) for( int i=0; i< methods.length; i++ ) { boolean ok=true; if( setter.equals( methods[i].getName() ) && methods[i].getParameterTypes().length == 1) { // match - find the type and invoke it Class paramType=methods[i].getParameterTypes()[0]; Object params[]=new Object[1]; if ("java.lang.Integer".equals( paramType.getName()) || "int".equals( paramType.getName())) { try { params[0]=new Integer(value); } catch( NumberFormatException ex ) {ok=false;} } else if ("java.lang.Boolean".equals( paramType.getName()) || "boolean".equals( paramType.getName())) { params[0]=new Boolean(value); } else { ctx.log("Unknown type " + paramType.getName() ); } if( ok ) { // System.out.println("XXX: " + methods[i] + " " + o + " " + params[0] ); methods[i].invoke( o, params ); return; } } // save "setProperty" for later if( "setProperty".equals( methods[i].getName())) { setPropertyMethod=methods[i]; } } // Ok, no setXXX found, try a setProperty("name", "value") if( setPropertyMethod != null ) { Object params[]=new Object[2]; params[0]=name; params[1]=value; setPropertyMethod.invoke( o, params ); } } catch( SecurityException ex1 ) { 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() > 1 ) iae.printStackTrace(); } catch (InvocationTargetException ie) { if( ctx.getDebug() > 0 ) ctx.log("InvocationTargetException for " + o.getClass() + " " + name + "=" + value +")" ); if( ctx.getDebug() > 1 ) ie.printStackTrace(); } } /** Reverse of Introspector.decapitalize */ static String capitalize(String name) { if (name == null || name.length() == 0) { return name; } char chars[] = name.toCharArray(); chars[0] = Character.toUpperCase(chars[0]); return new String(chars); }}/** Set parent */class SetParent extends XmlAction { String childM; String paramT = null; public SetParent(String p) { childM=p; } public SetParent(String p, String c) { childM=p; paramT=c; } 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 String parentC=parent.getClass().getName(); if( ctx.getDebug() > 0 ) ctx.log("Calling " + obj.getClass().getName() + "." + childM + " " + parentC); Class params[]=new Class[1]; if( paramT==null) { params[0]=parent.getClass(); } else { params[0]=Class.forName( paramT ); } Method m=obj.getClass().getMethod( childM, params ); m.invoke(obj, new Object[] { parent } ); }}/** Set parent */class AddChild extends XmlAction { String parentM; String paramT; public AddChild(String p, String c) { parentM=p; paramT=c; } 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 String parentC=parent.getClass().getName(); if( ctx.getDebug() >0) ctx.log("Calling " + parentC + "." + parentM +" " + obj ); Class params[]=new Class[1]; if( paramT==null) { params[0]=obj.getClass(); } else { params[0]=Class.forName( paramT ); } Method m=parent.getClass().getMethod( parentM, params ); m.invoke(parent, new Object[] { obj } ); }}/** */class MethodSetter extends XmlAction { String mName; int paramC; String paramTypes[]; public MethodSetter( String mName, int paramC) { this.mName=mName; this.paramC=paramC; } public MethodSetter( String mName, int paramC, String paramTypes[]) { this.mName=mName; this.paramC=paramC; this.paramTypes=paramTypes; } public void start( SaxContext ctx) { Stack st=ctx.getObjectStack(); if(paramC==0) return; String params[]=new String[paramC]; st.push( params ); } static final Class STRING_CLASS="String".getClass(); // XXX is String.CLASS valid in 1.1 ? 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(); if( paramC == 0 ) { params=new String[1]; params[0]= ctx.getBody().trim(); if( ctx.getDebug() > 0 ) ctx.log("" + parent.getClass().getName() + "." + mName + "( " + params[0] + ")"); } Class paramT[]=new Class[params.length]; Object realParam[]=new Object[params.length]; for (int i=0; i< params.length; i++ ) { if( paramTypes==null) { realParam[i]=params[i]; paramT[i]=STRING_CLASS; } else { // XXX Add more types if( "int".equals( paramTypes[i] ) ) { realParam[i]=new Integer( params[i] ); paramT[i]=int.class; } else { realParam[i]=params[i]; paramT[i]=STRING_CLASS; } } } Method m=null; try { m=parent.getClass().getMethod( mName, paramT ); } catch( NoSuchMethodException ex ) { ctx.log("Can't find method " + mName + " in " + parent + " CLASS " + parent.getClass()); return; } m.invoke( parent, realParam ); if(ctx.getDebug() > 0 ) { // debug StringBuffer sb=new StringBuffer(); sb.append("" + parent.getClass().getName() + "." + mName + "( " ); for(int i=0; i<paramC; i++ ) { if(i>0) sb.append( ", "); sb.append(params[i]); } sb.append(")"); if( ctx.getDebug() > 0 ) ctx.log(sb.toString()); } }}/** */class MethodParam extends XmlAction { int paramId; String attrib; public MethodParam( int paramId, String attrib) { this.paramId=paramId; this.attrib=attrib; } // 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 ); 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(); h[paramId]= ctx.getBody().trim(); }}/** */class PopStack extends XmlAction { public PopStack() { super(); } public void end( SaxContext ctx) { Stack st=ctx.getObjectStack(); Object top = st.pop(); if( ctx.getDebug() > 0 ) ctx.log("Pop " + top.getClass().getName()); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -