1 package com
.intellij
.conversion
.impl
;
3 import com
.intellij
.conversion
.CannotConvertException
;
4 import com
.intellij
.conversion
.ComponentManagerSettings
;
5 import com
.intellij
.conversion
.ModuleSettings
;
6 import com
.intellij
.facet
.FacetManagerImpl
;
7 import com
.intellij
.ide
.highlighter
.ModuleFileType
;
8 import com
.intellij
.ide
.impl
.convert
.JDomConvertingUtil
;
9 import com
.intellij
.openapi
.module
.impl
.ModuleImpl
;
10 import com
.intellij
.openapi
.roots
.impl
.*;
11 import com
.intellij
.openapi
.roots
.impl
.ModuleLibraryOrderEntryImpl
;
12 import com
.intellij
.openapi
.roots
.impl
.libraries
.LibraryImpl
;
13 import com
.intellij
.openapi
.util
.text
.StringUtil
;
14 import com
.intellij
.openapi
.util
.io
.FileUtil
;
15 import com
.intellij
.openapi
.vfs
.VfsUtil
;
16 import org
.jdom
.Element
;
17 import org
.jetbrains
.annotations
.NonNls
;
18 import org
.jetbrains
.annotations
.NotNull
;
19 import org
.jetbrains
.annotations
.Nullable
;
22 import java
.io
.IOException
;
23 import java
.util
.ArrayList
;
24 import java
.util
.Collection
;
25 import java
.util
.Collections
;
26 import java
.util
.List
;
31 public class ModuleSettingsImpl
extends ComponentManagerSettingsImpl
implements ModuleSettings
{
32 private String myModuleName
;
33 @NonNls private static final String MODULE_ROOT_MANAGER_COMPONENT
= "NewModuleRootManager";
35 public ModuleSettingsImpl(File moduleFile
, ConversionContextImpl context
) throws CannotConvertException
{
36 super(moduleFile
, context
);
37 myModuleName
= StringUtil
.trimEnd(moduleFile
.getName(), ModuleFileType
.DOT_DEFAULT_EXTENSION
);
41 public String
getModuleName() {
46 public String
getModuleType() {
47 return getRootElement().getAttributeValue(ModuleImpl
.ELEMENT_TYPE
);
51 public File
getModuleFile() {
52 return mySettingsFile
.getFile();
56 public Collection
<?
extends Element
> getFacetElements(@NotNull String facetTypeId
) {
57 final Element facetManager
= getComponentElement(FacetManagerImpl
.COMPONENT_NAME
);
58 final ArrayList
<Element
> elements
= new ArrayList
<Element
>();
59 for (Element child
: JDomConvertingUtil
.getChildren(facetManager
, FacetManagerImpl
.FACET_ELEMENT
)) {
60 if (facetTypeId
.equals(child
.getAttributeValue(FacetManagerImpl
.TYPE_ATTRIBUTE
))) {
67 public void setModuleType(@NotNull String moduleType
) {
68 getRootElement().setAttribute(ModuleImpl
.ELEMENT_TYPE
, moduleType
);
72 public String
expandPath(@NotNull String path
) {
73 return myContext
.expandPath(path
, this);
77 public Collection
<File
> getSourceRoots(boolean includeTests
) {
78 final List
<File
> result
= new ArrayList
<File
>();
79 for (Element contentRoot
: getContentRootElements()) {
80 for (Element sourceFolder
: JDomConvertingUtil
.getChildren(contentRoot
, SourceFolderImpl
.ELEMENT_NAME
)) {
81 boolean isTestFolder
= Boolean
.parseBoolean(sourceFolder
.getAttributeValue(SourceFolderImpl
.TEST_SOURCE_ATTR
));
82 if (includeTests
|| !isTestFolder
) {
83 result
.add(getFile(sourceFolder
.getAttributeValue(SourceFolderImpl
.URL_ATTRIBUTE
)));
90 private List
<Element
> getContentRootElements() {
91 return JDomConvertingUtil
.getChildren(getComponentElement(MODULE_ROOT_MANAGER_COMPONENT
), ContentEntryImpl
.ELEMENT_NAME
);
95 public Collection
<File
> getContentRoots() {
96 final List
<File
> result
= new ArrayList
<File
>();
97 for (Element contentRoot
: getContentRootElements()) {
98 String path
= VfsUtil
.urlToPath(contentRoot
.getAttributeValue(ContentEntryImpl
.URL_ATTRIBUTE
));
99 result
.add(new File(FileUtil
.toSystemDependentName(expandPath(path
))));
104 public void addExcludedFolder(@NotNull File directory
) {
105 final ComponentManagerSettings rootManagerSettings
= myContext
.getProjectRootManagerSettings();
106 if (rootManagerSettings
!= null) {
107 final Element projectRootManager
= rootManagerSettings
.getComponentElement("ProjectRootManager");
108 if (projectRootManager
!= null) {
109 final Element outputElement
= projectRootManager
.getChild("output");
110 if (outputElement
!= null) {
111 final String outputUrl
= outputElement
.getAttributeValue("url");
112 if (outputUrl
!= null) {
113 final File outputFile
= getFile(outputUrl
);
115 if (FileUtil
.isAncestor(outputFile
, directory
, false)) {
119 catch (IOException ignored
) {
125 for (Element contentRoot
: getContentRootElements()) {
126 final File root
= getFile(contentRoot
.getAttributeValue(ContentEntryImpl
.URL_ATTRIBUTE
));
128 if (FileUtil
.isAncestor(root
, directory
, true)) {
129 addExcludedFolder(directory
, contentRoot
);
132 catch (IOException ignored
) {
137 public List
<File
> getModuleLibraryRootUrls(String libraryName
) {
138 final Element component
= getComponentElement(MODULE_ROOT_MANAGER_COMPONENT
);
139 for (Element element
: JDomConvertingUtil
.getChildren(component
, OrderEntryFactory
.ORDER_ENTRY_ELEMENT_NAME
)) {
140 if (ModuleLibraryOrderEntryImpl
.ENTRY_TYPE
.equals(element
.getAttributeValue(OrderEntryFactory
.ORDER_ENTRY_TYPE_ATTR
))) {
141 final Element library
= element
.getChild(LibraryImpl
.ELEMENT
);
142 if (library
!= null && libraryName
.equals(library
.getAttributeValue(LibraryImpl
.LIBRARY_NAME_ATTR
))) {
143 return myContext
.getClassRoots(library
, this);
147 return Collections
.emptyList();
150 private void addExcludedFolder(File directory
, Element contentRoot
) throws IOException
{
151 for (Element excludedFolder
: JDomConvertingUtil
.getChildren(contentRoot
, ExcludeFolderImpl
.ELEMENT_NAME
)) {
152 final File excludedDir
= getFile(excludedFolder
.getAttributeValue(ExcludeFolderImpl
.URL_ATTRIBUTE
));
153 if (FileUtil
.isAncestor(excludedDir
, directory
, false)) {
157 String path
= myContext
.collapsePath(FileUtil
.toSystemIndependentName(directory
.getAbsolutePath()), this);
158 contentRoot
.addContent(new Element(ExcludeFolderImpl
.ELEMENT_NAME
).setAttribute(ExcludeFolderImpl
.URL_ATTRIBUTE
, VfsUtil
.pathToUrl(path
)));
161 private File
getFile(String url
) {
162 return new File(FileUtil
.toSystemDependentName(expandPath(VfsUtil
.urlToPath(url
))));