PageRenderTime 44ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/src/main/java/org/andya/confluence/utils/page/FindPage.java

https://bitbucket.org/jwalton/metadata-confluence-plugin
Java | 108 lines | 64 code | 10 blank | 34 comment | 24 complexity | 616d87db4ca20f14f324fd2873b01b08 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.page;
  29. import com.atlassian.confluence.core.ContentEntityObject;
  30. import com.atlassian.confluence.pages.Page;
  31. import com.atlassian.confluence.pages.PageManager;
  32. import com.atlassian.confluence.renderer.PageContext;
  33. import com.atlassian.renderer.RenderContext;
  34. import com.atlassian.renderer.v2.macro.MacroException;
  35. import org.andya.confluence.utils.MacroConstants;
  36. /**
  37. * Locates pages. Supports @self, @parent, raw page names with an optional space key.
  38. *
  39. * @author kelsey
  40. *
  41. */
  42. public class FindPage {
  43. private final PageManager pageManager;
  44. public FindPage(PageManager pageManager) {
  45. this.pageManager = pageManager;
  46. }
  47. public Page find(String pageName, RenderContext renderContext, String spaceKey) throws MacroException {
  48. return find(pageName, renderContext, spaceKey, true);
  49. }
  50. public Page find(String pageName, RenderContext renderContext, String spaceKey, boolean required) throws MacroException {
  51. Page thisPage = null;
  52. if (renderContext instanceof PageContext) {
  53. ContentEntityObject ceo = ((PageContext)renderContext).getEntity();
  54. if (ceo instanceof Page) {
  55. thisPage = (Page)ceo;
  56. if (spaceKey == null || "".equals(spaceKey)) {
  57. // use our current space
  58. spaceKey = thisPage.getSpaceKey();
  59. }
  60. }
  61. }
  62. if (pageName.startsWith("@")) {
  63. if(thisPage == null) {
  64. if (required)
  65. throw new MacroException("Cannot get self/parent pages from context " + renderContext);
  66. } else {
  67. if (MacroConstants.SELF_KEY.equalsIgnoreCase(pageName)) {
  68. return thisPage;
  69. } else if (MacroConstants.PARENT_KEY.equalsIgnoreCase(pageName)) {
  70. return thisPage.getParent();
  71. } else if (required) {
  72. throw new MacroException("Page Name '" + pageName + "' is not valid");
  73. }
  74. }
  75. return null;
  76. } else {
  77. return getPage(renderContext, pageName, spaceKey, required);
  78. }
  79. }
  80. protected Page getPage(RenderContext renderContext, String pageName, String spaceKey, boolean required)
  81. throws MacroException {
  82. String[] pageSections = pageName.split(":");
  83. if (pageSections.length == 2) {
  84. return getPage(pageSections[0], pageSections[1], required);
  85. } else if (renderContext instanceof PageContext) {
  86. return getPage(spaceKey, pageName, required);
  87. }
  88. if (required)
  89. throw new MacroException("No such page \"" + pageName + "\"");
  90. return null;
  91. }
  92. protected Page getPage(String spaceKey, String pageName, boolean required) throws MacroException {
  93. Page page = pageManager.getPage(spaceKey, pageName);
  94. if (page == null && required)
  95. throw new MacroException("No such page \"" + pageName + "\"");
  96. return page;
  97. }
  98. }