View Javadoc

1   package com.atlassian.plugin.elements;
2   
3   import com.atlassian.plugin.loaders.LoaderUtils;
4   import org.dom4j.Element;
5   
6   import java.util.Map;
7   import java.util.Collections;
8   import java.util.regex.Pattern;
9   
10  public class ResourceDescriptor
11  {
12      private String type;
13      private String name;
14      private String location;
15      private String contentType;
16  
17      private Pattern pattern;
18      private String content;
19      private Map<String,String> params;
20      private ResourceLocation ourLocation;
21  
22      public ResourceDescriptor(Element element)
23      {
24          this.type = element.attributeValue("type");
25          String name = element.attributeValue("name");
26          String namePattern = element.attributeValue("namePattern");
27          if (name == null && namePattern == null)
28          {
29              throw new RuntimeException("resource descriptor needs one of 'name' and 'namePattern' attributes.");
30          }
31  
32          if (name != null && namePattern != null)
33          {
34              throw new RuntimeException("resource descriptor can have only one of 'name' and 'namePattern' attributes.");
35          }
36  
37          this.name = name;
38  
39          this.location = element.attributeValue("location");
40  
41          if (namePattern != null && !location.endsWith("/"))
42          {
43              throw new RuntimeException("when 'namePattern' is specified, 'location' must be a directory (ending in '/')");
44          }
45          this.params = LoaderUtils.getParams(element);
46  
47          if (element.getTextTrim() != null && !"".equals(element.getTextTrim()))
48          {
49              content = element.getTextTrim();
50          }
51  
52          contentType = getParameter("content-type");
53  
54          if (namePattern != null)
55          {
56              pattern = Pattern.compile(namePattern);
57          }
58          else
59          {
60              ourLocation = new ResourceLocation(location, name, type, contentType, content, params);
61          }
62  
63      }
64  
65      public String getType()
66      {
67          return type;
68      }
69  
70      /**
71       * This may throw an exception if one of the deprecated methods is used on a ResourceDescriptor which has been given a namePattern
72       */
73      public String getName()
74      {
75          if (name == null)
76          {
77              throw new RuntimeException("tried to get name from ResourceDescriptor with null name and namePattern = " + pattern);
78          }
79          return name;
80      }
81  
82      public String getLocation()
83      {
84          return location;
85      }
86  
87  
88      public String getContent()
89      {
90          return content;
91      }
92  
93      public boolean doesTypeAndNameMatch(String type, String name)
94      {
95          if (type != null && type.equalsIgnoreCase(this.type))
96          {
97              if (pattern != null)
98              {
99                  return pattern.matcher(name).matches();
100             }
101             else
102             {
103                 return name != null && name.equalsIgnoreCase(this.name);
104             }
105         }
106         else
107         {
108             return false;
109         }
110     }
111 
112     public Map getParameters()
113     {
114         return Collections.unmodifiableMap(params);
115     }
116 
117     public String getParameter(String key)
118     {
119         return (String) params.get(key);
120     }
121 
122     public boolean equals(Object o)
123     {
124         if (this == o)
125         {
126             return true;
127         }
128         if (!(o instanceof ResourceDescriptor))
129         {
130             return false;
131         }
132 
133         final ResourceDescriptor resourceDescriptor = (ResourceDescriptor) o;
134 
135         if (!name.equals(resourceDescriptor.name))
136         {
137             return false;
138         }
139         if (!type.equals(resourceDescriptor.type))
140         {
141             return false;
142         }
143 
144         return true;
145     }
146 
147     public int hashCode()
148     {
149         int result;
150         result = type.hashCode();
151         result = 29 * result + name.hashCode();
152         return result;
153     }
154 
155     /**
156      * Used for resource descriptors that specify multiple resources, via {@link #pattern}.
157      *
158      * @return the location of an individual resource with the name if it matches the pattern,
159      * otherwise the location for the actual resource descriptor.
160      */
161     public ResourceLocation getResourceLocationForName(String name)
162     {
163 
164         if (pattern != null)
165         {
166             if (pattern.matcher(name).matches())
167             {
168                 return new ResourceLocation(getLocation(), name, type, contentType, content, params);
169             }
170             else
171             {
172                 throw new RuntimeException("Thiss descriptor does not provide resources named " + name);
173             }
174         }
175         else
176         {
177             return ourLocation;
178         }
179     }
180 }