|
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-255
Link Here
|
| 235 |
} |
237 |
} |
| 236 |
|
238 |
|
| 237 |
public boolean includes(IModelConstraint constraint) { |
239 |
public boolean includes(IModelConstraint constraint) { |
| 238 |
boolean result = false; |
240 |
Boolean result; |
| 239 |
|
241 |
|
| 240 |
IConstraintDescriptor descriptor = constraint.getDescriptor(); |
242 |
IConstraintDescriptor descriptor = constraint.getDescriptor(); |
| 241 |
|
243 |
|
| 242 |
if (descriptor != null) { |
244 |
if (descriptor == null) { |
| 243 |
result = constraintBindings.contains(descriptor.getId()); |
245 |
result = Boolean.FALSE; |
|
|
246 |
} else { |
| 247 |
result = constraintBindings.get(descriptor.getId()); |
| 244 |
|
248 |
|
| 245 |
if (!result && !categoryBindings.isEmpty()) { |
249 |
if (result == null) { |
| 246 |
// look for a bound category |
250 |
// cache the result for this constraint |
| 247 |
result = hasCategoryBinding(descriptor.getCategories()); |
251 |
result = filter.getBinding(descriptor); |
| 248 |
|
252 |
constraintBindings.put(descriptor.getId(), result); |
| 249 |
if (result) { |
|
|
| 250 |
// cache the result for this constraint |
| 251 |
bindConstraint(descriptor.getId()); |
| 252 |
} |
| 253 |
} |
253 |
} |
| 254 |
} |
254 |
} |
| 255 |
|
255 |
|
|
Lines 257-314
Link Here
|
| 257 |
} |
257 |
} |
| 258 |
|
258 |
|
| 259 |
/** |
259 |
/** |
| 260 |
* Determines whether any of the specified <code>categories</code> is bound |
260 |
* Adds a constraint inclusion binding to me. |
| 261 |
* to me. |
|
|
| 262 |
* |
261 |
* |
| 263 |
* @param categories a collection of categories (usually from a constraint) |
262 |
* @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 |
*/ |
263 |
*/ |
| 267 |
private boolean hasCategoryBinding(Collection<Category> categories) { |
264 |
void includeConstraint(String constraintId) { |
| 268 |
boolean result = false; |
265 |
filter = filter.includeConstraint(constraintId); |
| 269 |
|
266 |
} |
| 270 |
for (Iterator<Category> iter = categories.iterator(); !result && iter.hasNext();) { |
267 |
|
| 271 |
Category category = iter.next(); |
268 |
/** |
| 272 |
|
269 |
* Adds a constraint exclusion binding to me. |
| 273 |
result = categoryBindings.contains(category.getPath()); |
270 |
* |
| 274 |
|
271 |
* @param constraintId the ID of a constraint that is to be excluded from me |
| 275 |
if (!result) { |
272 |
*/ |
| 276 |
// search the ancestors |
273 |
void excludeConstraint(String constraintId) { |
| 277 |
Category ancestor = category.getParent(); |
274 |
filter = filter.excludeConstraint(constraintId); |
| 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 |
} |
275 |
} |
| 294 |
|
276 |
|
| 295 |
/** |
277 |
/** |
| 296 |
* Binds a constraint to me. |
278 |
* Adds a constraint category inclusion to me. |
| 297 |
* |
279 |
* |
| 298 |
* @param constraintId the ID of a constraint that is to be bound to me |
280 |
* @param categoryId the qualified ID (path) of a constraint category that |
|
|
281 |
* is to be included in me |
| 299 |
*/ |
282 |
*/ |
| 300 |
void bindConstraint(String constraintId) { |
283 |
void includeCategory(String categoryId) { |
| 301 |
constraintBindings.add(constraintId); |
284 |
filter = filter.includeCategory(categoryId); |
| 302 |
} |
285 |
} |
| 303 |
|
286 |
|
| 304 |
/** |
287 |
/** |
| 305 |
* Binds a constraint category to me. |
288 |
* Adds a constraint category exclusion to me. |
| 306 |
* |
289 |
* |
| 307 |
* @param categoryId the qualified ID (path) of a constraint category that |
290 |
* @param categoryId the qualified ID (path) of a constraint category that |
| 308 |
* is to be bound to me |
291 |
* is to be excluded from me |
| 309 |
*/ |
292 |
*/ |
| 310 |
void bindCategory(String categoryId) { |
293 |
void excludeCategory(String categoryId) { |
| 311 |
categoryBindings.add(categoryId); |
294 |
filter = filter.excludeCategory(categoryId); |
| 312 |
} |
295 |
} |
| 313 |
|
296 |
|
| 314 |
/** |
297 |
/** |
|
Lines 332-335
Link Here
|
| 332 |
public String toString() { |
315 |
public String toString() { |
| 333 |
return "ClientContext[" + getId() + ']'; //$NON-NLS-1$ |
316 |
return "ClientContext[" + getId() + ']'; //$NON-NLS-1$ |
| 334 |
} |
317 |
} |
|
|
318 |
|
| 319 |
|
| 320 |
/** |
| 321 |
* A chain-structured constraint-binding filter. Filters are chained in |
| 322 |
* the order in which they are parsed from the extension point. The head of |
| 323 |
* the filter chain applies its filter and, if it doesn't find any match, |
| 324 |
* delegates down the chain. The chain is terminated by the {@link #NULL} |
| 325 |
* filter, which always excludes the constraint. |
| 326 |
* |
| 327 |
* @author Christian W. Damus (cdamus) |
| 328 |
*/ |
| 329 |
private static class BindingFilter { |
| 330 |
private BindingFilter next; |
| 331 |
|
| 332 |
/** A filter that excludes every constraint. */ |
| 333 |
static final BindingFilter NULL = new BindingFilter() { |
| 334 |
boolean getBinding(IConstraintDescriptor constraint) {return false;} |
| 335 |
}; |
| 336 |
|
| 337 |
/** |
| 338 |
* Queries whether the specified constraint is definitely included |
| 339 |
* (<code>true</code>) or excluded (<code>false</code>) from the client |
| 340 |
* context. If I don't have definitive knowledge of this constraint, |
| 341 |
* I delegate to the next in the chain. |
| 342 |
* |
| 343 |
* @param constraint a constraint descriptor |
| 344 |
* @return whether the constraint is included |
| 345 |
*/ |
| 346 |
boolean getBinding(IConstraintDescriptor constraint) { |
| 347 |
return isExcluded(constraint) |
| 348 |
? false |
| 349 |
: isIncluded(constraint) |
| 350 |
? true |
| 351 |
: next().getBinding(constraint); |
| 352 |
} |
| 353 |
|
| 354 |
/** |
| 355 |
* Queries whether I know that a constraint is included. |
| 356 |
* |
| 357 |
* @param constraint a constraint descriptor |
| 358 |
* @return <code>true</code> if the constraint is included, or |
| 359 |
* <code>false</code> if I do not know |
| 360 |
*/ |
| 361 |
boolean isExcluded(IConstraintDescriptor constraint) { |
| 362 |
return false; |
| 363 |
} |
| 364 |
|
| 365 |
/** |
| 366 |
* Queries whether I know that a constraint is excluded. |
| 367 |
* |
| 368 |
* @param constraint a constraint descriptor |
| 369 |
* @return <code>true</code> if the constraint is excluded, or |
| 370 |
* <code>false</code> if I do not know |
| 371 |
*/ |
| 372 |
boolean isIncluded(IConstraintDescriptor constraint) { |
| 373 |
return false; |
| 374 |
} |
| 375 |
|
| 376 |
/** |
| 377 |
* Obtains the next filter in my chain. |
| 378 |
* |
| 379 |
* @return my next, or <code>null</code> if I am the end of the chain |
| 380 |
*/ |
| 381 |
BindingFilter next() { |
| 382 |
return next; |
| 383 |
} |
| 384 |
|
| 385 |
/** |
| 386 |
* Assigns my next filter. |
| 387 |
* |
| 388 |
* @param next my new next |
| 389 |
*/ |
| 390 |
void setNext(BindingFilter next) { |
| 391 |
this.next = next; |
| 392 |
} |
| 393 |
|
| 394 |
/** |
| 395 |
* Obtains a filter, chaining me, that definitively includes the |
| 396 |
* specified category and all of its constraints and sub-categories. The |
| 397 |
* result may be optimized to be myself augmented with this category, if |
| 398 |
* I am a filter of the appropriate kind. Or, the result may be a new |
| 399 |
* filter chain. |
| 400 |
* |
| 401 |
* @param category |
| 402 |
* a category to include |
| 403 |
* |
| 404 |
* @return a filter that includes the category |
| 405 |
*/ |
| 406 |
BindingFilter includeCategory(String category) { |
| 407 |
CategoryInclusion result = new CategoryInclusion(category); |
| 408 |
result.setNext(this); |
| 409 |
return result; |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Obtains a filter, chaining me, that definitively excludes the |
| 414 |
* specified category and all of its constraints and sub-categories. The |
| 415 |
* result may be optimized to be myself augmented with this category, if |
| 416 |
* I am a filter of the appropriate kind. Or, the result may be a new |
| 417 |
* filter chain. |
| 418 |
* |
| 419 |
* @param category |
| 420 |
* a category to exclude |
| 421 |
* |
| 422 |
* @return a filter that excludes the category |
| 423 |
*/ |
| 424 |
BindingFilter excludeCategory(String category) { |
| 425 |
CategoryExclusion result = new CategoryExclusion(category); |
| 426 |
result.setNext(this); |
| 427 |
return result; |
| 428 |
} |
| 429 |
|
| 430 |
/** |
| 431 |
* Obtains a filter, chaining me, that definitively includes the |
| 432 |
* specified constraint. The result may be optimized to be myself |
| 433 |
* augmented with this category, if I am a filter of the appropriate |
| 434 |
* kind. Or, the result may be a new filter chain. |
| 435 |
* |
| 436 |
* @param constraint |
| 437 |
* a constraint to include |
| 438 |
* |
| 439 |
* @return a filter that includes the constraint |
| 440 |
*/ |
| 441 |
BindingFilter includeConstraint(String constraint) { |
| 442 |
ConstraintInclusion result = new ConstraintInclusion(constraint); |
| 443 |
result.setNext(this); |
| 444 |
return result; |
| 445 |
} |
| 446 |
|
| 447 |
/** |
| 448 |
* Obtains a filter, chaining me, that definitively excludes the |
| 449 |
* specified constraint. The result may be optimized to be myself |
| 450 |
* augmented with this category, 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 exclude |
| 455 |
* |
| 456 |
* @return a filter that excludes the constraint |
| 457 |
*/ |
| 458 |
BindingFilter excludeConstraint(String constraint) { |
| 459 |
ConstraintExclusion result = new ConstraintExclusion(constraint); |
| 460 |
result.setNext(this); |
| 461 |
return result; |
| 462 |
} |
| 463 |
} |
| 464 |
|
| 465 |
/** |
| 466 |
* A binding filter that definitively includes one or more constraints. |
| 467 |
* |
| 468 |
* @author Christian W. Damus (cdamus) |
| 469 |
*/ |
| 470 |
private static class ConstraintInclusion extends BindingFilter { |
| 471 |
private final Set<String> constraints = new java.util.HashSet<String>(); |
| 472 |
|
| 473 |
ConstraintInclusion(String constraint) { |
| 474 |
constraints.add(constraint); |
| 475 |
} |
| 476 |
|
| 477 |
@Override |
| 478 |
boolean isIncluded(IConstraintDescriptor constraint) { |
| 479 |
return constraints.contains(constraint.getId()); |
| 480 |
} |
| 481 |
|
| 482 |
@Override |
| 483 |
BindingFilter includeConstraint(String constraint) { |
| 484 |
constraints.add(constraint); |
| 485 |
return this; |
| 486 |
} |
| 487 |
} |
| 488 |
|
| 489 |
/** |
| 490 |
* A binding filter that definitively excludes one or more constraints. |
| 491 |
* |
| 492 |
* @author Christian W. Damus (cdamus) |
| 493 |
*/ |
| 494 |
private static class ConstraintExclusion extends BindingFilter { |
| 495 |
private final Set<String> constraints = new java.util.HashSet<String>(); |
| 496 |
|
| 497 |
ConstraintExclusion(String constraint) { |
| 498 |
constraints.add(constraint); |
| 499 |
} |
| 500 |
|
| 501 |
@Override |
| 502 |
boolean isExcluded(IConstraintDescriptor constraint) { |
| 503 |
return constraints.contains(constraint.getId()); |
| 504 |
} |
| 505 |
|
| 506 |
@Override |
| 507 |
BindingFilter excludeConstraint(String constraint) { |
| 508 |
constraints.add(constraint); |
| 509 |
return this; |
| 510 |
} |
| 511 |
} |
| 512 |
|
| 513 |
/** |
| 514 |
* A binding filter that definitively includes one or more categories. |
| 515 |
* |
| 516 |
* @author Christian W. Damus (cdamus) |
| 517 |
*/ |
| 518 |
private static class CategoryInclusion extends BindingFilter { |
| 519 |
private final CategorySet categories; |
| 520 |
|
| 521 |
CategoryInclusion(String category) { |
| 522 |
categories = new CategorySet(category); |
| 523 |
} |
| 524 |
|
| 525 |
@Override |
| 526 |
boolean isIncluded(IConstraintDescriptor constraint) { |
| 527 |
return categories.containsAny(constraint.getCategories()); |
| 528 |
} |
| 529 |
|
| 530 |
@Override |
| 531 |
BindingFilter includeCategory(String category) { |
| 532 |
categories.add(category); |
| 533 |
return this; |
| 534 |
} |
| 535 |
} |
| 536 |
|
| 537 |
/** |
| 538 |
* A binding filter that definitively excludes one or more categories. |
| 539 |
* |
| 540 |
* @author Christian W. Damus (cdamus) |
| 541 |
*/ |
| 542 |
private static class CategoryExclusion extends BindingFilter { |
| 543 |
private final CategorySet categories; |
| 544 |
|
| 545 |
CategoryExclusion(String category) { |
| 546 |
categories = new CategorySet(category); |
| 547 |
} |
| 548 |
|
| 549 |
@Override |
| 550 |
boolean isExcluded(IConstraintDescriptor constraint) { |
| 551 |
return categories.containsAny(constraint.getCategories()); |
| 552 |
} |
| 553 |
|
| 554 |
@Override |
| 555 |
BindingFilter excludeCategory(String category) { |
| 556 |
categories.add(category); |
| 557 |
return this; |
| 558 |
} |
| 559 |
} |
| 560 |
|
| 561 |
private static final class CategorySet { |
| 562 |
private final Set<String> categories = new java.util.HashSet<String>(); |
| 563 |
|
| 564 |
CategorySet(String category) { |
| 565 |
categories.add(category); |
| 566 |
} |
| 567 |
|
| 568 |
boolean containsAny(Collection<? extends Category> categories) { |
| 569 |
boolean result = false; |
| 570 |
|
| 571 |
for (Category next : categories) { |
| 572 |
if (contains(next)) { |
| 573 |
result = true; |
| 574 |
break; |
| 575 |
} |
| 576 |
} |
| 577 |
|
| 578 |
return result; |
| 579 |
} |
| 580 |
|
| 581 |
boolean contains(Category category) { |
| 582 |
boolean result = false; |
| 583 |
|
| 584 |
String path = category.getPath(); |
| 585 |
result = categories.contains(path); |
| 586 |
|
| 587 |
if (!result) { |
| 588 |
// search the ancestors |
| 589 |
Category ancestor = category.getParent(); |
| 590 |
|
| 591 |
while ((ancestor != null) && !result) { |
| 592 |
result = categories.contains(ancestor.getPath()); |
| 593 |
ancestor = ancestor.getParent(); |
| 594 |
} |
| 595 |
|
| 596 |
if (result) { |
| 597 |
// cache the original category for quicker results on the |
| 598 |
// next category that it contains |
| 599 |
add(path); |
| 600 |
} |
| 601 |
} |
| 602 |
|
| 603 |
return result; |
| 604 |
} |
| 605 |
|
| 606 |
void add(String category) { |
| 607 |
categories.add(category); |
| 608 |
} |
| 609 |
} |
| 335 |
} |
610 |
} |