/plugins/PHPParser/tags/PHPParser-1.2.4/src/net/sourceforge/phpdt/internal/compiler/ast/PHPDocument.java

# · Java · 231 lines · 128 code · 31 blank · 72 comment · 45 complexity · ebdd73a8e1d5fbc5f0a505c6bf5dd930 MD5 · raw file

  1. package net.sourceforge.phpdt.internal.compiler.ast;
  2. import java.util.ArrayList;
  3. import java.util.List;
  4. import net.sourceforge.phpdt.internal.compiler.parser.Outlineable;
  5. import net.sourceforge.phpdt.internal.compiler.parser.OutlineableWithChildren;
  6. import gatchan.phpparser.project.itemfinder.PHPItem;
  7. import gatchan.phpparser.parser.PHPParser;
  8. import sidekick.IAsset;
  9. import javax.swing.text.Position;
  10. import javax.swing.*;
  11. /**
  12. * It's a php document. This class is an outlineable object It will contains html and php
  13. *
  14. * @author Matthieu Casanova
  15. */
  16. public final class PHPDocument implements OutlineableWithChildren, IAsset {
  17. /** The nodes. It will include html nodes or php nodes */
  18. private AstNode[] nodes;
  19. private final String name;
  20. /** The outlineable children (those will be in the node array too. */
  21. private final List children = new ArrayList();
  22. private transient Position start;
  23. private transient Position end;
  24. /**
  25. * Create the PHPDocument.
  26. *
  27. * @param name the nale f the document
  28. */
  29. public PHPDocument(String name) {
  30. this.name = name;
  31. }
  32. /**
  33. * Return the php document as String.
  34. *
  35. * @return a string representation of the object.
  36. */
  37. public String toString() {
  38. StringBuffer buff = new StringBuffer();
  39. AstNode node;
  40. if (nodes != null) {
  41. int i;
  42. for (i = 0; i < nodes.length; i++) {
  43. node = nodes[i];
  44. if (node == null) {
  45. break;
  46. }
  47. buff.append(node.toString(0));
  48. if (node instanceof HTMLCode) {
  49. buff.append('\n');
  50. } else {
  51. buff.append(";\n");
  52. }
  53. }
  54. }
  55. return buff.toString();
  56. }
  57. /**
  58. * Add an outlineable object.
  59. *
  60. * @param o the new outlineable
  61. *
  62. * @return does the addition worked ?
  63. */
  64. public boolean add(Outlineable o) {
  65. return children.add(o);
  66. }
  67. /**
  68. * Return the outlineable at the index.
  69. *
  70. * @param index the index
  71. *
  72. * @return an outlineable object
  73. */
  74. public Outlineable get(int index) {
  75. return (Outlineable) children.get(index);
  76. }
  77. /**
  78. * The number of outlineable children.
  79. *
  80. * @return the number of children that are outlineable
  81. */
  82. public int size() {
  83. return children.size();
  84. }
  85. /**
  86. * Get the parent of the object.
  87. *
  88. * @return null
  89. */
  90. public OutlineableWithChildren getParent() {
  91. return null;
  92. }
  93. /**
  94. * Analyze the code of a php document.
  95. *
  96. * @param phpParser the php parser
  97. */
  98. public void analyzeCode(PHPParser phpParser) {
  99. if (nodes != null) {
  100. for (int i = 0; i < nodes.length; i++) {
  101. AstNode node = nodes[i];
  102. if (node == null) break;
  103. node.analyzeCode(phpParser);
  104. }
  105. }
  106. }
  107. public String getName() {
  108. return name;
  109. }
  110. /**
  111. * Give the method at the line and column given. It will returns null if no method can be found at the offset.
  112. *
  113. * @param line the line
  114. * @param column the offset
  115. * @return the method at the offset or null
  116. */
  117. public ClassDeclaration classAtOffset(int line, int column) {
  118. for (int i = 0; i < children.size(); i++) {
  119. Outlineable outlineable = (Outlineable) children.get(i);
  120. if (outlineable.getItemType() == PHPItem.CLASS) {
  121. ClassDeclaration classDeclaration = (ClassDeclaration) outlineable;
  122. if (line == classDeclaration.getBodyLineStart() && column > classDeclaration.getBodyColumnStart()) return classDeclaration;
  123. if (line == classDeclaration.getBodyLineEnd() && column < classDeclaration.getBodyColumnEnd()) return classDeclaration;
  124. if (line > classDeclaration.getBodyLineStart() && line < classDeclaration.getBodyLineEnd()) return classDeclaration;
  125. }
  126. }
  127. return null;
  128. }
  129. /**
  130. * Give the method at the line and column given. It will returns null if no method can be found at the offset.
  131. *
  132. * @param line the line
  133. * @param column the offset
  134. * @return the method at the offset or null
  135. */
  136. public MethodDeclaration methodAtOffset(int line, int column) {
  137. for (int i = 0; i < children.size(); i++) {
  138. Outlineable outlineable = (Outlineable) children.get(i);
  139. if (outlineable.getItemType() == PHPItem.METHOD) {
  140. MethodDeclaration methodDeclaration = (MethodDeclaration) outlineable;
  141. if (line == methodDeclaration.getBodyLineStart() && column > methodDeclaration.getBodyColumnStart()) return methodDeclaration;
  142. if (line == methodDeclaration.getBodyLineEnd() && column < methodDeclaration.getBodyColumnEnd()) return methodDeclaration;
  143. if (line > methodDeclaration.getBodyLineStart() && line < methodDeclaration.getBodyLineEnd()) return methodDeclaration;
  144. }
  145. }
  146. return null;
  147. }
  148. /**
  149. * Returns the statement at the given position.
  150. *
  151. * @param line the line
  152. * @param column the column
  153. * @return the statement at the position
  154. */
  155. public Statement getStatementAt(int line, int column) {
  156. Statement statement = null;
  157. for (int i = 0; i < nodes.length; i++) {
  158. statement = (Statement) nodes[i];
  159. if (statement == null) break;
  160. if (line == statement.getBeginLine() && column > statement.getBeginColumn()) return statement;
  161. if (line == statement.getEndLine() && column < statement.getEndColumn()) return statement;
  162. if (line > statement.getBeginLine() && line < statement.getEndLine()) return statement;
  163. }
  164. return statement;
  165. }
  166. /**
  167. * Set the nodes of the document.
  168. *
  169. * @param nodes the nodes
  170. */
  171. public void setNodes(AstNode[] nodes) {
  172. this.nodes = nodes;
  173. }
  174. public int getItemType() {
  175. return PHPItem.DOCUMENT;
  176. }
  177. public Position getEnd() {
  178. return end;
  179. }
  180. public void setEnd(Position end) {
  181. this.end = end;
  182. }
  183. public Position getStart() {
  184. return start;
  185. }
  186. public void setStart(Position start) {
  187. this.start = start;
  188. }
  189. public Icon getIcon() {
  190. return null;
  191. }
  192. public String getShortString() {
  193. return "/";
  194. }
  195. public String getLongString() {
  196. return "/";
  197. }
  198. public void setName(String name) {
  199. }
  200. }