/**
   * This method gets all relevant facets. It behaves differently from a simple getFacets() call in
   * that enumerations are not congealed.
   */
  public static Set getUsefulFacets(XSDSimpleTypeDefinition type) {
    Set rv = new HashSet();

    Iterator itor = type.getFacets().iterator();
    while (itor.hasNext()) {
      XSDConstrainingFacet facet = (XSDConstrainingFacet) itor.next();
      if (facet instanceof XSDRepeatableFacet && facet.getElement() == null) {
        // this is a fake pattern or enum; get the individual entries:
        if (facet instanceof XSDPatternFacet) {
          XSDPatternFacet pf = (XSDPatternFacet) facet;
          XSDSimpleTypeDefinition realParent = (XSDSimpleTypeDefinition) pf.getContainer();
          // only add patterns if they belong to this type:
          if (realParent == type) {
            rv.addAll(realParent.getPatternFacets());
          } // endif
        } else if (facet instanceof XSDEnumerationFacet) {
          XSDEnumerationFacet ef = (XSDEnumerationFacet) facet;
          XSDSimpleTypeDefinition realParent = (XSDSimpleTypeDefinition) ef.getContainer();
          // only add enums if they belong to this type:
          if (realParent == type) {
            rv.addAll(realParent.getEnumerationFacets());
          } // endif
        } // endif -- which kind of repeatable?
      } else {
        rv.add(facet);
      } // endif -- fake facet
    } // endwhile -- facets

    return rv;
  }
  public static Object getMainFacetValue(XSDConstrainingFacet facet) {
    Object rv = null;
    int facetClassifierID = facet.eClass().getClassifierID();
    switch (facetClassifierID) {
      case XSDPackage.XSD_LENGTH_FACET:
        {
          XSDLengthFacet lf = (XSDLengthFacet) facet;
          rv = new Integer(lf.getValue());
        }
        break;
      case XSDPackage.XSD_MAX_LENGTH_FACET:
        {
          XSDMaxLengthFacet mf = (XSDMaxLengthFacet) facet;
          rv = new Integer(mf.getValue());
        }
        break;
      case XSDPackage.XSD_MIN_LENGTH_FACET:
        {
          XSDMinLengthFacet mf = (XSDMinLengthFacet) facet;
          rv = new Integer(mf.getValue());
        }
        break;
      case XSDPackage.XSD_PATTERN_FACET:
        {
          XSDPatternFacet pf = (XSDPatternFacet) facet;
          List value = pf.getValue();
          if (value.size() > 1) {
            // skip this, it is a multi-element
            rv = value;
          } else if (value.size() == 1) {
            // single entry, use it:
            rv = value.get(0);
          } else {
            // no elements; try the lex value:
            rv = pf.getLexicalValue();
          } // endif
        }
        break;
      case XSDPackage.XSD_ENUMERATION_FACET:
        {
          XSDEnumerationFacet ef = (XSDEnumerationFacet) facet;
          List value = ef.getValue();
          if (value.size() > 1) {
            // skip this, it is a multi-element
            rv = value;
          } else if (value.size() == 1) {
            // single entry, use it:
            rv = value.get(0);
          } else {
            // no elements; try the lex value:
            rv = ef.getLexicalValue();
          } // endif
        }
        break;
      case XSDPackage.XSD_WHITE_SPACE_FACET:
        {
          XSDWhiteSpaceFacet wf = (XSDWhiteSpaceFacet) facet;
          rv = wf.getValue().getName();
        }
        break;
      case XSDPackage.XSD_MIN_EXCLUSIVE_FACET:
      case XSDPackage.XSD_MIN_INCLUSIVE_FACET:
        {
          XSDMinFacet mf = (XSDMinFacet) facet;
          // defect 18279 - read as string, since the backing datatype can be in different value
          // spaces.
          int value;
          try {
            String lexicalValue = mf.getLexicalValue();
            if (lexicalValue != null) {
              value = Integer.parseInt(lexicalValue);
            } else {
              // it is null, use invalid number:
              value = -Integer.MAX_VALUE;
            } // endif
          } catch (NumberFormatException ex) {
            value = DEFAULT_MIN_BOUNDS;
          } // endtry
          rv = new InclusiveInteger(value, mf.isInclusive());
        }
        break;
      case XSDPackage.XSD_MAX_EXCLUSIVE_FACET:
      case XSDPackage.XSD_MAX_INCLUSIVE_FACET:
        {
          XSDMaxFacet mf = (XSDMaxFacet) facet;
          // defect 18279 - read as string, since the backing datatype can be in different value
          // spaces.
          int value;
          try {
            String lexicalValue = mf.getLexicalValue();
            if (lexicalValue != null) {
              value = Integer.parseInt(lexicalValue);
            } else {
              // it is null, use zero:
              value = -Integer.MAX_VALUE;
            } // endif
          } catch (NumberFormatException ex) {
            value = DEFAULT_MAX_BOUNDS;
          } // endtry
          rv = new InclusiveInteger(value, mf.isInclusive());
        }
        break;
      case XSDPackage.XSD_FRACTION_DIGITS_FACET:
        {
          XSDFractionDigitsFacet ff = (XSDFractionDigitsFacet) facet;
          rv = new Integer(ff.getValue());
        }
        break;
      case XSDPackage.XSD_TOTAL_DIGITS_FACET:
        {
          XSDTotalDigitsFacet tf = (XSDTotalDigitsFacet) facet;
          rv = new Integer(tf.getValue());
        }
        break;

      default:
        ModelerXsdUiConstants.Util.log(
            ModelerXsdUiConstants.Util.getString(ERROR_KEY_UNUSABLE_FACET, facet));
        break;
    } // endswitch

    return rv;
  }
  /**
   * Sets the core value of the specified facet. If facet cannot be properly modified to match fv,
   * facets will be added or removed as necessary to make it work. This occurs when a min or max
   * value is changed from inclusive to exclusive, and when dealing with patterns and enumerations.
   *
   * @param facet
   * @param fv
   */
  private static XSDConstrainingFacet setMainFacetValue(
      XSDSimpleTypeDefinition type, XSDConstrainingFacet facet, Object value) {
    int facetClassifierID = facet.eClass().getClassifierID();
    switch (facetClassifierID) {
      case XSDPackage.XSD_LENGTH_FACET:
        {
          XSDLengthFacet lf = (XSDLengthFacet) facet;
          if (value instanceof Integer) {
            Integer i = (Integer) value;
            lf.setLexicalValue(i.toString());
          } else if (value instanceof InclusiveInteger) {
            InclusiveInteger ii = (InclusiveInteger) value;
            lf.setLexicalValue(Integer.toString(ii.value));
          } // endif
        }
        break;
      case XSDPackage.XSD_MAX_LENGTH_FACET:
        {
          XSDMaxLengthFacet mf = (XSDMaxLengthFacet) facet;
          if (value instanceof Integer) {
            Integer i = (Integer) value;
            mf.setLexicalValue(i.toString());
          } else if (value instanceof InclusiveInteger) {
            InclusiveInteger ii = (InclusiveInteger) value;
            mf.setLexicalValue(Integer.toString(ii.value));
          } // endif
        }
        break;
      case XSDPackage.XSD_MIN_LENGTH_FACET:
        {
          XSDMinLengthFacet mf = (XSDMinLengthFacet) facet;
          if (value instanceof Integer) {
            Integer i = (Integer) value;
            mf.setLexicalValue(i.toString());
          } else if (value instanceof InclusiveInteger) {
            InclusiveInteger ii = (InclusiveInteger) value;
            mf.setLexicalValue(Integer.toString(ii.value));
          } // endif
        }
        break;
      case XSDPackage.XSD_PATTERN_FACET:
        {
          XSDPatternFacet pf = (XSDPatternFacet) facet;
          pf.setLexicalValue((String) value);
        }
        break;
      case XSDPackage.XSD_ENUMERATION_FACET:
        {
          XSDEnumerationFacet ef = (XSDEnumerationFacet) facet;
          ef.setLexicalValue((String) value);
        }
        break;
      case XSDPackage.XSD_WHITE_SPACE_FACET:
        {
          XSDWhiteSpaceFacet wf = (XSDWhiteSpaceFacet) facet;
          if (value instanceof String) {
            String white = (String) value;
            wf.setLexicalValue(white);
          } // endif
        }
        break;
      case XSDPackage.XSD_MIN_EXCLUSIVE_FACET:
      case XSDPackage.XSD_MIN_INCLUSIVE_FACET:
        {
          XSDMinFacet mf = (XSDMinFacet) facet;
          if (value instanceof Integer) {
            Integer i = (Integer) value;
            mf.setLexicalValue(i.toString());
          } else if (value instanceof InclusiveInteger) {
            InclusiveInteger ii = (InclusiveInteger) value;
            if (ii.isInclusive == mf.isInclusive()) {
              // same inclusive types, don't need to do anything crazy
              mf.setLexicalValue(Integer.toString(ii.value));
            } else {
              XSDMinFacet mf2;
              if (ii.isInclusive) {
                mf2 = XSDFactory.eINSTANCE.createXSDMinInclusiveFacet();
              } else {
                mf2 = XSDFactory.eINSTANCE.createXSDMinExclusiveFacet();
              } // endif
              mf2.setLexicalValue(Integer.toString(ii.value));
              try {
                // remove old:
                ModelerCore.getModelEditor().removeValue(type, mf, type.getFacetContents());
                // add the copy:
                ModelerCore.getModelEditor().addValue(type, mf2, type.getFacetContents());
                // update the return value:
                facet = mf2;
              } catch (ModelerCoreException err) {
                ModelerXsdUiConstants.Util.log(err);
              } // endtry

              return mf2;
            } // endif -- same inclusive
          } // endif -- integer or iinteger
        }
        break;
      case XSDPackage.XSD_MAX_EXCLUSIVE_FACET:
      case XSDPackage.XSD_MAX_INCLUSIVE_FACET:
        {
          XSDMaxFacet mf = (XSDMaxFacet) facet;
          if (value instanceof Integer) {
            Integer i = (Integer) value;
            mf.setLexicalValue(i.toString());
          } else if (value instanceof InclusiveInteger) {
            InclusiveInteger ii = (InclusiveInteger) value;
            if (ii.isInclusive == mf.isInclusive()) {
              // same inclusive types, don't need to do anything crazy
              mf.setLexicalValue(Integer.toString(ii.value));
            } else {
              XSDMaxFacet mf2;
              if (ii.isInclusive) {
                mf2 = XSDFactory.eINSTANCE.createXSDMaxInclusiveFacet();
              } else {
                mf2 = XSDFactory.eINSTANCE.createXSDMaxExclusiveFacet();
              } // endif
              mf2.setLexicalValue(Integer.toString(ii.value));
              try {
                // remove old:
                ModelerCore.getModelEditor().removeValue(type, mf, type.getFacetContents());
                // add the copy:
                ModelerCore.getModelEditor().addValue(type, mf2, type.getFacetContents());
                // update the return value:
                facet = mf2;
              } catch (ModelerCoreException err) {
                ModelerXsdUiConstants.Util.log(err);
              } // endtry

              return mf2;
            } // endif -- same inclusive
          } // endif -- integer or iinteger
        }
        break;
      case XSDPackage.XSD_FRACTION_DIGITS_FACET:
        {
          XSDFractionDigitsFacet ff = (XSDFractionDigitsFacet) facet;
          if (value instanceof Integer) {
            Integer i = (Integer) value;
            ff.setLexicalValue(i.toString());
          } else if (value instanceof InclusiveInteger) {
            InclusiveInteger ii = (InclusiveInteger) value;
            ff.setLexicalValue(Integer.toString(ii.value));
          } // endif
        }
        break;
      case XSDPackage.XSD_TOTAL_DIGITS_FACET:
        {
          XSDTotalDigitsFacet tf = (XSDTotalDigitsFacet) facet;
          if (value instanceof Integer) {
            Integer i = (Integer) value;
            tf.setLexicalValue(i.toString());
          } else if (value instanceof InclusiveInteger) {
            InclusiveInteger ii = (InclusiveInteger) value;
            tf.setLexicalValue(Integer.toString(ii.value));
          } // endif
        }
        break;

      default:
        ModelerXsdUiConstants.Util.log(
            ModelerXsdUiConstants.Util.getString(ERROR_KEY_UNUSABLE_FACET, facet));
        break;
    } // endswitch

    return facet;
  }