<?xml version="1.0" encoding="UTF-8"?>
<!--

    Copyright (c) 1997, 2021 Oracle and/or its affiliates. All rights reserved.

    This program and the accompanying materials are made available under the
    terms of the Eclipse Distribution License v. 1.0, which is available at
    http://www.eclipse.org/org/documents/edl-v10.php.

    SPDX-License-Identifier: BSD-3-Clause

-->

<grammar
	ns="http://www.w3.org/2001/XMLSchema"
	xmlns:cc="http://www.xml.gr.jp/xmlns/relaxngcc"
	xmlns="http://relaxng.org/ns/structure/1.0"
	datatypeLibrary="http://www.w3.org/2001/XMLSchema-datatypes"
	>
	


  <!-- ################################################################## -->
  <!--                                                                    -->
  <!-- include/import/redefine                                            -->
  <!--                                                                    -->
  <!-- ################################################################## -->
  <define name="includeDecl">
    <element name="include">
      <!--optional>
        <attribute name="ID">
          <data type="ID"/>
        </attribute>
      </optional-->
      <attribute name="schemaLocation">
        <data type="anyURI" cc:alias="schemaLocation" />
      </attribute>
      <!--ref name="foreignAttributes"/-->
      <optional>
        <ref name="annotation"/>(null,AnnotationContext.SCHEMA);
      </optional>
    </element>
    <!-- parse this entity into this schema -->
   	$runtime.includeSchema( schemaLocation );
  </define>
  
  
  
  <define name="importDecl">
    <element name="import">
      <!--optional>
        <attribute name="ID">
          <data type="ID"/>
        </attribute>
      </optional-->
      <optional>
        <attribute name="namespace">
          <data type="anyURI" cc:alias="ns" />
        </attribute>
      </optional>
      <optional>
        <attribute name="schemaLocation">
          <data type="anyURI" cc:alias="schemaLocation" />
        </attribute>
      </optional>
      <!--ref name="foreignAttributes"/-->
      <optional>
        <ref name="annotation"/>(null,AnnotationContext.SCHEMA);
      </optional>
    </element>
    <!-- parse this schema -->
		<cc:java>
			if(ns==null)    ns="";
    	$runtime.importSchema( ns,schemaLocation );
		</cc:java>
  </define>
  
  
  
  <define name="redefine">
    <element name="redefine">
      <!--optional>
        <attribute name="ID">
          <data type="ID"/>
        </attribute>
      </optional-->
      <attribute name="schemaLocation">
        schemaLocation = <data type="anyURI"/>
		   	$runtime.includeSchema( schemaLocation );
      </attribute>

      <!--ref name="foreignAttributes"/-->
      <zeroOrMore>
        <choice>
          <ref name="annotation"/>(null,AnnotationContext.SCHEMA);
          <group>
          	newSt = <ref name="simpleType"/>
          	XSSimpleType oldSt = $runtime.currentSchema.getSimpleType(newSt.getName());
            if(oldSt==null) {
              $runtime.reportError( Messages.format(Messages.ERR_UNDEFINED_SIMPLETYPE,newSt.getName()) );
            } else {
              newSt.redefine((SimpleTypeImpl)oldSt);
              $runtime.currentSchema.addSimpleType(newSt,true);
            }
         	</group>
         	<group>
	          newCt = <ref name="complexType"/>
	          XSComplexType oldCt = $runtime.currentSchema.getComplexType(newCt.getName());
            if(oldCt==null) {
              $runtime.reportError( Messages.format(Messages.ERR_UNDEFINED_COMPLEXTYPE,newCt.getName()) );
            } else {
              newCt.redefine((ComplexTypeImpl)oldCt);
              $runtime.currentSchema.addComplexType(newCt,true);
            }
         	</group>
         	<group>
	          newGrp = <ref name="group"/>
	          XSModelGroupDecl oldGrp = $runtime.currentSchema.getModelGroupDecl(newGrp.getName());
            if(oldGrp==null) {
              $runtime.reportError( Messages.format(Messages.ERR_UNDEFINED_MODELGROUP,newGrp.getName()) );
            } else {
              newGrp.redefine((ModelGroupDeclImpl)oldGrp);
              $runtime.currentSchema.addModelGroupDecl(newGrp,true);
            }
         	</group>
          <group>
          	newAg = <ref name="attributeGroupDecl"/>
          	XSAttGroupDecl oldAg = $runtime.currentSchema.getAttGroupDecl(newAg.getName());
            if(oldAg==null) {
              $runtime.reportError( Messages.format(Messages.ERR_UNDEFINED_ATTRIBUTEGROUP,newAg.getName()) );
            } else {
              newAg.redefine((AttGroupDeclImpl)oldAg);
              $runtime.currentSchema.addAttGroupDecl(newAg,true);
            }
          </group>
        </choice>
      </zeroOrMore>
    </element>
  </define>


</grammar>
