summaryrefslogtreecommitdiff
path: root/platform/lang-api/src/com/intellij/execution/configurations/LogFileOptions.java
blob: 6f23831c9e3a22dce259e1ddc4adf7a23b841a53 (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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/*
 * Copyright 2000-2009 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.execution.configurations;

import com.intellij.openapi.util.InvalidDataException;
import com.intellij.openapi.util.JDOMExternalizable;
import com.intellij.openapi.util.WriteExternalException;
import com.intellij.openapi.util.io.FileUtil;
import org.jdom.Element;
import org.jetbrains.annotations.NonNls;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.File;
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.regex.Pattern;

/**
 * The information about a single log file displayed in the console when the configuration
 * is run.
 *
 * @since 5.1
 */
public class LogFileOptions implements JDOMExternalizable {
  @NonNls private static final String PATH = "path";
  @NonNls private static final String CHECKED = "checked";
  @NonNls private static final String ALIAS = "alias";
  @NonNls private static final String SKIPPED = "skipped";
  @NonNls private static final String SHOW_ALL = "show_all";
  @NonNls private static final String CHARSET = "charset";

  private String myName;
  private String myPathPattern;
  private boolean myEnabled;
  private boolean mySkipContent;
  private boolean myShowAll;
  @NotNull
  private Charset myCharset;

  //read external
  public LogFileOptions() {
  }

  public LogFileOptions(String name, String path, boolean enabled, boolean skipContent, final boolean showAll) {
    this(name, path, null, enabled, skipContent, showAll);
  }

  public LogFileOptions(String name, String path, @Nullable final Charset charset, boolean enabled, boolean skipContent, final boolean showAll) {
    myName = name;
    myPathPattern = path;
    myEnabled = enabled;
    mySkipContent = skipContent;
    myShowAll = showAll;
    myCharset = charset != null ? charset : Charset.defaultCharset();
  }

  public String getName() {
    return myName;
  }

  public String getPathPattern() {
    return myPathPattern;
  }

  public Set<String> getPaths(){
    Set<String> result = new HashSet<String>();
    final File logFile = new File(myPathPattern);
    if (logFile.exists()){
      result.add(myPathPattern);
      return result;
    }
    final int dirIndex = myPathPattern.lastIndexOf(File.separator);
    if (dirIndex != -1) {
      final ArrayList<File> files = new ArrayList<File>();
      final String basePath = myPathPattern.substring(0, dirIndex);
      final String pattern = myPathPattern.substring(dirIndex + File.separator.length());
      collectMatchedFiles(new File(basePath), Pattern.compile(FileUtil.convertAntToRegexp(pattern)), files);
      if (!files.isEmpty()) {
        if (myShowAll) {
          for (File file : files) {
            result.add(file.getPath());
          }
        }
        else {
          File lastFile = null;
          for (File file : files) {
            if (lastFile != null) {
              if (file.lastModified() > lastFile.lastModified()) {
                lastFile = file;
              }
            }
            else {
              lastFile = file;
            }
          }
          assert lastFile != null;
          result.add(lastFile.getPath());
        }
      }
    }
    return result;
  }

  public static void collectMatchedFiles(final File root, final Pattern pattern, final List<File> files) {
    final File[] dirs = root.listFiles();
    if (dirs == null) return;
    for (File dir : dirs) {
      if (pattern.matcher(dir.getName()).matches() && dir.isFile()) {
        files.add(dir);
      }
    }
  }

  public boolean isEnabled() {
    return myEnabled;
  }

  public boolean isSkipContent() {
    return mySkipContent;
  }

  public void setEnable(boolean enable) {
    myEnabled = enable;
  }


  public void setName(final String name) {
    myName = name;
  }

  public void setPathPattern(final String pathPattern) {
    myPathPattern = pathPattern;
  }

  public void setSkipContent(final boolean skipContent) {
    mySkipContent = skipContent;
  }

  public void setShowAll(final boolean showAll) {
    myShowAll = showAll;
  }

  public void setLast(final boolean last) {
    myShowAll = !last;
  }

  public boolean isShowAll() {
    return myShowAll;
  }

  public Charset getCharset() {
    return myCharset;
  }

  public void setCharset(Charset charset) {
    myCharset = charset;
  }

  @Override
  public void readExternal(Element element) throws InvalidDataException {
    String file = element.getAttributeValue(PATH);
    if (file != null){
      file = FileUtil.toSystemDependentName(file);
    }
    setPathPattern(file);

    Boolean checked = Boolean.valueOf(element.getAttributeValue(CHECKED));
    setEnable(checked.booleanValue());

    final String skipped = element.getAttributeValue(SKIPPED);
    Boolean skip = skipped != null ? Boolean.valueOf(skipped) : Boolean.TRUE;
    setSkipContent(skip.booleanValue());

    final String all = element.getAttributeValue(SHOW_ALL);
    Boolean showAll = skipped != null ? Boolean.valueOf(all) : Boolean.TRUE;
    setShowAll(showAll.booleanValue());

    setName(element.getAttributeValue(ALIAS));

    final String charsetStr = element.getAttributeValue(CHARSET);
    try {
      setCharset(Charset.forName(charsetStr));
    }
    catch (Exception e) {
      setCharset(Charset.defaultCharset());
    }
  }

  @Override
  public void writeExternal(Element element) throws WriteExternalException {
    element.setAttribute(PATH, FileUtil.toSystemIndependentName(getPathPattern()));
    element.setAttribute(CHECKED, String.valueOf(isEnabled()));
    element.setAttribute(SKIPPED, String.valueOf(isSkipContent()));
    element.setAttribute(SHOW_ALL, String.valueOf(isShowAll()));
    element.setAttribute(ALIAS, getName());
    if (!getCharset().equals(Charset.defaultCharset())) {
      element.setAttribute(CHARSET, getCharset().name());
    }
  }

  public static boolean areEqual(@Nullable LogFileOptions options1, @Nullable LogFileOptions options2) {
    if (options1 == null || options2 == null) {
      return options1 == options2;
    }

    return options1.myName.equals(options2.myName) &&
           options1.myPathPattern.equals(options2.myPathPattern) &&
           !options1.myShowAll == !options2.myShowAll &&
           options1.myEnabled == options2.myEnabled &&
           options1.mySkipContent == options2.mySkipContent;

  }
}