/*******************************************************************************
 * Copyright (c) 2011-2015 The University of York.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Contributors:
 *     Konstantinos Barmpis - initial API and implementation
 ******************************************************************************/
package org.hawk.modelio.model.util;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.hawk.core.model.IHawkObject;
import org.hawk.modelio.exml.metamodel.ModelioClass;
import org.hawk.modelio.exml.metamodel.ModelioMetaModelResource;
import org.hawk.modelio.exml.metamodel.ModelioPackage;

public class RegisterMeta {

	private static int registered = 0;
	private final static Map<String, ModelioPackage> registeredMetamodels = new HashMap<String, ModelioPackage>();

	public static Collection<ModelioPackage> getRegisteredPackages() {
		return registeredMetamodels.values();
	}

	public void clean() {
		registeredMetamodels.clear();
	}

	// registers metamodel
	/**
	 * register Modelio package 
	 * 
	 * @param pkg
	 */
	public static int registerPackages(ModelioPackage pkg) {

		if (registeredMetamodels.put(pkg.getNsURI(), pkg) == null) {
			System.err.println("registering package: " + pkg.getName()
					+ "(" + pkg.getNsURI() + ")");
			registered++;
		}

		return registered;
	}

	public static void registerPackages(ModelioMetaModelResource r) {
		for (IHawkObject e : r.getAllContents()) {
			if (e instanceof ModelioPackage) {
				registerPackages((ModelioPackage) e);
			} 
		}
	}

	public static ModelioPackage getPackage(String  uri) {
		return registeredMetamodels.get(uri);
	}

	
	public static ModelioClass getModelioClass(String  className) {
		String pkgName;
		String mcName;
		ModelioClass mc = null;

		final int idxDot = className.indexOf(".");
		if(idxDot > -1) {
			pkgName = className.substring(0, idxDot);
			mcName = className.substring(idxDot + 1);
			mc = getMClass(pkgName, mcName);
		} else {

			mc = getMClass(className);
		}

		return mc;
	}
	
	private static ModelioClass getMClass(String  className) {
		for( ModelioPackage pkg : registeredMetamodels.values()) {
			ModelioClass mc = pkg.getClassifier(className);
			if( mc != null) {
				return mc;
			}
		}
		return null;
	}
	
	private static ModelioClass getMClass(String pkgName, String className) {
		for( ModelioPackage pkg : registeredMetamodels.values()) {
			if( pkg.getName().equals(pkgName)) {
				return pkg.getClassifier(className);
			}
		}
		return null;
	}
}

@@ -1437,20 +1437,19 @@
 pkg.getN
-sURI
+ame
 (), pkg)
@@ -1830,114 +1830,8 @@
 %09%7D%0D%0A
-%0D%0A%09public static ModelioPackage getPackage(String  uri) %7B%0D%0A%09%09return registeredMetamodels.get(uri);%0D%0A%09%7D%0D%0A%0D%0A
 %09%0D%0A%09
@@ -2547,37 +2547,32 @@
 className) %7B%0D%0A%09%09
-for( 
 ModelioPackage p
@@ -2566,33 +2566,33 @@
 elioPackage pkg 
-:
+=
  registeredMetam
@@ -2601,63 +2601,45 @@
 els.
-values()) %7B
+get(pkgName);
 %0D%0A%09%09
-%09
 if(
- 
 pkg
-.getName().equals(pkgName)
+ != null
 ) %7B
+%09
 %0D%0A
-%09
 %09%09%09r
@@ -2675,22 +2675,16 @@
 Name);%0D%0A
-%09%09%09%7D%0D%0A
 %09%09%7D%0D%0A%09%09r

Change Metamodels hashmap to use package name as Key (instread of URI)