1 19 20 package org.netbeans.modules.schema2beansdev; 21 22 import java.util.*; 23 import java.io.*; 24 25 import org.w3c.dom.*; 26 import org.xml.sax.*; 27 import javax.xml.parsers.*; 28 29 import org.netbeans.modules.schema2beans.*; 30 import org.netbeans.modules.schema2beansdev.gen.XMLWriter; 31 import org.netbeans.modules.schema2beansdev.metadd.*; 32 33 public class XMLSchemaParser extends GeneralParser implements SchemaParser { 34 public final static String JAVA_TYPE_NS = "http://schema2beans.netbeans.org/javaTypes"; 35 36 private DocDefHandler handler; 38 39 private boolean debug; 40 private GenBeans.Config config = null; 41 private Stack parentTypes = new Stack(); 42 private Stack parentUniqueNames = new Stack(); 43 private String lastDefinedType = null; 44 private boolean lastDefinedExternalType = true; 45 private List perAttributeExtraData = new LinkedList(); 46 private String targetNamespace; 47 private Map elementsAlreadyDefined = new IdentityHashMap(); 48 49 SchemaRep schema; 50 51 public XMLSchemaParser(GenBeans.Config config, DocDefHandler handler) { 52 this.config = config; 53 this.filename = config.getFilename(); 54 this.schemaIn = config.getFileIn(); 55 this.handler = handler; 56 this.debug = config.isTraceParse(); 57 58 schema = new SchemaRep(); 59 schema.debug = debug; 60 handler.setPrefixGuesser(schema); 61 } 62 63 public void process() throws java.io.IOException , Schema2BeansException { 64 startupReader(); 65 try { 66 MetaDD mdd = config.getMetaDD(); 67 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 68 dbf.setNamespaceAware(true); 69 dbf.setIgnoringComments(true); 70 dbf.setIgnoringElementContentWhitespace(true); 71 DocumentBuilder db = dbf.newDocumentBuilder(); 72 Document xmlSchema = db.parse(new InputSource(reader)); 73 schema.setCurrentParsedURI(getReaderURI()); 74 if (config.isForME()) 75 schema.setSchemaTypesForME(true); 76 overrideSchemaTypes(); 77 schema.readDocument(xmlSchema); 78 } catch (javax.xml.parsers.ParserConfigurationException e) { 79 throw new Schema2BeansNestedException(Common.getMessage("MSG_FailedToParse", filename), e); 80 } catch (org.xml.sax.SAXException e) { 81 throw new Schema2BeansNestedException(Common.getMessage("MSG_FailedToParse", filename), e); 82 } finally { 83 shutdownReader(); 84 } 85 if (debug) { 86 PrintWriter pw = new PrintWriter(config.messageOut); 87 schema.writeXMLSchemaStandalone(pw); 88 pw.flush(); 89 } 90 schema.optimize(); 91 handler.startDocument(config.getDocRoot()); 92 process(schema.getRootElement()); 93 handler.endDocument(); 94 } 95 96 100 protected void overrideSchemaTypes() { 101 String xsdNS = schema.getNamespaceURI("xsd"); 102 String xmlNS = schema.getNamespaceURI("xml"); 103 for (Iterator it = config.readBeanGraphs(); it.hasNext(); ) { 104 org.netbeans.modules.schema2beansdev.beangraph.BeanGraph bg = (org.netbeans.modules.schema2beansdev.beangraph.BeanGraph) it.next(); 105 for (int i = 0; i < bg.sizeSchemaTypeMapping(); ++i) { 106 org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType stm = bg.getSchemaTypeMapping(i); 107 if (xsdNS.equals(stm.getSchemaTypeNamespace()) || 108 xmlNS.equals(stm.getSchemaTypeNamespace())) { 109 setSchemaType(stm); 110 } 111 } 112 } 113 } 114 115 public void setSchemaType(org.netbeans.modules.schema2beansdev.beangraph.BeanGraph bg) { 116 for (int i = 0; i < bg.sizeSchemaTypeMapping(); ++i) { 117 setSchemaType(bg.getSchemaTypeMapping(i)); 118 } 119 } 120 121 public void setSchemaType(org.netbeans.modules.schema2beansdev.beangraph.SchemaTypeMappingType stm) { 122 schema.setSchemaTypeMapping(stm.getSchemaTypeNamespace(), 123 stm.getSchemaTypeName(), 124 stm.getJavaType()); 125 } 126 127 protected void process(SchemaRep.ElementExpr ee) throws Schema2BeansException { 128 if (ee instanceof SchemaRep.Element) { 129 processElement((SchemaRep.Element) ee); 130 } else if (ee instanceof SchemaRep.ComplexType) { 131 processComplexType((SchemaRep.ComplexType) ee); 133 } else if (ee instanceof SchemaRep.UnionType) { 134 processUnionType((SchemaRep.UnionType) ee); 136 } else if (ee instanceof SchemaRep.SimpleType) { 137 processSimpleType((SchemaRep.SimpleType) ee); 139 } else if (ee instanceof SchemaRep.Restriction) { 140 processRestriction((SchemaRep.Restriction) ee); 141 } else if (ee instanceof SchemaRep.SchemaNode) { 142 processSchemaNode((SchemaRep.SchemaNode) ee); 143 } else if (ee instanceof SchemaRep.ElementInformationItem) { 144 if (parentTypes.empty()) { 145 } else { 147 handler.addExtraDataNode((String ) parentUniqueNames.peek(), 148 (String ) parentTypes.peek(), ee); 149 } 150 } else if (ee instanceof SchemaRep.RestrictionType) { 151 } else if (ee instanceof SchemaRep.ModelGroup) { 153 processModelGroup((SchemaRep.ModelGroup) ee); 154 } else if (ee instanceof SchemaRep.Annotation) { 155 processAnnotation((SchemaRep.Annotation) ee); 156 } else if (ee instanceof SchemaRep.Extension) { 157 processExtension((SchemaRep.Extension) ee); 158 } else if (ee instanceof SchemaRep.ContainsSubElements) { 159 processContainsSubElements((SchemaRep.ContainsSubElements) ee); 160 } else { 161 config.messageOut.println("XMLSchemaPraser.process: Hit unknown ElementExpr: "+ee); 162 } 163 164 } 165 166 protected void processContainsSubElements(SchemaRep.ContainsSubElements cse) throws Schema2BeansException { 167 Iterator it = cse.subElementsIterator(); 168 while (it.hasNext()) { 169 SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it.next(); 170 process(childee); 171 } 172 } 173 174 protected void processContainsSubElementsAndAttributes(SchemaRep.ContainsSubElements cse, String elementName) throws Schema2BeansException { 175 Iterator it = cse.subElementsIterator(); 176 while (it.hasNext()) { 177 SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it.next(); 178 if (childee instanceof SchemaRep.Attribute) { 179 processAttribute(elementName, (SchemaRep.Attribute)childee); 180 } else { 181 process(childee); 182 } 183 } 184 } 185 186 protected void processElement(SchemaRep.Element el) throws Schema2BeansException { 187 boolean alreadyDefined = false; 188 if (elementsAlreadyDefined.containsKey(el)) 189 alreadyDefined = true; 190 else 191 elementsAlreadyDefined.put(el, el); 192 setLastDefined(config.getDefaultElementType(), true); 193 perAttributeExtraData.clear(); 194 195 String name = el.getElementName(); 196 SchemaRep.Restriction[] restrict = null; 197 boolean externalType = false; 198 String schemaType; 199 String namespace = targetNamespace; 200 if (el.getRef() == null) { 201 schemaType = el.getXMLSchemaType(); 202 namespace = el.getElementNamespace(); 203 } else { 204 SchemaRep.Element referredElement = el.getRefElement(); 205 if (referredElement == null) { 206 config.messageOut.println("referredElement is null for "+el); 207 throw new IllegalStateException ("referredElement is null for "+el); 208 } 209 if (elementsAlreadyDefined.containsKey(referredElement)) 210 alreadyDefined = true; 211 else 212 elementsAlreadyDefined.put(referredElement, el); 213 name = referredElement.getElementName(); 214 schemaType = referredElement.getXMLSchemaType(); 215 if (schemaType == null) { 216 schemaType = name; 220 } 221 String ns = schema.prefixOf(name); 222 if (ns != null) { 223 name = schema.removePrefix(name); 224 } 226 namespace = referredElement.getElementNamespace(); 227 } 228 if (debug) 229 config.messageOut.println("processElement (start: elementName="+name+" namespace="+namespace); 230 if (name == null) { 231 config.messageOut.println("WARNING: elementName is null."); 232 } 233 boolean definedInSubElements; 234 if (schemaType == null) { 235 definedInSubElements = true; 236 schemaType = name; 237 } else { 238 definedInSubElements = false; 239 } 240 String fullSchemaType = schema.resolveNamespaceDefault(schemaType, namespace); 241 247 String defaultValue = el.getDefault(); 248 if (debug) 249 config.messageOut.println("processElement: name="+name+" schemaType="+schemaType+" fullSchemaType="+fullSchemaType+" definedInSubElements="+definedInSubElements); 250 if (!definedInSubElements) { 251 SchemaRep.ElementExpr schemaTypeDef = schema.getSchemaTypeDef(schemaType); 252 if (schemaTypeDef instanceof SchemaRep.ContainsSubElements) 253 if (hasUnionType((SchemaRep.ContainsSubElements)schemaTypeDef)) 254 handler.setUnion(el.getFullContentName(), fullSchemaType, true); 255 if (schemaTypeDef instanceof SchemaRep.ContainsSubElements) { 258 SchemaRep.ContainsSubElements cse = (SchemaRep.ContainsSubElements) schemaTypeDef; 259 restrict = lookForRestriction(cse); 260 String foundDefault = lookForDefault(restrict); 261 if (foundDefault != null) 262 defaultValue = foundDefault; 263 } 264 String javaType = null; 265 if (schemaTypeDef instanceof SchemaRep.HasJavaTypeName) { 266 javaType = ((SchemaRep.HasJavaTypeName)schemaTypeDef).getJavaTypeName(); 267 } else { 268 String ns = schema.prefixOf(schemaType); 269 String nsURI = schema.getNamespaceURI(ns); 270 if (JAVA_TYPE_NS.equals(nsURI)) { 271 javaType = schema.removePrefix(schemaType); 272 } 273 } 274 handler.element(el.getFullContentName(), fullSchemaType, 275 name, namespace, 276 getInstanceValue(el.getMinOccurs(), 277 el.getMaxOccurs()), 278 externalType, defaultValue); 279 addExtraDataForType(el.getFullContentName(), fullSchemaType, schemaTypeDef); 280 if (javaType != null) { 281 handler.javaType(el.getFullContentName(), fullSchemaType, javaType); 283 if (parentTypes.isEmpty()) { 284 String mySchemaType = schema.resolveNamespaceDefault(name, namespace); 286 handler.javaType(el.getFullContentName(), mySchemaType, javaType); 288 } 289 } 290 handler.nillable(el.isNillable()); 291 } else { 292 restrict = lookForRestriction(el); 293 boolean existsAlready; 294 while (!alreadyDefined) { 295 existsAlready = handler.doesElementExist(fullSchemaType); 296 if (!existsAlready) 297 break; 298 if (debug) 299 config.messageOut.println("existsAlready: "+existsAlready+", "+el); 300 String contextName = null; 301 if (!parentTypes.isEmpty()) { 302 contextName = (String ) parentTypes.peek(); 303 int curlyBracePos = contextName.lastIndexOf('}'); 304 if (curlyBracePos >= 0) 305 contextName = contextName.substring(curlyBracePos+1); 306 } 307 if (contextName == null) 308 contextName = "other"; 309 fullSchemaType += '-' + contextName; 310 if (debug) 311 config.messageOut.println("New name: "+fullSchemaType); 312 } 313 } 314 parentTypes.push(fullSchemaType); 315 parentUniqueNames.push(el.getFullContentName()); 316 processContainsSubElements(el); 317 318 parentUniqueNames.pop(); 319 parentTypes.pop(); 320 321 if (definedInSubElements) { 322 if (!parentTypes.isEmpty()) { 329 if (restrict != null) { 330 String foundDefault = lookForDefault(restrict); 331 if (foundDefault != null) 332 defaultValue = foundDefault; 333 } 334 handler.element(el.getFullContentName(), lastDefinedType, 335 name, namespace, 336 getInstanceValue(el.getMinOccurs(), 337 el.getMaxOccurs()), 338 lastDefinedExternalType, defaultValue); 339 handler.nillable(el.isNillable()); 340 } 341 SchemaRep.ElementExpr schemaTypeDef = schema.getSchemaTypeDefResolvedNamespace(lastDefinedType); 342 if (schemaTypeDef instanceof SchemaRep.HasJavaTypeName) { 343 String javaType = ((SchemaRep.HasJavaTypeName)schemaTypeDef).getJavaTypeName(); 344 if (javaType != null) { 346 handler.javaType(el.getFullContentName(), 347 lastDefinedType, javaType); 348 } 349 } 350 } 351 352 if (restrict != null) { 354 addExtraDataCurLink(restrict); 355 } 356 if (!("1".equals(el.getMaxOccurs()) || "unbounded".equals(el.getMaxOccurs()))) { 357 handler.addExtraDataCurLink(new MaxOccursRestriction(el.getMaxOccurs())); 359 } 360 if (!("1".equals(el.getMinOccurs()) || "0".equals(el.getMinOccurs()))) { 361 handler.addExtraDataCurLink(new MinOccursRestriction(el.getMinOccurs())); 363 } 364 if (perAttributeExtraData.size() > 0) { 365 for (Iterator it = perAttributeExtraData.iterator(); it.hasNext(); ) { 366 handler.addExtraDataCurLink(it.next()); 367 } 368 } 369 370 if (debug) 371 config.messageOut.println("processElement finish): elementName="+name); 372 } 373 374 protected boolean hasUnionType(SchemaRep.ContainsSubElements schemaTypeDef) { 375 if (schemaTypeDef instanceof SchemaRep.UnionType) 376 return true; 377 Iterator itr = schemaTypeDef.subElementsIterator(); 378 while (itr.hasNext()) { 379 SchemaRep.ElementExpr ee = (SchemaRep.ElementExpr) itr.next(); 380 if (ee instanceof SchemaRep.ContainsSubElements) { 381 if (hasUnionType((SchemaRep.ContainsSubElements)ee)) 382 return true; 383 } 384 } 385 return false; 386 } 387 388 protected SchemaRep.Restriction[] lookForRestriction(SchemaRep.ContainsSubElements schemaTypeDef) { 389 if (schemaTypeDef instanceof SchemaRep.UnionType) { 390 ArrayList restrictions = new ArrayList(); 391 SchemaRep.Restriction restricts[] = null; 392 410 SchemaRep.ElementExpr[] eeList = 411 ((SchemaRep.UnionType)schemaTypeDef).getMemberTypeElements(); 412 if (eeList != null) { 413 for (int i=0; i < eeList.length; i++) { 414 restricts = null; 415 if (eeList[i] instanceof SchemaRep.ContainsSubElements) 416 restricts = lookForRestriction((SchemaRep.ContainsSubElements)eeList[i]); 417 if (restricts != null) { 418 for (int j=0; j < restricts.length; j++) 419 restrictions.add(restricts[j]); 420 } 421 } 422 } 423 Iterator itr = schemaTypeDef.subElementsIterator(); 424 while (itr.hasNext()) { 425 restricts = null; 426 SchemaRep.ElementExpr ee = (SchemaRep.ElementExpr) itr.next(); 427 if (ee instanceof SchemaRep.ContainsSubElements) 428 restricts = lookForRestriction((SchemaRep.ContainsSubElements)ee); 429 else { 430 continue; 431 } 432 if (restricts != null) { 433 for (int i=0; i < restricts.length; i++) 434 restrictions.add(restricts[i]); 435 } 436 } 437 if (restrictions.size() == 0) 438 return null; 439 restricts = new SchemaRep.Restriction[restrictions.size()]; 440 return (SchemaRep.Restriction[])restrictions.toArray(restricts); 441 } else if (schemaTypeDef instanceof SchemaRep.SimpleType) { 442 SchemaRep.ContainsSubElements sube = 443 (SchemaRep.ContainsSubElements) 444 schemaTypeDef.findSubElement(SchemaRep.UnionType.class); 445 if (sube != null) 446 return lookForRestriction(sube); 447 else { 448 SchemaRep.Restriction restrict = 449 (SchemaRep.Restriction) 450 schemaTypeDef.findSubElement( 451 SchemaRep.Restriction.class); 452 if (restrict == null) 453 return null; 454 return (new SchemaRep.Restriction[] { restrict }); 455 } 456 } else if (schemaTypeDef instanceof SchemaRep.SimpleContent) { 457 SchemaRep.Restriction restrict = 458 (SchemaRep.Restriction) 459 schemaTypeDef.findSubElement( 460 SchemaRep.Restriction.class); 461 if (restrict == null) 462 return null; 463 return (new SchemaRep.Restriction[] { restrict }); 464 } else if (schemaTypeDef instanceof SchemaRep.Element) { 465 return lookForRestriction((SchemaRep.ContainsSubElements)schemaTypeDef.findSubElement(SchemaRep.SimpleType.class)); 466 } else if (schemaTypeDef instanceof SchemaRep.ComplexType) { 467 return lookForRestriction((SchemaRep.ContainsSubElements)schemaTypeDef.findSubElement(SchemaRep.SimpleContent.class)); 468 } 469 return null; 470 } 471 472 protected void processComplexType(SchemaRep.ComplexType el) throws Schema2BeansException { 473 setLastDefined(null, true); 474 String name = el.getTypeName(); 475 if (debug) 476 config.messageOut.println("processComplexType: el="+el); 477 if (name == null) { 478 if (debug) 479 config.messageOut.println("Found unnamed complexType."); 480 if (!parentTypes.isEmpty()) 481 name = (String ) parentTypes.peek(); 482 if (name == null) 483 name = el.getFullContentName(); 484 } else { 485 name = schema.resolveNamespace(name); 486 } 487 parentTypes.push(name); 488 parentUniqueNames.push(el.getFullContentName()); 489 handler.startElement(el.getFullContentName(), name, Common.ELEMENT); 490 handler.setAbstract(el.getFullContentName(), name, el.isAbstract()); 491 for (Iterator it = el.subElementsIterator(); it.hasNext(); ) { 492 SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it.next(); 493 if (childee instanceof SchemaRep.ModelGroup) { 494 processModelGroup((SchemaRep.ModelGroup) childee); 495 } else if (childee instanceof SchemaRep.Attribute) { 496 processAttribute(name, (SchemaRep.Attribute)childee); 497 } else if (childee instanceof SchemaRep.AttributeGroup) { 498 processAttributeGroup(name, (SchemaRep.AttributeGroup)childee); 499 } else if (childee instanceof SchemaRep.SimpleContent) { 500 processSimpleContent((SchemaRep.SimpleContent) childee); 501 } else if (childee instanceof SchemaRep.Annotation) { 502 processAnnotation((SchemaRep.Annotation) childee); 503 } else if (childee instanceof SchemaRep.ComplexContent) { 504 processComplexContent((SchemaRep.ComplexContent) childee); 505 } else { 506 config.messageOut.println("processComplexType: Unfamiliar subelement: "+childee); 507 } 508 } 509 handler.endElement(); 510 parentUniqueNames.pop(); 511 parentTypes.pop(); 512 setLastDefined(name, false); 513 } 514 515 protected void processComplexContent(SchemaRep.ComplexContent el) throws Schema2BeansException { 516 for (Iterator it = el.subElementsIterator(); it.hasNext(); ) { 518 SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it.next(); 519 if (childee instanceof SchemaRep.Extension) { 520 processExtension((SchemaRep.Extension) childee); 521 } else if (childee instanceof SchemaRep.Restriction) { 522 processRestriction((SchemaRep.Restriction) childee); 523 } else if (childee instanceof SchemaRep.Annotation) { 524 processAnnotation((SchemaRep.Annotation) childee); 525 } else { 526 config.messageOut.println("processComplexContent: Unfamiliar subelement: "+childee); 527 } 528 } 529 } 530 531 protected void processSimpleContent(SchemaRep.SimpleContent el) throws Schema2BeansException { 532 processContainsSubElements(el); 533 if (lastDefinedType == null) 535 return; 536 SchemaRep.ElementExpr schemaTypeDef = schema.getSchemaTypeDefResolvedNamespace(lastDefinedType); 537 if (schemaTypeDef == null) 538 return; 539 String javaType = null; 541 if (schemaTypeDef instanceof SchemaRep.HasJavaTypeName) { 542 javaType = ((SchemaRep.HasJavaTypeName)schemaTypeDef).getJavaTypeName(); 543 } 544 addExtraDataForType((String ) parentUniqueNames.peek(), 545 (String ) parentTypes.peek(), schemaTypeDef); 546 if (javaType != null) { 547 handler.javaType((String ) parentUniqueNames.peek(), 548 (String ) parentTypes.peek(), javaType); 549 } 550 } 551 552 protected void processExtension(SchemaRep.Extension el) throws Schema2BeansException { 553 if (debug) 554 config.messageOut.println("extension el="+el); 555 String uniqueName = (String ) parentUniqueNames.peek(); 556 String name = (String ) parentTypes.peek(); 557 String base = el.getBase(); 558 SchemaRep.ElementExpr baseDef = schema.getSchemaTypeDef(base); 559 SchemaRep.Restriction[] restrict = null; 561 if (baseDef instanceof SchemaRep.ContainsSubElements) { 562 restrict = lookForRestriction((SchemaRep.ContainsSubElements)baseDef); 563 if (!config.isRespectExtension()) 565 processContainsSubElementsAndAttributes((SchemaRep.ContainsSubElements)baseDef, name); 566 } 567 addExtraDataForType(uniqueName, name, baseDef); 568 if (baseDef instanceof SchemaRep.ComplexType) { 569 SchemaRep.ComplexType complexType = (SchemaRep.ComplexType) baseDef; 570 String resolvedExtendsName = schema.resolveNamespace(complexType.getTypeName()); 571 handler.setExtension(uniqueName, name, resolvedExtendsName); 573 } 574 String javaType = el.getJavaTypeName(); 575 if (javaType != null) { 576 if (debug) 577 config.messageOut.println("Setting javatype of "+name+" to "+javaType); 578 handler.javaType(uniqueName, name, javaType); 579 if (restrict != null) { 580 addExtraDataNode(uniqueName, name, restrict); 581 } 582 } 583 processContainsSubElementsAndAttributes(el, name); 584 } 585 586 protected void addExtraDataForType(String uniqueName, String name, 587 SchemaRep.ElementExpr schemaTypeDef) throws Schema2BeansException { 588 if (schemaTypeDef instanceof SchemaRep.Base64Binary || 589 schemaTypeDef instanceof SchemaRep.HexBinary) { 590 handler.addExtraDataNode(uniqueName, name, schemaTypeDef); 591 } else if (schemaTypeDef instanceof SchemaRep.ContainsSubElements) { 593 SchemaRep.Restriction[] restrict = 594 lookForRestriction((SchemaRep.ContainsSubElements) schemaTypeDef); 595 if (restrict != null) 597 for (int i=0; i < restrict.length; i++) 598 addExtraDataForType(uniqueName, name, 599 schema.getSchemaTypeDef(restrict[i].getBase())); 600 } else { 601 } 603 } 604 605 protected void processModelGroup(SchemaRep.ModelGroup group) throws Schema2BeansException { 606 if (debug) 607 config.messageOut.println("processModelGroup: group="+group); 608 if (group instanceof SchemaRep.Group) { 609 SchemaRep.Group grp = (SchemaRep.Group) group; 610 if (grp.getRef() == null) { 611 return; 614 } else { 615 SchemaRep.Group referredGroup = grp.getRefGroup(); 616 if (referredGroup == null) { 617 config.messageOut.println(Common.getMessage("MSG_UnableToFind", "group", grp.getRef())); 618 } else { 619 processContainsSubElements(referredGroup); 620 } 621 return; 622 } 623 } 624 char separator = ' '; 625 if (group instanceof SchemaRep.Sequence) 626 separator = ','; 627 else if (group instanceof SchemaRep.Choice) 628 separator = '|'; 629 int groupInstance = getInstanceValue(group.getMinOccurs(), 630 group.getMaxOccurs()); 631 handler.startGroupElements(); 632 633 boolean first = true; 634 Iterator it = group.subElementsIterator(); 635 while (it.hasNext()) { 636 if (first) 637 first = false; 638 else 639 handler.character(separator); 640 641 SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it.next(); 642 if (childee instanceof SchemaRep.Element) { 643 processElement((SchemaRep.Element) childee); 644 } else if (childee instanceof SchemaRep.ModelGroup) { 645 processModelGroup((SchemaRep.ModelGroup) childee); 646 } else if (childee instanceof SchemaRep.Annotation) { 647 } else if (childee instanceof SchemaRep.Any) { 648 processAny((SchemaRep.Any) childee); 649 } else { 650 config.messageOut.println("processModelGroup: Unfamiliar subelement: "+childee); 651 } 652 } 653 654 handler.endGroupElements(groupInstance); 655 } 656 657 protected void processSimpleType(SchemaRep.SimpleType el) throws Schema2BeansException { 658 if (debug) 659 config.messageOut.println("processSimpleType: el="+el); 660 667 processContainsSubElements(el); 668 676 } 677 678 protected void processUnionType(SchemaRep.UnionType el) throws Schema2BeansException { 679 if (debug) 680 config.messageOut.println("processUnionType: el="+el); 681 688 700 processContainsSubElements(el); 701 709 } 710 711 protected void processRestriction(SchemaRep.Restriction el) throws Schema2BeansException { 712 722 setLastDefined(schema.resolveNamespace(el.getBase()), false); 724 processContainsSubElements(el); 727 } 728 729 protected void processAny(SchemaRep.Any el) throws Schema2BeansException { 730 if (debug) 731 config.messageOut.println("Found "+el); 732 String namespace = el.getNamespace(); 733 if (namespace != null && namespace.startsWith("##")) 734 namespace = null; 735 handler.element(el.getFullContentName(), "any", 736 "any", namespace, 737 getInstanceValue(el.getMinOccurs(), 738 el.getMaxOccurs()), 739 true, null); 740 handler.javaType("any", "any", "org.w3c.dom.Element"); 741 handler.addExtraDataCurLink(el); 742 } 743 744 protected void processSchemaNode(SchemaRep.SchemaNode sn) throws Schema2BeansException { 745 targetNamespace = sn.getTargetNamespace(); 746 if (targetNamespace != null && !"".equals(targetNamespace)) 747 handler.setDefaultNamespace(targetNamespace); 748 processContainsSubElements(sn); 749 } 750 751 protected void setLastDefined(String typeName) { 752 this.lastDefinedType = typeName; 754 this.lastDefinedExternalType = false; 755 } 756 757 protected void setLastDefined(String typeName, boolean externalType) { 758 this.lastDefinedType = typeName; 760 this.lastDefinedExternalType = externalType; 761 } 762 763 protected void addTopAttributes(SchemaRep.Element parentElement, 764 SchemaRep.Element el) { 765 Iterator it = el.subElementsIterator(); 767 while (it.hasNext()) { 768 SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it.next(); 769 if (childee instanceof SchemaRep.Attribute) { 770 } else if (childee instanceof SchemaRep.AttributeGroup) { 772 } else if (childee instanceof SchemaRep.ComplexType) { 774 addTopAttributes(parentElement, (SchemaRep.ComplexType) childee); 775 } 776 } 777 } 778 779 protected void addTopAttributes(SchemaRep.Element parentElement, 780 SchemaRep.ComplexType el) { 781 Iterator it = el.subElementsIterator(); 782 while (it.hasNext()) { 783 SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it.next(); 784 if (childee instanceof SchemaRep.Attribute) { 785 } else if (childee instanceof SchemaRep.AttributeGroup) { 787 } 789 } 790 } 791 792 protected void processAttribute(String parentElement, 793 SchemaRep.Attribute attr) throws Schema2BeansException { 794 if (debug) 795 config.messageOut.println("processAttribute to "+parentElement+" attr="+attr); 796 if (attr.getRef() != null) { 797 SchemaRep.Attribute referredAttr = attr.getRefAttribute(); 798 if (referredAttr == null) { 799 config.messageOut.println(Common.getMessage("MSG_UnableToFind", "attribute", attr.getRef())); 800 } else { 801 processAttribute(parentElement, referredAttr); 802 } 803 return; 804 } 805 806 String attributeName = attr.getAttributeName(); 808 boolean externalType = true; 809 String attrType = attr.getJavaType(); 810 String schemaType = attr.getType(); 811 String defaultValue = attr.getDefaultValue(); 812 SchemaRep.Restriction[] restrict = null; 813 SchemaRep.ElementExpr ee = null; 814 if (schemaType != null && defaultValue == null) { 815 ee = schema.getSchemaTypeDef(schemaType); 816 if (ee instanceof SchemaRep.SimpleType) { 817 SchemaRep.SimpleType st = (SchemaRep.SimpleType) ee; 818 SchemaRep.Restriction r = 819 (SchemaRep.Restriction) st.findSubElement("restriction"); 820 if (r != null) 821 restrict = new SchemaRep.Restriction[] { r }; 822 String foundDefault = lookForDefault(restrict); 824 if (foundDefault != null) 825 defaultValue = foundDefault; 826 } else { 827 config.messageOut.println("Type for attribute "+attributeName+" is not simple enough: "+ee); 828 } 829 } 831 int instance; 832 if (defaultValue != null || attr.getFixed() != null || attr.isRequired()) 833 instance = Common.TYPE_1; 834 else { 835 instance = Common.TYPE_0_1; 836 } 837 handler.startElement(attr.getFullContentName(), 838 parentElement, 839 Common.ATTLIST); 840 handler.element(attr.getFullContentName(), attrType, 841 attributeName, attr.getAttributeNamespace(), 842 instance, externalType, defaultValue); 843 handler.element("CDATA", "CDATA", instance); 844 if (attr.getFixed() != null) { 845 handler.element("#FIXED", "#FIXED", instance); handler.element(attr.getFullContentName(), attrType, 847 attr.getFixed(), null, instance, externalType, 848 defaultValue); 849 } else if (attr.isRequired()) { 850 handler.element("#REQUIRED", "#REQUIRED", instance); } else { 852 handler.element("#IMPLIED", "#IMPLIED", instance); } 854 handler.javaType(attr.getFullContentName(), attr.getAttributeName(), 855 attrType); 856 if (ee != null) { 857 addExtraDataForType(attr.getFullContentName(), attr.getAttributeName(), ee); 858 } 859 if (restrict != null) { 860 addExtraDataCurLink(restrict); 861 } 862 handler.endElement(); 863 } 864 865 protected void addExtraDataCurLink(SchemaRep.Restriction[] restrict) { 866 for (int i=0; i < restrict.length; i++) 868 if (restrict[i].subElementsIterator().hasNext()) 869 handler.addExtraDataCurLink(restrict[i]); 870 879 } 880 881 protected void addExtraDataNode(String uniqueName, String name, 882 SchemaRep.Restriction[] restrict) throws org.netbeans.modules.schema2beans.Schema2BeansException { 883 for (int i=0; i < restrict.length; i++) 884 if (restrict[i].subElementsIterator().hasNext()) 885 handler.addExtraDataNode(uniqueName, name, restrict[i]); 886 894 } 895 896 899 protected String lookForDefault(SchemaRep.Restriction[] restrict) { 900 if (config.isMakeDefaults() && restrict != null) { 901 for (int i=0; i < restrict.length; i++) 902 for (Iterator subelements = restrict[i].subElementsIterator(); 903 subelements.hasNext(); ) { 904 Object rt = subelements.next(); 905 if (rt instanceof SchemaRep.Enumeration) { 910 String defaultValue = ((SchemaRep.Enumeration)rt).getValue(); 911 return defaultValue; 913 } 914 } 915 } 916 return null; 917 } 918 919 protected void processAttributeGroup(String parentElement, 920 SchemaRep.AttributeGroup attrGroup) throws Schema2BeansException { 921 SchemaRep.AttributeGroup schemaTypeDef = (SchemaRep.AttributeGroup) schema.getSchemaTypeDef(attrGroup.getRef()); 922 if (debug) 923 config.messageOut.println("processAttributeGroup schemaTypeDef="+schemaTypeDef); 924 if (schemaTypeDef == null) 925 throw new IllegalStateException ("attributeGroup ref has reference to unknown name: "+attrGroup.getRef()); 926 927 Iterator it = schemaTypeDef.subElementsIterator(); 928 while (it.hasNext()) { 929 SchemaRep.ElementExpr childee = (SchemaRep.ElementExpr) it.next(); 930 if (childee instanceof SchemaRep.Attribute) { 931 processAttribute(parentElement, (SchemaRep.Attribute) childee); 932 } else if (childee instanceof SchemaRep.AttributeGroup) { 933 processAttributeGroup(parentElement, (SchemaRep.AttributeGroup) childee); 934 } 935 } 936 } 937 938 protected void processAnnotation(SchemaRep.Annotation ann) throws Schema2BeansException { 939 SchemaRep.Documentation doc = (SchemaRep.Documentation) ann.findSubElement(SchemaRep.Documentation.class); 940 String name = null; 941 if (!parentTypes.isEmpty()) 942 name = (String ) parentTypes.peek(); 943 if (name == null) 944 return; 945 String uniqueName = (String ) parentUniqueNames.peek(); 946 if (doc != null) { 947 StringBuffer comment = new StringBuffer (); 948 for (Iterator subelements = doc.subElementsIterator(); 949 subelements.hasNext(); ) { 950 SchemaRep.ElementExpr el = (SchemaRep.ElementExpr) subelements.next(); 951 if (el instanceof SchemaRep.TextNode) { 952 comment.append(((SchemaRep.TextNode)el).getText()); 953 } else if (el instanceof SchemaRep.AnyNode) { 954 try { 955 XMLWriter xw = new XMLWriter(false); 956 ((SchemaRep.AnyNode)el).writeXMLSchema(xw); 957 xw.writeTo(comment); 958 } catch (IOException e) { 959 throw new RuntimeException (e); 961 } 962 } 963 } 964 handler.setExtendedProperty(uniqueName, name, "comment", comment.toString()); 965 } 966 SchemaRep.AppInfo appInfo = (SchemaRep.AppInfo) ann.findSubElement(SchemaRep.AppInfo.class); 967 if (appInfo != null) { 968 String switchName = null; 969 String switchHelp = null; 970 boolean switchMandatory = false; 971 for (Iterator subelements = appInfo.subElementsIterator(); 972 subelements.hasNext(); ) { 973 SchemaRep.ElementExpr el = (SchemaRep.ElementExpr) subelements.next(); 974 if (el instanceof SchemaRep.AnyNode) { 975 SchemaRep.AnyNode anyNode = (SchemaRep.AnyNode) el; 976 String anyNodeName = anyNode.getContentName(); 977 if (anyNodeName == null) 978 continue; 979 anyNodeName = anyNodeName.intern(); 980 if ("extends" == anyNodeName) { 981 SchemaRep.TextNode value = (SchemaRep.TextNode) anyNode.findSubElement(SchemaRep.TextNode.class); 982 if (value != null) { 983 handler.setExtendedProperty(uniqueName, name, "extends", value.getText()); 984 } 985 } else if ("implements" == anyNodeName) { 986 SchemaRep.TextNode value = (SchemaRep.TextNode) anyNode.findSubElement(SchemaRep.TextNode.class); 987 if (value != null) { 988 handler.setExtendedProperty(uniqueName, name, "implements", value.getText()); 989 } 990 } else if ("switch" == anyNodeName) { 991 SchemaRep.TextNode value = (SchemaRep.TextNode) anyNode.findSubElement(SchemaRep.TextNode.class); 992 if (value != null) 993 switchName = value.getText(); 994 } else if ("switchHelp" == anyNodeName) { 995 SchemaRep.TextNode value = (SchemaRep.TextNode) anyNode.findSubElement(SchemaRep.TextNode.class); 996 if (value != null) 997 switchHelp = value.getText(); 998 } else if ("switchMandatory" == anyNodeName) { 999 SchemaRep.TextNode value = (SchemaRep.TextNode) anyNode.findSubElement(SchemaRep.TextNode.class); 1000 if (value != null) { 1001 switchMandatory = "true".equalsIgnoreCase(value.getText()); 1002 } 1003 } 1004 } 1005 } 1006 if (switchName != null) { 1007 perAttributeExtraData.add(new SwitchData(switchName, 1008 switchHelp, 1009 switchMandatory)); 1010 } 1011 } 1012 } 1013 1014 static protected int getInstanceValue(String minOccurs, String maxOccurs) { 1015 if (minOccurs == null) 1016 minOccurs = "1"; 1017 if (maxOccurs == null) 1018 maxOccurs = "1"; 1019 1020 if (minOccurs.equals("0")) { 1021 if (maxOccurs.equals("1")) 1022 return Common.TYPE_0_1; 1023 return Common.TYPE_0_N; 1025 } 1026 if (maxOccurs.equals("1")) 1027 return Common.TYPE_1; 1028 return Common.TYPE_1_N; 1029 } 1030 1031 public static class MaxOccursRestriction implements DataListRestriction { 1032 private String maxOccurs; 1033 1034 public MaxOccursRestriction(String maxOccurs) { 1035 this.maxOccurs = maxOccurs; 1037 } 1038 1039 public void genRestriction(Writer out, String sizeExpr, 1040 String readMethod, String type, 1041 String failVar, boolean passCheck) 1042 throws IOException { 1043 if (!passCheck) { 1044 out.write("if ("+sizeExpr+" > "+maxOccurs+") {\n"); 1045 out.write(failVar+" = true;\n"); 1046 out.write("}\n"); 1047 } else { 1048 out.write("if ("+sizeExpr+" <= "+maxOccurs+") {\n"); 1049 out.write(failVar+" = true;\n"); 1050 out.write("}\n"); 1051 } 1052 } 1053 1054 public int getMaxOccurs() { 1055 if ("unbounded".equalsIgnoreCase(maxOccurs)) 1056 return Integer.MAX_VALUE; 1057 return Integer.parseInt(maxOccurs); 1058 } 1059 1060 public String toString() { 1061 return "maxOccurs ("+maxOccurs+")"; 1062 } 1063 1064 public String genAnnotation() { 1065 return "MaxOccurs("+maxOccurs+")"; 1066 } 1067 1068 } 1069 1070 public static class MinOccursRestriction implements DataListRestriction { 1071 private String minOccurs; 1072 1073 public MinOccursRestriction(String minOccurs) { 1074 this.minOccurs = minOccurs; 1076 } 1077 1078 public void genRestriction(Writer out, String sizeExpr, 1079 String readMethod, String type, 1080 String failVar, boolean passCheck) 1081 throws IOException { 1082 if (!passCheck) { 1083 out.write("if ("+sizeExpr+" < "+minOccurs+") {\n"); 1084 out.write(failVar+" = true;\n"); 1085 out.write("}\n"); 1086 } else { 1087 out.write("if ("+sizeExpr+" >= "+minOccurs+") {\n"); 1088 out.write(failVar+" = true;\n"); 1089 out.write("}\n"); 1090 } 1091 } 1092 1093 public int getMinOccurs() { 1094 return Integer.parseInt(minOccurs); 1095 } 1096 1097 public String toString() { 1098 return "minOccurs ("+minOccurs+")"; 1099 } 1100 1101 public String genAnnotation() { 1102 return "MinOccurs("+minOccurs+")"; 1103 } 1104 1105 } 1106 1107 public static class GeneralAnnotation implements HasAnnotation { 1108 private String annotation; 1109 1110 public GeneralAnnotation(String annotation) { 1111 this.annotation = annotation; 1112 } 1113 1114 public String genAnnotation() { 1115 return annotation; 1116 } 1117 } 1118 1119 public static class SwitchData { 1120 private String switchName; 1121 private String switchHelp; 1122 private boolean mandatory; 1123 1124 public SwitchData(String switchName) { 1125 this.switchName = switchName; 1126 } 1127 1128 public SwitchData(String switchName, String switchHelp) { 1129 this.switchName = switchName; 1130 this.switchHelp = switchHelp; 1131 } 1132 1133 public SwitchData(String switchName, String switchHelp, boolean mandatory) { 1134 this.switchName = switchName; 1135 this.switchHelp = switchHelp; 1136 this.mandatory = mandatory; 1137 } 1138 1139 public String getName() { 1140 return switchName; 1141 } 1142 1143 public String getHelp() { 1144 return switchHelp; 1145 } 1146 1147 public boolean isMandatory() { 1148 return mandatory; 1149 } 1150 1151 public String toString() { 1152 return "Switch"; 1153 } 1154 } 1155} 1156 | Popular Tags |