diff options
Diffstat (limited to 'eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/templates')
3 files changed, 317 insertions, 0 deletions
diff --git a/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/templates/SetFolders.java b/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/templates/SetFolders.java new file mode 100644 index 000000000..2e8f714bb --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/templates/SetFolders.java @@ -0,0 +1,105 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.eclipse.org/org/documents/epl-v10.php + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.ide.eclipse.ndk.internal.templates; + +import com.android.ide.eclipse.ndk.internal.Messages; + +import org.eclipse.cdt.core.CCorePlugin; +import org.eclipse.cdt.core.model.CoreModel; +import org.eclipse.cdt.core.model.ICProject; +import org.eclipse.cdt.core.model.IPathEntry; +import org.eclipse.cdt.core.templateengine.TemplateCore; +import org.eclipse.cdt.core.templateengine.process.ProcessArgument; +import org.eclipse.cdt.core.templateengine.process.ProcessFailureException; +import org.eclipse.cdt.core.templateengine.process.ProcessRunner; +import org.eclipse.core.resources.IFolder; +import org.eclipse.core.resources.IProject; +import org.eclipse.core.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.Path; + +import java.util.ArrayList; +import java.util.List; + +public class SetFolders extends ProcessRunner { + + @Override + public void process(TemplateCore template, ProcessArgument[] args, String processId, + IProgressMonitor monitor) + throws ProcessFailureException { + String projectName = null; + String[] sourceFolders = null; + String[] outputFolders = null; + + for (ProcessArgument arg : args) { + String argName = arg.getName(); + if (argName.equals("projectName")) { //$NON-NLS-1$ + projectName = arg.getSimpleValue(); + } else if (argName.equals("sourceFolders")) { //$NON-NLS-1$ + sourceFolders = arg.getSimpleArrayValue(); + } else if (argName.equals("outputFolders")) { //$NON-NLS-1$ + outputFolders = arg.getSimpleArrayValue(); + } + } + + // Get the project + if (projectName == null) + throw new ProcessFailureException(Messages.SetFolders_Missing_project_name); + + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); + if (!project.exists()) + throw new ProcessFailureException(Messages.SetFolders_Project_does_not_exist); + + // Create the folders + if (sourceFolders == null && outputFolders == null) + throw new ProcessFailureException(Messages.SetFolders_No_folders); + + try { + // Add them in + ICProject cproject = CCorePlugin.getDefault().getCoreModel().create(project); + IPathEntry[] pathEntries = cproject.getRawPathEntries(); + List<IPathEntry> newEntries = new ArrayList<IPathEntry>(pathEntries.length); + for (IPathEntry pathEntry : pathEntries) { + // remove the old source and output entries + if (pathEntry.getEntryKind() != IPathEntry.CDT_SOURCE + && pathEntry.getEntryKind() != IPathEntry.CDT_OUTPUT) { + newEntries.add(pathEntry); + } + } + if (sourceFolders != null) + for (String sourceFolder : sourceFolders) { + IFolder folder = project.getFolder(new Path(sourceFolder)); + if (!folder.exists()) + folder.create(true, true, monitor); + newEntries.add(CoreModel.newSourceEntry(folder.getFullPath())); + } + if (outputFolders != null) + for (String outputFolder : outputFolders) { + IFolder folder = project.getFolder(new Path(outputFolder)); + if (!folder.exists()) + folder.create(true, true, monitor); + newEntries.add(CoreModel.newOutputEntry(folder.getFullPath())); + } + cproject.setRawPathEntries(newEntries.toArray(new IPathEntry[newEntries.size()]), + monitor); + } catch (CoreException e) { + throw new ProcessFailureException(e); + } + } + +} diff --git a/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/templates/SimpleFile.java b/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/templates/SimpleFile.java new file mode 100644 index 000000000..7f249cacb --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/templates/SimpleFile.java @@ -0,0 +1,125 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.eclipse.org/org/documents/epl-v10.php + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.ide.eclipse.ndk.internal.templates; + +import com.android.ide.eclipse.ndk.internal.Activator; +import com.android.ide.eclipse.ndk.internal.Messages; + +import org.eclipse.cdt.core.templateengine.TemplateCore; +import org.eclipse.cdt.core.templateengine.process.ProcessArgument; +import org.eclipse.cdt.core.templateengine.process.ProcessFailureException; +import org.eclipse.cdt.core.templateengine.process.ProcessRunner; +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.resources.ResourcesPlugin; +import org.eclipse.core.runtime.CoreException; +import org.eclipse.core.runtime.FileLocator; +import org.eclipse.core.runtime.IProgressMonitor; +import org.eclipse.core.runtime.Path; +import org.osgi.framework.Bundle; + +import java.io.IOException; +import java.net.URL; +import java.util.ArrayList; +import java.util.List; + +public class SimpleFile extends ProcessRunner { + + private static final class FileOp { + public String source; + public String destination; + } + + @Override + public void process(TemplateCore template, ProcessArgument[] args, String processId, + IProgressMonitor monitor) + throws ProcessFailureException { + + // Fetch the args + String projectName = null; + List<FileOp> fileOps = new ArrayList<FileOp>(); + + for (ProcessArgument arg : args) { + if (arg.getName().equals("projectName")) //$NON-NLS-1$ + projectName = arg.getSimpleValue(); + else if (arg.getName().equals("files")) { //$NON-NLS-1$ + ProcessArgument[][] files = arg.getComplexArrayValue(); + for (ProcessArgument[] file : files) { + FileOp op = new FileOp(); + for (ProcessArgument fileArg : file) { + if (fileArg.getName().equals("source")) //$NON-NLS-1$ + op.source = fileArg.getSimpleValue(); + else if (fileArg.getName().equals("destination")) //$NON-NLS-1$ + op.destination = fileArg.getSimpleValue(); + } + if (op.source == null || op.destination == null) + throw new ProcessFailureException(Messages.SimpleFile_Bad_file_operation); + fileOps.add(op); + } + } + } + + if (projectName == null) + throw new ProcessFailureException(Messages.SimpleFile_No_project_name); + IProject project = ResourcesPlugin.getWorkspace().getRoot().getProject(projectName); + if (!project.exists()) + throw new ProcessFailureException(Messages.SimpleFile_Project_does_not_exist); + + // Find bundle to find source files + Bundle bundle = Activator.getBundle(template.getTemplateInfo().getPluginId()); + if (bundle == null) + throw new ProcessFailureException(Messages.SimpleFile_Bundle_not_found); + + try { + for (FileOp op : fileOps) { + IFile destFile = project.getFile(new Path(op.destination)); + if (destFile.exists()) + // don't overwrite files if they exist already + continue; + + // Make sure parent folders are created + mkDirs(project, destFile.getParent(), monitor); + + URL sourceURL = FileLocator.find(bundle, new Path(op.source), null); + if (sourceURL == null) + throw new ProcessFailureException(Messages.SimpleFile_Could_not_fine_source + + op.source); + + TemplatedInputStream in = new TemplatedInputStream(sourceURL.openStream(), + template.getValueStore()); + destFile.create(in, true, monitor); + in.close(); + } + } catch (IOException e) { + throw new ProcessFailureException(e); + } catch (CoreException e) { + throw new ProcessFailureException(e); + } + + } + + private void mkDirs(IProject project, IContainer container, IProgressMonitor monitor) + throws CoreException { + if (container.exists()) + return; + mkDirs(project, container.getParent(), monitor); + ((IFolder) container).create(true, true, monitor); + } + +} diff --git a/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/templates/TemplatedInputStream.java b/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/templates/TemplatedInputStream.java new file mode 100644 index 000000000..129caa327 --- /dev/null +++ b/eclipse/plugins/com.android.ide.eclipse.ndk/src/com/android/ide/eclipse/ndk/internal/templates/TemplatedInputStream.java @@ -0,0 +1,87 @@ +/* + * Copyright (C) 2011 The Android Open Source Project + * + * Licensed under the Eclipse Public License, Version 1.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.eclipse.org/org/documents/epl-v10.php + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.ide.eclipse.ndk.internal.templates; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Map; + +/** + * Reads from a template substituting marked values from the supplied Map. + */ +public class TemplatedInputStream extends InputStream { + + private final InputStream mIn; + private final Map<String, String> mMap; + private char[] mSub; + private int mPos; + private int mMark; + + public TemplatedInputStream(InputStream in, Map<String, String> map) { + this.mIn = in; + this.mMap = map; + } + + @Override + public int read() throws IOException { + // if from a mark, return the char + if (mMark != 0) { + int c = mMark; + mMark = 0; + return c; + } + + // return char from sub layer if available + if (mSub != null) { + char c = mSub[mPos++]; + if (mPos >= mSub.length) + mSub = null; + return c; + } + + int c = mIn.read(); + if (c == '%') { + // check if it's a sub + c = mIn.read(); + if (c == '{') { + // it's a sub + StringBuffer buff = new StringBuffer(); + for (c = mIn.read(); c != '}' && c >= 0; c = mIn.read()) + buff.append((char) c); + String str = mMap.get(buff.toString()); + if (str != null) { + mSub = str.toCharArray(); + mPos = 0; + } + return read(); // recurse to get the real char + } else { + // not a sub + mMark = c; + return '%'; + } + } + + return c; + } + + @Override + public void close() throws IOException { + super.close(); + mIn.close(); + } + +} |