1 19 package org.netbeans.modules.xml.wsdl.validator.visitor; 20 21 import java.util.Collection ; 22 import java.util.StringTokenizer ; 23 import javax.xml.namespace.QName ; 24 import org.netbeans.modules.xml.wsdl.model.WSDLComponent; 25 import org.netbeans.modules.xml.xam.Component; 26 import org.netbeans.modules.xml.xam.dom.AbstractDocumentComponent; 27 import org.netbeans.modules.xml.xam.spi.Validator; 28 import org.netbeans.modules.xml.xam.spi.Validator.ResultItem; 29 import org.openide.util.NbBundle; 30 31 32 38 public class ValidateSupport { 39 40 41 42 private ValidateConfiguration mValConfig; 43 44 45 public static final String VAL_MISSING_ATTRIB = "VAL_MISSING_ATTRIB"; 47 48 public static final String FIX_MISSING_ATTRIB = "FIX_MISSING_ATTRIB"; 50 51 public static final String VAL_NOT_NCNAME_ATTRIB = "VAL_NOT_NCNAME_ATTRIB"; 53 54 public static final String FIX_NOT_NCNAME_ATTRIB = "FIX_NOT_NCNAME_ATTRIB"; 56 57 public static final String VAL_NOT_QNAME_ATTRIB = "VAL_NOT_QNAME_ATTRIB"; 59 60 public static final String FIX_NOT_QNAME_ATTRIB = "FIX_NOT_QNAME_ATTRIB"; 62 63 public static final String VAL_PREFIX_NULL_QNAME_ATTRIB = 64 "VAL_PREFIX_NULL_QNAME_ATTRIB"; 66 67 public static final String FIX_PREFIX_NULL_QNAME_ATTRIB = 68 "FIX_PREFIX_NULL_QNAME_ATTRIB"; 70 71 public static final String VAL_PREFIX_NOT_DEFINED = "VAL_PREFIX_NOT_DEFINED"; 73 74 public static final String FIX_PREFIX_NOT_DEFINED = "FIX_PREFIX_NOT_DEFINED"; 76 77 public static final String VAL_NOT_BOOLEAN_ATTRIB = "VAL_NOT_BOOLEAN_ATTRIB"; 79 80 public static final String FIX_NOT_BOOLEAN_ATTRIB = "FIX_NOT_BOOLEAN_ATTRIB"; 82 83 public static final String VAL_NOT_ENUM_ATTRIB = "VAL_NOT_ENUM_ATTRIB"; 85 86 public static final String FIX_NOT_ENUM_ATTRIB = "FIX_NOT_ENUM_ATTRIB"; 88 89 public static final String VAL_MIN_ELEM_NOT_MET = "VAL_MIN_ELEM_NOT_MET"; 91 92 public static final String FIX_MIN_ELEM_NOT_MET = "FIX_MIN_ELEM_NOT_MET"; 94 95 public static final String VAL_MISSING_ELEM = "VAL_MISSING_ELEM"; 97 98 public static final String FIX_MISSING_ELEM = "FIX_MISSING_ELEM"; 100 101 public static final String VAL_MATCHING_WSDL_NOT_FOUND = "VAL_MATCHING_WSDL_NOT_FOUND"; 103 104 public static final String FIX_MATCHING_WSDL_NOT_FOUND = "FIX_MATCHING_WSDL_NOT_FOUND"; 106 107 public static final String VAL_COMPENSATE_WITH_AN_INVALID_SCOPE = 108 "VAL_COMPENSATE_WITH_AN_INVALID_SCOPE"; 110 111 public static final String FIX_COMPENSATE_WITH_AN_INVALID_SCOPE = 112 "FIX_COMPENSATE_WITH_AN_INVALID_SCOPE"; 114 115 public static final String VAL_MESSAGETYPE_IN_CONTAINER = "VAL_MESSAGETYPE_IN_CONTAINER"; 117 public static final String FIX_MESSAGETYPE_IN_CONTAINER = "FIX_MESSAGETYPE_IN_CONTAINER"; 119 120 public static final String VAL_UNKNOWN_CONTAINER_IN_CATCH = "VAL_UNKNOWN_CONTAINER_IN_CATCH"; 122 public static final String FIX_UNKNOWN_CONTAINER_IN_CATCH = "FIX_UNKNOWN_CONTAINER_IN_CATCH"; 124 public static final String VAL_EMPTY_CONTAINER_IN_CATCH = "VAL_EMPTY_CONTAINER_IN_CATCH"; 126 public static final String SEQUENCE_TITLE_NAME = "SEQUENCE_TITLE_NAME"; 128 public static final String VAL_MISSING_CONNECTIONS = "VAL_MISSING_CONNECTIONS"; 130 public static final String FIX_MISSING_CONNECTIONS = "FIX_MISSING_CONNECTIONS"; 132 133 public static final String VAL_SEMANTICALLY_CORRECT_CORRELATION_SET = 134 "VAL_SEMANTICALLY_CORRECT_CORRELATION_SET"; 136 public static final String FIX_SEMANTICALLY_CORRECT_CORRELATION_SET = 137 "FIX_SEMANTICALLY_CORRECT_CORRELATION_SET"; 139 public static final String VAL_FAULT_NAME_CANNOT_BE_EMPTY = "VAL_FAULT_NAME_CANNOT_BE_EMPTY"; 141 public static final String FIX_FAULT_NAME_CANNOT_BE_EMPTY = "FIX_FAULT_NAME_CANNOT_BE_EMPTY"; 143 public static final String VAL_UNKNOWN_OPERATION = "VAL_UNKNOWN_OPERATION"; 145 public static final String FIX_UNKNOWN_OPERATION = "FIX_UNKNOWN_OPERATION"; 147 public static final String VAL_UNKNOWN_PORT_TYPE = "VAL_UNKNOWN_PORT_TYPE"; 149 public static final String FIX_UNKNOWN_PORT_TYPE = "FIX_UNKNOWN_PORT_TYPE"; 151 public static final String VAL_ASSIGN_MUST_HAVE_MINIMUM = "VAL_ASSIGN_MUST_HAVE_MINIMUM"; 153 public static final String FIX_ASSIGN_MUST_HAVE_MINIMUM = "FIX_ASSIGN_MUST_HAVE_MINIMUM"; 157 public static final String VAL_FOR_EACH_MUST_HAVE_MIN_ONE_COPY = 158 "VAL_FOR_EACH_MUST_HAVE_MIN_ONE_COPY"; 160 public static final String FIX_FOR_EACH_MUST_HAVE_MIN_ONE_COPY = 161 "FIX_FOR_EACH_MUST_HAVE_MIN_ONE_COPY"; 163 166 public static final String VAL_EMPTY_SCOPE = "VAL_EMPTY_SCOPE"; 168 171 public static final String FIX_EMPTY_SCOPE = "FIX_EMPTY_SCOPE"; 173 176 public static final String VAL_EMPTY_WHILE = "VAL_EMPTY_WHILE"; 178 181 public static final String FIX_EMPTY_WHILE = "FIX_EMPTY_WHILE"; 183 186 public static final String VAL_EMPTY_COMPENSATION_HANDLER = "VAL_EMPTY_COMPENSATION_HANDLER"; 188 191 public static final String FIX_EMPTY_COMPENSATION_HANDLER = "FIX_EMPTY_COMPENSATION_HANDLER"; 193 196 public static final String VAL_EMPTY_CATCH = "VAL_EMPTY_CATCH"; 198 201 public static final String FIX_EMPTY_CATCH = "FIX_EMPTY_CATCH"; 203 206 public static final String VAL_EMPTY_CATCHALL = "VAL_EMPTY_CATCHALL"; 208 211 public static final String FIX_EMPTY_CATCHALL = "FIX_EMPTY_CATCHALL"; 213 214 public static final String VAL_NO_EINSIGHT_LICENSE_W_EVISION = "VAL_NO_EINSIGHT_LICENSE_W_EVISION"; 216 217 public static final String FIX_NO_EINSIGHT_LICENSE_W_EVISION = "FIX_NO_EINSIGHT_LICENSE_W_EVISION"; 219 220 private Collection <ResultItem> mResultItems; 221 private Validator mValidator; 222 223 226 public ValidateSupport(ValidateConfiguration valConfig) { 227 super(); 228 mValConfig = valConfig; 229 } 230 231 234 public ValidateConfiguration getValidateConfiguration() { 235 return mValConfig; 236 } 237 238 241 public void setValidateConfiguration(ValidateConfiguration valConfig) { 242 mValConfig = valConfig; 243 } 244 245 public void setResultItems(Collection <ResultItem> resultItems) { 246 mResultItems = resultItems; 247 } 248 public void setValidator(Validator validator) { 249 mValidator = validator; 250 } 251 252 256 public static boolean isAttributeAbsent(String value) { 257 return ((null == value) || (value.trim().length() == 0)); 258 } 259 260 267 public boolean assertRequiredAttrib(String value, String name, Component source) { 268 if (((source instanceof WSDLComponent) 269 && !mValConfig.getBooleanProperty(ValidateConfiguration.WSDL_SYNTAX_ATTRIB_REQUIRED))) { 270 return true; 271 } 272 273 if (isAttributeAbsent(value)) { 274 return fireToDo(Validator.ResultType.ERROR, source, NbBundle.getMessage(ValidateSupport.class, VAL_MISSING_ATTRIB, name), 275 NbBundle.getMessage(ValidateSupport.class, FIX_MISSING_ATTRIB, name)); 276 } 277 278 return true; 279 } 280 281 288 public boolean assertRequiredAttrib(QName value, String name, Component source, int category) { 289 if (((source instanceof WSDLComponent) 290 && !mValConfig.getBooleanProperty(ValidateConfiguration.WSDL_SYNTAX_ATTRIB_REQUIRED))) { 291 return true; 292 } 293 294 if (value == null || isAttributeAbsent(value.toString())) { 295 return fireToDo(Validator.ResultType.ERROR, source, NbBundle.getMessage(ValidateSupport.class, VAL_MISSING_ATTRIB, name), 296 NbBundle.getMessage(ValidateSupport.class, FIX_MISSING_ATTRIB, name)); 297 } 298 299 return true; 300 } 301 302 309 public boolean assertNCNameAttrib(String value, String name, Component source) { 310 if (((source instanceof WSDLComponent) 311 && !mValConfig.getBooleanProperty(ValidateConfiguration.WSDL_SYNTAX_ATTRIB_NCNAME))) { 312 return true; 313 } 314 315 if (!isAttributeAbsent(value) && !ValidationUtils.isNCName(value)) { 316 return fireToDo(Validator.ResultType.ERROR, source, 317 NbBundle.getMessage(ValidateSupport.class, VAL_NOT_NCNAME_ATTRIB, name), 318 NbBundle.getMessage(ValidateSupport.class, FIX_NOT_NCNAME_ATTRIB, name) 319 ); 320 } 321 return true; 322 } 323 324 331 public boolean assertQNameListAttrib(String value, String name, Component source, int category) { 332 if ((source instanceof WSDLComponent) 333 && !mValConfig.getBooleanProperty(ValidateConfiguration.WSDL_SYNTAX_ATTRIB_QNAME)) { 334 return true; 335 } 336 337 if (!isAttributeAbsent(value)) { 338 StringTokenizer parser = new StringTokenizer (value, " "); 339 while (parser.hasMoreTokens()) { 340 if (!assertQNameAttrib(parser.nextToken(), name, source, category)) { 341 return false; 342 } 343 } 344 } 345 return true; 346 } 347 348 355 public boolean assertQNameAttrib(QName value, String name, Component source, int category) { 356 if(value != null) { 357 return assertQNameAttrib(value.toString(), name, source, category); 358 } 359 return true; 360 } 361 362 369 public boolean assertQNameAttrib(String value, String name, Component source, int category) { 370 if (((source instanceof WSDLComponent) 371 && !mValConfig.getBooleanProperty(ValidateConfiguration.WSDL_SYNTAX_ATTRIB_QNAME))) { 372 return true; 373 } 374 375 if (!isAttributeAbsent(value)) { 376 if (!ValidationUtils.isQName(value)) { 378 if (!fireToDo(Validator.ResultType.ERROR, 379 source, 380 381 NbBundle.getMessage(ValidateSupport.class, VAL_NOT_QNAME_ATTRIB, name), 382 NbBundle.getMessage(ValidateSupport.class, FIX_NOT_QNAME_ATTRIB, name) 383 )) { 384 return false; 385 } 386 } 387 String [] parts = value.split(":"); if (parts != null && parts.length > 1) { 389 390 String prefix = parts[0]; 391 if (prefix != null) { 392 String ns = ((AbstractDocumentComponent) source).lookupNamespaceURI(prefix); 394 395 if (null == ns) { 396 if (!fireToDo(Validator.ResultType.ERROR, 397 source, 398 NbBundle.getMessage(ValidateSupport.class, VAL_PREFIX_NOT_DEFINED, prefix), 399 NbBundle.getMessage(ValidateSupport.class, FIX_PREFIX_NOT_DEFINED, prefix) 400 )) { 401 return false; 402 } 403 } 404 } 405 } 406 } 407 return true; 408 } 409 410 419 public boolean assertQNameAttrib(String value, String name, Component source, 420 int category, boolean verifyNullPrefix) { 421 boolean valid = assertQNameAttrib(value, name, source, category); 422 if (valid && verifyNullPrefix) { 423 424 String [] parts = value.split(":"); if (parts != null && parts.length > 1) { 426 427 String prefix = parts[0]; 428 if (prefix == null || "".equals(prefix.trim())) { 430 if (!fireToDo(Validator.ResultType.ERROR, 431 source, 432 NbBundle.getMessage(ValidateSupport.class, VAL_PREFIX_NULL_QNAME_ATTRIB, name), 433 NbBundle.getMessage(ValidateSupport.class, FIX_PREFIX_NULL_QNAME_ATTRIB, name) 434 )) { 435 return false; 436 } 437 } 438 } 439 } 440 441 return true; 442 } 443 444 451 public boolean assertBooleanAttrib(String value, String name, Component source, int category) { 452 if ((source instanceof WSDLComponent) 453 && !mValConfig.getBooleanProperty(ValidateConfiguration.WSDL_SYNTAX_ATTRIB_BOOLEAN)) { 454 return true; 455 } 456 457 if (!isAttributeAbsent(value) && !(value.equals("yes") || value.equals("no"))) { 458 return fireToDo(Validator.ResultType.ERROR, 459 source, 460 NbBundle.getMessage(ValidateSupport.class, VAL_NOT_BOOLEAN_ATTRIB, name), 461 NbBundle.getMessage(ValidateSupport.class, FIX_NOT_BOOLEAN_ATTRIB, name) 462 ); 463 } 464 return true; 465 } 466 467 475 public boolean assertMinimumElem(int num, int min, String name, Component source, int category) { 476 if (((source instanceof WSDLComponent) 477 && !mValConfig.getBooleanProperty(ValidateConfiguration.WSDL_SYNTAX_ELEM_MIN))) { 478 return true; 479 } 480 481 if (num < min) { 482 return fireToDo(Validator.ResultType.ERROR, 483 source, 484 486 NbBundle.getMessage(ValidateSupport.class, VAL_MIN_ELEM_NOT_MET, Integer.valueOf(min), name, Integer.valueOf(num)), 487 NbBundle.getMessage(ValidateSupport.class, FIX_MIN_ELEM_NOT_MET, Integer.valueOf(min - num), name) 488 ); 489 } 490 return true; 491 } 492 493 501 public boolean assertEnumeratedAttrib(String value, String [] enums, String name, Component source, 502 int category) { 503 if (((source instanceof WSDLComponent) 504 && !mValConfig.getBooleanProperty(ValidateConfiguration.WSDL_SYNTAX_ATTRIB_ENUMERATED))) { 505 return true; 506 } 507 508 if (!isAttributeAbsent(value) && (enums != null) && (enums.length > 0)) { 509 boolean found = false; 510 for (int i = 0; i < enums.length; i++) { 511 if (ValidationUtils.areEqualXMLValues(value, enums[i])) { 512 found = true; 513 break; 514 } 515 } 516 if (!found) { 517 StringBuffer enumsb = new StringBuffer (); 518 for (int i = 0; i < enums.length; i++) { 519 if (i > 0) { 520 enumsb.append(", "); 521 } 522 enumsb.append(enums[i]); 523 } 524 return fireToDo(Validator.ResultType.ERROR, 525 source, 526 NbBundle.getMessage(ValidateSupport.class, VAL_NOT_ENUM_ATTRIB, name, enumsb.toString()), 527 NbBundle.getMessage(ValidateSupport.class, FIX_NOT_ENUM_ATTRIB, enumsb.toString(), name) 528 ); 529 } 530 } 531 return true; 532 } 533 534 541 public boolean assertRequiredElement(Component subElem, String name, Component source, int category) { 542 if (((source instanceof WSDLComponent) 543 && !mValConfig.getBooleanProperty(ValidateConfiguration.WSDL_SYNTAX_ELEM_REQUIRED))) { 544 return true; 545 } 546 547 if (null == subElem) { 548 return fireToDo(Validator.ResultType.ERROR, 549 source, 550 NbBundle.getMessage(ValidateSupport.class, VAL_MISSING_ELEM, name), 551 NbBundle.getMessage(ValidateSupport.class, FIX_MISSING_ELEM, name)); 552 } 553 return true; 554 } 555 556 561 public boolean fireToDo(Validator.ResultType type, Component component, String desc, String correction) { 562 String message = desc; 563 if (correction != null) { 564 message = desc + " : " + correction; 565 } 566 ResultItem item = new Validator.ResultItem(mValidator, type, component, message); 567 mResultItems.add(item); 568 return true; 569 } 570 } 571 | Popular Tags |