1 11 package org.eclipse.jdt.internal.core.jdom; 12 13 import org.eclipse.jdt.core.Flags; 14 import org.eclipse.jdt.core.IJavaElement; 15 import org.eclipse.jdt.core.IPackageFragment; 16 import org.eclipse.jdt.core.jdom.*; 17 import org.eclipse.jdt.internal.compiler.util.SuffixConstants; 18 import org.eclipse.jdt.internal.core.util.CharArrayBuffer; 19 import org.eclipse.jdt.internal.core.util.Messages; 20 import org.eclipse.jdt.internal.core.util.Util; 21 30 class DOMCompilationUnit extends DOMNode implements IDOMCompilationUnit, SuffixConstants { 31 32 37 protected String fHeader; 38 41 DOMCompilationUnit() { 42 fHeader=""; } 44 54 DOMCompilationUnit(char[] document, int[] sourceRange) { 55 super(document, sourceRange, null, new int[]{-1, -1}); 56 fHeader = ""; } 58 61 protected void appendFragmentedContents(CharArrayBuffer buffer) { 62 buffer.append(getHeader()); 63 appendContentsOfChildren(buffer); 64 } 65 68 public boolean canHaveChildren() { 69 return true; 70 } 71 74 public String getHeader() { 75 return fHeader; 76 } 77 80 public IJavaElement getJavaElement(IJavaElement parent) throws IllegalArgumentException { 81 if (parent.getElementType() == IJavaElement.PACKAGE_FRAGMENT) { 82 return ((IPackageFragment)parent).getCompilationUnit(getName()); 83 } else { 84 throw new IllegalArgumentException (Messages.element_illegalParent); 85 } 86 } 87 90 public String getName() { 91 IDOMType topLevelType= null; 92 IDOMType firstType= null; 93 IDOMNode child= fFirstChild; 94 while (child != null) { 95 if (child.getNodeType() == IDOMNode.TYPE) { 96 IDOMType type= (IDOMType)child; 97 if (firstType == null) { 98 firstType= type; 99 } 100 if (Flags.isPublic(type.getFlags())) { 101 topLevelType= type; 102 break; 103 } 104 } 105 child= child.getNextNode(); 106 } 107 if (topLevelType == null) { 108 topLevelType= firstType; 109 } 110 if (topLevelType != null) { 111 return topLevelType.getName() + Util.defaultJavaExtension(); 112 } else { 113 return null; 114 } 115 } 116 119 public int getNodeType() { 120 return IDOMNode.COMPILATION_UNIT; 121 } 122 125 protected void initalizeHeader() { 126 DOMNode child = (DOMNode)getFirstChild(); 127 if (child != null) { 128 int childStart = child.getStartPosition(); 129 if (childStart > 1) { 130 setHeader(new String (fDocument, 0, childStart)); 131 } 132 } 133 } 134 137 public boolean isAllowableChild(IDOMNode node) { 138 if (node != null) { 139 int type= node.getNodeType(); 140 return type == IDOMNode.PACKAGE || type == IDOMNode.IMPORT || type == IDOMNode.TYPE; 141 } else { 142 return false; 143 } 144 145 } 146 149 protected DOMNode newDOMNode() { 150 return new DOMCompilationUnit(); 151 } 152 157 void normalize(ILineStartFinder finder) { 158 super.normalize(finder); 159 initalizeHeader(); 160 } 161 164 public void setHeader(String comment) { 165 fHeader= comment; 166 fragment(); 167 } 168 171 public void setName(String name) { 172 } 174 177 protected void shareContents(DOMNode node) { 178 super.shareContents(node); 179 fHeader= ((DOMCompilationUnit)node).fHeader; 180 } 181 184 public String toString() { 185 return "COMPILATION_UNIT: " + getName(); } 187 } 188 | Popular Tags |