View Javadoc

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