KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > uml > transformation > engine > MappingRuleEngineWithCollection


1 /*====================================================================
2
3 OpenCCM: The Open CORBA Component Model Platform
4 Copyright (C) 2000-2004 INRIA - USTL - LIFL - GOAL
5 Contact: openccm@objectweb.org
6
7 This library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Lesser General Public
9 License as published by the Free Software Foundation; either
10 version 2.1 of the License, or any later version.
11
12 This library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Lesser General Public License for more details.
16
17 You should have received a copy of the GNU Lesser General Public
18 License along with this library; if not, write to the Free Software
19 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
20 USA
21
22 Initial developer(s): Pierre Carpentier.
23 Contributor(s): Philippe Merle.
24
25 ---------------------------------------------------------------------
26 $Id: MappingRuleEngineWithCollection.java,v 1.1 2004/05/26 11:25:35 carpentier Exp $
27 ====================================================================*/

28
29 package org.objectweb.openccm.uml.transformation.engine;
30
31 import ispuml.mdaTransformation.EngineContext;
32 import ispuml.mdaTransformation.MappingRuleEngine;
33 import ispuml.mdaTransformation.RuleContext;
34 import ispuml.mdaTransformation.TransformationException;
35
36 import java.util.ArrayList JavaDoc;
37 import java.util.Collection JavaDoc;
38 import java.util.Iterator JavaDoc;
39 import java.util.List JavaDoc;
40
41 /**
42  * Extension of the MdaTransf engine in order to take in account the
43  * transformation of collections.
44  */

45 public class MappingRuleEngineWithCollection extends MappingRuleEngine {
46
47     /**
48      * Apply the transformation rules to each object of the collection.
49      * @param bean The bean to transform
50      * @param request The current request context
51      * @param rulesetName The name of the ruleset to use. If null, use the default ruleset
52      * @param ruleName The name of the rule to use. If null, use a matching rule.
53      * @return Object A list of result.
54      */

55     protected Object JavaDoc transformCollection(Object JavaDoc bean, RuleContext request, String JavaDoc rulesetName, String JavaDoc ruleName)
56         throws TransformationException {
57         if (!(bean instanceof Collection JavaDoc))
58             throw new TransformationException("Object to transform should denote a Collection.");
59         
60         if (request.getCurrentRuleContext().getRule() instanceof ispuml.mdaTransformation.rules.xml.MultiParamXmlRule) {
61             ispuml.mdaTransformation.rules.xml.MultiParamXmlRule rule = (ispuml.mdaTransformation.rules.xml.MultiParamXmlRule)request.getCurrentRuleContext().getRule();
62             ispuml.mdaTransformation.rules.Parameters parameters = rule.getSrcParameterNames();
63             if (parameters != null && parameters.size() > 1) {
64                 return transform(bean, request, rulesetName, ruleName);
65             }
66         }
67         
68         Collection JavaDoc collection = (Collection JavaDoc) bean;
69         Iterator JavaDoc iter = collection.iterator();
70         List JavaDoc results = new ArrayList JavaDoc();
71         while (iter.hasNext()) {
72             Object JavaDoc curBean = iter.next();
73             if (curBean != null) {
74                 if (Collection JavaDoc.class.isAssignableFrom(curBean.getClass())) {
75                     List JavaDoc list = (List JavaDoc) transformCollection(curBean, request, rulesetName, ruleName);
76                     results.addAll(list);
77                 } else {
78                     Object JavaDoc res = transform(curBean, request, rulesetName, ruleName);
79                     if (res != null)
80                         results.add(res);
81                 }
82             } // end if
83
} // end loop
84

85         return results;
86     }
87
88     /* (non-Javadoc)
89      * @see ispuml.mdaTransformation.MappingRuleEngine#getContext()
90      */

91     public EngineContext getContext() {
92         EngineContext context = super.getContext();
93         if (context.getAttribute("CR") == null)
94             context.putAttribute("CR", "\n");
95         return context;
96     }
97
98 }
99
Popular Tags