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  
25  import com.yahoo.platform.yui.compressor.JavaScriptCompressor;
26  
27  /**
28   * A JS compressor that uses Dojo modified Rhino engine to compress the script.
29   * The resulting compressed-js is garanteed to be functionaly equivalent as this
30   * is the internal view of the rhino context.
31   * 
32   * @author <a href="mailto:nicolas@apache.org">nicolas De Loof</a>
33   */
34  public class YahooUICompressor
35      implements JSCompressor
36  {
37      ErrorReporter4Mojo log = null;
38  
39      /**
40       * Set a the JSCompressorLogger implementation that will receive logs
41       *
42       * @param logger a logger
43       */
44      public void setLogger(JSCompressorLogger logger) throws CompressionException
45      {
46          log = new ErrorReporter4Mojo(logger,true);
47      }
48  
49      /**
50       * Return current JSCompressorLogger used for logging
51       *
52       * @return the current JSCompressorLogger used for logging
53       */
54      public JSCompressorLogger getLogger() throws CompressionException
55      {
56          return log.getLogger();
57      }
58  
59      /**
60       * {@inheritDoc}
61       * 
62       * @see org.codehaus.mojo.javascript.compress.JSCompressor#compress(java.io.File,
63       * java.io.File, int, int)
64       */
65      public void compress( final File input, File compressed, int level, int language)
66          throws CompressionException
67      {
68          FileWriter out = null;
69          try
70          {
71              JavaScriptCompressor compressor =
72                  new JavaScriptCompressor( new FileReader( input ), log );
73  
74              int linebreakpos = (level < 6) ? 0 : (level < 9) ? 120 : -1;
75              boolean nomunge = level < 3;
76              level = (level>2) ? ((level>5) ? level-6 : level-3) : level;
77              boolean preserveAllSemiColons = level < 2;
78              boolean disableOptimizations = level < 1;
79  
80              out = new FileWriter( compressed );
81              compressor.compress( out, linebreakpos, !nomunge, true, preserveAllSemiColons, disableOptimizations );
82          }
83          catch ( Exception e )
84          {
85              throw new CompressionException( "Failed to create compressed file", e, input );
86          }
87          finally
88          {
89              IOUtil.close( out );
90          }
91      }
92  }