/** INTERNAL: Get the XMLDescriptor associated with this Type or generate a new one. */
  public void initializeNamespaces(List namespaceResolvers) {
    if (!isDataType()) {
      NamespaceResolver nr = new NamespaceResolver();

      // copy namespaces between resolvers for well known and SDO namespaces
      if (namespaceResolvers != null) {
        for (int i = 0; i < namespaceResolvers.size(); i++) {
          NamespaceResolver nextNR = (NamespaceResolver) namespaceResolvers.get(i);
          if (nextNR != null) {
            for (int j = 0, size = nextNR.getNamespaces().size(); j < size; j++) {
              Namespace nextNamespace = (Namespace) nextNR.getNamespaces().get(j);
              if ((!nextNamespace.getPrefix().equals(XMLConstants.XMLNS))
                  && (!nextNamespace.getNamespaceURI().equals(XMLConstants.SCHEMA_URL))
                  && (!nextNamespace.getNamespaceURI().equals(SDOConstants.SDOJAVA_URL))
                  && (!nextNamespace.getNamespaceURI().equals(SDOConstants.SDOXML_URL))
                  && (!nextNamespace.getNamespaceURI().equals(SDOConstants.SDO_URL))) {
                String newPrefix =
                    ((SDOTypeHelper) aHelperContext.getTypeHelper())
                        .addNamespace(nextNamespace.getPrefix(), nextNamespace.getNamespaceURI());
                nr.put(newPrefix, nextNamespace.getNamespaceURI());
              }
            }
          }
        }
      }
      xmlDescriptor.setNamespaceResolver(nr);
      if (getURI() != null) {
        String prefix = ((SDOTypeHelper) aHelperContext.getTypeHelper()).getPrefix(getURI());
        xmlDescriptor.getNamespaceResolver().put(prefix, getURI());
      }
      xmlDescriptor
          .getNamespaceResolver()
          .put(XMLConstants.SCHEMA_INSTANCE_PREFIX, XMLConstants.SCHEMA_INSTANCE_URL);
    }
  }
  /**
   * INTERNAL: Convenience method that sets up class indicator and @sdoRef attribute.
   *
   * @param xdesc
   * @param pCls
   */
  private void addClassIndicator(XMLDescriptor xdesc, Class pCls, boolean isInheritanceRoot) {
    XMLField field = (XMLField) getXmlDescriptor().buildField("@xsi:type");
    xdesc.getInheritancePolicy().setClassIndicatorField(field);

    String parentIndicator = getName();
    String parentPrefix = xdesc.getNamespaceResolver().resolveNamespaceURI(getURI());
    if (parentPrefix != null) {
      parentIndicator =
          parentPrefix + SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + parentIndicator;
    }
    xdesc.getInheritancePolicy().addClassIndicator(pCls, parentIndicator);

    // only add the @sdoRef attribute if necessary
    if (xdesc.getMappingForAttributeName(SDO_REF_MAPPING_ATTRIBUTE_NAME) == null) {
      String sdoPrefix =
          ((SDOTypeHelper) aHelperContext.getTypeHelper()).getPrefix(SDOConstants.SDO_URL);
      XMLDirectMapping sdoRefMapping = new XMLDirectMapping();
      sdoRefMapping.setAttributeName(SDO_REF_MAPPING_ATTRIBUTE_NAME);

      XMLField xmlField =
          new XMLField(
              "@"
                  + sdoPrefix
                  + SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT
                  + SDOConstants.CHANGESUMMARY_REF);
      xmlField.getXPathFragment().setNamespaceURI(SDOConstants.SDO_URL);
      xmlField.getLastXPathFragment().setNamespaceURI(SDOConstants.SDO_URL);

      sdoRefMapping.setField(xmlField);
      xdesc.addMapping(sdoRefMapping);
    }
  }
 @SuppressWarnings("unchecked")
 @Override
 public void handleSqlType(String sqlTypeName, int typecode, String targetTypeName) {
   if (!stac.isEmpty()) {
     ListenerHelper listenerHelper = stac.pop();
     if (listenerHelper.isAttribute()) {
       AttributeFieldHelper attributeFieldHelper = (AttributeFieldHelper) listenerHelper;
       attributeFieldHelper.setSqlTypeName(sqlTypeName);
       String fieldName = attributeFieldHelper.attributeFieldName();
       String attributeName = fieldName.toLowerCase();
       ListenerHelper listenerHelper2 = stac.peek();
       if (listenerHelper2.isObject()) {
         ObjectTypeHelper objectTypeHelper = (ObjectTypeHelper) listenerHelper2;
         XMLDescriptor xdesc = descriptorMap.get(objectTypeHelper.objectTypename());
         DatabaseMapping dm = xdesc.getMappingForAttributeName(attributeName);
         if (dm == null) {
           XMLDirectMapping fieldMapping = new XMLDirectMapping();
           fieldMapping.setAttributeName(attributeName);
           XMLField xField = new XMLField(attributeName + "/text()");
           xField.setRequired(true);
           QName qnameFromDatabaseType = qnameFromDatabaseType(listenerHelper);
           xField.setSchemaType(qnameFromDatabaseType);
           // special case to avoid Calendar problems
           if (qnameFromDatabaseType == DATE_QNAME) {
             fieldMapping.setAttributeClassification(java.sql.Date.class);
             xField.addXMLConversion(DATE_QNAME, java.sql.Date.class);
             xField.addJavaConversion(java.sql.Date.class, DATE_QNAME);
             xdesc.getNamespaceResolver().put(SCHEMA_PREFIX, W3C_XML_SCHEMA_NS_URI);
           } else {
             Class attributeClass =
                 (Class) XMLConversionManager.getDefaultXMLTypes().get(qnameFromDatabaseType);
             if (attributeClass == null) {
               attributeClass = Object_Class;
             }
             fieldMapping.setAttributeClassification(attributeClass);
           }
           fieldMapping.setField(xField);
           AbstractNullPolicy nullPolicy = fieldMapping.getNullPolicy();
           nullPolicy.setNullRepresentedByEmptyNode(false);
           nullPolicy.setMarshalNullRepresentation(XSI_NIL);
           nullPolicy.setNullRepresentedByXsiNil(true);
           fieldMapping.setNullPolicy(nullPolicy);
           xdesc
               .getNamespaceResolver()
               .put(
                   SCHEMA_INSTANCE_PREFIX,
                   W3C_XML_SCHEMA_INSTANCE_NS_URI); // to support xsi:nil policy
           xdesc.addMapping(fieldMapping);
         }
         // last attribute, pop ObjectTypeHelper off stack
         int numAttributes = objectTypeHelper.decrNumAttributes();
         if (numAttributes == 0) {
           stac.pop();
         }
       } else if (listenerHelper2.isArray()) {
         SqlArrayTypeHelper sqlArrayTypeHelper = (SqlArrayTypeHelper) listenerHelper2;
         XMLDescriptor xdesc = descriptorMap.get(sqlArrayTypeHelper.arrayTypename());
         DatabaseMapping dm = xdesc.getMappingForAttributeName(attributeName);
         if (dm == null) {
           XMLCompositeDirectCollectionMapping dirCollectMapping =
               new XMLCompositeDirectCollectionMapping();
           SqltypeHelper componentType = new SqltypeHelper(sqlTypeName);
           dirCollectMapping.setAttributeElementClass(
               attributeClassFromDatabaseType((DefaultListenerHelper) componentType));
           dirCollectMapping.setAttributeName(attributeName);
           dirCollectMapping.setUsesSingleNode(true);
           dirCollectMapping.setXPath(attributeName + "/text()");
           XMLField xField = (XMLField) dirCollectMapping.getField();
           xField.setRequired(true);
           xField.setSchemaType(qnameFromDatabaseType(componentType));
           dirCollectMapping.useCollectionClassName("java.util.ArrayList");
           AbstractNullPolicy nullPolicy = dirCollectMapping.getNullPolicy();
           nullPolicy.setNullRepresentedByEmptyNode(false);
           nullPolicy.setMarshalNullRepresentation(XSI_NIL);
           nullPolicy.setNullRepresentedByXsiNil(true);
           dirCollectMapping.setNullPolicy(nullPolicy);
           xdesc
               .getNamespaceResolver()
               .put(
                   SCHEMA_INSTANCE_PREFIX,
                   W3C_XML_SCHEMA_INSTANCE_NS_URI); // to support xsi:nil policy
           xdesc.addMapping(dirCollectMapping);
         }
       }
     } else if (listenerHelper.isArray()) {
       SqlArrayTypeHelper sqlArrayTypeHelper2 = (SqlArrayTypeHelper) listenerHelper;
       XMLDescriptor xdesc = descriptorMap.get(sqlArrayTypeHelper2.arrayTypename());
       DatabaseMapping dm = xdesc.getMappingForAttributeName(ITEMS_MAPPING_ATTRIBUTE_NAME);
       if (dm == null) {
         XMLCompositeDirectCollectionMapping itemsMapping =
             new XMLCompositeDirectCollectionMapping();
         SqltypeHelper componentType = new SqltypeHelper(sqlTypeName);
         itemsMapping.setAttributeElementClass(
             attributeClassFromDatabaseType((DefaultListenerHelper) componentType));
         itemsMapping.setAttributeName(ITEMS_MAPPING_ATTRIBUTE_NAME);
         itemsMapping.setUsesSingleNode(true);
         itemsMapping.setXPath(ITEM_MAPPING_NAME + "/text()");
         XMLField xField = (XMLField) itemsMapping.getField();
         xField.setRequired(true);
         xField.setSchemaType(qnameFromDatabaseType(componentType));
         itemsMapping.useCollectionClassName("java.util.ArrayList");
         AbstractNullPolicy nullPolicy = itemsMapping.getNullPolicy();
         nullPolicy.setNullRepresentedByEmptyNode(false);
         nullPolicy.setMarshalNullRepresentation(XSI_NIL);
         nullPolicy.setNullRepresentedByXsiNil(true);
         itemsMapping.setNullPolicy(nullPolicy);
         xdesc
             .getNamespaceResolver()
             .put(
                 SCHEMA_INSTANCE_PREFIX,
                 W3C_XML_SCHEMA_INSTANCE_NS_URI); // to support xsi:nil policy
         xdesc.addMapping(itemsMapping);
       }
       ListenerHelper listenerHelper2 = stac.peek();
       if (listenerHelper2.isAttribute()) {
         // type built above used in field definition of object further up stack
         stac.pop();
         AttributeFieldHelper fieldHelper = (AttributeFieldHelper) listenerHelper2;
         ListenerHelper listenerHelper3 = stac.peek();
         if (listenerHelper3.isObject()) {
           ObjectTypeHelper objectTypeHelper = (ObjectTypeHelper) listenerHelper3;
           XMLDescriptor xdesc2 = descriptorMap.get(objectTypeHelper.objectTypename());
           String fieldName = fieldHelper.attributeFieldName();
           DatabaseMapping dm2 = xdesc2.getMappingForAttributeName(fieldName.toLowerCase());
           if (dm2 == null) {
             XMLCompositeDirectCollectionMapping fieldMapping =
                 new XMLCompositeDirectCollectionMapping();
             SqltypeHelper componentType = new SqltypeHelper(sqlTypeName);
             fieldMapping.setAttributeElementClass(
                 attributeClassFromDatabaseType((DefaultListenerHelper) componentType));
             fieldMapping.setAttributeName(fieldName.toLowerCase());
             XMLField field =
                 new XMLField(fieldName.toLowerCase() + "/" + ITEM_MAPPING_NAME + "/text()");
             field.setRequired(true);
             fieldMapping.setField(field);
             field.setSchemaType(qnameFromDatabaseType(componentType));
             fieldMapping.useCollectionClassName("java.util.ArrayList");
             AbstractNullPolicy nullPolicy = fieldMapping.getNullPolicy();
             nullPolicy.setNullRepresentedByEmptyNode(false);
             nullPolicy.setMarshalNullRepresentation(XSI_NIL);
             nullPolicy.setNullRepresentedByXsiNil(true);
             fieldMapping.setNullPolicy(nullPolicy);
             xdesc2
                 .getNamespaceResolver()
                 .put(
                     SCHEMA_INSTANCE_PREFIX,
                     W3C_XML_SCHEMA_INSTANCE_NS_URI); // to support xsi:nil policy
             xdesc2.addMapping(fieldMapping);
           }
           // last attribute, pop ObjectTypeHelper off stack
           int numAttributes = objectTypeHelper.decrNumAttributes();
           if (numAttributes == 0) {
             stac.pop();
           }
         }
       }
     }
   }
 }
  public void setupInheritance(SDOType parentType) {
    if (parentType == null) {
      // root of inheritance
      addClassIndicator(getXmlDescriptor(), getImplClass(), true);
    } else {
      if ((parentType.getURI() != null) && (!parentType.getURI().equals(SDOConstants.SDO_URL))) {
        // set parent descriptor indicator if necessary
        if (!parentType.getXmlDescriptor().hasInheritance()) {
          addClassIndicator(parentType.getXmlDescriptor(), parentType.getImplClass(), false);
        }

        XMLDescriptor parentDescriptor =
            (XMLDescriptor)
                parentType.getXmlDescriptor().getInheritancePolicy().getRootParentDescriptor();
        NamespaceResolver parentNR = parentDescriptor.getNonNullNamespaceResolver();
        if (parentNR != null) {
          for (int i = 0; i < parentNR.getNamespaces().size(); i++) {
            Namespace nextNamespace = (Namespace) parentNR.getNamespaces().get(i);
            if ((!nextNamespace.getPrefix().equals(XMLConstants.XMLNS))
                && (!nextNamespace.getNamespaceURI().equals(XMLConstants.SCHEMA_URL))
                && (!nextNamespace.getNamespaceURI().equals(SDOConstants.SDOJAVA_URL))
                && (!nextNamespace.getNamespaceURI().equals(SDOConstants.SDOXML_URL))
                && (!nextNamespace.getNamespaceURI().equals(SDOConstants.SDO_URL))) {
              getXmlDescriptor()
                  .getNonNullNamespaceResolver()
                  .put(nextNamespace.getPrefix(), nextNamespace.getNamespaceURI());
            }
          }
        }
        if (getInstanceClassName() != null) {
          String indicator = getName();
          String prefix = parentDescriptor.getNamespaceResolver().resolveNamespaceURI(getURI());
          if (prefix == null) {
            prefix = getXmlDescriptor().getNamespaceResolver().resolveNamespaceURI(getURI());
            if (prefix != null) {
              parentDescriptor.getNamespaceResolver().put(prefix, getURI());
            }
          }
          if (prefix != null) {
            indicator = prefix + SDOConstants.SDO_XPATH_NS_SEPARATOR_FRAGMENT + indicator;
          }
          Class implClass = getImplClass();
          parentDescriptor.getInheritancePolicy().addClassIndicator(implClass, indicator);
          parentDescriptor.getInheritancePolicy().setShouldReadSubclasses(true);
          Class parentClass = parentType.getImplClass();
          getXmlDescriptor().getInheritancePolicy().setParentClass(parentClass);
          getXmlDescriptor()
              .getInheritancePolicy()
              .setParentDescriptor(parentType.getXmlDescriptor());
          parentType
              .getXmlDescriptor()
              .getNamespaceResolver()
              .put(XMLConstants.SCHEMA_INSTANCE_PREFIX, XMLConstants.SCHEMA_INSTANCE_URL);
          getXmlDescriptor()
              .getNamespaceResolver()
              .put(XMLConstants.SCHEMA_INSTANCE_PREFIX, XMLConstants.SCHEMA_INSTANCE_URL);
        }
      }
    }
    // now setup inheritance for any subtypes
    if (isBaseType()) {
      for (int i = 0; i < subTypes.size(); i++) {
        SDOType nextSubType = (SDOType) subTypes.get(i);
        if (!nextSubType.isDataType() && nextSubType.isSubType()) {
          nextSubType.setupInheritance(this);
        }
      }
    }
  }