for(Iterator iter2 = getLanguagePrefs().iterator(); (currentVariant.getLanguage() != null) && iter2.hasNext();)
if(getLanguagePrefs() != null)
   public void setBestOutput(Resource resource) throws RestletException   {      Parameter currentParam = null;      boolean compatiblePref = false;      boolean compatibleLanguage = false;      RepresentationMetadata currentVariant = null;      Language currentLanguage = null;      MediaType currentMediaType = null;      RepresentationMetadata bestVariant = null;      float bestQuality = 0;      Preference currentPref = null;      Preference bestLanguagePref = null;      Preference bestMediaTypePref = null;      float currentScore = 0;      float bestLanguageScore = 0;      float bestMediaTypeScore = 0;      // For each media type supported by this resource      List<RepresentationMetadata> variants = resource.getVariantsMetadata();      if(variants == null)      {         logger.warning("No variant found for resource: " + getResourceRef().getIdentifier());         setStatus(Statuses.CLIENT_ERROR_NOT_FOUND);      }      else      {         logger.info(Integer.toString(variants.size()) + " variants found for resource: " + getResourceRef().getIdentifier());         // For each available variant, we will compute the negotiation score         // which is dependant on the language score and on the media type score         for(Iterator iter1 = variants.iterator(); iter1.hasNext();)         {            currentVariant = (RepresentationMetadata)iter1.next();            // For each language preference defined in the call            // Calculate the score and remember the best scoring preference            for(Iterator iter2 = getLanguagePrefs().iterator(); (currentVariant.getLanguage() != null) && iter2.hasNext();)            {               currentPref = (Preference)iter2.next();               currentLanguage = (Language)currentPref.getMetadata();               compatiblePref = true;               currentScore = 0;               // 1) Compare the main tag               if(currentVariant.getLanguage().getMainTag().equals(currentLanguage.getMainTag()))               {                  currentScore += 100;               }               else if(!currentLanguage.getMainTag().equals("*"))               {                  compatiblePref = false;               }               else if(currentLanguage.getSubTag() != null)               {                  // Only "*" is an acceptable language range                  compatiblePref = false;               }               else               {                  // The valid "*" range has the lowest valid score                  currentScore++;               }               if(compatiblePref)               {                  // 2) Compare the sub tags                  if((currentLanguage.getSubTag() == null) || (currentVariant.getLanguage().getSubTag() == null))                  {                     if(currentVariant.getLanguage().getSubTag() == currentLanguage.getSubTag())                     {                        currentScore += 10;                     }                     else                     {                        // Don't change the score                     }                  }                  else if(currentLanguage.getSubTag().equals(currentVariant.getLanguage().getSubTag()))                  {                     currentScore += 10;                  }                  else                  {                     // SubTags are different                     compatiblePref = false;                  }                  // 3) Do we have a better preference?                  // currentScore *= currentPref.getQuality();                  if(compatiblePref && ((bestLanguagePref == null) || (currentScore > bestLanguageScore)))                  {                     bestLanguagePref = currentPref;                     bestLanguageScore = currentScore;                  }               }            }                        // If the variant has a language set, do we have a compatible preference?            compatibleLanguage = (currentVariant.getLanguage() == null) || (bestLanguagePref != null);                            // For each media range preference defined in the call            // Calculate the score and remember the best scoring preference            for(Iterator iter2 = getMediaTypePrefs().iterator(); compatibleLanguage && iter2.hasNext();)            {               currentPref = (Preference)iter2.next();               currentMediaType = (MediaType)currentPref.getMetadata();               compatiblePref = true;               currentScore = 0;               // 1) Compare the main types               if(currentMediaType.getMainType().equals(currentVariant.getMediaType().getMainType()))               {                  currentScore += 1000;               }               else if(!currentMediaType.getMainType().equals("*"))               {                  compatiblePref = false;               }               else if(!currentMediaType.getSubType().equals("*"))               {                  // Ranges such as "*/html" are not supported                  // Only "*/*" is acceptable in this case                  compatiblePref = false;               }               if(compatiblePref)               {                  // 2) Compare the sub types                  if(currentVariant.getMediaType().getSubType().equals(currentMediaType.getSubType()))                  {                     currentScore += 100;                  }                  else if(!currentMediaType.getSubType().equals("*"))                  {                     // Subtype are different                     compatiblePref = false;                  }                  if(compatiblePref && (currentVariant.getMediaType().getParameters() != null))                  {                     // 3) Compare the parameters                     // If current media type is compatible with the current                     // media range then the parameters need to be checked too                     for(Iterator iter3 = currentVariant.getMediaType().getParameters().iterator(); iter3                           .hasNext();)                     {                        currentParam = (Parameter)iter3.next();                        if(isParameterFound(currentParam, currentMediaType))                        {                           currentScore++;                        }                     }                  }                  // 3) Do we have a better preference?                  // currentScore *= currentPref.getQuality();                  if(compatiblePref && ((bestMediaTypePref == null) || (currentScore > bestMediaTypeScore)))                  {                     bestMediaTypePref = currentPref;                     bestMediaTypeScore = currentScore;                  }               }            }            // Do we have a compatible media type?            if(bestMediaTypePref != null)            {               // So, let's conclude on the current variant, its quality               float currentQuality = bestMediaTypePref.getQuality();               if(bestLanguagePref != null)               {                  currentQuality += (bestLanguagePref.getQuality() * 10F);               }                           if(bestVariant == null)               {                  bestVariant = currentVariant;                  bestQuality = currentQuality;               }               else if(currentQuality > bestQuality)               {                  bestVariant = currentVariant;                  bestQuality = currentQuality;               }                              // Reset the preference variables               bestLanguagePref = null;               bestLanguageScore = 0;               bestMediaTypePref = null;               bestMediaTypeScore = 0;            }         }         if(bestVariant == null)         {            // No variant was found matchin the call preferences            setStatus(Statuses.CLIENT_ERROR_NOT_ACCEPTABLE);         }         else         {            // Set the best representation as the call output            setOutput(resource.getRepresentation(bestVariant));            setStatus(Statuses.SUCCESS_OK);         }      }   }