| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ImageFileConversionInfo |
|
| 4.8;4,8 |
| 1 | package wjhk.jupload2.filedata.helper; | |
| 2 | ||
| 3 | import java.util.HashMap; | |
| 4 | import java.util.Map; | |
| 5 | ||
| 6 | import wjhk.jupload2.exception.JUploadException; | |
| 7 | import wjhk.jupload2.policies.UploadPolicy; | |
| 8 | ||
| 9 | /** | |
| 10 | * this class is used to parse the | |
| 11 | * {@link UploadPolicy#PROP_TARGET_PICTURE_FORMAT} parameter and provide easy | |
| 12 | * access to the conversions | |
| 13 | * <ul> | |
| 14 | * <li>all file extensions are case-insensitive</li> | |
| 15 | * <li>jpg and jpeg are distinct!</li> | |
| 16 | * </ul> | |
| 17 | * expected format example: {@code "png,bmp:jpg;gif:png;"} | |
| 18 | * | |
| 19 | * @see UploadPolicy | |
| 20 | */ | |
| 21 | public class ImageFileConversionInfo { | |
| 22 | ||
| 23 | private static final String FORMAT_SEPARATOR = ","; | |
| 24 | ||
| 25 | private static final String RELATION_SEPARATOR = ";"; | |
| 26 | ||
| 27 | private static final String RELATION_ASSIGNMENT = ":"; | |
| 28 | ||
| 29 | /** | |
| 30 | * will only contain strings in lower-case formats | |
| 31 | * <ul> | |
| 32 | * <li>key: source format</li> | |
| 33 | * <li>value: target format</li> | |
| 34 | * </ul> | |
| 35 | */ | |
| 36 | 50 | private Map<String, String> formatRelations = new HashMap<String, String>(); |
| 37 | ||
| 38 | /** | |
| 39 | * will build a new ImageFileConversionInfo object for the given | |
| 40 | * conversionList. | |
| 41 | * | |
| 42 | * @param conversionList e.g. {@code "png,bmp:jpg;gif:png;"}, may be empty | |
| 43 | * or {@code null} | |
| 44 | * @throws JUploadException if the conversionList is erroneous | |
| 45 | */ | |
| 46 | public ImageFileConversionInfo(String conversionList) | |
| 47 | 50 | throws JUploadException { |
| 48 | 50 | parseConversionList(conversionList); |
| 49 | 50 | } |
| 50 | ||
| 51 | /** | |
| 52 | * returns the target format (in lowercase) for the given sourceFormat or | |
| 53 | * {@code null} if no conversion is necessary (or if sourceFormat is {@code | |
| 54 | * null}) | |
| 55 | * | |
| 56 | * @param sourceFormat format of the source file (case does not matter): | |
| 57 | * e.g. jpg, JpeG, png, .. | |
| 58 | * @return the target format (in lowercase) for the given sourceFormat or | |
| 59 | * {@code null} if no conversion is necessary (or if sourceFormat is | |
| 60 | * {@code null}) | |
| 61 | */ | |
| 62 | public String getTargetFormatOrNull(String sourceFormat) { | |
| 63 | 15 | if (sourceFormat == null) { |
| 64 | 0 | return null; |
| 65 | } | |
| 66 | 15 | String mapValue = this.formatRelations.get(sourceFormat.toLowerCase()); |
| 67 | 15 | return mapValue; |
| 68 | } | |
| 69 | ||
| 70 | /** | |
| 71 | * returns the target format for the given sourceFormat. | |
| 72 | * <ul> | |
| 73 | * <li>the case of the sourceFormat does not matter</li> | |
| 74 | * <li>the returned format will always be lower-case</li> | |
| 75 | * <li>if a conversion is necessary the target format will be returned: e.g. | |
| 76 | * if "bmp" should be converted to a "png": "png" will be returned</li> | |
| 77 | * <li>if no conversion is necessary, the sourceFormat (in lower-case) will | |
| 78 | * be returned</li> | |
| 79 | * <li>if sourceFormat is {@code null}, {@code null} will be returned</li> | |
| 80 | * </ul> | |
| 81 | * | |
| 82 | * @param sourceFormat format of the source file (case does not matter): | |
| 83 | * e.g. jpg, JpeG, png, .. | |
| 84 | * @return the target format for the given sourceFormat (see details in the | |
| 85 | * method description) | |
| 86 | */ | |
| 87 | public String getTargetFormat(String sourceFormat) { | |
| 88 | 7 | if (sourceFormat == null) { |
| 89 | 0 | return null; |
| 90 | } | |
| 91 | 7 | String targetFormatOrNull = getTargetFormatOrNull(sourceFormat); |
| 92 | 7 | if (targetFormatOrNull == null) { |
| 93 | 7 | return sourceFormat.toLowerCase(); |
| 94 | } | |
| 95 | 0 | return targetFormatOrNull; |
| 96 | } | |
| 97 | ||
| 98 | /** | |
| 99 | * will parse the conversion list and fill formatRelations with entries.<br /> | |
| 100 | * see description of {@link UploadPolicy} for expected format. | |
| 101 | * | |
| 102 | * @param conversionList the conversion list to parse | |
| 103 | * @throws JUploadException if problems parsing the conversionList occured | |
| 104 | */ | |
| 105 | private void parseConversionList(String conversionList) | |
| 106 | throws JUploadException { | |
| 107 | 50 | if (conversionList == null || conversionList.equals("")) { |
| 108 | 50 | return; |
| 109 | } | |
| 110 | ||
| 111 | /* | |
| 112 | * example: conversionList="Png,bmp:JPG;gif:png" | |
| 113 | */ | |
| 114 | ||
| 115 | /* | |
| 116 | * if the conversion list does not end with the relation separator, we | |
| 117 | * add it to keep the parsing logic simpler | |
| 118 | */ | |
| 119 | 0 | if (!conversionList.endsWith(RELATION_SEPARATOR)) { |
| 120 | 0 | conversionList += RELATION_SEPARATOR; |
| 121 | } | |
| 122 | ||
| 123 | /* | |
| 124 | * example: conversionList="Png,bmp:JPG;gif:png;" | |
| 125 | */ | |
| 126 | ||
| 127 | 0 | String[] relations = conversionList.split(RELATION_SEPARATOR); |
| 128 | 0 | for (String relation : relations) { |
| 129 | /* | |
| 130 | * example: relation="Png,bmp:JPG" | |
| 131 | */ | |
| 132 | 0 | String[] assignmentDetails = relation.split(RELATION_ASSIGNMENT); |
| 133 | 0 | if (assignmentDetails.length != 2) { |
| 134 | 0 | throw new JUploadException("Invalid format: relation '" |
| 135 | + relation + "' should contain exatly one '" | |
| 136 | + RELATION_ASSIGNMENT + "'"); | |
| 137 | } | |
| 138 | 0 | String sourceFormatList = assignmentDetails[0]; |
| 139 | /* | |
| 140 | * example: sourceFormatList="Png,bmp" | |
| 141 | */ | |
| 142 | 0 | String targetFormat = assignmentDetails[1].toLowerCase(); |
| 143 | /* | |
| 144 | * example: targetFormat="jpg" | |
| 145 | */ | |
| 146 | 0 | String[] sourceFormats = sourceFormatList.split(FORMAT_SEPARATOR); |
| 147 | 0 | for (String sourceFormat : sourceFormats) { |
| 148 | /* | |
| 149 | * example: sourceFormat="Png" | |
| 150 | */ | |
| 151 | 0 | String lcSourceFormat = sourceFormat.toLowerCase(); |
| 152 | /* | |
| 153 | * example: lcSourceFormat="png" | |
| 154 | */ | |
| 155 | 0 | if (lcSourceFormat.equals(targetFormat)) { |
| 156 | 0 | throw new JUploadException("format '" + sourceFormat |
| 157 | + "' is assigned to itself"); | |
| 158 | } | |
| 159 | 0 | String putResult = this.formatRelations.put(lcSourceFormat, |
| 160 | targetFormat); | |
| 161 | 0 | if (putResult != null) { |
| 162 | 0 | throw new JUploadException("format '" + lcSourceFormat |
| 163 | + "' is assigned to multiple target formats: '" | |
| 164 | + targetFormat + "', '" + putResult + "'"); | |
| 165 | } | |
| 166 | } | |
| 167 | } | |
| 168 | 0 | } |
| 169 | ||
| 170 | /** | |
| 171 | * @see java.lang.Object#toString() | |
| 172 | */ | |
| 173 | @Override | |
| 174 | public String toString() { | |
| 175 | 0 | StringBuilder sb = new StringBuilder("ImageFileConversionInfo ("); |
| 176 | 0 | for (Map.Entry<String, String> formatRelation : this.formatRelations |
| 177 | 0 | .entrySet()) { |
| 178 | 0 | sb.append(formatRelation.getKey()); |
| 179 | 0 | sb.append("-->"); |
| 180 | 0 | sb.append(formatRelation.getValue()); |
| 181 | 0 | sb.append(";"); |
| 182 | 0 | } |
| 183 | 0 | sb.append(")"); |
| 184 | ||
| 185 | 0 | return sb.toString(); |
| 186 | } | |
| 187 | } |