1 package com
.intellij
.conversion
.impl
;
3 import com
.intellij
.application
.options
.PathMacrosImpl
;
4 import com
.intellij
.application
.options
.ReplacePathToMacroMap
;
5 import com
.intellij
.conversion
.*;
6 import com
.intellij
.ide
.highlighter
.ProjectFileType
;
7 import com
.intellij
.ide
.highlighter
.WorkspaceFileType
;
8 import com
.intellij
.ide
.impl
.convert
.JDomConvertingUtil
;
9 import com
.intellij
.openapi
.application
.PathManager
;
10 import com
.intellij
.openapi
.components
.ExpandMacroToPathMap
;
11 import com
.intellij
.openapi
.components
.StorageScheme
;
12 import com
.intellij
.openapi
.diagnostic
.Logger
;
13 import com
.intellij
.openapi
.module
.impl
.ModuleManagerImpl
;
14 import com
.intellij
.openapi
.roots
.impl
.libraries
.LibraryImpl
;
15 import com
.intellij
.openapi
.roots
.libraries
.LibraryTablesRegistrar
;
16 import com
.intellij
.openapi
.util
.Condition
;
17 import com
.intellij
.openapi
.util
.SystemInfo
;
18 import com
.intellij
.openapi
.util
.io
.FileUtil
;
19 import com
.intellij
.openapi
.util
.text
.StringUtil
;
20 import com
.intellij
.openapi
.vfs
.VfsUtil
;
21 import com
.intellij
.util
.PathUtil
;
22 import org
.jdom
.Element
;
23 import org
.jetbrains
.annotations
.NotNull
;
24 import org
.jetbrains
.annotations
.Nullable
;
27 import java
.io
.FileFilter
;
28 import java
.io
.IOException
;
34 public class ConversionContextImpl
implements ConversionContext
{
35 private static final Logger LOG
= Logger
.getInstance("#com.intellij.conversion.impl.ConversionContextImpl");
36 private Map
<File
, SettingsXmlFile
> mySettingsFiles
= new HashMap
<File
, SettingsXmlFile
>();
37 private StorageScheme myStorageScheme
;
38 private File myProjectBaseDir
;
39 private File myProjectFile
;
40 private File myWorkspaceFile
;
41 private File
[] myModuleFiles
;
42 private ProjectSettingsImpl myProjectSettings
;
43 private WorkspaceSettingsImpl myWorkspaceSettings
;
44 private List
<File
> myNonExistingModuleFiles
= new ArrayList
<File
>();
45 private Map
<File
, ModuleSettingsImpl
> myModuleSettingsMap
= new HashMap
<File
, ModuleSettingsImpl
>();
46 private RunManagerSettingsImpl myRunManagerSettings
;
47 private File mySettingsBaseDir
;
48 private ComponentManagerSettings myCompilerManagerSettings
;
49 private ComponentManagerSettings myProjectRootManagerSettings
;
51 public ConversionContextImpl(String projectPath
) throws CannotConvertException
{
52 myProjectFile
= new File(projectPath
);
55 if (myProjectFile
.isDirectory()) {
56 myStorageScheme
= StorageScheme
.DIRECTORY_BASED
;
57 myProjectBaseDir
= myProjectFile
;
58 mySettingsBaseDir
= new File(myProjectBaseDir
.getAbsolutePath(), ".idea");
59 modulesFile
= new File(mySettingsBaseDir
, "modules.xml");
60 myWorkspaceFile
= new File(mySettingsBaseDir
, "workspace.xml");
63 myStorageScheme
= StorageScheme
.DEFAULT
;
64 myProjectBaseDir
= myProjectFile
.getParentFile();
65 modulesFile
= myProjectFile
;
66 myWorkspaceFile
= new File(StringUtil
.trimEnd(projectPath
, ProjectFileType
.DOT_DEFAULT_EXTENSION
) + WorkspaceFileType
.DOT_DEFAULT_EXTENSION
);
69 myModuleFiles
= findModuleFiles(JDomConvertingUtil
.loadDocument(modulesFile
).getRootElement());
72 public Set
<File
> getAllProjectFiles() {
73 final HashSet
<File
> files
= new HashSet
<File
>(Arrays
.asList(myModuleFiles
));
74 if (myStorageScheme
== StorageScheme
.DEFAULT
) {
75 files
.add(myProjectFile
);
76 files
.add(myWorkspaceFile
);
79 addFilesRecursively(mySettingsBaseDir
, files
);
84 private static void addFilesRecursively(File file
, Set
<File
> files
) {
85 if (file
.isDirectory()) {
86 final File
[] children
= file
.listFiles();
87 if (children
!= null) {
88 for (File child
: children
) {
89 addFilesRecursively(child
, files
);
93 else if (StringUtil
.endsWithIgnoreCase(file
.getName(), ".xml") && !file
.getName().startsWith(".")) {
99 public File
getProjectBaseDir() {
100 return myProjectBaseDir
;
103 public File
[] getModuleFiles() {
104 return myModuleFiles
;
107 private File
[] findModuleFiles(final Element root
) {
108 final Element modulesManager
= JDomConvertingUtil
.findComponent(root
, ModuleManagerImpl
.COMPONENT_NAME
);
109 if (modulesManager
== null) return new File
[0];
111 final Element modules
= modulesManager
.getChild(ModuleManagerImpl
.ELEMENT_MODULES
);
112 if (modules
== null) return new File
[0];
114 final ExpandMacroToPathMap macros
= createExpandMacroMap();
116 List
<File
> files
= new ArrayList
<File
>();
117 for (Element module
: JDomConvertingUtil
.getChildren(modules
, ModuleManagerImpl
.ELEMENT_MODULE
)) {
118 String filePath
= module
.getAttributeValue(ModuleManagerImpl
.ATTRIBUTE_FILEPATH
);
119 filePath
= macros
.substitute(filePath
, true, null);
120 files
.add(new File(FileUtil
.toSystemDependentName(filePath
)));
122 return files
.toArray(new File
[files
.size()]);
126 public String
expandPath(@NotNull String path
, @NotNull ModuleSettingsImpl moduleSettings
) {
127 return createExpandMacroMap(moduleSettings
).substitute(path
, true, null);
130 private ExpandMacroToPathMap
createExpandMacroMap(@Nullable ModuleSettingsImpl moduleSettings
) {
131 final ExpandMacroToPathMap map
= createExpandMacroMap();
132 if (moduleSettings
!= null) {
133 final String modulePath
= FileUtil
.toSystemIndependentName(moduleSettings
.getModuleFile().getParentFile().getAbsolutePath());
134 map
.addMacroExpand(PathMacrosImpl
.MODULE_DIR_MACRO_NAME
, modulePath
);
140 public String
collapsePath(@NotNull String path
) {
141 ReplacePathToMacroMap map
= createCollapseMacroMap(PathMacrosImpl
.PROJECT_DIR_MACRO_NAME
, myProjectBaseDir
);
142 return map
.substitute(path
, SystemInfo
.isFileSystemCaseSensitive
, null);
145 public String
collapsePath(@NotNull String path
, @NotNull ModuleSettingsImpl moduleSettings
) {
146 final ReplacePathToMacroMap map
= createCollapseMacroMap(PathMacrosImpl
.MODULE_DIR_MACRO_NAME
, moduleSettings
.getModuleFile().getParentFile());
147 return map
.substitute(path
, SystemInfo
.isFileSystemCaseSensitive
, null);
150 private static ReplacePathToMacroMap
createCollapseMacroMap(final String macroName
, final File dir
) {
151 ReplacePathToMacroMap map
= new ReplacePathToMacroMap();
152 map
.addMacroReplacement(FileUtil
.toSystemIndependentName(dir
.getAbsolutePath()), macroName
);
153 PathMacrosImpl
.getInstanceEx().addMacroReplacements(map
);
157 public Collection
<File
> getLibraryClassRoots(@NotNull String name
, @NotNull String level
) {
159 Element libraryElement
= null;
160 if (LibraryTablesRegistrar
.PROJECT_LEVEL
.equals(level
)) {
161 libraryElement
= findProjectLibraryElement(name
);
163 else if (LibraryTablesRegistrar
.APPLICATION_LEVEL
.equals(level
)) {
164 libraryElement
= findGlobalLibraryElement(name
);
167 if (libraryElement
!= null) {
168 return getClassRoots(libraryElement
, null);
171 return Collections
.emptyList();
173 catch (CannotConvertException e
) {
174 return Collections
.emptyList();
178 public List
<File
> getClassRoots(Element libraryElement
, ModuleSettingsImpl moduleSettings
) {
179 List
<File
> files
= new ArrayList
<File
>();
180 //todo[nik] support jar directories
181 final Element classesChild
= libraryElement
.getChild("CLASSES");
182 if (classesChild
!= null) {
183 final List
<Element
> roots
= JDomConvertingUtil
.getChildren(classesChild
, "root");
184 final ExpandMacroToPathMap pathMap
= createExpandMacroMap(moduleSettings
);
185 for (Element root
: roots
) {
186 final String url
= root
.getAttributeValue("url");
187 final String path
= VfsUtil
.urlToPath(url
);
188 files
.add(new File(PathUtil
.getLocalPath(pathMap
.substitute(path
, true, null))));
194 public ComponentManagerSettings
getCompilerSettings() {
195 if (myCompilerManagerSettings
== null) {
196 myCompilerManagerSettings
= createProjectSettings("compiler.xml");
198 return myCompilerManagerSettings
;
201 public ComponentManagerSettings
getProjectRootManagerSettings() {
202 if (myProjectRootManagerSettings
== null) {
203 myProjectRootManagerSettings
= createProjectSettings("misc.xml");
205 return myProjectRootManagerSettings
;
209 private ComponentManagerSettingsImpl
createProjectSettings(final String fileName
) {
212 if (myStorageScheme
== StorageScheme
.DEFAULT
) {
213 file
= myProjectFile
;
216 file
= new File(mySettingsBaseDir
, fileName
);
218 return new ComponentManagerSettingsImpl(file
, this);
220 catch (CannotConvertException e
) {
227 private Element
findGlobalLibraryElement(String name
) throws CannotConvertException
{
228 final File file
= PathManager
.getOptionsFile("applicationLibraries");
230 final Element root
= JDomConvertingUtil
.loadDocument(file
).getRootElement();
231 final Element libraryTable
= JDomConvertingUtil
.findComponent(root
, "libraryTable");
232 if (libraryTable
!= null) {
233 return findLibraryInTable(libraryTable
, name
);
240 private Element
findProjectLibraryElement(String name
) throws CannotConvertException
{
241 if (myStorageScheme
== StorageScheme
.DEFAULT
) {
242 final Element tableElement
= getProjectSettings().getComponentElement("libraryTable");
243 if (tableElement
!= null) {
244 return findLibraryInTable(tableElement
, name
);
248 File libraryFile
= new File(new File(mySettingsBaseDir
, "libraries"), name
+ ".xml");
249 if (libraryFile
.exists()) {
250 return JDomConvertingUtil
.loadDocument(libraryFile
).getRootElement().getChild(LibraryImpl
.ELEMENT
);
257 private Element
findLibraryInTable(Element tableElement
, String name
) {
258 final Condition
<Element
> filter
= JDomConvertingUtil
.createElementWithAttributeFilter(LibraryImpl
.ELEMENT
,
259 LibraryImpl
.LIBRARY_NAME_ATTR
, name
);
260 return JDomConvertingUtil
.findChild(tableElement
, filter
);
263 private ExpandMacroToPathMap
createExpandMacroMap() {
264 final ExpandMacroToPathMap macros
= new ExpandMacroToPathMap();
265 final String projectDir
= FileUtil
.toSystemIndependentName(myProjectBaseDir
.getAbsolutePath());
266 macros
.addMacroExpand(PathMacrosImpl
.PROJECT_DIR_MACRO_NAME
, projectDir
);
267 PathMacrosImpl
.getInstanceEx().addMacroExpands(macros
);
271 public File
getSettingsBaseDir() {
272 return mySettingsBaseDir
;
275 public File
getProjectFile() {
276 return myProjectFile
;
279 public ProjectSettings
getProjectSettings() throws CannotConvertException
{
280 if (myProjectSettings
== null) {
281 myProjectSettings
= new ProjectSettingsImpl(myProjectFile
, this);
283 return myProjectSettings
;
286 public RunManagerSettingsImpl
getRunManagerSettings() throws CannotConvertException
{
287 if (myRunManagerSettings
== null) {
288 if (myStorageScheme
== StorageScheme
.DEFAULT
) {
289 myRunManagerSettings
= new RunManagerSettingsImpl(myWorkspaceFile
, myProjectFile
, null, this);
292 final File
[] files
= new File(mySettingsBaseDir
, "runConfigurations").listFiles(new FileFilter() {
293 public boolean accept(File file
) {
294 return !file
.isDirectory() && file
.getName().endsWith(".xml");
297 myRunManagerSettings
= new RunManagerSettingsImpl(myWorkspaceFile
, null, files
, this);
300 return myRunManagerSettings
;
303 public WorkspaceSettings
getWorkspaceSettings() throws CannotConvertException
{
304 if (myWorkspaceSettings
== null) {
305 myWorkspaceSettings
= new WorkspaceSettingsImpl(myWorkspaceFile
, this);
307 return myWorkspaceSettings
;
311 public ModuleSettings
getModuleSettings(File moduleFile
) throws CannotConvertException
{
312 ModuleSettingsImpl settings
= myModuleSettingsMap
.get(moduleFile
);
313 if (settings
== null) {
314 settings
= new ModuleSettingsImpl(moduleFile
, this);
315 myModuleSettingsMap
.put(moduleFile
, settings
);
320 public List
<File
> getNonExistingModuleFiles() {
321 return myNonExistingModuleFiles
;
324 public StorageScheme
getStorageScheme() {
325 return myStorageScheme
;
328 public File
getWorkspaceFile() {
329 return myWorkspaceFile
;
332 public void saveFiles(Collection
<File
> files
) throws IOException
{
333 for (File file
: files
) {
334 final SettingsXmlFile xmlFile
= mySettingsFiles
.get(file
);
335 if (xmlFile
!= null) {
341 public SettingsXmlFile
getOrCreateFile(File file
) throws CannotConvertException
{
342 SettingsXmlFile settingsFile
= mySettingsFiles
.get(file
);
343 if (settingsFile
== null) {
344 settingsFile
= new SettingsXmlFile(file
);
345 mySettingsFiles
.put(file
, settingsFile
);