Coverage Report - org.argouml.notation.providers.java.ModelElementNameNotationJava
 
Classes in this File Line Coverage Branch Coverage Complexity
ModelElementNameNotationJava
0%
0/91
0%
0/64
8.333
 
 1  
 /* $Id: ModelElementNameNotationJava.java 17827 2010-01-12 18:52:31Z linus $
 2  
  *****************************************************************************
 3  
  * Copyright (c) 2009 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) 2005-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.java;
 40  
 
 41  
 import java.text.ParseException;
 42  
 import java.util.ArrayList;
 43  
 import java.util.List;
 44  
 import java.util.NoSuchElementException;
 45  
 
 46  
 import org.argouml.application.events.ArgoEventPump;
 47  
 import org.argouml.application.events.ArgoEventTypes;
 48  
 import org.argouml.application.events.ArgoHelpEvent;
 49  
 import org.argouml.i18n.Translator;
 50  
 import org.argouml.model.Model;
 51  
 import org.argouml.notation.NotationSettings;
 52  
 import org.argouml.notation.providers.ModelElementNameNotation;
 53  
 import org.argouml.util.MyTokenizer;
 54  
 
 55  
 /**
 56  
  * Java notation for the name of a modelelement.
 57  
  * 
 58  
  * @author Michiel
 59  
  */
 60  
 public class ModelElementNameNotationJava extends ModelElementNameNotation {
 61  
 
 62  
     /**
 63  
      * The constructor.
 64  
      *
 65  
      * @param name the modelelement
 66  
      */
 67  
     public ModelElementNameNotationJava(Object name) {
 68  0
         super(name);
 69  0
     }
 70  
 
 71  
     /*
 72  
      * @see org.argouml.notation.providers.NotationProvider#parse(
 73  
      * java.lang.Object, java.lang.String)
 74  
      */
 75  
     public void parse(Object modelElement, String text) {
 76  
         try {
 77  0
             parseModelElement(modelElement, text);
 78  0
         } catch (ParseException pe) {
 79  0
             String msg = "statusmsg.bar.error.parsing.node-modelelement";
 80  0
             Object[] args = {
 81  
                 pe.getLocalizedMessage(),
 82  
                 Integer.valueOf(pe.getErrorOffset()),
 83  
             };
 84  0
             ArgoEventPump.fireEvent(new ArgoHelpEvent(
 85  
                     ArgoEventTypes.HELP_CHANGED, this,
 86  
                 Translator.messageFormat(msg, args)));
 87  0
         }
 88  0
     }
 89  
 
 90  
     /**
 91  
      * @param modelElement the UML modelelement
 92  
      * @param text the string to parse
 93  
      */
 94  
     static void parseModelElement(Object modelElement, String text) 
 95  
         throws ParseException {
 96  
         MyTokenizer st;
 97  
 
 98  0
         boolean abstrac = false;
 99  0
         boolean fina = false;
 100  0
         boolean publi = false;
 101  0
         boolean privat = false;
 102  0
         boolean protect = false;
 103  
         String token;
 104  0
         List<String> path = null;
 105  0
         String name = null;
 106  
 
 107  
         try {
 108  0
             st = new MyTokenizer(text, 
 109  
                     " ,.,abstract,final,public,private,protected");
 110  0
             while (st.hasMoreTokens()) {
 111  0
                 token = st.nextToken();
 112  
 
 113  0
                 if (" ".equals(token)) {
 114  
                     /* skip spaces */
 115  0
                 } else if ("abstract".equals(token)) {
 116  0
                     abstrac = true;
 117  0
                 } else if ("final".equals(token)) {
 118  0
                     fina = true;
 119  0
                 } else if ("public".equals(token)) {
 120  0
                     publi = true;
 121  0
                 } else if ("private".equals(token)) {
 122  0
                     privat = true;
 123  0
                 } else if ("protected".equals(token)) {
 124  0
                     protect = true;
 125  0
                 } else if (".".equals(token)) {
 126  0
                     if (name != null) {
 127  0
                         name = name.trim();
 128  
                     }
 129  
 
 130  0
                     if (path != null && (name == null || "".equals(name))) {
 131  0
                         String msg = 
 132  
                             "parsing.error.model-element-name.anon-qualifiers";
 133  0
                         throw new ParseException(Translator.localize(msg), 
 134  
                                 st.getTokenIndex());
 135  
                     }
 136  
 
 137  0
                     if (path == null) {
 138  0
                         path = new ArrayList<String>();
 139  
                     }
 140  0
                     if (name != null) {
 141  0
                         path.add(name);
 142  
                     }
 143  0
                     name = null;
 144  
                 
 145  
                 } else { // the name itself
 146  0
                     if (name != null) {
 147  0
                         String msg = 
 148  
                             "parsing.error.model-element-name.twin-names";
 149  0
                         throw new ParseException(Translator.localize(msg), 
 150  
                                 st.getTokenIndex());
 151  
                     }
 152  0
                     name = token;
 153  
                 }
 154  
             }
 155  0
         } catch (NoSuchElementException nsee) {
 156  0
             String msg = 
 157  
                 "parsing.error.model-element-name.unexpected-name-element";
 158  0
             throw new ParseException(Translator.localize(msg),
 159  
                     text.length());
 160  0
         } catch (ParseException pre) {
 161  0
             throw pre;
 162  0
         }
 163  
         
 164  0
         if (name != null) {
 165  0
             name = name.trim();
 166  
         }
 167  
         
 168  0
         if (path != null && (name == null || "".equals(name))) {
 169  0
             String msg = "parsing.error.model-element-name.must-end-with-name";
 170  0
             throw new ParseException(Translator.localize(msg), 0);
 171  
         }
 172  
         
 173  
         /* Check the name for validity: */
 174  0
         if (!isValidJavaClassName(name)) {
 175  0
             throw new ParseException(
 176  
                     "Invalid class name for Java: "
 177  
                     + name, 0);
 178  
         }
 179  
         
 180  0
         if (path != null) {
 181  0
             Object nspe =
 182  
                 Model.getModelManagementHelper().getElement(
 183  
                     path,
 184  
                     Model.getFacade().getRoot(modelElement));
 185  
 
 186  0
             if (nspe == null || !(Model.getFacade().isANamespace(nspe))) {
 187  0
                 String msg = 
 188  
                         "parsing.error.model-element-name.namespace-unresolved";
 189  0
                 throw new ParseException(Translator.localize(msg), 
 190  
                         0);
 191  
             }
 192  0
             if (!Model.getCoreHelper().isValidNamespace(
 193  
                     modelElement, nspe)) {
 194  0
                 String msg = 
 195  
                         "parsing.error.model-element-name.namespace-invalid";
 196  0
                 throw new ParseException(Translator.localize(msg), 
 197  
                         0);
 198  
             }
 199  0
             Model.getCoreHelper().addOwnedElement(nspe, modelElement);
 200  
         }
 201  
 
 202  0
         Model.getCoreHelper().setName(modelElement, name);
 203  
         
 204  0
         if (abstrac) {
 205  0
             Model.getCoreHelper().setAbstract(modelElement, abstrac);
 206  
         }
 207  0
         if (fina) {
 208  0
             Model.getCoreHelper().setLeaf(modelElement, fina);
 209  
         }
 210  0
         if (publi) {
 211  0
             Model.getCoreHelper().setVisibility(modelElement,
 212  
                 Model.getVisibilityKind().getPublic());
 213  
         }
 214  0
         if (privat) {
 215  0
             Model.getCoreHelper().setVisibility(modelElement,
 216  
                 Model.getVisibilityKind().getPrivate());
 217  
         }
 218  0
         if (protect) {
 219  0
             Model.getCoreHelper().setVisibility(modelElement,
 220  
                 Model.getVisibilityKind().getProtected());
 221  
         }
 222  0
     }
 223  
 
 224  
     /**
 225  
      * @param name the name of the element
 226  
      * @return true if the given name is a valid name according java syntax
 227  
      */
 228  
     private static boolean isValidJavaClassName(String name) {
 229  
         /* TODO: Check the name for validity. */
 230  0
         return true;
 231  
     }
 232  
 
 233  
     /*
 234  
      * @see org.argouml.notation.providers.NotationProvider#getParsingHelp()
 235  
      */
 236  
     public String getParsingHelp() {
 237  0
         return "parsing.help.java.fig-nodemodelelement";
 238  
     }
 239  
 
 240  
     public String toString(Object modelElement, NotationSettings settings) {
 241  
         String name;
 242  0
         name = Model.getFacade().getName(modelElement);
 243  0
         if (name == null) {
 244  0
             return "";
 245  
         }
 246  0
         String visibility = "";
 247  0
         if (settings.isShowVisibilities()) {
 248  0
             visibility = NotationUtilityJava.generateVisibility(modelElement);
 249  
         }
 250  0
         String path = "";
 251  0
         if (settings.isShowPaths()) {
 252  0
             path = NotationUtilityJava.generatePath(modelElement);
 253  
         }
 254  0
         return NotationUtilityJava.generateLeaf(modelElement)
 255  
             + NotationUtilityJava.generateAbstract(modelElement)
 256  
             + visibility
 257  
             + path
 258  
             + name;
 259  
     }
 260  
 
 261  
 }