182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll/* 282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * Copyright (C) 2011 The Android Open Source Project 382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * 482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * Licensed under the Eclipse Public License, Version 1.0 (the "License"); 582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * you may not use this file except in compliance with the License. 682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * You may obtain a copy of the License at 782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * 882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * http://www.eclipse.org/org/documents/epl-v10.php 982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * 1082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * Unless required by applicable law or agreed to in writing, software 1182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * distributed under the License is distributed on an "AS IS" BASIS, 1282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * See the License for the specific language governing permissions and 1482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll * limitations under the License. 1582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll */ 1682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 1782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollpackage com.android.ide.eclipse.ndk.internal.templates; 1882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 1982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport com.android.ide.eclipse.ndk.internal.Activator; 2082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport com.android.ide.eclipse.ndk.internal.Messages; 2182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 2282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.cdt.core.templateengine.TemplateCore; 2382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.cdt.core.templateengine.process.ProcessArgument; 2482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.cdt.core.templateengine.process.ProcessFailureException; 2582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.cdt.core.templateengine.process.ProcessRunner; 2682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.resources.IContainer; 2782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.resources.IFile; 2882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.resources.IFolder; 2982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.resources.IProject; 3082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.resources.ResourcesPlugin; 3182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.runtime.CoreException; 3282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.runtime.FileLocator; 3382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.runtime.IProgressMonitor; 3482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.eclipse.core.runtime.Path; 3582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport org.osgi.framework.Bundle; 3682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 3782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport java.io.IOException; 3882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport java.net.URL; 3982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport java.util.ArrayList; 4082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollimport java.util.List; 4182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 4282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Mollpublic class SimpleFile extends ProcessRunner { 4382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 4482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll private static final class FileOp { 4582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll public String source; 4682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll public String destination; 4782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll } 4882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 4982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll @Override 5082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll public void process(TemplateCore template, ProcessArgument[] args, String processId, 5182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll IProgressMonitor monitor) 5282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll throws ProcessFailureException { 5382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 5482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll // Fetch the args 5582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll String projectName = null; 5682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll List<FileOp> fileOps = new ArrayList<FileOp>(); 5782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 5882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll for (ProcessArgument arg : args) { 5982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll if (arg.getName().equals("projectName")) //$NON-NLS-1$ 6082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll projectName = arg.getSimpleValue(); 6182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll else if (arg.getName().equals("files")) { //$NON-NLS-1$ 6282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll ProcessArgument[][] files = arg.getComplexArrayValue(); 6382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll for (ProcessArgument[] file : files) { 6482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll FileOp op = new FileOp(); 6582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll for (ProcessArgument fileArg : file) { 6682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll if (fileArg.getName().equals("source")) //$NON-NLS-1$ 6782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll op.source = fileArg.getSimpleValue(); 6882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll else if (fileArg.getName().equals("destination")) //$NON-NLS-1$ 6982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll op.destination = fileArg.getSimpleValue(); 7082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll } 7182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll if (op.source == null || op.destination == null) 7282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll throw new ProcessFailureException(Messages.SimpleFile_Bad_file_operation); 7382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll fileOps.add(op); 7482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll } 7582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll } 7682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll } 7782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 7882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll if (projectName == null) 7982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll throw new ProcessFailureException(Messages.SimpleFile_No_project_name); 8082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); 8182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll if (!project.exists()) 8282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll throw new ProcessFailureException(Messages.SimpleFile_Project_does_not_exist); 8382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 8482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll // Find bundle to find source files 8582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll Bundle bundle = Activator.getBundle(template.getTemplateInfo().getPluginId()); 8682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll if (bundle == null) 8782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll throw new ProcessFailureException(Messages.SimpleFile_Bundle_not_found); 8882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 8982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll try { 9082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll for (FileOp op : fileOps) { 9182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll IFile destFile = project.getFile(new Path(op.destination)); 9282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll if (destFile.exists()) 9382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll // don't overwrite files if they exist already 9482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll continue; 9582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 9682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll // Make sure parent folders are created 9782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll mkDirs(project, destFile.getParent(), monitor); 9882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 9982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll URL sourceURL = FileLocator.find(bundle, new Path(op.source), null); 10082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll if (sourceURL == null) 10182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll throw new ProcessFailureException(Messages.SimpleFile_Could_not_fine_source 10282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll + op.source); 10382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 10482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll TemplatedInputStream in = new TemplatedInputStream(sourceURL.openStream(), 10582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll template.getValueStore()); 10682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll destFile.create(in, true, monitor); 10782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll in.close(); 10882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll } 10982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll } catch (IOException e) { 11082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll throw new ProcessFailureException(e); 11182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll } catch (CoreException e) { 11282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll throw new ProcessFailureException(e); 11382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll } 11482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 11582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll } 11682087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 11782087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll private void mkDirs(IProject project, IContainer container, IProgressMonitor monitor) 11882087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll throws CoreException { 11982087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll if (container.exists()) 12082087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll return; 12182087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll mkDirs(project, container.getParent(), monitor); 12282087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll ((IFolder) container).create(true, true, monitor); 12382087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll } 12482087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll 12582087aa19796b0a0353e5480450d4a9fc5d935b5Raphael Moll} 126