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 |
} |