2 * Copyright 2000-2009 JetBrains s.r.o.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package com
.intellij
.codeInsight
.daemon
;
19 import com
.intellij
.codeHighlighting
.HighlightDisplayLevel
;
20 import com
.intellij
.codeInspection
.InspectionProfile
;
21 import com
.intellij
.codeInspection
.InspectionProfileEntry
;
22 import com
.intellij
.codeInspection
.ModifiableModel
;
23 import com
.intellij
.codeInspection
.ex
.InspectionProfileImpl
;
24 import com
.intellij
.openapi
.diagnostic
.Logger
;
25 import com
.intellij
.openapi
.util
.JDOMUtil
;
26 import com
.intellij
.profile
.codeInspection
.InspectionProfileManager
;
27 import com
.intellij
.psi
.search
.scope
.packageSet
.NamedScope
;
28 import com
.intellij
.util
.SystemProperties
;
29 import org
.jdom
.Document
;
30 import org
.jdom
.Element
;
31 import org
.jdom
.JDOMException
;
32 import org
.jetbrains
.annotations
.NonNls
;
33 import org
.jetbrains
.annotations
.Nullable
;
36 import java
.io
.FileFilter
;
37 import java
.io
.IOException
;
38 import java
.util
.HashMap
;
44 public class InspectionProfileConvertor
{
45 private final HashMap
<String
, HighlightDisplayLevel
> myDisplayLevelMap
= new HashMap
<String
, HighlightDisplayLevel
>();
46 public static final @NonNls String OLD_HIGHTLIGHTING_SETTINGS_PROFILE
= "EditorHighlightingSettings";
47 public static final @NonNls String OLD_DEFAUL_PROFILE
= "OldDefaultProfile";
49 private static final Logger LOG
= Logger
.getInstance("#com.intellij.codeInsight.daemon.DaemonCodeAnalyzerSettingsConvertor");
51 @NonNls private static final String INSPECTIONS_TAG
= "inspections";
52 @NonNls private static final String NAME_ATT
= "name";
53 @NonNls private static final String INSP_TOOL_TAG
= "inspection_tool";
54 @NonNls private static final String CLASS_ATT
= "class";
55 @NonNls private static final String VERSION_ATT
= "version";
56 @NonNls private static final String PROFILE_NAME_ATT
= "profile_name";
57 @NonNls private static final String OPTION_TAG
= "option";
58 @NonNls private static final String DISPLAY_LEVEL_MAP_OPTION
= "DISPLAY_LEVEL_MAP";
59 @NonNls protected static final String VALUE_ATT
= "value";
60 @NonNls private static final String DEFAULT_XML
= "Default.xml";
61 @NonNls private static final String XML_EXTENSION
= ".xml";
62 @NonNls public static final String LEVEL_ATT
= "level";
63 private final InspectionProfileManager myManager
;
65 public InspectionProfileConvertor(InspectionProfileManager manager
) {
67 renameOldDefaultsProfile();
70 private boolean retrieveOldSettings(Element element
) {
71 boolean hasOldSettings
= false;
72 for (final Object obj
: element
.getChildren(OPTION_TAG
)) {
73 Element option
= (Element
)obj
;
74 final String name
= option
.getAttributeValue(NAME_ATT
);
76 hasOldSettings
|= processElement(option
, name
);
79 return hasOldSettings
;
82 protected boolean processElement(final Element option
, final String name
) {
83 if (name
.equals(DISPLAY_LEVEL_MAP_OPTION
)) {
84 final Element levelMap
= option
.getChild(VALUE_ATT
);
85 for (final Object o
: levelMap
.getChildren()) {
86 Element e
= (Element
)o
;
87 String key
= e
.getName();
88 String levelName
= e
.getAttributeValue(LEVEL_ATT
);
89 HighlightDisplayLevel level
= HighlightDisplayLevel
.find(myManager
.getSeverityRegistrar().getSeverity(levelName
));
90 if (level
== null) continue;
91 myDisplayLevelMap
.put(key
, level
);
101 public void storeEditorHighlightingProfile(Element element
) {
102 if (retrieveOldSettings(element
)) {
103 final InspectionProfileImpl editorProfile
= new InspectionProfileImpl(OLD_HIGHTLIGHTING_SETTINGS_PROFILE
);
105 final ModifiableModel editorProfileModel
= editorProfile
.getModifiableModel();
107 fillErrorLevels(editorProfileModel
);
109 editorProfileModel
.commit();
111 catch (IOException e
) {
117 public static Element
convertToNewFormat(Element profileFile
, InspectionProfile profile
) throws IOException
, JDOMException
{
118 Element rootElement
= new Element(INSPECTIONS_TAG
);
119 rootElement
.setAttribute(NAME_ATT
, profile
.getName());
120 final InspectionProfileEntry
[] tools
= profile
.getInspectionTools(null);
121 for (final Object o
: profileFile
.getChildren(INSP_TOOL_TAG
)) {
122 Element toolElement
= (Element
)((Element
)o
).clone();
123 String toolClassName
= toolElement
.getAttributeValue(CLASS_ATT
);
124 final String shortName
= convertToShortName(toolClassName
, tools
);
125 if (shortName
== null) {
128 toolElement
.setAttribute(CLASS_ATT
, shortName
);
129 rootElement
.addContent(toolElement
);
134 private static void renameOldDefaultsProfile() {
135 final File profileDirectory
= InspectionProfileManager
.getProfileDirectory();
136 if (profileDirectory
== null) return;
137 final File
[] files
= profileDirectory
.listFiles(new FileFilter() {
138 public boolean accept(File pathname
) {
139 return pathname
.getPath().endsWith(File
.separator
+ DEFAULT_XML
);
142 if (files
== null || files
.length
!= 1) {
145 final File dest
= new File(profileDirectory
, OLD_DEFAUL_PROFILE
+ XML_EXTENSION
);
147 Document doc
= JDOMUtil
.loadDocument(files
[0]);
148 Element root
= doc
.getRootElement();
149 if (root
.getAttributeValue(VERSION_ATT
) == null){
150 root
.setAttribute(PROFILE_NAME_ATT
, OLD_DEFAUL_PROFILE
);
151 JDOMUtil
.writeDocument(doc
, dest
, SystemProperties
.getLineSeparator());
155 catch (IOException e
) {
158 catch (JDOMException e
) {
163 protected void fillErrorLevels(final ModifiableModel profile
) {
164 InspectionProfileEntry
[] tools
= profile
.getInspectionTools(null);
165 LOG
.assertTrue(tools
!= null, "Profile was not correctly init");
167 for (final String shortName
: myDisplayLevelMap
.keySet()) {
169 HighlightDisplayLevel level
= myDisplayLevelMap
.get(shortName
);
171 HighlightDisplayKey key
= HighlightDisplayKey
.find(shortName
);
173 if (key
== null) continue;
175 //set up tools for default profile
176 if (level
!= HighlightDisplayLevel
.DO_NOT_SHOW
) {
177 profile
.enableTool(shortName
, (NamedScope
)null);
180 if (level
== null || level
== HighlightDisplayLevel
.DO_NOT_SHOW
) {
181 level
= HighlightDisplayLevel
.WARNING
;
183 profile
.setErrorLevel(key
, level
);
189 private static String
convertToShortName(String displayName
, InspectionProfileEntry
[] tools
) {
190 if (displayName
== null) return null;
191 for (InspectionProfileEntry tool
: tools
) {
192 if (displayName
.equals(tool
.getDisplayName())) {
193 return tool
.getShortName();