KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > openccm > uml > transformation > rules > xml > CopyEnumeration


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: CopyEnumeration.java,v 1.2 2004/10/08 15:53:07 merle Exp $
27 ====================================================================*/

28
29 package org.objectweb.openccm.uml.transformation.rules.xml;
30
31 import ispuml.mdaTransformation.TransformationException;
32 import ispuml.mdaTransformation.rules.xml.Rule;
33
34 import java.util.StringTokenizer JavaDoc;
35 import javax.jmi.reflect.RefEnum;
36
37 /**
38  * Rule to copy an enumeration (source model) into an other
39  * enumeration (in the destination model).
40  *
41  * @author Pierre Carpentier
42  */

43 public class CopyEnumeration extends Rule implements EnumerationRule {
44
45     /** The possible value of the source enumeration. */
46     private String JavaDoc srcLabels [];
47     
48     /** The possible value of the destination enumeration. */
49     private String JavaDoc dstLabels [];
50     
51     /**
52      * Parses the string representing the possible value of the enumeration
53      * and converts it into an array of String.
54      * @param labels The String representing the value of the enumeration (separated with '|')
55      * @return The array of String (values of enumeration).
56      */

57     private String JavaDoc [] parseLabels(String JavaDoc labels) {
58         StringTokenizer JavaDoc token = new StringTokenizer JavaDoc(labels, "|");
59         String JavaDoc [] arrayLabels = new String JavaDoc[token.countTokens()];
60         for (int i=0 ; token.hasMoreTokens() ; i++) {
61             arrayLabels[i] = token.nextToken();
62         }
63         return arrayLabels;
64     }
65
66     /**
67      * Sets the possible values of the source enumeration.
68      * @param srcLabel The String representing the value of the source enumeration (separated with '|')
69      */

70     public void setMappingSrcLabel(String JavaDoc srcLabel) {
71         srcLabels = parseLabels(srcLabel);
72     }
73     
74     /**
75      * Sets the possible values of the destination enumeration.
76      * @param srcLabel The String representing the value of the destination enumeration (separated with '|')
77      */

78     public void setMappingDstLabel(String JavaDoc dstLabel) {
79         dstLabels = parseLabels(dstLabel);
80     }
81     
82     /**
83      * Gets the equivalent value of the destination enumeration which
84      * corresponds to the value of the source enumeration.
85      * @param object The source enumeration value.
86      * @return The String representing the value of the destination enumeration.
87      * @throws TransformationException
88      */

89     public String JavaDoc getEquivalentLabel(Object JavaDoc object) throws TransformationException {
90         RefEnum refEnum = (RefEnum) object;
91         if (srcLabels.length != dstLabels.length)
92             throw new TransformationException("The mapping is incorrect.");
93         for (int i=0 ; i<srcLabels.length ; i++) {
94             //System.out.println(">>> Comparison between " + refEnum.toString() + " and " + srcLabels[i]);
95
if (refEnum.toString().equals(srcLabels[i])) {
96                  return dstLabels[i];
97             }
98         }
99         throw new TransformationException("The equivalent of " + refEnum.toString() + " is not found.");
100     }
101
102 }
103
Popular Tags