001/** 002 * Java Web Archive Toolkit - Software to read and validate ARC, WARC 003 * and GZip files. (http://jwat.org/) 004 * Copyright 2011-2012 Netarkivet.dk (http://netarkivet.dk/) 005 * 006 * Licensed under the Apache License, Version 2.0 (the "License"); 007 * you may not use this file except in compliance with the License. 008 * You may obtain a copy of the License at 009 * 010 * http://www.apache.org/licenses/LICENSE-2.0 011 * 012 * Unless required by applicable law or agreed to in writing, software 013 * distributed under the License is distributed on an "AS IS" BASIS, 014 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 015 * See the License for the specific language governing permissions and 016 * limitations under the License. 017 */ 018package org.jwat.warc; 019 020import java.net.InetAddress; 021import java.util.Date; 022 023import org.jwat.common.ContentType; 024import org.jwat.common.Diagnosis; 025import org.jwat.common.DiagnosisType; 026import org.jwat.common.Diagnostics; 027import org.jwat.common.IPAddressParser; 028import org.jwat.common.Uri; 029import org.jwat.common.UriProfile; 030 031/** 032 * Separate class containing all the different types of field parser. 033 * Including validating parsers for strings, integers, longs, 034 * content-types, IP's, URI's, WARC dates, and WARC digests. 035 * 036 * @author nicl 037 */ 038public class WarcFieldParsers { 039 040 /** Diagnostics used to report diagnoses. 041 * Must be set prior to calling the various methods. */ 042 public Diagnostics diagnostics; 043 044 /** 045 * Add an error diagnosis on the given entity stating that it is invalid 046 * and something else was expected. The optional information should provide 047 * more details and/or format information. 048 * @param entity entity examined 049 * @param information optional extra information 050 */ 051 public void addInvalidExpectedError(String entity, String... information) { 052 diagnostics.addError(new Diagnosis(DiagnosisType.INVALID_EXPECTED, entity, information)); 053 } 054 055 /** 056 * Add a warning diagnosis on the given entity stating that it is empty. 057 * @param entity entity examined 058 */ 059 public void addEmptyWarning(String entity) { 060 diagnostics.addWarning(new Diagnosis(DiagnosisType.EMPTY, entity)); 061 } 062 063 /** 064 * Validates that the string is not null. 065 * @param str the value to validate 066 * @param field field name 067 * @return the original value 068 */ 069 public String parseString(String str, String field) { 070 if (((str == null) || (str.trim().length() == 0))) { 071 addEmptyWarning("'" + field + "' field"); 072 } 073 return str; 074 } 075 076 /** 077 * Returns an Integer object holding the value of the specified string. 078 * @param intStr the value to parse. 079 * @param field field name 080 * @return an integer object holding the value of the specified string or null, 081 * if unable to parse the value as an integer 082 */ 083 public Integer parseInteger(String intStr, String field) { 084 Integer iVal = null; 085 if (intStr != null && intStr.length() > 0) { 086 try { 087 iVal = Integer.valueOf(intStr); 088 } catch (Exception e) { 089 // Invalid integer value. 090 addInvalidExpectedError("'" + field + "' value", intStr, "Numeric format"); 091 } 092 } else { 093 // Missing integer value. 094 addEmptyWarning("'" + field + "' field"); 095 } 096 return iVal; 097 } 098 099 /** 100 * Returns a Long object holding the value of the specified string. 101 * @param longStr the value to parse. 102 * @param field field name 103 * @return a long object holding the value of the specified string or null, 104 * if unable to parse the value as a Long 105 */ 106 public Long parseLong(String longStr, String field) { 107 Long lVal = null; 108 if (longStr != null && longStr.length() > 0) { 109 try { 110 lVal = Long.valueOf(longStr); 111 } catch (Exception e) { 112 // Invalid long value. 113 addInvalidExpectedError("'" + field + "' value", longStr, "Numeric format"); 114 } 115 } else { 116 // Missing long value. 117 addEmptyWarning("'" + field + "' field"); 118 } 119 return lVal; 120 } 121 122 /** 123 * Parse and validate content-type string with optional parameters. 124 * @param contentTypeStr content-type string to parse 125 * @param field field name 126 * @return content type or null, if unable to extract the 127 * content-type 128 */ 129 public ContentType parseContentType(String contentTypeStr, String field) { 130 ContentType contentType = null; 131 if (contentTypeStr != null && contentTypeStr.length() != 0) { 132 contentType = ContentType.parseContentType(contentTypeStr); 133 if (contentType == null) { 134 // Invalid content-type. 135 addInvalidExpectedError("'" + field + "' value", contentTypeStr, WarcConstants.CONTENT_TYPE_FORMAT); 136 } 137 } else { 138 // Missing content-type. 139 addEmptyWarning("'" + field + "' field"); 140 } 141 return contentType; 142 } 143 144 /** 145 * Parse and validate an IP address. 146 * @param ipAddress the IP address to parse 147 * @param field field name 148 * @return the IP address or null, if unable to parse the value as an 149 * IP-address 150 */ 151 public InetAddress parseIpAddress(String ipAddress, String field) { 152 InetAddress inetAddr = null; 153 if (ipAddress != null && ipAddress.length() > 0) { 154 inetAddr = IPAddressParser.getAddress(ipAddress); 155 if (inetAddr == null) { 156 // Invalid ip address. 157 addInvalidExpectedError("'" + field + "' value", ipAddress, "IPv4 or IPv6 format"); 158 } 159 } else { 160 // Missing ip address. 161 addEmptyWarning("'" + field + "' field"); 162 } 163 return inetAddr; 164 } 165 166 /** 167 * Returns an URI object holding the value of the specified string. 168 * @param uriStr the URL to parse 169 * @param bLtGt switch indicating if we expected the uri inside '<' / '>' characters 170 * @param uriProfile the uri profile 171 * @param field field name 172 * @return an URI object holding the value of the specified string or null, 173 * if unable to parse the value as an URI object 174 */ 175 public Uri parseUri(String uriStr, boolean bLtGt, UriProfile uriProfile, String field) { 176 Uri uri = null; 177 String uriStrClean = uriStr; 178 int ltGtBf = 0; 179 if (uriStrClean != null && uriStrClean.length() != 0) { 180 int fIdx = 0; 181 int tIdx = uriStrClean.length(); 182 if (uriStrClean.startsWith("<")) { 183 ltGtBf |= 2; 184 ++fIdx; 185 } 186 if (uriStrClean.endsWith(">")) { 187 ltGtBf |= 1; 188 --tIdx; 189 } 190 if (ltGtBf != 0) { 191 uriStrClean = uriStrClean.substring(fIdx, tIdx); 192 } 193 if (bLtGt) { 194 switch (ltGtBf) { 195 case 2: 196 addInvalidExpectedError("'" + field + "' value", uriStr, "Missing trailing '>' character"); 197 break; 198 case 1: 199 addInvalidExpectedError("'" + field + "' value", uriStr, "Missing leading '<' character"); 200 break; 201 case 0: 202 addInvalidExpectedError("'" + field + "' value", uriStr, "Missing encapsulating '<' and '>' characters"); 203 break; 204 case 3: 205 default: 206 break; 207 } 208 } else { 209 switch (ltGtBf) { 210 case 2: 211 addInvalidExpectedError("'" + field + "' value", uriStr, "Unexpected leading '<' character"); 212 break; 213 case 1: 214 addInvalidExpectedError("'" + field + "' value", uriStr, "Unexpected trailing '>' character"); 215 break; 216 case 3: 217 addInvalidExpectedError("'" + field + "' value", uriStr, "Unexpected encapsulating '<' and '>' characters"); 218 break; 219 case 0: 220 default: 221 break; 222 } 223 } 224 try { 225 uri = new Uri(uriStrClean, uriProfile); 226 } catch (Exception e) { 227 // Invalid URI. 228 addInvalidExpectedError("'" + field + "' value", 229 uriStrClean, 230 e.getMessage()); 231 } 232 if (uri != null) { 233 String scheme = uri.getScheme(); 234 if (scheme == null) { 235 uri = null; 236 // Relative URI. 237 addInvalidExpectedError("'" + field + "' value", 238 uriStrClean, 239 "Absolute URI"); 240 } 241 } 242 } else { 243 // Missing URI. 244 addEmptyWarning("'" + field + "' field"); 245 } 246 return uri; 247 } 248 249 /** 250 * Parses WARC record date. 251 * @param dateStr the date to parse. 252 * @param field field name 253 * @return the formatted date or null, if unable to parse the value as a 254 * WARC record date 255 */ 256 @Deprecated 257 public Date parseDate(String dateStr, String field) { 258 Date date = null; 259 if (dateStr != null && dateStr.length() > 0) { 260 date = WarcDateParser.getDate(dateStr); 261 if (date == null) { 262 // Invalid date. 263 addInvalidExpectedError("'" + field + "' value", dateStr, WarcConstants.WARC_DATE_FORMAT); 264 } 265 } else { 266 // Missing date. 267 addEmptyWarning("'" + field + "' field"); 268 } 269 return date; 270 } 271 272 // FIXME Don't show format string for WARC/1.1 273 public WarcDate parseWarcDate(String dateStr, String field) { 274 WarcDate warcDate = null; 275 if (dateStr != null && dateStr.length() > 0) { 276 warcDate = WarcDate.getWarcDate(dateStr); 277 if (warcDate == null) { 278 // Invalid date. 279 addInvalidExpectedError("'" + field + "' value", dateStr, WarcConstants.WARC_DATE_FORMAT); 280 } 281 } else { 282 // Missing date. 283 addEmptyWarning("'" + field + "' field"); 284 } 285 return warcDate; 286 } 287 288 /** 289 * Parse and validate WARC digest string. 290 * @param labelledDigest WARC digest string to parse 291 * @param field field name 292 * @return digest wrapper object or null, if unable to parse the value as a 293 * WARC Digest 294 */ 295 public WarcDigest parseDigest(String labelledDigest, String field) { 296 WarcDigest digest = null; 297 if (labelledDigest != null && labelledDigest.length() > 0) { 298 digest = WarcDigest.parseWarcDigest(labelledDigest); 299 if (digest == null) { 300 // Invalid digest. 301 addInvalidExpectedError("'" + field + "' value", labelledDigest, WarcConstants.WARC_DIGEST_FORMAT); 302 } 303 } else { 304 // Missing digest. 305 addEmptyWarning("'" + field + "' field"); 306 } 307 return digest; 308 } 309 310}