Coverage Report - org.argouml.uml.generator.GeneratorManager
 
Classes in this File Line Coverage Branch Coverage Complexity
GeneratorManager
16%
8/50
0%
0/24
2.385
 
 1  
 /* $Id: GeneratorManager.java 17868 2010-01-12 20:47:51Z 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  
  *    tfmorris
 11  
  *****************************************************************************
 12  
  *
 13  
  * Some portions of this file was previously release using the BSD License:
 14  
  */
 15  
 
 16  
 // Copyright (c) 2005-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.uml.generator;
 40  
 
 41  
 import java.util.HashMap;
 42  
 import java.util.Iterator;
 43  
 import java.util.Map;
 44  
 import java.util.Set;
 45  
 
 46  
 import org.apache.log4j.Logger;
 47  
 import org.argouml.application.events.ArgoEventPump;
 48  
 import org.argouml.application.events.ArgoEventTypes;
 49  
 import org.argouml.application.events.ArgoGeneratorEvent;
 50  
 import org.argouml.model.Model;
 51  
 import org.argouml.uml.reveng.ImportInterface;
 52  
 
 53  
 /**
 54  
  * Keeps an instance of each CodeGenerator implementation module registered,
 55  
  * associated with a language name. Also remembers the currently selected
 56  
  * language. GeneratorManager is a singleton.
 57  
  */
 58  
 public final class GeneratorManager {
 59  
     /**
 60  
      * Logger.
 61  
      */
 62  900
     private static final Logger LOG =
 63  
         Logger.getLogger(GeneratorManager.class);
 64  
 
 65  
     /**
 66  
      * The instance.
 67  
      */
 68  900
     private static final GeneratorManager INSTANCE =
 69  
         new GeneratorManager();
 70  
 
 71  
     /**
 72  
      * @return The singleton instance of the generator manager.
 73  
      */
 74  
     public static GeneratorManager getInstance() {
 75  941
         return INSTANCE;
 76  
     }
 77  
 
 78  900
     private Map<Language, CodeGenerator> generators = 
 79  
         new HashMap<Language, CodeGenerator>();
 80  
 
 81  900
     private Language currLanguage = null;
 82  
 
 83  
     /**
 84  
      * The constructor.
 85  
      */
 86  900
     private GeneratorManager() {
 87  
         // private constructor to enforce singleton
 88  900
     }
 89  
 
 90  
     /**
 91  
      * Registers a new generator. If a generator with the same language is
 92  
      * already registered, it's replaced by the new one.
 93  
      *
 94  
      * @param lang The language.
 95  
      * @param gen The CodeGenerator object to register.
 96  
      */
 97  
     public void addGenerator(Language lang, CodeGenerator gen) {
 98  0
         if (currLanguage == null) {
 99  0
             currLanguage = lang;
 100  
         }
 101  0
         generators.put(lang, gen);
 102  0
         ArgoEventPump.fireEvent(
 103  
                 new ArgoGeneratorEvent(ArgoEventTypes.GENERATOR_ADDED, gen));
 104  0
         LOG.debug("Added generator " + gen + " for " + lang);
 105  0
     }
 106  
 
 107  
     /**
 108  
      * Removes a generator. If no generator with that name is registered,
 109  
      * nothing is done.
 110  
      *
 111  
      * @param lang The language. Shall not be null.
 112  
      * @return The old generator being removed or null.
 113  
      */
 114  
     public CodeGenerator removeGenerator(Language lang) {
 115  0
         CodeGenerator old = generators.remove(lang);
 116  0
         if (lang.equals(currLanguage)) {
 117  0
             Iterator it = generators.keySet().iterator();
 118  0
             if (it.hasNext()) {
 119  0
                 currLanguage = (Language) it.next();
 120  
             } else {
 121  0
                 currLanguage = null;
 122  
             }
 123  
         }
 124  0
         if (old != null) {
 125  0
             ArgoEventPump.fireEvent(
 126  
                     new ArgoGeneratorEvent(
 127  
                             ArgoEventTypes.GENERATOR_REMOVED, old));
 128  
         }
 129  0
         LOG.debug("Removed generator " + old + " for " + lang);
 130  0
         return old;
 131  
     }
 132  
 
 133  
     /**
 134  
      * Removes the generator associated with the specified language.
 135  
      * @param name The language name.
 136  
      * @return The old generator, or null if there wasn't any.
 137  
      */
 138  
     public CodeGenerator removeGenerator(String name) {
 139  0
         Language lang = findLanguage(name);
 140  0
         if (lang != null) {
 141  0
             return removeGenerator(lang);
 142  
         }
 143  0
         return null;
 144  
     }
 145  
 
 146  
     /**
 147  
      * Access method that finds the correct generator based on a name.
 148  
      *
 149  
      * @param lang The language.
 150  
      * @return a CodeGenerator (or <code>null</code> if not found).
 151  
      */
 152  
     public CodeGenerator getGenerator(Language lang) {
 153  0
         if (lang == null) {
 154  0
             return null;
 155  
         }
 156  0
         return generators.get(lang);
 157  
     }
 158  
 
 159  
     /**
 160  
      * @param name The name of the language,
 161  
      * @return a CodeGenerator (or <code>null</code> if not found).
 162  
      */
 163  
     public CodeGenerator getGenerator(String name) {
 164  0
         Language lang = findLanguage(name);
 165  0
         return getGenerator(lang);
 166  
     }
 167  
 
 168  
     /**
 169  
      * @return the current language name, or <code>null</code> if there are no
 170  
      * generator registered at all.
 171  
      */
 172  
     public Language getCurrLanguage() {
 173  0
         return currLanguage;
 174  
     }
 175  
 
 176  
     /**
 177  
      * @return the current generator, or <code>null</code> if there are no
 178  
      * generator registered at all.
 179  
      */
 180  
     public CodeGenerator getCurrGenerator() {
 181  0
         return currLanguage == null ? null : getGenerator(currLanguage);
 182  
     }
 183  
 
 184  
     /**
 185  
      * @return A copy of the map of the generators. The map
 186  
      * keys Language objects, and values are CodeGenerator objects.
 187  
      */
 188  
     public Map<Language, CodeGenerator> getGenerators() {
 189  0
         Object  clone = ((HashMap<Language, CodeGenerator>) generators).clone();
 190  0
         return (Map<Language, CodeGenerator>) clone;
 191  
     }
 192  
 
 193  
     /**
 194  
      * @return A copy of the Set of the languages.
 195  
      */
 196  
     public Set<Language> getLanguages() {
 197  941
         return generators.keySet();
 198  
     }
 199  
 
 200  
     /**
 201  
      * Find a language by name from the available ones.
 202  
      * @param name The name of the language
 203  
      * @return The language with the specified name, or null if it
 204  
      * doesn't exist.
 205  
      */
 206  
     public Language findLanguage(String name) {
 207  0
         for (Language lang : getLanguages()) {
 208  0
             if (lang.getName().equals(name)) {
 209  0
                 return lang;
 210  
             }
 211  
         }
 212  0
         return null;
 213  
     }
 214  
 
 215  
     // some convenience methods
 216  
 
 217  
     /**
 218  
      * Gets the path of the code base for a model element.<p>
 219  
      * If empty or not existing return <code>null</code>.
 220  
      *
 221  
      * @param me The model element
 222  
      * @return String representation of SOURCE_PATH_TAG tagged value.
 223  
      */
 224  
     public static String getCodePath(Object me) {
 225  0
         if (me == null) {
 226  0
             return null;
 227  
         }
 228  
 
 229  0
         Object taggedValue = Model.getFacade().getTaggedValue(me,
 230  
                 ImportInterface.SOURCE_PATH_TAG);
 231  
         String s;
 232  0
         if (taggedValue == null) {
 233  0
             return null;
 234  
         }
 235  0
         s =  Model.getFacade().getValueOfTag(taggedValue);
 236  0
         if (s != null) {
 237  0
             return s.trim();
 238  
         }
 239  0
         return null;
 240  
     }
 241  
 
 242  
 }