PackageContext.java

  1. /* $Id$
  2.  *****************************************************************************
  3.  * Copyright (c) 2009-2012 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. // Copyright (c) 2003-2007 The Regents of the University of California. All
  16. // Rights Reserved. Permission to use, copy, modify, and distribute this
  17. // software and its documentation without fee, and without a written
  18. // agreement is hereby granted, provided that the above copyright notice
  19. // and this paragraph appear in all copies.  This software program and
  20. // documentation are copyrighted by The Regents of the University of
  21. // California. The software program and documentation are supplied "AS
  22. // IS", without any accompanying services from The Regents. The Regents
  23. // does not warrant that the operation of the program will be
  24. // uninterrupted or error-free. The end-user understands that the program
  25. // was developed for research purposes and is advised not to rely
  26. // exclusively on the program for any reason.  IN NO EVENT SHALL THE
  27. // UNIVERSITY OF CALIFORNIA BE LIABLE TO ANY PARTY FOR DIRECT, INDIRECT,
  28. // SPECIAL, INCIDENTAL, OR CONSEQUENTIAL DAMAGES, INCLUDING LOST PROFITS,
  29. // ARISING OUT OF THE USE OF THIS SOFTWARE AND ITS DOCUMENTATION, EVEN IF
  30. // THE UNIVERSITY OF CALIFORNIA HAS BEEN ADVISED OF THE POSSIBILITY OF
  31. // SUCH DAMAGE. THE UNIVERSITY OF CALIFORNIA SPECIFICALLY DISCLAIMS ANY
  32. // WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  33. // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE SOFTWARE
  34. // PROVIDED HEREUNDER IS ON AN "AS IS" BASIS, AND THE UNIVERSITY OF
  35. // CALIFORNIA HAS NO OBLIGATIONS TO PROVIDE MAINTENANCE, SUPPORT,
  36. // UPDATES, ENHANCEMENTS, OR MODIFICATIONS.

  37. package org.argouml.uml.reveng.idl;

  38. import java.net.MalformedURLException;
  39. import java.util.logging.Level;
  40. import java.util.logging.Logger;

  41. import org.argouml.model.Facade;
  42. import org.argouml.model.Model;
  43. import org.argouml.uml.reveng.ImportClassLoader;

  44. /**
  45.  * This context is a package.
  46.  *
  47.  * @author Marcus Andersson
  48.  */
  49. class PackageContext extends Context {
  50.    
  51.     private static final Logger LOG =
  52.         Logger.getLogger(PackageContext.class.getName());
  53.    
  54.     /** The package this context represents. */
  55.     private Object mPackage;

  56.     /** The java style name of the package. */
  57.     private String javaName;

  58.     /**
  59.        Create a new context from a package.

  60.        @param base Based on this context.
  61.        @param thePackage Represents this package.
  62.     */
  63.     public PackageContext(Context base, Object thePackage) {
  64.     super(base);
  65.     this.mPackage = thePackage;
  66.     javaName = getJavaName(thePackage);
  67.     }

  68.     public Object getInterface(String name)
  69.     throws ClassifierNotFoundException {
  70.         return get(name, true);
  71.     }

  72.     /**
  73.      * Get a classifier from the model. If it is not in the model, try
  74.      * to find it with the CLASSPATH. If found, in the classpath, the
  75.      * classifier is created and added to the model. If not found at
  76.      * all, a datatype is created and added to the model.
  77.      *
  78.      * @param name The name of the classifier to find.
  79.      * @return Found classifier.
  80.      * @throws ClassifierNotFoundException if classifier couldn't be located
  81.      */
  82.     public Object get(String name)
  83.         throws ClassifierNotFoundException {
  84.         return get(name, false);
  85.     }
  86.    
  87.     /**
  88.      * Get a classifier from the model. If it is not in the model, try
  89.      * to find it with the CLASSPATH. If found, in the classpath, the
  90.      * classifier is created and added to the model. If not found at
  91.      * all, a datatype is created and added to the model.
  92.      *
  93.      * @param name The name of the classifier to find.
  94.      * @return Found classifier.
  95.      * @throws ClassifierNotFoundException if classifier couldn't be located
  96.      */
  97.     public Object get(String name, boolean interfacesOnly)
  98.     throws ClassifierNotFoundException {
  99.     // Search in model
  100.     Object mClassifier = Model.getFacade().lookupIn(mPackage, name);

  101.     if (mClassifier == null) {
  102.         Class classifier;
  103.         // Try to find it via the classpath
  104.         try {

  105.         // Special case for model
  106.         if (Model.getFacade().isAModel(mPackage)) {
  107.             classifier = Class.forName(name);
  108.         }
  109.         else {
  110.                     String clazzName = javaName + "." + name;
  111.                     classifier = Class.forName(clazzName);
  112.         }
  113.         if (classifier.isInterface()) {
  114.             mClassifier =
  115.                             Model.getCoreFactory().buildInterface(
  116.                                     name, mPackage);
  117.         } else {
  118.                     if (!interfacesOnly) {
  119.                         mClassifier =
  120.                                 Model.getCoreFactory().buildClass(
  121.                                         name, mPackage);
  122.                     }
  123.         }
  124.                 if (mClassifier != null) {
  125.                     setGeneratedTag(mClassifier);
  126.                 }
  127.         }
  128.         catch (ClassNotFoundException e) {
  129.         // No class or interface found
  130.                 // try USER classpath

  131.                 try {
  132.                     // Special case for model
  133.                     if (Model.getFacade().isAModel(mPackage)) {
  134.                         classifier =
  135.                 ImportClassLoader.getInstance().loadClass(name);
  136.                     }
  137.                     else {
  138.                         String clazzName = javaName + "." + name;
  139.                         classifier =
  140.                 ImportClassLoader.getInstance()
  141.                     .loadClass(clazzName);
  142.                     }
  143.             if (classifier.isInterface()) {
  144.             mClassifier =
  145.                                 Model.getCoreFactory().buildInterface(
  146.                                         name, mPackage);
  147.             } else {
  148.                 if (!interfacesOnly) {
  149.                             mClassifier =
  150.                                     Model.getCoreFactory().buildClass(
  151.                                             name, mPackage);
  152.                         }
  153.             }
  154.                     if (mClassifier != null) {
  155.                         setGeneratedTag(mClassifier);
  156.                     }
  157.                 }
  158.                 catch (ClassNotFoundException e1) {
  159.                     // Ignore - we'll deal with this later by checking to see
  160.                     // if we found anything.
  161.                 } catch (MalformedURLException e1) {
  162.                     LOG.log(Level.WARNING,
  163.                             "Classpath configuration error", e1);
  164.                 }
  165.             }
  166.     }
  167.     if (mClassifier == null) {
  168.         // Continue the search through the rest of the model
  169.         if (getContext() != null) {
  170.         mClassifier = getContext().get(name, interfacesOnly);
  171.         } else {
  172.         // Check for java data types
  173.             if (!interfacesOnly
  174.                     && name.equals("int")
  175.                     || name.equals("long")
  176.                     || name.equals("short")
  177.                     || name.equals("byte")
  178.                     || name.equals("char")
  179.                     || name.equals("float")
  180.                     || name.equals("double")
  181.                     || name.equals("boolean")
  182.                     || name.equals("void")
  183.                     // How do I represent arrays in UML?
  184.                     || name.indexOf("[]") != -1) {
  185.             mClassifier =
  186.             Model.getCoreFactory()
  187.                 .buildDataType(name, mPackage);
  188.         }
  189.         }
  190.     }
  191.     if (mClassifier == null) {
  192.         throw new ClassifierNotFoundException(name);
  193.     }

  194.     return mClassifier;
  195.     }

  196.     // Historically this used the value "yes", but all existing
  197.     // code only checks for the presence of the tag, not its value
  198.     private static final String GENERATED_TAG_VALUE = "true";
  199.    
  200.     /**
  201.      * Set the tagged value which indicates this element was
  202.      * generated as a result of reverse engineering.
  203.      *
  204.      * @param element the ModelElement to set the tag on
  205.      */
  206.     private void setGeneratedTag(Object element) {
  207.         Object tv =
  208.                 Model.getFacade().getTaggedValue(element, Facade.GENERATED_TAG);
  209.         if (tv == null) {
  210.             Model.getExtensionMechanismsHelper().addTaggedValue(
  211.                     element,
  212.                     Model.getExtensionMechanismsFactory().buildTaggedValue(
  213.                             Facade.GENERATED_TAG, GENERATED_TAG_VALUE));
  214.         } else {
  215.             Model.getExtensionMechanismsHelper().setValueOfTag(
  216.                     tv, GENERATED_TAG_VALUE);
  217.         }
  218.     }
  219. }