1 11 12 package org.eclipse.pde.internal.core.builders; 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.w3c.dom.Element ; 22 import org.w3c.dom.Node ; 23 import org.w3c.dom.NodeList ; 24 25 29 public class ElementOccurenceChecker { 30 31 38 public static HashSet findMaxOccurenceViolations(ISchemaElement sElement, 39 Element element) { 40 HashMap tagNameMap = countXMLChildrenByTagName(element); 43 return processChildrenMax(sElement, tagNameMap, element); 44 } 45 46 53 public static HashSet findMinOccurenceViolations(ISchemaElement sElement, 54 Element element) { 55 HashMap tagNameMap = countXMLChildrenByTagName(element); 58 return processChildrenMin(sElement, tagNameMap); 59 } 60 61 68 private static HashMap countXMLChildrenByTagName(Element element) { 69 NodeList children = element.getChildNodes(); 70 HashMap tagNameMap = new HashMap (); 71 72 for (int i = 0; i < children.getLength(); i++) { 73 Node child = children.item(i); 74 if (child.getNodeType() == Node.ELEMENT_NODE) { 75 String key = child.getNodeName(); 76 if (tagNameMap.containsKey(key)) { 77 int value = ((Integer )tagNameMap.get(key)).intValue(); 78 value++; 79 tagNameMap.put(key, new Integer (value)); 80 } else { 81 tagNameMap.put(key, new Integer (1)); 82 } 83 } 84 } 85 86 return tagNameMap; 87 } 88 89 94 private static HashSet processChildrenMax(ISchemaElement sElement, 95 HashMap tagNameMap, Element element) { 96 97 HashSet elementSet = new HashSet (); 98 ISchemaCompositor compositor = 100 ((ISchemaComplexType)sElement.getType()).getCompositor(); 101 int multiplicityTracker = 1; 103 processCompositorMax(compositor, 105 elementSet, tagNameMap, multiplicityTracker, element); 106 return elementSet; 107 } 108 109 114 private static HashSet processChildrenMin(ISchemaElement sElement, 115 HashMap tagNameMap) { 116 117 HashSet elementSet = new HashSet (); 118 ISchemaCompositor compositor = 120 ((ISchemaComplexType)sElement.getType()).getCompositor(); 121 int multiplicityTracker = 1; 123 processCompositorMin(compositor, 125 elementSet, tagNameMap, multiplicityTracker); 126 return elementSet; 127 } 128 129 135 private static void processCompositorMin( 136 ISchemaCompositor compositor, HashSet elementSet, 137 HashMap siblings, int multiplicityTracker) { 138 if (compositor == null) { 144 return; 145 } else if (compositor.getKind() == ISchemaCompositor.CHOICE) { 146 processChoiceMin(compositor, elementSet, siblings, 147 multiplicityTracker); 148 } else if (compositor.getKind() == ISchemaCompositor.SEQUENCE) { 149 processSequenceMin(compositor, elementSet, siblings, 150 multiplicityTracker); 151 } 152 } 153 154 155 161 private static void processCompositorMax( 162 ISchemaCompositor compositor, HashSet elementSet, 163 HashMap siblings, int multiplicityTracker, Element element) { 164 if (compositor == null) { 170 return; 171 } else if (compositor.getKind() == ISchemaCompositor.CHOICE) { 172 processChoiceMax(compositor, elementSet, siblings, 173 multiplicityTracker, element); 174 } else if (compositor.getKind() == ISchemaCompositor.SEQUENCE) { 175 processSequenceMax(compositor, elementSet, siblings, 176 multiplicityTracker, element); 177 } 178 } 179 180 186 private static void processSequenceMin( 187 ISchemaCompositor compositor, HashSet elementSet, HashMap siblings, 188 int multiplicityTracker) { 189 190 ISchemaObject[] schemaObject = compositor.getChildren(); 191 if (multiplicityTracker < Integer.MAX_VALUE) { 193 multiplicityTracker = compositor.getMinOccurs() * multiplicityTracker; 195 } 196 for (int i = 0; i < compositor.getChildCount(); i++) { 198 processObjectMin(schemaObject[i], elementSet, 199 siblings, multiplicityTracker); 200 } 201 } 202 203 209 private static void processSequenceMax( 210 ISchemaCompositor compositor, HashSet elementSet, HashMap siblings, 211 int multiplicityTracker, Element element) { 212 213 ISchemaObject[] schemaObject = compositor.getChildren(); 214 if (multiplicityTracker < Integer.MAX_VALUE) { 216 multiplicityTracker = compositor.getMaxOccurs() * multiplicityTracker; 218 } 219 for (int i = 0; i < compositor.getChildCount(); i++) { 221 processObjectMax(schemaObject[i], elementSet, 222 siblings, multiplicityTracker, element); 223 } 224 } 225 226 232 private static void processChoiceMin( 233 ISchemaCompositor compositor, HashSet elementSet, HashMap siblings, 234 int multiplicityTracker) { 235 236 if (multiplicityTracker < Integer.MAX_VALUE) { 238 multiplicityTracker = compositor.getMinOccurs() * multiplicityTracker; 240 } 241 adjustChoiceMinSiblings(compositor, siblings); 242 243 ISchemaObject[] schemaObject = compositor.getChildren(); 244 for (int i = 0; i < compositor.getChildCount(); i++) { 246 processObjectMin(schemaObject[i], elementSet, 247 siblings, multiplicityTracker); 248 } 249 } 250 251 257 private static void processChoiceMax( 258 ISchemaCompositor compositor, HashSet elementSet, HashMap siblings, 259 int multiplicityTracker, Element element) { 260 261 if (multiplicityTracker < Integer.MAX_VALUE) { 263 multiplicityTracker = compositor.getMaxOccurs() * multiplicityTracker; 265 } 266 adjustChoiceMaxSiblings(compositor, siblings); 267 268 ISchemaObject[] schemaObject = compositor.getChildren(); 269 for (int i = 0; i < compositor.getChildCount(); i++) { 271 processObjectMax(schemaObject[i], elementSet, 272 siblings, multiplicityTracker, element); 273 } 274 } 275 276 280 private static void adjustChoiceMaxSiblings(ISchemaCompositor compositor, 281 HashMap siblings) { 282 283 if (isSimpleChoice(compositor)) { 284 int childElementCount = 290 countChoiceElementChildren(compositor, siblings); 291 updateChoiceElementChildren(compositor, siblings, childElementCount); 292 } else { 293 updateChoiceElementChildren(compositor, siblings, Integer.MIN_VALUE); 300 } 301 } 302 303 307 private static boolean isSimpleChoice(ISchemaCompositor compositor) { 308 ISchemaObject[] schemaObject = compositor.getChildren(); 309 for (int i = 0; i < compositor.getChildCount(); i++) { 313 if (schemaObject[i] instanceof ISchemaCompositor) { 314 return false; 315 } 316 } 317 return true; 318 } 319 320 324 private static void adjustChoiceMinSiblings(ISchemaCompositor compositor, 325 HashMap siblings) { 326 327 if (isSimpleChoice(compositor)) { 328 int childElementCount = 334 countChoiceElementChildren(compositor, siblings); 335 updateChoiceElementChildren(compositor, siblings, childElementCount); 336 } else { 337 updateChoiceElementChildren(compositor, siblings, Integer.MAX_VALUE); 344 } 345 } 346 347 352 private static int countChoiceElementChildren(ISchemaCompositor compositor, 353 HashMap siblings) { 354 ISchemaObject[] schemaObject = compositor.getChildren(); 355 int childElementCount = 0; 358 for (int i = 0; i < compositor.getChildCount(); i++) { 359 if (schemaObject[i] instanceof ISchemaElement) { 360 String name = schemaObject[i].getName(); 361 if (siblings.containsKey(name)) { 362 int occurences = ((Integer )siblings.get(name)).intValue(); 363 if (childElementCount < Integer.MAX_VALUE) { 364 childElementCount = childElementCount + occurences; 365 } 366 } 367 } 368 } 369 return childElementCount; 370 } 371 372 377 private static void updateChoiceElementChildren(ISchemaCompositor compositor, 378 HashMap siblings, int childElementCount) { 379 ISchemaObject[] schemaObject = compositor.getChildren(); 380 for (int i = 0; i < compositor.getChildCount(); i++) { 381 if (schemaObject[i] instanceof ISchemaElement) { 382 String name = schemaObject[i].getName(); 383 siblings.put(name, new Integer (childElementCount)); 384 } else if (schemaObject[i] instanceof ISchemaCompositor) { 385 updateChoiceElementChildren((ISchemaCompositor)schemaObject[i], 386 siblings, childElementCount); 387 } 388 } 389 } 390 391 397 private static void processObjectMax(ISchemaObject schemaObject, 398 HashSet elementSet, HashMap siblings, 399 int multiplicityTracker, Element element) { 400 if (schemaObject instanceof ISchemaElement) { 401 ISchemaElement schemaElement = (ISchemaElement)schemaObject; 402 Element childElement = findChildElement(element, schemaElement.getName()); 403 if (childElement != null) { 404 processElementMax(schemaElement, elementSet, 405 siblings, multiplicityTracker, childElement); 406 } 407 } else if (schemaObject instanceof ISchemaCompositor) { 408 ISchemaCompositor sCompositor = (ISchemaCompositor)schemaObject; 409 processCompositorMax(sCompositor, elementSet, 410 siblings, multiplicityTracker, element); 411 } 412 } 413 414 420 private static void processObjectMin(ISchemaObject schemaObject, 421 HashSet elementSet, HashMap siblings, 422 int multiplicityTracker) { 423 if (schemaObject instanceof ISchemaElement) { 424 ISchemaElement schemaElement = (ISchemaElement)schemaObject; 425 processElementMin(schemaElement, elementSet, 426 siblings, multiplicityTracker); 427 } else if (schemaObject instanceof ISchemaCompositor) { 428 ISchemaCompositor sCompositor = (ISchemaCompositor)schemaObject; 429 processCompositorMin(sCompositor, elementSet, 430 siblings, multiplicityTracker); 431 } 432 } 433 434 440 private static void processElementMax(ISchemaElement schemaElement, 441 HashSet elementSet, HashMap siblings, 442 int multiplicityTracker, Element element) { 443 444 int occurrences = 0; 445 String name = schemaElement.getName(); 446 if (siblings.containsKey(name)) { 448 occurrences = ((Integer ) siblings.get(schemaElement.getName())) 449 .intValue(); 450 } 451 if (multiplicityTracker < Integer.MAX_VALUE) { 453 multiplicityTracker = schemaElement.getMaxOccurs() * multiplicityTracker; 454 } 455 if (occurrences > multiplicityTracker) { 464 elementSet.add(new ElementOccurrenceResult(element, schemaElement, 465 occurrences, multiplicityTracker)); 466 } 467 } 468 469 475 private static void processElementMin(ISchemaElement schemaElement, 476 HashSet elementSet, HashMap siblings, 477 int multiplicityTracker) { 478 479 int occurrences = 0; 480 String name = schemaElement.getName(); 481 if (siblings.containsKey(name)) { 483 occurrences = ((Integer ) siblings.get(schemaElement.getName())) 484 .intValue(); 485 } 486 if (multiplicityTracker < Integer.MAX_VALUE) { 488 multiplicityTracker = schemaElement.getMinOccurs() * multiplicityTracker; 489 } 490 if (occurrences < multiplicityTracker) { 499 elementSet.add(new ElementOccurrenceResult(null, schemaElement, 500 occurrences, multiplicityTracker)); 501 } 502 } 503 504 509 private static Element findChildElement(Element element, String name) { 510 NodeList children = element.getChildNodes(); 511 Element match = null; 512 for (int i = 0; i < children.getLength(); i++) { 513 Node child = children.item(i); 514 if (child.getNodeType() == Node.ELEMENT_NODE) { 515 String key = child.getNodeName(); 516 if (key.equals(name)) { 517 match = (Element)child; 523 } 524 } 525 } 526 return match; 527 } 528 529 } 530 | Popular Tags |