PageRenderTime 42ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/atlassian-plugins-api/src/main/java/com/atlassian/plugin/elements/ResourceDescriptor.java

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