summaryrefslogtreecommitdiff
path: root/platform/lang-impl/src/com/intellij/ide/fileTemplates/impl/FileTemplateBase.java
blob: 73ee43594f87afe4c588033ea5778342f31964f4 (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
/*
 * Copyright 2000-2011 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.ide.fileTemplates.impl;

import com.intellij.ide.fileTemplates.FileTemplate;
import com.intellij.ide.fileTemplates.FileTemplateUtil;
import com.intellij.openapi.fileTypes.FileType;
import com.intellij.openapi.fileTypes.ex.FileTypeManagerEx;
import com.intellij.openapi.util.text.StringUtil;
import org.apache.velocity.runtime.parser.ParseException;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import java.io.*;
import java.util.Map;
import java.util.Properties;

/**
 * @author Eugene Zhuravlev
 *         Date: 4/6/11
 */
public abstract class FileTemplateBase implements FileTemplate {
  public static final boolean DEFAULT_REFORMAT_CODE_VALUE = true;
  public static final boolean DEFAULT_ENABLED_VALUE = true;
  @Nullable
  private String myText;
  private boolean myShouldReformatCode = DEFAULT_REFORMAT_CODE_VALUE;

  @Override
  public final boolean isReformatCode() {
    return myShouldReformatCode;
  }

  @Override
  public final void setReformatCode(boolean reformat) {
    myShouldReformatCode = reformat;
  }

  public final String getQualifiedName() {
    return getQualifiedName(getName(), getExtension());
  }

  public static String getQualifiedName(final String name, final String extension) {
    return FTManager.encodeFileName(name, extension);
  }

  @Override
  @NotNull
  public final String getText() {
    final String text = myText;
    return text != null? text : getDefaultText();
  }

  @Override
  public final void setText(@Nullable String text) {
    if (text == null) {
      myText = null;
    }
    else {
      final String converted = StringUtil.convertLineSeparators(text);
      myText = converted.equals(getDefaultText())? null : converted;
    }
  }

  @NotNull
  protected String getDefaultText() {
    return "";
  }

  @Override
  @NotNull
  public final String getText(Map attributes) throws IOException{
    return FileTemplateUtil.mergeTemplate(attributes, getText(), false);
  }

  @Override
  @NotNull
  public final String getText(Properties attributes) throws IOException{
    return FileTemplateUtil.mergeTemplate(attributes, getText(), false);
  }

  @Override
  @NotNull
  public final String[] getUnsetAttributes(@NotNull Properties properties) throws ParseException {
    return FileTemplateUtil.calculateAttributes(getText(), properties, false);
  }

  @Override
  public FileTemplateBase clone() {
    try {
      return (FileTemplateBase)super.clone();
    }
    catch (CloneNotSupportedException e) {
      throw new RuntimeException(e);
    }
  }

  @Override
  public boolean isTemplateOfType(@NotNull final FileType fType) {
    return fType.equals(FileTypeManagerEx.getInstanceEx().getFileTypeByExtension(getExtension()));
  }
}