summaryrefslogtreecommitdiff
path: root/plugins/gradle/src/org/jetbrains/plugins/gradle/model/data/WarDirectory.java
blob: 050f6eec128e66f9ce88b1cca15f9c834257a1b6 (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
/*
 * 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 org.jetbrains.plugins.gradle.model.data;

import com.intellij.openapi.util.text.StringUtil;
import org.jetbrains.annotations.NotNull;

import java.io.Serializable;

/**
 * @author Vladislav.Soroka
 * @since 2/10/14
 */
public class WarDirectory implements Serializable {
  /**
   * Public files typically include the following:
   * HTML files.
   * JSP files.
   * Image files and other multimedia files -- it is a common convention to store image files in an images subdirectory.
   */
  public static final WarDirectory WAR_ROOT = new WarDirectory("/");
  /**
   * directory can contain the following file:
   * META-INF/MANIFEST.MF -- an optional file that can be used to specify additional meta-information for the WAR.
   */
  public static final WarDirectory META_INF = new WarDirectory("/META-INF");
  /**
   * Directory contains a Web archive's private files and directories.
   * That is, when the Web archive is deployed, the files and directories under the WEB-INF/ directory cannot be accessed directly by Web clients.
   */
  public static final WarDirectory WEB_INF = new WarDirectory("/WEB-INF");
  /**
   * Subdirectory can store JAR files used by the Web module.
   * The JAR files in this directory are automatically accessible to the Web module without needing to be added to the class path.
   */
  public static final WarDirectory WEB_INF_LIB = new WarDirectory("/WEB-INF/lib");
  /**
   * Subdirectory contains the compiled Java code for the Web module.
   */
  public static final WarDirectory WEB_INF_CLASSES = new WarDirectory("/WEB-INF/classes");

  private static final WarDirectory[] WAR_DIRECTORIES = new WarDirectory[]{WAR_ROOT, META_INF, WEB_INF, WEB_INF_LIB, WEB_INF_CLASSES};

  @NotNull
  private final String myRelativePath;

  WarDirectory(@NotNull final String relativePath) {
    myRelativePath = getAdjustedPath(relativePath);
  }

  @NotNull
  public String getRelativePath() {
    return myRelativePath;
  }

  public boolean isCustomDirectory() {
    for (WarDirectory warDirectory : WAR_DIRECTORIES) {
      if (myRelativePath.equals(warDirectory.getRelativePath())) return false;
    }
    return true;
  }

  @NotNull
  public static WarDirectory fromPath(final @NotNull String path) {
    if (StringUtil.isEmpty(path)) return WAR_ROOT;

    final String adjustedPath = getAdjustedPath(path);
    for (WarDirectory warDirectory : WAR_DIRECTORIES) {
      if (warDirectory.myRelativePath.equals(adjustedPath)) return warDirectory;
    }
    return new WarDirectory(adjustedPath);
  }

  private static String getAdjustedPath(final @NotNull String path) {
    return path.isEmpty() || path.charAt(0) != '/' ? '/' + path : path;
  }

  @Override
  public String toString() {
    return myRelativePath;
  }

  @Override
  public boolean equals(Object o) {
    if (this == o) return true;
    if (o == null || getClass() != o.getClass()) return false;
    WarDirectory directory = (WarDirectory)o;
    if (!myRelativePath.equals(directory.myRelativePath)) return false;
    return true;
  }

  @Override
  public int hashCode() {
    return myRelativePath.hashCode();
  }
}