예제 #1
0
    /**
     * Processes the given ComplexType and creates all necessary class
     * to support it
     * @param complexType the ComplexType to process
    **/
    private void processComplexType(ComplexType complexType, SGStateInfo sInfo) {

        if (complexType == null) return;

        ClassInfo classInfo = sInfo.resolve(complexType);

        if (classInfo == null) {

            //-- handle top-leve complextypes
            if (complexType.isTopLevel()) {

                JClass[] classes
                    = sourceFactory.createSourceCode(complexType, sInfo);

                for (int i = 0; i < classes.length; i++)
                    processJClass(classes[i], sInfo);
            }

            //-- process base complextype if necessary
            XMLType baseType= complexType.getBaseType();
            if (baseType != null &&
				baseType.getSchema() == complexType.getSchema()) {

				if (baseType.isComplexType())
                    processComplexType((ComplexType)baseType, sInfo);
            }

            //-- process AttributeDecl
            processAttributes(complexType, sInfo);

            //-- process ContentModel
            processContentModel(complexType, sInfo);

        }
        else {
            JClass jClass = classInfo.getJClass();
            if (!sInfo.processed(jClass)) {
                //-- process AttributeDecl
                processAttributes(complexType, sInfo);
                //-- process ContentModel
                processContentModel(complexType, sInfo);
                processJClass(jClass, sInfo);
            }
        }
    } //-- processComplexType
예제 #2
0
 /**
  * Sets the base type for this ComplexType
  *
  * @param baseType the base type which this ComplexType extends or restricts
  */
 public void setBaseType(XMLType baseType) {
   super.setBaseType(baseType);
   if (baseType != null) {
     if (baseType.isSimpleType()) {
       _complexContent = false;
       _content = new SimpleContent((SimpleType) baseType);
     } else if (baseType.isComplexType()) {
       ComplexType complexType = (ComplexType) baseType;
       if (complexType.isSimpleContent()) {
         _complexContent = false;
         _content = ((SimpleContent) complexType.getContentType()).copy();
       } else _complexContent = true;
     } else {
       // -- assuming anyType
       _complexContent = true;
     }
   }
 } // -- setBaseType
예제 #3
0
    private void createClasses(ElementDecl elementDecl, SGStateInfo sInfo) {

		//-- when mapping schema types, only interested in producing classes
		//-- for elements with anonymous complex types
		if (SourceGenerator.mappingSchemaType2Java())
			if (elementDecl.isReference() ||
				(elementDecl.getType()!=null &&
				 elementDecl.getType().getName()!=null))
				return;

		if (sInfo.verbose()) {
		    System.out.print("Creating classes for element: ");
		    System.out.println(elementDecl.getName());
		}
        //-- create classes for sub-elements if necessary
        XMLType xmlType = elementDecl.getType();

        //-- No type definition
        if (xmlType == null) {
            System.out.print("Type not found for element: ");
            System.out.println(elementDecl.getName());
            return;
        }
        //-- ComplexType
        else if (xmlType.isComplexType()) {

			JClass[] classes = sourceFactory.createSourceCode(elementDecl,
                                                        sInfo);

            processComplexType((ComplexType)xmlType, sInfo);

            for (int i = 0; i < classes.length; i++)
                 processJClass(classes[i], sInfo);

        }
        //-- SimpleType
        else {
            processSimpleType((SimpleType)xmlType, sInfo);
        }

    }  //-- createClasses
예제 #4
0
 /**
  * Merges the two element declarations. The resulting
  * merge is placed in ElementDecl e1.
  *
  * @param e1 the main ElementDecl 
  * @param e2 the secondary ElementDecl to merge with e1
 **/
 private void merge(ElementDecl e1, ElementDecl e2) 
     throws SchemaException
 {
     
     XMLType e1Type = e1.getType();
     XMLType e2Type = e2.getType();
      
     //-- Make sure types are not null and if so create them
     if (e1Type == null) {
         if (e2Type == null) return; //-- nothing to merge
         else {
             if (e2Type.isSimpleType()) {
                 e1.setType(e2Type);
             }
             else {
                 ComplexType cType = new ComplexType(_schema);
                 Group group = new Group();
                 group.setOrder(_defaultGroupOrder);
                 cType.addGroup(group);
                 e1.setType(cType);
                 e1Type = cType;
             }
         }
     }
     else if (e2Type == null) {
         if (e1Type.isSimpleType()) {
             e2.setType(e1Type);
         }
         else {
             ComplexType cType = new ComplexType(_schema);
             Group group = new Group();
             group.setOrder(_defaultGroupOrder);
             cType.addGroup(group);
             e2.setType(cType);
             e2Type = cType;
         }
     }
     
     //-- both simple types
     if (e1Type.isSimpleType() && e2Type.isSimpleType()) {
         if (!e1Type.getName().equals(e2Type.getName())) {
             String typeName = _nsPrefix + ':' +
                 DatatypeHandler.whichType(e1Type.getName(),
                     e2Type.getName());
             e1.setType(null);
             e1.setTypeReference(typeName);
         }
         return;
     }
     //-- e1 is simple, e2 is complex
     else if (e1Type.isSimpleType()) {
         ComplexType cType = new ComplexType(_schema);
         e1.setType(cType);
         Group group = new Group();
         group.setOrder(_defaultGroupOrder);
         cType.addGroup(group);
         cType.setContentType(ContentType.mixed);
         e1Type = cType;
         //-- do not return here...we need to now treat as both
         //-- were complex
     }
     //-- e2 is simple, e1 is complex
     else if (e2Type.isSimpleType()) {
         ComplexType cType = new ComplexType(_schema);
         e2.setType(cType);
         Group group = new Group();
         group.setOrder(_defaultGroupOrder);
         cType.addGroup(group);
         cType.setContentType(ContentType.mixed);
         e2Type = cType;
         //-- do not return here...we need to now treat as both
         //-- were complex
     }
     
     //-- both complex types
     ComplexType cType1 = (ComplexType)e1Type;
     ComplexType cType2 = (ComplexType)e2Type;
     
     //-- loop through all element/attribute declarations
     //-- of e2 and add them to e1 if they do not already exist
     //-- and mark them as optional
     
     Group e1Group = (Group) cType1.getParticle(0);
     if (e1Group == null) {
         e1Group = new Group();
         e1Group.setOrder(_defaultGroupOrder);
         cType1.addGroup(e1Group);
         
     }
     Group e2Group = (Group) cType2.getParticle(0);
     if (e2Group == null) {
         e2Group = new Group();
         e2Group.setOrder(_defaultGroupOrder);
         cType2.addGroup(e2Group);
         
     }
     
     Enumeration enum = e2Group.enumerate();
     while (enum.hasMoreElements()) {
         Particle particle = (Particle)enum.nextElement();
         if (particle.getStructureType() == Structure.ELEMENT) {
             ElementDecl element = (ElementDecl)particle;
             ElementDecl main = e1Group.getElementDecl(element.getName());
             if (main == null) {
                 e1Group.addElementDecl(element);
                 element.setMinOccurs(0);
             }
             else {
                 merge(main, element);
             }
         }
     }
     //-- add all attributes from type2
     enum = cType2.getAttributeDecls();
     
     while (enum.hasMoreElements()) {
         //-- check for attribute with same name
         AttributeDecl attNew =  (AttributeDecl)enum.nextElement();
                 
         String attName = attNew.getName();
         AttributeDecl attPrev = cType1.getAttributeDecl(attName);
         if (attPrev == null) {
             attNew.setUse(AttributeDecl.USE_OPTIONAL);
             cType1.addAttributeDecl(attNew);
         }
         else {
             String type1 = attPrev.getSimpleType().getName();
             String type2 = attNew.getSimpleType().getName();
             if (!type1.equals(type2)) {
                 String typeName = _nsPrefix + ':' + 
                     DatatypeHandler.whichType(type1, type2);
                 attPrev.setSimpleTypeReference(typeName);                        }
         }
     }
     
     //-- loop through all element/attribute declarations
     //-- of e1 and if they do not exist in e2, simply
     //-- mark them as optional
     enum = e1Group.enumerate();
     while (enum.hasMoreElements()) {
         Particle particle = (Particle)enum.nextElement();
         if (particle.getStructureType() == Structure.ELEMENT) {
             ElementDecl element = (ElementDecl)particle;
             if (e2Group.getElementDecl(element.getName()) == null) {
                 element.setMinOccurs(0);
             }
         }
     }
     
     
 } //-- merge
예제 #5
0
  /**
   * Checks the validity of this ComplexType defintion.
   *
   * @throws ValidationException when this ComplexType definition is invalid.
   */
  public void validate() throws ValidationException {
    // -- check name
    if (_parent != null && _parent.getStructureType() != Structure.SCHEMA) {
      if (getName() != null) {
        String err = "Only top-level complexTypes can be named.";
        err += getName() + "is not a valid complexType.";
        throw new ValidationException(err);
      }
    }
    // -- check attributes
    _attributes.validate();

    // -- check content model
    Enumeration enumeration = _contentModel.enumerate();
    while (enumeration.hasMoreElements()) {
      ((Structure) enumeration.nextElement()).validate();
    }

    // -- make sure baseType is accessible
    XMLType type = getBaseType();
    if ((type == null) && (_baseType != null)) {
      String error = "The base type '" + _baseType + "' was not found.";
      throw new ValidationException(error);
    }
    if (type != null) {
      if (type.getStructureType() == Structure.SIMPLE_TYPE) {
        if (_restricted) {
          String name = getName();
          if (name == null) {
            name = "anonymous-complexType-for-element: ";
            if (_parent != null) {
              // -- parent should be an element if name is null, but
              // -- we'll check the type to be on the safe side
              if (_parent.getStructureType() == Structure.ELEMENT)
                name += ((ElementDecl) _parent).getName();
              else name += _parent.toString();
            }
          }
          String err = "complexType: " + name;
          err += "; A complex type cannot be a restriction" + " of a simpleType:";
          err += type.getName();
          throw new ValidationException(err);
        }
      } else if (type.getStructureType() == Structure.COMPLEX_TYPE) {

        if (!_complexContent) {
          // we are now sure that the base is a ComplexType
          // but is the base of this complexType a simpleType? (see 4.3.3->simpleContent->content
          // type)
          if (((ComplexType) type).getContentType().getType() != ContentType.SIMPLE) {
            String name = getName();
            if (name == null) {
              name = "anonymous-complexType-for-element: ";
              if (_parent != null) {
                // -- parent should be an element if name is null, but
                // -- we'll check the type to be on the safe side
                if (_parent.getStructureType() == Structure.ELEMENT)
                  name += ((ElementDecl) _parent).getName();
                else name += _parent.toString();
              }
            }
            String err = "complexType: " + name;
            err +=
                "; When a complexType is a restriction of simpleContent the base type"
                    + " must be a complexType whose base is also simpleContent.";
            throw new ValidationException(err);
          }
        }
      }
    }
  } // -- validate