001: /*******************************************************************************
002: * Copyright (c) 2007 IBM Corporation and others. All rights reserved. This
003: * program and the accompanying materials are made available under the terms of
004: * the Eclipse Public License v1.0 which accompanies this distribution, and is
005: * available at http://www.eclipse.org/legal/epl-v10.html
006: *
007: * Contributors: IBM Corporation - initial API and implementation
008: ******************************************************************************/package org.eclipse.pde.build.internal.tests;
009:
010: import java.io.File;
011: import java.io.IOException;
012: import java.net.URL;
013: import java.util.Iterator;
014: import java.util.List;
015:
016: import org.apache.tools.ant.*;
017: import org.eclipse.ant.core.AntCorePlugin;
018: import org.eclipse.ant.core.Type;
019: import org.eclipse.core.runtime.FileLocator;
020:
021: public class AntUtils {
022:
023: /* This is somewhat destructive to the target, as resolving UnknownTasks
024: * results in Types being filtered from the getTasks list and no longer
025: * being accessible.
026: */
027: static public Object getFirstChildByName(Target target, String name) {
028: Task[] tasks = target.getTasks();
029: for (int i = 0; i < tasks.length; i++) {
030: if (tasks[i].getTaskName().equals(name)) {
031: if (tasks[i] instanceof UnknownElement) {
032: UnknownElement task = (UnknownElement) tasks[i];
033: task.maybeConfigure();
034: return task.getRealThing();
035: } else {
036: return tasks[i];
037: }
038: }
039: }
040: return null;
041: }
042:
043: static public void setupProject(Project project) {
044: setupClasspath();
045:
046: List tasks = AntCorePlugin.getPlugin().getPreferences()
047: .getTasks();
048:
049: for (Iterator iterator = tasks.iterator(); iterator.hasNext();) {
050: org.eclipse.ant.core.Task coreTask = (org.eclipse.ant.core.Task) iterator
051: .next();
052:
053: AntTypeDefinition def = new AntTypeDefinition();
054: String name = ProjectHelper.genComponentName(coreTask
055: .getURI(), coreTask.getTaskName());
056: def.setName(name);
057: def.setClassName(coreTask.getClassName());
058: def.setClassLoader(project.getClass().getClassLoader());
059: def.setAdaptToClass(Task.class);
060: def.setAdapterClass(TaskAdapter.class);
061: ComponentHelper.getComponentHelper(project)
062: .addDataTypeDefinition(def);
063: }
064:
065: List types = AntCorePlugin.getPlugin().getPreferences()
066: .getTypes();
067: for (Iterator iterator = types.iterator(); iterator.hasNext();) {
068: Type type = (Type) iterator.next();
069: AntTypeDefinition def = new AntTypeDefinition();
070: String name = ProjectHelper.genComponentName(type.getURI(),
071: type.getTypeName());
072: def.setName(name);
073: def.setClassName(type.getClassName());
074: def.setClassLoader(project.getClass().getClassLoader());
075: ComponentHelper.getComponentHelper(project)
076: .addDataTypeDefinition(def);
077: }
078: }
079:
080: static private void setupClasspath() {
081: URL[] antClasspath = AntCorePlugin.getPlugin().getPreferences()
082: .getURLs();
083: StringBuffer buff = new StringBuffer();
084: File file = null;
085: for (int i = 0; i < antClasspath.length; i++) {
086: try {
087: file = new File(FileLocator.toFileURL(antClasspath[i])
088: .getPath());
089: } catch (IOException e) {
090: continue;
091: }
092: buff.append(file.getAbsolutePath());
093: buff.append("; "); //$NON-NLS-1$
094: }
095:
096: org.apache.tools.ant.types.Path systemClasspath = new org.apache.tools.ant.types.Path(
097: null, buff.substring(0, buff.length() - 2));
098: org.apache.tools.ant.types.Path.systemClasspath = systemClasspath;
099: }
100: }
|