public static void removeFacet(XSDSimpleTypeDefinition type, XSDConstrainingFacet facet) {
   try {
     ModelerCore.getModelEditor().removeValue(type, facet, type.getFacetContents());
   } catch (ModelerCoreException err) {
     ModelerXsdUiConstants.Util.log(err);
   } // endtry
 }
  public static XSDConstrainingFacet addOrSetFacetValue(
      XSDSimpleTypeDefinition type, String facetName, FacetValue fv) {
    XSDConstrainingFacet workFacet = fv.facet;
    // do we need to add?
    if (fv.facet == null || fv.facet.getContainer() != type) {
      // need to add to this type:
      boolean inclusiveness = false;
      if (fv.value instanceof InclusiveInteger) {
        inclusiveness = ((InclusiveInteger) fv.value).isInclusive;
      } // endif

      // remove any facets with opposite inclusiveness:
      if (facetName == FAKE_FACET_MAXIMUM || facetName == FAKE_FACET_MINIMUM) {
        // go ahead and remove:
        removeFacet(type, facetName);
      } // endif -- using fake facets?

      workFacet = createFacet(facetName, inclusiveness);

      try {
        ModelerCore.getModelEditor().addValue(type, workFacet, type.getFacetContents());
      } catch (ModelerCoreException err) {
        ModelerXsdUiConstants.Util.log(err);
      } // endtry
    } // endif

    // set main value:
    if (!FormUtil.safeEquals(fv.value, getMainFacetValue(workFacet))) {
      // only set if changed:
      workFacet = setMainFacetValue(type, workFacet, fv.value);
      // in case this is a bounds facet and we swapped them out:
      fv.facet = workFacet;
    } // endif

    // set description:
    String existingDesc = ModelObjectUtilities.getDescription(workFacet);
    if (fv.description != null) {
      // new not null:
      if (!fv.description.equals(existingDesc)) {
        // description changed to a nonnull value:
        ModelObjectUtilities.setDescription(workFacet, fv.description, type);
      } // endif -- different
    } else if (existingDesc != null && existingDesc.length() > 0) {
      // new null, old not null:
      ModelObjectUtilities.setDescription(workFacet, " ", type); // $NON-NLS-1$
    } // endif

    // Lastly, set fixed if applicable:
    setFixed(workFacet, fv.isFixedLocal);

    return workFacet;
  }
  private static void removeFacet(XSDSimpleTypeDefinition type, String facetName) {
    // resolve the name, in case it is one of the bounds facets:
    String nameEx = getRealFacetName(facetName, false);
    String nameIn = getRealFacetName(facetName, true); // most of the time, name == name2

    Iterator itor = type.getFacetContents().iterator();
    while (itor.hasNext()) {
      XSDConstrainingFacet facet = (XSDConstrainingFacet) itor.next();
      String thisName = facet.getFacetName();
      if (nameEx.equals(thisName) || nameIn.equals(thisName)) {
        itor.remove();
      } // endif
    } // endwhile
  }
  private Object[] getXSDSimpleTypeDefinitionChildren_ATOMIC(XSDSimpleTypeDefinition parent) {
    ArrayList<Object> list = new ArrayList<Object>();
    // add Base Type if not a pre-defined type
    if (parent != null
        && parent.getSchema() != null
        && parent.getSchema().getSchemaForSchema() != null) {
      if (!parent.getSchema().getSchemaForSchema().getTypeDefinitions().contains(parent)) {
        list.add(parent.getBaseTypeDefinition());
      }
    }

    if (!Util.isBuildInType(parent)) {
      list.addAll(parent.getFacetContents());
    }

    return list.toArray(new Object[list.size()]);
  }
  /**
   * 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;
  }