|
Lines 1-25
Link Here
|
| 1 |
/****************************************************************************** |
1 |
/****************************************************************************** |
| 2 |
* Copyright (c) 2005, 2007 IBM Corporation and others. |
2 |
* Copyright (c) 2005, 2008 IBM Corporation, Zeligsoft Inc., and others. |
| 3 |
* All rights reserved. This program and the accompanying materials |
3 |
* All rights reserved. This program and the accompanying materials |
| 4 |
* are made available under the terms of the Eclipse Public License v1.0 |
4 |
* are made available under the terms of the Eclipse Public License v1.0 |
| 5 |
* which accompanies this distribution, and is available at |
5 |
* which accompanies this distribution, and is available at |
| 6 |
* http://www.eclipse.org/legal/epl-v10.html |
6 |
* http://www.eclipse.org/legal/epl-v10.html |
| 7 |
* |
7 |
* |
| 8 |
* Contributors: |
8 |
* Contributors: |
| 9 |
* IBM Corporation - initial API and implementation |
9 |
* IBM Corporation - initial API and implementation |
|
|
10 |
* Zeligsoft - Bug 249496 |
| 10 |
****************************************************************************/ |
11 |
****************************************************************************/ |
| 11 |
|
12 |
|
| 12 |
package org.eclipse.emf.validation.internal.service; |
13 |
package org.eclipse.emf.validation.internal.service; |
| 13 |
|
14 |
|
| 14 |
import java.util.Collection; |
15 |
import java.util.Collection; |
| 15 |
import java.util.Iterator; |
16 |
import java.util.Map; |
| 16 |
import java.util.Set; |
17 |
import java.util.Set; |
| 17 |
|
18 |
|
| 18 |
import org.eclipse.core.runtime.CoreException; |
19 |
import org.eclipse.core.runtime.CoreException; |
| 19 |
import org.eclipse.core.runtime.IConfigurationElement; |
20 |
import org.eclipse.core.runtime.IConfigurationElement; |
| 20 |
import org.eclipse.core.runtime.IStatus; |
21 |
import org.eclipse.core.runtime.IStatus; |
| 21 |
import org.eclipse.core.runtime.Status; |
22 |
import org.eclipse.core.runtime.Status; |
| 22 |
|
|
|
| 23 |
import org.eclipse.emf.validation.internal.EMFModelValidationPlugin; |
23 |
import org.eclipse.emf.validation.internal.EMFModelValidationPlugin; |
| 24 |
import org.eclipse.emf.validation.internal.EMFModelValidationStatusCodes; |
24 |
import org.eclipse.emf.validation.internal.EMFModelValidationStatusCodes; |
| 25 |
import org.eclipse.emf.validation.internal.l10n.ValidationMessages; |
25 |
import org.eclipse.emf.validation.internal.l10n.ValidationMessages; |
|
Lines 52-62
Link Here
|
| 52 |
private final IClientSelector selector; |
52 |
private final IClientSelector selector; |
| 53 |
private final boolean isDefault; |
53 |
private final boolean isDefault; |
| 54 |
|
54 |
|
| 55 |
// set of String constraint IDs that are bound to me |
55 |
// map of String constraint IDs that are bound to me, the boolean value |
| 56 |
private final Set<String> constraintBindings = new java.util.HashSet<String>(); |
56 |
// indicating absolute inclusion or exclusion. Absence of a value means |
|
|
57 |
// that we still need to compute |
| 58 |
private final Map<String, Boolean> constraintBindings = new java.util.HashMap<String, Boolean>(); |
| 57 |
|
59 |
|
| 58 |
// set of String category IDs that are bound to me |
60 |
// set of String category IDs that are bound to me |
| 59 |
private final Set<String> categoryBindings = new java.util.HashSet<String>(); |
61 |
private BindingFilter filter = BindingFilter.NULL; |
| 60 |
|
62 |
|
| 61 |
/** |
63 |
/** |
| 62 |
* Initializes me with my XML configuration. |
64 |
* Initializes me with my XML configuration. |
|
Lines 235-314
Link Here
|
| 235 |
} |
237 |
} |
| 236 |
|
238 |
|
| 237 |
public boolean includes(IModelConstraint constraint) { |
239 |
public boolean includes(IModelConstraint constraint) { |
| 238 |
boolean result = false; |
240 |
IConstraintDescriptor desc = constraint.getDescriptor(); |
| 239 |
|
241 |
|
| 240 |
IConstraintDescriptor descriptor = constraint.getDescriptor(); |
242 |
return (desc != null) && includes(desc); |
|
|
243 |
} |
| 244 |
|
| 245 |
boolean includes(IConstraintDescriptor constraint) { |
| 246 |
Boolean result = constraintBindings.get(constraint.getId()); |
| 241 |
|
247 |
|
| 242 |
if (descriptor != null) { |
248 |
if (result == null) { |
| 243 |
result = constraintBindings.contains(descriptor.getId()); |
249 |
// cache the result for this constraint |
| 244 |
|
250 |
result = filter.getBinding(constraint); |
| 245 |
if (!result && !categoryBindings.isEmpty()) { |
251 |
constraintBindings.put(constraint.getId(), result); |
| 246 |
// look for a bound category |
|
|
| 247 |
result = hasCategoryBinding(descriptor.getCategories()); |
| 248 |
|
| 249 |
if (result) { |
| 250 |
// cache the result for this constraint |
| 251 |
bindConstraint(descriptor.getId()); |
| 252 |
} |
| 253 |
} |
| 254 |
} |
252 |
} |
| 255 |
|
253 |
|
| 256 |
return result; |
254 |
return result; |
| 257 |
} |
255 |
} |
| 258 |
|
256 |
|
| 259 |
/** |
257 |
/** |
| 260 |
* Determines whether any of the specified <code>categories</code> is bound |
258 |
* Adds a constraint inclusion binding to me. |
| 261 |
* to me. |
|
|
| 262 |
* |
259 |
* |
| 263 |
* @param categories a collection of categories (usually from a constraint) |
260 |
* @param constraintId the ID of a constraint that is to be included in me |
| 264 |
* @return <code>true</code> if any of the <code>categories</code> is bound, |
|
|
| 265 |
* or if any of their ancestors is bound; <code>false</code>, otherwise |
| 266 |
*/ |
261 |
*/ |
| 267 |
private boolean hasCategoryBinding(Collection<Category> categories) { |
262 |
void includeConstraint(String constraintId) { |
| 268 |
boolean result = false; |
263 |
filter = filter.includeConstraint(constraintId); |
| 269 |
|
|
|
| 270 |
for (Iterator<Category> iter = categories.iterator(); !result && iter.hasNext();) { |
| 271 |
Category category = iter.next(); |
| 272 |
|
| 273 |
result = categoryBindings.contains(category.getPath()); |
| 274 |
|
| 275 |
if (!result) { |
| 276 |
// search the ancestors |
| 277 |
Category ancestor = category.getParent(); |
| 278 |
|
| 279 |
while ((ancestor != null) && !result) { |
| 280 |
result = categoryBindings.contains(ancestor.getPath()); |
| 281 |
ancestor = ancestor.getParent(); |
| 282 |
} |
| 283 |
|
| 284 |
if (result) { |
| 285 |
// cache the original category for quicker results on the |
| 286 |
// next constraint that it includes |
| 287 |
bindCategory(category.getPath()); |
| 288 |
} |
| 289 |
} |
| 290 |
} |
| 291 |
|
| 292 |
return result; |
| 293 |
} |
264 |
} |
| 294 |
|
265 |
|
| 295 |
/** |
266 |
/** |
| 296 |
* Binds a constraint to me. |
267 |
* Adds a constraint exclusion binding to me. |
| 297 |
* |
268 |
* |
| 298 |
* @param constraintId the ID of a constraint that is to be bound to me |
269 |
* @param constraintId the ID of a constraint that is to be excluded from me |
| 299 |
*/ |
270 |
*/ |
| 300 |
void bindConstraint(String constraintId) { |
271 |
void excludeConstraint(String constraintId) { |
| 301 |
constraintBindings.add(constraintId); |
272 |
filter = filter.excludeConstraint(constraintId); |
| 302 |
} |
273 |
} |
| 303 |
|
274 |
|
| 304 |
/** |
275 |
/** |
| 305 |
* Binds a constraint category to me. |
276 |
* Adds a constraint category inclusion to me. |
| 306 |
* |
277 |
* |
| 307 |
* @param categoryId the qualified ID (path) of a constraint category that |
278 |
* @param categoryId the qualified ID (path) of a constraint category that |
| 308 |
* is to be bound to me |
279 |
* is to be included in me |
| 309 |
*/ |
280 |
*/ |
| 310 |
void bindCategory(String categoryId) { |
281 |
void includeCategory(String categoryId) { |
| 311 |
categoryBindings.add(categoryId); |
282 |
filter = filter.includeCategory(categoryId); |
|
|
283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Adds a constraint category exclusion to me. |
| 287 |
* |
| 288 |
* @param categoryId |
| 289 |
* the qualified ID (path) of a constraint category that is to be |
| 290 |
* excluded from me |
| 291 |
*/ |
| 292 |
void excludeCategory(String categoryId) { |
| 293 |
filter = filter.excludeCategory(categoryId); |
| 294 |
} |
| 295 |
|
| 296 |
/** |
| 297 |
* Adds a client-context extension to me. |
| 298 |
* |
| 299 |
* @param clientContextID |
| 300 |
* a client-context to extend |
| 301 |
*/ |
| 302 |
void extendClientContext(String clientContextID) { |
| 303 |
filter = filter.extendClientContext(clientContextID); |
| 312 |
} |
304 |
} |
| 313 |
|
305 |
|
| 314 |
/** |
306 |
/** |
|
Lines 332-335
Link Here
|
| 332 |
public String toString() { |
324 |
public String toString() { |
| 333 |
return "ClientContext[" + getId() + ']'; //$NON-NLS-1$ |
325 |
return "ClientContext[" + getId() + ']'; //$NON-NLS-1$ |
| 334 |
} |
326 |
} |
|
|
327 |
|
| 328 |
|
| 329 |
/** |
| 330 |
* A chain-structured constraint-binding filter. Filters are chained in the |
| 331 |
* order in which they are parsed from the extension point. The head of the |
| 332 |
* filter chain applies its filter and, if it doesn't find any match, |
| 333 |
* delegates down the chain. The chain is terminated by the {@link #NULL} |
| 334 |
* filter, which always excludes the constraint. |
| 335 |
* |
| 336 |
* @author Christian W. Damus (cdamus) |
| 337 |
*/ |
| 338 |
private static class BindingFilter { |
| 339 |
|
| 340 |
private BindingFilter next; |
| 341 |
|
| 342 |
/** A filter that excludes every constraint. */ |
| 343 |
static final BindingFilter NULL = new BindingFilter() { |
| 344 |
|
| 345 |
boolean getBinding(IConstraintDescriptor constraint) { |
| 346 |
return false; |
| 347 |
} |
| 348 |
}; |
| 349 |
|
| 350 |
/** |
| 351 |
* Queries whether the specified constraint is definitely included ( |
| 352 |
* <code>true</code>) or excluded (<code>false</code>) from the client |
| 353 |
* context. If I don't have definitive knowledge of this constraint, I |
| 354 |
* delegate to the next in the chain. |
| 355 |
* |
| 356 |
* @param constraint |
| 357 |
* a constraint descriptor |
| 358 |
* @return whether the constraint is included |
| 359 |
*/ |
| 360 |
boolean getBinding(IConstraintDescriptor constraint) { |
| 361 |
return isExcluded(constraint) |
| 362 |
? false |
| 363 |
: isIncluded(constraint) |
| 364 |
? true |
| 365 |
: next().getBinding(constraint); |
| 366 |
} |
| 367 |
|
| 368 |
/** |
| 369 |
* Queries whether I know that a constraint is included. |
| 370 |
* |
| 371 |
* @param constraint |
| 372 |
* a constraint descriptor |
| 373 |
* @return <code>true</code> if the constraint is included, or |
| 374 |
* <code>false</code> if I do not know |
| 375 |
*/ |
| 376 |
boolean isExcluded(IConstraintDescriptor constraint) { |
| 377 |
return false; |
| 378 |
} |
| 379 |
|
| 380 |
/** |
| 381 |
* Queries whether I know that a constraint is excluded. |
| 382 |
* |
| 383 |
* @param constraint |
| 384 |
* a constraint descriptor |
| 385 |
* @return <code>true</code> if the constraint is excluded, or |
| 386 |
* <code>false</code> if I do not know |
| 387 |
*/ |
| 388 |
boolean isIncluded(IConstraintDescriptor constraint) { |
| 389 |
return false; |
| 390 |
} |
| 391 |
|
| 392 |
/** |
| 393 |
* Obtains the next filter in my chain. |
| 394 |
* |
| 395 |
* @return my next, or <code>null</code> if I am the end of the chain |
| 396 |
*/ |
| 397 |
BindingFilter next() { |
| 398 |
return next; |
| 399 |
} |
| 400 |
|
| 401 |
/** |
| 402 |
* Assigns my next filter. |
| 403 |
* |
| 404 |
* @param next |
| 405 |
* my new next |
| 406 |
*/ |
| 407 |
void setNext(BindingFilter next) { |
| 408 |
this.next = next; |
| 409 |
} |
| 410 |
|
| 411 |
/** |
| 412 |
* Obtains a filter, chaining me, that definitively includes the |
| 413 |
* specified category and all of its constraints and sub-categories. The |
| 414 |
* result may be optimized to be myself augmented with this category, if |
| 415 |
* I am a filter of the appropriate kind. Or, the result may be a new |
| 416 |
* filter chain. |
| 417 |
* |
| 418 |
* @param category |
| 419 |
* a category to include |
| 420 |
* |
| 421 |
* @return a filter that includes the category |
| 422 |
*/ |
| 423 |
BindingFilter includeCategory(String category) { |
| 424 |
CategoryInclusion result = new CategoryInclusion(category); |
| 425 |
result.setNext(this); |
| 426 |
return result; |
| 427 |
} |
| 428 |
|
| 429 |
/** |
| 430 |
* Obtains a filter, chaining me, that definitively excludes the |
| 431 |
* specified category and all of its constraints and sub-categories. The |
| 432 |
* result may be optimized to be myself augmented with this category, if |
| 433 |
* I am a filter of the appropriate kind. Or, the result may be a new |
| 434 |
* filter chain. |
| 435 |
* |
| 436 |
* @param category |
| 437 |
* a category to exclude |
| 438 |
* |
| 439 |
* @return a filter that excludes the category |
| 440 |
*/ |
| 441 |
BindingFilter excludeCategory(String category) { |
| 442 |
CategoryExclusion result = new CategoryExclusion(category); |
| 443 |
result.setNext(this); |
| 444 |
return result; |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
* Obtains a filter, chaining me, that definitively includes the |
| 449 |
* specified constraint. The result may be optimized to be myself |
| 450 |
* augmented with this constraint, if I am a filter of the appropriate |
| 451 |
* kind. Or, the result may be a new filter chain. |
| 452 |
* |
| 453 |
* @param constraint |
| 454 |
* a constraint to include |
| 455 |
* |
| 456 |
* @return a filter that includes the constraint |
| 457 |
*/ |
| 458 |
BindingFilter includeConstraint(String constraint) { |
| 459 |
ConstraintInclusion result = new ConstraintInclusion(constraint); |
| 460 |
result.setNext(this); |
| 461 |
return result; |
| 462 |
} |
| 463 |
|
| 464 |
/** |
| 465 |
* Obtains a filter, chaining me, that definitively excludes the |
| 466 |
* specified constraint. The result may be optimized to be myself |
| 467 |
* augmented with this constraint, if I am a filter of the appropriate |
| 468 |
* kind. Or, the result may be a new filter chain. |
| 469 |
* |
| 470 |
* @param constraint |
| 471 |
* a constraint to exclude |
| 472 |
* |
| 473 |
* @return a filter that excludes the constraint |
| 474 |
*/ |
| 475 |
BindingFilter excludeConstraint(String constraint) { |
| 476 |
ConstraintExclusion result = new ConstraintExclusion(constraint); |
| 477 |
result.setNext(this); |
| 478 |
return result; |
| 479 |
} |
| 480 |
|
| 481 |
/** |
| 482 |
* Obtains a filter, chaining me, that inherits the constraints bound to |
| 483 |
* the specified client-context. The result may be optimized to be |
| 484 |
* myself myself augmented with this client-context, if I am a filter of |
| 485 |
* the appropriate kind. Or, the result may be a new filter chain. |
| 486 |
* |
| 487 |
* @param clientContext |
| 488 |
* a client-context to extend |
| 489 |
* |
| 490 |
* @return a filter that extends the client-context |
| 491 |
*/ |
| 492 |
BindingFilter extendClientContext(String clientContext) { |
| 493 |
ContextExtension result = new ContextExtension(clientContext); |
| 494 |
result.setNext(this); |
| 495 |
return result; |
| 496 |
} |
| 497 |
} |
| 498 |
|
| 499 |
/** |
| 500 |
* A binding filter that definitively includes one or more constraints. |
| 501 |
* |
| 502 |
* @author Christian W. Damus (cdamus) |
| 503 |
*/ |
| 504 |
private static class ConstraintInclusion |
| 505 |
extends BindingFilter { |
| 506 |
|
| 507 |
private final Set<String> constraints = new java.util.HashSet<String>(); |
| 508 |
|
| 509 |
ConstraintInclusion(String constraint) { |
| 510 |
constraints.add(constraint); |
| 511 |
} |
| 512 |
|
| 513 |
@Override |
| 514 |
boolean isIncluded(IConstraintDescriptor constraint) { |
| 515 |
return constraints.contains(constraint.getId()); |
| 516 |
} |
| 517 |
|
| 518 |
@Override |
| 519 |
BindingFilter includeConstraint(String constraint) { |
| 520 |
constraints.add(constraint); |
| 521 |
return this; |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
/** |
| 526 |
* A binding filter that definitively excludes one or more constraints. |
| 527 |
* |
| 528 |
* @author Christian W. Damus (cdamus) |
| 529 |
*/ |
| 530 |
private static class ConstraintExclusion |
| 531 |
extends BindingFilter { |
| 532 |
|
| 533 |
private final Set<String> constraints = new java.util.HashSet<String>(); |
| 534 |
|
| 535 |
ConstraintExclusion(String constraint) { |
| 536 |
constraints.add(constraint); |
| 537 |
} |
| 538 |
|
| 539 |
@Override |
| 540 |
boolean isExcluded(IConstraintDescriptor constraint) { |
| 541 |
return constraints.contains(constraint.getId()); |
| 542 |
} |
| 543 |
|
| 544 |
@Override |
| 545 |
BindingFilter excludeConstraint(String constraint) { |
| 546 |
constraints.add(constraint); |
| 547 |
return this; |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
/** |
| 552 |
* A binding filter that definitively includes one or more categories. |
| 553 |
* |
| 554 |
* @author Christian W. Damus (cdamus) |
| 555 |
*/ |
| 556 |
private static class CategoryInclusion |
| 557 |
extends BindingFilter { |
| 558 |
|
| 559 |
private final CategorySet categories; |
| 560 |
|
| 561 |
CategoryInclusion(String category) { |
| 562 |
categories = new CategorySet(category); |
| 563 |
} |
| 564 |
|
| 565 |
@Override |
| 566 |
boolean isIncluded(IConstraintDescriptor constraint) { |
| 567 |
return categories.containsAny(constraint.getCategories()); |
| 568 |
} |
| 569 |
|
| 570 |
@Override |
| 571 |
BindingFilter includeCategory(String category) { |
| 572 |
categories.add(category); |
| 573 |
return this; |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
/** |
| 578 |
* A binding filter that definitively excludes one or more categories. |
| 579 |
* |
| 580 |
* @author Christian W. Damus (cdamus) |
| 581 |
*/ |
| 582 |
private static class CategoryExclusion |
| 583 |
extends BindingFilter { |
| 584 |
|
| 585 |
private final CategorySet categories; |
| 586 |
|
| 587 |
CategoryExclusion(String category) { |
| 588 |
categories = new CategorySet(category); |
| 589 |
} |
| 590 |
|
| 591 |
@Override |
| 592 |
boolean isExcluded(IConstraintDescriptor constraint) { |
| 593 |
return categories.containsAny(constraint.getCategories()); |
| 594 |
} |
| 595 |
|
| 596 |
@Override |
| 597 |
BindingFilter excludeCategory(String category) { |
| 598 |
categories.add(category); |
| 599 |
return this; |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
private static final class CategorySet { |
| 604 |
|
| 605 |
private final Set<String> categories = new java.util.HashSet<String>(); |
| 606 |
|
| 607 |
CategorySet(String category) { |
| 608 |
categories.add(category); |
| 609 |
} |
| 610 |
|
| 611 |
boolean containsAny(Collection<? extends Category> categories) { |
| 612 |
boolean result = false; |
| 613 |
|
| 614 |
for (Category next : categories) { |
| 615 |
if (contains(next)) { |
| 616 |
result = true; |
| 617 |
break; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
return result; |
| 622 |
} |
| 623 |
|
| 624 |
boolean contains(Category category) { |
| 625 |
boolean result = false; |
| 626 |
|
| 627 |
String path = category.getPath(); |
| 628 |
result = categories.contains(path); |
| 629 |
|
| 630 |
if (!result) { |
| 631 |
// search the ancestors |
| 632 |
Category ancestor = category.getParent(); |
| 633 |
|
| 634 |
while ((ancestor != null) && !result) { |
| 635 |
result = categories.contains(ancestor.getPath()); |
| 636 |
ancestor = ancestor.getParent(); |
| 637 |
} |
| 638 |
|
| 639 |
if (result) { |
| 640 |
// cache the original category for quicker results on the |
| 641 |
// next category that it contains |
| 642 |
add(path); |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
return result; |
| 647 |
} |
| 648 |
|
| 649 |
void add(String category) { |
| 650 |
categories.add(category); |
| 651 |
} |
| 652 |
} |
| 653 |
|
| 654 |
/** |
| 655 |
* A binding filter that inherits the bindings of another context. |
| 656 |
* |
| 657 |
* @author Christian W. Damus (cdamus) |
| 658 |
*/ |
| 659 |
private static class ContextExtension |
| 660 |
extends BindingFilter { |
| 661 |
|
| 662 |
private final Set<String> extendedContextIDs = new java.util.HashSet<String>(); |
| 663 |
|
| 664 |
private volatile Set<ClientContext> extendedContexts; |
| 665 |
|
| 666 |
ContextExtension(String clientContext) { |
| 667 |
extendedContextIDs.add(clientContext); |
| 668 |
} |
| 669 |
|
| 670 |
@Override |
| 671 |
boolean isIncluded(IConstraintDescriptor constraint) { |
| 672 |
if (extendedContexts == null) { |
| 673 |
// do this lazily because we don't know the order in which |
| 674 |
// client-contexts will be discovered in the extension registry |
| 675 |
ClientContextManager mgr = ClientContextManager.getInstance(); |
| 676 |
Set<ClientContext> contexts = new java.util.HashSet<ClientContext>(); |
| 677 |
|
| 678 |
synchronized (extendedContextIDs) { |
| 679 |
for (String next : extendedContextIDs) { |
| 680 |
contexts |
| 681 |
.add((ClientContext) mgr.getClientContext(next)); |
| 682 |
} |
| 683 |
|
| 684 |
extendedContexts = contexts; |
| 685 |
} |
| 686 |
} |
| 687 |
|
| 688 |
for (ClientContext extended : extendedContexts) { |
| 689 |
if (extended.includes(constraint)) { |
| 690 |
return true; |
| 691 |
} |
| 692 |
} |
| 693 |
|
| 694 |
return false; |
| 695 |
} |
| 696 |
|
| 697 |
@Override |
| 698 |
BindingFilter extendClientContext(String clientContext) { |
| 699 |
synchronized (extendedContextIDs) { |
| 700 |
extendedContexts = null; // purge |
| 701 |
extendedContextIDs.add(clientContext); |
| 702 |
} |
| 703 |
|
| 704 |
return this; |
| 705 |
} |
| 706 |
} |
| 335 |
} |
707 |
} |