named module libraries supported in artifacts
[fedora-idea.git] / platform / lang-impl / src / com / intellij / conversion / impl / ConversionContextImpl.java
blob0a649bea3779ea77c9c45bcb9620e880011e6af4
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;
26 import java.io.File;
27 import java.io.FileFilter;
28 import java.io.IOException;
29 import java.util.*;
31 /**
32 * @author nik
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> myFile2ModuleSettings = new HashMap<File, ModuleSettingsImpl>();
46 private Map<String, ModuleSettingsImpl> myName2ModuleSettings = new HashMap<String, ModuleSettingsImpl>();
47 private RunManagerSettingsImpl myRunManagerSettings;
48 private File mySettingsBaseDir;
49 private ComponentManagerSettings myCompilerManagerSettings;
50 private ComponentManagerSettings myProjectRootManagerSettings;
52 public ConversionContextImpl(String projectPath) throws CannotConvertException {
53 myProjectFile = new File(projectPath);
55 File modulesFile;
56 if (myProjectFile.isDirectory()) {
57 myStorageScheme = StorageScheme.DIRECTORY_BASED;
58 myProjectBaseDir = myProjectFile;
59 mySettingsBaseDir = new File(myProjectBaseDir.getAbsolutePath(), ".idea");
60 modulesFile = new File(mySettingsBaseDir, "modules.xml");
61 myWorkspaceFile = new File(mySettingsBaseDir, "workspace.xml");
63 else {
64 myStorageScheme = StorageScheme.DEFAULT;
65 myProjectBaseDir = myProjectFile.getParentFile();
66 modulesFile = myProjectFile;
67 myWorkspaceFile = new File(StringUtil.trimEnd(projectPath, ProjectFileType.DOT_DEFAULT_EXTENSION) + WorkspaceFileType.DOT_DEFAULT_EXTENSION);
70 myModuleFiles = findModuleFiles(JDomConvertingUtil.loadDocument(modulesFile).getRootElement());
73 public Set<File> getAllProjectFiles() {
74 final HashSet<File> files = new HashSet<File>(Arrays.asList(myModuleFiles));
75 if (myStorageScheme == StorageScheme.DEFAULT) {
76 files.add(myProjectFile);
77 files.add(myWorkspaceFile);
79 else {
80 addFilesRecursively(mySettingsBaseDir, files);
82 return files;
85 private static void addFilesRecursively(File file, Set<File> files) {
86 if (file.isDirectory()) {
87 final File[] children = file.listFiles();
88 if (children != null) {
89 for (File child : children) {
90 addFilesRecursively(child, files);
94 else if (StringUtil.endsWithIgnoreCase(file.getName(), ".xml") && !file.getName().startsWith(".")) {
95 files.add(file);
99 @NotNull
100 public File getProjectBaseDir() {
101 return myProjectBaseDir;
104 public File[] getModuleFiles() {
105 return myModuleFiles;
108 private File[] findModuleFiles(final Element root) {
109 final Element modulesManager = JDomConvertingUtil.findComponent(root, ModuleManagerImpl.COMPONENT_NAME);
110 if (modulesManager == null) return new File[0];
112 final Element modules = modulesManager.getChild(ModuleManagerImpl.ELEMENT_MODULES);
113 if (modules == null) return new File[0];
115 final ExpandMacroToPathMap macros = createExpandMacroMap();
117 List<File> files = new ArrayList<File>();
118 for (Element module : JDomConvertingUtil.getChildren(modules, ModuleManagerImpl.ELEMENT_MODULE)) {
119 String filePath = module.getAttributeValue(ModuleManagerImpl.ATTRIBUTE_FILEPATH);
120 filePath = macros.substitute(filePath, true);
121 files.add(new File(FileUtil.toSystemDependentName(filePath)));
123 return files.toArray(new File[files.size()]);
126 @NotNull
127 public String expandPath(@NotNull String path, @NotNull ModuleSettingsImpl moduleSettings) {
128 return createExpandMacroMap(moduleSettings).substitute(path, true);
131 private ExpandMacroToPathMap createExpandMacroMap(@Nullable ModuleSettingsImpl moduleSettings) {
132 final ExpandMacroToPathMap map = createExpandMacroMap();
133 if (moduleSettings != null) {
134 final String modulePath = FileUtil.toSystemIndependentName(moduleSettings.getModuleFile().getParentFile().getAbsolutePath());
135 map.addMacroExpand(PathMacrosImpl.MODULE_DIR_MACRO_NAME, modulePath);
137 return map;
140 @NotNull
141 public String collapsePath(@NotNull String path) {
142 ReplacePathToMacroMap map = createCollapseMacroMap(PathMacrosImpl.PROJECT_DIR_MACRO_NAME, myProjectBaseDir);
143 return map.substitute(path, SystemInfo.isFileSystemCaseSensitive);
146 public String collapsePath(@NotNull String path, @NotNull ModuleSettingsImpl moduleSettings) {
147 final ReplacePathToMacroMap map = createCollapseMacroMap(PathMacrosImpl.MODULE_DIR_MACRO_NAME, moduleSettings.getModuleFile().getParentFile());
148 return map.substitute(path, SystemInfo.isFileSystemCaseSensitive);
151 private static ReplacePathToMacroMap createCollapseMacroMap(final String macroName, final File dir) {
152 ReplacePathToMacroMap map = new ReplacePathToMacroMap();
153 map.addMacroReplacement(FileUtil.toSystemIndependentName(dir.getAbsolutePath()), macroName);
154 PathMacrosImpl.getInstanceEx().addMacroReplacements(map);
155 return map;
158 public Collection<File> getLibraryClassRoots(@NotNull String name, @NotNull String level) {
159 try {
160 Element libraryElement = null;
161 if (LibraryTablesRegistrar.PROJECT_LEVEL.equals(level)) {
162 libraryElement = findProjectLibraryElement(name);
164 else if (LibraryTablesRegistrar.APPLICATION_LEVEL.equals(level)) {
165 libraryElement = findGlobalLibraryElement(name);
168 if (libraryElement != null) {
169 return getClassRoots(libraryElement, null);
172 return Collections.emptyList();
174 catch (CannotConvertException e) {
175 return Collections.emptyList();
179 @NotNull
180 public List<File> getClassRoots(Element libraryElement, ModuleSettingsImpl moduleSettings) {
181 List<File> files = new ArrayList<File>();
182 //todo[nik] support jar directories
183 final Element classesChild = libraryElement.getChild("CLASSES");
184 if (classesChild != null) {
185 final List<Element> roots = JDomConvertingUtil.getChildren(classesChild, "root");
186 final ExpandMacroToPathMap pathMap = createExpandMacroMap(moduleSettings);
187 for (Element root : roots) {
188 final String url = root.getAttributeValue("url");
189 final String path = VfsUtil.urlToPath(url);
190 files.add(new File(PathUtil.getLocalPath(pathMap.substitute(path, true))));
193 return files;
196 public ComponentManagerSettings getCompilerSettings() {
197 if (myCompilerManagerSettings == null) {
198 myCompilerManagerSettings = createProjectSettings("compiler.xml");
200 return myCompilerManagerSettings;
203 public ComponentManagerSettings getProjectRootManagerSettings() {
204 if (myProjectRootManagerSettings == null) {
205 myProjectRootManagerSettings = createProjectSettings("misc.xml");
207 return myProjectRootManagerSettings;
210 @Nullable
211 private ComponentManagerSettingsImpl createProjectSettings(final String fileName) {
212 try {
213 File file;
214 if (myStorageScheme == StorageScheme.DEFAULT) {
215 file = myProjectFile;
217 else {
218 file = new File(mySettingsBaseDir, fileName);
220 return new ComponentManagerSettingsImpl(file, this);
222 catch (CannotConvertException e) {
223 LOG.info(e);
224 return null;
228 @Nullable
229 private Element findGlobalLibraryElement(String name) throws CannotConvertException {
230 final File file = PathManager.getOptionsFile("applicationLibraries");
231 if (file.exists()) {
232 final Element root = JDomConvertingUtil.loadDocument(file).getRootElement();
233 final Element libraryTable = JDomConvertingUtil.findComponent(root, "libraryTable");
234 if (libraryTable != null) {
235 return findLibraryInTable(libraryTable, name);
238 return null;
241 @Nullable
242 private Element findProjectLibraryElement(String name) throws CannotConvertException {
243 if (myStorageScheme == StorageScheme.DEFAULT) {
244 final Element tableElement = getProjectSettings().getComponentElement("libraryTable");
245 if (tableElement != null) {
246 return findLibraryInTable(tableElement, name);
249 else {
250 File libraryFile = new File(new File(mySettingsBaseDir, "libraries"), name + ".xml");
251 if (libraryFile.exists()) {
252 return JDomConvertingUtil.loadDocument(libraryFile).getRootElement().getChild(LibraryImpl.ELEMENT);
255 return null;
258 @Nullable
259 private Element findLibraryInTable(Element tableElement, String name) {
260 final Condition<Element> filter = JDomConvertingUtil.createElementWithAttributeFilter(LibraryImpl.ELEMENT,
261 LibraryImpl.LIBRARY_NAME_ATTR, name);
262 return JDomConvertingUtil.findChild(tableElement, filter);
265 private ExpandMacroToPathMap createExpandMacroMap() {
266 final ExpandMacroToPathMap macros = new ExpandMacroToPathMap();
267 final String projectDir = FileUtil.toSystemIndependentName(myProjectBaseDir.getAbsolutePath());
268 macros.addMacroExpand(PathMacrosImpl.PROJECT_DIR_MACRO_NAME, projectDir);
269 PathMacrosImpl.getInstanceEx().addMacroExpands(macros);
270 return macros;
273 public File getSettingsBaseDir() {
274 return mySettingsBaseDir;
277 public File getProjectFile() {
278 return myProjectFile;
281 public ProjectSettings getProjectSettings() throws CannotConvertException {
282 if (myProjectSettings == null) {
283 myProjectSettings = new ProjectSettingsImpl(myProjectFile, this);
285 return myProjectSettings;
288 public RunManagerSettingsImpl getRunManagerSettings() throws CannotConvertException {
289 if (myRunManagerSettings == null) {
290 if (myStorageScheme == StorageScheme.DEFAULT) {
291 myRunManagerSettings = new RunManagerSettingsImpl(myWorkspaceFile, myProjectFile, null, this);
293 else {
294 final File[] files = new File(mySettingsBaseDir, "runConfigurations").listFiles(new FileFilter() {
295 public boolean accept(File file) {
296 return !file.isDirectory() && file.getName().endsWith(".xml");
299 myRunManagerSettings = new RunManagerSettingsImpl(myWorkspaceFile, null, files, this);
302 return myRunManagerSettings;
305 public WorkspaceSettings getWorkspaceSettings() throws CannotConvertException {
306 if (myWorkspaceSettings == null) {
307 myWorkspaceSettings = new WorkspaceSettingsImpl(myWorkspaceFile, this);
309 return myWorkspaceSettings;
313 public ModuleSettings getModuleSettings(File moduleFile) throws CannotConvertException {
314 ModuleSettingsImpl settings = myFile2ModuleSettings.get(moduleFile);
315 if (settings == null) {
316 settings = new ModuleSettingsImpl(moduleFile, this);
317 myFile2ModuleSettings.put(moduleFile, settings);
318 myName2ModuleSettings.put(settings.getModuleName(), settings);
320 return settings;
323 public ModuleSettings getModuleSettings(@NotNull String moduleName) {
324 if (!myName2ModuleSettings.containsKey(moduleName)) {
325 for (File moduleFile : myModuleFiles) {
326 try {
327 getModuleSettings(moduleFile);
329 catch (CannotConvertException ignored) {
333 return myName2ModuleSettings.get(moduleName);
336 public List<File> getNonExistingModuleFiles() {
337 return myNonExistingModuleFiles;
340 public StorageScheme getStorageScheme() {
341 return myStorageScheme;
344 public File getWorkspaceFile() {
345 return myWorkspaceFile;
348 public void saveFiles(Collection<File> files) throws IOException {
349 for (File file : files) {
350 final SettingsXmlFile xmlFile = mySettingsFiles.get(file);
351 if (xmlFile != null) {
352 xmlFile.save();
357 public SettingsXmlFile getOrCreateFile(File file) throws CannotConvertException {
358 SettingsXmlFile settingsFile = mySettingsFiles.get(file);
359 if (settingsFile == null) {
360 settingsFile = new SettingsXmlFile(file);
361 mySettingsFiles.put(file, settingsFile);
363 return settingsFile;