View Javadoc

1   /*
2    * Copyright 2011 SOFTEC sa. All rights reserved.
3    *
4    * This source code is licensed under the Creative Commons
5    * Attribution-NonCommercial-NoDerivs 3.0 Luxembourg
6    * License.
7    *
8    * To view a copy of this license, visit
9    * http://creativecommons.org/licenses/by-nc-nd/3.0/lu/
10   * or send a letter to Creative Commons, 171 Second Street,
11   * Suite 300, San Francisco, California, 94105, USA.
12   */
13  
14  package org.codehaus.mojo.javascript.titanium;
15  
16  import org.apache.maven.plugin.MojoExecutionException;
17  import org.codehaus.plexus.util.FileUtils;
18  import org.codehaus.plexus.util.IOUtil;
19  
20  import java.io.*;
21  
22  /**
23   * Class to strip lines from a file.
24   */
25  public class FileStrip {
26  
27      /**
28       * A list of special token to recognize lines to be removed from scripts (debugging
29       * code).
30       */
31      private String[] strips;
32  
33      /**
34       * A special token to recognize lines to be removed from scripts (debugging
35       * code).
36       */
37      private String strip;
38  
39      /**
40       * The folder where stripped files will be created.
41       */
42      private File strippedDirectory;
43  
44      /**
45       * Retrieve the special token to recognize lines to be removed from scripts.
46       * @return The strip token. May be null.
47       */
48      public String getStrip() {
49          return strip;
50      }
51  
52      public void setStrip(String strip) {
53          this.strip = strip;
54      }
55  
56      public File getStrippedDirectory() {
57          return strippedDirectory;
58      }
59  
60      public void setStrippedDirectory(File strippedDirectory) {
61          this.strippedDirectory = strippedDirectory;
62      }
63  
64      public String[] getStrips() {
65          return strips;
66      }
67  
68      public void setStrips(String[] strips) {
69          this.strips = strips;
70      }
71  
72      public FileStrip(File strippedDirectory, String strip) {
73          this(strippedDirectory, strip, null);
74      }
75  
76      public FileStrip(File strippedDirectory, String[] strips) {
77          this(strippedDirectory, null, strips);
78      }
79  
80      public FileStrip(File strippedDirectory, String strip, String[] strips) {
81          this.strippedDirectory = strippedDirectory;
82          this.strip = strip;
83          this.strips = strips;
84      }
85  
86  
87      /**
88       * Strip the specified file.
89       * @param name The name of the destination file.
90       * Will be generated in the {@link #strippedDirectory} folder.
91       * @param file The file to strip.
92       * @return The stripped file.
93       * @throws MojoExecutionException When an error occurs while stripping the file.
94       */
95      public File strip(String name, File file) throws MojoExecutionException {
96          return stripDebugs(name, file);
97      }
98  
99      private File stripDebugs( String name, File file )
100         throws MojoExecutionException
101     {
102         if ( strip == null && (strips == null || strips.length == 0))
103         {
104             return file;
105         }
106 
107         File stripped = new File( getStrippedDirectory(), name );
108         stripped.getParentFile().mkdirs();
109         if ( file.equals( stripped ) )
110         {
111             try
112             {
113                 File temp = File.createTempFile( "stripped", ".js" );
114                 stripDebugs(file, temp);
115                 FileUtils.copyFile(temp, file);
116                 temp.delete();
117                 return file;
118             }
119             catch ( IOException e )
120             {
121                 throw new MojoExecutionException( "Error creating temp file for stripping", e );
122             }
123         }
124         else
125         {
126             stripDebugs(file, stripped);
127             return stripped;
128         }
129     }
130 
131     private void stripDebugs( File file, File stripped )
132             throws MojoExecutionException
133     {
134         try
135         {
136             BufferedReader reader = new BufferedReader( new FileReader( file ) );
137             PrintWriter writer = new PrintWriter( stripped );
138             String line;
139             while ( ( line = reader.readLine() ) != null )
140             {
141                 String trimmed = line.trim();
142                 boolean stripLine = (strip != null && trimmed.startsWith( strip ));
143                 if( strips != null ) {
144                     for( int i=0, len = strips.length; !stripLine && i < len; i++ ) {
145                         stripLine = trimmed.startsWith( strips[i] );
146                     }
147                 }
148                 if ( !stripLine )
149                 {
150                     writer.println( line );
151                 }
152             }
153             IOUtil.close(reader);
154             IOUtil.close( writer );
155         }
156         catch ( IOException e )
157         {
158             throw new MojoExecutionException( "Failed to strip debug code in " + file, e );
159         }
160     }
161 
162 
163 }