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

(-)apisupport.ant/src/org/netbeans/modules/apisupport/project/NbRefactoringProviderImpl.java (+230 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2013 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.apisupport.project;
43
44
import java.io.File;
45
import java.io.FileNotFoundException;
46
import java.io.IOException;
47
import java.io.OutputStream;
48
import java.util.ArrayList;
49
import java.util.HashMap;
50
import java.util.List;
51
import java.util.Map;
52
import javax.xml.parsers.DocumentBuilder;
53
import javax.xml.parsers.DocumentBuilderFactory;
54
import javax.xml.parsers.ParserConfigurationException;
55
import org.netbeans.api.java.project.JavaProjectConstants;
56
import org.netbeans.api.project.Project;
57
import org.netbeans.api.project.SourceGroup;
58
import org.netbeans.api.project.Sources;
59
import org.netbeans.modules.apisupport.project.api.EditableManifest;
60
import org.netbeans.modules.apisupport.project.api.ManifestManager;
61
import org.netbeans.modules.apisupport.project.api.Util;
62
import org.netbeans.modules.apisupport.project.spi.NbRefactoringContext;
63
import org.netbeans.modules.apisupport.project.spi.NbRefactoringProvider;
64
import org.openide.filesystems.FileObject;
65
import org.openide.filesystems.FileStateInvalidException;
66
import org.openide.filesystems.FileSystem;
67
import org.openide.filesystems.FileUtil;
68
import org.openide.util.Exceptions;
69
import org.openide.xml.XMLUtil;
70
import org.w3c.dom.Document;
71
import org.w3c.dom.NamedNodeMap;
72
import org.w3c.dom.Node;
73
import org.xml.sax.SAXException;
74
75
/**
76
 *
77
 * @author mkozeny
78
 */
79
public class NbRefactoringProviderImpl implements NbRefactoringProvider {
80
81
    private final Project project;
82
83
    NbRefactoringProviderImpl(Project project) {
84
        this.project = project;
85
    }
86
87
    @Override
88
    public Map<String, String> getManifestRefactoringElements(NbRefactoringContext context) {
89
        Sources srcs = org.netbeans.api.project.ProjectUtils.getSources(project);
90
        SourceGroup[] grps = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
91
        for (SourceGroup gr : grps) {
92
            if (!FileUtil.isParentOf(gr.getRootFolder(), context.getFileToRefactored())) {
93
                return null;
94
            }
95
        }
96
        Map<String, String> result = null;
97
        if (context.getOldPackagePath() != null && context.getNewPackagePath() != null) {
98
            result = new HashMap<String, String>();
99
            try {
100
                NbModuleProject nbmProject = project.getLookup().lookup(NbModuleProject.class);
101
                FileObject manifest;
102
                if (nbmProject != null && (manifest = nbmProject.getManifestFile()) != null) {
103
                    EditableManifest em = Util.loadManifest(manifest);
104
                    String oldCodeNameBase = em.getAttribute(ManifestManager.OPENIDE_MODULE, null);
105
                    if (oldCodeNameBase != null && oldCodeNameBase.equals(context.getOldPackagePath())) {
106
                        result.put(ManifestManager.OPENIDE_MODULE, context.getNewPackagePath());
107
                    }
108
                    String oldBundlePath = em.getAttribute(ManifestManager.OPENIDE_MODULE_LOCALIZING_BUNDLE, null);
109
                    if (oldBundlePath != null && oldBundlePath.equals(context.getOldPackagePath() + "/Bundle.properties")) {
110
                        result.put(ManifestManager.OPENIDE_MODULE_LOCALIZING_BUNDLE, context.getNewPackagePath()
111
                                + "/Bundle.properties");
112
                    }
113
                    String oldLayerPath = em.getAttribute(ManifestManager.OPENIDE_MODULE_LAYER, null);
114
                    if (oldLayerPath != null && oldLayerPath.equals(context.getOldPackagePath() + "/layer.xml")) {
115
                        result.put(ManifestManager.OPENIDE_MODULE_LAYER, context.getNewPackagePath()
116
                                + "/layer.xml");
117
                    }
118
                }
119
            } catch (FileNotFoundException ex) {
120
                Exceptions.printStackTrace(ex);
121
            } catch (IOException ex) {
122
                Exceptions.printStackTrace(ex);
123
            }
124
        }
125
        return result;
126
    }
127
128
    @Override
129
    public List<ProjectFileRefactoring> getProjectXmlFilesRefactoring(NbRefactoringContext context) {
130
        Sources srcs = org.netbeans.api.project.ProjectUtils.getSources(project);
131
        SourceGroup[] grps = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_JAVA);
132
        for (SourceGroup gr : grps) {
133
            if (!FileUtil.isParentOf(gr.getRootFolder(), context.getFileToRefactored())) {
134
                return null;
135
            }
136
        }
137
        List<ProjectFileRefactoring> result = new ArrayList<ProjectFileRefactoring>();
138
        NbModuleProject nbmProject = project.getLookup().lookup(NbModuleProject.class);
139
        if (nbmProject != null) {
140
            FileObject buildFileObj = FileUtil.toFileObject(nbmProject.getHelper().resolveFile("build.xml"));
141
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
142
            File buildFile = FileUtil.toFile(buildFileObj);
143
            synchronized (buildFile) {
144
                try {
145
                    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
146
                    Document buildScript = dBuilder.parse(buildFile);
147
                    if (buildScript.getDocumentElement().getAttribute("name").equals(context.getOldPackagePath())) {
148
                        Map<String, Map<String, String>> resultElements = new HashMap<String, Map<String, String>>(1);
149
                        HashMap<String, String> elements = new HashMap<String, String>(1);
150
                        elements.put("name", context.getNewPackagePath());
151
                        resultElements.put("project", elements);
152
                        result.add(new ProjectFileRefactoring(buildFileObj, resultElements));
153
                    }
154
                } catch (ParserConfigurationException ex) {
155
                    Exceptions.printStackTrace(ex);
156
                } catch (SAXException ex) {
157
                    Exceptions.printStackTrace(ex);
158
                } catch (IOException ex) {
159
                    Exceptions.printStackTrace(ex);
160
                }
161
            }
162
            FileObject projectFileObj = FileUtil.toFileObject(new File(nbmProject.getProjectDirectoryFile(), "nbproject/project.xml"));
163
            File projectFile = FileUtil.toFile(projectFileObj);
164
            synchronized (projectFile) {
165
                try {
166
                    DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
167
                    Document projectXmlFile = dBuilder.parse(projectFile);
168
                    Node elementNode = projectXmlFile.getElementsByTagName("code-name-base").item(0);
169
                    if (elementNode.getTextContent().equals(context.getOldPackagePath())) {
170
                        Map<String, Map<String, String>> resultElements = new HashMap<String, Map<String, String>>(1);
171
                        HashMap<String, String> elements = new HashMap<String, String>(1);
172
                        elements.put(null, context.getNewPackagePath());
173
                        resultElements.put("code-name-base", elements);
174
                        result.add(new ProjectFileRefactoring(projectFileObj, resultElements));
175
                    }
176
                } catch (ParserConfigurationException ex) {
177
                    Exceptions.printStackTrace(ex);
178
                } catch (SAXException ex) {
179
                    Exceptions.printStackTrace(ex);
180
                } catch (IOException ex) {
181
                    Exceptions.printStackTrace(ex);
182
                }
183
            }
184
        }
185
        return result;
186
    }
187
188
    @Override
189
    public void doProjectXmlFilesRefactoring(final FileObject projectFileObj, final String element, final String attrName, final String value) {
190
        try {
191
            projectFileObj.getFileSystem().runAtomicAction(new FileSystem.AtomicAction() {
192
                @Override
193
                public void run() throws IOException {
194
                    DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
195
                    File projectFile = FileUtil.toFile(projectFileObj);
196
                    synchronized (projectFile) {
197
                        try {
198
                            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
199
                            Document buildScript = dBuilder.parse(projectFile);
200
                            Node elementNode = buildScript.getElementsByTagName(element).item(0);
201
                            if (attrName != null) {
202
                                NamedNodeMap attr = elementNode.getAttributes();
203
                                Node nodeAttr = attr.getNamedItem(attrName);
204
                                nodeAttr.setTextContent(value);
205
                                buildScript.getDocumentElement().setAttribute(attrName, value);
206
                            } else {
207
                                elementNode.setTextContent(value);
208
                            }
209
                            OutputStream os = projectFileObj.getOutputStream();
210
                            try {
211
                                XMLUtil.write(buildScript, os, "UTF-8"); // NOI18N
212
                            } finally {
213
                                os.close();
214
                            }
215
                        } catch (ParserConfigurationException ex) {
216
                            Exceptions.printStackTrace(ex);
217
                        } catch (SAXException ex) {
218
                            Exceptions.printStackTrace(ex);
219
                        }
220
                    }
221
                }
222
            });
223
        } catch (FileStateInvalidException ex) {
224
            Exceptions.printStackTrace(ex);
225
        } catch (IOException ex) {
226
            Exceptions.printStackTrace(ex);
227
        }
228
    }
229
230
}
(-)apisupport.project/src/org/netbeans/modules/apisupport/project/spi/NbRefactoringContext.java (+78 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2013 Sun Microsystems, Inc.
41
 */
42
43
package org.netbeans.modules.apisupport.project.spi;
44
45
import org.openide.filesystems.FileObject;
46
47
/**
48
 *
49
 * @author mkozeny
50
 */
51
public final class NbRefactoringContext {
52
    
53
    private FileObject fileToRefactored;
54
    
55
    private String oldPackagePath;
56
    
57
    private String newPackagePath;
58
59
    public NbRefactoringContext(FileObject fileToRefactored, String newPackagePath, String oldPackagePath) {
60
        this.fileToRefactored = fileToRefactored;
61
        this.newPackagePath = newPackagePath;
62
        this.oldPackagePath = oldPackagePath;
63
    }
64
65
    public FileObject getFileToRefactored() {
66
        return fileToRefactored;
67
    }
68
    
69
    public String getOldPackagePath() {
70
        return oldPackagePath;
71
    }
72
73
    public String getNewPackagePath() {
74
        return newPackagePath;
75
    }
76
    
77
    
78
}
(-)apisupport.project/src/org/netbeans/modules/apisupport/project/spi/NbRefactoringProvider.java (+100 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2013 Sun Microsystems, Inc.
41
 */
42
43
package org.netbeans.modules.apisupport.project.spi;
44
45
import java.util.List;
46
import java.util.Map;
47
import org.openide.filesystems.FileObject;
48
49
/**
50
 * Interface to be implemented by NetBeans module projects. 
51
 *
52
 * @author Martin Kozeny
53
 * @since org.netbeans.modules.apisupport.project 1.65
54
 */
55
public interface NbRefactoringProvider {
56
    
57
    /**
58
     * Refactors project's manifest file according to passed context
59
     * @param context refactoring context
60
     * @return  
61
     */
62
    Map<String, String> getManifestRefactoringElements(final NbRefactoringContext context);
63
    
64
    /**
65
     * Returns list of xml files and its elements to refactor
66
     * @param context refactoring context
67
     * @return 
68
     */
69
    List<ProjectFileRefactoring> getProjectXmlFilesRefactoring(final NbRefactoringContext context);
70
    
71
    /**
72
     * Refactors project's xml file according to passed element, attribute and value
73
     * @param projectFile
74
     * @param element
75
     * @param attrName
76
     * @param value 
77
     */
78
    void doProjectXmlFilesRefactoring(final FileObject projectFile, final String element, final String attrName, final String value);
79
    
80
    public final class ProjectFileRefactoring {
81
        
82
        private FileObject refactoredFile;
83
        
84
        private Map<String,Map<String, String>> refactoredElements;
85
86
        public ProjectFileRefactoring(FileObject refactoredFile, Map<String, Map<String, String>> refactoredElements) {
87
            this.refactoredFile = refactoredFile;
88
            this.refactoredElements = refactoredElements;
89
        }
90
91
        public FileObject getRefactoredFile() {
92
            return refactoredFile;
93
        }
94
95
        public Map<String, Map<String, String>> getRefactoredElements() {
96
            return refactoredElements;
97
        }
98
        
99
    }
100
}
(-)maven.apisupport/src/org/netbeans/modules/maven/apisupport/MavenRefactoringProviderImpl.java (+121 lines)
Line 0 Link Here
1
/*
2
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3
 *
4
 * Copyright 2013 Oracle and/or its affiliates. All rights reserved.
5
 *
6
 * Oracle and Java are registered trademarks of Oracle and/or its affiliates.
7
 * Other names may be trademarks of their respective owners.
8
 *
9
 * The contents of this file are subject to the terms of either the GNU
10
 * General Public License Version 2 only ("GPL") or the Common
11
 * Development and Distribution License("CDDL") (collectively, the
12
 * "License"). You may not use this file except in compliance with the
13
 * License. You can obtain a copy of the License at
14
 * http://www.netbeans.org/cddl-gplv2.html
15
 * or nbbuild/licenses/CDDL-GPL-2-CP. See the License for the
16
 * specific language governing permissions and limitations under the
17
 * License.  When distributing the software, include this License Header
18
 * Notice in each file and include the License file at
19
 * nbbuild/licenses/CDDL-GPL-2-CP.  Oracle designates this
20
 * particular file as subject to the "Classpath" exception as provided
21
 * by Oracle in the GPL Version 2 section of the License file that
22
 * accompanied this code. If applicable, add the following below the
23
 * License Header, with the fields enclosed by brackets [] replaced by
24
 * your own identifying information:
25
 * "Portions Copyrighted [year] [name of copyright owner]"
26
 *
27
 * If you wish your version of this file to be governed by only the CDDL
28
 * or only the GPL Version 2, indicate your decision by adding
29
 * "[Contributor] elects to include this software in this distribution
30
 * under the [CDDL or GPL Version 2] license." If you do not indicate a
31
 * single choice of license, a recipient has the option to distribute
32
 * your version of this file under either the CDDL, the GPL Version 2 or
33
 * to extend the choice of license to its licensees as provided above.
34
 * However, if you add GPL Version 2 code and therefore, elected the GPL
35
 * Version 2 license, then the option applies only if the new code is
36
 * made subject to such option by the copyright holder.
37
 *
38
 * Contributor(s):
39
 *
40
 * Portions Copyrighted 2013 Sun Microsystems, Inc.
41
 */
42
package org.netbeans.modules.maven.apisupport;
43
44
import java.io.IOException;
45
import java.util.HashMap;
46
import java.util.List;
47
import java.util.Map;
48
import org.netbeans.api.java.project.JavaProjectConstants;
49
import org.netbeans.api.project.Project;
50
import org.netbeans.api.project.SourceGroup;
51
import org.netbeans.api.project.Sources;
52
import org.netbeans.modules.apisupport.project.api.EditableManifest;
53
import org.netbeans.modules.apisupport.project.api.ManifestManager;
54
import org.netbeans.modules.apisupport.project.api.Util;
55
import org.netbeans.modules.apisupport.project.spi.NbModuleProvider;
56
import org.netbeans.modules.apisupport.project.spi.NbRefactoringContext;
57
import org.netbeans.modules.apisupport.project.spi.NbRefactoringProvider;
58
import org.netbeans.modules.maven.api.NbMavenProject;
59
import org.netbeans.spi.project.ProjectServiceProvider;
60
import org.openide.filesystems.FileObject;
61
import org.openide.filesystems.FileUtil;
62
import org.openide.util.Exceptions;
63
64
/**
65
 *
66
 * @author mkozeny
67
 */
68
@ProjectServiceProvider(service = NbRefactoringProvider.class, projectType = "org-netbeans-modules-maven/" + NbMavenProject.TYPE_NBM)
69
public class MavenRefactoringProviderImpl implements NbRefactoringProvider {
70
71
    private Project project;
72
73
    public MavenRefactoringProviderImpl(Project project) {
74
        this.project = project;
75
    }
76
77
    @Override
78
    public Map<String, String> getManifestRefactoringElements(final NbRefactoringContext context) {
79
        Sources srcs = org.netbeans.api.project.ProjectUtils.getSources(project);
80
        SourceGroup[] grps = srcs.getSourceGroups(JavaProjectConstants.SOURCES_TYPE_RESOURCES);
81
        for (SourceGroup gr : grps) {
82
            if (!FileUtil.isParentOf(gr.getRootFolder(), context.getFileToRefactored())) {
83
                return null;
84
            }
85
        }
86
        Map<String, String> result = null;
87
        if (context.getOldPackagePath() != null && context.getNewPackagePath() != null) {
88
            result = new HashMap<String, String>();
89
            try {
90
                NbModuleProvider provider = project.getLookup().lookup(NbModuleProvider.class);
91
                FileObject manifest = provider.getManifestFile();
92
                if (manifest != null) {
93
                    EditableManifest em = Util.loadManifest(manifest);
94
                    String oldBundlePath = em.getAttribute(ManifestManager.OPENIDE_MODULE_LOCALIZING_BUNDLE, null);
95
                    if (oldBundlePath != null && oldBundlePath.equals(context.getOldPackagePath() + "/Bundle.properties")) {
96
                        result.put(ManifestManager.OPENIDE_MODULE_LOCALIZING_BUNDLE, context.getNewPackagePath()
97
                                + "/Bundle.properties");
98
                    }
99
                    String oldLayerPath = em.getAttribute(ManifestManager.OPENIDE_MODULE_LAYER, null);
100
                    if (oldLayerPath != null && oldLayerPath.equals(context.getOldPackagePath() + "/layer.xml")) {
101
                        result.put(ManifestManager.OPENIDE_MODULE_LAYER, context.getNewPackagePath()
102
                                + "/layer.xml");
103
                    }
104
                }
105
            } catch (IOException ex) {
106
                Exceptions.printStackTrace(ex);
107
            }
108
        }
109
        return result;
110
    }
111
112
    @Override
113
    public List<ProjectFileRefactoring> getProjectXmlFilesRefactoring(NbRefactoringContext context) {
114
        return null;
115
    }
116
117
    @Override
118
    public void doProjectXmlFilesRefactoring(FileObject projectFile, String element, String attrName, String attrValue) {
119
    }
120
121
}
(-)src/main/java/org/codehaus/mojo/nbm/BrandingMojo.java (-11 / +114 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
            if(subBrandingFileIterName.matches( "([a-zA-z][a-zA-Z0-9]*)+_[a-z]{2}")) {
207
                locale = brandingFilename.substring( subBrandingFileIterName.lastIndexOf('_') + 1, brandingFilename.lastIndexOf( '.'));
208
            }
209
            brandingFilePath = brandingFilePath.substring( 0, brandingFilePath.lastIndexOf( '.') - (locale.length() +1)) + 
210
                        brandingFilePath.substring( brandingFilePath.lastIndexOf( '.'));
211
        }
212
        
213
        String infix = "_" + branding + (!"".equals( locale )?("_" + locale) : "");
214
215
        // no underscores, use dot
216
        int lastDot = brandingFilePath.lastIndexOf( '.' );
217
        if (lastDot == -1 || lastDot < lastSeparator) {
218
            return brandingFilePath + infix;
219
        }
220
        return brandingFilePath.substring( 0, lastDot ) + infix + brandingFilePath.substring( lastDot );
221
    }
222
    
223
    static  String destinationJarName( String brandingFilePath, String branding )
224
    {
225
        // use first underscore in filename 
226
        int lastSeparator = brandingFilePath.lastIndexOf( File.separator );
172
        String infix = "_" + branding;
227
        String infix = "_" + branding;
173
228
174
        // no underscores, use dot
229
        // no underscores, use dot
175
        int lastDot = brandingFilePath.lastIndexOf( "." );
230
        int lastDot = brandingFilePath.lastIndexOf( '.' );
176
        if (lastDot == -1 || lastDot < lastSeparator) {
231
        if (lastDot == -1 || lastDot < lastSeparator) {
177
            return brandingFilePath + infix;
232
            return brandingFilePath + infix;
178
        }
233
        }
179
        return brandingFilePath.substring( 0, lastDot ) + infix + brandingFilePath.substring( lastDot );
234
        return brandingFilePath.substring( 0, lastDot ) + infix + brandingFilePath.substring( lastDot );
180
    }
235
    }
236
    
237
    static Map<String, Set<File>> groupBrandingFilesByLocale(String [] brandingFiles) {
238
        
239
        Map<String, Set<File>> result = new HashMap<String, Set<File>>();
240
        for(String brandingFileStrIter:brandingFiles) {
241
            
242
            File brandingFileIter = new File(brandingFileStrIter);
243
            String locale = "";
244
            
245
            //pass files to be localized
246
            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]+")) {
247
                
248
                int lastSeparator = brandingFileStrIter.lastIndexOf( File.separator );
249
                String brandingFilename = "";
250
                if(lastSeparator != -1) {
251
                    brandingFilename = brandingFileStrIter.substring( lastSeparator + 1);
252
                } else {
253
                    brandingFilename = brandingFileStrIter;
181
}
254
}
255
                
256
                locale = brandingFilename.substring( brandingFilename.lastIndexOf('_') + 1, brandingFilename.lastIndexOf( '.'));
257
                
258
                //last index of '_' exists according to pattern
259
                String subBrandingFileIterName = brandingFilename.substring( 0, brandingFilename.lastIndexOf('_') );
260
                
261
                if(subBrandingFileIterName.matches( "([a-zA-z][a-zA-Z0-9]*)+_[a-zA-z][a-zA-Z0-9]*_[a-z]{2}")) {
262
                    locale = brandingFilename.substring( subBrandingFileIterName.lastIndexOf( "_" ) + 1, brandingFilename.lastIndexOf( '.'));
263
                }
264
                
265
                if(result.get( locale)==null) {
266
                    Set<File> newFileLst = new HashSet<File>();
267
                    newFileLst.add( brandingFileIter );
268
                    result.put( locale, newFileLst);
269
                } else {
270
                    result.get( locale).add( brandingFileIter );
271
                }
272
            } else {
273
                if(result.get( null)==null) {
274
                    Set<File> newFileLst = new HashSet<File>();
275
                    newFileLst.add( brandingFileIter );
276
                    result.put( null, newFileLst);
277
                } else {
278
                    result.get( null).add( brandingFileIter );
279
                }
280
            }
281
        }
282
        return result;
283
    }
284
}
(-)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