private SkinMetadata _findSkinMetadata()

in trinidad-impl/src/main/java/org/apache/myfaces/trinidadinternal/skin/provider/BaseSkinProvider.java [230:376]


  private SkinMetadata _findSkinMetadata(ExternalContext context, SkinMetadata search)
  {
    SkinMetadata matchingSkinMetadata = null;
    String skinId = search.getId();

    Collection<SkinMetadata> skinMetadata = getSkinMetadata(context);

    if (skinMetadata == null || skinMetadata.isEmpty())
      return null;

    // search is with either id or (family, renderkit, version)
    // we have ensure that either id or family is passed in the search
    if (skinId != null)
    {
      // Id is available then search using ID
      for (SkinMetadata m : skinMetadata)
        if (skinId.equals(m.getId()))
        {
          matchingSkinMetadata = m;
        }

      if (matchingSkinMetadata == null)
      {
        if (_LOG.isFine())
        {
          _LOG.fine("SP_CANNOT_FIND_MATCHING_SKIN_ID", new Object[]{skinId});
        }
      }
    }
    else
    {
      // search using family, renderkit, version
      String family = search.getFamily();

      // we need at least the family to go on with the search
      if (family == null)
        return null;

      // renderkit id cannot be null, we initialize it to APACHE_TRINIDAD_DESKTOP
      String renderKit = search.getRenderKitId();

      // version cannot be null as we initialize it to SkinVersion.EMPTY_SKIN_VERSION
      SkinVersion version = search.getVersion();

      List<SkinMetadata> familyRenderKitMatches = new ArrayList<SkinMetadata>(2);

      // search using family and renderkit id
      for (SkinMetadata m : skinMetadata)
        if (family.equalsIgnoreCase(m.getFamily()) &&
              renderKit.equalsIgnoreCase(m.getRenderKitId()))
          familyRenderKitMatches.add(m);

      if (familyRenderKitMatches.isEmpty())
      {
        // if we get here, that means we couldn't find an exact
        // family/renderKitId match, so return the simple skin
        // that matches the renderKitId.
        if (_LOG.isFine())
        {
          _LOG.fine("SP_CANNOT_FIND_MATCHING_SKIN", new Object[]{family, renderKit});
        }

        return null;
      }

      // at this point we know we have something in the familyRenderKitMatches
      // which is a list of matching family and renderKitId skins. Now match the version
      // to find the best matched skin.
      String versionName = version.getName();
      boolean versionIsDefault = version.isDefault();
      boolean foundMatchingSkin = false;

      // if the user didn't ask for the 'default' version, then look for the exact match
      if (!versionIsDefault)
      {
        matchingSkinMetadata = _findSkinMetadataForVersionName(familyRenderKitMatches, version);
      }

      // matchingSkinMetadata will be null if an exact version match (family+renderKitId+version)
      // was not found;
      // or if user was asking for a default version
      // we can have an exact version match if the user asks for null version,
      // and we find a skin with no
      // version set.
      if (matchingSkinMetadata == null || versionIsDefault)
      {
        // at this point either user is looking for default skin
        // or did not find the exact version match specified
        // so we find the default skin
        matchingSkinMetadata = _findSkinMetadataWithDefaultVersion(familyRenderKitMatches);

        if (matchingSkinMetadata == null)
        {
          // if we fail to find a default skin then
          // get the latest skin in the matchingSkinList if there is no skin marked default.
          if (_LOG.isFine())
            _LOG.fine(this + " did not find default / version skinMetadata. so getting leaf skin");

          matchingSkinMetadata = _findLeafSkinMetadata(familyRenderKitMatches);
        }
        else if ((matchingSkinMetadata != null) && versionIsDefault)
        {
          // found the default skin the user wanted
          if (_LOG.isFine())
            _LOG.fine(this + " found default skinMetadata");

          foundMatchingSkin = true;
        }
      }

      // log messages
      if (foundMatchingSkin)
      {
        if (_LOG.isFine())
          _LOG.fine("GET_SKIN_FOUND_SKIN_VERSION",
                    new Object[]{family, version, matchingSkinMetadata.getId()});
      }
      else if (matchingSkinMetadata != null)
      {
        if (_LOG.isFine())
        {
          if ("".equals(versionName))
          {
            _LOG.fine("GET_SKIN_CANNOT_FIND_NO_VERSION",
                      new Object[]{family, matchingSkinMetadata.getId()});
          }
          else
          {
            _LOG.fine("GET_SKIN_CANNOT_FIND_SKIN_VERSION",
                      new Object[]{family, version, matchingSkinMetadata.getId()});
          }
        }
      }

      if (matchingSkinMetadata == null)
      {
        matchingSkinMetadata = familyRenderKitMatches.get(familyRenderKitMatches.size() - 1);
      }
    }


    if (matchingSkinMetadata != null)
      if (_LOG.isFine())
        _LOG.fine(this + " found matching metadata: " + matchingSkinMetadata);

    return matchingSkinMetadata;
  }