Exemplo n.º 1
0
 /**
  * Retrieve the string values for an enumeration.
  *
  * @param type
  */
 public static List<String> enumeratorValues(XmlSchemaSimpleType type) {
   XmlSchemaSimpleTypeContent content = type.getContent();
   XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction) content;
   List<XmlSchemaFacet> facets = restriction.getFacets();
   List<String> values = new ArrayList<String>();
   for (XmlSchemaFacet facet : facets) {
     XmlSchemaEnumerationFacet enumFacet = (XmlSchemaEnumerationFacet) facet;
     values.add(enumFacet.getValue().toString());
   }
   return values;
 }
 private QName getSimpleContentTypeName(XmlSchemaSimpleType schemaSimpleType) {
   QName simpleContentTypeName = null;
   if (schemaSimpleType.getContent() != null
       && schemaSimpleType.getContent() instanceof XmlSchemaSimpleTypeRestriction) {
     XmlSchemaSimpleTypeRestriction simpleContent =
         (XmlSchemaSimpleTypeRestriction) schemaSimpleType.getContent();
     simpleContentTypeName = simpleContent.getBaseTypeName();
   } else {
     throw new RuntimeException(
         "Unsupported simple content model: "
             + schemaSimpleType.getContent().getClass().getCanonicalName());
   }
   return simpleContentTypeName;
 }
Exemplo n.º 3
0
 /**
  * Return true if a simple type is a straightforward XML Schema representation of an enumeration.
  * If we discover schemas that are 'enum-like' with more complex structures, we might make this
  * deal with them.
  *
  * @param type Simple type, possible an enumeration.
  * @return true for an enumeration.
  */
 public static boolean isEumeration(XmlSchemaSimpleType type) {
   XmlSchemaSimpleTypeContent content = type.getContent();
   if (!(content instanceof XmlSchemaSimpleTypeRestriction)) {
     return false;
   }
   XmlSchemaSimpleTypeRestriction restriction = (XmlSchemaSimpleTypeRestriction) content;
   List<XmlSchemaFacet> facets = restriction.getFacets();
   for (XmlSchemaFacet facet : facets) {
     if (!(facet instanceof XmlSchemaEnumerationFacet)) {
       return false;
     }
   }
   return true;
 }
  private NeutralSchema recursiveParseSimpleType(
      XmlSchemaSimpleType schemaSimpleType, XmlSchema schema) {
    NeutralSchema simpleSchema = null;

    String simpleTypeName = schemaSimpleType.getName();

    if (NeutralSchemaType.isPrimitive(schemaSimpleType.getQName())) {
      simpleSchema = getSchemaFactory().createSchema(schemaSimpleType.getQName());

    } else if (NeutralSchemaType.exists(schemaSimpleType.getBaseSchemaTypeName())) {

      if (NeutralSchemaType.isPrimitive(schemaSimpleType.getBaseSchemaTypeName())) {
        simpleSchema = getSchemaFactory().createSchema(schemaSimpleType.getBaseSchemaTypeName());

      } else {
        XmlSchemaSimpleType simpleBaseType =
            getSimpleBaseType(schemaSimpleType.getBaseSchemaTypeName(), schema);
        if (simpleBaseType != null) {

          if (simpleTypeName == null) {
            simpleTypeName = simpleBaseType.getName();
          }
          simpleSchema = getSchemaFactory().createSchema(simpleTypeName);
        }
      }

    } else if (schemaSimpleType.getContent() != null
        && schemaSimpleType.getContent() instanceof XmlSchemaSimpleTypeList) {

      ListSchema listSchema = (ListSchema) getSchemaFactory().createSchema("list");

      XmlSchemaSimpleTypeList content = (XmlSchemaSimpleTypeList) schemaSimpleType.getContent();
      NeutralSchema listContentSchema = null;

      if (content.getItemType() != null) {
        listContentSchema = parseSimpleType(content.getItemType(), schema, null);

      } else {
        QName itemTypeName = content.getItemTypeName();
        listContentSchema = getSchemaFactory().createSchema(itemTypeName.getLocalPart());
      }
      listSchema.getList().add(listContentSchema);
      listSchema.updateAnnotations();
      return listSchema;

    } else if (getSimpleContentTypeName(schemaSimpleType) != null) {

      if (NeutralSchemaType.isPrimitive(getSimpleContentTypeName(schemaSimpleType))) {
        simpleSchema = getSchemaFactory().createSchema(getSimpleContentTypeName(schemaSimpleType));

      } else {

        XmlSchemaSimpleType simpleBaseType =
            getSimpleBaseType(getSimpleContentTypeName(schemaSimpleType), schema);
        simpleSchema = recursiveParseSimpleType(simpleBaseType, schema);
      }
    }

    if (simpleSchema != null && schemaSimpleType.getContent() != null) {

      if (schemaSimpleType.getContent() instanceof XmlSchemaSimpleTypeRestriction) {

        XmlSchemaSimpleTypeRestriction simpleRestrictedContent =
            (XmlSchemaSimpleTypeRestriction) schemaSimpleType.getContent();
        XmlSchemaObjectCollection facets = simpleRestrictedContent.getFacets();
        List<String> tokens = new ArrayList<String>();
        for (int i = 0; i < facets.getCount(); i++) {
          XmlSchemaObject facetObject = facets.getItem(i);

          if (facetObject instanceof XmlSchemaEnumerationFacet) {
            XmlSchemaEnumerationFacet enumerationFacet = (XmlSchemaEnumerationFacet) facetObject;
            tokens.add(enumerationFacet.getValue().toString());
          } else if (facetObject instanceof XmlSchemaFacet) {
            XmlSchemaFacet facet = (XmlSchemaFacet) facetObject;
            String facetPropertyName = NeutralSchemaType.lookupPropertyName(facet);
            simpleSchema.getProperties().put(facetPropertyName, facet.getValue().toString());
          }
        }

        if (tokens.size() > 0) {
          // Token Schema
          Collections.sort(tokens); // sort so we can binary search
          simpleSchema.getProperties().put(TokenSchema.TOKENS, tokens);
        }
      }
    }

    parseAnnotations(simpleSchema, schemaSimpleType);

    if ((simpleSchema != null) && (simpleTypeName != null)) {
      simpleSchema.setType(simpleTypeName);
    }

    return simpleSchema;
  }