Search in sources :

Example 1 with MergeEngine

use of org.walkmod.merger.MergeEngine in project walkmod-core by walkmod.

the class DefaultConfigurationAdapter method prepare.

@Override
public void prepare() {
    Collection<ProviderConfig> providers = config.getProviderConfigurations();
    if (providers != null) {
        for (ProviderConfig pc : providers) {
            Object aux = config.getBean(pc.getType(), pc.getParameters());
            if (aux instanceof ConfigurationProvider) {
                ConfigurationProvider cp = ((ConfigurationProvider) aux);
                cp.init(config);
                cp.load();
            }
        }
    }
    Collection<MergePolicyConfig> mergePolicies = config.getMergePolicies();
    if (mergePolicies != null) {
        Map<String, MergeEngine> mergeEngines = new HashMap<String, MergeEngine>();
        config.setMergeEngines(mergeEngines);
        for (MergePolicyConfig mpc : mergePolicies) {
            MergeEngine me = new MergeEngine();
            mergeEngines.put(mpc.getName(), me);
            String dopTypeLabel = mpc.getDefaultObjectPolicy();
            Object dop = null;
            Object top = null;
            if (dopTypeLabel != null) {
                dop = config.getBean(dopTypeLabel, null);
            }
            if (dop != null && dop instanceof MergePolicy<?>) {
                me.setDefaultObjectMergePolicy((MergePolicy) dop);
            }
            String topTypeLabel = mpc.getDefaultTypePolicy();
            if (topTypeLabel != null) {
                top = config.getBean(topTypeLabel, null);
            }
            if ((top != null) && top instanceof MergePolicy<?>) {
                me.setDefaultTypeMergePolicy((MergePolicy) top);
            }
            Map<String, String> policyEntries = mpc.getPolicyEntries();
            Class<?> oType = null;
            Object pType = null;
            Map<Class<?>, MergePolicy> resolvedEntries = new HashMap<Class<?>, MergePolicy>();
            if (policyEntries != null && !policyEntries.isEmpty()) {
                for (Map.Entry<String, String> entry : policyEntries.entrySet()) {
                    try {
                        oType = config.getClassLoader().loadClass(entry.getKey());
                    } catch (ClassNotFoundException e) {
                        throw new WalkModException("Invalid policy entry for " + entry.getKey());
                    }
                    pType = config.getBean(entry.getValue(), null);
                    if (pType instanceof MergePolicy) {
                        resolvedEntries.put(oType, (MergePolicy) pType);
                    }
                }
            }
            me.setPolicyConfiguration(resolvedEntries);
        }
    }
}
Also used : MergePolicyConfig(org.walkmod.conf.entities.MergePolicyConfig) WalkModException(org.walkmod.exceptions.WalkModException) HashMap(java.util.HashMap) ProviderConfig(org.walkmod.conf.entities.ProviderConfig) ConfigurationProvider(org.walkmod.conf.ConfigurationProvider) MergeEngine(org.walkmod.merger.MergeEngine) MergePolicy(org.walkmod.merger.MergePolicy) Map(java.util.Map) HashMap(java.util.HashMap)

Example 2 with MergeEngine

use of org.walkmod.merger.MergeEngine in project walkmod-core by walkmod.

the class AbstractWalker method visit.

protected void visit(Object element, List<Object> visitors, List<TransformationConfig> transformations, VisitorContext context) throws Exception {
    if (rootNamespace != null && !"".equals(rootNamespace)) {
        String qualifiedName = getResource().getNearestNamespace(element, NAMESPACE_SEPARATOR);
        if (rootNamespace.startsWith(qualifiedName)) {
            return;
        }
    }
    if (visitors.isEmpty()) {
        context.addResultNode(element);
    } else {
        int index = 0;
        for (Object visitor : visitors) {
            boolean isMergeable = transformations.get(index).isMergeable();
            String mergePolicy = transformations.get(index).getMergePolicy();
            if (mergePolicy != null) {
                isMergeable = true;
            }
            if (isMergeable && mergePolicy == null) {
                mergePolicy = "default";
            }
            Method[] methods = visitor.getClass().getMethods();
            List<Object> restVisitors = visitors.subList(index + 1, visitors.size());
            List<TransformationConfig> restTransformations = transformations.subList(index + 1, transformations.size());
            Set<String> visitedTypes = new HashSet<String>();
            for (int j = 0; j < methods.length; j++) {
                if (methods[j].getName().equals("visit")) {
                    Class<?> type = methods[j].getParameterTypes()[0];
                    if ((!visitedTypes.contains(element.getClass().getName())) && type.isInstance(element)) {
                        visitedTypes.add(type.getName());
                        int paramsLength = methods[j].getParameterTypes().length;
                        Object[] params = new Object[paramsLength];
                        params[0] = element;
                        VisitorContext args = new VisitorContext(getChainConfig());
                        args.putAll(context);
                        if (paramsLength == 2) {
                            params[1] = args;
                        }
                        methods[j].invoke(visitor, params);
                        context.getVisitorMessages().addAll(args.getVisitorMessages());
                        MergeEngine me = null;
                        if (isMergeable) {
                            me = chainConfig.getConfiguration().getMergeEngine(mergePolicy);
                        }
                        if (args.hasResultNodes()) {
                            Iterator<Object> it = args.getResultNodes().iterator();
                            while (it.hasNext()) {
                                Object currentArg = it.next();
                                if (isMergeable) {
                                    currentArg = merge(currentArg, me, context);
                                }
                                context.addResultNode(currentArg);
                                visit(currentArg, restVisitors, restTransformations, context);
                            }
                            return;
                        } else {
                            context.addResultNode(element);
                        }
                    }
                }
            }
            index++;
        }
    }
}
Also used : TransformationConfig(org.walkmod.conf.entities.TransformationConfig) Method(java.lang.reflect.Method) MergeEngine(org.walkmod.merger.MergeEngine) HashSet(java.util.HashSet)

Aggregations

MergeEngine (org.walkmod.merger.MergeEngine)2 Method (java.lang.reflect.Method)1 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 Map (java.util.Map)1 ConfigurationProvider (org.walkmod.conf.ConfigurationProvider)1 MergePolicyConfig (org.walkmod.conf.entities.MergePolicyConfig)1 ProviderConfig (org.walkmod.conf.entities.ProviderConfig)1 TransformationConfig (org.walkmod.conf.entities.TransformationConfig)1 WalkModException (org.walkmod.exceptions.WalkModException)1 MergePolicy (org.walkmod.merger.MergePolicy)1