Coverage Report - org.argouml.model.mdr.DataTypesFactoryMDRImpl
 
Classes in this File Line Coverage Branch Coverage Complexity
DataTypesFactoryMDRImpl
4%
4/85
0%
0/26
1.955
 
 1  
 /* $Id: DataTypesFactoryMDRImpl.java 18760 2010-09-18 05:19:53Z tfmorris $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009,2010 Contributors - see below
 4  
  * All rights reserved. This program and the accompanying materials
 5  
  * are made available under the terms of the Eclipse Public License v1.0
 6  
  * which accompanies this distribution, and is available at
 7  
  * http://www.eclipse.org/legal/epl-v10.html
 8  
  *
 9  
  * Contributors:
 10  
  *    Tom Morris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 1996-2007 The Regents of the University of California. All
 17  
 // Rights Reserved. Permission to use, copy, modify, and distribute this
 18  
 // software and its documentation without fee, and without a written
 19  
 // agreement is hereby granted, provided that the above copyright notice
 20  
 // and this paragraph appear in all copies.  This software program and
 21  
 // documentation are copyrighted by The Regents of the University of
 22  
 // California. The software program and documentation are supplied "AS
 23  
 // IS", without any accompanying services from The Regents. The Regents
 24  
 // does not warrant that the operation of the program will be
 25  
 // uninterrupted or error-free. The end-user understands that the program
 26  
 // was developed for research purposes and is advised not to rely
 27  
 // exclusively on the program for any reason.  IN NO EVENT SHALL THE
 28  
 // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
 29  
 // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
 30  
 // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
 31  
 // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
 32  
 // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
 33  
 // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 34  
 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
 35  
 // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
 36  
 // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
 37  
 // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.
 38  
 
 39  
 package org.argouml.model.mdr;
 40  
 
 41  
 import java.util.ArrayList;
 42  
 import java.util.Collections;
 43  
 import java.util.List;
 44  
 import java.util.StringTokenizer;
 45  
 
 46  
 import org.apache.log4j.Logger;
 47  
 import org.argouml.model.DataTypesFactory;
 48  
 import org.omg.uml.foundation.datatypes.ActionExpression;
 49  
 import org.omg.uml.foundation.datatypes.ArgListsExpression;
 50  
 import org.omg.uml.foundation.datatypes.BooleanExpression;
 51  
 import org.omg.uml.foundation.datatypes.Expression;
 52  
 import org.omg.uml.foundation.datatypes.IterationExpression;
 53  
 import org.omg.uml.foundation.datatypes.MappingExpression;
 54  
 import org.omg.uml.foundation.datatypes.Multiplicity;
 55  
 import org.omg.uml.foundation.datatypes.MultiplicityRange;
 56  
 import org.omg.uml.foundation.datatypes.ObjectSetExpression;
 57  
 import org.omg.uml.foundation.datatypes.ProcedureExpression;
 58  
 import org.omg.uml.foundation.datatypes.TimeExpression;
 59  
 import org.omg.uml.foundation.datatypes.TypeExpression;
 60  
 
 61  
 /**
 62  
  * Factory to create UML classes for the UML DataTypes package.
 63  
  * <p>
 64  
  * @since ARGO0.19.5
 65  
  * @author Ludovic Ma&icirc;tre
 66  
  * @author Tom Morris
 67  
  * derived from NSUML implementation by:
 68  
  * @author Thierry Lach
 69  
  */
 70  0
 class DataTypesFactoryMDRImpl extends AbstractUmlModelFactoryMDR
 71  
         implements DataTypesFactory {
 72  
 
 73  900
     private static final Logger LOG = 
 74  
         Logger.getLogger(DataTypesFactoryMDRImpl.class);
 75  
     /**
 76  
      * The model implementation.
 77  
      */
 78  
     private MDRModelImplementation modelImpl;
 79  
 
 80  
     /**
 81  
      * Constructor.
 82  
      * 
 83  
      * @param implementation
 84  
      *            To get other helpers and factories.
 85  
      */
 86  900
     DataTypesFactoryMDRImpl(MDRModelImplementation implementation) {
 87  900
         this.modelImpl = implementation;
 88  900
     }
 89  
 
 90  
 
 91  
     public ActionExpression createActionExpression(String language, String body) {
 92  0
         ActionExpression myActionExpression = modelImpl.getUmlPackage()
 93  
                 .getDataTypes().getActionExpression()
 94  
                 .createActionExpression(language, body);
 95  0
         super.initialize(myActionExpression);
 96  0
         return myActionExpression;
 97  
     }
 98  
 
 99  
 
 100  
     public ArgListsExpression createArgListsExpression(String language, String body) {
 101  0
         ArgListsExpression myArgListsExpression = modelImpl.getUmlPackage()
 102  
                 .getDataTypes().getArgListsExpression()
 103  
                 .createArgListsExpression(language, body);
 104  0
         super.initialize(myArgListsExpression);
 105  0
         return myArgListsExpression;
 106  
     }
 107  
 
 108  
 
 109  
     public BooleanExpression createBooleanExpression(String language, String body) {
 110  0
         BooleanExpression myBooleanExpression = modelImpl.getUmlPackage()
 111  
                 .getDataTypes().getBooleanExpression()
 112  
                 .createBooleanExpression(language, body);
 113  0
         super.initialize(myBooleanExpression);
 114  0
         return myBooleanExpression;
 115  
     }
 116  
 
 117  
 
 118  
     public Expression createExpression(String language, String body) {
 119  0
         Expression myExpression = modelImpl.getUmlPackage().getDataTypes()
 120  
                 .getExpression().createExpression(language, body);
 121  0
         super.initialize(myExpression);
 122  0
         return myExpression;
 123  
     }
 124  
 
 125  
 
 126  
     public IterationExpression createIterationExpression(String language, String body) {
 127  0
         IterationExpression myIterationExpression = modelImpl.getUmlPackage()
 128  
                 .getDataTypes().getIterationExpression()
 129  
                 .createIterationExpression(language, body);
 130  0
         super.initialize(myIterationExpression);
 131  0
         return myIterationExpression;
 132  
     }
 133  
 
 134  
 
 135  
     public MappingExpression createMappingExpression(String language, String body) {
 136  0
         MappingExpression myMappingExpression = modelImpl.getUmlPackage()
 137  
                 .getDataTypes().getMappingExpression().createMappingExpression(
 138  
                         language, body);
 139  0
         super.initialize(myMappingExpression);
 140  0
         return myMappingExpression;
 141  
     }
 142  
 
 143  
 
 144  
     public ObjectSetExpression createObjectSetExpression(String language, String body) {
 145  0
         ObjectSetExpression myObjectSetExpression = modelImpl.getUmlPackage()
 146  
                 .getDataTypes().getObjectSetExpression()
 147  
                 .createObjectSetExpression(language, body);
 148  0
         super.initialize(myObjectSetExpression);
 149  0
         return myObjectSetExpression;
 150  
     }
 151  
 
 152  
 
 153  
     public ProcedureExpression createProcedureExpression(String language, String body) {
 154  0
         ProcedureExpression myProcedureExpression = modelImpl.getUmlPackage()
 155  
                 .getDataTypes().getProcedureExpression()
 156  
                 .createProcedureExpression(language, body);
 157  0
         super.initialize(myProcedureExpression);
 158  0
         return myProcedureExpression;
 159  
     }
 160  
 
 161  
 
 162  
     public TimeExpression createTimeExpression(String language, String body) {
 163  0
         TimeExpression myTimeExpression = modelImpl.getUmlPackage()
 164  
                 .getDataTypes().getTimeExpression()
 165  
                 .createTimeExpression(language, body);
 166  0
         super.initialize(myTimeExpression);
 167  0
         return myTimeExpression;
 168  
     }
 169  
 
 170  
 
 171  
     public TypeExpression createTypeExpression(String language, String body) {
 172  0
         TypeExpression myTypeExpression = modelImpl.getUmlPackage()
 173  
                 .getDataTypes().getTypeExpression()
 174  
                 .createTypeExpression(language, body);
 175  0
         super.initialize(myTypeExpression);
 176  0
         return myTypeExpression;
 177  
     }
 178  
 
 179  
 
 180  
     @Deprecated
 181  
     public Multiplicity createMultiplicity(int lower, int upper) {
 182  0
         return createMultiplicityInternal(lower, upper);
 183  
     }
 184  
 
 185  
     Multiplicity createMultiplicityInternal(int lower, int upper) {
 186  0
         Multiplicity multiplicity = modelImpl.getUmlPackage().getDataTypes()
 187  
                 .getMultiplicity().createMultiplicity();
 188  0
         if (LOG.isDebugEnabled()) {
 189  0
             LOG.debug("Multiplicity created for range " + lower + ".." + upper);
 190  
         }
 191  0
         multiplicity.getRange().add(createMultiplicityRange(lower, upper));
 192  0
         super.initialize(multiplicity);
 193  0
         return multiplicity;
 194  
     }
 195  
     
 196  
     /*
 197  
      * TODO: As currently implemented, this expects a list of
 198  
      * MultiplicityRanges. Is this an interface change from the NSUML
 199  
      * implementation? I suspect it used to accept a list of Integers. - tfm
 200  
      * 
 201  
      * @see org.argouml.model.DataTypesFactory#createMultiplicity(java.util.List)
 202  
      */
 203  
     @Deprecated
 204  
     public Multiplicity createMultiplicity(final List range) {
 205  0
         return createMultiplicityInternal(range);
 206  
     }
 207  
 
 208  
     Multiplicity createMultiplicityInternal(final List<MultiplicityRange> range) {
 209  0
         Multiplicity multiplicity = modelImpl.getUmlPackage().getDataTypes()
 210  
                 .getMultiplicity().createMultiplicity();
 211  0
         if (LOG.isDebugEnabled()) {
 212  0
             LOG.debug("Multiplicity created for list " + range);
 213  
         }
 214  0
         multiplicity.getRange().addAll(range);
 215  0
         super.initialize(multiplicity);
 216  0
         return multiplicity;
 217  
     }
 218  
     
 219  
     @Deprecated
 220  
     public Multiplicity createMultiplicity(final String str) {
 221  0
         return createMultiplicityInternal(str);
 222  
     }
 223  
     
 224  
     Multiplicity createMultiplicityInternal(final String str) {
 225  0
         List<MultiplicityRange> ranges = 
 226  
             Collections.unmodifiableList(parseRanges(str));
 227  0
         return createMultiplicity(ranges);
 228  
     }
 229  
     
 230  
     private List<MultiplicityRange> parseRanges(String str) {
 231  0
         List<MultiplicityRange> rc = new ArrayList<MultiplicityRange>();
 232  
         // Return 1..1 multiplicity for empty string
 233  0
         if ("".equals(str)) {
 234  0
             rc.add(createMultiplicityRange("1..1"));
 235  0
             return rc;
 236  
         }
 237  0
         StringTokenizer stk = new StringTokenizer(str, ",");
 238  0
         while (stk.hasMoreTokens()) {
 239  0
             rc.add(createMultiplicityRange(stk.nextToken()));
 240  
         }
 241  0
         if (rc.size() > 1) {
 242  0
             LOG.debug("UML 2.x does not support multiple multiplicity ranges.  " 
 243  
                     + str);
 244  
         }
 245  0
         return rc;
 246  
     }
 247  
 
 248  
     
 249  
     @Deprecated
 250  
     public MultiplicityRange createMultiplicityRange(String str) {
 251  0
         StringTokenizer stk = new StringTokenizer(str, ". ");
 252  0
         if (!stk.hasMoreTokens()) {
 253  0
             throw new IllegalArgumentException("empty multiplicity range");
 254  
         }
 255  0
         int lower = stringToBound(stk.nextToken());
 256  0
         int upper = 0;
 257  0
         if (!stk.hasMoreTokens()) {
 258  0
             upper = lower;
 259  
             // Convert "*" to "0..*" instead of "*..*"
 260  0
             if (lower == -1) {
 261  0
                 lower = 0;
 262  
             }
 263  
         } else {
 264  0
             upper = stringToBound(stk.nextToken());
 265  0
             if (stk.hasMoreTokens()) {
 266  0
                 throw new IllegalArgumentException(
 267  
                         "illegal range specification");
 268  
             }
 269  
         }
 270  0
         return createMultiplicityRange(lower, upper);
 271  
     }
 272  
     
 273  
 
 274  
     @Deprecated
 275  
     public MultiplicityRange createMultiplicityRange(final int lower, 
 276  
             final int upper) {
 277  0
         MultiplicityRange range = 
 278  
             modelImpl.getUmlPackage().getDataTypes().getMultiplicityRange()
 279  
                 .createMultiplicityRange(lower, upper);
 280  0
         return range;
 281  
     }
 282  
 
 283  
     /**
 284  
      * Convert an integer to a string using MultiplicityRange notation.
 285  
      * 
 286  
      * @param i integer to convert
 287  
      * @return String version of integer or "*" for unlimited (-1)
 288  
      */
 289  
     static String boundToString(int i) {
 290  0
         if (i == -1) {
 291  0
             return "*";
 292  
         } else {
 293  0
             return "" + i;
 294  
         }
 295  
     }
 296  
 
 297  
     /**
 298  
      * Convert a MultiplicityRange bound string to an integer.
 299  
      * 
 300  
      * @param b String containing a single MultiplicityRange bound
 301  
      * @return integer representation
 302  
      */
 303  
     private static int stringToBound(String b) {
 304  
         try {
 305  0
             if (b.equals("n") || b.equals("*")) {
 306  0
                 return -1;
 307  
             } else {
 308  0
                 return Integer.parseInt(b);
 309  
             }
 310  0
         } catch (Exception ex) {
 311  0
             throw new IllegalArgumentException("illegal range bound : "
 312  
                     + (b == null ? "null" : b));
 313  
         }
 314  
     }
 315  
 }