示例#1
0
    private void createClasses(Schema schema, SGStateInfo sInfo) {

        Enumeration structures = schema.getElementDecls();

        //-- handle all top-level element declarations
        while (structures.hasMoreElements())
            createClasses((ElementDecl)structures.nextElement(), sInfo);

        //-- handle all top-level complextypes
        structures = schema.getComplexTypes();
        while (structures.hasMoreElements())
            processComplexType((ComplexType)structures.nextElement(), sInfo);

        //-- handle all top-level simpletypes
        structures = schema.getSimpleTypes();
        while (structures.hasMoreElements())
            processSimpleType((SimpleType)structures.nextElement(), sInfo);

        //-- handle all top-level groups
        structures = schema.getModelGroups();
        while (structures.hasMoreElements())
            createClasses((ModelGroup)structures.nextElement(), sInfo);


    } //-- createClasses
示例#2
0
  /**
   * Returns the ModeGroup of associated with the given name
   *
   * @return the ModelGroup of associated with the given name, or null if no ModelGroup with the
   *     given name was found.
   */
  public ModelGroup getModelGroup(String name) {

    String ns = null;
    if (name == null) {
      String err = NULL_ARGUMENT + "getModelGroup: ";
      err += " 'name' can not be null";
      throw new IllegalArgumentException(err);
    }

    int idx = name.indexOf(':');
    if (idx >= 0) {
      String nsPrefix = name.substring(0, idx);
      name = name.substring(idx + 1);
      ns = (String) namespaces.get(nsPrefix);
      if (ns == null) {
        String err = "getModelGroup: ";
        err += "Namespace prefix not recognized '" + nsPrefix + "'";
        throw new IllegalArgumentException(err);
      }
    }

    if ((ns == null) || (ns.equals(targetNS))) return (ModelGroup) _group.get(name);
    else {
      Schema schema = getImportedSchema(ns);
      if (schema != null) {
        String warning = "Warning : do not forget to generate the source ";
        warning += "for the schema with this targetNamespace" + schema.getTargetNamespace();
        System.out.println(warning);
        return schema.getModelGroup(name);
      }
    }

    return null;
  } // --getModelGroup
示例#3
0
  /**
   * Returns the ComplexType of associated with the given name
   *
   * @return the ComplexType of associated with the given name, or null if no ComplexType with the
   *     given name was found.
   */
  public ComplexType getComplexType(String name) {

    // -- Null?
    if (name == null) {
      String err = NULL_ARGUMENT + "getComplexType: ";
      err += "'name' cannot be null.";
      throw new IllegalArgumentException(err);
    }

    // -- Namespace prefix?
    String canonicalName = name;
    String nsprefix = "";
    String ns = targetNS;
    int colon = name.indexOf(':');
    if (colon != -1) {
      canonicalName = name.substring(colon + 1);
      nsprefix = name.substring(0, colon);
      ns = (String) namespaces.get(nsprefix);
      if (ns == null) {
        String err = "getComplexType: ";
        err += "Namespace prefix not recognized '" + name + "'";
        throw new IllegalArgumentException(err);
      }
    }

    // -- Get GetComplexType object
    if ((ns == null) || (ns.equals(targetNS))) return (ComplexType) complexTypes.get(canonicalName);
    else {
      Schema schema = getImportedSchema(ns);
      if (schema != null) return schema.getComplexType(canonicalName);
    }

    return null;
  } // -- getComplexType
示例#4
0
 /**
  * Creates a new XMLInstance2SchemaHandler
  *
 **/
 public XMLInstance2SchemaHandler(Schema schema) {
     super();
     
     _siStack   = new Stack();
     
     _schema = schema;
     //-- create Schema and initialize
     if (_schema == null) {
         _schema = new Schema();
         _schema.addNamespace(DEFAULT_PREFIX, Schema.DEFAULT_SCHEMA_NS);
         _nsPrefix = DEFAULT_PREFIX;
     }
     //-- find or declare namespace prefix
     else {
         _nsPrefix = null;
         Hashtable namespaces = _schema.getNamespaces();
         Enumeration enum = namespaces.keys();
         while (enum.hasMoreElements()) {
             String key = (String) enum.nextElement();
             if (namespaces.get(key).equals(Schema.DEFAULT_SCHEMA_NS)) {
                 _nsPrefix = key;
                 break;
             }
         }
         if (_nsPrefix == null) {
             _schema.addNamespace(DEFAULT_PREFIX, Schema.DEFAULT_SCHEMA_NS);
             _nsPrefix = DEFAULT_PREFIX;
         }
     }
 } //-- XMLInstance2SchemaHandler
示例#5
0
     /**
     * Returns the ModeGroup of associated with the given name
     * @return the ModelGroup of associated with the given name, or
     *  null if no ModelGroup with the given name was found.
    **/
    public ModelGroup getModelGroup(String name) {

        String ns = null;
        if (name == null) {
            String err = NULL_ARGUMENT + "getModelGroup: ";
            err += " 'name' can not be null";
            throw new IllegalArgumentException(err);
        }

        int idx = name.indexOf(':');
        if (idx >= 0)
        {
            String nsPrefix = name.substring(0,idx);
            name = name.substring(idx + 1);
            ns = (String) _namespaces.getNamespaceURI(nsPrefix);
            if (ns == null)  {
                String err = "getModelGroup: ";
                err += "Namespace prefix not recognized '"+nsPrefix+"'";
                throw new IllegalArgumentException(err);
            }
        }

        if ((ns==null) || (ns.equals(_targetNamespace)) )
            return (ModelGroup)_groups.get(name);
        else {
            Schema schema = getImportedSchema(ns);
            if (schema!=null) {
                return schema.getModelGroup(name);
            }
        }

        return null;
    } //--getModelGroup
示例#6
0
  /**
   * Returns the SimpleType associated with the given name, or null if no such SimpleType exists.
   *
   * @return the SimpleType associated with the given name, or null if no such SimpleType exists.
   */
  public SimpleType getSimpleType(String name) {

    // -- Null?
    if (name == null) {
      String err = NULL_ARGUMENT + "getSimpleType: ";
      err += "'name' cannot be null.";
      throw new IllegalArgumentException(err);
    }

    // -- Namespace prefix?
    String canonicalName = name;
    String nsPrefix = "";
    String ns = null;
    int colon = name.indexOf(':');
    if (colon >= 0) {
      canonicalName = name.substring(colon + 1);
      nsPrefix = name.substring(0, colon);
      ns = (String) namespaces.get(nsPrefix);
      if (ns == null) {
        String err = "getSimpleType: ";
        err += "Namespace prefix not recognised '" + name + "'";
        throw new IllegalArgumentException(err);
      }
    }

    // -- Get SimpleType object
    SimpleType result = null;
    if (ns == null) {
      // -- first try built-in types
      result = simpleTypesFactory.getBuiltInType(name);
      // if we have a built-in type not declared in the good namespace -> Exception
      if ((result != null) && (namespaces.containsValue(DEFAULT_SCHEMA_NS))) {
        String err =
            "getSimpleType: the simple type '"
                + name
                + "' has not been declared in XML Schema namespace.";
        throw new IllegalArgumentException(err);
      }

      // -- otherwise check user-defined types
      if (result == null) result = (SimpleType) simpleTypes.get(name);
    } else if (ns.equals(schemaNS)) result = simpleTypesFactory.getBuiltInType(canonicalName);
    else if (ns.equals(targetNS)) result = (SimpleType) simpleTypes.get(canonicalName);
    else {
      Schema schema = getImportedSchema(ns);
      if (schema != null) result = schema.getSimpleType(canonicalName);
    }

    // -- Result could be a deferredSimpleType => getType will resolve it
    if (result != null) result = (SimpleType) result.getType();

    return result;
  } // -- getSimpleType
示例#7
0
 /**
  * Adds the given Schema definition to this Schema definition as an imported schenma
  *
  * @param schema the Schema to add to this Schema as an imported schema
  * @exception SchemaException if the Schema already exists
  */
 public synchronized void addImportedSchema(Schema schema) throws SchemaException {
   String targetNamespace = schema.getTargetNamespace();
   if (importedSchemas.get(targetNamespace) != null) {
     String err = "a Schema has already been imported with the given namespace: ";
     throw new SchemaException(err + targetNamespace);
   }
   importedSchemas.put(targetNamespace, schema);
 } // -- addImportedSchema
示例#8
0
 /**
  * Returns an imported schema by its namespace
  * @return The imported schema
  */
 public Schema getImportedSchema(String ns)
 {
     Schema result = (Schema) _importedSchemas.get(ns);
     //--maybe we are the schema imported is at
     //--a depth > 1
     if (result == null) {
         Enumeration schemas = _importedSchemas.elements();
         boolean found = false;
         while (schemas.hasMoreElements() && !found) {
             Schema temp = (Schema) schemas.nextElement();
             result = temp.getImportedSchema(ns);
             if (result != null)
                found = true;
        }
     }
     return result;
 } //-- getImportedSchema
示例#9
0
    /**
     * Creates Java Source code (Object model) for the given XML Schema
     * @param schema the XML schema to generate the Java sources for
     * @param packageName the package for the generated source files
    **/
    public void generateSource(Schema schema, String packageName) {
        SGStateInfo sInfo = new SGStateInfo();
        sInfo.packageName = packageName;
		if(sInfo.packageName==null)
			sInfo.packageName=SourceGenerator.getJavaPackage(schema.getTargetNamespace());

        sInfo.setPromptForOverwrite(warnOnOverwrite);
        sInfo.setVerbose(verbose);

        createClasses(schema, sInfo);
    } //-- generateSource
示例#10
0
    /**
     * Returns the top-level Attribute associated with the given name.
     *
     * @return the Attribute associated with the given name,
     * or null if no Attribute association is found.
    **/
    public AttributeDecl getAttribute(String name) {

        //-- Null?
        if (name == null)  {
            String err = NULL_ARGUMENT + "getAttribute: ";
            err += "'name' cannot be null.";
            throw new IllegalArgumentException(err);
        }

        //-- Namespace prefix?
        String canonicalName = name;
        String nsprefix = "";
        String ns = _targetNamespace;
        int colon = name.indexOf(':');
        if (colon != -1)
        {
            canonicalName = name.substring(colon + 1);
            nsprefix = name.substring(0,colon);
            ns = (String) _namespaces.getNamespaceURI(nsprefix);
            if (ns == null)  {
                String err = "getAttribute: ";
                err += "Namespace prefix not recognized '"+name+"'";
                throw new IllegalArgumentException(err);
            }
        }

        if ((ns==null) || (ns.equals(_targetNamespace)) )
            return (AttributeDecl)_attributes.get(canonicalName);
        else {
            Schema schema = getImportedSchema(ns);
            if (schema!=null) {
                AttributeDecl att = schema.getAttribute(canonicalName);;
                return att;
            }
        }

        return null;

    } //-- getAttribute
示例#11
0
 /**
  * Returns the reference if any
  *
  * @returns the reference if any
  */
 public ModelGroup getReference() {
   if (groupRef != null) return _schema.getModelGroup(groupRef);
   return null;
 } // -- getReference
示例#12
0
 public void endElement(String name) 
     throws org.xml.sax.SAXException
 {
     
     //-- strip namespace prefix
     int idx = name.indexOf(':');
     if (idx >= 0) {
         name = name.substring(idx+1);
     }
     
     StateInfo sInfo = (StateInfo) _siStack.pop();
     
     //-- if we don't have a type, it means there are no
     //-- children and therefore the type is a simpleType or
     //-- simpleContent
     if ((sInfo.element.getType() == null) && (sInfo.buffer != null)) {
         
         //-- create SimpleType (guess type)
         String typeName = _nsPrefix + ':' + 
             DatatypeHandler.guessType(sInfo.buffer.toString());
         sInfo.element.setTypeReference(typeName);
         //-- simpleContent
         if (sInfo.attributes.size() > 0) {
             ComplexType cType = new ComplexType(_schema);
             //-- SHOULD CHANGE THIS TO SIMPLE CONTENT WHEN
             //-- SCHEMA WRITER BUGS ARE FIXED
             cType.setContentType(ContentType.mixed);
             sInfo.element.setType(cType);
             Group group = new Group();
             group.setOrder(_defaultGroupOrder);
             //-- add attributes
             try {
                 cType.addGroup(group);
                 for (int i = 0; i < sInfo.attributes.size(); i++) {
                     AttributeDecl attDecl = 
                         (AttributeDecl)sInfo.attributes.elementAt(i);
                     cType.addAttributeDecl(attDecl);
                 }
             }
             catch(SchemaException sx) {
                 throw new SAXException(sx);
             }
         }
     }
     else {
         ComplexType cType = (ComplexType)sInfo.element.getType();
         if (cType != null) {
             //-- add attributes
             try {
                 for (int i = 0; i < sInfo.attributes.size(); i++) {
                     AttributeDecl attDecl = 
                         (AttributeDecl)sInfo.attributes.elementAt(i);
                     cType.addAttributeDecl(attDecl);
                 }
             }
             catch(SchemaException sx) {
                 throw new SAXException(sx);
             }
         }
     }
     
     //-- put element into parent element or as top-level in schema
     if (!_siStack.isEmpty()) {
         StateInfo parentInfo = (StateInfo)_siStack.peek();
         ComplexType type = (ComplexType) parentInfo.element.getType();
         Group group = null;
         if (type == null) {
             parentInfo.complex = true;
             type = new ComplexType(_schema);
             parentInfo.element.setType(type);
             group = new Group();
             group.setOrder(_defaultGroupOrder);
             try {
                 type.addGroup(group);
                 //-- add element
                 group.addElementDecl(sInfo.element);
             }
             catch(SchemaException sx) {
                 throw new SAXException(sx);
             }
         }
         else {
             group = (Group) type.getParticle(0);
             //-- check for another element declaration with
             //-- same name ...
             ElementDecl element = group.getElementDecl(name);
             boolean checkGroupType = false;
             if (element != null) {
                 //-- if complex...merge definition
                 if (sInfo.complex) {
                     try {
                         merge(element, sInfo.element);
                     }
                     catch(SchemaException sx) {
                         throw new SAXException(sx);
                     }
                 }
                 element.setMaxOccurs(Particle.UNBOUNDED);
                 checkGroupType = true;
             }
             else {
                 try {
                     group.addElementDecl(sInfo.element);
                 }
                 catch(SchemaException sx) {
                     throw new SAXException(sx);
                 }
             }
             
             //-- change group type if necessary
             if (checkGroupType && (group.getOrder() == Order.seq)) {
                 //-- make sure element is last item in group,
                 //-- otherwise we need to switch to all
                 boolean found = false;
                 boolean changeType = false;
                 for (int i = 0; i < group.getParticleCount(); i++) {
                     if (found) {
                         changeType = true;
                         break;
                     }
                     if (element == group.getParticle(i)) found = true;
                 }
                 if (changeType) {
                     group.setOrder(Order.all);
                 }
             }
         }
     }
     else {
         try {
             _schema.addElementDecl(sInfo.element);
             
             //-- make complexType top-level also
             //XMLType type = sInfo.element.getType();
             //if ((type != null) && (type.isComplexType())) {
             //    if (type.getName() == null) {
             //        type.setName(sInfo.element.getName() + "Type");
             //        _schema.addComplexType((ComplexType)type);
             //    }
             //}
         }
         catch(SchemaException sx) {
             throw new SAXException(sx);
         }
     }
     
 } //-- endElement
示例#13
0
    /**
     * Returns the SimpleType associated with the given name
     * and namespace, or null if no such SimpleType exists.
     *
     * @param name the name of the simpleType. It is an error
     * if this name contains a prefix, it must be an NCName.
     * @param namespace the namespace URI of the simpleType.
     * @return the SimpleType, or null if no such SimpleType exists.
    **/
    public SimpleType getSimpleType(String name, String namespace) {

        //-- name must not be null
        if (name == null)  {
            String err = NULL_ARGUMENT + "getSimpleType: ";
            err += "'name' cannot be null.";
            throw new IllegalArgumentException(err);
        }

        //--Is the declaration in the default namespace (if any)
        boolean isDefaultNS = false;
        if (namespace == null) {
            namespace = (String)_namespaces.getNamespaceURI("");
            isDefaultNS = true;
        }

        
        //-- Get SimpleType object
        SimpleType result = null;
        if ((namespace == null) || (isDefaultNS)) {

            //-- first check user-defined types
            result = (SimpleType)_simpleTypes.get(name);
            if (result != null) {
                //-- resolve deferred type if necessary
                if (result.getType() != result) {
                    //-- can result.getType ever return null?
                    //-- We can check, just in case.
                    if (result.getType() != null) {
                        result = (SimpleType)result.getType();
                        result.setParent(this);
                        _simpleTypes.put(name, result);
                    }
                }
            }
            //-- otherwise try built-in types
            else {
                result= simpleTypesFactory.getBuiltInType(name);
                //if we have a built-in type not declared in the good namespace -> Exception
                if ( (result != null) && (!_schemaNamespace.equals(namespace))) {
                    String err = "getSimpleType: the simple type '"+name+
                                "' has not been declared in XML Schema namespace.";
                    throw new IllegalArgumentException(err);
                }
            }
        }
        else if (namespace.equals(_schemaNamespace)) {
            result= simpleTypesFactory.getBuiltInType(name);
            if (result == null)  {
                    String err = "getSimpleType: the simple type '"+name+
                                "' is not a built-in type as defined in XML Schema specification.";
                    throw new IllegalArgumentException(err);
                }
        }
        else if (namespace.equals(_targetNamespace)) {
            result = (SimpleType)_simpleTypes.get(name);
            if (result != null) {
                //-- resolve deferred type if necessary
                if (result.getType() != result) {
                    //-- can result.getType ever return null?
                    //-- We can check, just in case.
                    if (result.getType() != null) {
                        result = (SimpleType)result.getType();
                        result.setParent(this);
                        _simpleTypes.put(name, result);
                    }
                }
            }
        }
        else {
            Schema schema = getImportedSchema(namespace);
            if (schema != null) {
                result = schema.getSimpleType(name, namespace);
            }
        }

        //-- Result could be a deferredSimpleType => getType will resolve it
        if (result!=null)
            result= (SimpleType)result.getType();

        return result;
    } //-- getSimpleType