1
2
3
4
5
6
7
8
9
10
11
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
24
25 public class FileStrip {
26
27
28
29
30
31 private String[] strips;
32
33
34
35
36
37 private String strip;
38
39
40
41
42 private File strippedDirectory;
43
44
45
46
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
89
90
91
92
93
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 }