diff options
Diffstat (limited to 'plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse')
19 files changed, 3644 insertions, 0 deletions
diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/Activator.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/Activator.java new file mode 100644 index 00000000000..db1f9227ddb --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/Activator.java @@ -0,0 +1,75 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * + * 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors; + +import org.eclipse.papyrus.infra.core.log.LogHelper; +import org.eclipse.ui.plugin.AbstractUIPlugin; +import org.osgi.framework.BundleContext; + +/** + * The activator class controls the plug-in life cycle + */ +public class Activator extends AbstractUIPlugin { + + // The plug-in ID + public static final String PLUGIN_ID = "org.eclipse.papyrus.eclipse.project.editors"; //$NON-NLS-1$ + + // The shared instance + private static Activator plugin; + + public static LogHelper log; + + /** + * The constructor + */ + public Activator() { + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ui.plugin.AbstractUIPlugin#start(org.osgi.framework.BundleContext + * ) + */ + @Override + public void start(final BundleContext context) throws Exception { + super.start(context); + plugin = this; + log = new LogHelper(this); + } + + /* + * (non-Javadoc) + * + * @see + * org.eclipse.ui.plugin.AbstractUIPlugin#stop(org.osgi.framework.BundleContext + * ) + */ + @Override + public void stop(final BundleContext context) throws Exception { + plugin = null; + super.stop(context); + } + + /** + * Returns the shared instance + * + * @return the shared instance + */ + public static Activator getDefault() { + return plugin; + } + +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/AbstractFileEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/AbstractFileEditor.java new file mode 100644 index 00000000000..2fbe4dd44a1 --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/AbstractFileEditor.java @@ -0,0 +1,122 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * + * 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.file; + +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.util.HashSet; +import java.util.Set; + +import org.eclipse.core.resources.IProject; +import org.eclipse.papyrus.eclipse.project.editors.Activator; +import org.eclipse.papyrus.eclipse.project.editors.interfaces.IFileEditor; + +/** + * + * An abstract class for File Editor + * + */ +public abstract class AbstractFileEditor implements IFileEditor { + + /** the Eclipse Project */ + private final IProject project; + + + /** + * + * Constructor. + * + * @param project + * the eclipse project + */ + public AbstractFileEditor(final IProject project) { + this.project = project; + } + + /** + * + * @return + * the eclipse project + */ + public IProject getProject() { + return this.project; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.AbstractProjectEditor#init() + * + * {@inheritDoc} + */ + public void init() { + + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor#getMissingFiles() + * + * {@inheritDoc} + */ + public Set<String> getMissingFiles() { + return new HashSet<String>(); + } + + /** + * + * @param text + * the initial text + * @return + * an input stream + */ + protected InputStream getInputStream(final String text) { + if (text == null) { + Activator.log.warn("Cannot open an input stream for a null text"); //$NON-NLS-1$ + return null; + } + + final StringReader sr = new StringReader(text); + InputStream is = new InputStream() { + + @Override + public int read() throws IOException { + return sr.read(); + } + + }; + return is; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.AbstractProjectEditor.plugin.AbstractEditor#exists() + * + * {@inheritDoc} + */ + public boolean exists() { + return getMissingFiles().size() == 0; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IFileEditor#create() + * + * {@inheritDoc} + */ + public void create() { + createFiles(getMissingFiles()); + init(); + } +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/BuildEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/BuildEditor.java new file mode 100644 index 00000000000..cfb832ef2d3 --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/BuildEditor.java @@ -0,0 +1,290 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * 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: + * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation + * Vincent Lorenzo (CEA-LIST) vincent.lorenzo@cea.fr + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.file; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.util.Arrays; +import java.util.List; +import java.util.Properties; +import java.util.Set; + +import org.eclipse.core.resources.IProject; +import org.eclipse.papyrus.eclipse.project.editors.Activator; +import org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor; +import org.eclipse.papyrus.infra.widgets.util.FileUtil; + +public class BuildEditor extends AbstractFileEditor implements IBuildEditor { + + /** the build config */ + private Properties buildConfig; + + /** the buidl file */ + private File buildFile; + + /** + * The build key to edit + * Defaults to "bin.includes" (The eclipse standard binary build) + */ + private String buildKey = "bin.includes"; //$NON-NLS-1$ + + + /** + * + * Constructor. + * + * @param project + * the eclipse project + */ + public BuildEditor(final IProject project) { + super(project); + } + + /** + * + * Constructor. + * + * @param project + * the eclipse project + * @param buildKey + * the build key to edit. If null, the "bin.includes" key will be used + * + * @see IBuildEditor#BINARY_BUILD + * @see IBuildEditor#SOURCE_BUILD + */ + public BuildEditor(final IProject project, String buildKey) { + super(project); + if (buildKey != null) { + this.buildKey = buildKey; + } + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#init() + * + * {@inheritDoc} + */ + @Override + public void init() { + this.buildConfig = new Properties(); + this.buildFile = getBuildProperties(); + if (this.buildFile != null && this.buildFile.exists()) { + try { + this.buildConfig.load(new FileInputStream(this.buildFile)); + } catch (FileNotFoundException e) { + Activator.log.error(e); + } catch (IOException e) { + Activator.log.error(e); + } + } + } + + + private File getBuildProperties() { + return this.buildFile = FileUtil.getWorkspaceFile("/" + getProject().getName() + "/" + BUILD_PROPERTIES_FILE); //$NON-NLS-1$ //$NON-NLS-2$ + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#addToBuild(java.lang.String) + * + * {@inheritDoc} + */ + public void addToBuild(final String path) { + if (exists()) { + String currentValue = this.buildConfig.getProperty(buildKey); + if (Arrays.asList(getElementsInBuild()).contains(path)) { + return; + } + if (currentValue == null || currentValue.trim().equals("")) { //$NON-NLS-1$ + this.buildConfig.setProperty(buildKey, path); + } else { + this.buildConfig.setProperty(buildKey, currentValue + "," + path); //$NON-NLS-1$ + } + } + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#removeFromBuild(java.lang.String) + * + * {@inheritDoc} + */ + public void removeFromBuild(String path) { + if (isRegisteredSourceFolder(path)) { + // Get the files from the build + List<String> allFiles = Arrays.asList(getElementsInBuild()); + + // Clear the build + this.buildConfig.setProperty(buildKey, ""); + + // Recreate the build without the removed files + for (String filePath : allFiles) { + if (!filePath.equals(path)) { + addToBuild(filePath); + } + } + } + } + + /** + * + * @throws Throwable + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#save() + * + * {@inheritDoc} + */ + public void save() { + if (exists()) { + try { + this.buildConfig.store(new FileOutputStream(this.buildFile), ""); + } catch (FileNotFoundException ex) { + Activator.log.error(ex); + } catch (IOException ex) { + Activator.log.error(ex); + } + } + } + + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#registerSourceFolder(java.lang.String) + * + * {@inheritDoc} + */ + public void registerSourceFolder(final String path) { + if (exists() && !isRegisteredSourceFolder(path)) { + String currentValue = this.buildConfig.getProperty(SOURCE_FOLDER_KEY); + if (currentValue == null || currentValue.trim().equals("")) { //$NON-NLS-1$ + this.buildConfig.setProperty(SOURCE_FOLDER_KEY, path); + } else { + this.buildConfig.setProperty(SOURCE_FOLDER_KEY, currentValue + "," + path); //$NON-NLS-1$ + } + } + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#isRegistred(java.lang.String) + * + * {@inheritDoc} + */ + public boolean isRegisteredSourceFolder(final String path) { + return Arrays.asList(getSourceFolders()).contains(path); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.file.AbstractFileEditor#getMissingFiles() + * + * {@inheritDoc} + */ + @Override + public Set<String> getMissingFiles() { + Set<String> files = super.getMissingFiles(); + if (!getBuildProperties().exists()) { + files.add(BUILD_PROPERTIES_FILE); + } + return files; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#getSourceFolders() + * + * {@inheritDoc} + */ + public String[] getSourceFolders() { + if (exists()) { + String currentValue = this.buildConfig.getProperty(SOURCE_FOLDER_KEY, "").replaceAll("\t|\r|\n", "").trim(); + String[] values = currentValue.split(","); //$NON-NLS-1$ + return values; + } + return new String[0]; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.AbstractProjectEditor#exists() + * + * {@inheritDoc} + */ + @Override + public boolean exists() { + return this.buildFile.getParentFile().exists() && this.buildFile.exists() && super.exists(); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#createFiles(Set) + * + * {@inheritDoc} + */ + public void createFiles(final Set<String> files) { + if (files.contains(BUILD_PROPERTIES_FILE)) { + if (!this.buildFile.exists()) { + if (!this.buildFile.getParentFile().exists()) { + this.buildFile.getParentFile().mkdirs(); + } + try { + this.buildFile.createNewFile(); + init(); + } catch (IOException e) { + Activator.log.error(e); + } + } + } + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#getElementsInBuild() + * + * {@inheritDoc} + */ + public String[] getElementsInBuild() { + String value = this.buildConfig.getProperty(buildKey); + return splitValues(value); + } + + private String[] splitValues(String value) { + if (value == null) { + return new String[0]; + } + return value.replace("\t|\r|\n", "").split(","); //$NON-NLS-1$ //$NON-NLS-2$ + } + + public void registerBinFolder(String binFolder) { + if (isRegisteredBinFolder(binFolder)) { + return; + } + + String value = this.buildConfig.getProperty(BIN_KEY, ""); //$NON-NLS-1$ + if (value.trim().equals("")) { //$NON-NLS-1$ + value = binFolder; + } else { + value = value + "," + binFolder; + } + this.buildConfig.setProperty(BIN_KEY, value); + } + + public boolean isRegisteredBinFolder(String binFolder) { + List<String> folders = Arrays.asList(splitValues(this.buildConfig.getProperty(BIN_KEY, ""))); //$NON-NLS-1$ + return folders.contains(binFolder); + } +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/BundlePropertiesEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/BundlePropertiesEditor.java new file mode 100644 index 00000000000..ba870790e50 --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/BundlePropertiesEditor.java @@ -0,0 +1,67 @@ +package org.eclipse.papyrus.eclipse.project.editors.file; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.util.Properties; +import java.util.Set; + +import org.eclipse.core.resources.IProject; +import org.eclipse.papyrus.eclipse.project.editors.Activator; +import org.eclipse.papyrus.infra.widgets.util.FileUtil; + + +public class BundlePropertiesEditor extends AbstractFileEditor { + + /** the build config */ + private Properties bundleConfig; + + /** the buidl file */ + private File bundleFile; + + private String filePath; + + public BundlePropertiesEditor(IProject project, String filePath) { + super(project); + this.filePath = filePath; + + } + + @Override + public void init() { + this.bundleFile = getBundleProperties(); + // if(this.bundleFile != null && this.bundleFile.exists()) { + try { + this.bundleConfig.load(new FileInputStream(this.bundleFile)); + } catch (FileNotFoundException e) { + Activator.log.error(e); + } catch (IOException e) { + Activator.log.error(e); + } + // } + } + + public void createFiles(Set<String> files) { + // TODO Auto-generated method stub + + } + + public void save() { + // TODO Auto-generated method stub + + } + + public String getValue(String propertyName) { + return this.bundleConfig.getProperty(propertyName); + + } + + + + + + private File getBundleProperties() { + return this.bundleFile = FileUtil.getWorkspaceFile("/" + getProject().getName() + "/" + filePath + ".properties"); //$NON-NLS-1$ //$NON-NLS-2$ + } +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/ClasspathEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/ClasspathEditor.java new file mode 100644 index 00000000000..e16d91c449f --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/ClasspathEditor.java @@ -0,0 +1,261 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * + * 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr + * + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.file; + + +import java.io.InputStream; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.AssertionFailedException; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IPath; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.jdt.core.IAccessRule; +import org.eclipse.jdt.core.IClasspathAttribute; +import org.eclipse.jdt.core.IClasspathEntry; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.IPackageFragmentRoot; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.jdt.internal.core.ClasspathEntry; +import org.eclipse.papyrus.eclipse.project.editors.Activator; +import org.eclipse.papyrus.eclipse.project.editors.interfaces.IClasspathEditor; + +/** + * + * The editor for the classpath file + * + */ +public class ClasspathEditor extends AbstractFileEditor implements IClasspathEditor { + + /** + * the edited java project + */ + private final IJavaProject javaProject; + + + /** + * + * Constructor. + * + * @param project + */ + public ClasspathEditor(final IProject project) throws AssertionFailedException { + super(project); + this.javaProject = JavaCore.create(project); + } + + /** + * + * Constructor. + * + * @param javaProject + * a java project + */ + public ClasspathEditor(final IJavaProject javaProject) { + super(javaProject.getProject()); + this.javaProject = javaProject; + } + + @Override + public void init() { + // nothing to do here + } + + /** + * save the modification + * + * @throws Throwable + */ + public void save() { + try { + this.javaProject.save(new NullProgressMonitor(), true); + } catch (JavaModelException ex) { + Activator.log.error(ex); + } + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IClasspathEditor#addSourceFolderToClasspath(java.lang.String) + * + * {@inheritDoc} + */ + public void addSourceFolderToClasspath(final String folderPath) { + + if (exists() && !isSourceFolderRegistered(folderPath)) { + + // parameters for the new ClasspathEntry + boolean isExported = false; + IPath[] exclusionPatterns = new IPath[0]; + IPath sourceAttachmentPath = null; + IPath specificOutputLocation = null; + boolean combineAccessRules = false; + IClasspathAttribute[] extraAttributes = new IClasspathAttribute[0]; + IPath sourceAttachmentRootPath = null; + IAccessRule[] accessRules = null; + int contentKind = IPackageFragmentRoot.K_SOURCE; + IPath[] inclusionPatterns = new IPath[0]; + int entryKind = IClasspathEntry.CPE_SOURCE; + IPath path2 = new Path(folderPath); + + IClasspathEntry[] classpathes = null; + try { + classpathes = this.javaProject.getRawClasspath(); + } catch (JavaModelException e) { + e.printStackTrace(); + } + + IClasspathEntry[] entries = new IClasspathEntry[classpathes.length + 1]; + for (int i = 0; i < classpathes.length; i++) { + entries[i] = classpathes[i]; + } + entries[classpathes.length] = new ClasspathEntry(contentKind, entryKind, path2, inclusionPatterns, exclusionPatterns, sourceAttachmentPath, sourceAttachmentRootPath, specificOutputLocation, isExported, accessRules, combineAccessRules, + extraAttributes); + try { + this.javaProject.setRawClasspath(entries, new NullProgressMonitor()); + } catch (JavaModelException e) { + Activator.log.error(e); + } + } + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IClasspathEditor#isSourceFolderRegistered(java.lang.String) + * + * {@inheritDoc} + */ + public boolean isSourceFolderRegistered(final String folderPath) { + IClasspathEntry[] entries = null; + try { + entries = this.javaProject.getRawClasspath(); + } catch (JavaModelException e) { + Activator.log.error(e); + } + + for (int i = 0; i < entries.length; i++) { + IClasspathEntry entry = entries[i]; + if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { + if (entry.getPath().equals(new Path(folderPath))) { + return true; + } + } + } + return false; + } + + /** + * Tests if the classpath file exists + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.AbstractProjectEditor.plugin.AbstractEditor#exists() + * + * {@inheritDoc} + */ + @Override + public boolean exists() { + IFile classpath = getProject().getFile(CLASSPATH_FILE); + return classpath.exists(); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#getMissingFiles() + * + * {@inheritDoc} + */ + @Override + public Set<String> getMissingFiles() { + Set<String> files = super.getMissingFiles(); + IFile classpath = getProject().getFile(CLASSPATH_FILE); + if (!classpath.exists()) { + files.add(CLASSPATH_FILE); + } + return files; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#createFiles(Set) + * + * {@inheritDoc} + */ + public void createFiles(final Set<String> files) { + if (files.contains(CLASSPATH_FILE)) { + IFile classpath = getProject().getFile(CLASSPATH_FILE); + if (!classpath.exists()) { + InputStream is = getInputStream("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<?eclipse version=\"3.4\"?>\n" + "<classpath>\n" + "</classpath>\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + + try { + classpath.create(is, true, null); + } catch (CoreException ex) { + Activator.log.error(ex); + } + } + } + } + + public String[] getSourceFolders() { + List<String> sourceFolders = new LinkedList<String>(); + IClasspathEntry[] entries = null; + try { + entries = this.javaProject.getRawClasspath(); + } catch (JavaModelException e) { + Activator.log.error(e); + } + + for (int i = 0; i < entries.length; i++) { + IClasspathEntry entry = entries[i]; + if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { + sourceFolders.add(entry.getPath().makeRelativeTo(javaProject.getPath()).toString()); + } + } + + return sourceFolders.toArray(new String[sourceFolders.size()]); + } + + public String[] getBinFolders() { + List<String> binFolders = new LinkedList<String>(); + try { + // General bin folder + binFolders.add(javaProject.getOutputLocation().makeRelativeTo(javaProject.getPath()).toString()); + } catch (JavaModelException ex) { + Activator.log.error(ex); + } + IClasspathEntry[] entries = null; + try { + entries = this.javaProject.getRawClasspath(); + } catch (JavaModelException e) { + Activator.log.error(e); + } + + for (int i = 0; i < entries.length; i++) { + IClasspathEntry entry = entries[i]; + if (entry.getEntryKind() == IClasspathEntry.CPE_SOURCE) { + if (entry.getOutputLocation() != null) { + // Bin folder associated to each source folder + binFolders.add(entry.getOutputLocation().makeRelativeTo(javaProject.getPath()).toString()); + } + } + } + + return binFolders.toArray(new String[binFolders.size()]); + } +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/ManifestEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/ManifestEditor.java new file mode 100644 index 00000000000..91767d21074 --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/file/ManifestEditor.java @@ -0,0 +1,594 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * 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: + * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.file; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.StringReader; +import java.util.Set; +import java.util.jar.Attributes; +import java.util.jar.Attributes.Name; +import java.util.jar.Manifest; + +import org.eclipse.core.resources.IContainer; +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.papyrus.eclipse.project.editors.Activator; +import org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor; +import org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor; + +public class ManifestEditor extends ProjectEditor implements IManifestEditor { + + // string constants + private static final String CRNL = "\r\n"; //$NON-NLS-1$ + + private static final String CRNLSP = "\r\n "; //$NON-NLS-1$ + + private static final String SEMICOLON = ";"; //$NON-NLS-1$ + + private static final String COMMA = ","; //$NON-NLS-1$ + + private static final String ASSIGN = "="; //$NON-NLS-1$ + + private static final String BUNDLE_SYMBOLIC_NAME = "Bundle-SymbolicName"; //$NON-NLS-1$ + + private static final String IMPORT_PACKAGE = "Import-Package"; + + private static final String EXPORT_PACKAGE = "Export-Package"; + + private static final String SINGLETON = "singleton:="; //$NON-NLS-1$ + + /** the manifest file */ + private IFile manifestFile; + + /** the manifest itself */ + private Manifest manifest; + + /** + * + * Constructor. + * + * @param project + * @throws IOException + * @throws CoreException + */ + public ManifestEditor(final IProject project) throws IOException, CoreException { + super(project); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#initOk() + * + * {@inheritDoc} + */ + public boolean initOk() { + return manifest != null && manifestFile != null; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#addDependency(java.lang.String) + * + * {@inheritDoc} + */ + public void addDependency(final String dependency) { + addDependency(dependency, null); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#init() + * + * {@inheritDoc} + */ + @Override + public void init() { + super.init(); + if (manifest != null && manifestFile != null) { + return; + } + if (manifestFile == null) { + manifestFile = getManifestFile(); + } + if (manifestFile != null) { + try { + manifest = new Manifest(manifestFile.getContents()); + } catch (final IOException e) { + Activator.log.error(e); + // assure that exception is not silently captured (for users not examining the error log) + throw new RuntimeException(e); + } catch (final CoreException e) { + Activator.log.error(e); + // assure that exception is not silently captured (for users not examining the error log) + throw new RuntimeException(e); + } + } + + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#addDependency(java.lang.String, java.lang.String) + * + * {@inheritDoc} + */ + public void addDependency(final String dependency, final String version) { + final Name rqBundle = new Name(REQUIRED_BUNDLE); + String requireBundle = manifest.getMainAttributes().getValue(rqBundle); + + // TODO : Improve the detection of existing dependency + // If a.b.c exists, then a.b cannot be added (Because it is already contained) + // Moreover, the Manifest allows newlines anywhere (Including in the + // middle of a word) : check if these newlines appear in this map, + // or if they have already been parsed. If the manifest value is copied as-is in the map, + // then we need to take care of newlines when parsing it + + if (requireBundle == null) { + requireBundle = dependency; + + if (version != null) { + requireBundle += SEMICOLON + "bundle-version=\"" + version + "\""; + } + } else if (!requireBundle.contains(dependency)) { + requireBundle += COMMA + dependency; + // TODO: Update version + } + + manifest.getMainAttributes().put(rqBundle, requireBundle); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#hasDependency(java.lang.String) + * + * {@inheritDoc} + */ + public boolean hasDependency(final String dependency) { + final Name rqBundle = new Name(REQUIRED_BUNDLE); + String requireBundle = manifest.getMainAttributes().getValue(rqBundle); + return requireBundle != null && requireBundle.contains(dependency); + } + + public void setDependenciesVersion(final String dependencyPattern, final String newVersion) { + final Name rqBundle = new Name(REQUIRED_BUNDLE); + final String requireBundles = manifest.getMainAttributes().getValue(rqBundle); + final String[] bundles = requireBundles.split(COMMA); + String newRequiredBundles = ""; //$NON-NLS-1$ + for (int ii = 0; ii < bundles.length; ii++) {// we iterate on the declared dependencies + final String currentDependency = bundles[ii]; + final String[] dependencyValue = currentDependency.split(SEMICOLON); + if (dependencyValue[0].contains(dependencyPattern)) { + final String newBundleVersion = BUNDLE_VERSION + ASSIGN + '"' + newVersion + '"'; + newRequiredBundles += dependencyValue[0] + SEMICOLON + newBundleVersion; + for (int i = 1; i < dependencyValue.length; i++) { + final String declaration = dependencyValue[i]; + if (declaration.contains(BUNDLE_VERSION + ASSIGN)) { + // we ignore it + } else { + newRequiredBundles += SEMICOLON + dependencyValue[i];// we add the others declaration + } + } + } else { + newRequiredBundles += currentDependency;// we copy the existing declaration + } + if (ii < bundles.length - 1) { + newRequiredBundles += COMMA; + } + } + setValue(REQUIRED_BUNDLE, newRequiredBundles); + + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#setValue(java.lang.String, java.lang.String) + * + * {@inheritDoc} + */ + public void setValue(final String key, final String value) { + setValue(key, "", value); //$NON-NLS-1$ + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#setValue(java.lang.String, java.lang.String, java.lang.String) + * + * {@inheritDoc} + */ + public void setValue(final String key, final String name, final String value) { + manifest.getMainAttributes().putValue(key, value); + // this.manifest.getAttributes(key).put(name, value); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#removeValue(java.lang.String, java.lang.String) + * + * {@inheritDoc} + */ + public void removeValue(final String key, final String value) { + + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#removeValue(java.lang.String) + * + * {@inheritDoc} + */ + public void removeValue(final String key) { + manifest.getAttributes(key).remove(key); + } + + /** + * + * @return + */ + private IFile getManifestFile() { + final IFile manifest = getProject().getFile("META-INF/MANIFEST.MF"); //$NON-NLS-1$ + if (manifest.exists()) { + return manifest; + } + return null; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.AbstractProjectEditor#exists() + * + * {@inheritDoc} + */ + @Override + public boolean exists() { + return super.exists() && getManifestFile() != null && getSymbolicBundleName() != null && getBundleVersion() != null; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#save() + * + * {@inheritDoc} + */ + @Override + public void save() { + + final ByteArrayOutputStream os = new ByteArrayOutputStream(); + + try { + manifest.write(os); + + final StringReader reader = new StringReader(format(os.toString("UTF-8"))); //$NON-NLS-1$ + manifestFile.setContents(new InputStream() { + + @Override + public int read() throws IOException { + return reader.read(); + } + }, true, true, null); + + } catch (final IOException ex) { + Activator.log.error(ex); + } catch (final CoreException ex) { + Activator.log.error(ex); + } + } + + /** + * Simple formatting of the MANIFEST. Do not use the PDE formatter, since this makes an already opened + * MANIFEST editor dirty (see bug 447548 [OCL for Papyrus] Buggy DSML plugin generator) + */ + protected String format(String text) { + // 1. undo 72safe formatting + String[] lines = text.split(CRNLSP); + String non72safe = ""; //$NON-NLS-1$ + for (String line : lines) { + non72safe += line; + } + // 2. split lines on comma + lines = non72safe.split(COMMA); + String newText = ""; //$NON-NLS-1$ + for (int i = 0; i < lines.length; i++) { + newText += lines[i].trim(); + if (i < lines.length - 1) { + newText += COMMA + CRNLSP; + } + } + return newText + CRNL; + } + + @Override + public Set<String> getMissingFiles() { + final Set<String> files = super.getMissingFiles(); + final IFile classpath = getProject().getFile(MANIFEST_PATH); + if (!classpath.exists()) { + files.add(MANIFEST_PATH); + } + return files; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#createFiles(java.util.Set) + * + * {@inheritDoc} + */ + @Override + public void createFiles(final Set<String> files) { + if (files.contains(MANIFEST_PATH)) { + manifestFile = getProject().getFile(MANIFEST_PATH); + if (!manifestFile.exists()) { + try { + final String input = "Manifest-Version: 1.0\n"; //without the "/n", it doesn't work!!!!! //$NON-NLS-1$ + if (!manifestFile.getParent().exists()) { + final IContainer parent = manifestFile.getParent(); + if (parent instanceof IFolder) { + if (!parent.exists()) { + ((IFolder) parent).create(true, false, null); + } + } + } + manifestFile.create(getInputStream(input), true, null); + manifestFile = getProject().getFile(MANIFEST_PATH); + + // final int i; + // InputStream is = this.manifestFile.getContents(); + // while((i = is.read()) > 0) { + // System.out.println(i); + // } + // this.manifest = new Manifest(this.manifestFile.getContents()); + + } catch (final CoreException ex) { + Activator.log.error(ex); + } + } + } + + try { + manifest = new Manifest(manifestFile.getContents()); + } catch (IOException e) { + Activator.log.error(e); + } catch (CoreException e) { + Activator.log.error(e); + } + + if (getSymbolicBundleName() == null) { + setSymbolicBundleName(getProject().getName()); + } + + if (getBundleVersion() == null) { + setBundleVersion("0.0.1"); //$NON-NLS-1$ + } + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#setSymbolicBundleName(java.lang.String) + * + * {@inheritDoc} + */ + public void setSymbolicBundleName(String newName) { + if (newName == null) { + newName = "noName"; //$NON-NLS-1$ + } + final Name symbolicName = new Name(BUNDLE_SYMBOLIC_NAME); + manifest.getMainAttributes().put(symbolicName, newName); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#getSymbolicBundleName() + * + * {@inheritDoc} + */ + public String getSymbolicBundleName() { + if (manifest != null) { + final Name symbolicName = new Name(BUNDLE_SYMBOLIC_NAME); + final String name = manifest.getMainAttributes().getValue(symbolicName); + + if (name != null) { + int semiColon = name.indexOf(SEMICOLON); + return semiColon != -1 ? name.substring(0, semiColon) : name; + } + } + + return null; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#getBundleVersion() + * + * {@inheritDoc} + */ + public String getBundleVersion() { + if (manifest != null) { + final Name symbolicName = new Name(BUNDLE_VERSION); + final String version = manifest.getMainAttributes().getValue(symbolicName); + return version; + } + return null; + } + + /** + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#setBundleVersion(java.lang.String) + * + * {@inheritDoc} + */ + public void setBundleVersion(final String version) { + if (manifest != null) { + final Name bundleVersion = new Name(BUNDLE_VERSION); + if (version == null) { + manifest.getMainAttributes().remove(bundleVersion); + } else { + manifest.getMainAttributes().put(bundleVersion, version); + } + } + } + + /** + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#getBundleVendor() + * + * {@inheritDoc} + */ + public String getBundleVendor() { + if (manifest != null) { + final Name bundleVendor = new Name(BUNDLE_VENDOR); + return manifest.getMainAttributes().getValue(bundleVendor); + } + return null; + } + + /** + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#setBundleVendor(java.lang.String) + * + * {@inheritDoc} + */ + public void setBundleVendor(final String vendor) { + if (manifest != null) { + final Name bundleVendor = new Name(BUNDLE_VENDOR); + if (vendor == null) { + manifest.getMainAttributes().remove(bundleVendor); + } else { + manifest.getMainAttributes().put(bundleVendor, vendor); + } + } + } + + public String getValue(final String key) { + if (manifest != null) { + String value = manifest.getMainAttributes().getValue(key); + if (value == null) { + final Attributes attributes = manifest.getAttributes(key); + if (attributes != null) { + value = attributes.getValue(key); + } + } + return value; + } + return null; + } + + public String getBundleName() { + if (manifest != null) { + final Name bundleName = new Name(BUNDLE_NAME); + final String name = manifest.getMainAttributes().getValue(bundleName); + return name; + } + return null; + } + + public void setBundleName(String newName) { + if (newName == null) { + newName = "noName"; //$NON-NLS-1$ + } + final Name bundleNameName = new Name(BUNDLE_NAME); + manifest.getMainAttributes().put(bundleNameName, newName); + } + + public String getBundleLocalization() { + if (manifest != null) { + final Name bundleLocalization = new Name(BUNDLE_LOCALIZATION); + final String name = manifest.getMainAttributes().getValue(bundleLocalization); + return name; + } + return null; + } + + public void setSingleton(final boolean singleton) { + String value = manifest.getMainAttributes().getValue(BUNDLE_SYMBOLIC_NAME); + final String[] directives = value.split(SEMICOLON); + + if (directives.length == 0) { + return; // This should not happen if the Manifest is well-formed + } else { + value = directives[0]; + boolean isDefined = false; + for (int i = 1; i < directives.length; i++) { + String directive = directives[i]; + if (directive.startsWith(SINGLETON)) { + directive = SINGLETON + singleton; + isDefined = true; + } + value += SEMICOLON + directive; + } + if (!isDefined) { + value += SEMICOLON + SINGLETON + singleton; + } + } + + manifest.getMainAttributes().putValue(BUNDLE_SYMBOLIC_NAME, value); + } + + /** + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#addImportPackage(java.lang.String) + * + * @param packageName + */ + public void addImportPackage(String packageName) { + addImportPackage(packageName, null); + } + + public void addImportPackage(String packageName, String version) { + addPackage(packageName, IMPORT_PACKAGE, version); + } + + /** + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#addExportPackage(java.lang.String) + * + * @param packageName + */ + public void addExportPackage(String packageName) { + addExportPackage(packageName, null); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IManifestEditor#addExportPackage(java.lang.String, java.lang.String) + * + * @param packageName + * @param version + */ + public void addExportPackage(String packageName, String version) { + addPackage(packageName, EXPORT_PACKAGE, version); + } + + /** + * Adds a package name in a manifest header type. + * + * @param packageName the package name to add + * @param type IMPORT_PACKAGE or EXPORT_PACKAGE + */ + private void addPackage(String packageName, String type, String version) { + final Name manifestHeader = new Name(type); + String manifestHeaderValue = manifest.getMainAttributes().getValue(manifestHeader); + + // TODO: Same as addDependency(final String, final String) : Improve the detection of existing packages + + if (manifestHeaderValue == null) { + manifestHeaderValue = packageName; + + if (version != null) { + manifestHeaderValue += SEMICOLON + "version=\"" + version + "\""; + } + } else if (!manifestHeaderValue.contains(packageName)) { + manifestHeaderValue += COMMA + packageName; + + // TODO: Update version + } + + manifest.getMainAttributes().put(manifestHeader, manifestHeaderValue); + } +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IBuildEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IBuildEditor.java new file mode 100644 index 00000000000..960e018af6e --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IBuildEditor.java @@ -0,0 +1,98 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * + * 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr + * + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.interfaces; + +/** + * + * Editor for the build.properties + * + */ +public interface IBuildEditor extends IFileEditor { + + /** + * key for the bin + * + * @Deprecated The build key should be specified for each IBuildEditor instance + */ + @Deprecated + public final static String BUILD_KEY = "bin.includes"; //$NON-NLS-1$ + + /** key for the source folders */ + public final static String SOURCE_FOLDER_KEY = "source.."; //$NON-NLS-1$ + + /** key for the bin folders */ + public static final String BIN_KEY = "bin.."; //$NON-NLS-1$ + + /** name of the file build.properties */ + public static final String BUILD_PROPERTIES_FILE = "build.properties"; //$NON-NLS-1$ + + /** + * The build key for the Eclipse Binary Build + */ + public static final String BINARY_BUILD = "bin.includes"; + + /** + * The build key for the Eclipse Source Build + */ + public static final String SOURCE_BUILD = "src.includes"; + + /** the method to register a new source folder */ + public void registerSourceFolder(String string); + + /** registers a new bin folder */ + public void registerBinFolder(String binFolder); + + /** the method to add an element to the build */ + public void addToBuild(final String path); + + /** removes the given path from the build */ + public void removeFromBuild(final String path); + + /** + * Returns <code>true</code> if the folder is registered as a Source Folder + * + * @param path + * the path of a source folder + * @return + * <code>true</code> if the folder is registered + */ + public boolean isRegisteredSourceFolder(final String path); + + /** + * Returns <code>true</code> if the folder is registered as a Bin Folder + * + * @param path + * the path of a bin folder + * @return + * <code>true</code> if the folder is registered + */ + public boolean isRegisteredBinFolder(final String binFolder); + + /** + * Returns all the registered source folders + * + * @return + * all the registered source folders + */ + public String[] getSourceFolders(); + + /** + * Returns all the files added to the build + * + * @return + * all the files added to the build + */ + public String[] getElementsInBuild(); +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IClasspathEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IClasspathEditor.java new file mode 100644 index 00000000000..e8921a045a1 --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IClasspathEditor.java @@ -0,0 +1,56 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * + * 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr + * + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.interfaces; + + +/** + * + * This interface for the file .classpath + * + */ +public interface IClasspathEditor extends IFileEditor { + + /** the classpath file */ + public static final String CLASSPATH_FILE = ".classpath"; //$NON-NLS-1$ + + /** + * add a source folder to the class path + * + * @param folderPath + */ + public void addSourceFolderToClasspath(final String folderPath); + + /** + * Tests if a folder is already registered in the classpath + * + * @param folderPath + * a folder path + * @return + * <code>true</code> if the folderPath is already registered + */ + public boolean isSourceFolderRegistered(final String folderPath); + + /** + * + * @return the source folders for this classpath + */ + public String[] getSourceFolders(); + + /** + * + * @return the bin folders for this classpath + */ + public String[] getBinFolders(); +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IFeatureProjectEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IFeatureProjectEditor.java new file mode 100644 index 00000000000..f3ebd669f0e --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IFeatureProjectEditor.java @@ -0,0 +1,106 @@ +package org.eclipse.papyrus.eclipse.project.editors.interfaces; + +import java.util.Set; + +import org.w3c.dom.Document; + +public interface IFeatureProjectEditor { + + void init(); + + void save(); + + void createFiles(final Set<String> files); + + /** + * Retrieves the feature.xml XML Document associated to this project. + * + * @return The XML Document associated to this feature.xml file + */ + Document getDocument(); + + /** + * Gets the feature's id. + * + * @return the feature's id + */ + String getId(); + + /** + * Gets the feature's label. + * + * @return the feature's label + */ + String getLabel(); + + /** + * Gets the feature's version. + * + * @return the feature's version + */ + String getVersion(); + + /** + * Gets the feature's provider name. + * + * @return the feature's provider name + */ + String getProviderName(); + + /** + * Gets the operating system of the feature + * @return + */ + String getOS(); + + String getWS(); + + String getNL(); + + String getArch(); + + String getDescriptionText(); + + String getDescriptionURL(); + + String getCopyrightText(); + + String getCopyrightURL(); + + String getLicenseText(); + + String getLicenceURL(); + + void setId(final String id); + + void setLabel(final String label); + + void setVersion(final String version); + + void setProviderName(final String providerName); + + void setOS(final String os); + + void setWS(final String ws); + + void setNL(final String nl); + + void setArch(final String arch); + + void setDescription(final String descriptionURL, final String descriptionDesc); + + void setCopyright(final String copyrightURL, final String copyrightDesc); + + void setLicense(final String licenseURL, final String licenseDesc); + + void setUpdateURL(final String urlLabel, final String url); + + void addPlugin(final String pluginName); + + void addRequiredFeature(final String featureName, final String version); + + void addRequiredPlugin(String pluginName); + + void addInclude(String featureName, String version); + +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IFileEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IFileEditor.java new file mode 100644 index 00000000000..89879fcba2f --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IFileEditor.java @@ -0,0 +1,66 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * + * 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.interfaces; + +import java.util.Set; + +import org.eclipse.core.resources.IProject; + +/** + * + * Interface to edit file + * + */ +public interface IFileEditor { + + /** test if all the required elements exist for the editor */ + public boolean exists(); + + + /** create the required elements for the editor */ + public abstract void create(); + + /** + * a method to initialize the field of the editors + * This method should be called by the programmer AFTER the construciton of the element + */ + public abstract void init(); + + /** + * + * @return + * the missing files for the project + */ + public Set<String> getMissingFiles(); + + /** + * + * @param files + * create the required files + */ + public void createFiles(final Set<String> files); + + + // TODO : Remove that "Throwable" exception... + /** save the modification on the editor */ + public void save(); + + /** + * + * @return + * the eclipse project + */ + public IProject getProject(); + +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IJavaProjectEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IJavaProjectEditor.java new file mode 100644 index 00000000000..6a470f16208 --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IJavaProjectEditor.java @@ -0,0 +1,52 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * + * 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.interfaces; + +/** + * + * Editor for the java project + * + */ +public interface IJavaProjectEditor extends IBuildEditor { + + /** The java nature */ + public static final String JAVA_NATURE = "org.eclipse.jdt.core.javanature"; //$NON-NLS-1$ + + /** the build command for the java project */ + public static final String JAVA_BUILD_COMMAND = "org.eclipse.jdt.core.javabuilder"; //$NON-NLS-1$ + + /** + * Add a new java source folder in the .classpath and in the build.properties + * + * @param path + * the path of the java source folder + */ + public void addJavaSourceFolder(final String path); + + /** + * Gets the classpath editor. + * + * @return the classpath editor + */ + public IClasspathEditor getClasspathEditor(); + + + /** + * Gets the builds the editor. + * + * @return the builds the editor + */ + public IBuildEditor getBuildEditor(); + +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IManifestEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IManifestEditor.java new file mode 100644 index 00000000000..7ffb9f48d01 --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IManifestEditor.java @@ -0,0 +1,229 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * + * 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr + * + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.interfaces; + + + +/** + * + * Editor for the Manifest + * + */ +public interface IManifestEditor extends IProjectEditor, IFileEditor { + + /** the path for the manifest file */ + public static final String MANIFEST_PATH = "META-INF/MANIFEST.MF"; //$NON-NLS-1$ + + /** the path for the META-INF folder */ + public static final String META_INF_PATH = "META-INF/"; //$NON-NLS-1$ + + /** the key for the bundle symbolic name */ + public static final String BUNDLE_SYMBOLIC_NAME = "Bundle-SymbolicName"; //$NON-NLS-1$ + + /** the key for the bundle name */ + public static final String BUNDLE_NAME = "Bundle-Name";//$NON-NLS-1$ + + /** the key for the required bundle */ + public static final String REQUIRED_BUNDLE = "Require-Bundle"; //$NON-NLS-1$ + + /** the key for the bundle version */ + public static final String BUNDLE_VERSION = "Bundle-Version"; //$NON-NLS-1$ + + /** the key for the bundle vendor */ + public static final String BUNDLE_VENDOR = "Bundle-Vendor"; //$NON-NLS-1$ + + /** the build command for the manifest */ + public static final String MANIFEST_BUILD_COMMAND = "org.eclipse.pde.ManifestBuilder"; //$NON-NLS-1$ + + /** the key for the bundle localization */ + public static final String BUNDLE_LOCALIZATION = "Bundle-Localization"; //$NON-NLS-1$ + + /** + * Add a dependency to the MANIFEST + * @param dependency + * the dependency to add + */ + public void addDependency(final String dependency); + + /** + * Add a specific version of a dependency to the MANIFEST + * + * @param dependency + * the dependency to add + * @param version + * the version of the dependency + */ + public void addDependency(final String dependency, final String version); + + /** + * Check whether a dependency is already present in the MANIFEST + * + * @param dependency + * the dependency to check + */ + public boolean hasDependency(final String dependency); + + /** + * + * @param key + * the key + * @param value + * the new value for the key + */ + public void setValue(final String key, final String value); + + public String getValue(final String key); + + /** + * + * @param key + * the key + * @param name + * the name + * @param value + * the new value for the key + */ + public void setValue(final String key, final String name, final String value); + + /** + * + * @param key + * the key + * @param value + * the value to remove for this key + */ + public void removeValue(final String key, final String value); + + /** + * + * @param key + * a key to remove from the manifest + * + */ + public void removeValue(final String key); + + /** + * + * @param name + * the symbolic name for the bundle + */ + public void setSymbolicBundleName(final String name); + + + /** + * + * @return + * the bundle name for the project + */ + public String getSymbolicBundleName(); + + /** + * + * @return + * the symbolic bundle name for the project + */ + public String getBundleName(); + + /** + * + * @param name + * the bundle name for the bundle + */ + public void setBundleName(final String name); + + /** + * + * @return + * the version of the bundle + */ + public String getBundleVersion(); + + /** + * Set the version of the bundle + */ + public void setBundleVersion(final String version); + + /** + * Gets this plug-in's provider + * + * @return + * this plug-in's provider + */ + public String getBundleVendor(); + + /** + * Sets this plug-in's provider + * + * @param vendor + * this plug-in's provider + */ + public void setBundleVendor(final String vendor); + + /** + * Get the bundle localization + * + * @return + * the bundle localization + */ + public String getBundleLocalization(); + + /** + * Sets the Manifest's singleton directive + * + * @param singleton + * Whether this plug-in should be a singleton + */ + public void setSingleton(boolean singleton); + + /** + * + * @param dependencyPattern + * the pattern for the dependency to update + * @param newVersion + * the version for the dependency + */ + public void setDependenciesVersion(final String dependencyPattern, final String newVersion); + + /** + * Adds an import package to the MANIFEST. + * + * @param packageName the package name to add + */ + public void addImportPackage(String packageName); + + /** + * Adds an import package and its version to the MANIFEST. + * + * @param packageName the package name to add + * @param version the package version + */ + public void addImportPackage(String packageName, String version); + + /** + * Adds an export package to the MANFIEST. + * + * @param packageName the package name to add + */ + public void addExportPackage(String packageName); + + /** + * Adds an export package and its version to the MANIFEST. + * + * @param packageName the package name to add + * @param version the package version + */ + public void addExportPackage(String packageName, String version); + +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IPluginProjectEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IPluginProjectEditor.java new file mode 100644 index 00000000000..085ad042945 --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IPluginProjectEditor.java @@ -0,0 +1,58 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * + * 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.interfaces; + +import java.util.List; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; + +/** + * + * Editor for the file plugin.xml + * + */ +public interface IPluginProjectEditor extends IProjectEditor, IFileEditor { + + /** the node extension */ + public static final String EXTENSION = "extension"; //$NON-NLS-1$ + + /** the attribute point */ + public static final String POINT = "point"; //$NON-NLS-1$ + + /** the name of the file plugin.xml */ + public static final String PLUGIN_XML_FILE = "plugin.xml"; //$NON-NLS-1$ + + /** the plugin nature */ + public static final String PLUGIN_NATURE = "org.eclipse.pde.PluginNature"; //$NON-NLS-1$ + + /** the plugin build command */ + public static final String PLUGIN_BUILD_COMMAND = "org.eclipse.pde.SchemaBuilder"; //$NON-NLS-1$ + + /** the method to add an extension to the plugin */ + public Element addExtension(final String extensionPoint); + + /** the method to get all the extensions of the type extensionPoint */ + public List<Node> getExtensions(final String extensionPoint); + + /** the method to set an attribute to an element */ + public void setAttribute(final Element element, final String attributeName, final String attributeValue); + + /** the method to add a child to an element */ + public Element addChild(final Element element, final String childName); + + /** the method to retrieve the plugin.xml XML Document associated to this project */ + public Document getDocument(); +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IProjectEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IProjectEditor.java new file mode 100644 index 00000000000..233c679d40a --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/interfaces/IProjectEditor.java @@ -0,0 +1,93 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * + * 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.interfaces; + +import java.net.URL; +import java.util.Set; + +/** + * + * This interface defines the methods for the ProjectEditor + * + */ +public interface IProjectEditor extends IFileEditor { + + /** the node buildSpec */ + public static final String BUILD_SPEC = "buildSpec"; //$NON-NLS-1$ + + /** the node comment */ + public static final String COMMENT = "comment"; //$NON-NLS-1$ + + /** the node name */ + public static final String NAME = "name"; //$NON-NLS-1$ + + /** the node nature */ + public static final String NATURE = "nature"; //$NON-NLS-1$ + + /** the node project description */ + public static final String PROJECT_DESCRIPTION = "projectDescription"; //$NON-NLS-1$ + + /** the name of the project file */ + public static final String PROJECT_FILE = ".project"; //$NON-NLS-1$ + + + + /** + * + * @return + * the missing nature for the project + */ + public Set<String> getMissingNature(); + + /** + * + * @param nature + * a nature + * @return + * <code>true</code> if the project has this nature + */ + public boolean hasNature(final String nature); + + /** + * + * @return + * the missing build command + */ + public Set<String> getMissingBuildCommand(); + + /** + * + * @param commands + * the command to add + */ + public void addBuildCommands(Set<String> commands); + + /** + * + * @param command + * a build command + * @return + * <code>true</code> if the project has the build command + */ + public boolean hasBuildCommand(String command); + + /** + * + * @param url + * the url of the file to copy + * @param fileDestinationPath + * the destination path for the added file + */ + public void addFile(final URL url, final String fileDestinationPath, final boolean eraseExistingFile); +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/AbstractProjectEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/AbstractProjectEditor.java new file mode 100644 index 00000000000..e01a43355b0 --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/AbstractProjectEditor.java @@ -0,0 +1,195 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * + * 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.project; + +import java.util.ArrayList; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Iterator; +import java.util.LinkedList; +import java.util.List; +import java.util.Set; + +import org.eclipse.core.internal.events.BuildCommand; +import org.eclipse.core.resources.ICommand; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.IProjectDescription; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.papyrus.eclipse.project.editors.Activator; +import org.eclipse.papyrus.eclipse.project.editors.file.AbstractFileEditor; +import org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor; + +/** + * + * This class provides useful method for project editor + * + */ +public abstract class AbstractProjectEditor extends AbstractFileEditor implements IProjectEditor { + + + /** the header for XML files */ + public static final String XML_HEADER = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n"; //$NON-NLS-1$ + + /** + * the project description + */ + private final IProjectDescription description; + + /** + * + * Constructor. + * + * @param project + * the eclipse project + * @throws CoreException + */ + public AbstractProjectEditor(final IProject project) throws CoreException { + super(project); + this.description = getProject().getDescription(); + } + + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.AbstractProjectEditor.plugin.AbstractEditor#exists() + * + * {@inheritDoc} + */ + @Override + public boolean exists() { + return super.exists() && getMissingNature().size() == 0 && getMissingBuildCommand().size() == 0; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.file.AbstractFileEditor#create() + * + * {@inheritDoc} + */ + @Override + public void create() { + createFiles(getMissingFiles()); + addNatures(getMissingNature()); + addBuildCommands(getMissingBuildCommand()); + init(); + } + + /** + * + * @param nature + * a nature + * @return + * <code>true</code> if the project has the wanted nature + */ + public boolean hasNature(final String nature) { + List<String> natures = new LinkedList<String>(Arrays.asList(this.description.getNatureIds())); + return natures.contains(nature); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor#getMissingNature() + * + * {@inheritDoc} + */ + public Set<String> getMissingNature() { + return new HashSet<String>(); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor#addNatures(java.util.List) + * + * {@inheritDoc} + */ + public void addNatures(final Set<String> natures) { + List<String> existingNatures = new LinkedList<String>(Arrays.asList(this.description.getNatureIds())); + Iterator<String> iter = natures.iterator(); + while (iter.hasNext()) { + String nature = iter.next(); + if (!existingNatures.contains(nature)) { + existingNatures.add(nature); + } + } + this.description.setNatureIds(existingNatures.toArray(new String[existingNatures.size()])); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.AbstractProjectEditor#hasBuildCommand(java.lang.String) + * + * {@inheritDoc} + */ + public boolean hasBuildCommand(final String command) { + ICommand[] buildSpec = this.description.getBuildSpec(); + for (int i = 0; i < buildSpec.length; i++) { + if (buildSpec[i].getBuilderName().equals(command)) { + return true; + } + } + return false; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.AbstractProjectEditor#addBuildCommands(java.util.Set) + * + * {@inheritDoc} + */ + public void addBuildCommands(final Set<String> commands) { + ICommand[] buildSpec = this.description.getBuildSpec(); + + List<ICommand> buildSpecList = new ArrayList<ICommand>(); + buildSpecList.addAll(Arrays.asList(buildSpec)); + + Iterator<String> iter = commands.iterator(); + while (iter.hasNext()) { + String name = iter.next(); + if (!hasBuildCommand(name)) { + ICommand cmd = new BuildCommand(); + cmd.setBuilderName(name); + buildSpecList.add(cmd); + } + } + this.description.setBuildSpec(buildSpecList.toArray(new ICommand[buildSpecList.size()])); + } + + /** + * + * @throws Throwable + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor#save() + * + * {@inheritDoc} + */ + public void save() { + if (this.description != null) { + try { + getProject().setDescription(this.description, null); + } catch (CoreException e) { + Activator.log.error(e); + } + } + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor#getMissingBuildCommand() + * + * {@inheritDoc} + */ + public Set<String> getMissingBuildCommand() { + return new HashSet<String>(); + } + +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/FeatureProjectEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/FeatureProjectEditor.java new file mode 100644 index 00000000000..9a828ba2622 --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/FeatureProjectEditor.java @@ -0,0 +1,552 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * + * 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.project; + +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; +import java.util.Collections; +import java.util.Properties; +import java.util.Set; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.papyrus.eclipse.project.editors.Activator; +import org.eclipse.papyrus.eclipse.project.editors.interfaces.IFeatureProjectEditor; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +public class FeatureProjectEditor extends ProjectEditor implements IFeatureProjectEditor { + + /** the name of the file feature.xml */ + public static final String FRAGMENT_XML_FILE = "feature.xml"; //$NON-NLS-1$ + + private static final String ID = "id"; //$NON-NLS-1$ + private static final String LABEL = "label"; //$NON-NLS-1$ + private static final String VERSION = "version"; //$NON-NLS-1$ + private static final String PROVIDER = "provider-name"; //$NON-NLS-1$ + + private static final String URL = "url"; //$NON-NLS-1$ + private static final String COPYRIGHT = "copyright"; //$NON-NLS-1$ + private static final String LICENSE = "license"; //$NON-NLS-1$ + private static final String DESCRIPTION = "description"; //$NON-NLS-1$ + + private static final String OS = "os"; //$NON-NLS-1$ + private static final String WS = "ws"; //$NON-NLS-1$ + private static final String NL = "nl"; //$NON-NLS-1$ + private static final String ARCH = "arch"; //$NON-NLS-1$ + private static final String UPDATE = "update"; //$NON-NLS-1$ + + private static final String PLUGIN = "plugin"; //$NON-NLS-1$ + private static final String IMPORT = "import"; //$NON-NLS-1$ + private static final String INCLUDES = "includes"; //$NON-NLS-1$ + private static final String REQUIRES = "requires"; //$NON-NLS-1$ + private static final String FEATURE = "feature"; //$NON-NLS-1$ + + // TODO pour l'externalization : utiliser l'éditeur de Properties! dans java Utils + + private Document fragmentXML; + + private IFile fragmentFile; + + private Element fragmentRoot; + + /** + * Constructor. + * + * @param project the eclipse project + * @throws ParserConfigurationException + * @throws SAXException + * @throws IOException + * @throws CoreException + */ + public FeatureProjectEditor(final IProject project) throws ParserConfigurationException, SAXException, IOException, CoreException { + super(project); + } + + @Override + public void init() { + fragmentFile = getFeature(); + if (fragmentFile != null && fragmentFile.exists()) { + final DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance(); + try { + DocumentBuilder documentBuilder = documentFactory.newDocumentBuilder(); + fragmentXML = documentBuilder.parse(fragmentFile.getLocation().toOSString()); + fragmentRoot = fragmentXML.getDocumentElement(); + } catch (final ParserConfigurationException e) { + Activator.log.error(e); + } catch (final SAXException e) { + Activator.log.error(e); + } catch (final IOException e) { + Activator.log.error(e); + } + } + } + + @Override + public void createFiles(final Set<String> files) { + if (files.contains(FRAGMENT_XML_FILE)) { + fragmentFile = getProject().getFile(FRAGMENT_XML_FILE); + if (!fragmentFile.exists()) { + InputStream content = getInputStream("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<feature>\n</feature>\n\n"); //$NON-NLS-1$ + + try { + fragmentFile.create(content, true, null); + } catch (CoreException e) { + Activator.log.error(e); + } + } + } + } + + @Override + public boolean exists() { + return getFeature().exists() && super.exists(); + } + + public void setAttribute(final Element element, final String attributeName, final String attributeValue) { + element.setAttribute(attributeName, attributeValue); + } + + /** + * @return the feature.xml file if it exists + */ + private IFile getFeature() { + final IFile fragment = getProject().getFile(FRAGMENT_XML_FILE); + + if (fragment.exists()) { + return fragment; + } + + return null; + } + + @Override + public void save() { + if (exists()) { + try { + final TransformerFactory transformerFactory = TransformerFactory.newInstance(); + final Transformer transformer = transformerFactory.newTransformer(); + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$ + final StreamResult result = new StreamResult(new StringWriter()); + final DOMSource source = new DOMSource(fragmentXML); + transformer.transform(source, result); + + final InputStream inputStream = getInputStream(result.getWriter().toString()); + fragmentFile.setContents(inputStream, true, true, null); + } catch (final TransformerException ex) { + Activator.log.error(ex); + } catch (final CoreException ex) { + Activator.log.error(ex); + } + } + super.save(); + } + + @Override + public Set<String> getMissingNature() { + // TODO + return Collections.emptySet(); + } + + @Override + public Set<String> getMissingFiles() { + // TODO + return Collections.emptySet(); + } + + @Override + public Set<String> getMissingBuildCommand() { + // TODO + return Collections.emptySet(); + } + + public Document getDocument() { + return fragmentXML; + } + + public String getId() { + return fragmentRoot.getAttribute(ID); + } + + public String getLabel() { + return fragmentRoot.getAttribute(LABEL); + } + + public String getVersion() { + return fragmentRoot.getAttribute(VERSION); + } + + public String getProviderName() { + return fragmentRoot.getAttribute(PROVIDER); + } + + public String getDescriptionText() { + // TODO Auto-generated method stub + return null; + } + + public String getDescriptionURL() { + // TODO Auto-generated method stub + return null; + } + + public String getCopyrightURL() { + final Element copyrightNode = getNode(COPYRIGHT); + if (copyrightNode != null) { + final String value = copyrightNode.getAttribute("url"); + if (value != null && value.startsWith("%")) { + final IFile file = getProject().getFile("feature.properties"); + final Properties prop = new Properties(); // TODO create a method to use Properties for others fields too + try { + prop.load(file.getContents()); + } catch (final IOException e) { + Activator.log.error(e); + } catch (final CoreException e) { + Activator.log.error(e); + } + final Object val = prop.get("url"); + if (val != null) { + return (String) val; + } + } + return copyrightNode.getAttribute("url"); + } + return null; + } + + public String getCopyrightText() { + final Element copyrightNode = getNode(COPYRIGHT); + + return copyrightNode != null ? copyrightNode.getTextContent() : null; + } + + public String getLicenseText() { + // TODO Auto-generated method stub + return null; + } + + public String getLicenceURL() { + // TODO Auto-generated method stub + return null; + } + + public String getOS() { + return fragmentRoot.getAttribute(OS); + } + + public String getWS() { + return fragmentRoot.getAttribute(WS); + } + + public String getNL() { + return fragmentRoot.getAttribute(NL); + } + + public String getArch() { + return fragmentRoot.getAttribute(ARCH); + } + + public void setId(final String id) { + fragmentRoot.setAttribute(ID, id); + } + + public void setLabel(final String label) { + fragmentRoot.setAttribute(LABEL, label); + } + + public void setVersion(final String version) { + fragmentRoot.setAttribute(VERSION, version); + } + + public void setProviderName(final String providerName) { + fragmentRoot.setAttribute(PROVIDER, providerName); + } + + public void setDescription(final String descriptionURL, final String description) { + if (exists()) { + Element extension = getNode(DESCRIPTION); + + if (extension == null) { + extension = fragmentXML.createElement(DESCRIPTION); + fragmentRoot.appendChild(extension); + } + + extension.setAttribute(URL, descriptionURL); + extension.setTextContent(description); + } + } + + public void setCopyright(final String copyrightURL, final String copyrightDesc) { + setURLNode(COPYRIGHT, copyrightURL, copyrightDesc); + } + + public void setLicense(final String licenseURL, final String licenseDesc) { + setURLNode(LICENSE, licenseURL, licenseDesc); + } + + public void setOS(final String os) { + fragmentRoot.setAttribute(OS, os); + } + + public void setWS(final String ws) { + fragmentRoot.setAttribute(WS, ws); + } + + public void setNL(final String nl) { + fragmentRoot.setAttribute(NL, nl); + } + + public void setArch(final String architecture) { + fragmentRoot.setAttribute(ARCH, architecture); + } + + public void setUpdateURL(final String urlLabel, final String url) { + Element urlNode = getNode(URL); + + if (urlNode == null) { + urlNode = createElement(URL); + fragmentRoot.appendChild(urlNode); + } + + Element updateNode = getNodeChild(UPDATE, urlNode); + if (updateNode == null) { + updateNode = createElement(UPDATE); + urlNode.appendChild(updateNode); + } + + updateNode.setAttribute(LABEL, urlLabel); + updateNode.setAttribute(URL, url); + } + + public void addPlugin(final String pluginName) { + // Get the plug-in element or create it if it does not exist + Element pluginNode = getPlugin(pluginName); + + if (pluginNode == null) { + pluginNode = createElement(PLUGIN); + fragmentRoot.appendChild(pluginNode); + } + + // Set the id on the element + pluginNode.setAttribute(ID, pluginName); + } + + public void addRequiredFeature(final String featureName, final String version) { + // Make sure the "requires" element exists + Element requires = getNode(REQUIRES); + + if (requires == null) { + requires = createElement(REQUIRES); + fragmentRoot.appendChild(requires); + } + + // Get or create the required feature element + Element feature = getRequiredFeature(featureName); + + if (feature == null) { + feature = createElement(IMPORT); + requires.appendChild(feature); + } + + // Set the element values + feature.setAttribute(FEATURE, featureName); + feature.setAttribute(VERSION, version); + } + + public void addRequiredPlugin(final String pluginName) { + // Make sure the "requires" element exists + Element requires = getNode(REQUIRES); + + if (requires == null) { + requires = createElement(REQUIRES); + fragmentRoot.appendChild(requires); + } + + // Get or create the plug-in element + Element plugin = getRequiredPlugin(pluginName); + + if (plugin == null) { + plugin = createElement(IMPORT); + requires.appendChild(plugin); + } + + plugin.setAttribute(PLUGIN, pluginName); + } + + public void addInclude(final String featureName, final String version) { + Element includeNode = getInclude(featureName); + + if (includeNode == null) { + includeNode = createElement(INCLUDES); + fragmentRoot.appendChild(includeNode); + } + + includeNode.setAttribute(ID, featureName); + includeNode.setAttribute(VERSION, version); + } + + /** + * Creates an element and returns it. + * + * @param elementName the name of the element to create + * @return the created element + */ + private Element createElement(String elementName) { + return fragmentXML.createElement(elementName); + } + + protected void setURLNode(final String nodeName, final String url, final String description) { + if (exists()) { + Element extension = getNode(nodeName); + if (extension == null) { + extension = fragmentXML.createElement(nodeName); + if (url != null) { + extension.setAttribute(URL, url); + } + extension.setTextContent(description); + fragmentRoot.appendChild(extension); + } else { + if (url != null) { + extension.setAttribute(URL, url); + } + extension.setTextContent(description); + } + } + } + + /** + * Gets an element inside a parent element. + * + * @param parentElement + * @param nodeName the node name of the element + * @param attributeValue the value of the element's attribute to retrieve + * @return the element or null if it does not exist + */ + private Element getElement(final Element parentElement, final String nodeName, final String attributeName, final String attributeValue) { + NodeList childNodes = parentElement.getChildNodes(); + + for (int i = 0; i < childNodes.getLength(); i++) { + Node item = childNodes.item(i); + + if (nodeName.equals(item.getNodeName())) { + if (attributeValue.equals(getNodeAttribute(item, attributeName))) { + if (item instanceof Element) { + return (Element) item; + } + } + } + } + + return null; + } + + private Element getNodeChild(final String childName, final Element node) { + NodeList childNodes = node.getChildNodes(); + + if (childNodes == null) { + return null; + } + + for (int i = 0; i < childNodes.getLength(); i++) { + Node item = childNodes.item(i); + + if (item.getNodeName().equals(childName)) { + if (item instanceof Element) { + return (Element) item; + } + } + } + + return null; + } + + /** + * Gets a node element inside the root element. + * + * @param nodeName the node name + * @return the node element or null if it does not exist. + */ + private Element getNode(final String nodeName) { + if (exists()) { + final NodeList nodes = fragmentRoot.getChildNodes(); + for (int i = 0; i < nodes.getLength(); i++) { + final Node item = nodes.item(i); + if (item instanceof NodeList) { + final String name = item.getNodeName(); + if (name.equals(nodeName)) { + if (item instanceof Element) { + return (Element) item; + } + } + } + } + } + + return null; + } + + private Element getPlugin(String pluginName) { + return getElement(fragmentRoot, PLUGIN, ID, pluginName); + } + + private Element getInclude(String featureName) { + return getElement(fragmentRoot, INCLUDES, ID, featureName); + } + + /** + * @param pluginName + * @return + */ + private Element getRequiredPlugin(String pluginName) { + Element requires = getNode(REQUIRES); + + if (requires != null) { + return getElement(requires, IMPORT, PLUGIN, pluginName); + } + + return null; + } + + private String getNodeAttribute(Node node, String name) { + Node attribute = node.getAttributes().getNamedItem(name); + + return attribute != null ? attribute.getNodeValue() : null; + } + + private Element getRequiredFeature(String featureName) { + Element requires = getNode(REQUIRES); + + if (requires != null) { + return getElement(requires, IMPORT, FEATURE, featureName); + } + + return null; + } + +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/JavaProjectEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/JavaProjectEditor.java new file mode 100644 index 00000000000..e53e0677537 --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/JavaProjectEditor.java @@ -0,0 +1,272 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * + * 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: + * Vincent Lorenzo (CEA LIST) vincent.lorenzo@cea.fr - Initial API and implementation + * + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.project; + +import java.util.Set; + +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.jdt.core.IJavaProject; +import org.eclipse.jdt.core.JavaCore; +import org.eclipse.jdt.core.JavaModelException; +import org.eclipse.papyrus.eclipse.project.editors.Activator; +import org.eclipse.papyrus.eclipse.project.editors.file.BuildEditor; +import org.eclipse.papyrus.eclipse.project.editors.file.ClasspathEditor; +import org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor; +import org.eclipse.papyrus.eclipse.project.editors.interfaces.IClasspathEditor; +import org.eclipse.papyrus.eclipse.project.editors.interfaces.IJavaProjectEditor; + +/** + * + * This editor allows to manage a JavaProject + * + */ +public class JavaProjectEditor extends ProjectEditor implements IJavaProjectEditor { + + + /** the java project */ + private final IJavaProject javaProject; + + + + /** + * Gets the classpath editor. + * + * @return the classpath editor + */ + public IClasspathEditor getClasspathEditor() { + return classpathEditor; + } + + + /** + * Gets the builds the editor. + * + * @return the builds the editor + */ + public IBuildEditor getBuildEditor() { + return buildEditor; + } + + /** the classpath editor */ + private final IClasspathEditor classpathEditor; + + /** the build editor */ + private final IBuildEditor buildEditor; + + /** + * + * Constructor. + * + * @param project + * the eclipse project + * @throws CoreException + */ + public JavaProjectEditor(final IProject project) throws CoreException { + super(project); + this.javaProject = JavaCore.create(project); + this.buildEditor = new BuildEditor(project); + this.classpathEditor = new ClasspathEditor(this.javaProject); + } + + /** + * + * @return + * <code>true</code> if the project is a java project : + * + */ + @Override + public boolean exists() { + return super.exists() && this.javaProject.exists(); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#getMissingFiles() + * + * {@inheritDoc} + */ + @Override + public Set<String> getMissingFiles() { + Set<String> files = super.getMissingFiles(); + files.addAll(this.classpathEditor.getMissingFiles()); + files.addAll(this.buildEditor.getMissingFiles()); + return files; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.AbstractProjectEditor#getMissingNature() + * + * {@inheritDoc} + */ + @Override + public Set<String> getMissingNature() { + Set<String> natures = super.getMissingNature(); + if (!hasNature(JAVA_NATURE)) { + natures.add(JAVA_NATURE); + } + return natures; + } + + @Override + public void init() { + // TODO Auto-generated method stub + super.init(); + this.classpathEditor.init(); + this.buildEditor.init(); + } + + @Override + public void createFiles(final Set<String> files) { + super.createFiles(files); + this.classpathEditor.createFiles(files); + this.buildEditor.createFiles(files); + } + + + + /** + * save the modification + * + * @throws Throwable + */ + @Override + public void save() { + super.save(); + try { + this.javaProject.save(new NullProgressMonitor(), true); + } catch (JavaModelException ex) { + Activator.log.error(ex); + } + this.classpathEditor.save(); + this.buildEditor.save(); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IJavaProjectEditor#addJavaSourceFolder(java.lang.String) + * + * {@inheritDoc} + */ + public void addJavaSourceFolder(final String path) { + if (exists()) { + // we add this source folder to the class path + // String classPath = "/" + this.project.getName()+"/" + path; + String classPath = path; + this.classpathEditor.addSourceFolderToClasspath(classPath); + + // we add this source folder in the build file + this.buildEditor.registerSourceFolder(path + "/"); //$NON-NLS-1$ + } + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#registerSourceFolder(java.lang.String) + * + * {@inheritDoc} + */ + public void registerSourceFolder(final String string) { + this.buildEditor.registerSourceFolder(string); + + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#addToBuild(java.lang.String) + * + * {@inheritDoc} + */ + public void addToBuild(final String path) { + this.buildEditor.addToBuild(path); + + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#isRegistred(java.lang.String) + * + * {@inheritDoc} + */ + public boolean isRegisteredSourceFolder(final String path) { + return this.buildEditor.isRegisteredSourceFolder(path); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#getSourceFolders() + * + * {@inheritDoc} + */ + public String[] getSourceFolders() { + return this.buildEditor.getSourceFolders(); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.file.AbstractFileEditor#getMissingBuildCommand() + * + * {@inheritDoc} + */ + @Override + public Set<String> getMissingBuildCommand() { + Set<String> buildCommand = super.getMissingBuildCommand(); + if (!hasBuildCommand(IJavaProjectEditor.JAVA_BUILD_COMMAND)) { + buildCommand.add(IJavaProjectEditor.JAVA_BUILD_COMMAND); + } + return buildCommand; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#removeFromBuild(java.lang.String) + * + * {@inheritDoc} + */ + public void removeFromBuild(String path) { + buildEditor.removeFromBuild(path); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#getElementsInBuild() + * + * {@inheritDoc} + */ + public String[] getElementsInBuild() { + return buildEditor.getElementsInBuild(); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#registerBinFolder(java.lang.String) + * + * {@inheritDoc} + */ + public void registerBinFolder(String binFolder) { + buildEditor.registerBinFolder(binFolder); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IBuildEditor#isRegisteredBinFolder(java.lang.String) + * + * {@inheritDoc} + */ + public boolean isRegisteredBinFolder(String binFolder) { + return buildEditor.isRegisteredBinFolder(binFolder); + } +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/PluginProjectEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/PluginProjectEditor.java new file mode 100644 index 00000000000..fd9563ad056 --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/PluginProjectEditor.java @@ -0,0 +1,307 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * 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: + * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation + * Vincent Lorenzot (CEA-LIST) vincent.lorenzo@cea.fr + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.project; + +import java.io.IOException; +import java.io.InputStream; +import java.io.StringWriter; +import java.util.ArrayList; +import java.util.List; +import java.util.Set; + +import javax.xml.parsers.DocumentBuilder; +import javax.xml.parsers.DocumentBuilderFactory; +import javax.xml.parsers.ParserConfigurationException; +import javax.xml.transform.OutputKeys; +import javax.xml.transform.Transformer; +import javax.xml.transform.TransformerException; +import javax.xml.transform.TransformerFactory; +import javax.xml.transform.dom.DOMSource; +import javax.xml.transform.stream.StreamResult; + +import org.eclipse.core.resources.IFile; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.papyrus.eclipse.project.editors.Activator; +import org.eclipse.papyrus.eclipse.project.editors.interfaces.IPluginProjectEditor; +import org.w3c.dom.Document; +import org.w3c.dom.Element; +import org.w3c.dom.NamedNodeMap; +import org.w3c.dom.Node; +import org.w3c.dom.NodeList; +import org.xml.sax.SAXException; + +/** + * + * This editor allows to edit the plugin file + * + */ +public class PluginProjectEditor extends ProjectEditor implements IPluginProjectEditor { + + private Document pluginXML; + + private IFile pluginFile; + + private Element pluginRoot; + + /** + * + * Constructor. + * + * @param project + * the eclipse project + * @throws ParserConfigurationException + * @throws SAXException + * @throws IOException + * @throws CoreException + */ + public PluginProjectEditor(final IProject project) throws ParserConfigurationException, SAXException, IOException, CoreException { + super(project); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#init() + * + * {@inheritDoc} + */ + @Override + public void init() { + this.pluginFile = getPlugin(); + if (this.pluginFile != null && this.pluginFile.exists()) { + DocumentBuilderFactory documentFactory = DocumentBuilderFactory.newInstance(); + DocumentBuilder documentBuilder; + try { + documentBuilder = documentFactory.newDocumentBuilder(); + this.pluginXML = documentBuilder.parse(this.pluginFile.getLocation().toOSString()); + this.pluginRoot = this.pluginXML.getDocumentElement(); + } catch (ParserConfigurationException e) { + Activator.log.error(e); + } catch (SAXException e) { + Activator.log.error(e); + } catch (IOException e) { + Activator.log.error(e); + } + } + } + + /** + * Create the file plugin.xml + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#createFiles(Set) + * + * {@inheritDoc} + */ + @Override + public void createFiles(final Set<String> files) { + if (files.contains(PLUGIN_XML_FILE)) { + IFile plugin = getProject().getFile(PLUGIN_XML_FILE); + if (!plugin.exists()) { + InputStream is = getInputStream("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<?eclipse version=\"3.4\"?>\n" + "<plugin>\n" + "</plugin>\n"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + try { + plugin.create(is, true, null); + } catch (CoreException e) { + Activator.log.error(e); + } + } + } + super.createFiles(files); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.AbstractProjectEditor.plugin.AbstractEditor#exists() + * + * {@inheritDoc} + */ + @Override + public boolean exists() { + IFile plugin = getProject().getFile(PLUGIN_XML_FILE); + return plugin.exists() && super.exists(); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IPluginProjectEditor#addExtension(java.lang.String) + * + * {@inheritDoc} + */ + public Element addExtension(final String extensionPoint) { + if (exists()) { + Element extension = this.pluginXML.createElement(EXTENSION); + extension.setAttribute(POINT, extensionPoint); + this.pluginRoot.appendChild(extension); + return extension; + } + return null; + } + + /** + * Returns the list of the registered extension with this extension point + * + * @param extensionPoint + * the name of an extension point + * @return + * the list of the registered extension with this extension point + */ + public List<Node> getExtensions(final String extensionPoint) { + if (exists()) { + NodeList nodes = this.pluginRoot.getChildNodes(); + List<Node> extensions = new ArrayList<Node>(); + for (int i = 0; i < nodes.getLength(); i++) { + Node item = nodes.item(i); + if (item instanceof NodeList) { + String name = item.getNodeName(); + if (name.equals(EXTENSION)) { + NamedNodeMap attributes = item.getAttributes(); + Node point = attributes.getNamedItem(POINT); + if (extensionPoint.equals(point.getNodeValue())) { + if (item instanceof Node) { + extensions.add(item); + } + } + } + } + } + return extensions; + } + return null; + } + + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IPluginProjectEditor#setAttribute(org.w3c.dom.Element, java.lang.String, java.lang.String) + * + * {@inheritDoc} + */ + public void setAttribute(final Element element, final String attributeName, final String attributeValue) { + element.setAttribute(attributeName, attributeValue); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IPluginProjectEditor#addChild(org.w3c.dom.Element, java.lang.String) + * + * {@inheritDoc} + */ + public Element addChild(final Element element, final String childName) { + Element child = this.pluginXML.createElement(childName); + element.appendChild(child); + return child; + } + + /** + * + * @return + * the plugin file if it exists + */ + private IFile getPlugin() { + IFile plugin = getProject().getFile(PLUGIN_XML_FILE); + if (plugin.exists()) { + return plugin; + } + return null; + } + + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#save() + * + * {@inheritDoc} + */ + @Override + public void save() { + if (exists()) { + try { + TransformerFactory transformerFactory = TransformerFactory.newInstance(); + Transformer transformer = transformerFactory.newTransformer(); + transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); //$NON-NLS-1$ + transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$ + transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", //$NON-NLS-1$ + "3"); //$NON-NLS-1$ + StreamResult result = new StreamResult(new StringWriter()); + DOMSource source = new DOMSource(this.pluginXML); + transformer.transform(source, result); + + String resultAsString = result.getWriter().toString(); + if (!resultAsString.endsWith("\n")) { //$NON-NLS-1$ + resultAsString += "\n"; //$NON-NLS-1$ + } + InputStream inputStream = getInputStream(resultAsString); + this.pluginFile.setContents(inputStream, true, true, null); + } catch (TransformerException ex) { + Activator.log.error(ex); + } catch (CoreException ex) { + Activator.log.error(ex); + } + } + super.save(); + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#getMissingNature() + * + * {@inheritDoc} + */ + @Override + public Set<String> getMissingNature() { + Set<String> natures = super.getMissingNature(); + if (!hasNature(PLUGIN_NATURE)) { + natures.add(PLUGIN_NATURE); + } + return natures; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.project.ProjectEditor#getMissingFiles() + * + * {@inheritDoc} + */ + @Override + public Set<String> getMissingFiles() { + Set<String> files = super.getMissingFiles(); + IFile plugin = getProject().getFile(PLUGIN_XML_FILE); + if (!plugin.exists()) { + files.add(PLUGIN_XML_FILE); + } + return files; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.file.AbstractFileEditor#getMissingBuildCommand() + * + * {@inheritDoc} + */ + @Override + public Set<String> getMissingBuildCommand() { + Set<String> commands = super.getMissingBuildCommand(); + if (!hasBuildCommand(PLUGIN_BUILD_COMMAND)) { + commands.add(PLUGIN_BUILD_COMMAND); + } + return commands; + } + + /** + * @return The XML Document associated to this plugin.xml file + * @see PluginProjectEditor#init() + * @see PluginProjectEditor#create() + */ + public Document getDocument() { + return pluginXML; + } +} diff --git a/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/ProjectEditor.java b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/ProjectEditor.java new file mode 100644 index 00000000000..253339a1b4f --- /dev/null +++ b/plugins/editor/org.eclipse.papyrus.eclipse.project.editors/src/org/eclipse/papyrus/eclipse/project/editors/project/ProjectEditor.java @@ -0,0 +1,151 @@ +/***************************************************************************** + * Copyright (c) 2011 CEA LIST. + * + * 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: + * Camille Letavernier (CEA LIST) camille.letavernier@cea.fr - Initial API and implementation + *****************************************************************************/ +package org.eclipse.papyrus.eclipse.project.editors.project; + +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; +import java.util.Set; + +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.runtime.CoreException; +import org.eclipse.core.runtime.NullProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.eclipse.papyrus.eclipse.project.editors.Activator; +import org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor; + +/** + * + * This class allows to manage the eclipse project + * + */ +public class ProjectEditor extends AbstractProjectEditor implements IProjectEditor { + + + /** + * + * Constructor. + * + * @param project + * the eclipse project + * @throws CoreException + */ + public ProjectEditor(final IProject project) throws CoreException { + super(project); + } + + /** + * + * Constructor. + * + * @param folder + * a folder + * @throws CoreException + */ + public ProjectEditor(final IFolder folder) throws CoreException { + super(null); + // TODO : create an action to import a folder as a project! + // this will allow to test the create method + throw new UnsupportedOperationException(); + } + + + + /** + * Create the project file + * TODO NOT TESTED + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor#createFiles(Set) + * + * {@inheritDoc} + */ + public void createFiles(final Set<String> files) { + if (files.contains(PROJECT_FILE)) { + final IFile file = getProject().getFile(PROJECT_FILE); + if (!file.exists()) { + String input = ""; //$NON-NLS-1$ + input += AbstractProjectEditor.XML_HEADER; + input += "<" + NAME + ">"; //$NON-NLS-1$ //$NON-NLS-2$ + input += getProject().getName(); + input += "</" + NAME + ">"; //$NON-NLS-1$ //$NON-NLS-2$ + input += "<" + COMMENT + ">"; //$NON-NLS-1$ //$NON-NLS-2$ + input += "</" + COMMENT + ">"; //$NON-NLS-1$ //$NON-NLS-2$ + input += "<" + BUILD_SPEC + ">"; //$NON-NLS-1$ //$NON-NLS-2$ + input += "</" + BUILD_SPEC + ">"; //$NON-NLS-1$ //$NON-NLS-2$ + input += "<" + NATURE + ">"; //$NON-NLS-1$ //$NON-NLS-2$ + input += "</" + NATURE + ">"; //$NON-NLS-1$ //$NON-NLS-2$ + + try { + file.create(getInputStream(input), true, null); + } catch (final CoreException e) { + Activator.log.error(e); + } + } + } + } + + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor#getMissingFiles() + * + * {@inheritDoc} + */ + @Override + public Set<String> getMissingFiles() { + final Set<String> missingFile = super.getMissingFiles(); + final IFile projectFile = getProject().getFile(PROJECT_FILE); + if (!projectFile.exists()) { + missingFile.add(IProjectEditor.PROJECT_FILE); + } + return missingFile; + } + + /** + * + * @see org.eclipse.papyrus.eclipse.project.editors.interfaces.IProjectEditor#addFile(java.net.URL, java.lang.String) + * + * @param url + * @param fileDestinationPath + * @param eraseExitingFile + */ + public void addFile(final URL url, final String fileDestinationPath, final boolean eraseExitingFile) { + final IFile targetFile = getProject().getFile(new Path(fileDestinationPath)); + if (targetFile.exists()) { + if (eraseExitingFile) { + try { + targetFile.delete(true, new NullProgressMonitor()); + } catch (final CoreException e) { + Activator.log.error(e); + } + } else { + return; + } + } + try { + final InputStream is = url.openStream(); + ; + targetFile.create(is, false, new NullProgressMonitor()); + is.close(); + targetFile.refreshLocal(IResource.DEPTH_ZERO, new NullProgressMonitor()); + } catch (final CoreException e) { + Activator.log.error(e); + } catch (final IOException e) { + Activator.log.error(e); + } + + + } +} |