reinhard    2003/09/12 11:26:54

  Modified:    
src/blocks/scratchpad/java/org/apache/cocoon/components/flow/javascript/fom
                        JavaScriptAspectWeaver.java
  Log:
  - serialize result script in the directory of the base script
  
  Revision  Changes    Path
  1.5       +51 -13    
cocoon-2.1/src/blocks/scratchpad/java/org/apache/cocoon/components/flow/javascript/fom/JavaScriptAspectWeaver.java
  
  Index: JavaScriptAspectWeaver.java
  ===================================================================
  RCS file: 
/home/cvs/cocoon-2.1/src/blocks/scratchpad/java/org/apache/cocoon/components/flow/javascript/fom/JavaScriptAspectWeaver.java,v
  retrieving revision 1.4
  retrieving revision 1.5
  diff -u -r1.4 -r1.5
  --- JavaScriptAspectWeaver.java       12 Sep 2003 14:46:58 -0000      1.4
  +++ JavaScriptAspectWeaver.java       12 Sep 2003 18:26:54 -0000      1.5
  @@ -50,7 +50,9 @@
   package org.apache.cocoon.components.flow.javascript.fom;
   
   import java.io.BufferedReader;
  +import java.io.ByteArrayOutputStream;
   import java.io.CharArrayReader;
  +import java.io.FileOutputStream;
   import java.io.IOException;
   import java.io.InputStream;
   import java.io.InputStreamReader;
  @@ -123,7 +125,10 @@
       Environment environement = null;
       
       /** If debugging is true, the intercepted script is writen to filesystem 
*/
  -    boolean debug = false;
  +    boolean copyResultScript = false;
  +
  +    /** Base script <code>org.apache.excalibur.source.Source</code>*/
  +    Source source = null; 
       
       /** The javascript repsented in <code>JSToken</code>*/
       JSTokenList baseScriptTokenList = null; 
  @@ -141,6 +146,8 @@
        */
       public void setBaseScript( Source source ) throws IOException {
           
  +        this.source = source;
  +        
           // create tokens for javascript code
           this.baseScriptTokenList = JSParser.parse( 
               readSourceIntoCharArray( source.getInputStream() ) );
  @@ -159,18 +166,17 @@
        * Get the intercepted base script (all interceptions found in 
        * cocoon.apply(..) are added to the script.
        */
  -    public Reader getInterceptedScriptAsReader() {
  +    public Reader getInterceptedScriptAsReader() throws Exception {
           
           // comment out all cocoon.apply(..) parts
           this.baseScriptTokenList.commentScriptsApplied();
          
  -        // add interception events
  -        // add events to make parsing easier
  +        // add interception events to make parsing easier
           this.baseScriptTokenList.addInterceptionEvents( 
this.stopExecutionFunctions );        
          
           // replace return statements with variable defintions and put
           // it to the end of the function
  -        this.baseScriptTokenList.replaceReturn();
  +        this.baseScriptTokenList.replaceReturnStatements();
   
           // add the interceptions
           this.baseScriptTokenList.addInterceptions( this.interceptorGroups );
  @@ -181,9 +187,11 @@
           // logging
           this.getLogger().info( "\n\n" + this.baseScriptTokenList.toString() 
+ "\n" );
           
  -        // create a file at filesystem
  -        // TODO tbd   
  -        
  +        // create a file to make debugging easier
  +        if( copyResultScript ) {
  +            this.baseScriptTokenList.writeToFile( this.source );            
  +        }
  +
           // return the intercepted script        
           return this.baseScriptTokenList.getScriptAsReader();       
       }
  @@ -217,6 +225,14 @@
       public void setEnvironment( Environment env ) {
           this.environement = env;
       }
  +
  +    /**
  +     * Should the JavaScriptAspectWeaver write the result script
  +     * into separate file in the same directory as the basescript?
  +     */
  +    public void setCopyResultScript( boolean copy ) {
  +        this.copyResultScript = copy;
  +    }
       
       /** 
        * Provide configuration (part of the Interpreter configuration) 
  @@ -638,6 +654,24 @@
           }
           
           /**
  +         * @param string
  +         */
  +        public void writeToFile( Source source ) throws Exception {
  +            if( source.getScheme().equals( "file" ) ) {
  +                String filename = 
source.getURI().substring("file:/".length() ) + 
  +                    AO_FOM_JavaScriptInterpreter.INTERCEPTION_POSTFIX;
  +                FileOutputStream fos = new FileOutputStream( filename );
  +                ByteArrayOutputStream bs = new ByteArrayOutputStream();
  +                char completeChar[] = this.getScriptAsCharArray();
  +                for( int i = 0; i < completeChar.length; i++ ) {
  +                    bs.write( completeChar[i] );
  +                }
  +                fos.write( bs.toByteArray() );
  +                fos.close();
  +             }            
  +        }
  +
  +        /**
            * Add all tokens in the correct order 
            * 
            * @param interceptionsList - sorted list of all available 
interceptions
  @@ -937,7 +971,7 @@
            * Replace all occurencies of return (except those which
            * are in separte code blocks)
            */
  -        private void replaceReturn() {
  +        private void replaceReturnStatements() {
               ListIterator li = this.listIterator();
               int countOpenBrackets = 0;
               boolean inFunction = false;
  @@ -1108,7 +1142,11 @@
           /**
            * return the tokens as Reader
            */
  -        public Reader getScriptAsReader() {
  +        public Reader getScriptAsReader() {       
  +            return (Reader) new CharArrayReader( getScriptAsCharArray() );   
 
  +        }
  +
  +        public char[] getScriptAsCharArray() {
               ListIterator li = this.listIterator();
               char charComplete[] = new char[0];
               while( li.hasNext() ) {
  @@ -1119,8 +1157,8 @@
                   System.arraycopy( theChar, 0, copy, charComplete.length, 
theChar.length );      
                   charComplete = copy;
               }           
  -            return (Reader) new CharArrayReader(charComplete);    
  -        }
  +            return charComplete;    
  +        }        
           
           public String toString() {
               StringBuffer sb = new StringBuffer();
  
  
  

Reply via email to