source: ComponentRegistry/trunk/ComponentRegistry/src/main/java/clarin/cmdi/componentregistry/CMDComponentSpecExpander.java @ 4550

Last change on this file since 4550 was 4550, checked in by twagoo, 10 years ago

Fixed errors due to schema change allowing only one root element in a profile or component (see r4525)

File size: 5.1 KB
Line 
1package clarin.cmdi.componentregistry;
2
3import clarin.cmdi.componentregistry.components.CMDComponentSpec;
4import clarin.cmdi.componentregistry.components.CMDComponentType;
5import java.util.ArrayList;
6import java.util.Collection;
7import java.util.Collections;
8import java.util.HashSet;
9import java.util.List;
10import org.slf4j.Logger;
11import org.slf4j.LoggerFactory;
12
13/**
14 *
15 * @author Twan Goosen <twan.goosen@mpi.nl>
16 */
17public abstract class CMDComponentSpecExpander {
18   
19    private final static Logger LOG = LoggerFactory.getLogger(CMDComponentSpecExpander.class);
20    protected final ComponentRegistry registry;
21   
22    public CMDComponentSpecExpander(ComponentRegistry registry) {
23        this.registry = registry;
24    }
25   
26    public void expandNestedComponent(List<CMDComponentType> cmdComponents, String id) throws ComponentRegistryException {
27        expandNestedComponent(cmdComponents, new HashSet<String>(Collections.singleton(id)));
28    }
29   
30    private void expandNestedComponent(List<CMDComponentType> cmdComponents, Collection<String> path) throws ComponentRegistryException {
31        List<CMDComponentType> expanded = new ArrayList<CMDComponentType>();
32        for (CMDComponentType cmdComponentType : cmdComponents) {
33            String componentId = cmdComponentType.getComponentId();
34            if (componentId != null) {
35                if (LOG.isDebugEnabled()) {
36                    LOG.debug("[Level {}] Expanding {}", path.size(), componentId);
37                }
38                if (path.contains(componentId)) {
39                    throw new ComponentRegistryException("Detected recursion in component specification: " + path.toString() + " already contains " + componentId);
40                } else {
41                    Collection<String> newPath = new HashSet<String>(path);
42                    newPath.add(componentId);
43                    // Use uncached components and profiles, because we expand and thus change them this change should not be in the cache.
44                    CMDComponentSpec spec = getUncachedComponent(componentId);
45                    if (spec != null) {
46                        CMDComponentType nested = spec.getCMDComponent();
47                        expandNestedComponent(nested.getCMDComponent(), newPath);
48                        overwriteAttributes(cmdComponentType, nested);
49                        expanded.add(nested);
50                    } else {
51                        // Spec could not be resolved
52                        LOG.warn("Could not resolve referenced component with id " + componentId);
53                        // Add spec itself, without futher expanding
54                        expanded.add(cmdComponentType);
55                    }
56                }
57            } else {
58                // No id = embedded component
59                expandNestedComponent(cmdComponentType.getCMDComponent(), path);
60                expanded.add(cmdComponentType);//no attributes overwritten
61            }
62        }
63        cmdComponents.clear();
64        cmdComponents.addAll(expanded);
65    }
66
67    /**
68     * Copying the cardinality specified in the referenceDeclaration over the values in the actual component.
69     */
70    private void overwriteAttributes(CMDComponentType referenceDeclaration, CMDComponentType nested) {
71        if (!referenceDeclaration.getCardinalityMax().isEmpty()) {
72            List<String> cardinalityMax = nested.getCardinalityMax();
73            cardinalityMax.clear();
74            cardinalityMax.addAll(referenceDeclaration.getCardinalityMax());
75        }
76        if (!referenceDeclaration.getCardinalityMin().isEmpty()) {
77            List<String> cardinalityMin = nested.getCardinalityMin();
78            cardinalityMin.clear();
79            cardinalityMin.addAll(referenceDeclaration.getCardinalityMin());
80        }
81        nested.setComponentId(referenceDeclaration.getComponentId()); // Setting componentId for better xsd generation.
82    }
83   
84    protected CMDComponentSpec expandComponent(String componentId) throws ComponentRegistryException {
85        // Use uncached components and profiles, because we expand and thus change them this change should not be in the cache.
86        CMDComponentSpec result = getUncachedComponent(componentId);//registry.getUncachedComponent(componentId);
87        CMDComponentType cmdComponentType = result.getCMDComponent();
88        expandNestedComponent(cmdComponentType.getCMDComponent(), componentId);
89        return result;
90    }
91   
92    protected CMDComponentSpec expandProfile(String profileId) throws ComponentRegistryException {
93        // Use uncached components and profiles, because we expand and thus change them this change should not be in the cache.
94        CMDComponentSpec result = getUncachedProfile(profileId);//registry.getUncachedProfile(profileId);
95        CMDComponentType cmdComponent = result.getCMDComponent();
96        expandNestedComponent(Collections.singletonList(cmdComponent), profileId);
97        return result;
98    }
99
100//    protected CMDComponentSpec expandComment(String commentId) throws ComponentRegistryException {
101//        CMDComponentSpec result = getUncachedComment(commentId);
102//        List<CMDComponentType> cmdComponents = result.getCMDComponent();
103//        expandNestedComponent(cmdComponents);
104//        return result;
105//    }
106    /**
107     * Get uncached component from "this" registry and possibly from public registry. Note: "this" registry can be a user registry.
108     *
109     * @param componentId
110     */
111    protected abstract CMDComponentSpec getUncachedComponent(String componentId) throws ComponentRegistryException;
112   
113    protected abstract CMDComponentSpec getUncachedProfile(String profileId) throws ComponentRegistryException;
114//    protected abstract CMDComponentSpec getUncachedComment(String commentId) throws ComponentRegistryException;
115}
Note: See TracBrowser for help on using the repository browser.