001: /**********************************************************************
002: * Copyright (c) 2004, 2006 Eclipse Foundation and others.
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Gunnar Wagenknecht - Initial API and implementation
010: * IBM Corporation - Initial API and implementation
011: **********************************************************************/package org.eclipse.pde.internal.build.fetch;
012:
013: import java.util.Map;
014: import java.util.Properties;
015: import org.eclipse.core.runtime.*;
016: import org.eclipse.osgi.util.NLS;
017: import org.eclipse.pde.build.IAntScript;
018: import org.eclipse.pde.build.IFetchFactory;
019: import org.eclipse.pde.internal.build.*;
020:
021: /**
022: * An <code>IFetchFactory</code> that fetches features and plugins by
023: * copying from a specific location (id: <code>COPY</code>).
024: * <p>
025: * Map file arguments:
026: * <code><ROOT_LOCATION>[,<ELEMENT_LOCATION>]</code>
027: * <dl>
028: * <dt>ROOT_LOCATION</dt>
029: * <dd>The ROOT_LOCATION (eg. <code>/source/eclipse</code>, or
030: * <code>D:/dev/myproduct</code>) is used as root path to determine the
031: * location to fetch. It can be overwritten via the
032: * <code>fetchTag</code> to fetch from another location (for example, on a different machine).</dd>
033: * </dl>
034: * <dt>ELEMENT_LOCATION</dt>
035: * <dd>A path withing the ROOT_LOCATION (eg.
036: * <code>org.eclipse.sdk-feature/features/org.eclipse.rcp</code>) to retrive
037: * the element from if it is not within the root. If this is not provided the
038: * default path will be used which simply maps to the element name.</dd>
039: * </dl>
040: * </p>
041: */
042: public class COPYFetchTasksFactory implements IFetchFactory,
043: IPDEBuildConstants {
044: public static final String ID = "COPY"; //$NON-NLS-1$
045:
046: private static final String SEPARATOR = ","; //$NON-NLS-1$
047: private static final String OVERRIDE_TAG = ID;
048:
049: //COPY specific keys used in the map being passed around.
050: private static final String KEY_PATH = "path"; //$NON-NLS-1$
051: private static final String KEY_ROOT = "root"; //$NON-NLS-1$
052:
053: public void generateRetrieveElementCall(Map entryInfos,
054: IPath destination, IAntScript script) {
055: String element = (String) entryInfos.get(KEY_ELEMENT_NAME);
056:
057: // we directly copy the disc content into the destination
058: String root = (String) entryInfos.get(KEY_ROOT);
059: String path = (String) entryInfos.get(KEY_PATH);
060: IPath sourcePath = new Path(root);
061: if (path != null) {
062: sourcePath = sourcePath.append(path);
063: } else {
064: sourcePath = sourcePath.append(element);
065: }
066:
067: printCopyTask(null, destination.toString(),
068: new String[] { sourcePath.toString() }, false, true,
069: script);
070: }
071:
072: public void generateRetrieveFilesCall(final Map entryInfos,
073: IPath destination, final String[] files, IAntScript script) {
074: String root = (String) entryInfos.get(KEY_ROOT);
075: String path = (String) entryInfos.get(KEY_PATH);
076:
077: for (int i = 0; i < files.length; i++) {
078: String file = files[i];
079: IPath filePath = new Path(root);
080: if (path != null) {
081: filePath = filePath.append(path).append(file);
082: } else {
083: filePath = filePath.append(
084: (String) entryInfos.get(KEY_ELEMENT_NAME))
085: .append(file);
086: }
087: printCopyTask(filePath.toString(), destination.toString(),
088: null, true, true, script);
089: }
090: }
091:
092: public void addTargets(IAntScript script) {
093: // no additional targets
094: }
095:
096: public void parseMapFileEntry(String repoSpecificentry,
097: Properties overrideTags, Map entryInfos)
098: throws CoreException {
099: String[] arguments = Utils.getArrayFromStringWithBlank(
100: repoSpecificentry, SEPARATOR);
101: if (arguments.length < 1) {
102: String message = NLS.bind(
103: Messages.error_incorrectDirectoryEntry, entryInfos
104: .get(KEY_ELEMENT_NAME));
105: throw new CoreException(
106: new Status(IStatus.ERROR, PI_PDEBUILD,
107: EXCEPTION_ENTRY_MISSING, message, null));
108: }
109:
110: String overrideTag = overrideTags != null ? overrideTags
111: .getProperty(OVERRIDE_TAG) : null;
112: entryInfos.put(KEY_ROOT, (null == overrideTag || overrideTag
113: .trim().length() == 0) ? arguments[0] : overrideTag);
114: entryInfos.put(KEY_PATH, (arguments.length > 1 && arguments[1]
115: .trim().length() > 0) ? arguments[1] : null);
116: }
117:
118: /**
119: * Print a <code>copy</code> task to the script. The source file is specified
120: * by the <code>file</code> OR the <code>dirs</code> parameter.
121: * The destination directory is specified by the <code>todir</code> parameter.
122: * @param file the source file
123: * @param todir the destination directory
124: * @param dirs the directories to copy
125: * @param overwrite indicates if existing files should be overwritten
126: * @param script the script to print to
127: */
128: private void printCopyTask(String file, String todir,
129: String[] dirs, boolean failOnError, boolean overwrite,
130: IAntScript script) {
131: script.printTabs();
132: script.print("<copy"); //$NON-NLS-1$
133: script.printAttribute("file", file, false); //$NON-NLS-1$
134: script.printAttribute("todir", todir, false); //$NON-NLS-1$
135: script.printAttribute(
136: "failonerror", failOnError ? "true" : "false", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
137: script.printAttribute(
138: "overwrite", overwrite ? "true" : "false", true); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
139:
140: if (dirs == null)
141: script.println("/>"); //$NON-NLS-1$
142: else {
143: script.println(">"); //$NON-NLS-1$
144: for (int i = 0; i < dirs.length; i++) {
145: script.printTabs();
146: script.print("\t<fileset"); //$NON-NLS-1$
147: script.printAttribute("dir", dirs[i], true); //$NON-NLS-1$
148: script.println("/>"); //$NON-NLS-1$
149: }
150: script.println("</copy>"); //$NON-NLS-1$
151: }
152: }
153: }
|