| 1 | | | |
| 2 | | | |
| 3 | | | |
| 4 | | | |
| 5 | | | |
| 6 | | | |
| 7 | | | |
| 8 | | | |
| 9 | | | |
| 10 | | | |
| 11 | | | |
| 12 | | | |
| 13 | | | |
| 14 | | | |
| 15 | | | |
| 16 | | | |
| 17 | | | |
| 18 | | | |
| 19 | | | |
| 20 | | | |
| 21 | | | |
| 22 | | | |
| 23 | | | |
| 24 | | | |
| 25 | | | |
| 26 | | | |
| 27 | | | |
| 28 | | | |
| 29 | | | |
| 30 | | | |
| 31 | | | |
| 32 | | | |
| 33 | | | package org.jcoderz.commons.taskdefs; |
| 34 | | | |
| 35 | | | import java.util.ArrayList; |
| 36 | | | import java.util.HashMap; |
| 37 | | | import java.util.List; |
| 38 | | | import java.util.Map; |
| 39 | | | import java.util.regex.Pattern; |
| 40 | | | |
| 41 | | | import org.xml.sax.Attributes; |
| 42 | | | import org.xml.sax.SAXException; |
| 43 | | | import org.xml.sax.SAXParseException; |
| 44 | | | import org.xml.sax.helpers.DefaultHandler; |
| 45 | | | |
| 46 | | | class AppInfoSaxHandler |
| 47 | | | extends DefaultHandler |
| 48 | | | { |
| 49 | | | |
| 50 | | | public static final int APPLICATION_ID_FWK = 1; |
| 51 | | | |
| 52 | | | public static final int APPLICATION_ID_ASF = 100; |
| 53 | | | |
| 54 | | | public static final int APPLICATION_ID_PPG = 101; |
| 55 | | | |
| 56 | | | public static final int APPLICATION_ID_TAC = 102; |
| 57 | | | |
| 58 | | | public static final int APPLICATION_ID_ACM = 120; |
| 59 | | | |
| 60 | | | public static final int APPLICATION_ID_ACR = 121; |
| 61 | | | |
| 62 | | | public static final int APPLICATION_ID_DMB = 122; |
| 63 | | | |
| 64 | 0 | | private static final Pattern REGEX_SINGLE_QUOTES |
| 65 | | | = Pattern.compile(".*[^']'[^'].*", |
| 66 | | | Pattern.DOTALL + Pattern.MULTILINE); |
| 67 | | | |
| 68 | 0 | | private static final Pattern REGEX_VARIABLES |
| 69 | | | = Pattern.compile(".*\\{.+\\}.*", |
| 70 | | | Pattern.DOTALL + Pattern.MULTILINE); |
| 71 | | | |
| 72 | | | private static final String EMPTY_STRING = ""; |
| 73 | | | |
| 74 | 0 | | private SAXParseException mSaxParseException = null; |
| 75 | 0 | | private boolean mValidationError = false; |
| 76 | | | |
| 77 | | | |
| 78 | 0 | | private final NamedMap mMap = new NamedMap("applications"); |
| 79 | | | |
| 80 | 0 | | private String mCurrentAppName = EMPTY_STRING; |
| 81 | 0 | | private String mCurrentGrpName = EMPTY_STRING; |
| 82 | 0 | | private String mCurrentMsgName = EMPTY_STRING; |
| 83 | 0 | | private int mCurrentAppId = 0; |
| 84 | 0 | | private int mCurrentGrpId = 0; |
| 85 | 0 | | private int mCurrentMsgId = 0; |
| 86 | | | |
| 87 | 0 | | private final List mWarningMessages = new ArrayList(); |
| 88 | | | |
| 89 | 0 | | private final StringBuffer mBuffer = new StringBuffer(); |
| 90 | 0 | | private boolean mCaptureCharacters = false; |
| 91 | | | |
| 92 | | | |
| 93 | | | |
| 94 | | | |
| 95 | | | public AppInfoSaxHandler () |
| 96 | 0 | | { |
| 97 | 0 | | mMap.registerApplication(APPLICATION_ID_FWK, "FWK"); |
| 98 | 0 | | mMap.registerApplication(APPLICATION_ID_ASF, "ASF"); |
| 99 | 0 | | mMap.registerApplication(APPLICATION_ID_PPG, "PPG"); |
| 100 | 0 | | mMap.registerApplication(APPLICATION_ID_TAC, "TAC"); |
| 101 | 0 | | mMap.registerApplication(APPLICATION_ID_ACR, "ACR"); |
| 102 | 0 | | mMap.registerApplication(APPLICATION_ID_ACM, "ACM"); |
| 103 | 0 | | mMap.registerApplication(APPLICATION_ID_DMB, "DMB"); |
| 104 | 0 | | } |
| 105 | | | |
| 106 | | | {@inheritDoc} |
| 107 | | | public void error (SAXParseException exception) |
| 108 | | | { |
| 109 | 0 | | mValidationError = true; |
| 110 | 0 | | mSaxParseException = exception; |
| 111 | 0 | | } |
| 112 | | | |
| 113 | | | {@inheritDoc} |
| 114 | | | public void fatalError (SAXParseException exception) |
| 115 | | | { |
| 116 | 0 | | mValidationError = true; |
| 117 | 0 | | mSaxParseException = exception; |
| 118 | 0 | | } |
| 119 | | | |
| 120 | | | {@inheritDoc} |
| 121 | | | public void warning (SAXParseException exception) |
| 122 | | | { |
| 123 | | | |
| 124 | 0 | (1) | } |
| 125 | | | |
| 126 | | | {@inheritDoc} |
| 127 | | | public void startDocument () |
| 128 | | | { |
| 129 | 0 | | reset(); |
| 130 | 0 | | } |
| 131 | | | |
| 132 | | | {@inheritDoc} |
| 133 | | | public void startElement (String uri, String localName, |
| 134 | | | String qName, Attributes attributes) |
| 135 | | | throws SAXException |
| 136 | | | { |
| 137 | | | try |
| 138 | | | { |
| 139 | 0 | | if ("application".equals(localName)) |
| 140 | | | { |
| 141 | 0 | | mCurrentAppName = attributes.getValue("short-name"); |
| 142 | 0 | | mCurrentAppId = Integer.parseInt(attributes.getValue("id")); |
| 143 | | | |
| 144 | 0 | | mMap.addApplication(mCurrentAppId, mCurrentAppName); |
| 145 | | | } |
| 146 | 0 | | else if ("group".equals(localName)) |
| 147 | | | { |
| 148 | 0 | | mCurrentGrpName = attributes.getValue("short-name"); |
| 149 | 0 | | mCurrentGrpId = Integer.parseInt(attributes.getValue("id")); |
| 150 | | | |
| 151 | 0 | | mMap.getApp(mCurrentAppId).addGroup(mCurrentGrpId, mCurrentGrpName); |
| 152 | | | } |
| 153 | 0 | | else if ("message".equals(localName)) |
| 154 | | | { |
| 155 | 0 | | mCurrentMsgName = attributes.getValue("name"); |
| 156 | 0 | | mCurrentMsgId = Integer.parseInt(attributes.getValue("id")); |
| 157 | | | |
| 158 | 0 | | mMap.getApp(mCurrentAppId).getGrp( |
| 159 | | | mCurrentGrpId).addMessage(mCurrentMsgId, mCurrentMsgName); |
| 160 | | | } |
| 161 | 0 | | else if ("text".equals(localName) || "solution".equals(localName)) |
| 162 | | | { |
| 163 | 0 | | captureCharacters(); |
| 164 | | | } |
| 165 | | | } |
| 166 | 0 | | catch (AppInfoException e) |
| 167 | | | { |
| 168 | 0 | | throw new SAXException(e); |
| 169 | 0 | | } |
| 170 | 0 | | } |
| 171 | | | |
| 172 | | | {@inheritDoc} |
| 173 | | | public void endElement (String uri, String localName, String qName) |
| 174 | | | { |
| 175 | 0 | | if ("text".equals(localName)) |
| 176 | | | { |
| 177 | 0 | | final String cdata = characters().trim(); |
| 178 | 0 | | validateText(cdata); |
| 179 | 0 | | } |
| 180 | 0 | | else if ("solution".equals(localName)) |
| 181 | | | { |
| 182 | 0 | | final String cdata = characters().trim(); |
| 183 | 0 | | validateSolution(cdata); |
| 184 | 0 | | } |
| 185 | 0 | | else if ("description".equals(localName)) |
| 186 | | | { |
| 187 | 0 | | final String cdata = characters().trim(); |
| 188 | 0 | | validateDescription(cdata); |
| 189 | 0 | | } |
| 190 | 0 | | else if ("procedure".equals(localName)) |
| 191 | | | { |
| 192 | 0 | | final String cdata = characters().trim(); |
| 193 | 0 | | validateProcedure(cdata); |
| 194 | 0 | | } |
| 195 | 0 | | else if ("validation".equals(localName)) |
| 196 | | | { |
| 197 | 0 | | final String cdata = characters().trim(); |
| 198 | 0 | | validateValidation(cdata); |
| 199 | 0 | | } |
| 200 | 0 | | else if ("application".equals(localName)) |
| 201 | | | { |
| 202 | 0 | | mCurrentAppId = 0; |
| 203 | 0 | | mCurrentAppName = EMPTY_STRING; |
| 204 | | | } |
| 205 | 0 | | else if ("group".equals(localName)) |
| 206 | | | { |
| 207 | 0 | | mCurrentGrpId = 0; |
| 208 | 0 | | mCurrentGrpName = EMPTY_STRING; |
| 209 | | | } |
| 210 | 0 | | else if ("message".equals(localName)) |
| 211 | | | { |
| 212 | 0 | | mCurrentMsgId = 0; |
| 213 | 0 | | mCurrentMsgName = EMPTY_STRING; |
| 214 | | | } |
| 215 | 0 | | } |
| 216 | | | |
| 217 | | | {@inheritDoc} |
| 218 | | | public void characters (char[] ch, int start, int length) |
| 219 | | | { |
| 220 | 0 | | if (mCaptureCharacters) |
| 221 | | | { |
| 222 | 0 | | mBuffer.append(ch, start, length); |
| 223 | | | } |
| 224 | 0 | | } |
| 225 | | | |
| 226 | | | |
| 227 | | | <b></b> |
| 228 | | | |
| 229 | | | @return |
| 230 | | | |
| 231 | | | public String characters () |
| 232 | | | { |
| 233 | 0 | | final String result = mBuffer.toString(); |
| 234 | 0 | | mBuffer.setLength(0); |
| 235 | 0 | | mCaptureCharacters = false; |
| 236 | 0 | | return result; |
| 237 | | | } |
| 238 | | | |
| 239 | | | |
| 240 | | | <tt></tt> |
| 241 | | | @return<tt></tt> |
| 242 | | | <tt></tt> |
| 243 | | | |
| 244 | | | public boolean hasWarningMessages () |
| 245 | | | { |
| 246 | 0 | | return !mWarningMessages.isEmpty(); |
| 247 | | | } |
| 248 | | | |
| 249 | | | |
| 250 | | | |
| 251 | | | @return |
| 252 | | | |
| 253 | | | public List getWarningMessages () |
| 254 | | | { |
| 255 | 0 | | return mWarningMessages; |
| 256 | | | } |
| 257 | | | |
| 258 | | | |
| 259 | | | void captureCharacters () |
| 260 | | | { |
| 261 | 0 | | mCaptureCharacters = true; |
| 262 | 0 | | } |
| 263 | | | |
| 264 | | | SAXParseException getParseException () |
| 265 | | | { |
| 266 | 0 | | return mSaxParseException; |
| 267 | | | } |
| 268 | | | |
| 269 | | | boolean hasValidationErrors () |
| 270 | | | { |
| 271 | 0 | | return mValidationError; |
| 272 | | | } |
| 273 | | | |
| 274 | | | private void validateText (String cdata) |
| 275 | | | { |
| 276 | 0 | | if (cdata != null) |
| 277 | | | { |
| 278 | 0 | | if (REGEX_SINGLE_QUOTES.matcher(cdata).matches()) |
| 279 | | | { |
| 280 | 0 | | warn("The text element contains single quotes."); |
| 281 | | | } |
| 282 | | | } |
| 283 | 0 | | } |
| 284 | | | |
| 285 | | | private void validateSolution (String cdata) |
| 286 | | | { |
| 287 | 0 | | if (cdata != null) |
| 288 | | | { |
| 289 | 0 | | if (REGEX_VARIABLES.matcher(cdata).matches()) |
| 290 | | | { |
| 291 | 0 | (2) | warn("The solution element MUST NOT use variables: " + cdata); |
| 292 | | | } |
| 293 | | | } |
| 294 | 0 | | } |
| 295 | | | |
| 296 | | | private void validateDescription (String cdata) |
| 297 | | | { |
| 298 | 0 | | if (cdata != null) |
| 299 | | | { |
| 300 | 0 | | if (REGEX_VARIABLES.matcher(cdata).matches()) |
| 301 | | | { |
| 302 | 0 | | warn("The solution element MUST NOT use variables: " + cdata); |
| 303 | | | } |
| 304 | | | } |
| 305 | 0 | | } |
| 306 | | | |
| 307 | | | private void validateProcedure (String cdata) |
| 308 | | | { |
| 309 | 0 | | if (cdata != null) |
| 310 | | | { |
| 311 | 0 | | if (REGEX_VARIABLES.matcher(cdata).matches()) |
| 312 | | | { |
| 313 | 0 | | warn("The solution element MUST NOT use variables: " + cdata); |
| 314 | | | } |
| 315 | | | } |
| 316 | 0 | | } |
| 317 | | | |
| 318 | | | private void validateValidation (String cdata) |
| 319 | | | { |
| 320 | 0 | | if (cdata != null) |
| 321 | | | { |
| 322 | 0 | | if (REGEX_VARIABLES.matcher(cdata).matches()) |
| 323 | | | { |
| 324 | 0 | | warn("The solution element MUST NOT use variables: " + cdata); |
| 325 | | | } |
| 326 | | | } |
| 327 | 0 | | } |
| 328 | | | |
| 329 | | | private void warn (String message) |
| 330 | | | { |
| 331 | 0 | | mWarningMessages.add("[" + mCurrentAppName + "." |
| 332 | | | + mCurrentGrpName + "." + mCurrentMsgName + "] " + message); |
| 333 | 0 | | } |
| 334 | | | |
| 335 | | | private void reset () |
| 336 | | | { |
| 337 | 0 | | mBuffer.setLength(0); |
| 338 | 0 | | mCaptureCharacters = false; |
| 339 | 0 | | mCurrentAppName = EMPTY_STRING; |
| 340 | 0 | | mCurrentGrpName = EMPTY_STRING; |
| 341 | 0 | | mCurrentMsgName = EMPTY_STRING; |
| 342 | 0 | | mWarningMessages.clear(); |
| 343 | 0 | | mCurrentAppId = 0; |
| 344 | 0 | | mCurrentGrpId = 0; |
| 345 | 0 | | mCurrentMsgId = 0; |
| 346 | 0 | | } |
| 347 | | | |
| 348 | | | private static class NamedMap |
| 349 | | | { |
| 350 | 0 | | private final Map mMap = new HashMap(); |
| 351 | | | private final String mName; |
| 352 | | | |
| 353 | | | public NamedMap (String name) |
| 354 | 0 | | { |
| 355 | 0 | | mName = name; |
| 356 | 0 | | } |
| 357 | | | |
| 358 | | | public String getName () |
| 359 | | | { |
| 360 | 0 | | return mName; |
| 361 | | | } |
| 362 | | | |
| 363 | | | public boolean contains (int id) |
| 364 | | | { |
| 365 | 0 | | return mMap.containsKey(new Integer(id)); |
| 366 | | | } |
| 367 | | | |
| 368 | | | public NamedMap getApp (int id) |
| 369 | | | { |
| 370 | 0 | | return (NamedMap) mMap.get(new Integer(id)); |
| 371 | | | } |
| 372 | | | |
| 373 | | | public void registerApplication (int id, String appName) |
| 374 | | | { |
| 375 | 0 | | mMap.put(new Integer(id), new NamedMap(appName)); |
| 376 | 0 | | } |
| 377 | | | |
| 378 | | | public void addApplication (int id, String appName) |
| 379 | | | throws AppInfoException |
| 380 | | | { |
| 381 | 0 | | if (!contains(id)) |
| 382 | | | { |
| 383 | 0 | | throw new AppInfoException( |
| 384 | | | "Application " + appName + " with the identifier " |
| 385 | | | + id + " is not registered. " |
| 386 | | | + "Registered applications are " + mMap); |
| 387 | | | } |
| 388 | 0 | | } |
| 389 | | | |
| 390 | | | |
| 391 | | | public NamedMap getGrp (int id) |
| 392 | | | { |
| 393 | 0 | | return (NamedMap) mMap.get(new Integer(id)); |
| 394 | | | } |
| 395 | | | |
| 396 | | | public void addGroup (int id, String groupName) |
| 397 | | | throws AppInfoException |
| 398 | | | { |
| 399 | 0 | | if (contains(id)) |
| 400 | | | { |
| 401 | 0 | | final String registeredGrpName |
| 402 | | | = ((NamedMap) mMap.get(new Integer(id))).getName(); |
| 403 | 0 | | throw new AppInfoException("The group " + groupName |
| 404 | | | + " with the id " + id |
| 405 | | | + " is already assigned to " + registeredGrpName + "."); |
| 406 | | | } |
| 407 | 0 | | mMap.put(new Integer(id), new NamedMap(groupName)); |
| 408 | 0 | | } |
| 409 | | | |
| 410 | | | public void addMessage (int id, String messageName) |
| 411 | | | throws AppInfoException |
| 412 | | | { |
| 413 | 0 | | if (contains(id)) |
| 414 | | | { |
| 415 | 0 | | final String registeredMsgName = (String) mMap.get(new Integer(id)); |
| 416 | 0 | | throw new AppInfoException("The message " + messageName |
| 417 | | | + " with the id " + id |
| 418 | | | + " is already assigned to " + registeredMsgName + "."); |
| 419 | | | } |
| 420 | 0 | | mMap.put(new Integer(id), messageName); |
| 421 | | | |
| 422 | 0 | | } |
| 423 | | | |
| 424 | | | public String toString () |
| 425 | | | { |
| 426 | 0 | | final StringBuffer sb = new StringBuffer(); |
| 427 | 0 | | sb.append(mName); |
| 428 | 0 | | sb.append(' '); |
| 429 | 0 | | sb.append(mMap); |
| 430 | 0 | | return sb.toString(); |
| 431 | | | } |
| 432 | | | } |
| 433 | | | |
| 434 | | | |
| 435 | | | private static class AppInfoException |
| 436 | | | extends Exception |
| 437 | | | { |
| 438 | | | private static final long serialVersionUID = 1L; |
| 439 | | | |
| 440 | | | public AppInfoException (String msg) |
| 441 | | | { |
| 442 | 0 | | super(msg); |
| 443 | 0 | | } |
| 444 | | | |
| 445 | | | public AppInfoException (String msg, Throwable cause) |
| 446 | | | { |
| 447 | 0 | | super(msg, cause); |
| 448 | 0 | | } |
| 449 | | | } |
| 450 | | | |
| 451 | | | } |