This Bugzilla instance is a read-only archive of historic NetBeans bug reports. To report a bug in NetBeans please follow the project's instructions for reporting issues.

View | Details | Raw Unified | Return to bug 234883
Collapse All | Expand All

(-)src/main/java/org/codehaus/mojo/nbm/BrandingMojo.java (-11 / +118 lines)
Lines 17-27 Link Here
17
package org.codehaus.mojo.nbm;
17
package org.codehaus.mojo.nbm;
18
18
19
import java.io.File;
19
import java.io.File;
20
import java.util.ArrayList;
21
import java.util.Collection;
22
import java.util.Collections;
23
import java.util.HashMap;
24
import java.util.HashSet;
25
import java.util.List;
26
import java.util.Locale;
27
import java.util.Map;
28
import java.util.Set;
20
import org.apache.maven.plugin.MojoExecutionException;
29
import org.apache.maven.plugin.MojoExecutionException;
21
import org.apache.maven.plugins.annotations.LifecyclePhase;
30
import org.apache.maven.plugins.annotations.LifecyclePhase;
22
import org.apache.maven.plugins.annotations.Mojo;
31
import org.apache.maven.plugins.annotations.Mojo;
23
import org.apache.maven.plugins.annotations.Parameter;
32
import org.apache.maven.plugins.annotations.Parameter;
24
import org.apache.maven.project.MavenProject;
33
import org.apache.maven.project.MavenProject;
34
import org.codehaus.plexus.archiver.FileSet;
25
import org.codehaus.plexus.archiver.jar.JarArchiver;
35
import org.codehaus.plexus.archiver.jar.JarArchiver;
26
import org.codehaus.plexus.util.DirectoryScanner;
36
import org.codehaus.plexus.util.DirectoryScanner;
27
import org.codehaus.plexus.util.FileUtils;
37
import org.codehaus.plexus.util.FileUtils;
Lines 139-162 Link Here
139
149
140
            for ( String jarDirectoryPath : scanner.getIncludedDirectories() )
150
            for ( String jarDirectoryPath : scanner.getIncludedDirectories() )
141
            {
151
            {
142
152
                DirectoryScanner ds = new DirectoryScanner();
143
                // move nnn.jar directory to nnn.jar.tmp
153
                ds.setIncludes( new String[]
154
                    {
155
                        "**/*.*"
156
                    } );
157
                ds.setBasedir( new File(clusterDir, jarDirectoryPath) );
158
                ds.scan();
159
                String [] includedFiles = ds.getIncludedFiles();
160
                Map<String, Set<File>> brandingFilesGroupByLocale = groupBrandingFilesByLocale( includedFiles);
144
                File jarDirectory = new File( clusterDir, jarDirectoryPath );
161
                File jarDirectory = new File( clusterDir, jarDirectoryPath );
145
162
                for(Map.Entry<String, Set<File>> entryIter: brandingFilesGroupByLocale.entrySet()) {
146
                // jars should be placed in locales/ under the same directory the jar-directories are
163
                    String locale = entryIter.getKey();
164
                    Set<File> brandingFiles = entryIter.getValue();
147
                File destinationJar =
165
                File destinationJar =
148
                    new File( jarDirectory.getParentFile().getAbsolutePath() + File.separator + "locale"
166
                    new File( jarDirectory.getParentFile().getAbsolutePath() + File.separator + "locale"
149
                        + File.separator + destinationFileName( jarDirectory.getName(), brandingToken ) );
167
                                + File.separator + destinationJarName( jarDirectory.getName(), brandingToken + (locale!=null? ("_" + locale): "") ) );
150
151
                // create nnn.jar archive of contents
152
                JarArchiver archiver = new JarArchiver();
168
                JarArchiver archiver = new JarArchiver();
153
                archiver.setDestFile( destinationJar );
169
                archiver.setDestFile( destinationJar );
154
                archiver.addDirectory( jarDirectory );
170
                    for(File brandingFileIter:brandingFiles) {
171
                        archiver.addDirectory( jarDirectory, new String[]
172
                            {
173
                                "**/"+brandingFileIter.getName()
174
                            }, new String[]{});
175
                    }
155
                archiver.createArchive();
176
                archiver.createArchive();
156
177
                }
157
                FileUtils.deleteDirectory( jarDirectory );
178
                FileUtils.deleteDirectory( jarDirectory );
158
            }
179
            }
159
160
        }
180
        }
161
        catch ( Exception ex )
181
        catch ( Exception ex )
162
        {
182
        {
Lines 168-181 Link Here
168
    static  String destinationFileName( String brandingFilePath, String branding )
188
    static  String destinationFileName( String brandingFilePath, String branding )
169
    {
189
    {
170
        // use first underscore in filename 
190
        // use first underscore in filename 
191
        String locale = "";
192
        String brandingFilename = "";
171
        int lastSeparator = brandingFilePath.lastIndexOf( File.separator );
193
        int lastSeparator = brandingFilePath.lastIndexOf( File.separator );
194
        if(lastSeparator != -1) {
195
            brandingFilename = brandingFilePath.substring( lastSeparator + 1);
196
        } else {
197
            brandingFilename = brandingFilePath;
198
        }
199
        
200
        //pass files to be localized
201
        if(brandingFilename.matches("([a-zA-z][a-zA-Z0-9]*)+_[a-z]{2}(_[a-z]{2})?.[a-zA-Z][a-zA-Z0-9]+")) {
202
            //last index of '_' exists according to pattern
203
            locale = brandingFilename.substring( brandingFilename.lastIndexOf( '_') + 1, brandingFilename.lastIndexOf( '.'));
204
            String subBrandingFileIterName = brandingFilename.substring( 0, brandingFilename.lastIndexOf('_') );
205
            
206
            //not sure about existing of last index of '_'
207
            int lastUnderscore = subBrandingFileIterName.lastIndexOf( '_' );
208
            if(lastUnderscore != -1) {
209
                locale = brandingFilename.substring( subBrandingFileIterName.lastIndexOf('_') + 1, brandingFilename.lastIndexOf( '.'));
210
            }
211
            brandingFilePath = brandingFilePath.substring( 0, brandingFilePath.lastIndexOf( '.') - (locale.length() +1)) + 
212
                        brandingFilePath.substring( brandingFilePath.lastIndexOf( '.'));
213
        }
214
        
215
        String infix = "_" + branding + (!"".equals( locale )?("_" + locale) : "");
216
217
        // no underscores, use dot
218
        int lastDot = brandingFilePath.lastIndexOf( '.' );
219
        if (lastDot == -1 || lastDot < lastSeparator) {
220
            return brandingFilePath + infix;
221
        }
222
        return brandingFilePath.substring( 0, lastDot ) + infix + brandingFilePath.substring( lastDot );
223
    }
224
    
225
    static  String destinationJarName( String brandingFilePath, String branding )
226
    {
227
        // use first underscore in filename 
228
        int lastSeparator = brandingFilePath.lastIndexOf( File.separator );
172
        String infix = "_" + branding;
229
        String infix = "_" + branding;
173
230
174
        // no underscores, use dot
231
        // no underscores, use dot
175
        int lastDot = brandingFilePath.lastIndexOf( "." );
232
        int lastDot = brandingFilePath.lastIndexOf( '.' );
176
        if (lastDot == -1 || lastDot < lastSeparator) {
233
        if (lastDot == -1 || lastDot < lastSeparator) {
177
            return brandingFilePath + infix;
234
            return brandingFilePath + infix;
178
        }
235
        }
179
        return brandingFilePath.substring( 0, lastDot ) + infix + brandingFilePath.substring( lastDot );
236
        return brandingFilePath.substring( 0, lastDot ) + infix + brandingFilePath.substring( lastDot );
180
    }
237
    }
238
    
239
    static Map<String, Set<File>> groupBrandingFilesByLocale(String [] brandingFiles) {
240
        
241
        Map<String, Set<File>> result = new HashMap<String, Set<File>>();
242
        for(String brandingFileStrIter:brandingFiles) {
243
            
244
            File brandingFileIter = new File(brandingFileStrIter);
245
            String locale = "";
246
            
247
            //pass files to be localized
248
            if(brandingFileIter.getName().matches("([a-zA-z][a-zA-Z0-9]*)+_[a-zA-z][a-zA-Z0-9]*_[a-z]{2}(_[a-z]{2})?.[a-zA-Z][a-zA-Z0-9]+")) {
249
                
250
                int lastSeparator = brandingFileStrIter.lastIndexOf( File.separator );
251
                String brandingFilename = "";
252
                if(lastSeparator != -1) {
253
                    brandingFilename = brandingFileStrIter.substring( lastSeparator + 1);
254
                } else {
255
                    brandingFilename = brandingFileStrIter;
181
}
256
}
257
                
258
                locale = brandingFilename.substring( brandingFilename.lastIndexOf('_') + 1, brandingFilename.lastIndexOf( '.'));
259
                
260
                //last index of '_' exists according to pattern
261
                String subBrandingFileIterName = brandingFilename.substring( 0, brandingFilename.lastIndexOf('_') );
262
                
263
                //not sure about existing of last index of '_'
264
                int lastUnderscore = subBrandingFileIterName.lastIndexOf( "_" );
265
                if(lastUnderscore != -1) {
266
                    locale = brandingFilename.substring( lastUnderscore + 1, brandingFilename.lastIndexOf( '.'));
267
                }
268
                
269
                if(result.get( locale)==null) {
270
                    Set<File> newFileLst = new HashSet<File>();
271
                    newFileLst.add( brandingFileIter );
272
                    result.put( locale, newFileLst);
273
                } else {
274
                    result.get( locale).add( brandingFileIter );
275
                }
276
            } else {
277
                if(result.get( null)==null) {
278
                    Set<File> newFileLst = new HashSet<File>();
279
                    newFileLst.add( brandingFileIter );
280
                    result.put( null, newFileLst);
281
                } else {
282
                    result.get( null).add( brandingFileIter );
283
                }
284
            }
285
        }
286
        return result;
287
    }
288
}
(-)src/test/java/org/codehaus/mojo/nbm/BrandingMojoTest.java (+1 lines)
Lines 36-41 Link Here
36
    @Test
36
    @Test
37
    public void testDestinationFileName()
37
    public void testDestinationFileName()
38
    {
38
    {
39
        assertEquals( "Bundle_project_en_us.properties", BrandingMojo.destinationFileName( "Bundle_en_us.properties", "project"));
39
        assertEquals( "cut_brandingToken.gif", BrandingMojo.destinationFileName( "cut.gif", "brandingToken" ) );
40
        assertEquals( "cut_brandingToken.gif", BrandingMojo.destinationFileName( "cut.gif", "brandingToken" ) );
40
        assertEquals( "cut_brandingToken", BrandingMojo.destinationFileName( "cut", "brandingToken" ) );
41
        assertEquals( "cut_brandingToken", BrandingMojo.destinationFileName( "cut", "brandingToken" ) );
41
        assertEquals( "cut_pressed_brandingToken.gif", BrandingMojo.destinationFileName( "cut_pressed.gif", "brandingToken" ) );
42
        assertEquals( "cut_pressed_brandingToken.gif", BrandingMojo.destinationFileName( "cut_pressed.gif", "brandingToken" ) );

Return to bug 234883