diff --git a/platform/analysis-impl/src/com/intellij/profile/codeInspection/InspectionProfileLoadUtil.java b/platform/analysis-impl/src/com/intellij/profile/codeInspection/InspectionProfileLoadUtil.java new file mode 100644 index 000000000000..2d3598574540 --- /dev/null +++ b/platform/analysis-impl/src/com/intellij/profile/codeInspection/InspectionProfileLoadUtil.java @@ -0,0 +1,76 @@ +/* + * Copyright 2000-2013 JetBrains s.r.o. + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package com.intellij.profile.codeInspection; + +import com.intellij.codeInspection.ex.InspectionProfileImpl; +import com.intellij.codeInspection.ex.InspectionToolRegistrar; +import com.intellij.openapi.util.InvalidDataException; +import com.intellij.openapi.util.JDOMUtil; +import com.intellij.openapi.util.io.FileUtil; +import com.intellij.profile.Profile; +import com.intellij.profile.ProfileManager; +import org.jdom.Document; +import org.jdom.Element; +import org.jdom.JDOMException; +import org.jetbrains.annotations.NonNls; +import org.jetbrains.annotations.NotNull; +import org.jetbrains.annotations.Nullable; + +import java.io.File; +import java.io.IOException; + +public class InspectionProfileLoadUtil { + @NonNls private static final String PROFILE_NAME_TAG = "profile_name"; + @NonNls public static final String PROFILE_TAG = "profile"; + + private static String getProfileName(@NotNull File file, @NotNull Document document) { + String name = getRootElementAttribute(PROFILE_NAME_TAG, document); + if (name != null) return name; + return FileUtil.getNameWithoutExtension(file); + } + + private static String getRootElementAttribute(@NotNull Document document, @NonNls String name) { + Element root = document.getRootElement(); + return root.getAttributeValue(name); + } + + @Nullable + private static String getRootElementAttribute(@NonNls String name, final Document doc) { + return getRootElementAttribute(doc, name); + } + + @NotNull + static String getProfileName(@NotNull Document document) { + String name = getRootElementAttribute(document, PROFILE_NAME_TAG); + if (name != null) return name; + return "unnamed"; + } + + @NotNull + public static Profile load(@NotNull File file, + @NotNull InspectionToolRegistrar registrar, + @NotNull ProfileManager profileManager) throws JDOMException, IOException, InvalidDataException { + Document document = JDOMUtil.loadDocument(file); + InspectionProfileImpl profile = new InspectionProfileImpl(getProfileName(file, document), registrar, profileManager); + Element rootElement = document.getRootElement(); + final Element profileElement = rootElement.getChild(PROFILE_TAG); + if (profileElement != null) { + rootElement = profileElement; + } + profile.readExternal(rootElement); + return profile; + } +} diff --git a/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProfileManagerImpl.java b/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProfileManagerImpl.java index 711501b3f0d6..477a7b7aa999 100644 --- a/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProfileManagerImpl.java +++ b/platform/lang-impl/src/com/intellij/profile/codeInspection/InspectionProfileManagerImpl.java @@ -43,7 +43,6 @@ import com.intellij.openapi.project.Project; import com.intellij.openapi.project.ProjectManager; import com.intellij.openapi.ui.Messages; import com.intellij.openapi.util.*; -import com.intellij.openapi.util.io.FileUtil; import com.intellij.profile.Profile; import com.intellij.util.ArrayUtil; import com.intellij.util.ui.UIUtil; @@ -52,7 +51,6 @@ import org.jdom.Element; import org.jdom.JDOMException; import org.jetbrains.annotations.NonNls; import org.jetbrains.annotations.NotNull; -import org.jetbrains.annotations.Nullable; import org.jetbrains.annotations.TestOnly; import java.io.File; @@ -66,7 +64,6 @@ import java.util.concurrent.atomic.AtomicBoolean; */ public class InspectionProfileManagerImpl extends InspectionProfileManager implements SeverityProvider, ExportableComponent, JDOMExternalizable, NamedComponent { - @NonNls private static final String PROFILE_NAME_TAG = "profile_name"; private final InspectionToolRegistrar myRegistrar; private final SchemesManager mySchemesManager; @@ -89,8 +86,8 @@ public class InspectionProfileManagerImpl extends InspectionProfileManager imple SchemeProcessor processor = new BaseSchemeProcessor() { @Override public InspectionProfileImpl readScheme(final Document document) { - InspectionProfileImpl profile = new InspectionProfileImpl(getProfileName(document), myRegistrar, InspectionProfileManagerImpl.this); - load(profile, document.getRootElement()); + InspectionProfileImpl profile = new InspectionProfileImpl(InspectionProfileLoadUtil.getProfileName(document), myRegistrar, InspectionProfileManagerImpl.this); + read(profile, document.getRootElement()); return profile; } @@ -130,7 +127,7 @@ public class InspectionProfileManagerImpl extends InspectionProfileManager imple mySchemesManager = schemesManagerFactory.createSchemesManager(FILE_SPEC, processor, RoamingType.PER_USER); } - private static void load(final InspectionProfileImpl profile, @NotNull Element element) { + private static void read(@NotNull final InspectionProfileImpl profile, @NotNull Element element) { try { profile.readExternal(element); } @@ -226,49 +223,28 @@ public class InspectionProfileManagerImpl extends InspectionProfileManager imple public Profile loadProfile(@NotNull String path) throws IOException, JDOMException { final File file = new File(path); if (file.exists()){ - InspectionProfileImpl profile = new InspectionProfileImpl(getProfileName(file), myRegistrar, this); - Element rootElement = JDOMUtil.loadDocument(file).getRootElement(); - final Element profileElement = rootElement.getChild("profile"); - if (profileElement != null) { - rootElement = profileElement; + try { + return InspectionProfileLoadUtil.load(file, myRegistrar, this); + } + catch (IOException e) { + throw e; + } + catch (JDOMException e) { + throw e; + } + catch (Exception e) { + ApplicationManager.getApplication().invokeLater(new Runnable() { + @Override + public void run() { + Messages.showErrorDialog(InspectionsBundle.message("inspection.error.loading.message", 0, file), + InspectionsBundle.message("inspection.errors.occurred.dialog.title")); + } + }, ModalityState.NON_MODAL); } - load(profile, rootElement); - return profile; } return getProfile(path, false); } - private static String getProfileName(Document document) { - String name = getRootElementAttribute(document, PROFILE_NAME_TAG); - if (name != null) return name; - return "unnamed"; - } - - private static String getProfileName(File file) { - String name = getRootElementAttribute(file, PROFILE_NAME_TAG); - if (name != null) return name; - return FileUtil.getNameWithoutExtension(file); - } - - private static String getRootElementAttribute(final Document document, @NonNls String name) { - Element root = document.getRootElement(); - return root.getAttributeValue(name); - } - - @Nullable - private static String getRootElementAttribute(final File file, @NonNls String name) { - try { - Document doc = JDOMUtil.loadDocument(file); - return getRootElementAttribute(doc, name); - } - catch (JDOMException e) { - return null; - } - catch (IOException e) { - return null; - } - } - @Override public void updateProfile(@NotNull Profile profile) { mySchemesManager.addNewScheme(profile, true); @@ -331,9 +307,7 @@ public class InspectionProfileManagerImpl extends InspectionProfileManager imple if (returnRootProfileIfNamedIsAbsent) { return getRootProfile(); } - else { - return null; - } + return null; } @NotNull @@ -371,6 +345,7 @@ public class InspectionProfileManagerImpl extends InspectionProfileManager imple return getProfile(name, true); } + @NotNull public SchemesManager getSchemesManager() { return mySchemesManager; }