/******************************************************************************* * Copyright (c) 2008, 2012 Obeo. * 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: * Obeo - initial API and implementation *******************************************************************************/ package org.eclipse.emf.eef.codegen.core.launcher; import java.io.IOException; import java.net.URL; import java.util.Enumeration; import java.util.HashSet; import java.util.Set; import org.eclipse.core.resources.IContainer; import org.eclipse.core.runtime.IPath; import org.eclipse.core.runtime.Path; import org.eclipse.core.runtime.Platform; import org.eclipse.emf.common.util.URI; import org.osgi.framework.Bundle; /** * @author <a href="mailto:goulwen.lefur@obeo.fr">Goulwen Le Fur</a> */ public abstract class AbstractPropertiesGeneratorLauncher implements IPropertiesGeneratorLauncher { /** * Name of the launcher */ private String name; /** * @return the name */ public String getName() { return name; } /** * @param name * the name to set */ public void setName(String name) { this.name = name; } /** * A set containing the generation target */ protected Set<IContainer> targetContainer = new HashSet<IContainer>(); /** * @return the targetContainer */ public Set<IContainer> getTargetContainer() { return targetContainer; } /** * Finds the template in the plug-in. Returns the template plug-in URI. * * @param bundleID * is the plug-in ID * @param relativePath * is the relative path of the template in the plug-in * @return the template URI * @throws IOException * @deprecated */ protected URI getTemplateURI(String bundleID, IPath relativePath) throws IOException { Bundle bundle = Platform.getBundle(bundleID); if (bundle == null) { // no need to go any further return URI.createPlatformResourceURI(new Path(bundleID).append(relativePath).toString(), false); } URL url = bundle.getEntry(relativePath.toString()); if (url == null && relativePath.segmentCount() > 1) { Enumeration<URL> entries = bundle.findEntries("/", "*.emtl", true); if (entries != null) { String[] segmentsRelativePath = relativePath.segments(); while (url == null && entries.hasMoreElements()) { URL entry = entries.nextElement(); IPath path = new Path(entry.getPath()); if (path.segmentCount() > relativePath.segmentCount()) { path = path.removeFirstSegments(path.segmentCount() - relativePath.segmentCount()); } String[] segmentsPath = path.segments(); boolean equals = segmentsPath.length == segmentsRelativePath.length; for (int i = 0; equals && i < segmentsPath.length; i++) { equals = segmentsPath[i].equals(segmentsRelativePath[i]); } if (equals) { url = bundle.getEntry(entry.getPath()); } } } } URI result; if (url != null) { result = URI.createPlatformPluginURI(new Path(bundleID).append(new Path(url.getPath())) .toString(), false); } else { result = URI.createPlatformResourceURI(new Path(bundleID).append(relativePath).toString(), false); } return result; } }