3 * Copyright 2000-2009 JetBrains s.r.o.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 package com
.intellij
.refactoring
.migration
;
19 import com
.intellij
.openapi
.application
.PathManager
;
20 import com
.intellij
.openapi
.diagnostic
.Logger
;
21 import com
.intellij
.openapi
.util
.InvalidDataException
;
22 import com
.intellij
.openapi
.util
.JDOMUtil
;
23 import com
.intellij
.openapi
.util
.io
.FileUtil
;
24 import com
.intellij
.openapi
.util
.text
.StringUtil
;
25 import com
.intellij
.psi
.codeStyle
.CodeStyleSettingsManager
;
26 import com
.intellij
.util
.UniqueFileNamesProvider
;
27 import org
.jdom
.Document
;
28 import org
.jdom
.Element
;
29 import org
.jdom
.JDOMException
;
30 import org
.jetbrains
.annotations
.NonNls
;
33 import java
.util
.ArrayList
;
34 import java
.util
.Iterator
;
39 public class MigrationMapSet
{
40 private static final Logger LOG
= Logger
.getInstance("#com.intellij.refactoring.migration.MigrationMapSet");
42 private ArrayList
<MigrationMap
> myMaps
= null;
43 @NonNls private static final String MIGRATION_MAP
= "migrationMap";
44 @NonNls private static final String ENTRY
= "entry";
45 @NonNls private static final String NAME
= "name";
46 @NonNls private static final String OLD_NAME
= "oldName";
47 @NonNls private static final String NEW_NAME
= "newName";
48 @NonNls private static final String DESCRIPTION
= "description";
49 @NonNls private static final String VALUE
= "value";
50 @NonNls private static final String TYPE
= "type";
51 @NonNls private static final String PACKAGE_TYPE
= "package";
52 @NonNls private static final String CLASS_TYPE
= "class";
53 @NonNls private static final String RECURSIVE
= "recursive";
55 @NonNls private static final String
[] DEFAULT_MAPS
= new String
[] {
56 "/com/intellij/refactoring/migration/res/Swing__1_0_3____1_1_.xml",
59 public MigrationMapSet() {
62 public void addMap(MigrationMap map
) {
70 public void replaceMap(MigrationMap oldMap
, MigrationMap newMap
) {
71 for(int i
= 0; i
< myMaps
.size(); i
++){
72 if (myMaps
.get(i
) == oldMap
){
73 myMaps
.set(i
, newMap
);
78 public void removeMap(MigrationMap map
) {
85 public MigrationMap
[] getMaps() {
89 MigrationMap
[] ret
= new MigrationMap
[myMaps
.size()];
90 for(int i
= 0; i
< myMaps
.size(); i
++){
91 ret
[i
] = myMaps
.get(i
);
96 private File
getMapDirectory() {
97 @NonNls String directoryPath
= PathManager
.getConfigPath() + File
.separator
+ "migration";
98 File dir
= new File(directoryPath
);
102 LOG
.error("cannot create directory: " + dir
.getAbsolutePath());
106 for (int i
= 0; i
< DEFAULT_MAPS
.length
; i
++) {
107 String defaultTemplate
= DEFAULT_MAPS
[i
];
108 java
.net
.URL url
= MigrationMapSet
.class.getResource(defaultTemplate
);
109 LOG
.assertTrue(url
!= null);
110 String fileName
= defaultTemplate
.substring(defaultTemplate
.lastIndexOf("/") + 1);
111 File targetFile
= new File(dir
, fileName
);
114 FileOutputStream outputStream
= new FileOutputStream(targetFile
);
115 InputStream inputStream
= url
.openStream();
118 FileUtil
.copy(inputStream
, outputStream
);
121 outputStream
.close();
125 catch (Exception e
) {
134 private File
[] getMapFiles() {
135 File dir
= getMapDirectory();
139 File
[] ret
= dir
.listFiles(new FileFilter() {
140 @SuppressWarnings({"HardCodedStringLiteral"})
141 public boolean accept(File file
){
142 return !file
.isDirectory() && StringUtil
.endsWithIgnoreCase(file
.getName(), ".xml");
146 LOG
.error("cannot read directory: " + dir
.getAbsolutePath());
152 private void loadMaps() {
153 myMaps
= new ArrayList
<MigrationMap
>();
155 File
[] files
= getMapFiles();
156 for(int i
= 0; i
< files
.length
; i
++){
158 MigrationMap map
= readMap(files
[i
]);
163 catch(InvalidDataException e
){
164 LOG
.error("Invalid data in file: " + files
[i
].getAbsolutePath());
166 catch (JDOMException e
) {
167 LOG
.error("Invalid data in file: " + files
[i
].getAbsolutePath());
169 catch (IOException e
) {
175 private MigrationMap
readMap(File file
) throws JDOMException
, InvalidDataException
, IOException
{
176 if (!file
.exists()) return null;
177 Document document
= JDOMUtil
.loadDocument(file
);
179 Element root
= document
.getRootElement();
180 if (root
== null || !MIGRATION_MAP
.equals(root
.getName())){
181 throw new InvalidDataException();
184 MigrationMap map
= new MigrationMap();
186 for(Iterator i
= root
.getChildren().iterator(); i
.hasNext(); ){
187 Element node
= (Element
)i
.next();
188 if (NAME
.equals(node
.getName())){
189 String name
= node
.getAttributeValue(VALUE
);
192 if (DESCRIPTION
.equals(node
.getName())){
193 String description
= node
.getAttributeValue(VALUE
);
194 map
.setDescription(description
);
197 if (ENTRY
.equals(node
.getName())){
198 MigrationMapEntry entry
= new MigrationMapEntry();
199 String oldName
= node
.getAttributeValue(OLD_NAME
);
200 if (oldName
== null){
201 throw new InvalidDataException();
203 entry
.setOldName(oldName
);
204 String newName
= node
.getAttributeValue(NEW_NAME
);
205 if (newName
== null){
206 throw new InvalidDataException();
208 entry
.setNewName(newName
);
209 String typeStr
= node
.getAttributeValue(TYPE
);
210 if (typeStr
== null){
211 throw new InvalidDataException();
213 entry
.setType(MigrationMapEntry
.CLASS
);
214 if (typeStr
.equals(PACKAGE_TYPE
)){
215 entry
.setType(MigrationMapEntry
.PACKAGE
);
216 @NonNls String isRecursiveStr
= node
.getAttributeValue(RECURSIVE
);
217 if (isRecursiveStr
!= null && isRecursiveStr
.equals("true")){
218 entry
.setRecursive(true);
221 entry
.setRecursive(false);
231 public void saveMaps() throws IOException
{
232 File dir
= getMapDirectory();
237 File
[] files
= getMapFiles();
239 @NonNls String
[] filePaths
= new String
[myMaps
.size()];
240 Document
[] documents
= new Document
[myMaps
.size()];
242 UniqueFileNamesProvider namesProvider
= new UniqueFileNamesProvider();
243 for(int i
= 0; i
< myMaps
.size(); i
++){
244 MigrationMap map
= myMaps
.get(i
);
246 filePaths
[i
] = dir
+ File
.separator
+ namesProvider
.suggestName(map
.getName()) + ".xml";
247 documents
[i
] = saveMap(map
);
250 JDOMUtil
.updateFileSet(files
, filePaths
, documents
, CodeStyleSettingsManager
.getSettings(null).getLineSeparator());
253 private Document
saveMap(MigrationMap map
) {
254 Element root
= new Element(MIGRATION_MAP
);
256 Element nameElement
= new Element(NAME
);
257 nameElement
.setAttribute(VALUE
, map
.getName());
258 root
.addContent(nameElement
);
260 Element descriptionElement
= new Element(DESCRIPTION
);
261 descriptionElement
.setAttribute(VALUE
, map
.getDescription());
262 root
.addContent(descriptionElement
);
264 for(int i
= 0; i
< map
.getEntryCount(); i
++){
265 MigrationMapEntry entry
= map
.getEntryAt(i
);
266 Element element
= new Element(ENTRY
);
267 element
.setAttribute(OLD_NAME
, entry
.getOldName());
268 element
.setAttribute(NEW_NAME
, entry
.getNewName());
269 if (entry
.getType() == MigrationMapEntry
.PACKAGE
){
270 element
.setAttribute(TYPE
, PACKAGE_TYPE
);
271 element
.setAttribute(RECURSIVE
, Boolean
.valueOf(entry
.isRecursive()).toString());
274 element
.setAttribute(TYPE
, CLASS_TYPE
);
276 root
.addContent(element
);
279 return new Document(root
);