PageRenderTime 52ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/andya/confluence/utils/MacroUtils.java

https://bitbucket.org/jwalton/metadata-confluence-plugin
Java | 174 lines | 118 code | 20 blank | 36 comment | 19 complexity | b6c81e563d599198ff11559b29e49674 MD5 | raw file
  1. /*
  2. * Copyright (c) 2006, 2007 Andy Armstrong, Kelsey Grant and other contributors.
  3. * All rights reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * * Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * * Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * * The names of contributors may not
  14. * be used to endorse or promote products derived from this software without
  15. * specific prior written permission.
  16. *
  17. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  18. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  19. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  20. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR
  21. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  22. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  23. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  24. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  25. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  26. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  27. */
  28. package org.andya.confluence.utils;
  29. import com.atlassian.renderer.v2.macro.MacroException;
  30. import com.atlassian.renderer.v2.RenderUtils;
  31. import com.atlassian.renderer.v2.RenderMode;
  32. import com.atlassian.renderer.v2.SubRenderer;
  33. import com.atlassian.renderer.RenderContext;
  34. import com.atlassian.confluence.core.ContentEntityObject;
  35. import com.atlassian.confluence.core.ConfluenceEntityObject;
  36. import com.atlassian.confluence.renderer.PageContext;
  37. import java.util.Map;
  38. import java.io.StringWriter;
  39. import java.io.PrintWriter;
  40. /**
  41. * Useful utilitis for building macros.
  42. */
  43. public class MacroUtils {
  44. private static final String DEBUG_KEY = "debug";
  45. @SuppressWarnings("unchecked")
  46. public static String getStringParameter(Map parameters, String name, boolean required) throws MacroException {
  47. String value = (String)parameters.get(name);
  48. if (value == null && required)
  49. throw new MacroException("Required parameter \"" + name + "\" not provided");
  50. return value;
  51. }
  52. @SuppressWarnings("unchecked")
  53. public static String getStringParameter(Map parameters, String name, String defaultValue) throws MacroException {
  54. String value = getStringParameter(parameters, name, false);
  55. return value != null ? value : defaultValue;
  56. }
  57. @SuppressWarnings("unchecked")
  58. public static Integer getIntegerParameter(Map parameters, String name) throws MacroException {
  59. String value = getStringParameter(parameters, name, false);
  60. if (value == null)
  61. return null;
  62. try {
  63. return Integer.valueOf(value);
  64. } catch (NumberFormatException e) {
  65. throw new MacroException("Non-integer value for parameter \"" + name + "\": " + value);
  66. }
  67. }
  68. @SuppressWarnings("unchecked")
  69. public static int getIntParameter(Map parameters, String name, int defaultValue) throws MacroException {
  70. Integer result = getIntegerParameter(parameters, name);
  71. return result != null ? result.intValue() : defaultValue;
  72. }
  73. @SuppressWarnings("unchecked")
  74. public static boolean getBooleanParameter(Map parameters, String name) throws MacroException {
  75. return getBooleanParameter(parameters, name, false);
  76. }
  77. @SuppressWarnings("unchecked")
  78. public static boolean getBooleanParameter(Map parameters, String name, boolean defaultValue) throws MacroException {
  79. return getBooleanParameter(parameters, name, "true", defaultValue);
  80. }
  81. @SuppressWarnings("unchecked")
  82. public static boolean getBooleanParameter(Map parameters, String name, String trueKey, boolean defaultValue) throws MacroException {
  83. String value = getStringParameter(parameters, name, null);
  84. return value != null ? trueKey.equals(value) : defaultValue;
  85. }
  86. @SuppressWarnings("unchecked")
  87. public static boolean isDebugMode(Map parameters) {
  88. try {
  89. return getBooleanParameter(parameters, DEBUG_KEY, false);
  90. } catch (MacroException e) {
  91. return false;
  92. }
  93. }
  94. public static String getExceptionString(Throwable e) {
  95. StringWriter writer = new StringWriter();
  96. e.printStackTrace(new PrintWriter(writer));
  97. return writer.toString();
  98. }
  99. public static String getExceptionMessage(Throwable e) {
  100. String message = e.getMessage();
  101. if (message == null)
  102. message = e.toString();
  103. return message;
  104. }
  105. /** Renders an exception to be displayed from a macro. */
  106. @SuppressWarnings("unchecked")
  107. public static String showRenderedExceptionString(Map parameters, String title, Throwable e) {
  108. e.printStackTrace();
  109. if (isDebugMode(parameters))
  110. return "<div class=\"error\"><h3>" + title + "</h3>\n<pre>" + getExceptionString(e) + "</pre></div>\n";
  111. else
  112. return RenderUtils.blockError(title + ":", getExceptionMessage(e));
  113. }
  114. /** Renders an exception to be displayed from a macro. */
  115. @SuppressWarnings("unchecked")
  116. public static String showRenderedExceptionString(Map parameters, String title, String message) {
  117. if (isDebugMode(parameters))
  118. return "<div class=\"error\"><h3>" + title + "<h3>\n<pre>" + message + "</pre></div>\n";
  119. else
  120. return RenderUtils.blockError(title + ":", message);
  121. }
  122. /** Returns an unrendered message string for an exception. */
  123. @SuppressWarnings("unchecked")
  124. public static String showUnrenderedExceptionString(Map parameters, String title, Throwable e) {
  125. if (isDebugMode(parameters))
  126. return "{warning:title=" + title + "}{noformat}" + getExceptionString(e) + "{noformat}{warning}\n";
  127. else {
  128. return "{warning:title=" + title + "}" + getExceptionMessage(e) + "{warning}\n";
  129. }
  130. }
  131. /** Returns an unrendered message string for an exception. */
  132. @SuppressWarnings("unchecked")
  133. public static String showUnrenderedExceptionString(Map parameters, String title, String message) {
  134. if (isDebugMode(parameters))
  135. return "{warning:title=" + title + "}" + message + "{warning}\n";
  136. else
  137. return "{warning:title=" + title + "}" + message + "{warning}\n";
  138. }
  139. /** Renders a page value for the given render context. */
  140. public static String renderValue(SubRenderer subRenderer, RenderContext renderContext, String value) {
  141. return subRenderer.render(value != null ? value : "", renderContext,
  142. renderContext.getRenderMode().and(RenderMode.suppress(RenderMode.F_FIRST_PARA)));
  143. }
  144. /** Renders a content entity object's metadata value. */
  145. public static String renderValue(SubRenderer subRenderer, RenderContext renderContext,
  146. ConfluenceEntityObject ceo, String value) {
  147. if (renderContext instanceof PageContext && ceo instanceof ContentEntityObject) {
  148. PageContext pageContext = new PageContext((ContentEntityObject)ceo, (PageContext)renderContext);
  149. return renderValue(subRenderer, pageContext, value);
  150. } else {
  151. return renderValue(subRenderer, renderContext, value);
  152. }
  153. }
  154. }