001/*
002 * Copyright c 2018 Rusi Popov, MDA Tools.net All rights reserved.
003 *
004 * This program and the accompanying materials are made available under the terms of the
005 * Eclipse Public License v2.0 which accompanies this distribution, and is available at
006 * http://www.eclipse.org/legal/epl-v20.html
007 */
008package net.mdatools.modelant.core.name;
009
010import javax.jmi.model.Namespace;
011import javax.jmi.reflect.InvalidNameException;
012import javax.jmi.reflect.JmiException;
013import javax.jmi.reflect.RefPackage;
014
015import net.mdatools.modelant.core.api.name.Name;
016import net.mdatools.modelant.core.api.name.PackageName;
017
018/**
019 * A key in package mapping
020 * @author Rusi Popov (popovr@mdatools.net)
021 */
022public class PackageNameImpl extends NameImpl<PackageName> implements PackageName {
023  public PackageNameImpl(String packageName) {
024    super(packageName);
025  }
026
027  public PackageNameImpl(PackageName parentName, String packageName) {
028    super(parentName, packageName);
029  }
030
031  public PackageNameImpl(Namespace container) {
032    super(container.getContainer() == null ? null: new PackageNameImpl(container.getContainer()),
033          container.getName());
034  }
035
036
037  /**
038   * @param qualifiedName not null
039   * @return the Package Name that represents the qualified name provided,
040   * @throws IllegalArgumentException when qualifiedName is empty
041   */
042  public static PackageName parseQualifiedPackageName(String qualifiedName) throws IllegalArgumentException {
043    PackageNameImpl result;
044    String[] names;
045
046    result = null;
047    names = qualifiedName.split( METAMODEL_PATH_SEPARATOR_PARSE );
048    for (String name:names) {
049      result = new PackageNameImpl( result, name );
050    }
051    return result;
052  }
053
054  /**
055   * @param rootPackage the extent or another instance of a package proxy (class) to start navigating form
056   * @return the metamodel package this describes, starting from the rootPackage extent
057   * @throws JmiException
058   * @see net.mdatools.modelant.core.api.name.PackageName#getMetaPackage(javax.jmi.reflect.RefPackage)
059   */
060  public RefPackage getMetaPackage(RefPackage rootPackage) throws JmiException {
061    RefPackage result;
062
063    assert rootPackage != null : "Expected a non-null package";
064
065    if ( getOwner() == null ) {
066      result = rootPackage;
067    } else {
068      result = getOwner().getMetaPackage( rootPackage );
069    }
070
071    try {
072      result = result.refPackage( getName() );
073    } catch (JmiException ex) {
074      throw new IllegalArgumentException("Looking up the package "+ this
075                                       + " reached " + PRINT_MODEL_ELEMENT.execute( result )
076                                       + " for which retrieving the nested package '"+getName()+"'"
077                                       + " caused ",ex);
078    }
079    return result;
080  }
081
082  /**
083   * @see net.mdatools.modelant.core.api.name.Name#constructName(net.mdatools.modelant.core.api.name.Name, java.lang.String)
084   */
085  public Name<PackageName> constructName(PackageName parent, String name) {
086    return new PackageNameImpl(parent, name);
087  }
088}