PageRenderTime 46ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://bitbucket.org/jwalton/metadata-confluence-plugin
Java | 122 lines | 64 code | 16 blank | 42 comment | 7 complexity | 1310d9f2cd57186409712f7623fded7b 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 java.util.LinkedList;
  30. import java.util.List;
  31. import com.atlassian.confluence.core.ContentEntityObject;
  32. import com.atlassian.confluence.labels.Label;
  33. import com.atlassian.confluence.labels.LabelManager;
  34. import com.atlassian.confluence.labels.LabelParser;
  35. import com.atlassian.confluence.labels.ParsedLabelName;
  36. import com.atlassian.confluence.pages.Page;
  37. import com.opensymphony.util.TextUtils;
  38. import org.andya.confluence.utils.parse.ExpressionVisitor;
  39. import org.andya.confluence.utils.parse.OrExpression;
  40. import org.andya.confluence.utils.parse.AndExpression;
  41. import org.andya.confluence.utils.parse.LabelExpression;
  42. /**
  43. * Walks a label expression tree collecting the appropriate pages
  44. *
  45. * @author kelsey
  46. *
  47. */
  48. class LabelExpressionVisitor implements ExpressionVisitor {
  49. /** The pages we have collected */
  50. private List<Page> pages;
  51. private final PageSearchContext context;
  52. public LabelExpressionVisitor(PageSearchContext context) {
  53. this.context = context;
  54. }
  55. /**
  56. * Get the pages that have matched this expression. Returns null if this
  57. * visitor has not visited an expression yet.
  58. *
  59. * @return the pages that have matched this expression
  60. */
  61. public List<Page> getPages() {
  62. return pages;
  63. }
  64. public void visit(OrExpression or) {
  65. or.getLeft().accept(this);
  66. List<Page> pagesLeft = this.pages;
  67. or.getRight().accept(this);
  68. List<Page> pagesRight = this.pages;
  69. // add them all!
  70. pagesLeft.addAll(pagesRight);
  71. this.pages = pagesLeft;
  72. }
  73. public void visit(AndExpression and) {
  74. and.getLeft().accept(this);
  75. List<Page> pagesLeft = this.pages;
  76. and.getRight().accept(this);
  77. List<Page> pagesRight = this.pages;
  78. // only keep if in both
  79. pagesLeft.retainAll(pagesRight);
  80. this.pages = pagesLeft;
  81. }
  82. @SuppressWarnings("unchecked")
  83. public void visit(LabelExpression labelExpression) {
  84. this.pages = new LinkedList<Page>();
  85. final LabelManager labelManager = context.getLabelManager();
  86. final String spaceKey = context.getSpaceKey();
  87. String labelName = labelExpression.getLabel();
  88. ParsedLabelName ref = LabelParser.parse(labelName);
  89. if (ref != null)
  90. {
  91. Label label = labelManager.getLabel(ref);
  92. if (label != null)
  93. {
  94. List<ContentEntityObject> entities;
  95. if (TextUtils.stringSet(spaceKey))
  96. entities = labelManager.getCurrentContentForLabelAndSpace(label, spaceKey);
  97. else
  98. entities = labelManager.getCurrentContentForLabel(label);
  99. for (ContentEntityObject entity : entities) {
  100. if(entity instanceof Page)
  101. pages.add((Page)entity);
  102. }
  103. }
  104. }
  105. }
  106. }