KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > kilim > description > TemplateDescription


1 /**
2  * Copyright (C) 2002 Kelua SA
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the Free Software
16  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17  */

18 package org.objectweb.kilim.description;
19 import java.util.Collection;
20 import java.util.HashSet;
21 import java.util.LinkedHashMap;
22 import java.util.Set;
23 import java.util.Iterator;
24 import java.util.List;
25 import java.util.ArrayList;
26
27 import org.objectweb.kilim.InternalException;
28 import org.objectweb.kilim.KilimException;
29 import org.objectweb.kilim.repository.ResourceLoader;
30
31 /**
32  * @author horn
33  * This is the class for Kilim templates.
34  */

35
36 public class TemplateDescription {
37
38     private static final Object LOCK = new Object();
39     
40     private static final int PROVIDER_REFERENCE = 1;
41     private static final int TRANSFORMER_REFERENCE = 2;
42
43     private static final int PROVIDER_KEY = 1;
44     private static final int TRANSFORMER_KEY = 2;
45     private static final int PROPERTY_KEY = 3;
46     private static final int INSTANCE_KEY = 4;
47     private static final int PORT_KEY = 5;
48     private static final int TRIGGER_KEY = 6;
49     private static final int BINDING_KEY = 7;
50     private static final int SLOT_KEY = 8;
51     private static final int PLUG_KEY = 9;
52     private static final int ANY_PROVIDER_KEY = 10;
53     
54     private static final int LEGAL_OVERRIDE = 1;
55     private static final int ILLEGAL_OVERRIDE = 2;
56     private static final int NAME_CLASH = 3;
57     private static final int NO_OVERRIDE = 4;
58     
59     private String name;
60     private TemplateDescription superTemplate;
61     
62     private LinkedHashMap transformers;
63     private LinkedHashMap instances;
64     private LinkedHashMap slots;
65     private LinkedHashMap references;
66     private LinkedHashMap allProviders;
67     
68     private Set subTemplates;
69     private List triggers;
70     private List plugs;
71     private List bindings;
72     
73     private ResourceLoader resourceLoader;
74     
75     private class ReferenceData {
76         int referenceKind;
77         Object reference;
78         
79         ReferenceData(int aKind, Object aReference) {
80             referenceKind = aKind;
81             reference = aReference;
82         }
83     }
84     
85     private class ProviderData {
86         int referenceKind;
87         NamedElement provider;
88         
89         ProviderData(int aKind, NamedElement aProvider) {
90             referenceKind = aKind;
91             provider = aProvider;
92         }
93     }
94         
95     /**
96      * A public constructor to be used to build a template extending an existing template
97      * @param aName : the name of the template (which must be non null)
98      * @param aSuperTemplate : the reference of the super template (xhich can be null)
99      * @throws KilimException : thrown when name is null.
100      */

101     public TemplateDescription(String aName, TemplateDescription aSuperTemplate) throws KilimException {
102         if (aName == null) {
103             if (superTemplate != null) {
104                 throw new KilimException("attempt to create a sub-template with a null name in template " + aSuperTemplate);
105             } else {
106                 throw new KilimException("attempt to create a sub-template with a null name ");
107             }
108         }
109         name = aName;
110         
111         superTemplate = aSuperTemplate;
112         if (superTemplate != null) {
113             superTemplate.addSubTemplate(this);
114         }
115     }
116     
117     /**
118      * To be used to build a top level template (i.e. a template without super template).
119      * @param aName : the name of the template (which must be non null)
120      * @throws KilimException : thrown when name is null
121      */

122     public TemplateDescription(String aName) throws KilimException {
123         this(aName, null);
124     }
125     
126     /**
127      * @see org.objectweb.kilim.description.Template#getName()
128      */

129     public String getName() {
130         return name;
131     }
132     
133     /**
134      * @see org.objectweb.kilim.description.Template#setName(String)
135      */

136     public void setName(String aName) throws KilimException {
137         if (aName == null) {
138             throw new KilimException("illegal null name in setName of TemplateImpl");
139         }
140         name = aName;
141     }
142         
143     /**
144      * @see org.objectweb.kilim.description.Template#getSuperTemplate()
145      */

146     public TemplateDescription getSuperTemplate() {
147         return superTemplate;
148     }
149
150     /**
151      * @see org.objectweb.kilim.description.Template#setSuperTemplate(Template)
152      */

153     public void setSuperTemplate(TemplateDescription aTemplate) throws KilimException {
154         if (superTemplate != null) {
155             superTemplate.removeSubTemplate(this);
156         }
157         superTemplate = aTemplate;
158         if (superTemplate != null) {
159             superTemplate.addSubTemplate(this);
160         }
161     }
162     
163     /**
164      * @see org.objectweb.kilim.description.Template#addSubTemplate(Template)
165      */

166     public void addSubTemplate(TemplateDescription aTemplate) throws KilimException {
167         if (aTemplate == null) {
168             throw new KilimException("null template in addSubTemplate");
169         }
170         if (subTemplates == null) {
171             subTemplates = new HashSet();
172         }
173         subTemplates.add(aTemplate);
174         
175         Iterator iter = getPorts(false);
176         while (iter.hasNext()) {
177             NamedElement element = (NamedElement) iter.next();
178             addSubTemplate1(element, PORT_KEY, aTemplate);
179         }
180         iter = getProperties(false);
181         while (iter.hasNext()) {
182             NamedElement element = (NamedElement) iter.next();
183             addSubTemplate1(element, PROPERTY_KEY, aTemplate);
184         }
185         iter = getProviders(false);
186         while (iter.hasNext()) {
187             NamedElement element = (NamedElement) iter.next();
188             addSubTemplate1(element, PROVIDER_KEY, aTemplate);
189         }
190         iter = getTransformers(false);
191         while (iter.hasNext()) {
192             NamedElement element = (NamedElement) iter.next();
193             addSubTemplate1(element, TRANSFORMER_KEY, aTemplate);
194         }
195     }
196     
197     private void addSubTemplate1(NamedElement element, int tabKey, TemplateDescription aTemplate) throws KilimException {
198         String eName = element.getLocalName();
199         int status = element.getStatus();
200         boolean canOverride = (status == KILIM.PUBLIC || status == KILIM.PROTECTED);
201         int result = aTemplate.checkDownOverride(eName, tabKey, canOverride);
202         if (result == NAME_CLASH) {
203             throw new KilimException("setting template " + name + " as super template of " + aTemplate + " leads to a name clash for" + eName);
204         }
205         if (result == ILLEGAL_OVERRIDE) {
206             throw new KilimException("setting template " + name + " as super template of " + aTemplate + " leads to an illegal override" + eName);
207         }
208     }
209         
210     /**
211      * @see org.objectweb.kilim.description.Template#removeSubTemplate(Template)
212      */

213     public void removeSubTemplate(TemplateDescription aTemplate) throws KilimException {
214         if (aTemplate == null) {
215             throw new KilimException("illegal null template in removeSubTemplate");
216         }
217         subTemplates.remove(aTemplate);
218     }
219     
220     /**
221      * @see org.objectweb.kilim.description.Template#getSubTemplates()
222      */

223     public Iterator getSubTemplates() {
224         if (subTemplates == null) {
225             return KILIM.EMPTY_ITERATOR;
226         }
227         return subTemplates.iterator();
228     }
229     
230     /**
231      * Method getTemplateDefHierarchy returns an iterator providing all the template elements overriden by a given element.
232      * The iterator contains the base element used for the look up.
233      * @param aElement : the base element used for the look up.
234      * @return Iterator
235      * @throws KilimException :
236      */

237     public Iterator getTemplateDefHierarchy(TemplateElementImpl aElement) throws KilimException {
238         String lName = null;
239         if (aElement instanceof Port) {
240             lName = ((Port) aElement).getLocalName();
241             return getMapDefHierarchy(lName, PORT_KEY);
242         }
243         if (aElement instanceof Property) {
244             lName = ((Property) aElement).getLocalName();
245             return getMapDefHierarchy(lName, PROPERTY_KEY);
246         }
247         if (aElement instanceof Provider) {
248             lName = ((Provider) aElement).getLocalName();
249             return getMapDefHierarchy(lName, PROVIDER_KEY);
250         }
251         if (aElement instanceof Transformer) {
252             lName = ((Transformer) aElement).getLocalName();
253             return getMapDefHierarchy(lName, TRANSFORMER_KEY);
254         }
255         if (aElement instanceof Slot) {
256             lName = ((Slot) aElement).getLocalName();
257             return getMapDefHierarchy(lName, SLOT_KEY);
258         }
259         if (aElement instanceof Instance) {
260             lName = ((Instance) aElement).getLocalName();
261             return getMapDefHierarchy(lName, INSTANCE_KEY);
262         }
263         if (aElement instanceof Binding) {
264             lName = ((Binding) aElement).getPortName();
265             return getListDefHierarchy(lName, BINDING_KEY);
266         }
267         if (aElement instanceof Trigger) {
268             lName = ((Trigger) aElement).getSourceName();
269             return getListDefHierarchy(lName, TRIGGER_KEY);
270         }
271         if (aElement instanceof Plug) {
272             lName = ((Plug) aElement).getSlotName();
273             return getListDefHierarchy(lName, PLUG_KEY);
274         }
275         return KILIM.EMPTY_ITERATOR;
276     }
277     
278     /**
279      * Method getAllMapContainingTemplates.
280      * @param aName :
281      * @param tabKey :
282      * @return Iterator
283      * @throws KilimException :
284      */

285     private Iterator getMapDefHierarchy(String aName, int tabKey) throws KilimException {
286         if (aName == null) {
287             throw new KilimException("attempt to get the containing templates of an element through a null name");
288         }
289         
290         TemplateDescription tmp0 = this;
291         List list = new ArrayList();
292         do {
293             NamedElement elem = tmp0.getLocalMapElement(aName, tabKey);
294             if (elem != null) {
295                 list.add(tmp0);
296             }
297         } while ((tmp0 = tmp0.getSuperTemplate()) != null);
298         if (list.size() == 0) {
299             return KILIM.EMPTY_ITERATOR;
300         }
301         return list.iterator();
302     }
303
304         
305     private Iterator getListDefHierarchy(String aName, int tabKey) throws KilimException {
306         if (aName == null) {
307             throw new KilimException("attempt to get the containing templates of an element through a null name");
308         }
309         
310         TemplateDescription tmp0 = this;
311         List list = new ArrayList();
312         do {
313             Iterator iter = tmp0.getLocalListElements(aName, tabKey);
314             if (iter != KILIM.EMPTY_ITERATOR) {
315                 list.add(tmp0);
316             }
317         } while ((tmp0 = tmp0.getSuperTemplate()) != null);
318         if (list.size() == 0) {
319             return KILIM.EMPTY_ITERATOR;
320         }
321         return list.iterator();
322     }
323     
324     /**
325      * @see org.objectweb.kilim.description.Template#getPort(String, boolean)
326      */

327     public BasicNamedElement getAnyProvider(String aName, boolean onlyLocal) throws KilimException {
328         LinkedHashMap map = getMap(ANY_PROVIDER_KEY, false);
329         BasicNamedElement provider = null;
330         if (map != null) {
331             ProviderData data = (ProviderData) map.get(aName);
332             if (data != null) {
333                 provider = (BasicNamedElement) data.provider;
334             }
335         }
336         if (provider != null || onlyLocal || superTemplate == null) {
337             return provider;
338         }
339         return (BasicNamedElement) getSuperTemplate().getRMapElement(aName, ANY_PROVIDER_KEY);
340     }
341
342     /**
343      * @see org.objectweb.kilim.description.Template#getProviderMap(boolean)
344      */

345     public Iterator getProviders(boolean onlyLocal) throws KilimException {
346         List list = new ArrayList();
347         Iterator iter = getAllInterfaces(onlyLocal);
348         while (iter.hasNext()) {
349             Object elem = iter.next();
350             if (elem instanceof Provider) {
351                 list.add(elem);
352             }
353         }
354
355         if (list.size() == 0) {
356             return KILIM.EMPTY_ITERATOR;
357         }
358         return list.iterator();
359     }
360
361     /**
362      * @see org.objectweb.kilim.description.Template#getProviderMap(boolean)
363      */

364     public Iterator getPorts(boolean onlyLocal) throws KilimException {
365         List list = new ArrayList();
366         Iterator iter = getAllInterfaces(onlyLocal);
367         while (iter.hasNext()) {
368             Object elem = iter.next();
369             if (elem instanceof Port) {
370                 list.add(elem);
371             }
372         }
373
374         if (list.size() == 0) {
375             return KILIM.EMPTY_ITERATOR;
376         }
377         return list.iterator();
378     }
379
380     /**
381      * @see org.objectweb.kilim.description.Template#getProviderMap(boolean)
382      */

383     public Iterator getProperties(boolean onlyLocal) throws KilimException {
384         List list = new ArrayList();
385         Iterator iter = getAllInterfaces(onlyLocal);
386         while (iter.hasNext()) {
387             Object elem = iter.next();
388             if (elem instanceof Property) {
389                 list.add(elem);
390             }
391         }
392
393         if (list.size() == 0) {
394             return KILIM.EMPTY_ITERATOR;
395         }
396         return list.iterator();
397     }
398
399     /**
400      * @see org.objectweb.kilim.description.Template#getPort(String, boolean)
401      */

402     public Slot getSlot(String aName, boolean onlyLocal) throws KilimException {
403         LinkedHashMap map = getMap(SLOT_KEY, false);
404         Slot slot = null;
405         if (map != null) {
406             slot = (Slot) map.get(aName);
407         }
408             
409         if (slot != null || onlyLocal || superTemplate == null) {
410             return slot;
411         }
412
413         return (Slot) getSuperTemplate().getRMapElement(aName, SLOT_KEY);
414     }
415         
416     /**
417      * @see org.objectweb.kilim.description.Template#getPortMap(boolean)
418      */

419     public Iterator getSlots(boolean onlyLocal) throws KilimException {
420         if (onlyLocal) {
421             if (slots == null) {
422                 return KILIM.EMPTY_ITERATOR;
423             }
424             return slots.values().iterator();
425         }
426         
427         LinkedHashMap map = new LinkedHashMap();
428         addMapVisible(map, SLOT_KEY, false);
429         return map.values().iterator();
430     }
431                     
432     /**
433      * @see org.objectweb.kilim.description.Template#getTransformer(String, boolean)
434      */

435     public BasicNamedElement getTransformer(String aName, boolean onlyLocal) throws KilimException {
436         LinkedHashMap map = getMap(TRANSFORMER_KEY, false);
437         BasicNamedElement transformer = null;
438         if (map != null) {
439             transformer = (BasicNamedElement) map.get(aName);
440         }
441
442         if (transformer != null || onlyLocal || superTemplate == null) {
443             return transformer;
444         }
445         return (BasicNamedElement) ((TemplateDescription) superTemplate).getRMapElement(aName, TRANSFORMER_KEY);
446     }
447
448     /**
449      * @see org.objectweb.kilim.description.Template#getTransformerMap(boolean)
450      */

451     public Iterator getTransformers(boolean onlyLocal) throws KilimException {
452         if (onlyLocal) {
453             if (transformers == null) {
454                 return KILIM.EMPTY_ITERATOR;
455             }
456             return transformers.values().iterator();
457         }
458         
459         LinkedHashMap map = new LinkedHashMap();
460         addMapVisible(map, TRANSFORMER_KEY, false);
461         return map.values().iterator();
462     }
463
464     /**
465      * @see org.objectweb.kilim.description.Template#getInstance(String, boolean)
466      */

467     public Instance getInstance(String aName, boolean onlyLocal) throws KilimException {
468         LinkedHashMap map = getMap(INSTANCE_KEY, false);
469         Instance instance = null;
470         if (map != null) {
471             instance = (Instance) map.get(aName);
472         }
473         
474         if (instance != null || onlyLocal || superTemplate == null) {
475             return instance;
476         }
477         return (Instance) ((TemplateDescription) superTemplate).getRMapElement(aName, INSTANCE_KEY);
478     }
479             
480     /**
481      * @see org.objectweb.kilim.description.Template#getInstanceMap(boolean)
482      */

483     public Iterator getInstances(boolean onlyLocal) throws KilimException {
484         if (onlyLocal) {
485             if (instances == null) {
486                 return KILIM.EMPTY_ITERATOR;
487             }
488             return instances.values().iterator();
489         }
490         
491         LinkedHashMap map = new LinkedHashMap();
492         addMapVisible(map, INSTANCE_KEY, false);
493         
494         Collection vls = map.values();
495         return vls.iterator();
496     }
497     
498     /**
499      * @see org.objectweb.kilim.description.Template#addProviderReference(ProviderReference)
500      */

501     public void addProviderReference(Reference reference) throws KilimException {
502         if (reference == null) {
503             throw new KilimException("illegal null reference in addProviderReference of TemplateImpl " + name);
504         }
505         if (!reference.providesValue()) {
506             throw new KilimException("illegal attempt to use a non provider reference in addProvider");
507         }
508         
509         String rName = reference.getTargetName();
510         if (rName == null) {
511             throw new KilimException("illegal null name in addProvider of TemplateImpl " + name);
512         }
513         ReferenceData data = (ReferenceData) getReferenceMap().get(rName);
514         
515         if (data == null) {
516             getReferenceMap().put(rName, new ReferenceData(PROVIDER_REFERENCE, reference));
517             return;
518         }
519     }
520     
521     /**
522      * @see org.objectweb.kilim.description.Template#removeProviderReference(String)
523      */

524     public void removeProviderReference(String aName) throws KilimException {
525         if (aName == null) {
526             throw new KilimException("illegal null name in removeProviderReference of TemplateImpl " + name);
527         }
528
529         ReferenceData data = (ReferenceData) getReferenceMap().get(aName);
530         
531         if (data == null) {
532             throw new KilimException("name " + aName + " is unknown in removeProviderReference of TemplateImpl" + name);
533         }
534         
535         if (data.referenceKind == PROVIDER_REFERENCE) {
536             getReferenceMap().remove(aName);
537         } else {
538             throw new KilimException("use of a transformer name in removeProviderReference of TemplateImpl " + name);
539         }
540     }
541     
542     /**
543      * @see org.objectweb.kilim.description.Template#getProviderReference(String, boolean)
544      */

545     public Reference getProviderReference(String aName, boolean onlyLocal) throws KilimException {
546         if (aName == null) {
547             throw new KilimException("illegal null name in getProviderReference of TemplateImpl" + name);
548         }
549
550         ReferenceData data = (ReferenceData) getReferenceMap().get(aName);
551                 
552         if (data == null) {
553             //throw new KilimException("name " + aName + " is unknown in getProviderReference of TemplateImpl " + name);
554
return null;
555         }
556         
557         if (data.referenceKind == PROVIDER_REFERENCE) {
558             return (Reference) data.reference;
559         } else {
560             throw new KilimException("use of a transformer name in removeProviderReference of TemplateImpl " + name);
561         }
562     }
563         
564     /**
565      * @see org.objectweb.kilim.description.Template#getTransformerReference(String, boolean)
566      */

567     public Reference getTransformerReference(String aName, boolean onlyLocal) throws KilimException {
568         if (aName == null) {
569             throw new KilimException("illegal null name in getTransformerReference of TemplateImpl " + name);
570         }
571
572         ReferenceData data = (ReferenceData) getReferenceMap().get(aName);
573         
574         if (data == null) {
575             //throw new KilimException("name " + aName + " is unknown in getTransformerReference of TemplateImpl " + name);
576
return null;
577         }
578         
579         if (data.referenceKind == TRANSFORMER_REFERENCE) {
580             return (Reference) data.reference;
581         } else {
582             throw new KilimException("use of a transformer name in removeProviderReference of TemplateImpl " + name);
583         }
584     }
585
586     /**
587      * @see org.objectweb.kilim.description.Template#getUnresolvedProviderReferences()
588      */

589     public Iterator getProviderReferences(boolean onlyLocal) {
590         List list = new ArrayList();
591         getReferences(list, onlyLocal, PROVIDER_REFERENCE);
592         return list.listIterator();
593     }
594
595     /**
596      * @see org.objectweb.kilim.description.Template#addTransformerReference(TransformerReference)
597      */

598     public void addTransformerReference(Reference reference) throws KilimException {
599         if (reference == null) {
600             throw new KilimException("illegal null reference in addProviderReference of TemplateImpl " + name);
601         }
602         if (!reference.performsAction()) {
603             throw new KilimException("attempt to use a non transformer reference in addTransformer");
604         }
605         
606         String rName = reference.getTargetName();
607         if (rName == null) {
608             throw new KilimException("illegal null name in addTransformer of TemplateImpl");
609         }
610         ReferenceData data = (ReferenceData) getReferenceMap().get(rName);
611         
612         if (data == null) {
613             getReferenceMap().put(rName, new ReferenceData(TRANSFORMER_REFERENCE, reference));
614             return;
615         }
616     }
617     
618     /**
619      * @see org.objectweb.kilim.description.Template#removeProviderReference(String)
620      */

621     public void removeTransformerReference(String aName) throws KilimException {
622         if (aName == null) {
623             throw new KilimException("illegal null name in removeTransformerReference of TemplateImpl " + name);
624         }
625
626         ReferenceData data = (ReferenceData) getReferenceMap().get(aName);
627         
628         if (data == null) {
629             throw new KilimException("transformer name " + aName + " is unknown in removeTransformerReference of TemplateImpl " + name);
630         }
631         
632         if (data.referenceKind == TRANSFORMER_REFERENCE) {
633             getReferenceMap().remove(aName);
634         } else {
635             throw new KilimException("use of a provider name in removeTransformerReference of TemplateImpl " + name);
636         }
637     }
638         
639     /**
640      * @see org.objectweb.kilim.description.Template#getUnresolvedTransformerReferences()
641      */

642     public Iterator getTransformerReferences(boolean onlyLocal) {
643         List list = new ArrayList();
644         getReferences(list, onlyLocal, TRANSFORMER_REFERENCE);
645         return list.listIterator();
646     }
647         
648     /**
649      * @see org.objectweb.kilim.description.Template#getReference(String, boolean)
650      */

651     public Reference getReference(String aName, boolean onlyLocal) throws KilimException {
652         if (aName == null) {
653             throw new KilimException("illegal null name in getProviderReference of TemplateImpl" + name);
654         }
655
656         ReferenceData data = (ReferenceData) getReferenceMap().get(aName);
657         
658         if (data == null) {
659             return null;
660         }
661         
662         return (Reference) data.reference;
663     }
664
665     /**
666      * @see org.objectweb.kilim.description.Template#addLocalPort(Port)
667      */

668     public void addLocalPort(Port port) throws KilimException {
669         addLocalMapElement(port, PORT_KEY);
670     }
671     
672     /**
673      * @see org.objectweb.kilim.description.Template#removeLocalPort(String)
674      */

675     public void removeLocalPort(String aName) throws KilimException {
676         removeLocalMapElement(aName, PORT_KEY);
677     }
678     
679     /**
680      * @see org.objectweb.kilim.description.Template#addLocalProperty(Property)
681      */

682     public void addLocalProperty(Property property) throws KilimException {
683         addLocalMapElement(property, PROPERTY_KEY);
684     }
685
686     /**
687      * @see org.objectweb.kilim.description.Template#removeLocalProperty(String)
688      */

689     public void removeLocalProperty(String aName) throws KilimException {
690         removeLocalMapElement(aName, PROPERTY_KEY);
691     }
692
693     /**
694      * @see org.objectweb.kilim.description.Template#addProvider(Provider)
695      */

696     public void addLocalProvider(BasicNamedElement aProvider) throws KilimException {
697         addLocalMapElement(aProvider, PROVIDER_KEY);
698     }
699
700     /**
701      * @see org.objectweb.kilim.description.Template#removeLocalProvider(String)
702      */

703     public void removeLocalProvider(String aName) throws KilimException {
704         removeLocalMapElement(aName, PROVIDER_KEY);
705     }
706
707     /**
708      * @see org.objectweb.kilim.description.Template#addLocalTransformer(Transformer)
709      */

710     public void addLocalTransformer(BasicNamedElement transformer) throws KilimException {
711         addLocalMapElement(transformer, TRANSFORMER_KEY);
712     }
713
714     /**
715      * @see org.objectweb.kilim.description.Template#removeLocalTransformer(String)
716      */

717     public void removeLocalTransformer(String aName) throws KilimException {
718         removeLocalMapElement(aName, TRANSFORMER_KEY);
719     }
720
721     /**
722      * @see org.objectweb.kilim.description.Template#addInstance(Instance)
723      */

724     public void addLocalInstance(Instance aInstance) throws KilimException {
725         addLocalMapElement(aInstance, INSTANCE_KEY);
726     }
727
728     /**
729      * @see org.objectweb.kilim.description.Template#removeLocalInstance(String)
730      */

731     public void removeLocalInstance(String aName) throws KilimException {
732         removeLocalMapElement(aName, INSTANCE_KEY);
733     }
734     
735     /**
736      * adds a slot declatation in the template description.
737      * @param aSlot : the slot to be added.
738      * @throws KilimException : generated when aSlot is null or has already been added previously.
739      */

740     public void addLocalSlot(Slot aSlot) throws KilimException {
741         addLocalMapElement(aSlot, SLOT_KEY);
742     }
743     
744     /**
745      * removes a slot definition from a template description.
746      * @param aName : the name of the the slot to be removed.
747      * @throws KilimException : generated when aSlot is null or unknown.
748      */

749     public void removeLocalSlot(String aName) throws KilimException {
750         removeLocalMapElement(aName, SLOT_KEY);
751     }
752
753     /**
754      * @see org.objectweb.kilim.description.Template#addLocalTrigger(Trigger)
755      */

756     public void addLocalTrigger(Trigger aTrigger) throws KilimException {
757         addLocalListElement(aTrigger, TRIGGER_KEY);
758     }
759     
760     /**
761      * @see org.objectweb.kilim.description.Template#removeLocalTrigger(Trigger)
762      */

763     public void removeLocalTrigger(Trigger aTrigger) throws KilimException {
764         removeLocalListElement(aTrigger, TRIGGER_KEY);
765     }
766         
767     /**
768      * @see org.objectweb.kilim.description.Template#addBinding(Binding)
769      */

770     public void addLocalBinding(Binding aBinding) throws KilimException {
771         addLocalListElement(aBinding, BINDING_KEY);
772     }
773     
774     /**
775      * @see org.objectweb.kilim.description.Template#removeBinding(String)
776      */

777     public void removeLocalBinding(Binding aBinding) throws KilimException {
778         removeLocalListElement(aBinding, BINDING_KEY);
779     }
780     
781         /**
782      * @see org.objectweb.kilim.description.Template#addBinding(Binding)
783      */

784     public void addLocalPlug(Plug aPlug) throws KilimException {
785         addLocalListElement(aPlug, PLUG_KEY);
786     }
787     
788     /**
789      * @see org.objectweb.kilim.description.Template#removeLocalPlug(String)
790      */

791     public void removeLocalPlug(Plug aPlug) throws KilimException {
792         removeLocalListElement(aPlug, PLUG_KEY);
793     }
794         
795     /**
796      * @see org.objectweb.kilim.description.Template#getTrigger(String, boolean)
797      */

798     public Iterator getTriggers(String aName, boolean onlyLocal) {
799         if (onlyLocal) {
800             return getLocalListElements(aName, TRIGGER_KEY);
801         }
802         
803         List list = new ArrayList();
804         addVisibleListElements(list, aName, TRIGGER_KEY);
805         return list.listIterator();
806     }
807     
808     /**
809      * @see org.objectweb.kilim.description.Template#getTrigger(String, boolean)
810      */

811     public Iterator getBindings(String aName, boolean onlyLocal) {
812         if (onlyLocal) {
813             return getLocalListElements(aName, BINDING_KEY);
814         }
815         
816         List list = new ArrayList();
817         addVisibleListElements(list, aName, BINDING_KEY);
818         return list.listIterator();
819     }
820
821     /**
822      * @see org.objectweb.kilim.description.Template#getTrigger(String, boolean)
823      */

824     public Iterator getPlugs(String aName, boolean onlyLocal) {
825         if (onlyLocal) {
826             return getLocalListElements(aName, PLUG_KEY);
827         }
828         
829         List list = new ArrayList();
830         addVisibleListElements(list, aName, PLUG_KEY);
831         return list.listIterator();
832     }
833     
834     /**
835      * @see org.objectweb.kilim.description.Template#getTriggerMap(boolean)
836      */

837     public Iterator getTriggers(boolean onlyLocal) {
838         if (onlyLocal) {
839             return getLocalListElements(TRIGGER_KEY);
840         }
841                     
842         List list = new ArrayList();
843         addAllVisibleListElements(list, TRIGGER_KEY);
844         if (list.size() == 0) {
845             return KILIM.EMPTY_ITERATOR;
846         }
847     
848         return list.listIterator();
849     }
850
851     /**
852      * @see org.objectweb.kilim.description.Template#getBindings(boolean)
853      */

854     public Iterator getBindings(boolean onlyLocal) {
855         if (onlyLocal) {
856             return getLocalListElements(BINDING_KEY);
857         }
858
859         List list = new ArrayList();
860         addAllVisibleListElements(list, BINDING_KEY);
861         if (list.size() == 0) {
862             return KILIM.EMPTY_ITERATOR;
863         }
864         return list.listIterator();
865     }
866     
867     /**
868      * @see org.objectweb.kilim.description.Template#getBindings(boolean)
869      */

870     public Iterator getPlugs(boolean onlyLocal) {
871         if (onlyLocal) {
872             return getLocalListElements(PLUG_KEY);
873         }
874
875         List list = new ArrayList();
876         addAllVisibleListElements(list, PLUG_KEY);
877         if (list.size() == 0) {
878             return KILIM.EMPTY_ITERATOR;
879         }
880         return list.listIterator();
881     }
882
883     
884     /**
885      * @see java.lang.Object#toString()
886      */

887     public String toString() {
888         return name + " :: " + super.toString();
889     }
890     
891     /**
892      * @see org.objectweb.kilim.description.Template#containsNewDefinitions()
893      */

894     public boolean containsNewDefinitions() throws KilimException {
895         if ((allProviders != null && allProviders.size() > 0)
896         || (transformers != null && transformers.size() > 0)
897         || (bindings != null && bindings.size() > 0)
898         || (triggers != null && triggers.size() > 0)
899         || (instances != null && instances.size() > 0)
900         || (slots != null && slots.size() > 0)
901         || (plugs != null && plugs.size() > 0)) {
902             return true;
903         }
904         return false;
905     }
906         
907     private Binding getRBinding(String aName) {
908         Binding binding = null;
909         
910         if (bindings != null) {
911             binding = (Binding) getMap(BINDING_KEY, true).get(aName);
912         }
913         
914         if (binding != null || superTemplate == null) {
915             return binding;
916         }
917         return ((TemplateDescription) superTemplate).getRBinding(aName);
918     }
919     
920     /**
921      * Method checkLocalNameUnicity.
922      * @param aName :
923      * @throws KilimException :
924      */

925     private boolean localNameUnicity (String aName) throws KilimException {
926         if (getAnyProvider(aName, true) != null || getSlot(aName, true) != null
927             || getTransformer(aName, true) != null || getInstance(aName, true) != null) {
928             return false;
929         }
930         return true;
931     }
932     
933     private void checkOverride(String aName, int aKey, boolean canOverride) throws KilimException {
934         int result = checkUpOverride(aName, aKey);
935         if (result == ILLEGAL_OVERRIDE) {
936             throw new KilimException("illegal override of element " + aName);
937         }
938             
939         if (result == NAME_CLASH) {
940             throw new KilimException("name clash for element (1) " + aName);
941         }
942         
943         result = checkDownOverride(aName, aKey, canOverride);
944         if (result == ILLEGAL_OVERRIDE) {
945             throw new KilimException("illegal override of element " + aName);
946         }
947         if (result == NAME_CLASH) {
948             throw new KilimException("name clash for element (2) " + aName);
949         }
950     }
951     
952     /**
953      * Method checkOverride : It checks whether override is legal or not.
954      * This method is not optimized and checks all the hierarchy in order to detect illegal situations.
955      * @param aName :
956      * @param tabKey :
957      * @throws KilimException :
958      */

959     private int checkUpOverride(String aName, int tabKey) throws KilimException {
960         //no super template. Override is thus trivially legal.
961
if (superTemplate == null) {
962             return LEGAL_OVERRIDE;
963         }
964         
965         //aName is present in super template with a private status
966
NamedElement element = ((TemplateDescription) superTemplate).getLocalMapElement(aName, tabKey);
967
968         if (element != null) {
969             if (element.getStatus() == KILIM.PRIVATE) {
970                 return ILLEGAL_OVERRIDE;
971             }
972         }
973         
974         //aName is present in super template in a different name space
975
if ((tabKey != PROVIDER_KEY && tabKey != PORT_KEY && tabKey != PROPERTY_KEY
976             && tabKey != ANY_PROVIDER_KEY && getLocalMapElement(aName, ANY_PROVIDER_KEY) != null)
977             || (tabKey != TRANSFORMER_KEY && getLocalMapElement(aName, TRANSFORMER_KEY) != null)
978             || (tabKey != INSTANCE_KEY && getLocalMapElement(aName, INSTANCE_KEY) != null)
979             || (tabKey != SLOT_KEY && getLocalMapElement(aName, SLOT_KEY) != null)) {
980             return NAME_CLASH;
981         }
982         
983         //no illegal situation detected : check in super template ....
984
return ((TemplateDescription) superTemplate).checkUpOverride(aName, tabKey);
985     }
986     
987     private int checkDownOverride(String aName, int tabKey, boolean canOverride) {
988         NamedElement element = getLocalMapElement(aName, tabKey);
989
990         //aName is present in template overriding a private element
991
if (element != null) {
992             if (!canOverride) {
993                 return ILLEGAL_OVERRIDE;
994             }
995         }
996         
997         //aName is present in template in a different name space
998
if ((tabKey != PROVIDER_KEY && tabKey != PORT_KEY && tabKey != PROPERTY_KEY
999             && tabKey != ANY_PROVIDER_KEY && getLocalMapElement(aName, ANY_PROVIDER_KEY) != null)
1000            || (tabKey != TRANSFORMER_KEY && getLocalMapElement(aName, TRANSFORMER_KEY) != null)
1001            || (tabKey != INSTANCE_KEY && getLocalMapElement(aName, INSTANCE_KEY) != null)
1002            || (tabKey != SLOT_KEY && getLocalMapElement(aName, SLOT_KEY) != null)) {
1003            return NAME_CLASH;
1004        }
1005        
1006        //no detected illegal situation detected : check in subTemplates ...
1007
Iterator iter = getSubTemplates();
1008        while (iter.hasNext()) {
1009            int result = 0;
1010            try {
1011            result = ((TemplateDescription) iter.next()).checkDownOverride(aName, tabKey, canOverride);
1012            } catch (ClassCastException e) {
1013                e.printStackTrace();
1014            }
1015            if (result == ILLEGAL_OVERRIDE || result == NAME_CLASH) {
1016                return result;
1017            }
1018        }
1019        return LEGAL_OVERRIDE;
1020    }
1021                
1022    /* ****************************************************************************************************
1023     * The following methods are generic methods used to handle template elements stored in LinkedHashMap tables.
1024     * ***************************************************************************************************/

1025    
1026    private String[] elementIdents = {
1027        "", "provider", "transformer", "property", "instance", "port", "trigger", "binding", "slot", "plug"
1028    };
1029        
1030    //The addLocalElement method is a method for adding a named template element in its LinkedHashMap
1031
private void addLocalMapElement(NamedElement aElement, int tabKey) throws KilimException {
1032        if (aElement == null) {
1033            throw new KilimException("attempt to add a null " + elementIdents[tabKey] + " in template " + name);
1034        }
1035        String pName = aElement.getLocalName();
1036        if (pName == null) {
1037            throw new KilimException("attempt to add a " + elementIdents[tabKey] + " with a null name in template " + name);
1038        }
1039
1040        if (!localNameUnicity(pName)) {
1041            throw new KilimException("name clash for " + pName + " in template " + name);
1042        }
1043        checkOverride(pName, tabKey, aElement.getStatus() != KILIM.PRIVATE);
1044        switch(tabKey) {
1045            case PROVIDER_KEY :
1046                BasicElement src = ((Provider) aElement).getSource();
1047                if (src instanceof InlinedElement) {
1048                    ((InlinedElement) src).setLocalName(pName);
1049                }
1050            case PORT_KEY :
1051            case PROPERTY_KEY :
1052            case ANY_PROVIDER_KEY :
1053                getMap(tabKey, true).put(pName, new ProviderData(tabKey, aElement));
1054                break;
1055            case TRANSFORMER_KEY :
1056                BasicElement act = ((Transformer) aElement).getAction();
1057                if (act instanceof InlinedElement) {
1058                    ((InlinedElement) act).setLocalName(pName);
1059                }
1060            default :
1061                getMap(tabKey, true).put(pName, aElement);
1062                break;
1063        }
1064        aElement.setContainingTemplate(this);
1065    }
1066    
1067    //The removeLocalElement method is a method for removing a named template element from its LinkedHashMap
1068
private void removeLocalMapElement(String aName, int tabKey) throws KilimException {
1069        if (aName == null) {
1070            throw new KilimException("attempt to remove a " + elementIdents[tabKey] + " through a null name in template " + name);
1071        }
1072        LinkedHashMap map = getMap(tabKey, false);
1073        if (map != null) {
1074            TemplateElementImpl element = (TemplateElementImpl) map.get(aName);
1075            if (map != null) {
1076                map.remove(aName);
1077            }
1078            element.setContainingTemplate(null);
1079        }
1080    }
1081    
1082    /**
1083     * Method addMapVisible.
1084     * This method gets all locally declared elements of a given kind and add them to the
1085     * LinkedHashMap received as an argument.
1086     * @param map : a hash map to be completed with the inherited elements.
1087     * @param tabKey : a key to designate the kind of the elements to be handled.
1088     * @param isParent : a boolean value indicating whether the current template is a parent template or not..
1089     */

1090    private void addMapVisible(LinkedHashMap map, int tabKey, boolean isParent) throws KilimException {
1091        Iterator enum = null;
1092        switch(tabKey) {
1093            case PROVIDER_KEY :
1094            case PROPERTY_KEY :
1095            case PORT_KEY :
1096            case ANY_PROVIDER_KEY :
1097                if (allProviders != null) {
1098                    enum = allProviders.values().iterator();
1099                }
1100                break;
1101            case TRANSFORMER_KEY :
1102                if (transformers != null) {
1103                    enum = transformers.values().iterator();
1104                }
1105                break;
1106            case INSTANCE_KEY :
1107                if (instances != null) {
1108                    enum = instances.values().iterator();
1109                }
1110                break;
1111            case TRIGGER_KEY :
1112                if (triggers != null) {
1113                    enum = triggers.iterator();
1114                }
1115                break;
1116            case SLOT_KEY :
1117                if (slots != null) {
1118                    enum = slots.values().iterator();
1119                }
1120                break;
1121            default :
1122                throw new InternalException("illegal key " + tabKey + " in _addMapVisible");
1123        }
1124        
1125        NamedElement provider = null;
1126        if (enum != null) {
1127            while (enum.hasNext()) {
1128                Object element = enum.next();
1129                String eName;
1130                switch (tabKey) {
1131                    case PROVIDER_KEY :
1132                    case PROPERTY_KEY :
1133                    case PORT_KEY :
1134                    case ANY_PROVIDER_KEY :
1135                        provider = (BasicNamedElement) ((ProviderData) element).provider;
1136                        break;
1137                    default :
1138                        provider = (NamedElement) element;
1139                        break;
1140                }
1141                
1142                eName = provider.getLocalName();
1143                //check whether the element is masked (i.e. it has been overriden in inherited templates)
1144
if (map.containsKey(eName)) {
1145                    continue;
1146                }
1147            
1148                //check whether the element is visible in the inherited templates (i.e; it has a status
1149
//which is different from PRIVATE).
1150
if ((provider.getStatus() != KILIM.PRIVATE) || !isParent) {
1151                    map.put(eName, element);
1152                }
1153            }
1154        }
1155        
1156        if (superTemplate != null) {
1157            ((TemplateDescription) superTemplate).addMapVisible(map, tabKey, true);
1158        }
1159    }
1160
1161    //The getLocalElement method is a method for retrieving a named template element from
1162
//its local LinkedHashMap
1163
private NamedElement getLocalMapElement(String aName, int tabKey) {
1164        switch(tabKey) {
1165            case PROVIDER_KEY :
1166            case PROPERTY_KEY :
1167            case PORT_KEY :
1168            case ANY_PROVIDER_KEY :
1169                if (allProviders == null) {
1170                    return null;
1171                }
1172                ProviderData data = (ProviderData) allProviders.get(aName);
1173                if (data == null) {
1174                    return null;
1175                }
1176                return (NamedElement) data.provider;
1177            case TRANSFORMER_KEY :
1178                if (transformers == null) {
1179                    return null;
1180                } else {
1181                    return (NamedElement) transformers.get(aName);
1182                }
1183            case INSTANCE_KEY :
1184                if (instances == null) {
1185                    return null;
1186                } else {
1187                    return (NamedElement) instances.get(aName);
1188                }
1189            case SLOT_KEY :
1190                if (slots == null) {
1191                    return null;
1192                } else {
1193                    return (NamedElement) slots.get(aName);
1194                }
1195            default :
1196                throw new InternalException("illegal key " + tabKey + " in getRElement for name " + aName);
1197        }
1198    }
1199    
1200    //The getRMapElement method is a recursive method for retrieving a named template element
1201
//stored in a LinkedHashMap
1202
private NamedElement getRMapElement(String aName, int tabKey) throws KilimException {
1203        NamedElement element = getLocalMapElement(aName, tabKey);
1204
1205        if (element != null || superTemplate == null) {
1206            return element;
1207        }
1208
1209        return getSuperTemplate().getRMapElement(aName, tabKey);
1210    }
1211    
1212    //This method returns the map corresponding to a given element kind. The map is created
1213
//if it does not exist and if the value of the create argument is equal to true.
1214
private LinkedHashMap getMap(int tabKey, boolean create) {
1215        switch(tabKey) {
1216            case PORT_KEY :
1217            case PROPERTY_KEY :
1218            case PROVIDER_KEY :
1219            case ANY_PROVIDER_KEY :
1220                if (allProviders == null && create) {
1221                    allProviders = new LinkedHashMap();
1222                }
1223                return allProviders;
1224            case TRANSFORMER_KEY :
1225                if (transformers == null && create) {
1226                    transformers = new LinkedHashMap();
1227                }
1228                return transformers;
1229            case INSTANCE_KEY :
1230                if (instances == null && create) {
1231                    instances = new LinkedHashMap();
1232                }
1233                return instances;
1234            case SLOT_KEY :
1235                if (slots == null && create) {
1236                    slots = new LinkedHashMap();
1237                }
1238                return slots;
1239            default :
1240                throw new InternalException("illegal key " + tabKey + " in getMap");
1241        }
1242    }
1243    
1244    /* ****************************************************************************************************
1245     * The following methods are generic methods used to handle template elements references.
1246     * ***************************************************************************************************/

1247    
1248    private void getReferences(List aList, boolean onlyLocal, int aKind) {
1249        if (references != null) {
1250            Iterator iter = references.values().iterator();
1251                    
1252            while (iter.hasNext()) {
1253                ReferenceData data = (ReferenceData) iter.next();
1254                if (data.referenceKind == aKind) {
1255                    aList.add(data.reference);
1256                }
1257            }
1258        }
1259        if (superTemplate == null || onlyLocal) {
1260            return;
1261        }
1262        ((TemplateDescription) superTemplate).getReferences(aList, onlyLocal, aKind);
1263    }
1264    
1265    private LinkedHashMap getReferenceMap() {
1266        if (references == null) {
1267            references = new LinkedHashMap();
1268        }
1269        return references;
1270    }
1271    
1272    /* ****************************************************************************************************
1273     * The following methods are generic methods used to handle template elements stored in linked lists.
1274     * ***************************************************************************************************/

1275        
1276    //The addLocalElement method is a method for adding a named template element in its LinkedHashMap
1277
private void addLocalListElement(TemplateElementImpl aElement, int listKey) throws KilimException {
1278        if (aElement == null) {
1279            throw new KilimException("attempt to add a null " + elementIdents[listKey] + " in template " + name);
1280        }
1281        getList(listKey, true).add(aElement);
1282        aElement.setContainingTemplate(this);
1283    }
1284    
1285    //The removeLocalElement method is a method for removing a named template element from its LinkedHashMap
1286
private void removeLocalListElement(TemplateElementImpl aElement, int listKey) throws KilimException {
1287        if (aElement == null) {
1288            throw new KilimException("attempt to remove a " + elementIdents[listKey] + " through a null reference in template " + name);
1289        }
1290        List list = getList(listKey, false);
1291        if (list == null) {
1292            throw new KilimException("attempt to remove a " + elementIdents[listKey] + " from an empty template " + name);
1293        }
1294        
1295        list.remove(aElement);
1296        aElement.setContainingTemplate(null);
1297    }
1298    
1299    //This method returns the list used to store a template element.
1300
private List getList(int tabKey, boolean create) {
1301        switch(tabKey) {
1302            case BINDING_KEY :
1303                if (bindings == null && create) {
1304                    bindings = new ArrayList();
1305                }
1306                return bindings;
1307            case PLUG_KEY :
1308                if (plugs == null && create) {
1309                    plugs = new ArrayList();
1310                }
1311                return plugs;
1312            case TRIGGER_KEY :
1313                if (triggers == null && create) {
1314                    triggers = new ArrayList();
1315                }
1316                return triggers;
1317        }
1318        return null;
1319    }
1320    
1321    private Iterator getLocalListElements(int listKey) {
1322        List list = getList(listKey, false);
1323        if (list == null) {
1324            return KILIM.EMPTY_ITERATOR;
1325        }
1326        
1327        Iterator iter = list.listIterator();
1328        List result = new ArrayList();
1329        
1330        Object element = null;
1331        while (iter.hasNext()) {
1332            switch (listKey) {
1333                case BINDING_KEY :
1334                    element = iter.next();
1335                    break;
1336                case TRIGGER_KEY :
1337                    element = iter.next();
1338                case PLUG_KEY :
1339                    element = iter.next();
1340                    break;
1341            }
1342            result.add(element);
1343        }
1344        if (result.size() == 0) {
1345            return KILIM.EMPTY_ITERATOR;
1346        }
1347        return result.listIterator();
1348    }
1349    
1350    private Iterator getLocalListElements(String aName, int listKey) {
1351        
1352        List list = getList(listKey, false);
1353        if (list == null) {
1354            return KILIM.EMPTY_ITERATOR;
1355        }
1356        
1357        Iterator iter = list.listIterator();
1358        List result = new ArrayList();
1359        
1360        Object element = null;
1361        String nm = null;
1362        while (iter.hasNext()) {
1363            switch (listKey) {
1364                case BINDING_KEY :
1365                    element = iter.next();
1366                    nm = ((Binding) element).getPortName();
1367                    break;
1368                case TRIGGER_KEY :
1369                    element = iter.next();
1370                    nm = ((Trigger) element).getSourceName();
1371                    break;
1372                case PLUG_KEY :
1373                    element = iter.next();
1374                    nm = ((Plug) element).getSlotName();
1375                    break;
1376            }
1377            if (aName.equals(nm)) {
1378                result.add(element);
1379            }
1380        }
1381        if (result.size() == 0) {
1382            return KILIM.EMPTY_ITERATOR;
1383        }
1384        return result.listIterator();
1385    }
1386    
1387    private void addVisibleListElements(List list, String aName, int listKey) {
1388        boolean stop = false;
1389
1390        //look up should stop as soon as a port, property or provider definition has been found , since this override maskes all previous binding
1391
//and trigger definitions ... It should be noted here that there is no restriction on the override process.
1392
if ((listKey == BINDING_KEY) || (listKey == TRIGGER_KEY)) {
1393            if (getLocalMapElement(aName, ANY_PROVIDER_KEY) != null) {
1394                stop = true;
1395            }
1396        }
1397        
1398        if (superTemplate != null && !stop) {
1399            ((TemplateDescription) superTemplate).addVisibleListElements(list, aName, listKey);
1400        }
1401        
1402        List list1 = getList(listKey, false);
1403        if (list1 != null) {
1404            Iterator iter = list1.listIterator();
1405            Object element = null;
1406            String nm = null;
1407            while (iter.hasNext()) {
1408                TemplateElementImpl elem = null;
1409                switch (listKey) {
1410                    case BINDING_KEY :
1411                        element = iter.next();
1412                        nm = ((Binding) element).getPortName();
1413                        break;
1414                    case TRIGGER_KEY :
1415                        element = iter.next();
1416                        nm = ((Trigger) element).getSourceName();
1417                        break;
1418                    case PLUG_KEY :
1419                        element = iter.next();
1420                        nm = ((Plug) element).getSlotName();
1421                        break;
1422                }
1423                if (aName.equals(nm)) {
1424                    list.add(element);
1425                }
1426            }
1427        }
1428    }
1429    
1430    private void addAllVisiblePlugs(List list) {
1431        if (superTemplate != null) {
1432            ((TemplateDescription) superTemplate).addAllVisiblePlugs(list);
1433        }
1434
1435        List list1 = getList(PLUG_KEY, false);
1436        if (list1 != null) {
1437            Iterator iter = list1.listIterator();
1438            TemplateElementImpl element;
1439            loop : while (iter.hasNext()) {
1440                element = (TemplateElementImpl) iter.next();
1441                list.add(element);
1442            }
1443        }
1444    }
1445    
1446    private void addAllVisibleListElements(List list, int listKey) {
1447        if (listKey == PLUG_KEY) {
1448            addAllVisiblePlugs(list);
1449            return;
1450        }
1451        
1452        LinkedHashMap allVisible = getProviderLastDefinitionTable();
1453        addAllVisibleListElements1(list, listKey, 0, allVisible);
1454    }
1455    
1456    private void addAllVisibleListElements1(List list, int listKey, int aLevel, LinkedHashMap aDefTable) {
1457        if (superTemplate != null) {
1458            ((TemplateDescription) superTemplate).addAllVisibleListElements1(list, listKey, aLevel + 1, aDefTable);
1459        }
1460
1461        List list1 = getList(listKey, false);
1462        if (list1 != null) {
1463            Iterator iter = list1.listIterator();
1464            while (iter.hasNext()) {
1465                Object element = iter.next();
1466                String nm = null;
1467                if (listKey == BINDING_KEY) {
1468                    nm = ((Binding) element).getPortName();
1469                } else if (listKey == TRIGGER_KEY) {
1470                    nm = ((Trigger) element).getSourceName();
1471                }
1472                
1473                if ("THIS".equals(nm)) {
1474                    list.add(element);
1475                    continue;
1476                }
1477                
1478                Integer level1 = (Integer) aDefTable.get(nm);
1479                if (level1 == null) {
1480                    throw new InternalException("Problem in AddAllVisibleListElements1 of TemplateDescription : unknown element " + nm);
1481                } else {
1482                    if (aLevel <= level1.intValue()) {
1483                        list.add(element);
1484                    }
1485                }
1486            }
1487        }
1488    }
1489    
1490    LinkedHashMap definitionTable = new LinkedHashMap();
1491
1492    private LinkedHashMap getProviderLastDefinitionTable() {
1493        //LinkedHashMap definitionTable = new LinkedHashMap();
1494
definitionTable.clear();
1495        getPLastDefinitionSet(definitionTable, 0);
1496        return definitionTable;
1497    }
1498    
1499    private void getPLastDefinitionSet(LinkedHashMap aTable, int aLevel) {
1500        Integer level = new Integer(aLevel);
1501        if (allProviders != null) {
1502            Iterator iter = allProviders.keySet().iterator();
1503            while (iter.hasNext()) {
1504                String name = (String) iter.next();
1505                if (aTable.containsKey(name)) {
1506                    continue;
1507                }
1508                ProviderData data = (ProviderData) allProviders.get(name);
1509                if (data.provider instanceof InlinedElement) {
1510                    continue;
1511                }
1512                aTable.put(name, level);
1513            }
1514        }
1515        
1516        if (slots != null) {
1517            Iterator iter = slots.values().iterator();
1518            while (iter.hasNext()) {
1519                Slot slot = (Slot) iter.next();
1520                String slotName = slot.getLocalName();
1521                Iterator iter1 = slot.getPorts();
1522                while (iter1.hasNext()) {
1523                    Port port = (Port) iter1.next();
1524                    String name = slotName + "/" + port.getLocalName();
1525                    aTable.put(name, level);
1526                }
1527            }
1528        }
1529                
1530        TemplateDescription superTmp = getSuperTemplate();
1531        
1532        if (superTmp != null) {
1533            superTmp.getPLastDefinitionSet(aTable, aLevel + 1);
1534        }
1535    }
1536    
1537    /**
1538     * Method getAllInterfaces.
1539     * @param onlyLocal :
1540     * @return Iterator
1541     */

1542    public Iterator getAllInterfaces(boolean onlyLocal) {
1543        LinkedHashMap allInterfaces = new LinkedHashMap();
1544        getAllInterfaces1(allInterfaces, onlyLocal);
1545        return allInterfaces.values().iterator();
1546    }
1547    
1548    private void getAllInterfaces1(LinkedHashMap aTable, boolean onlyLocal) {
1549        if (allProviders != null) {
1550            Iterator iter = allProviders.keySet().iterator();
1551            while (iter.hasNext()) {
1552                String name = (String) iter.next();
1553                if (aTable.containsKey(name)) {
1554                    continue;
1555                }
1556                ProviderData data = (ProviderData) allProviders.get(name);
1557                if (data.provider instanceof InlinedElement) {
1558                    continue;
1559                }
1560                aTable.put(name, data.provider);
1561            }
1562        }
1563                
1564        TemplateDescription superTmp = getSuperTemplate();
1565        
1566        if (superTmp != null && !onlyLocal) {
1567            superTmp.getAllInterfaces1(aTable, onlyLocal);
1568        }
1569    }
1570    
1571    /**
1572     * Returns the Resource Loader that was used to load this <code>TemplateDescription</code>.
1573     * @return the Resource Loader that was used to load this <code>TemplateDescription</code>.
1574     */

1575    public ResourceLoader getResourceLoader() {
1576        return resourceLoader;
1577    }
1578
1579    /**
1580     * Sets the Resource Loader that was used to load this <code>TemplateDescription</code>.
1581     * @param resourceLoader The resourceloader that was used to load this <code>TemplateDescription</code>.
1582     */

1583    public void setResourceLoader(ResourceLoader resourceLoader) {
1584        this.resourceLoader = resourceLoader;
1585    }
1586}
Popular Tags