} else if (!currentLanguage.getMainTag().equals("*")) { compatiblePref = false; } else if (currentLanguage.getSubTag() != null) {
} else if (!currentLanguage.getPrimaryTag().equals("*")) { compatiblePref = false; } else if (!currentLanguage.getSubTags().isEmpty()) {
    public Representation getPreferredVariant(ClientInfo client,            List<Representation> variants) {        if (variants == null) {            return null;        } else {            Parameter currentParam = null;            Language currentLanguage = null;            Language variantLanguage = null;            MediaType currentMediaType = null;            MediaType variantMediaType = null;            boolean compatiblePref = false;            boolean compatibleLanguage = false;            boolean compatibleMediaType = false;            Representation currentVariant = null;            Representation bestVariant = null;            Preference<Language> currentLanguagePref = null;            Preference<Language> bestLanguagePref = null;            Preference<MediaType> currentMediaTypePref = null;            Preference<MediaType> bestMediaTypePref = null;            float bestQuality = 0;            float currentScore = 0;            float bestLanguageScore = 0;            float bestMediaTypeScore = 0;            // 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 = (Representation) iter1.next();                variantLanguage = currentVariant.getLanguage();                variantMediaType = currentVariant.getMediaType();                // If no language preference is defined, assume that all                // languages are acceptable                List<Preference<Language>> languagePrefs = client                        .getAcceptedLanguages();                if (languagePrefs.size() == 0)                    languagePrefs.add(new Preference<Language>(Language.ALL));                // For each language preference defined in the call                // Calculate the score and remember the best scoring preference                for (Iterator<Preference<Language>> iter2 = languagePrefs                        .iterator(); (variantLanguage != null)                        && iter2.hasNext();) {                    currentLanguagePref = iter2.next();                    currentLanguage = currentLanguagePref.getMetadata();                    compatiblePref = true;                    currentScore = 0;                    // 1) Compare the main tag                    if (variantLanguage.getMainTag().equalsIgnoreCase(                            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)                                || (variantLanguage.getSubTag() == null)) {                            if (variantLanguage.getSubTag() == currentLanguage                                    .getSubTag()) {                                currentScore += 10;                            } else {                                // Don't change the score                            }                        } else if (currentLanguage.getSubTag()                                .equalsIgnoreCase(variantLanguage.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 = currentLanguagePref;                            bestLanguageScore = currentScore;                        }                    }                }                // Are the preferences compatible with the current variant                // language?                compatibleLanguage = (variantLanguage == null)                        || (bestLanguagePref != null);                // If no media type preference is defined, assume that all media                // types are acceptable                List<Preference<MediaType>> mediaTypePrefs = client                        .getAcceptedMediaTypes();                if (mediaTypePrefs.size() == 0)                    mediaTypePrefs                            .add(new Preference<MediaType>(MediaType.ALL));                // For each media range preference defined in the call                // Calculate the score and remember the best scoring preference                for (Iterator<Preference<MediaType>> iter2 = mediaTypePrefs                        .iterator(); compatibleLanguage && iter2.hasNext();) {                    currentMediaTypePref = iter2.next();                    currentMediaType = currentMediaTypePref.getMetadata();                    compatiblePref = true;                    currentScore = 0;                    // 1) Compare the main types                    if (currentMediaType.getMainType().equals(                            variantMediaType.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 (variantMediaType.getSubType().equals(                                currentMediaType.getSubType())) {                            currentScore += 100;                        } else if (!currentMediaType.getSubType().equals("*")) {                            // Subtype are different                            compatiblePref = false;                        }                        if (compatiblePref                                && (variantMediaType.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 = variantMediaType                                    .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 = currentMediaTypePref;                            bestMediaTypeScore = currentScore;                        }                    }                }                // Are the preferences compatible with the current media type?                compatibleMediaType = (variantMediaType == null)                        || (bestMediaTypePref != null);                if (compatibleLanguage && compatibleMediaType) {                    // Do we have a compatible media type?                    float currentQuality = 0;                    if (bestLanguagePref != null) {                        currentQuality += (bestLanguagePref.getQuality() * 10F);                    } else if (variantLanguage != null) {                        currentQuality += 0.1F * 10F;                    }                    if (bestMediaTypePref != null) {                        // So, let's conclude on the current variant, its                        // quality                        currentQuality += bestMediaTypePref.getQuality();                    }                    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;            }            return bestVariant;        }    }