View Javadoc

1   package org.codehaus.mojo.javascript.compress;
2   
3   /*
4    * Copyright 2001-2005 The Apache Software Foundation.
5    *
6    * Licensed under the Apache License, Version 2.0 (the "License");
7    * you may not use this file except in compliance with the License.
8    * You may obtain a copy of the License at
9    *
10   *      http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing, software
13   * distributed under the License is distributed on an "AS IS" BASIS,
14   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15   * See the License for the specific language governing permissions and
16   * limitations under the License.
17   */
18  
19  import java.io.File;
20  import java.io.FileReader;
21  import java.io.FileWriter;
22  
23  import org.codehaus.plexus.util.IOUtil;
24  import org.dojotoolkit.shrinksafe.Compressor;
25  import org.mozilla.javascript.Context;
26  import org.mozilla.javascript.tools.shell.ShellContextFactory;
27  
28  /**
29   * A JS compressor that uses Dojo modified Rhino engine to compress the script.
30   * The resulting compressed-js is garanteed to be functionaly equivalent as this
31   * is the internal view of the rhino context.
32   * 
33   * @author <a href="mailto:nicolas@apache.org">nicolas De Loof</a>
34   */
35  public class ShrinksafeCompressor
36      implements JSCompressor
37  {
38      private ErrorReporter4Mojo log = null;
39  
40      /**
41       * Set a the JSCompressorLogger implementation that will receive logs
42       *
43       * @param logger a logger
44       */
45      public void setLogger(JSCompressorLogger logger) throws CompressionException
46      {
47          log = new ErrorReporter4Mojo(logger,true);
48      }
49  
50      /**
51       * Return current JSCompressorLogger used for logging
52       *
53       * @return the current JSCompressorLogger used for logging
54       */
55      public JSCompressorLogger getLogger() throws CompressionException
56      {
57          return log.getLogger();
58      }
59  
60      /**
61       * {@inheritDoc}
62       * 
63       * @see org.codehaus.mojo.javascript.compress.JSCompressor#compress(java.io.File,
64       * java.io.File, int, int)
65       */
66      public void compress( final File input, File compressed, int level, int language )
67          throws CompressionException
68      {
69          final ShellContextFactory shellContextFactory = new ShellContextFactory();
70          shellContextFactory.setLanguageVersion( language );
71          shellContextFactory.setOptimizationLevel( level );
72          if( log != null ) {
73              shellContextFactory.setErrorReporter( log );
74          }
75          shellContextFactory.setStrictMode( true );
76  
77          Context cx = shellContextFactory.enterContext();
78  
79          FileWriter out = null;
80          try
81          {
82              out = new FileWriter( compressed );
83              out.write(Compressor.compressScript(new FileReader(input)));
84          }
85          catch ( Exception e )
86          {
87              throw new CompressionException( "Failed to create compressed file", e, input );
88          }
89          finally
90          {
91              Context.exit();
92              IOUtil.close( out );
93          }
94      }
95  }