<commit_before>/*******************************************************************************
 * Copyright (c) 2012 <NAME> and <NAME>.
 * 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:
 *     <NAME> - initial implementation
 *     <NAME> (XMLmodeling.com) - generalize and move code into this util class
 *     
 *******************************************************************************/
package org.openhealthtools.mdht.uml.cda.ui.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.pde.ui.templates.PluginReference;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;
import org.openhealthtools.mdht.uml.cda.CDAPackage;
import org.osgi.framework.Bundle;

public class CDAProjectUtil {

	private Map<String, Package> cdaPackages = new HashMap<String, Package>();

	private Map<String, Type> cdaDocuments = new HashMap<String, Type>();

	private Map<String, IFile> cdaDocumentsManfiest = new HashMap<String, IFile>();

	private Map<String, IFile> cdaDocumentsGenModels = new HashMap<String, IFile>();

	private Map<String, PluginReference> references = new HashMap<String, PluginReference>();

	private Type clinicalDocument = null;

	public CDAProjectUtil() {
	}

	/**
	 * @return the cdaPackages
	 */
	public Map<String, Package> getCDAPackages() {
		return cdaPackages;
	}

	/**
	 * @return the cdaDocuments
	 */
	public Map<String, Type> getCDADocuments() {
		return cdaDocuments;
	}

	/**
	 * @return the cdaDocumentsManfiest
	 */
	public Map<String, IFile> getCDADocumentsManfiests() {
		return cdaDocumentsManfiest;
	}

	/**
	 * @return the cdaDocumentsGenModels
	 */
	public Map<String, IFile> getCDADocumentsGenModels() {
		return cdaDocumentsGenModels;
	}

	/**
	 * @return the references
	 */
	public Map<String, PluginReference> getReferences() {
		return references;
	}

	public void loadCDAModelsfromWorkspace() {
		ResourceSet resourceSet = new ResourceSetImpl();

		IWorkspace workspace = ResourcesPlugin.getWorkspace();

		IWorkspaceRoot root = workspace.getRoot();

		Path model = new Path("model");

		for (IProject project : root.getProjects()) {
			if (project.exists(model)) {
				IFolder folder = project.getFolder(model);
				try {
					for (IResource resource : folder.members()) {
						if (resource.getName().endsWith(".uml") && !resource.getName().contains("_Ecore")) {
							URI modelFile = URI.createFileURI(project.getFolder(model).getFile(resource.getName()).getRawLocation().toOSString());
							PackageableElement pe = null;
							try {
								pe = (PackageableElement) EcoreUtil.getObjectByType(
									resourceSet.getResource(modelFile, true).getContents(),
									UMLPackage.eINSTANCE.getPackageableElement());
							} catch (Exception e) {
								// ignore models that cannot be loaded
							}

							if (pe != null) {
								if (pe instanceof Package) {
									Package p = (Package) pe;
									if (p.getAppliedProfile("CDA") != null || p.getName().equals("cda")) {

										IProject generatedProject = project;
										if (project.getName().endsWith(".model")) {
											generatedProject = root.getProject(project.getName().replace(".model", ""));
										}

										cdaPackages.put(p.getQualifiedName(), p);
										cdaDocumentsManfiest.put(
											p.getQualifiedName(), CDAUIUtil.getManifest(generatedProject));

										if (generatedProject.getFolder(model).exists()) {
											for (IResource genmodel : generatedProject.getFolder(model).members()) {
												if (genmodel.getName().endsWith(".genmodel")) {
													cdaDocumentsGenModels.put(
														p.getQualifiedName(),
														CDAUIUtil.getGenModel(
															generatedProject, genmodel.getProjectRelativePath()));
												}
											}
										}
									}
								}
							}
						}
					}
				} catch (CoreException e) {
					e.printStackTrace();
				}
			}
		}

		Package cdaPackage = cdaPackages.get("cda");

		// make sure the package is available
		if (cdaPackage != null) {
			clinicalDocument = cdaPackage.getOwnedType("ClinicalDocument");
			for (Package ps : cdaPackages.values()) {

				for (Type type : ps.getOwnedTypes()) {
					if (type.conformsTo(clinicalDocument)) {
						cdaDocuments.put(type.getQualifiedName(), type);
						cdaDocumentsManfiest.put(
							type.getQualifiedName(), cdaDocumentsManfiest.get(ps.getQualifiedName()));
						cdaDocumentsGenModels.put(
							type.getQualifiedName(), cdaDocumentsGenModels.get(ps.getQualifiedName()));
					}
				}
			}
		}
	}

	@SuppressWarnings("unchecked")
	public void loadCDAModelsfromPlugins() {
		new ResourceSetImpl();

		IExtensionRegistry registry = Platform.getExtensionRegistry();

		// Get all emf ecore plugins
		IExtensionPoint point = registry.getExtensionPoint("org.eclipse.emf.ecore.generated_package");

		if (point != null) {

			for (IExtension extension : point.getExtensions()) {

				Bundle bundle = Platform.getBundle(extension.getContributor().getName());

				try {
					for (IConfigurationElement ice : extension.getConfigurationElements()) {
						String packageClassName = ice.getAttribute("class");
						System.out.println(packageClassName);
						if (packageClassName != null) {
							Class<?> packageClass;
							// c = Class.forName(packageClass);
							packageClass = bundle.loadClass(packageClassName);
							if (packageClass != null) {
								Field eInstance = packageClass.getDeclaredField("eINSTANCE");
								Field ePrefix = packageClass.getDeclaredField("eNS_PREFIX");

								EPackage ePackage = (EPackage) eInstance.get(null);
								String prefix = (String) ePrefix.get(null);

								System.out.println(ePackage.getName());
								for (EClassifier eClassifier : ePackage.getEClassifiers()) {
									if (eClassifier instanceof EClass) {
										if (CDAPackage.eINSTANCE.getClinicalDocument().isSuperTypeOf(
											(EClass) eClassifier)) {
											cdaDocuments.put(prefix + "::" + eClassifier.getName(), null);
										}
									}
								}

							}
						}
					}
				} catch (Exception e) {
					e.printStackTrace();
				}

			}
		}

	}
}<commit_msg>Fixed issue with loadfromplugins<commit_after>/*******************************************************************************
 * Copyright (c) 2012 <NAME> and <NAME>lson.
 * 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:
 *     <NAME> - initial implementation
 *     <NAME> (XMLmodeling.com) - generalize and move code into this util class
 *     
 *******************************************************************************/
package org.openhealthtools.mdht.uml.cda.ui.util;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import org.eclipse.core.resources.IFile;
import org.eclipse.core.resources.IFolder;
import org.eclipse.core.resources.IProject;
import org.eclipse.core.resources.IResource;
import org.eclipse.core.resources.IWorkspace;
import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IConfigurationElement;
import org.eclipse.core.runtime.IExtension;
import org.eclipse.core.runtime.IExtensionPoint;
import org.eclipse.core.runtime.IExtensionRegistry;
import org.eclipse.core.runtime.Path;
import org.eclipse.core.runtime.Platform;
import org.eclipse.emf.common.util.URI;
import org.eclipse.emf.ecore.EClass;
import org.eclipse.emf.ecore.EClassifier;
import org.eclipse.emf.ecore.EPackage;
import org.eclipse.emf.ecore.resource.ResourceSet;
import org.eclipse.emf.ecore.resource.impl.ResourceSetImpl;
import org.eclipse.emf.ecore.util.EcoreUtil;
import org.eclipse.pde.ui.templates.PluginReference;
import org.eclipse.uml2.uml.Package;
import org.eclipse.uml2.uml.PackageableElement;
import org.eclipse.uml2.uml.Type;
import org.eclipse.uml2.uml.UMLPackage;
import org.openhealthtools.mdht.uml.cda.CDAPackage;
import org.osgi.framework.Bundle;

public class CDAProjectUtil {

	private Map<String, Package> cdaPackages = new HashMap<String, Package>();

	private Map<String, Type> cdaDocuments = new HashMap<String, Type>();

	private Map<String, IFile> cdaDocumentsManfiest = new HashMap<String, IFile>();

	private Map<String, IFile> cdaDocumentsGenModels = new HashMap<String, IFile>();

	private Map<String, PluginReference> references = new HashMap<String, PluginReference>();

	private Type clinicalDocument = null;

	public CDAProjectUtil() {
	}

	/**
	 * @return the cdaPackages
	 */
	public Map<String, Package> getCDAPackages() {
		return cdaPackages;
	}

	/**
	 * @return the cdaDocuments
	 */
	public Map<String, Type> getCDADocuments() {
		return cdaDocuments;
	}

	/**
	 * @return the cdaDocumentsManfiest
	 */
	public Map<String, IFile> getCDADocumentsManfiests() {
		return cdaDocumentsManfiest;
	}

	/**
	 * @return the cdaDocumentsGenModels
	 */
	public Map<String, IFile> getCDADocumentsGenModels() {
		return cdaDocumentsGenModels;
	}

	/**
	 * @return the references
	 */
	public Map<String, PluginReference> getReferences() {
		return references;
	}

	public void loadCDAModelsfromWorkspace() {
		ResourceSet resourceSet = new ResourceSetImpl();

		IWorkspace workspace = ResourcesPlugin.getWorkspace();

		IWorkspaceRoot root = workspace.getRoot();

		Path model = new Path("model");

		for (IProject project : root.getProjects()) {
			if (project.exists(model)) {
				IFolder folder = project.getFolder(model);
				try {
					for (IResource resource : folder.members()) {
						if (resource.getName().endsWith(".uml") && !resource.getName().contains("_Ecore")) {
							URI modelFile = URI.createFileURI(project.getFolder(model).getFile(resource.getName()).getRawLocation().toOSString());
							PackageableElement pe = null;
							try {
								pe = (PackageableElement) EcoreUtil.getObjectByType(
									resourceSet.getResource(modelFile, true).getContents(),
									UMLPackage.eINSTANCE.getPackageableElement());
							} catch (Exception e) {
								// ignore models that cannot be loaded
							}

							if (pe != null) {
								if (pe instanceof Package) {
									Package p = (Package) pe;
									if (p.getAppliedProfile("CDA") != null || p.getName().equals("cda")) {

										IProject generatedProject = project;
										if (project.getName().endsWith(".model")) {
											generatedProject = root.getProject(project.getName().replace(".model", ""));
										}

										cdaPackages.put(p.getQualifiedName(), p);
										cdaDocumentsManfiest.put(
											p.getQualifiedName(), CDAUIUtil.getManifest(generatedProject));

										if (generatedProject.getFolder(model).exists()) {
											for (IResource genmodel : generatedProject.getFolder(model).members()) {
												if (genmodel.getName().endsWith(".genmodel")) {
													cdaDocumentsGenModels.put(
														p.getQualifiedName(),
														CDAUIUtil.getGenModel(
															generatedProject, genmodel.getProjectRelativePath()));
												}
											}
										}
									}
								}
							}
						}
					}
				} catch (CoreException e) {
					e.printStackTrace();
				}
			}
		}

		Package cdaPackage = cdaPackages.get("cda");

		// make sure the package is available
		if (cdaPackage != null) {
			clinicalDocument = cdaPackage.getOwnedType("ClinicalDocument");
			for (Package ps : cdaPackages.values()) {

				for (Type type : ps.getOwnedTypes()) {
					if (type.conformsTo(clinicalDocument)) {
						cdaDocuments.put(type.getQualifiedName(), type);
						cdaDocumentsManfiest.put(
							type.getQualifiedName(), cdaDocumentsManfiest.get(ps.getQualifiedName()));
						cdaDocumentsGenModels.put(
							type.getQualifiedName(), cdaDocumentsGenModels.get(ps.getQualifiedName()));
					}
				}
			}
		}
	}

	@SuppressWarnings("unchecked")
	public void loadCDAModelsfromPlugins() {
		new ResourceSetImpl();

		IExtensionRegistry registry = Platform.getExtensionRegistry();

		// Get all emf ecore plugins
		IExtensionPoint point = registry.getExtensionPoint("org.eclipse.emf.ecore.generated_package");

		if (point != null) {

			for (IExtension extension : point.getExtensions()) {
				Bundle bundle = Platform.getBundle(extension.getContributor().getName());
				for (IConfigurationElement ice : extension.getConfigurationElements()) {
					String packageClassName = ice.getAttribute("class");
					if (packageClassName != null) {
						try {
							Class<?> packageClass;
							packageClass = bundle.loadClass(packageClassName);
							if (packageClass != null) {
								Field eInstance = packageClass.getDeclaredField("eINSTANCE");
								Field ePrefix = packageClass.getDeclaredField("eNS_PREFIX");
								EPackage ePackage = (EPackage) eInstance.get(null);
								String prefix = (String) ePrefix.get(null);
								for (EClassifier eClassifier : ePackage.getEClassifiers()) {
									if (eClassifier instanceof EClass) {
										if (CDAPackage.eINSTANCE.getClinicalDocument().isSuperTypeOf(
											(EClass) eClassifier)) {
											cdaDocuments.put(prefix + "::" + eClassifier.getName(), null);
										}
									}
								}
							}
						} catch (Exception e) {
						}
					}
				}

			}
		}

	}
}
{'fraction_non_alphanumeric': 0.13979814511729405, 'fraction_numerical': 0.0016366612111292963, 'mean_word_length': 3.5656911581569117, 'pattern_counts': {'":': 2, '<': 43, '<?xml version=': 0, '>': 43, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7156551', 'n_tokens_mistral': 5512, 'n_tokens_neox': 4275, 'n_words': 836}