summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/tools/ToolsProcessor.java
blob: 1efa7b05d19b870d44729e78e0dd7ff350d899a1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
/*
 * Copyright 2000-2014 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.tools;

import com.intellij.openapi.application.ApplicationManager;
import com.intellij.openapi.components.PathMacroManager;
import com.intellij.openapi.options.BaseSchemeProcessor;
import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.util.text.StringUtil;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;

import java.io.File;
import java.io.IOException;


abstract public class ToolsProcessor<T extends Tool> extends BaseSchemeProcessor<ToolsGroup<T>> {
  @NonNls private static final String TOOL_SET = "toolSet";
  @NonNls private static final String TOOL = "tool";
  @NonNls private static final String ATTRIBUTE_NAME = "name";
  @NonNls private static final String NAME = ATTRIBUTE_NAME;
  @NonNls private static final String DESCRIPTION = "description";
  @NonNls private static final String SHOW_IN_MAIN_MENU = "showInMainMenu";
  @NonNls private static final String SHOW_IN_EDITOR = "showInEditor";
  @NonNls private static final String SHOW_IN_PROJECT = "showInProject";
  @NonNls private static final String SHOW_IN_SEARCH_POPUP = "showInSearchPopup";
  @NonNls private static final String DISABLED = "disabled";
  @NonNls private static final String USE_CONSOLE = "useConsole";
  @NonNls private static final String SHOW_CONSOLE_ON_STDOUT = "showConsoleOnStdOut";
  @NonNls private static final String SHOW_CONSOLE_ON_STDERR = "showConsoleOnStdErr";
  @NonNls private static final String SYNCHRONIZE_AFTER_EXECUTION = "synchronizeAfterRun";
  @NonNls private static final String EXEC = "exec";
  @NonNls private static final String WORKING_DIRECTORY = "WORKING_DIRECTORY";
  @NonNls private static final String COMMAND = "COMMAND";
  @NonNls private static final String PARAMETERS = "PARAMETERS";
  @NonNls private static final String FILTER = "filter";
  @NonNls private static final String ELEMENT_OPTION = "option";
  @NonNls private static final String ATTRIBUTE_VALUE = "value";

  @NonNls private static final String APPLICATION_HOME_MACRO = "$APPLICATION_HOME_DIR$";

  @Override
  public ToolsGroup<T> readScheme(@NotNull final Document document) throws InvalidDataException, IOException, JDOMException {
    Element root = document.getRootElement();
    if (root == null || !TOOL_SET.equals(root.getName())) {
      throw new InvalidDataException();
    }

    String attrName = root.getAttributeValue(ATTRIBUTE_NAME);
    String groupName = StringUtil.isEmpty(attrName)? Tool.DEFAULT_GROUP_NAME : attrName;
    ToolsGroup<T> result = createToolsGroup(groupName);

    final PathMacroManager macroManager = PathMacroManager.getInstance(ApplicationManager.getApplication());

    for (final Object o : root.getChildren(TOOL)) {
      Element element = (Element)o;

      T tool = createTool();

      readToolAttributes(element, tool);

      Element exec = element.getChild(EXEC);
      if (exec != null) {
        for (final Object o1 : exec.getChildren(ELEMENT_OPTION)) {
          Element optionElement = (Element)o1;

          String name = optionElement.getAttributeValue(ATTRIBUTE_NAME);
          String value = optionElement.getAttributeValue(ATTRIBUTE_VALUE);

          if (WORKING_DIRECTORY.equals(name)) {
            if (value != null) {
              final String replace = macroManager.expandPath(value).replace('/', File.separatorChar);
              tool.setWorkingDirectory(replace);
            }
          }
          if (COMMAND.equals(name)) {
            tool.setProgram(macroManager.expandPath(ToolManager.convertString(value)));
          }
          if (PARAMETERS.equals(name)) {
            tool.setParameters(macroManager.expandPath(ToolManager.convertString(value)));
          }
        }
      }

      for (final Object o2 : element.getChildren(FILTER)) {
        Element childNode = (Element)o2;

        FilterInfo filterInfo = new FilterInfo();
        filterInfo.readExternal(childNode);
        tool.addOutputFilter(filterInfo);
      }

      tool.setGroup(groupName);
      result.addElement(tool);
    }

    return result;
  }

  protected void readToolAttributes(Element element, T tool) {
    tool.setName(ToolManager.convertString(element.getAttributeValue(NAME)));
    tool.setDescription(ToolManager.convertString(element.getAttributeValue(DESCRIPTION)));
    tool.setShownInMainMenu(Boolean.valueOf(element.getAttributeValue(SHOW_IN_MAIN_MENU)).booleanValue());
    tool.setShownInEditor(Boolean.valueOf(element.getAttributeValue(SHOW_IN_EDITOR)).booleanValue());
    tool.setShownInProjectViews(Boolean.valueOf(element.getAttributeValue(SHOW_IN_PROJECT)).booleanValue());
    tool.setShownInSearchResultsPopup(Boolean.valueOf(element.getAttributeValue(SHOW_IN_SEARCH_POPUP)).booleanValue());
    tool.setEnabled(!Boolean.valueOf(element.getAttributeValue(DISABLED)).booleanValue());
    tool.setUseConsole(Boolean.valueOf(element.getAttributeValue(USE_CONSOLE)).booleanValue());
    tool.setShowConsoleOnStdOut(Boolean.valueOf(element.getAttributeValue(SHOW_CONSOLE_ON_STDOUT)).booleanValue());
    tool.setShowConsoleOnStdErr(Boolean.valueOf(element.getAttributeValue(SHOW_CONSOLE_ON_STDERR)).booleanValue());
    tool.setFilesSynchronizedAfterRun(Boolean.valueOf(element.getAttributeValue(SYNCHRONIZE_AFTER_EXECUTION)).booleanValue());
  }

  protected abstract ToolsGroup<T> createToolsGroup(String groupName);

  protected abstract T createTool();

  @Override
  public Document writeScheme(@NotNull final ToolsGroup<T> scheme) throws WriteExternalException {
    Element groupElement = new Element(TOOL_SET);
    if (scheme.getName() != null) {
      groupElement.setAttribute(ATTRIBUTE_NAME, scheme.getName());
    }

    for (T tool : scheme.getElements()) {
      saveTool(tool, groupElement);
    }

    return new Document(groupElement);
  }

  @Override
  public boolean shouldBeSaved(@NotNull final ToolsGroup scheme) {
    return true;
  }

  private void saveTool(T tool, Element groupElement) {
    Element element = new Element(TOOL);
    if (tool.getName() != null) {
      element.setAttribute(NAME, tool.getName());
    }
    if (tool.getDescription() != null) {
      element.setAttribute(DESCRIPTION, tool.getDescription());
    }

    saveToolAttributes(tool, element);

    Element taskElement = new Element(EXEC);

    final PathMacroManager macroManager = PathMacroManager.getInstance(ApplicationManager.getApplication());

    Element option = new Element(ELEMENT_OPTION);
    taskElement.addContent(option);
    option.setAttribute(ATTRIBUTE_NAME, COMMAND);
    if (tool.getProgram() != null) {
      option.setAttribute(ATTRIBUTE_VALUE, macroManager.collapsePath(tool.getProgram()));
    }

    option = new Element(ELEMENT_OPTION);
    taskElement.addContent(option);
    option.setAttribute(ATTRIBUTE_NAME, PARAMETERS);
    if (tool.getParameters() != null) {
      option.setAttribute(ATTRIBUTE_VALUE, macroManager.collapsePath(tool.getParameters()));
    }

    option = new Element(ELEMENT_OPTION);
    taskElement.addContent(option);
    option.setAttribute(ATTRIBUTE_NAME, WORKING_DIRECTORY);
    if (tool.getWorkingDirectory() != null) {
      option.setAttribute(ATTRIBUTE_VALUE, macroManager.collapsePath(tool.getWorkingDirectory()).replace(File.separatorChar, '/'));
    }

    element.addContent(taskElement);

    FilterInfo[] filters = tool.getOutputFilters();
    for (FilterInfo filter : filters) {
      Element filterElement = new Element(FILTER);
      filter.writeExternal(filterElement);
      element.addContent(filterElement);
    }

    groupElement.addContent(element);
  }

  protected void saveToolAttributes(T tool, Element element) {
    element.setAttribute(SHOW_IN_MAIN_MENU, Boolean.toString(tool.isShownInMainMenu()));
    element.setAttribute(SHOW_IN_EDITOR, Boolean.toString(tool.isShownInEditor()));
    element.setAttribute(SHOW_IN_PROJECT, Boolean.toString(tool.isShownInProjectViews()));
    element.setAttribute(SHOW_IN_SEARCH_POPUP, Boolean.toString(tool.isShownInSearchResultsPopup()));
    element.setAttribute(DISABLED, Boolean.toString(!tool.isEnabled()));
    element.setAttribute(USE_CONSOLE, Boolean.toString(tool.isUseConsole()));
    element.setAttribute(SHOW_CONSOLE_ON_STDOUT, Boolean.toString(tool.isShowConsoleOnStdOut()));
    element.setAttribute(SHOW_CONSOLE_ON_STDERR, Boolean.toString(tool.isShowConsoleOnStdErr()));
    element.setAttribute(SYNCHRONIZE_AFTER_EXECUTION, Boolean.toString(tool.synchronizeAfterExecution()));
  }
}