Correct the order of processing post processors.
[EMFCompare2.git] / plugins / org.eclipse.emf.compare.rcp / src / org / eclipse / emf / compare / rcp / internal / postprocessor / PostProcessorRegistryImpl.java
blobd8b956def712bcfd4c0502469a0d9cd3ed8f0a7b
1 /*******************************************************************************
2 * Copyright (c) 2014 Obeo.
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * http://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors:
9 * Obeo - initial API and implementation
10 *******************************************************************************/
11 package org.eclipse.emf.compare.rcp.internal.postprocessor;
13 import static com.google.common.base.Predicates.in;
14 import static com.google.common.base.Predicates.not;
16 import com.google.common.collect.Collections2;
17 import com.google.common.collect.ImmutableList;
18 import com.google.common.collect.Lists;
20 import java.util.Collection;
21 import java.util.Collections;
22 import java.util.List;
23 import java.util.regex.Pattern;
25 import org.eclipse.emf.compare.postprocessor.IPostProcessor;
26 import org.eclipse.emf.compare.postprocessor.IPostProcessor.Descriptor;
27 import org.eclipse.emf.compare.rcp.EMFCompareRCPPlugin;
28 import org.eclipse.emf.compare.rcp.internal.extension.IItemDescriptor;
29 import org.eclipse.emf.compare.rcp.internal.extension.IItemRegistry;
30 import org.eclipse.emf.compare.rcp.internal.extension.impl.AbstractItemDescriptor;
31 import org.eclipse.emf.compare.rcp.internal.extension.impl.ItemUtil;
32 import org.eclipse.emf.compare.rcp.internal.extension.impl.WrapperItemDescriptor;
33 import org.eclipse.emf.compare.rcp.internal.preferences.EMFComparePreferences;
34 import org.eclipse.emf.compare.scope.IComparisonScope;
36 /**
37 * IPostProcessor.Descriptor.Registry implementation based on wrapping a {@link IItemRegistry}.
39 * @see IItemRegistry
40 * @author <a href="mailto:arthur.daussy@obeo.fr">Arthur Daussy</a>
42 public class PostProcessorRegistryImpl implements IPostProcessor.Descriptor.Registry<String> {
44 /** EMPTY_STRING. */
45 private static final String EMPTY_STRING = ""; //$NON-NLS-1$
47 /** {@link IItemRegistry} of descriptor of {@link IItemDescriptor}. */
48 private final IItemRegistry<IPostProcessor.Descriptor> baseRegisty;
50 /**
51 * Constructor.
53 * @param baseRegisty
54 * {@link IItemDescriptor} filled with {@link IItemDescriptor} of
55 * {@link IPostProcessor.Descriptor}.
57 public PostProcessorRegistryImpl(IItemRegistry<IPostProcessor.Descriptor> baseRegisty) {
58 super();
59 this.baseRegisty = baseRegisty;
62 /**
63 * {@inheritDoc}
65 public IPostProcessor.Descriptor put(String key, IPostProcessor.Descriptor descriptor) {
66 WrapperItemDescriptor<IPostProcessor.Descriptor> newDescriptor = new WrapperItemDescriptor<IPostProcessor.Descriptor>(
67 EMPTY_STRING, EMPTY_STRING, descriptor.getOrdinal(), descriptor.getInstanceClassName(),
68 descriptor);
69 IItemDescriptor<IPostProcessor.Descriptor> oldDescriptor = baseRegisty.add(newDescriptor);
70 if (oldDescriptor != null) {
71 return oldDescriptor.getItem();
73 return null;
76 /**
77 * {@inheritDoc}
79 public void clear() {
80 baseRegisty.clear();
84 /**
85 * {@inheritDoc}
87 public List<IPostProcessor.Descriptor> getDescriptors() {
88 List<IItemDescriptor<Descriptor>> itemDescriptors = baseRegisty.getItemDescriptors();
89 Collections.sort(itemDescriptors);
91 Collection<IItemDescriptor<IPostProcessor.Descriptor>> activeDescriptor = Collections2.filter(
92 itemDescriptors, not(in(getDisabledEngines())));
94 Collection<IPostProcessor.Descriptor> descriptors = Collections2.transform(activeDescriptor,
95 AbstractItemDescriptor.<IPostProcessor.Descriptor> getItemFunction());
96 return Lists.newArrayList(descriptors);
99 /**
100 * Return a collection of disabled {@link IItemDescriptor<IPostProcessor.Descriptor>}.
102 * @return Collection<IItemDescriptor<IPostProcessor.Descriptor>>
104 private Collection<IItemDescriptor<IPostProcessor.Descriptor>> getDisabledEngines() {
105 List<IItemDescriptor<IPostProcessor.Descriptor>> result = ItemUtil.getItemsDescriptor(baseRegisty,
106 EMFComparePreferences.DISABLED_POST_PROCESSOR, EMFCompareRCPPlugin.getDefault()
107 .getEMFComparePreferences());
108 if (result == null) {
109 result = Collections.emptyList();
111 return result;
115 * {@inheritDoc}
117 public Descriptor remove(String key) {
118 IItemDescriptor<IPostProcessor.Descriptor> oldDescriptor = baseRegisty.remove(key);
119 if (oldDescriptor != null) {
120 return oldDescriptor.getItem();
122 return null;
126 * {@inheritDoc}
128 * @see org.eclipse.emf.compare.postprocessor.IPostProcessor.Descriptor.Registry#getPostProcessors(org.eclipse.emf.compare.scope.IComparisonScope)
130 public List<IPostProcessor> getPostProcessors(IComparisonScope scope) {
131 final ImmutableList.Builder<IPostProcessor> processors = ImmutableList.builder();
132 for (IPostProcessor.Descriptor factory : getDescriptors()) {
133 Pattern nsURIPattern = factory.getNsURI();
134 if (nsURIPattern != null) {
135 for (String nsURI : scope.getNsURIs()) {
136 if (nsURIPattern.matcher(nsURI).matches()) {
137 processors.add(factory.getPostProcessor());
138 break;
142 // Should probably use two loops here to prioritize NsURI matching
143 Pattern resourceURIPattern = factory.getResourceURI();
144 if (resourceURIPattern != null) {
145 for (String resourceURI : scope.getResourceURIs()) {
146 if (resourceURIPattern.matcher(resourceURI).matches()) {
147 processors.add(factory.getPostProcessor());
148 break;
153 return processors.build();