Coverage Report - org.argouml.notation.providers.uml.AssociationEndNameNotationUml
 
Classes in this File Line Coverage Branch Coverage Complexity
AssociationEndNameNotationUml
0%
0/65
0%
0/42
5.833
 
 1  
 /* $Id: AssociationEndNameNotationUml.java 18615 2010-08-03 08:23:26Z mvw $
 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  
  *    mvw
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2006-2009 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.notation.providers.uml;
 40  
 
 41  
 import java.text.ParseException;
 42  
 import java.util.NoSuchElementException;
 43  
 
 44  
 import org.argouml.application.events.ArgoEventPump;
 45  
 import org.argouml.application.events.ArgoEventTypes;
 46  
 import org.argouml.application.events.ArgoHelpEvent;
 47  
 import org.argouml.i18n.Translator;
 48  
 import org.argouml.model.Model;
 49  
 import org.argouml.notation.NotationSettings;
 50  
 import org.argouml.notation.providers.AssociationEndNameNotation;
 51  
 import org.argouml.uml.StereotypeUtility;
 52  
 import org.argouml.util.MyTokenizer;
 53  
 
 54  
 /**
 55  
  * The UML notation for an association-end name (i.e. the  role). <p>
 56  
  * 
 57  
  * This notation supports the association end name, 
 58  
  * visibility, and stereotypes. <p>
 59  
  * 
 60  
  * There is no support for the interface specifier
 61  
  * (that maps to the "specification" of an AssociationEnd). <p>
 62  
  * 
 63  
  * This is the only notation (that I'm aware of) that requires state.
 64  
  * All others could become effectively singletons. - Bob.
 65  
  * 
 66  
  * @author michiel
 67  
  */
 68  
 public class AssociationEndNameNotationUml extends AssociationEndNameNotation {
 69  
         
 70  
     /**
 71  
      * The constructor.
 72  
      * 
 73  
      * @param associationEnd the UML element
 74  
      */
 75  
     public AssociationEndNameNotationUml(Object associationEnd) {
 76  0
         super(associationEnd);
 77  0
     }
 78  
 
 79  
     /*
 80  
      * @see org.argouml.notation.providers.NotationProvider#getParsingHelp()
 81  
      */
 82  
     public String getParsingHelp() {
 83  0
         return "parsing.help.fig-association-end-name";
 84  
     }
 85  
 
 86  
     /*
 87  
      * @see org.argouml.notation.providers.NotationProvider#parse(java.lang.Object, java.lang.String)
 88  
      */
 89  
     public void parse(Object modelElement, String text) {
 90  
         try {
 91  0
             parseAssociationEnd(modelElement, text);
 92  0
         } catch (ParseException pe) {
 93  0
             String msg = "statusmsg.bar.error.parsing.association-end-name";
 94  0
             Object[] args = {
 95  
                 pe.getLocalizedMessage(),
 96  
                 Integer.valueOf(pe.getErrorOffset()),
 97  
             };
 98  0
             ArgoEventPump.fireEvent(new ArgoHelpEvent(
 99  
                     ArgoEventTypes.HELP_CHANGED, this,
 100  
                 Translator.messageFormat(msg, args)));
 101  0
         }
 102  0
     }
 103  
 
 104  
     /**
 105  
      * @param role   The AssociationEnd <em>text</em> describes.
 106  
      * @param text A String on the above format.
 107  
      * @throws ParseException
 108  
      *             when it detects an error in the role string. See also
 109  
      *             ParseError.getErrorOffset().
 110  
      */
 111  
     protected void parseAssociationEnd(Object role, String text)
 112  
         throws ParseException {
 113  
         MyTokenizer st;
 114  
 
 115  0
         String name = null;
 116  0
         StringBuilder stereotype = null;
 117  
         String token;
 118  
 
 119  
         try {
 120  0
             st = new MyTokenizer(text, "<<,\u00AB,\u00BB,>>");
 121  0
             while (st.hasMoreTokens()) {
 122  0
                 token = st.nextToken();
 123  
 
 124  0
                 if ("<<".equals(token) || "\u00AB".equals(token)) {
 125  0
                     if (stereotype != null) {
 126  0
                             String msg = 
 127  
                             "parsing.error.association-name.twin-stereotypes";
 128  0
                         throw new ParseException(Translator.localize(msg), 
 129  
                                         st.getTokenIndex());
 130  
                     }
 131  
 
 132  0
                     stereotype = new StringBuilder();
 133  
                     while (true) {
 134  0
                         token = st.nextToken();
 135  0
                         if (">>".equals(token) || "\u00BB".equals(token)) {
 136  0
                             break;
 137  
                         }
 138  0
                         stereotype.append(token);
 139  
                     }
 140  
                 } else {
 141  0
                     if (name != null) {
 142  0
                             String msg = 
 143  
                                     "parsing.error.association-name.twin-names";
 144  0
                         throw new ParseException(Translator.localize(msg),
 145  
                                 st.getTokenIndex());
 146  
                     }
 147  0
                     name = token;
 148  
                 }
 149  
             }
 150  0
         } catch (NoSuchElementException nsee) {
 151  0
                 String ms = "parsing.error.association-name.unexpected-end-element";
 152  0
             throw new ParseException(Translator.localize(ms),
 153  
                     text.length());
 154  0
         } catch (ParseException pre) {
 155  0
             throw pre;
 156  0
         }
 157  
 
 158  0
         if (name != null) {
 159  0
             name = name.trim();
 160  
         }
 161  
 
 162  0
         if (name != null && name.startsWith("+")) {
 163  0
             name = name.substring(1).trim();
 164  0
             Model.getCoreHelper().setVisibility(role,
 165  
                             Model.getVisibilityKind().getPublic());
 166  
         }
 167  0
         if (name != null && name.startsWith("-")) {
 168  0
             name = name.substring(1).trim();
 169  0
             Model.getCoreHelper().setVisibility(role,
 170  
                             Model.getVisibilityKind().getPrivate());
 171  
         }
 172  0
         if (name != null && name.startsWith("#")) {
 173  0
             name = name.substring(1).trim();
 174  0
             Model.getCoreHelper().setVisibility(role,
 175  
                             Model.getVisibilityKind().getProtected());
 176  
         }
 177  0
         if (name != null && name.startsWith("~")) {
 178  0
             name = name.substring(1).trim();
 179  0
             Model.getCoreHelper().setVisibility(role,
 180  
                             Model.getVisibilityKind().getPackage());
 181  
         }
 182  0
         if (name != null) {
 183  0
             Model.getCoreHelper().setName(role, name);
 184  
         }
 185  
 
 186  0
         StereotypeUtility.dealWithStereotypes(role, stereotype, true);
 187  0
     }
 188  
 
 189  
     private String toString(Object modelElement, boolean showVisibility,
 190  
             boolean useGuillemets) {
 191  0
         String name = Model.getFacade().getName(modelElement);
 192  0
         if (name == null) {
 193  0
             name = "";
 194  
         }
 195  
 
 196  0
         String visibility = "";
 197  0
         if (showVisibility) {
 198  0
             visibility = NotationUtilityUml.generateVisibility2(modelElement);
 199  
 
 200  0
             if (name.length() < 1) {
 201  0
                 visibility = "";
 202  
                 // this is the temporary solution for issue 1011
 203  
             }
 204  
         }
 205  
 
 206  0
         String stereoString = 
 207  
             NotationUtilityUml.generateStereotype(modelElement, useGuillemets);
 208  
 
 209  0
         if (stereoString.length() > 0) {
 210  0
             stereoString += " ";
 211  
         }
 212  
 
 213  0
         return stereoString + visibility + name;
 214  
     }
 215  
 
 216  
     @Override
 217  
     public String toString(Object modelElement, NotationSettings settings) {
 218  0
         return toString(modelElement, settings.isShowVisibilities(), 
 219  
                 settings.isUseGuillemets());
 220  
     }
 221  
 
 222  
 }