1 11 12 package org.eclipse.pde.internal.ui.editor.contentassist; 13 14 import java.util.HashMap ; 15 import java.util.HashSet ; 16 17 import org.eclipse.pde.internal.core.ischema.ISchemaComplexType; 18 import org.eclipse.pde.internal.core.ischema.ISchemaCompositor; 19 import org.eclipse.pde.internal.core.ischema.ISchemaElement; 20 import org.eclipse.pde.internal.core.ischema.ISchemaObject; 21 import org.eclipse.pde.internal.core.text.IDocumentNode; 22 23 27 public class XMLElementProposalComputer { 28 29 37 public static HashSet computeElementProposal(ISchemaElement sElement, 38 IDocumentNode node) { 39 HashMap tagNameMap = countXMLChildrenByTagName(node); 42 return computeElementProposal(sElement, tagNameMap); 43 } 44 45 50 private static HashSet computeElementProposal(ISchemaElement sElement, 51 HashMap tagNameMap) { 52 53 HashSet elementSet = new HashSet (); 54 ISchemaCompositor compositor = 56 ((ISchemaComplexType)sElement.getType()).getCompositor(); 57 int multiplicityTracker = 1; 59 computeCompositorChildProposal(compositor, 61 elementSet, tagNameMap, multiplicityTracker); 62 return elementSet; 63 } 64 65 72 private static HashMap countXMLChildrenByTagName(IDocumentNode node) { 73 IDocumentNode[] children = node.getChildNodes(); 74 HashMap tagNameMap = new HashMap (); 75 for (int i = 0; i < children.length; i++) { 76 String key = children[i].getXMLTagName(); 77 if (tagNameMap.containsKey(key)) { 78 int value = ((Integer )tagNameMap.get(key)).intValue(); 79 value++; 80 tagNameMap.put(key, new Integer (value)); 81 } else { 82 tagNameMap.put(key, new Integer (1)); 83 } 84 } 85 return tagNameMap; 86 } 87 88 94 private static void computeCompositorChildProposal( 95 ISchemaCompositor compositor, HashSet elementSet, 96 HashMap siblings, int multiplicityTracker) { 97 if (compositor == null) { 103 return; 104 } else if (compositor.getKind() == ISchemaCompositor.CHOICE) { 105 computeCompositorChoiceProposal(compositor, elementSet, siblings, 106 multiplicityTracker); 107 } else if (compositor.getKind() == ISchemaCompositor.SEQUENCE) { 108 computeCompositorSequenceProposal(compositor, elementSet, siblings, 109 multiplicityTracker); 110 } 111 } 112 113 119 private static void computeCompositorSequenceProposal( 120 ISchemaCompositor compositor, HashSet elementSet, HashMap siblings, 121 int multiplicityTracker) { 122 123 ISchemaObject[] schemaObject = compositor.getChildren(); 124 if (multiplicityTracker < Integer.MAX_VALUE) { 126 multiplicityTracker = compositor.getMaxOccurs() * multiplicityTracker; 128 } 129 for (int i = 0; i < compositor.getChildCount(); i++) { 131 computeObjectChildProposal(schemaObject[i], elementSet, 132 siblings, multiplicityTracker); 133 } 134 } 135 136 142 private static void computeCompositorChoiceProposal( 143 ISchemaCompositor compositor, HashSet elementSet, HashMap siblings, 144 int multiplicityTracker) { 145 146 if (multiplicityTracker < Integer.MAX_VALUE) { 148 multiplicityTracker = compositor.getMaxOccurs() * multiplicityTracker; 150 } 151 adjustChoiceSiblings(compositor, siblings); 152 153 ISchemaObject[] schemaObject = compositor.getChildren(); 154 for (int i = 0; i < compositor.getChildCount(); i++) { 156 computeObjectChildProposal(schemaObject[i], elementSet, 157 siblings, multiplicityTracker); 158 } 159 } 160 161 165 private static void adjustChoiceSiblings(ISchemaCompositor compositor, 166 HashMap siblings) { 167 168 ISchemaObject[] schemaObject = compositor.getChildren(); 169 int childElementCount = 0; 172 for (int i = 0; i < compositor.getChildCount(); i++) { 173 if (schemaObject[i] instanceof ISchemaElement) { 174 String name = schemaObject[i].getName(); 175 if (siblings.containsKey(name)) { 176 int occurences = ((Integer )siblings.get(name)).intValue(); 177 childElementCount = childElementCount + occurences; 178 } 179 } 180 } 181 for (int i = 0; i < compositor.getChildCount(); i++) { 189 if (schemaObject[i] instanceof ISchemaElement) { 190 String name = schemaObject[i].getName(); 191 siblings.put(name, new Integer (childElementCount)); 192 } 193 } 194 } 195 196 202 private static void computeObjectChildProposal(ISchemaObject schemaObject, 203 HashSet elementSet, HashMap siblings, 204 int multiplicityTracker) { 205 if (schemaObject instanceof ISchemaElement) { 206 ISchemaElement schemaElement = (ISchemaElement)schemaObject; 207 computeElementChildProposal(schemaElement, elementSet, 208 siblings, multiplicityTracker); 209 } else if (schemaObject instanceof ISchemaCompositor) { 210 ISchemaCompositor sCompositor = (ISchemaCompositor)schemaObject; 211 computeCompositorChildProposal(sCompositor, elementSet, 212 siblings, multiplicityTracker); 213 } 214 } 215 216 222 private static void computeElementChildProposal(ISchemaElement schemaElement, 223 HashSet elementSet, HashMap siblings, 224 int multiplicityTracker) { 225 226 int occurrences = 0; 227 if (siblings.containsKey(schemaElement.getName())) { 229 occurrences = ((Integer ) siblings.get(schemaElement.getName())) 230 .intValue(); 231 } 232 if (multiplicityTracker < Integer.MAX_VALUE) { 234 multiplicityTracker = schemaElement.getMaxOccurs() * multiplicityTracker; 235 } 236 if (occurrences < multiplicityTracker) { 245 elementSet.add(schemaElement); 246 } 247 } 248 } 249 | Popular Tags |