You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@xalan.apache.org by zo...@apache.org on 2003/08/01 02:49:26 UTC

cvs commit: xml-xalan/java/src/org/apache/xalan/xsltc/compiler Stylesheet.java

zongaro     2003/07/31 17:49:26

  Modified:    java/src/org/apache/xalan/xsltc/compiler Stylesheet.java
  Log:
  Added code to create static char[] fields in the translet containing the
  literal text in the source stylesheet.  The code generation for xsl:comment and
  for literal text that constructs text nodes takes advantage of this by using
  the serializer's comment(char[],int,int) and characters(char[],int,int) methods,
  respectively, rather than the comment(String) and characters(String) methods.
  The former pair of methods avoid some potential overhead in the serializer from
  copying the contents of strings to char[] objects.
  
  Code that creates a static initializer method in a translet was written by
  Morris Kwan (mkwan@ca.ibm.com).
  
  Reviewed by Morris Kwan (mkwan@ca.ibm.com)
  
  Revision  Changes    Path
  1.52      +57 -1     xml-xalan/java/src/org/apache/xalan/xsltc/compiler/Stylesheet.java
  
  Index: Stylesheet.java
  ===================================================================
  RCS file: /home/cvs/xml-xalan/java/src/org/apache/xalan/xsltc/compiler/Stylesheet.java,v
  retrieving revision 1.51
  retrieving revision 1.52
  diff -u -r1.51 -r1.52
  --- Stylesheet.java	25 Jul 2003 20:41:25 -0000	1.51
  +++ Stylesheet.java	1 Aug 2003 00:49:26 -0000	1.52
  @@ -85,6 +85,7 @@
   import org.apache.bcel.generic.NEW;
   import org.apache.bcel.generic.PUSH;
   import org.apache.bcel.generic.PUTFIELD;
  +import org.apache.bcel.generic.PUTSTATIC;
   import org.apache.bcel.generic.TargetLostException;
   import org.apache.bcel.util.InstructionFinder;
   import org.apache.xalan.xsltc.compiler.util.ClassGenerator;
  @@ -559,6 +560,20 @@
       }
   
       /**
  +     * Add a static field
  +     */
  +    private void addStaticField(ClassGenerator classGen, String type,
  +                                String name)
  +    {
  +        final FieldGen fgen = new FieldGen(ACC_PROTECTED|ACC_STATIC,
  +                                           Util.getJCRefType(type),
  +                                           name,
  +                                           classGen.getConstantPool());
  +        classGen.addField(fgen.getField());
  +
  +    }
  +
  +    /**
        * Translate the stylesheet into JVM bytecodes. 
        */
       public void translate() {
  @@ -608,6 +623,7 @@
   	checkOutputMethod();
   	processModes();
   	compileModes(classGen);
  +        compileStaticInitializer(classGen);
   	compileConstructor(classGen, _lastOutputElement);
   
   	if (!getParser().errorsFound()) {
  @@ -615,6 +631,46 @@
   	}
       }
   
  +    /**
  +     * Create a static initializer for the translet which will contain
  +     * read-only that can be shared by all translet instances.
  +     */
  +    private void compileStaticInitializer(ClassGenerator classGen) {
  +        final ConstantPoolGen cpg = classGen.getConstantPool();
  +        final InstructionList il = new InstructionList();
  +
  +        final MethodGenerator staticConst =
  +            new MethodGenerator(ACC_PUBLIC|ACC_STATIC,
  +                                org.apache.bcel.generic.Type.VOID,
  +                                null, null, "<clinit>",
  +                                _className, il, cpg);
  +
  +        // Create fields of type char[] that will contain literal text from
  +        // the stylesheet.
  +        final int charDataFieldCount = getXSLTC().getCharacterDataCount();
  +        for (int i = 0; i < charDataFieldCount; i++) {
  +            addStaticField(classGen, STATIC_CHAR_DATA_FIELD_SIG,
  +                           STATIC_CHAR_DATA_FIELD+i);
  +        }
  +
  +        // Grab all the literal text in the stylesheet and put it in a char[]
  +        final int charDataCount = getXSLTC().getCharacterDataCount();
  +        final int toCharArray = cpg.addMethodref(STRING, "toCharArray", "()[C");
  +        for (int i = 0; i < charDataCount; i++) {
  +            il.append(new PUSH(cpg, getXSLTC().getCharacterData(i)));
  +            il.append(new INVOKEVIRTUAL(toCharArray));
  +            il.append(new PUTSTATIC(cpg.addFieldref(_className,
  +                                               STATIC_CHAR_DATA_FIELD+i,
  +                                               STATIC_CHAR_DATA_FIELD_SIG)));
  +        }
  +
  +        il.append(RETURN);
  +
  +        staticConst.stripAttributes(true);
  +        staticConst.setMaxLocals();
  +        staticConst.setMaxStack();
  +        classGen.addMethod(staticConst.getMethod());
  +    }
       /**
        * Compile the translet's constructor
        */
  
  
  

---------------------------------------------------------------------
To unsubscribe, e-mail: xalan-cvs-unsubscribe@xml.apache.org
For additional commands, e-mail: xalan-cvs-help@xml.apache.org