diff options
author | Andrew Niefer | 2010-01-13 16:56:09 +0000 |
---|---|---|
committer | Andrew Niefer | 2010-01-13 16:56:09 +0000 |
commit | c363f2984a09b73c422e38f4556fd3b23eafe958 (patch) | |
tree | 93e34c348de79e4d3f53fe06b344cebf7cd5e35a /bundles | |
parent | 39397b81698920b0710f07562127af23b03ff471 (diff) | |
download | rt.equinox.p2-c363f2984a09b73c422e38f4556fd3b23eafe958.tar.gz rt.equinox.p2-c363f2984a09b73c422e38f4556fd3b23eafe958.tar.xz rt.equinox.p2-c363f2984a09b73c422e38f4556fd3b23eafe958.zip |
Merge p2_api branch
Diffstat (limited to 'bundles')
68 files changed, 4049 insertions, 4117 deletions
diff --git a/bundles/org.eclipse.equinox.p2.publisher/.classpath b/bundles/org.eclipse.equinox.p2.publisher/.classpath index ef379494d..834ea833e 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/.classpath +++ b/bundles/org.eclipse.equinox.p2.publisher/.classpath @@ -1,6 +1,6 @@ <?xml version="1.0" encoding="UTF-8"?> <classpath> - <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.4"/> + <classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/J2SE-1.5"/> <classpathentry kind="con" path="org.eclipse.pde.core.requiredPlugins"/> <classpathentry kind="src" path="src"/> <classpathentry kind="src" output="bin_ant" path="src_ant"/> diff --git a/bundles/org.eclipse.equinox.p2.publisher/.settings/org.eclipse.jdt.core.prefs b/bundles/org.eclipse.equinox.p2.publisher/.settings/org.eclipse.jdt.core.prefs index d1b455045..236070f98 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/.settings/org.eclipse.jdt.core.prefs +++ b/bundles/org.eclipse.equinox.p2.publisher/.settings/org.eclipse.jdt.core.prefs @@ -1,4 +1,4 @@ -#Thu Nov 27 14:50:33 EST 2008 +#Tue Dec 22 17:35:56 CET 2009 eclipse.preferences.version=1 org.eclipse.jdt.core.builder.cleanOutputFolder=clean org.eclipse.jdt.core.builder.duplicateResourceTask=warning @@ -7,17 +7,17 @@ org.eclipse.jdt.core.builder.resourceCopyExclusionFilter=*.launch org.eclipse.jdt.core.circularClasspath=error org.eclipse.jdt.core.classpath.exclusionPatterns=enabled org.eclipse.jdt.core.classpath.multipleOutputLocations=enabled -org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=disabled -org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.2 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=jsr14 org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve -org.eclipse.jdt.core.compiler.compliance=1.4 +org.eclipse.jdt.core.compiler.compliance=1.5 org.eclipse.jdt.core.compiler.debug.lineNumber=generate org.eclipse.jdt.core.compiler.debug.localVariable=generate org.eclipse.jdt.core.compiler.debug.sourceFile=generate org.eclipse.jdt.core.compiler.doc.comment.support=enabled org.eclipse.jdt.core.compiler.maxProblemPerUnit=1000 org.eclipse.jdt.core.compiler.problem.annotationSuperInterface=warning -org.eclipse.jdt.core.compiler.problem.assertIdentifier=warning +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error org.eclipse.jdt.core.compiler.problem.autoboxing=ignore org.eclipse.jdt.core.compiler.problem.comparingIdentical=warning org.eclipse.jdt.core.compiler.problem.deprecation=warning @@ -25,7 +25,7 @@ org.eclipse.jdt.core.compiler.problem.deprecationInDeprecatedCode=disabled org.eclipse.jdt.core.compiler.problem.deprecationWhenOverridingDeprecatedMethod=enabled org.eclipse.jdt.core.compiler.problem.discouragedReference=error org.eclipse.jdt.core.compiler.problem.emptyStatement=warning -org.eclipse.jdt.core.compiler.problem.enumIdentifier=warning +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error org.eclipse.jdt.core.compiler.problem.fallthroughCase=ignore org.eclipse.jdt.core.compiler.problem.fatalOptionalError=enabled org.eclipse.jdt.core.compiler.problem.fieldHiding=warning @@ -73,7 +73,6 @@ org.eclipse.jdt.core.compiler.problem.unhandledWarningToken=warning org.eclipse.jdt.core.compiler.problem.unnecessaryElse=warning org.eclipse.jdt.core.compiler.problem.unnecessaryTypeCheck=warning org.eclipse.jdt.core.compiler.problem.unqualifiedFieldAccess=ignore -org.eclipse.jdt.core.compiler.problem.unsafeTypeOperation=warning org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownException=warning org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionExemptExceptionAndThrowable=enabled org.eclipse.jdt.core.compiler.problem.unusedDeclaredThrownExceptionIncludeDocCommentReference=enabled @@ -88,7 +87,7 @@ org.eclipse.jdt.core.compiler.problem.unusedParameterWhenOverridingConcrete=enab org.eclipse.jdt.core.compiler.problem.unusedPrivateMember=error org.eclipse.jdt.core.compiler.problem.unusedWarningToken=warning org.eclipse.jdt.core.compiler.problem.varargsArgumentNeedCast=warning -org.eclipse.jdt.core.compiler.source=1.3 +org.eclipse.jdt.core.compiler.source=1.5 org.eclipse.jdt.core.formatter.align_type_members_on_columns=false org.eclipse.jdt.core.formatter.alignment_for_arguments_in_allocation_expression=16 org.eclipse.jdt.core.formatter.alignment_for_arguments_in_enum_constant=16 @@ -160,7 +159,6 @@ org.eclipse.jdt.core.formatter.indent_statements_compare_to_body=true org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_cases=true org.eclipse.jdt.core.formatter.indent_switchstatements_compare_to_switch=true org.eclipse.jdt.core.formatter.indentation.size=4 -org.eclipse.jdt.core.formatter.insert_new_line_after_annotation=insert org.eclipse.jdt.core.formatter.insert_new_line_after_opening_brace_in_array_initializer=do not insert org.eclipse.jdt.core.formatter.insert_new_line_at_end_of_file_if_missing=do not insert org.eclipse.jdt.core.formatter.insert_new_line_before_catch_in_try_statement=do not insert diff --git a/bundles/org.eclipse.equinox.p2.publisher/META-INF/MANIFEST.MF b/bundles/org.eclipse.equinox.p2.publisher/META-INF/MANIFEST.MF index ab23f44ef..ef79934b2 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/META-INF/MANIFEST.MF +++ b/bundles/org.eclipse.equinox.p2.publisher/META-INF/MANIFEST.MF @@ -4,27 +4,36 @@ Bundle-SymbolicName: org.eclipse.equinox.p2.publisher;singleton:=true Bundle-Version: 1.1.0.qualifier Bundle-Name: %bundleName Bundle-Vendor: %providerName -Bundle-RequiredExecutionEnvironment: J2SE-1.4, +Bundle-RequiredExecutionEnvironment: J2SE-1.5, + J2SE-1.4, CDC-1.1/Foundation-1.1 Import-Package: javax.xml.parsers, org.eclipse.equinox.app;version="1.0.0";resolution:=optional, org.eclipse.equinox.internal.frameworkadmin.equinox, org.eclipse.equinox.internal.frameworkadmin.utils, org.eclipse.equinox.internal.p2.artifact.repository, + org.eclipse.equinox.internal.p2.artifact.repository.simple, org.eclipse.equinox.internal.p2.core, org.eclipse.equinox.internal.p2.core.helpers, org.eclipse.equinox.internal.p2.metadata, + org.eclipse.equinox.internal.p2.metadata.query, org.eclipse.equinox.internal.p2.metadata.repository, org.eclipse.equinox.internal.provisional.frameworkadmin, - org.eclipse.equinox.internal.provisional.p2.artifact.repository, org.eclipse.equinox.internal.provisional.p2.artifact.repository.processing, org.eclipse.equinox.internal.provisional.p2.core, org.eclipse.equinox.internal.provisional.p2.core.eventbus, org.eclipse.equinox.internal.provisional.p2.metadata, - org.eclipse.equinox.internal.provisional.p2.metadata.query, - org.eclipse.equinox.internal.provisional.p2.metadata.repository, - org.eclipse.equinox.internal.provisional.p2.repository, org.eclipse.equinox.internal.provisional.simpleconfigurator.manipulator, + org.eclipse.equinox.p2.core, + org.eclipse.equinox.p2.metadata, + org.eclipse.equinox.p2.metadata.expression, + org.eclipse.equinox.p2.metadata.query, + org.eclipse.equinox.p2.query, + org.eclipse.equinox.p2.repository, + org.eclipse.equinox.p2.repository.artifact, + org.eclipse.equinox.p2.repository.artifact.spi, + org.eclipse.equinox.p2.repository.metadata, + org.eclipse.osgi.service.datalocation;version="1.2.0", org.eclipse.osgi.service.environment;version="1.1.0", org.eclipse.osgi.service.pluginconversion;version="1.0.0", org.eclipse.osgi.service.resolver;version="1.2.0", diff --git a/bundles/org.eclipse.equinox.p2.publisher/build.properties b/bundles/org.eclipse.equinox.p2.publisher/build.properties index 23f024bf9..ad78f9038 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/build.properties +++ b/bundles/org.eclipse.equinox.p2.publisher/build.properties @@ -24,3 +24,5 @@ jars.compile.order = .,\ ant_tasks/publisher-ant.jar extra.ant_tasks/publisher-ant.jar = platform:/plugin/org.apache.ant jars.extra.classpath = platform:/plugin/org.apache.ant/lib/ant.jar +javacTarget=jsr14 +javacSource=1.5 diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/FileSetDescriptor.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/FileSetDescriptor.java index 4fd08471c..aa1c2d235 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/FileSetDescriptor.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/FileSetDescriptor.java @@ -16,8 +16,8 @@ import java.util.*; public class FileSetDescriptor { private final String key; private String configSpec = null; - private HashSet fileset = new HashSet(); - private final ArrayList permissions = new ArrayList(); + private HashSet<File> fileset = new HashSet<File>(); + private final ArrayList<String[]> permissions = new ArrayList<String[]>(); private String links = ""; //$NON-NLS-1$ public FileSetDescriptor(String key, String configSpec) { @@ -51,11 +51,11 @@ public class FileSetDescriptor { } public String[][] getPermissions() { - return (String[][]) permissions.toArray(new String[permissions.size()][]); + return permissions.toArray(new String[permissions.size()][]); } public File[] getFiles() { - return (File[]) fileset.toArray(new File[fileset.size()]); + return fileset.toArray(new File[fileset.size()]); } public int size() { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/Messages.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/Messages.java index 76e321431..91f5221b6 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/Messages.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/Messages.java @@ -18,10 +18,8 @@ public class Messages extends NLS { public static String exception_errorReadingManifest; public static String exception_errorLoadingManifest; public static String exception_noPluginConverter; - public static String exception_errorPublishingArtifacts; public static String exception_noArtifactRepo; public static String exception_noMetadataRepo; - public static String exception_errorLoadingProperties; public static String exception_noBundlesOrLocations; public static String exception_noFeaturesOrLocations; public static String exception_invalidSiteReference; diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/QuotedTokenizer.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/QuotedTokenizer.java index 452532749..d66af45cb 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/QuotedTokenizer.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/QuotedTokenizer.java @@ -17,7 +17,7 @@ import java.util.NoSuchElementException; * Tokenzier which supports quoting using '"' * The resulting tokens will not contain the quote character '"' unless it was escaped '\"' */ -public class QuotedTokenizer implements Enumeration { +public class QuotedTokenizer implements Enumeration<String> { private StreamTokenizer tokenizer = null; /** @@ -132,7 +132,7 @@ public class QuotedTokenizer implements Enumeration { return hasMoreTokens(); } - public Object nextElement() { + public String nextElement() { return nextToken(); } } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/SingleElementCollector.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/SingleElementCollector.java deleted file mode 100644 index 23fb74956..000000000 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/SingleElementCollector.java +++ /dev/null @@ -1,28 +0,0 @@ -/******************************************************************************* - * Copyright (c) 2009 IBM Corporation and others. 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: IBM Corporation - initial API and implementation - ******************************************************************************/ -package org.eclipse.equinox.internal.p2.publisher; - -import org.eclipse.equinox.internal.provisional.p2.metadata.query.Collector; - -/** - * Collect a single element and stop the Query - */ -public class SingleElementCollector extends Collector { - - public boolean accept(Object object) { - super.accept(object); - return false; - } - - public Object getElement() { - if (!isEmpty()) - return iterator().next(); - return null; - } -} diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/DataLoader.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/DataLoader.java index 254d93c12..ccf1ac19d 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/DataLoader.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/DataLoader.java @@ -21,8 +21,7 @@ import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; import org.eclipse.equinox.internal.p2.publisher.Activator; import org.eclipse.equinox.internal.provisional.frameworkadmin.*; import org.eclipse.equinox.internal.provisional.simpleconfigurator.manipulator.SimpleConfiguratorManipulator; -import org.osgi.framework.*; -import org.osgi.service.packageadmin.PackageAdmin; +import org.osgi.framework.Constants; public class DataLoader { @@ -34,8 +33,6 @@ public class DataLoader { private final static String frameworkAdminFillter = "(&" + FILTER_OBJECTCLASS + filterFwName + filterLauncherName + ")"; //$NON-NLS-1$ //$NON-NLS-2$ private static final String ORG_ECLIPSE_EQUINOX_SIMPLECONFIGURATOR_CONFIGURL = "org.eclipse.equinox.simpleconfigurator.configUrl"; //$NON-NLS-1$ - private static final String ORG_ECLIPSE_EQUINOX_SIMPLECONFIGURATOR_MANIPULATOR = "org.eclipse.equinox.simpleconfigurator.manipulator"; //$NON-NLS-1$ - private static final String ORG_ECLIPSE_EQUINOX_FRAMEWORKADMIN_EQUINOX = "org.eclipse.equinox.frameworkadmin.equinox"; //$NON-NLS-1$ private Manipulator manipulator; private File configurationLocation; @@ -124,33 +121,6 @@ public class DataLoader { } private FrameworkAdmin getFrameworkAdmin() { - FrameworkAdmin frameworkAdmin = (FrameworkAdmin) ServiceHelper.getService(Activator.getContext(), FrameworkAdmin.class.getName(), frameworkAdminFillter); - if (frameworkAdmin == null) { - startBundle(ORG_ECLIPSE_EQUINOX_FRAMEWORKADMIN_EQUINOX); - startBundle(ORG_ECLIPSE_EQUINOX_SIMPLECONFIGURATOR_MANIPULATOR); - frameworkAdmin = (FrameworkAdmin) ServiceHelper.getService(Activator.getContext(), FrameworkAdmin.class.getName(), frameworkAdminFillter); - } - return frameworkAdmin; - } - - private boolean startBundle(String bundleId) { - PackageAdmin packageAdmin = (PackageAdmin) ServiceHelper.getService(Activator.getContext(), PackageAdmin.class.getName()); - if (packageAdmin == null) - return false; - - Bundle[] bundles = packageAdmin.getBundles(bundleId, null); - if (bundles != null && bundles.length > 0) { - for (int i = 0; i < bundles.length; i++) { - try { - if ((bundles[0].getState() & Bundle.RESOLVED) > 0) { - bundles[0].start(); - return true; - } - } catch (BundleException e) { - // failed, try next bundle - } - } - } - return false; + return (FrameworkAdmin) ServiceHelper.getService(Activator.getContext(), FrameworkAdmin.class.getName(), frameworkAdminFillter); } } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/ExecutablesDescriptor.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/ExecutablesDescriptor.java index 6b2547f6e..e10b6c502 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/ExecutablesDescriptor.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/ExecutablesDescriptor.java @@ -12,7 +12,8 @@ package org.eclipse.equinox.internal.p2.publisher.eclipse; import java.io.*; -import java.util.*; +import java.util.HashSet; +import java.util.Set; import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.core.helpers.FileUtils; import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; @@ -23,7 +24,7 @@ import org.eclipse.osgi.service.environment.Constants; public class ExecutablesDescriptor { private File location; - private Set files; + private Set<File> files; private String executableName; private boolean temporary = false; private String os; @@ -125,9 +126,9 @@ public class ExecutablesDescriptor { this.executableName = executable; this.location = location; if (files == null) - this.files = new HashSet(11); + this.files = new HashSet<File>(11); else { - this.files = new HashSet(files.length); + this.files = new HashSet<File>(files.length); for (int i = 0; i < files.length; i++) addAllFiles(files[i]); } @@ -138,7 +139,7 @@ public class ExecutablesDescriptor { this.location = descriptor.location; this.executableName = descriptor.executableName; this.temporary = descriptor.temporary; - this.files = new HashSet(descriptor.files); + this.files = new HashSet<File>(descriptor.files); } public void addAllFiles(File file) { @@ -180,7 +181,7 @@ public class ExecutablesDescriptor { } public File[] getFiles() { - File[] result = (File[]) files.toArray(new File[files.size()]); + File[] result = files.toArray(new File[files.size()]); for (int i = 0; i < result.length; i++) result[i] = new File(location, result[i].getPath()); return result; @@ -223,8 +224,8 @@ public class ExecutablesDescriptor { try { tempFile = File.createTempFile("p2.brandingIron", ""); //$NON-NLS-1$ //$NON-NLS-2$ tempFile.delete(); - for (Iterator i = files.iterator(); i.hasNext();) - FileUtils.copy(location, tempFile, (File) i.next(), true); + for (File file : files) + FileUtils.copy(location, tempFile, file, true); } catch (IOException e) { LogHelper.log(new Status(IStatus.ERROR, Activator.ID, "Error publishing artifacts", e)); //$NON-NLS-1$ } @@ -245,9 +246,8 @@ public class ExecutablesDescriptor { String targetExecutable = executableName; String executableExtension = Constants.OS_WIN32.equals(os) ? ".exe" : ""; //$NON-NLS-1$ //$NON-NLS-2$ targetExecutable = executableName + executableExtension; - Set filesCopy = new HashSet(files); - for (Iterator i = filesCopy.iterator(); i.hasNext();) { - File file = (File) i.next(); + Set<File> filesCopy = new HashSet<File>(files); + for (File file : filesCopy) { String base = file.getParent(); // use String concatenation here because new File("", "foo") is absolute on at least windows... diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/FeatureManifestParser.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/FeatureManifestParser.java index ae6239b7f..bae677e86 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/FeatureManifestParser.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/FeatureManifestParser.java @@ -33,7 +33,7 @@ public class FeatureManifestParser extends DefaultHandler { private URL url; private StringBuffer characters = null; - private List messageKeys = new ArrayList(); + private List<String> messageKeys = new ArrayList<String>(); public FeatureManifestParser() { this(true); @@ -104,7 +104,7 @@ public class FeatureManifestParser extends DefaultHandler { return result; } - public List getMessageKeys() { + public List<String> getMessageKeys() { return messageKeys; } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/FeatureParser.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/FeatureParser.java index 47a802066..08fd6a805 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/FeatureParser.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/FeatureParser.java @@ -51,8 +51,8 @@ public class FeatureParser extends DefaultHandler { input = new BufferedInputStream(new FileInputStream(file)); feature = parser.parse(input); if (feature != null) { - List messageKeys = parser.getMessageKeys(); - String[] keyStrings = (String[]) messageKeys.toArray(new String[messageKeys.size()]); + List<String> messageKeys = parser.getMessageKeys(); + String[] keyStrings = messageKeys.toArray(new String[messageKeys.size()]); feature.setLocalizations(LocalizationHelper.getDirPropertyLocalizations(location, "feature", null, keyStrings)); //$NON-NLS-1$ } } catch (FileNotFoundException e) { @@ -76,8 +76,8 @@ public class FeatureParser extends DefaultHandler { InputStream input = new BufferedInputStream(jar.getInputStream(entry)); feature = parser.parse(input); if (feature != null) { - List messageKeys = parser.getMessageKeys(); - String[] keyStrings = (String[]) messageKeys.toArray(new String[messageKeys.size()]); + List<String> messageKeys = parser.getMessageKeys(); + String[] keyStrings = messageKeys.toArray(new String[messageKeys.size()]); feature.setLocalizations(LocalizationHelper.getJarPropertyLocalizations(location, "feature", null, keyStrings)); //$NON-NLS-1$ } } catch (IOException e) { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/GeneratorBundleInfo.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/GeneratorBundleInfo.java index b301e4aba..7442a08e4 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/GeneratorBundleInfo.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/GeneratorBundleInfo.java @@ -11,7 +11,7 @@ package org.eclipse.equinox.internal.p2.publisher.eclipse; import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo; -import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; public class GeneratorBundleInfo extends BundleInfo { private IInstallableUnit iu = null; diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/IProductDescriptor.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/IProductDescriptor.java index cdb7a70d8..b9d072ad3 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/IProductDescriptor.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/IProductDescriptor.java @@ -12,7 +12,9 @@ package org.eclipse.equinox.internal.p2.publisher.eclipse; import java.io.File; import java.util.List; -import java.util.Properties; +import java.util.Map; +import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo; +import org.eclipse.equinox.p2.metadata.IVersionedId; /** * Represents a product file. @@ -33,17 +35,17 @@ public interface IProductDescriptor { * @param includeFragments whether or not to include the fragments in the return value * @return the list of bundles in this product */ - public List getBundles(boolean includeFragments); + public List<IVersionedId> getBundles(boolean includeFragments); /** * Returns a list<VersionedName> of fragments that constitute this product. */ - public List getFragments(); + public List<IVersionedId> getFragments(); /** * Returns a List<VersionedName> of features that constitute this product. */ - public List getFeatures(); + public List<IVersionedId> getFeatures(); /** * Returns the path to the config.ini file as specified in the .product file. @@ -98,7 +100,7 @@ public interface IProductDescriptor { /** * Returns the properties for a product file. */ - public Properties getConfigurationProperties(); + public Map<String, String> getConfigurationProperties(); /** * Returns a list of icons for this product for a given OS. @@ -109,7 +111,7 @@ public interface IProductDescriptor { * Returns a List<BundleInfo> for each bundle that has custom configuration data. * @return A List<BundleInfo> */ - public List getBundleInfos(); + public List<BundleInfo> getBundleInfos(); /** * This is needed for config.ini files and p2 advice diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/ProductFile.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/ProductFile.java index e44e9dca6..0009c63d2 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/ProductFile.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/eclipse/ProductFile.java @@ -13,14 +13,16 @@ package org.eclipse.equinox.internal.p2.publisher.eclipse; -import org.eclipse.equinox.internal.provisional.p2.metadata.IVersionedId; - import java.io.*; import java.util.*; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; +import org.eclipse.equinox.internal.p2.core.helpers.*; +import org.eclipse.equinox.internal.p2.metadata.VersionedId; +import org.eclipse.equinox.internal.p2.publisher.Activator; import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo; -import org.eclipse.equinox.internal.provisional.p2.metadata.VersionedId; +import org.eclipse.equinox.p2.metadata.IVersionedId; +import org.eclipse.osgi.service.datalocation.Location; import org.xml.sax.Attributes; import org.xml.sax.InputSource; import org.xml.sax.helpers.DefaultHandler; @@ -119,25 +121,25 @@ public class ProductFile extends DefaultHandler implements IProductDescriptor { private SAXParser parser; private String launcherName = null; // private boolean useIco = false; - private Map icons = new HashMap(6); + private Map<String, Collection<String>> icons = new HashMap<String, Collection<String>>(6); private String configPath = null; - private final Map platformSpecificConfigPaths = new HashMap(); + private final Map<String, String> platformSpecificConfigPaths = new HashMap<String, String>(); private String configPlatform = null; private String platformConfigPath = null; private String id = null; private String uid = null; private boolean useFeatures = false; - private List plugins = null; - private List fragments = null; - private List features = null; + private List<IVersionedId> plugins = null; + private List<IVersionedId> fragments = null; + private List<IVersionedId> features = null; private String splashLocation = null; private String productName = null; private String application = null; private String version = null; private Properties launcherArgs = new Properties(); private File location; - private List bundleInfos; - private Properties properties; + private List<BundleInfo> bundleInfos; + private Map<String, String> properties; private String licenseURL; private String licenseText = null; @@ -199,11 +201,11 @@ public class ProductFile extends DefaultHandler implements IProductDescriptor { * Returns the properties found in .product file. Properties * are located in the <configurations> block of the file */ - public Properties getConfigurationProperties() { - Properties result = properties != null ? properties : new Properties(); - if (application != null && !result.contains(PROPERTY_ECLIPSE_APPLICATION)) + public Map<String, String> getConfigurationProperties() { + Map<String, String> result = properties != null ? properties : new HashMap<String, String>(); + if (application != null && !result.containsKey(PROPERTY_ECLIPSE_APPLICATION)) result.put(PROPERTY_ECLIPSE_APPLICATION, application); - if (id != null && !result.contains(PROPERTY_ECLIPSE_PRODUCT)) + if (id != null && !result.containsKey(PROPERTY_ECLIPSE_PRODUCT)) result.put(PROPERTY_ECLIPSE_PRODUCT, id); return result; @@ -214,17 +216,17 @@ public class ProductFile extends DefaultHandler implements IProductDescriptor { * @param includeFragments Indicates whether or not fragments should * be included in the list */ - public List getBundles(boolean includeFragments) { - List p = plugins != null ? plugins : Collections.EMPTY_LIST; + public List<IVersionedId> getBundles(boolean includeFragments) { + List<IVersionedId> p = plugins != null ? plugins : CollectionUtils.<IVersionedId> emptyList(); if (!includeFragments) return p; - List f = fragments != null ? fragments : Collections.EMPTY_LIST; + List<IVersionedId> f = fragments != null ? fragments : CollectionUtils.<IVersionedId> emptyList(); int size = p.size() + f.size(); if (size == 0) - return Collections.EMPTY_LIST; + return CollectionUtils.emptyList(); - List both = new ArrayList(size); + List<IVersionedId> both = new ArrayList<IVersionedId>(size); both.addAll(p); both.addAll(f); return both; @@ -235,37 +237,33 @@ public class ProductFile extends DefaultHandler implements IProductDescriptor { * in the product file. * @return A List<BundleInfo> */ - public List getBundleInfos() { - return bundleInfos != null ? bundleInfos : Collections.EMPTY_LIST; + public List<BundleInfo> getBundleInfos() { + return bundleInfos != null ? bundleInfos : CollectionUtils.<BundleInfo> emptyList(); } /** * Returns a list<VersionedName> of fragments that constitute this product. */ - public List getFragments() { - if (fragments == null) - return Collections.EMPTY_LIST; - return fragments; + public List<IVersionedId> getFragments() { + return fragments != null ? fragments : CollectionUtils.<IVersionedId> emptyList(); } /** * Returns a List<VersionedName> of features that constitute this product. */ - public List getFeatures() { - if (features == null) - return Collections.EMPTY_LIST; - return features; + public List<IVersionedId> getFeatures() { + return features != null ? features : CollectionUtils.<IVersionedId> emptyList(); } public String[] getIcons(String os) { - Collection result = (Collection) icons.get(os); + Collection<String> result = icons.get(os); if (result == null) return null; - return (String[]) result.toArray(new String[result.size()]); + return result.toArray(new String[result.size()]); } public String getConfigIniPath(String os) { - String specific = (String) platformSpecificConfigPaths.get(os); + String specific = platformSpecificConfigPaths.get(os); return specific == null ? configPath : specific; } @@ -507,7 +505,7 @@ public class ProductFile extends DefaultHandler implements IProductDescriptor { if (value == null) value = ""; //$NON-NLS-1$ if (properties == null) - properties = new Properties(); + properties = new HashMap<String, String>(); properties.put(name, value); } @@ -525,7 +523,7 @@ public class ProductFile extends DefaultHandler implements IProductDescriptor { if (value != null) info.setMarkedAsStarted(Boolean.valueOf(value).booleanValue()); if (bundleInfos == null) - bundleInfos = new ArrayList(); + bundleInfos = new ArrayList<BundleInfo>(); bundleInfos.add(info); } @@ -645,11 +643,11 @@ public class ProductFile extends DefaultHandler implements IProductDescriptor { IVersionedId name = new VersionedId(attributes.getValue(ATTRIBUTE_ID), attributes.getValue(ATTRIBUTE_VERSION)); if (fragment != null && new Boolean(fragment).booleanValue()) { if (fragments == null) - fragments = new ArrayList(); + fragments = new ArrayList<IVersionedId>(); fragments.add(name); } else { if (plugins == null) - plugins = new ArrayList(); + plugins = new ArrayList<IVersionedId>(); plugins.add(name); } } @@ -657,7 +655,7 @@ public class ProductFile extends DefaultHandler implements IProductDescriptor { private void processFeature(Attributes attributes) { IVersionedId name = new VersionedId(attributes.getValue(ATTRIBUTE_ID), attributes.getValue(ATTRIBUTE_VERSION)); if (features == null) - features = new ArrayList(); + features = new ArrayList<IVersionedId>(); features.add(name); } @@ -704,14 +702,26 @@ public class ProductFile extends DefaultHandler implements IProductDescriptor { private void addIcon(String os, String value) { if (value == null) return; - Collection list = (Collection) icons.get(os); + + File iconFile = new File(value); + if (!iconFile.isFile()) { + //workspace + Location instanceLocation = (Location) ServiceHelper.getService(Activator.getContext(), Location.class.getName(), Location.INSTANCE_FILTER); + if (instanceLocation != null && instanceLocation.getURL() != null) { + File workspace = URLUtil.toFile(instanceLocation.getURL()); + if (workspace != null) + iconFile = new File(workspace, value); + } + } + if (!iconFile.isFile()) + iconFile = new File(location.getParentFile(), value); + + Collection<String> list = icons.get(os); if (list == null) { - list = new ArrayList(6); + list = new ArrayList<String>(6); icons.put(os, list); } - if (!new File(value).isAbsolute()) - value = new File(location.getParentFile(), value).getAbsolutePath(); - list.add(value); + list.add(iconFile.getAbsolutePath()); } private void processSolaris(Attributes attributes) { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/messages.properties b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/messages.properties index 88ab7a43d..d0382034c 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/messages.properties +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/publisher/messages.properties @@ -14,8 +14,6 @@ exception_errorReadingManifest = An error occurred while parsing the bundle mani exception_errorLoadingManifest = An error occurred while loading the manifest {0}. exception_noBundlesOrLocations=No bundles or locations provided. exception_noFeaturesOrLocations=No features or locations provided. -exception_errorPublishingArtifacts = An error occurred while publishing artifacts. -exception_errorLoadingProperties = An error occurred while loading the properties file {0}. exception_noPluginConverter=Unable to acquire PluginConverter service during generation for: {0}. exception_noArtifactRepo=An artifact repository must be specified in order to publish artifacts. exception_noMetadataRepo=A metadata repository must be specified. diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/swt/tools/IconExe.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/swt/tools/IconExe.java index 3efd45525..6367a40b9 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/swt/tools/IconExe.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/internal/p2/swt/tools/IconExe.java @@ -25,33 +25,33 @@ import java.util.*; * Know To Start Programming 64-Bit Windows Systems". */ public class IconExe { - - /** - * Replace the Desktop icons provided in the Windows executable program - * with matching icons provided by the user. - * - * Takes 2 arguments - * argument 0: the Windows executable e.g c:/eclipse/eclipse.exe - * argument 1: The .ico file to write to the given executable e.g. c:/myApp.ico - * - * Note 1. Write access to the executable program is required. As a result, that - * program must not be currently running or edited elsewhere. - * - * Note 2. The Eclipse 3.4 launcher requires an .ico file with the following 7 images (in any order). - * 1. 48x48, 32 bit (RGB / Alpha Channel) - * 2. 32x32, 32 bit (RGB / Alpha Channel) - * 3. 16x16, 32 bit (RGB / Alpha Channel) - * 4. 48x48, 8 bit (256 colors) - * 5. 32x32, 8 bit (256 colors) - * 6. 24x24, 8 bit (256 colors) - * 7. 16x16, 8 bit (256 colors) - * A user icon matching exactly the width/height/depth of an executable icon will be written - * to the executable and will replace that executable icon. If an executable icon - * does not match a user icon, it is silently left as is. - * - * Note 3. This function modifies the content of the executable program and may cause - * its corruption. - */ + + /** + * Replace the Desktop icons provided in the Windows executable program + * with matching icons provided by the user. + * + * Takes 2 arguments + * argument 0: the Windows executable e.g c:/eclipse/eclipse.exe + * argument 1: The .ico file to write to the given executable e.g. c:/myApp.ico + * + * Note 1. Write access to the executable program is required. As a result, that + * program must not be currently running or edited elsewhere. + * + * Note 2. The Eclipse 3.4 launcher requires an .ico file with the following 7 images (in any order). + * 1. 48x48, 32 bit (RGB / Alpha Channel) + * 2. 32x32, 32 bit (RGB / Alpha Channel) + * 3. 16x16, 32 bit (RGB / Alpha Channel) + * 4. 48x48, 8 bit (256 colors) + * 5. 32x32, 8 bit (256 colors) + * 6. 24x24, 8 bit (256 colors) + * 7. 16x16, 8 bit (256 colors) + * A user icon matching exactly the width/height/depth of an executable icon will be written + * to the executable and will replace that executable icon. If an executable icon + * does not match a user icon, it is silently left as is. + * + * Note 3. This function modifies the content of the executable program and may cause + * its corruption. + */ public static void main(String[] args) throws Exception { if (args.length < 2) { System.err.println("Usage: IconExe <windows executable> <ico file>"); //$NON-NLS-1$ @@ -59,7 +59,7 @@ public class IconExe { } ImageLoader loader = new ImageLoader(); - List images = new ArrayList(); + List<ImageData> images = new ArrayList<ImageData>(); for (int i = 1; i < args.length; i++) { try { //An ICO should contain 7 images, a BMP will contain 1 @@ -72,40 +72,41 @@ public class IconExe { } } ImageData[] data = new ImageData[images.size()]; - data = (ImageData[]) images.toArray(data); - + data = images.toArray(data); + int nMissing = unloadIcons(args[0], data); if (nMissing != 0) System.err.println("Error - " + nMissing + " icon(s) not replaced in " + args[0] + " using " + args[1]); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ } - + /* Implementation */ /** * Retrieve the Desktop icons provided in the Windows executable program. * These icons are typically shown in various places of the Windows desktop. * - * Note. The Eclipse 3.4 launcher returns the following 7 images (in any order). - * 1. 48x48, 32 bit (RGB / Alpha Channel) - * 2. 32x32, 32 bit (RGB / Alpha Channel) - * 3. 16x16, 32 bit (RGB / Alpha Channel) - * 4. 48x48, 8 bit (256 colors) - * 5. 32x32, 8 bit (256 colors) - * 6. 24x24, 8 bit (256 colors) - * 7. 16x16, 8 bit (256 colors) + * Note. The Eclipse 3.4 launcher returns the following 7 images (in any order). + * 1. 48x48, 32 bit (RGB / Alpha Channel) + * 2. 32x32, 32 bit (RGB / Alpha Channel) + * 3. 16x16, 32 bit (RGB / Alpha Channel) + * 4. 48x48, 8 bit (256 colors) + * 5. 32x32, 8 bit (256 colors) + * 6. 24x24, 8 bit (256 colors) + * 7. 16x16, 8 bit (256 colors) * * @param program the Windows executable e.g c:/eclipse/eclipse.exe - */ + */ static ImageData[] loadIcons(String program) throws FileNotFoundException, IOException { RandomAccessFile raf = new RandomAccessFile(program, "r"); //$NON-NLS-1$ IconExe iconExe = new IconExe(); IconResInfo[] iconInfo = iconExe.getIcons(raf); ImageData[] data = new ImageData[iconInfo.length]; - for (int i = 0; i < data.length; i++) data[i] = iconInfo[i].data; + for (int i = 0; i < data.length; i++) + data[i] = iconInfo[i].data; raf.close(); return data; } - + /** * Replace the Desktop icons provided in the Windows executable program * with icons provided by the user. @@ -125,10 +126,10 @@ public class IconExe { * 1. 48x48, 32 bit (RGB / Alpha Channel) * 2. 32x32, 32 bit (RGB / Alpha Channel) * 3. 16x16, 32 bit (RGB / Alpha Channel) - * 4. 48x48, 8 bit (256 colors) + * 4. 48x48, 8 bit (256 colors) * 5. 32x32, 8 bit (256 colors) - * 6. 24x24, 8 bit (256 colors) - * 7. 16x16, 8 bit (256 colors) + * 6. 24x24, 8 bit (256 colors) + * 7. 16x16, 8 bit (256 colors) * * Note 4. This function modifies the content of the executable program and may cause * its corruption. @@ -136,16 +137,16 @@ public class IconExe { * @param program the Windows executable e.g c:/eclipse/eclipse.exe * @param icons to write to the given executable * @return the number of icons from the original program that were not successfully replaced (0 if success) - */ + */ static int unloadIcons(String program, ImageData[] icons) throws FileNotFoundException, IOException { RandomAccessFile raf = new RandomAccessFile(program, "rw"); //$NON-NLS-1$ IconExe iconExe = new IconExe(); IconResInfo[] iconInfo = iconExe.getIcons(raf); // Display an error if no icons found in target executable. if (iconInfo.length == 0) { - System.err.println("Warning - no icons detected in \"" + program + "\"."); //$NON-NLS-1$ //$NON-NLS-2$ - raf.close(); - return 0; + System.err.println("Warning - no icons detected in \"" + program + "\"."); //$NON-NLS-1$ //$NON-NLS-2$ + raf.close(); + return 0; } int cnt = 0; for (int i = 0; i < iconInfo.length; i++) { @@ -163,34 +164,39 @@ public class IconExe { raf.close(); return iconInfo.length - cnt; } - + public static final String VERSION = "20050124"; //$NON-NLS-1$ - + static final boolean DEBUG = false; + public static class IconResInfo { ImageData data; int offset; int size; } - + IconResInfo[] iconInfo = null; int iconCnt; - + IconResInfo[] getIcons(RandomAccessFile raf) throws IOException { iconInfo = new IconResInfo[4]; iconCnt = 0; IMAGE_DOS_HEADER imageDosHeader = new IMAGE_DOS_HEADER(); read(raf, imageDosHeader); - if (imageDosHeader.e_magic != IMAGE_DOS_SIGNATURE) return new IconResInfo[0]; + if (imageDosHeader.e_magic != IMAGE_DOS_SIGNATURE) + return new IconResInfo[0]; int imageNtHeadersOffset = imageDosHeader.e_lfanew; raf.seek(imageNtHeadersOffset); IMAGE_NT_HEADERS imageNtHeaders = new IMAGE_NT_HEADERS(); read(raf, imageNtHeaders); - if (imageNtHeaders.Signature != IMAGE_NT_SIGNATURE) return new IconResInfo[0]; + if (imageNtHeaders.Signature != IMAGE_NT_SIGNATURE) + return new IconResInfo[0]; // DumpResources int resourcesRVA = imageNtHeaders.OptionalHeader.DataDirectory[IMAGE_DIRECTORY_ENTRY_RESOURCE].VirtualAddress; - if (resourcesRVA == 0) return new IconResInfo[0]; - if (DEBUG) System.out.println("* Resources (RVA= "+resourcesRVA+")"); //$NON-NLS-1$ //$NON-NLS-2$ + if (resourcesRVA == 0) + return new IconResInfo[0]; + if (DEBUG) + System.out.println("* Resources (RVA= " + resourcesRVA + ")"); //$NON-NLS-1$ //$NON-NLS-2$ IMAGE_SECTION_HEADER imageSectionHeader = new IMAGE_SECTION_HEADER(); int firstSectionOffset = imageNtHeadersOffset + IMAGE_NT_HEADERS.FIELD_OFFSET_OptionalHeader + imageNtHeaders.FileHeader.SizeOfOptionalHeader; raf.seek(firstSectionOffset); @@ -203,7 +209,8 @@ public class IconExe { break; } } - if (!found) return new IconResInfo[0]; + if (!found) + return new IconResInfo[0]; int delta = imageSectionHeader.VirtualAddress - imageSectionHeader.PointerToRawData; int imageResourceDirectoryOffset = resourcesRVA - delta; dumpResourceDirectory(raf, imageResourceDirectoryOffset, imageResourceDirectoryOffset, delta, 0, 0, false); @@ -215,3125 +222,3118 @@ public class IconExe { return iconInfo; } -void dumpResourceDirectory(RandomAccessFile raf, int imageResourceDirectoryOffset, int resourceBase, int delta, int type, int level, boolean rt_icon_root) throws IOException { - if (DEBUG) System.out.println("** LEVEL "+level); //$NON-NLS-1$ + void dumpResourceDirectory(RandomAccessFile raf, int imageResourceDirectoryOffset, int resourceBase, int delta, int type, int level, boolean rt_icon_root) throws IOException { + if (DEBUG) + System.out.println("** LEVEL " + level); //$NON-NLS-1$ - IMAGE_RESOURCE_DIRECTORY imageResourceDirectory = new IMAGE_RESOURCE_DIRECTORY(); - raf.seek(imageResourceDirectoryOffset); - read(raf, imageResourceDirectory); + IMAGE_RESOURCE_DIRECTORY imageResourceDirectory = new IMAGE_RESOURCE_DIRECTORY(); + raf.seek(imageResourceDirectoryOffset); + read(raf, imageResourceDirectory); - if (DEBUG) { - String sType = ""+type; //$NON-NLS-1$ - // level 1 resources are resource types - if (level == 1) { - System.out.println("___________________________"); //$NON-NLS-1$ - if (type == RT_ICON) sType = "RT_ICON"; //$NON-NLS-1$ - if (type == RT_GROUP_ICON) sType = "RT_GROUP_ICON"; //$NON-NLS-1$ + if (DEBUG) { + String sType = "" + type; //$NON-NLS-1$ + // level 1 resources are resource types + if (level == 1) { + System.out.println("___________________________"); //$NON-NLS-1$ + if (type == RT_ICON) + sType = "RT_ICON"; //$NON-NLS-1$ + if (type == RT_GROUP_ICON) + sType = "RT_GROUP_ICON"; //$NON-NLS-1$ + } + System.out.println("Resource Directory [" + sType + "]" + " (Named " + imageResourceDirectory.NumberOfNamedEntries + ", ID " + imageResourceDirectory.NumberOfIdEntries + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ } - System.out.println("Resource Directory ["+sType+"]"+" (Named "+imageResourceDirectory.NumberOfNamedEntries+", ID "+imageResourceDirectory.NumberOfIdEntries+")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ //$NON-NLS-5$ - } - IMAGE_RESOURCE_DIRECTORY_ENTRY[] imageResourceDirectoryEntries = new IMAGE_RESOURCE_DIRECTORY_ENTRY[imageResourceDirectory.NumberOfIdEntries]; - for (int i = 0; i < imageResourceDirectoryEntries.length; i++) { - imageResourceDirectoryEntries[i] = new IMAGE_RESOURCE_DIRECTORY_ENTRY(); - read(raf, imageResourceDirectoryEntries[i]); - } - for (int i = 0; i < imageResourceDirectoryEntries.length; i++) { - if (imageResourceDirectoryEntries[i].DataIsDirectory) { - dumpResourceDirectory(raf, imageResourceDirectoryEntries[i].OffsetToDirectory + resourceBase, resourceBase, delta, imageResourceDirectoryEntries[i].Id, level + 1, rt_icon_root ? true : type == RT_ICON); - } else { - // Resource found - /// pResDirEntry->Name - IMAGE_RESOURCE_DIRECTORY_ENTRY irde = imageResourceDirectoryEntries[i]; - IMAGE_RESOURCE_DATA_ENTRY data = new IMAGE_RESOURCE_DATA_ENTRY(); - raf.seek(imageResourceDirectoryEntries[i].OffsetToData + resourceBase); - read(raf, data); - if (DEBUG) System.out.println("Resource Id "+irde.Id+" Data Offset RVA "+data.OffsetToData+", Size "+data.Size); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ - if (rt_icon_root) { - if (DEBUG) System.out.println("iconcnt "+iconCnt+" |"+iconInfo.length); //$NON-NLS-1$ //$NON-NLS-2$ - iconInfo[iconCnt] = new IconResInfo(); - iconInfo[iconCnt].data = parseIcon(raf, data.OffsetToData - delta, data.Size); - iconInfo[iconCnt].offset = data.OffsetToData - delta; - iconInfo[iconCnt].size = data.Size; - iconCnt++; - if (iconCnt == iconInfo.length) { - IconResInfo[] newArray = new IconResInfo[iconInfo.length + 4]; - System.arraycopy(iconInfo, 0, newArray, 0, iconInfo.length); - iconInfo = newArray; + IMAGE_RESOURCE_DIRECTORY_ENTRY[] imageResourceDirectoryEntries = new IMAGE_RESOURCE_DIRECTORY_ENTRY[imageResourceDirectory.NumberOfIdEntries]; + for (int i = 0; i < imageResourceDirectoryEntries.length; i++) { + imageResourceDirectoryEntries[i] = new IMAGE_RESOURCE_DIRECTORY_ENTRY(); + read(raf, imageResourceDirectoryEntries[i]); + } + for (int i = 0; i < imageResourceDirectoryEntries.length; i++) { + if (imageResourceDirectoryEntries[i].DataIsDirectory) { + dumpResourceDirectory(raf, imageResourceDirectoryEntries[i].OffsetToDirectory + resourceBase, resourceBase, delta, imageResourceDirectoryEntries[i].Id, level + 1, rt_icon_root ? true : type == RT_ICON); + } else { + // Resource found + /// pResDirEntry->Name + IMAGE_RESOURCE_DIRECTORY_ENTRY irde = imageResourceDirectoryEntries[i]; + IMAGE_RESOURCE_DATA_ENTRY data = new IMAGE_RESOURCE_DATA_ENTRY(); + raf.seek(imageResourceDirectoryEntries[i].OffsetToData + resourceBase); + read(raf, data); + if (DEBUG) + System.out.println("Resource Id " + irde.Id + " Data Offset RVA " + data.OffsetToData + ", Size " + data.Size); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + if (rt_icon_root) { + if (DEBUG) + System.out.println("iconcnt " + iconCnt + " |" + iconInfo.length); //$NON-NLS-1$ //$NON-NLS-2$ + iconInfo[iconCnt] = new IconResInfo(); + iconInfo[iconCnt].data = parseIcon(raf, data.OffsetToData - delta, data.Size); + iconInfo[iconCnt].offset = data.OffsetToData - delta; + iconInfo[iconCnt].size = data.Size; + iconCnt++; + if (iconCnt == iconInfo.length) { + IconResInfo[] newArray = new IconResInfo[iconInfo.length + 4]; + System.arraycopy(iconInfo, 0, newArray, 0, iconInfo.length); + iconInfo = newArray; + } } } } - } -} - -static ImageData parseIcon(RandomAccessFile raf, int offset, int size) throws IOException { - raf.seek(offset); - BITMAPINFO bitmapInfo = new BITMAPINFO(); - read(raf, bitmapInfo); - bitmapInfo.bmiHeader.biHeight /= 2; - int width = bitmapInfo.bmiHeader.biWidth; - int height = bitmapInfo.bmiHeader.biHeight; - int depth = bitmapInfo.bmiHeader.biBitCount; - - PaletteData palette = loadPalette(bitmapInfo.bmiHeader, raf); - byte[] shapeData = loadData(bitmapInfo.bmiHeader, raf); - bitmapInfo.bmiHeader.biBitCount = 1; - byte[] maskData = loadData(bitmapInfo.bmiHeader, raf); - maskData = convertPad(maskData, width, height, 1, 4, 2); - bitInvertData(maskData, 0, maskData.length); - return ImageData.internal_new( - width, - height, - depth, - palette, - 4, - shapeData, - 2, - maskData, - null, - -1, - -1, - SWT.IMAGE_ICO, - 0, - 0, - 0, - 0); -} - -static byte[] bitInvertData(byte[] data, int startIndex, int endIndex) { - // Destructively bit invert data in the given byte array. - for (int i = startIndex; i < endIndex; i++) { - data[i] = (byte)(255 - data[i - startIndex]); } - return data; -} -static final byte[] convertPad(byte[] data, int width, int height, int depth, int pad, int newPad) { - if (pad == newPad) return data; - int stride = (width * depth + 7) / 8; - int bpl = (stride + (pad - 1)) / pad * pad; - int newBpl = (stride + (newPad - 1)) / newPad * newPad; - byte[] newData = new byte[height * newBpl]; - int srcIndex = 0, destIndex = 0; - for (int y = 0; y < height; y++) { - System.arraycopy(data, srcIndex, newData, destIndex, newBpl); - srcIndex += bpl; - destIndex += newBpl; - } - return newData; -} -static PaletteData loadPalette(BITMAPINFOHEADER bih, RandomAccessFile raf) throws IOException { - int depth = bih.biBitCount; - if (depth <= 8) { - int numColors = bih.biClrUsed; - if (numColors == 0) { - numColors = 1 << depth; - } else { - if (numColors > 256) - numColors = 256; + static ImageData parseIcon(RandomAccessFile raf, int offset, int size) throws IOException { + raf.seek(offset); + BITMAPINFO bitmapInfo = new BITMAPINFO(); + read(raf, bitmapInfo); + bitmapInfo.bmiHeader.biHeight /= 2; + int width = bitmapInfo.bmiHeader.biWidth; + int height = bitmapInfo.bmiHeader.biHeight; + int depth = bitmapInfo.bmiHeader.biBitCount; + + PaletteData palette = loadPalette(bitmapInfo.bmiHeader, raf); + byte[] shapeData = loadData(bitmapInfo.bmiHeader, raf); + bitmapInfo.bmiHeader.biBitCount = 1; + byte[] maskData = loadData(bitmapInfo.bmiHeader, raf); + maskData = convertPad(maskData, width, height, 1, 4, 2); + bitInvertData(maskData, 0, maskData.length); + return ImageData.internal_new(width, height, depth, palette, 4, shapeData, 2, maskData, null, -1, -1, SWT.IMAGE_ICO, 0, 0, 0, 0); + } + + static byte[] bitInvertData(byte[] data, int startIndex, int endIndex) { + // Destructively bit invert data in the given byte array. + for (int i = startIndex; i < endIndex; i++) { + data[i] = (byte) (255 - data[i - startIndex]); } - byte[] buf = new byte[numColors * 4]; - raf.read(buf); - return paletteFromBytes(buf, numColors); - } - if (depth == 16) return new PaletteData(0x7C00, 0x3E0, 0x1F); - if (depth == 24) return new PaletteData(0xFF, 0xFF00, 0xFF0000); - return new PaletteData(0xFF00, 0xFF0000, 0xFF000000); -} -static PaletteData paletteFromBytes(byte[] bytes, int numColors) { - int bytesOffset = 0; - RGB[] colors = new RGB[numColors]; - for (int i = 0; i < numColors; i++) { - colors[i] = new RGB(bytes[bytesOffset + 2] & 0xFF, - bytes[bytesOffset + 1] & 0xFF, - bytes[bytesOffset] & 0xFF); - bytesOffset += 4; - } - return new PaletteData(colors); -} -static byte[] loadData(BITMAPINFOHEADER bih, RandomAccessFile raf) throws IOException { - int stride = (bih.biWidth * bih.biBitCount + 7) / 8; - stride = (stride + 3) / 4 * 4; // Round up to 4 byte multiple - byte[] data = loadData(bih, raf, stride); - flipScanLines(data, stride, bih.biHeight); - return data; -} -static void flipScanLines(byte[] data, int stride, int height) { - int i1 = 0; - int i2 = (height - 1) * stride; - for (int i = 0; i < height / 2; i++) { - for (int index = 0; index < stride; index++) { - byte b = data[index + i1]; - data[index + i1] = data[index + i2]; - data[index + i2] = b; - } - i1 += stride; - i2 -= stride; + return data; } -} -static byte[] loadData(BITMAPINFOHEADER bih, RandomAccessFile raf, int stride) throws IOException { - int dataSize = bih.biHeight * stride; - byte[] data = new byte[dataSize]; - int cmp = bih.biCompression; - if (cmp == 0) { // BMP_NO_COMPRESSION - raf.read(data); - } else { - if (DEBUG) System.out.println("ICO cannot be compressed?"); //$NON-NLS-1$ - } - return data; -} -static void unloadIcon(RandomAccessFile raf, ImageData icon) throws IOException { - int sizeImage = (((icon.width * icon.depth + 31) / 32 * 4) + - ((icon.width + 31) / 32 * 4)) * icon.height; - write4(raf, BMPHeaderFixedSize); - write4(raf, icon.width); - write4(raf, icon.height * 2); - writeU2(raf, 1); - writeU2(raf, icon.depth); - write4(raf, 0); - write4(raf, sizeImage); - write4(raf, 0); - write4(raf, 0); - write4(raf, icon.palette.colors != null ? icon.palette.colors.length : 0); - write4(raf, 0); - - byte[] rgbs = paletteToBytes(icon.palette); - raf.write(rgbs); - unloadShapeData(raf, icon); - unloadMaskData(raf, icon); -} -static byte[] paletteToBytes(PaletteData pal) { - int n = pal.colors == null ? 0 : (pal.colors.length < 256 ? pal.colors.length : 256); - byte[] bytes = new byte[n * 4]; - int offset = 0; - for (int i = 0; i < n; i++) { - RGB col = pal.colors[i]; - bytes[offset] = (byte)col.blue; - bytes[offset + 1] = (byte)col.green; - bytes[offset + 2] = (byte)col.red; - offset += 4; - } - return bytes; -} -static void unloadMaskData(RandomAccessFile raf, ImageData icon) { - ImageData mask = icon.getTransparencyMask(); - int bpl = (icon.width + 7) / 8; - int pad = mask.scanlinePad; - int srcBpl = (bpl + pad - 1) / pad * pad; - int destBpl = (bpl + 3) / 4 * 4; - byte[] buf = new byte[destBpl]; - int offset = (icon.height - 1) * srcBpl; - byte[] data = mask.data; - try { - for (int i = 0; i < icon.height; i++) { - System.arraycopy(data, offset, buf, 0, bpl); - bitInvertData(buf, 0, bpl); - raf.write(buf, 0, destBpl); - offset -= srcBpl; - } - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); - } -} -static void unloadShapeData(RandomAccessFile raf, ImageData icon) { - int bpl = (icon.width * icon.depth + 7) / 8; - int pad = icon.scanlinePad; - int srcBpl = (bpl + pad - 1) / pad * pad; - int destBpl = (bpl + 3) / 4 * 4; - byte[] buf = new byte[destBpl]; - int offset = (icon.height - 1) * srcBpl; - byte[] data = icon.data; - try { - for (int i = 0; i < icon.height; i++) { - System.arraycopy(data, offset, buf, 0, bpl); - raf.write(buf, 0, destBpl); - offset -= srcBpl; - } - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); + static final byte[] convertPad(byte[] data, int width, int height, int depth, int pad, int newPad) { + if (pad == newPad) + return data; + int stride = (width * depth + 7) / 8; + int bpl = (stride + (pad - 1)) / pad * pad; + int newBpl = (stride + (newPad - 1)) / newPad * newPad; + byte[] newData = new byte[height * newBpl]; + int srcIndex = 0, destIndex = 0; + for (int y = 0; y < height; y++) { + System.arraycopy(data, srcIndex, newData, destIndex, newBpl); + srcIndex += bpl; + destIndex += newBpl; + } + return newData; } -} -static boolean readIconGroup(RandomAccessFile raf, int offset, int size) throws IOException { - raf.seek(offset); - NEWHEADER newHeader = new NEWHEADER(); - read(raf, newHeader); - if (newHeader.ResType != RES_ICON) return false; - RESDIR[] resDir = new RESDIR[newHeader.ResCount]; - for (int i = 0; i < newHeader.ResCount; i++) { - resDir[i] = new RESDIR(); - read(raf, resDir[i]); - } - return true; -} - -static void copyFile(String src, String dst) throws FileNotFoundException, IOException { - File srcFile = new File(src); - File dstFile = new File(dst); - InputStream in = new BufferedInputStream(new FileInputStream(srcFile)); - OutputStream out = new BufferedOutputStream(new FileOutputStream(dstFile)); - int c; - while ((c = in.read()) != -1) out.write(c); - in.close(); - out.close(); -} - -/* IO utilities to parse Windows executable */ -static final int IMAGE_DOS_SIGNATURE = 0x5a4d; -static final int IMAGE_NT_SIGNATURE = 0x00004550; -static final int IMAGE_DIRECTORY_ENTRY_RESOURCE = 2; -static final int RES_ICON = 1; -static final int RT_ICON = 3; -static final int RT_GROUP_ICON = 14; -static final int BMPHeaderFixedSize = 40; -static final int IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b; -static final int IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b; - -public static class IMAGE_DOS_HEADER { - int e_magic; // WORD - int e_cblp; // WORD - int e_cp; // WORD - int e_crlc; // WORD - int e_cparhdr; // WORD - int e_minalloc; // WORD - int e_maxalloc; // WORD - int e_ss; // WORD - int e_sp; // WORD - int e_csum; // WORD - int e_ip; // WORD - int e_cs; // WORD - int e_lfarlc; // WORD - int e_ovno; // WORD - int[] e_res = new int[4]; // WORD[4] - int e_oemid; // WORD - int e_oeminfo; // WORD - int[] e_res2 = new int[10]; // WORD[10] - int e_lfanew; // LONG -} - -public static class IMAGE_FILE_HEADER { - int Machine; // WORD - int NumberOfSections; // WORD - int TimeDateStamp; // DWORD - int PointerToSymbolTable; // DWORD - int NumberOfSymbols; // DWORD - int SizeOfOptionalHeader; // WORD - int Characteristics; // WORD -} - -public static class IMAGE_DATA_DIRECTORY { - int VirtualAddress; // DWORD - int Size; // DWORD -} - -public static class IMAGE_OPTIONAL_HEADER { - // Allocate enough storage for the 64 bit version of the header. - int Magic; // WORD - int MajorLinkerVersion; // BYTE - int MinorLinkerVersion; // BYTE - int SizeOfCode; // DWORD - int SizeOfInitializedData; // DWORD - int SizeOfUninitializedData; // DWORD - int AddressOfEntryPoint; // DWORD - int BaseOfCode; // DWORD - int BaseOfData; // DWORD - long ImageBase; // ULONGLONG - int SectionAlignment; // DWORD - int FileAlignment; // DWORD - int MajorOperatingSystemVersion; // WORD - int MinorOperatingSystemVersion; // WORD - int MajorImageVersion; // WORD - int MinorImageVersion; // WORD - int MajorSubsystemVersion; // WORD - int MinorSubsystemVersion; // WORD - int Win32VersionValue; // DWORD - int SizeOfImage; // DWORD - int SizeOfHeaders; // DWORD - int CheckSum; // DWORD - int Subsystem; // WORD - int DllCharacteristics; // WORD - long SizeOfStackReserve; // ULONGLONG - long SizeOfStackCommit; // ULONGLONG - long SizeOfHeapReserve; // ULONGLONG - long SizeOfHeapCommit; // ULONGLONG - int LoaderFlags; // DWORD - int NumberOfRvaAndSizes; // DWORD - IMAGE_DATA_DIRECTORY[] DataDirectory = new IMAGE_DATA_DIRECTORY[16]; -} - -public static class IMAGE_NT_HEADERS { - int Signature; // DWORD - IMAGE_FILE_HEADER FileHeader = new IMAGE_FILE_HEADER(); - IMAGE_OPTIONAL_HEADER OptionalHeader = new IMAGE_OPTIONAL_HEADER(); - final static int FIELD_OFFSET_OptionalHeader = 24; -} - -public static class IMAGE_SECTION_HEADER { - int[] Name = new int[8]; // BYTE[8] - int Misc_VirtualSize; // DWORD (union Misc { DWORD PhysicalAddress; DWORD VirtualSize } - int VirtualAddress; // DWORD - int SizeOfRawData; // DWORD - int PointerToRawData; // DWORD - int PointerToRelocations; // DWORD - int PointerToLinenumbers; // DWORD - int NumberOfRelocations; // WORD - int NumberOfLinenumbers; // WORD - int Characteristics; // DWORD -} - -public static class IMAGE_RESOURCE_DIRECTORY { - int Characteristics; // DWORD - int TimeDateStamp; // DWORD - int MajorVersion; // WORD - int MinorVersion; // WORD - int NumberOfNamedEntries; // WORD - used - int NumberOfIdEntries; // WORD - used - final static int SIZEOF = 16; -} - -public static class IMAGE_RESOURCE_DIRECTORY_ENTRY { - // union - int NameOffset; // DWORD 31 bits - boolean NameIsString; // DWORD 1 bit - int Name; // DWORD - int Id; // WORD - // union - int OffsetToData; // DWORD - int OffsetToDirectory; // DWORD 31 bits - boolean DataIsDirectory; // DWORD 1 bit -} - -public static class IMAGE_RESOURCE_DATA_ENTRY { - int OffsetToData; // DWORD - int Size; // DWORD - int CodePage; // DWORD - int Reserved; // DWORD -} - -public static class NEWHEADER { - int Reserved; // WORD - int ResType; // WORD - int ResCount; // WORD -} -public static class ICONRESDIR { - int Width; // BYTE - int Height; // BYTE - int ColorCount; // BYTE - int reserved; // BYTE -} - -public static class CURSORDIR { - int Width; // WORD - int Height; // WORD -} - -public static class RESDIR { - // union - ICONRESDIR Icon = new ICONRESDIR(); - CURSORDIR Cursor = new CURSORDIR(); - int Planes; // WORD - int BitCount; // WORD - int BytesInRes; // DWORD - int IconCursorId; // WORD -} - -public static class BITMAPINFOHEADER { - int biSize; // DWORD - int biWidth; // LONG - int biHeight; // LONG - int biPlanes; // WORD - int biBitCount; // WORD - int biCompression; // DWORD - int biSizeImage; // DWORD - int biXPelsPerMeter; // LONG - int biYPelsPerMeter; // LONG - int biClrUsed; // DWORD - int biClrImportant; // DWORD -} + static PaletteData loadPalette(BITMAPINFOHEADER bih, RandomAccessFile raf) throws IOException { + int depth = bih.biBitCount; + if (depth <= 8) { + int numColors = bih.biClrUsed; + if (numColors == 0) { + numColors = 1 << depth; + } else { + if (numColors > 256) + numColors = 256; + } + byte[] buf = new byte[numColors * 4]; + raf.read(buf); + return paletteFromBytes(buf, numColors); + } + if (depth == 16) + return new PaletteData(0x7C00, 0x3E0, 0x1F); + if (depth == 24) + return new PaletteData(0xFF, 0xFF00, 0xFF0000); + return new PaletteData(0xFF00, 0xFF0000, 0xFF000000); + } + + static PaletteData paletteFromBytes(byte[] bytes, int numColors) { + int bytesOffset = 0; + RGB[] colors = new RGB[numColors]; + for (int i = 0; i < numColors; i++) { + colors[i] = new RGB(bytes[bytesOffset + 2] & 0xFF, bytes[bytesOffset + 1] & 0xFF, bytes[bytesOffset] & 0xFF); + bytesOffset += 4; + } + return new PaletteData(colors); + } -static class RGBQUAD { - int rgBlue; // BYTE - int rgbGreen; // BYTE - int rgbRed; // BYTE - int rgbReserved; // BYTE -} -static class BITMAPINFO { - BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER(); - RGBQUAD[] bmiColors = null; -} -static void read(RandomAccessFile raf, BITMAPINFOHEADER bih) throws IOException { - bih.biSize = read4(raf); - bih.biWidth = read4(raf); - bih.biHeight = read4(raf); - bih.biPlanes = readU2(raf); - bih.biBitCount = readU2(raf); - bih.biCompression = read4(raf); - bih.biSizeImage = read4(raf); - bih.biXPelsPerMeter = read4(raf); - bih.biYPelsPerMeter = read4(raf); - bih.biClrUsed = read4(raf); - bih.biClrImportant = read4(raf); -} -static void read(RandomAccessFile raf, BITMAPINFO bi) throws IOException { - read(raf, bi.bmiHeader); -} -/* Little Endian helpers */ -static int readU2(RandomAccessFile raf) throws IOException { - int b0 = raf.readByte() & 0xFF; - int b1 = raf.readByte() & 0xFF; - return (b1 << 8 | b0); -} -static int read4(RandomAccessFile raf) throws IOException { - int b0 = raf.readByte() & 0xFF; - int b1 = raf.readByte() & 0xFF; - int b2 = raf.readByte() & 0xFF; - int b3 = raf.readByte() & 0xFF; - return b3 << 24 | b2 << 16 | b1 << 8 | b0; -} -static long read8(RandomAccessFile raf) throws IOException { - int b0 = raf.readByte() & 0xFF; - int b1 = raf.readByte() & 0xFF; - int b2 = raf.readByte() & 0xFF; - int b3 = raf.readByte() & 0xFF; - int b4 = raf.readByte() & 0xFF; - int b5 = raf.readByte() & 0xFF; - int b6 = raf.readByte() & 0xFF; - int b7 = raf.readByte() & 0xFF; - return b7 << 56 | b6 << 48 | b5 << 40 | b4 << 32 | b3 << 24 | b2 << 16 | b1 << 8 | b0; -} -static void write4(RandomAccessFile raf, int value) throws IOException { - raf.write(value & 0xFF); - raf.write((value >> 8) & 0xFF); - raf.write((value >> 16) & 0xFF); - raf.write((value >> 24) & 0xFF); -} -static void writeU2(RandomAccessFile raf, int value) throws IOException { - raf.write(value & 0xFF); - raf.write((value >> 8) & 0xFF); -} -static void read(RandomAccessFile raf, IMAGE_DOS_HEADER idh) throws IOException { - idh.e_magic = readU2(raf); - idh.e_cblp = readU2(raf); - idh.e_cp = readU2(raf); - idh.e_crlc = readU2(raf); - idh.e_cparhdr = readU2(raf); - idh.e_minalloc = readU2(raf); - idh.e_maxalloc = readU2(raf); - idh.e_ss = readU2(raf); - idh.e_sp = readU2(raf); - idh.e_csum = readU2(raf); - idh.e_ip = readU2(raf); - idh.e_cs = readU2(raf); - idh.e_lfarlc = readU2(raf); - idh.e_ovno = readU2(raf); - for (int i = 0; i < idh.e_res.length; i++) idh.e_res[i] = readU2(raf); - idh.e_oemid = readU2(raf); - idh.e_oeminfo = readU2(raf); - for (int i = 0; i < idh.e_res2.length; i++) idh.e_res2[i] = readU2(raf); - idh.e_lfanew = read4(raf); -} -static void read(RandomAccessFile raf, IMAGE_FILE_HEADER ifh) throws IOException { - ifh.Machine = readU2(raf); - ifh.NumberOfSections = readU2(raf); - ifh.TimeDateStamp = read4(raf); - ifh.PointerToSymbolTable = read4(raf); - ifh.NumberOfSymbols = read4(raf); - ifh.SizeOfOptionalHeader = readU2(raf); - ifh.Characteristics = readU2(raf); -} -static void read(RandomAccessFile raf, IMAGE_DATA_DIRECTORY idd) throws IOException { - idd.VirtualAddress = read4(raf); - idd.Size = read4(raf); -} -static void read(RandomAccessFile raf, IMAGE_OPTIONAL_HEADER ioh) throws IOException { - ioh.Magic = readU2(raf); - - // Assume file is 32bit executable unless x64 magic is present - boolean is32 = !(ioh.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC); - - ioh.MajorLinkerVersion = raf.read(); - ioh.MinorLinkerVersion = raf.read(); - ioh.SizeOfCode = read4(raf); - ioh.SizeOfInitializedData = read4(raf); - ioh.SizeOfUninitializedData = read4(raf); - ioh.AddressOfEntryPoint = read4(raf); - ioh.BaseOfCode = read4(raf); - - if (is32) - { - ioh.BaseOfData = read4(raf); - ioh.ImageBase = read4(raf); - } - else - { - // BaseOfData deleted in the 64 bit version PE32+ - ioh.ImageBase = read8(raf); - } - - ioh.SectionAlignment = read4(raf); - ioh.FileAlignment = read4(raf); - ioh.MajorOperatingSystemVersion = readU2(raf); - ioh.MinorOperatingSystemVersion = readU2(raf); - ioh.MajorImageVersion = readU2(raf); - ioh.MinorImageVersion = readU2(raf); - ioh.MajorSubsystemVersion = readU2(raf); - ioh.MinorSubsystemVersion = readU2(raf); - ioh.Win32VersionValue = read4(raf); - ioh.SizeOfImage = read4(raf); - ioh.SizeOfHeaders = read4(raf); - ioh.CheckSum = read4(raf); - ioh.Subsystem = readU2(raf); - ioh.DllCharacteristics = readU2(raf); - - if (is32) - { - ioh.SizeOfStackReserve = read4(raf); - ioh.SizeOfStackCommit = read4(raf); - ioh.SizeOfHeapReserve = read4(raf); - ioh.SizeOfHeapCommit = read4(raf); - } - else - { - ioh.SizeOfStackReserve = read8(raf); - ioh.SizeOfStackCommit = read8(raf); - ioh.SizeOfHeapReserve = read8(raf); - ioh.SizeOfHeapCommit = read8(raf); - } - - ioh.LoaderFlags = read4(raf); - ioh.NumberOfRvaAndSizes = read4(raf); - for (int i = 0 ; i < ioh.DataDirectory.length; i++) { - ioh.DataDirectory[i] = new IMAGE_DATA_DIRECTORY(); - read(raf, ioh.DataDirectory[i]); + static byte[] loadData(BITMAPINFOHEADER bih, RandomAccessFile raf) throws IOException { + int stride = (bih.biWidth * bih.biBitCount + 7) / 8; + stride = (stride + 3) / 4 * 4; // Round up to 4 byte multiple + byte[] data = loadData(bih, raf, stride); + flipScanLines(data, stride, bih.biHeight); + return data; } -} -static void read(RandomAccessFile raf, IMAGE_NT_HEADERS inh) throws IOException { - inh.Signature = read4(raf); - read(raf, inh.FileHeader); - read(raf, inh.OptionalHeader); -} -static void read(RandomAccessFile raf, IMAGE_SECTION_HEADER ish) throws IOException { - for (int i = 0 ; i < ish.Name.length; i++) ish.Name[i] = raf.read(); - ish.Misc_VirtualSize = read4(raf); - ish.VirtualAddress = read4(raf); - ish.SizeOfRawData = read4(raf); - ish.PointerToRawData = read4(raf); - ish.PointerToRelocations = read4(raf); - ish.PointerToLinenumbers = read4(raf); - ish.NumberOfRelocations = readU2(raf); - ish.NumberOfLinenumbers = readU2(raf); - ish.Characteristics = read4(raf); -} -static void read(RandomAccessFile raf, IMAGE_RESOURCE_DIRECTORY ird) throws IOException { - ird.Characteristics = read4(raf); - ird.TimeDateStamp = read4(raf); - ird.MajorVersion = readU2(raf); - ird.MinorVersion = readU2(raf); - ird.NumberOfNamedEntries = readU2(raf); - ird.NumberOfIdEntries = readU2(raf); -} -static void read(RandomAccessFile raf, IMAGE_RESOURCE_DIRECTORY_ENTRY irde) throws IOException { - irde.Name = read4(raf); - irde.OffsetToData = read4(raf); - // construct other union members - irde.NameOffset = irde.Name & ~ (1 << 31); - irde.NameIsString = (irde.Name & (1 << 31)) != 0; - irde.Id = irde.Name & 0xFFFF; - irde.OffsetToDirectory = irde.OffsetToData & ~ (1 << 31); - irde.DataIsDirectory = (irde.OffsetToData & (1 << 31)) != 0; -} -static void read(RandomAccessFile raf, IMAGE_RESOURCE_DATA_ENTRY irde) throws IOException { - irde.OffsetToData = read4(raf); - irde.Size = read4(raf); - irde.CodePage = read4(raf); - irde.Reserved = read4(raf); -} -static void read(RandomAccessFile raf, NEWHEADER nh) throws IOException { - nh.Reserved = readU2(raf); - nh.ResType = readU2(raf); - nh.ResCount = readU2(raf); -} -static void read(RandomAccessFile raf, ICONRESDIR i) throws IOException { - i.Width = raf.read(); - i.Height = raf.read(); - i.ColorCount = raf.read(); - i.reserved = raf.read(); -} -static void read(RandomAccessFile raf, CURSORDIR c) throws IOException { - c.Width = readU2(raf); - c.Height = readU2(raf); -} -static void read(RandomAccessFile raf, RESDIR rs) throws IOException { - long start = raf.getFilePointer(); - read(raf, rs.Icon); - raf.seek(start); - read(raf, rs.Cursor); - rs.Planes = readU2(raf); - rs.BitCount = readU2(raf); - rs.BytesInRes = read4(raf); - rs.IconCursorId = readU2(raf); -} -/* ImageData and Image Decoder inlining to avoid dependency on SWT - * The following section can be entirely removed if SWT can be used. - */ + static void flipScanLines(byte[] data, int stride, int height) { + int i1 = 0; + int i2 = (height - 1) * stride; + for (int i = 0; i < height / 2; i++) { + for (int index = 0; index < stride; index++) { + byte b = data[index + i1]; + data[index + i1] = data[index + i2]; + data[index + i2] = b; + } + i1 += stride; + i2 -= stride; + } + } -static class RGB { - - /** - * the red component of the RGB - */ - public int red; - - /** - * the green component of the RGB - */ - public int green; - - /** - * the blue component of the RGB - */ - public int blue; - - static final long serialVersionUID = 3258415023461249074L; - -/** - * Constructs an instance of this class with the given - * red, green and blue values. - * - * @param red the red component of the new instance - * @param green the green component of the new instance - * @param blue the blue component of the new instance - * - * @exception IllegalArgumentException <ul> - * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li> - * </ul> - */ -public RGB(int red, int green, int blue) { - if ((red > 255) || (red < 0) || - (green > 255) || (green < 0) || - (blue > 255) || (blue < 0)) - SWT.error(SWT.ERROR_INVALID_ARGUMENT); - this.red = red; - this.green = green; - this.blue = blue; -} + static byte[] loadData(BITMAPINFOHEADER bih, RandomAccessFile raf, int stride) throws IOException { + int dataSize = bih.biHeight * stride; + byte[] data = new byte[dataSize]; + int cmp = bih.biCompression; + if (cmp == 0) { // BMP_NO_COMPRESSION + raf.read(data); + } else { + if (DEBUG) + System.out.println("ICO cannot be compressed?"); //$NON-NLS-1$ + } + return data; + } -/** - * Compares the argument to the receiver, and returns true - * if they represent the <em>same</em> object using a class - * specific comparison. - * - * @param object the object to compare with this object - * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise - * - * @see #hashCode() - */ -public boolean equals (Object object) { - if (object == this) return true; - if (!(object instanceof RGB)) return false; - RGB rgb = (RGB)object; - return (rgb.red == this.red) && (rgb.green == this.green) && (rgb.blue == this.blue); -} + static void unloadIcon(RandomAccessFile raf, ImageData icon) throws IOException { + int sizeImage = (((icon.width * icon.depth + 31) / 32 * 4) + ((icon.width + 31) / 32 * 4)) * icon.height; + write4(raf, BMPHeaderFixedSize); + write4(raf, icon.width); + write4(raf, icon.height * 2); + writeU2(raf, 1); + writeU2(raf, icon.depth); + write4(raf, 0); + write4(raf, sizeImage); + write4(raf, 0); + write4(raf, 0); + write4(raf, icon.palette.colors != null ? icon.palette.colors.length : 0); + write4(raf, 0); + + byte[] rgbs = paletteToBytes(icon.palette); + raf.write(rgbs); + unloadShapeData(raf, icon); + unloadMaskData(raf, icon); + } + + static byte[] paletteToBytes(PaletteData pal) { + int n = pal.colors == null ? 0 : (pal.colors.length < 256 ? pal.colors.length : 256); + byte[] bytes = new byte[n * 4]; + int offset = 0; + for (int i = 0; i < n; i++) { + RGB col = pal.colors[i]; + bytes[offset] = (byte) col.blue; + bytes[offset + 1] = (byte) col.green; + bytes[offset + 2] = (byte) col.red; + offset += 4; + } + return bytes; + } + + static void unloadMaskData(RandomAccessFile raf, ImageData icon) { + ImageData mask = icon.getTransparencyMask(); + int bpl = (icon.width + 7) / 8; + int pad = mask.scanlinePad; + int srcBpl = (bpl + pad - 1) / pad * pad; + int destBpl = (bpl + 3) / 4 * 4; + byte[] buf = new byte[destBpl]; + int offset = (icon.height - 1) * srcBpl; + byte[] data = mask.data; + try { + for (int i = 0; i < icon.height; i++) { + System.arraycopy(data, offset, buf, 0, bpl); + bitInvertData(buf, 0, bpl); + raf.write(buf, 0, destBpl); + offset -= srcBpl; + } + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + } -/** - * Returns an integer hash code for the receiver. Any two - * objects which return <code>true</code> when passed to - * <code>equals</code> must return the same value for this - * method. - * - * @return the receiver's hash - * - * @see #equals(Object) - */ -public int hashCode () { - return (blue << 16) | (green << 8) | red; -} + static void unloadShapeData(RandomAccessFile raf, ImageData icon) { + int bpl = (icon.width * icon.depth + 7) / 8; + int pad = icon.scanlinePad; + int srcBpl = (bpl + pad - 1) / pad * pad; + int destBpl = (bpl + 3) / 4 * 4; + byte[] buf = new byte[destBpl]; + int offset = (icon.height - 1) * srcBpl; + byte[] data = icon.data; + try { + for (int i = 0; i < icon.height; i++) { + System.arraycopy(data, offset, buf, 0, bpl); + raf.write(buf, 0, destBpl); + offset -= srcBpl; + } + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + } -/** - * Returns a string containing a concise, human-readable - * description of the receiver. - * - * @return a string representation of the <code>RGB</code> - */ -public String toString () { - return "RGB {" + red + ", " + green + ", " + blue + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ + static boolean readIconGroup(RandomAccessFile raf, int offset, int size) throws IOException { + raf.seek(offset); + NEWHEADER newHeader = new NEWHEADER(); + read(raf, newHeader); + if (newHeader.ResType != RES_ICON) + return false; + RESDIR[] resDir = new RESDIR[newHeader.ResCount]; + for (int i = 0; i < newHeader.ResCount; i++) { + resDir[i] = new RESDIR(); + read(raf, resDir[i]); + } + return true; + } + + static void copyFile(String src, String dst) throws FileNotFoundException, IOException { + File srcFile = new File(src); + File dstFile = new File(dst); + InputStream in = new BufferedInputStream(new FileInputStream(srcFile)); + OutputStream out = new BufferedOutputStream(new FileOutputStream(dstFile)); + int c; + while ((c = in.read()) != -1) + out.write(c); + in.close(); + out.close(); + } + + /* IO utilities to parse Windows executable */ + static final int IMAGE_DOS_SIGNATURE = 0x5a4d; + static final int IMAGE_NT_SIGNATURE = 0x00004550; + static final int IMAGE_DIRECTORY_ENTRY_RESOURCE = 2; + static final int RES_ICON = 1; + static final int RT_ICON = 3; + static final int RT_GROUP_ICON = 14; + static final int BMPHeaderFixedSize = 40; + static final int IMAGE_NT_OPTIONAL_HDR64_MAGIC = 0x20b; + static final int IMAGE_NT_OPTIONAL_HDR32_MAGIC = 0x10b; + + public static class IMAGE_DOS_HEADER { + int e_magic; // WORD + int e_cblp; // WORD + int e_cp; // WORD + int e_crlc; // WORD + int e_cparhdr; // WORD + int e_minalloc; // WORD + int e_maxalloc; // WORD + int e_ss; // WORD + int e_sp; // WORD + int e_csum; // WORD + int e_ip; // WORD + int e_cs; // WORD + int e_lfarlc; // WORD + int e_ovno; // WORD + int[] e_res = new int[4]; // WORD[4] + int e_oemid; // WORD + int e_oeminfo; // WORD + int[] e_res2 = new int[10]; // WORD[10] + int e_lfanew; // LONG + } + + public static class IMAGE_FILE_HEADER { + int Machine; // WORD + int NumberOfSections; // WORD + int TimeDateStamp; // DWORD + int PointerToSymbolTable; // DWORD + int NumberOfSymbols; // DWORD + int SizeOfOptionalHeader; // WORD + int Characteristics; // WORD + } + + public static class IMAGE_DATA_DIRECTORY { + int VirtualAddress; // DWORD + int Size; // DWORD + } + + public static class IMAGE_OPTIONAL_HEADER { + // Allocate enough storage for the 64 bit version of the header. + int Magic; // WORD + int MajorLinkerVersion; // BYTE + int MinorLinkerVersion; // BYTE + int SizeOfCode; // DWORD + int SizeOfInitializedData; // DWORD + int SizeOfUninitializedData; // DWORD + int AddressOfEntryPoint; // DWORD + int BaseOfCode; // DWORD + int BaseOfData; // DWORD + long ImageBase; // ULONGLONG + int SectionAlignment; // DWORD + int FileAlignment; // DWORD + int MajorOperatingSystemVersion; // WORD + int MinorOperatingSystemVersion; // WORD + int MajorImageVersion; // WORD + int MinorImageVersion; // WORD + int MajorSubsystemVersion; // WORD + int MinorSubsystemVersion; // WORD + int Win32VersionValue; // DWORD + int SizeOfImage; // DWORD + int SizeOfHeaders; // DWORD + int CheckSum; // DWORD + int Subsystem; // WORD + int DllCharacteristics; // WORD + long SizeOfStackReserve; // ULONGLONG + long SizeOfStackCommit; // ULONGLONG + long SizeOfHeapReserve; // ULONGLONG + long SizeOfHeapCommit; // ULONGLONG + int LoaderFlags; // DWORD + int NumberOfRvaAndSizes; // DWORD + IMAGE_DATA_DIRECTORY[] DataDirectory = new IMAGE_DATA_DIRECTORY[16]; + } + + public static class IMAGE_NT_HEADERS { + int Signature; // DWORD + IMAGE_FILE_HEADER FileHeader = new IMAGE_FILE_HEADER(); + IMAGE_OPTIONAL_HEADER OptionalHeader = new IMAGE_OPTIONAL_HEADER(); + final static int FIELD_OFFSET_OptionalHeader = 24; + } + + public static class IMAGE_SECTION_HEADER { + int[] Name = new int[8]; // BYTE[8] + int Misc_VirtualSize; // DWORD (union Misc { DWORD PhysicalAddress; DWORD VirtualSize } + int VirtualAddress; // DWORD + int SizeOfRawData; // DWORD + int PointerToRawData; // DWORD + int PointerToRelocations; // DWORD + int PointerToLinenumbers; // DWORD + int NumberOfRelocations; // WORD + int NumberOfLinenumbers; // WORD + int Characteristics; // DWORD + } + + public static class IMAGE_RESOURCE_DIRECTORY { + int Characteristics; // DWORD + int TimeDateStamp; // DWORD + int MajorVersion; // WORD + int MinorVersion; // WORD + int NumberOfNamedEntries; // WORD - used + int NumberOfIdEntries; // WORD - used + final static int SIZEOF = 16; + } + + public static class IMAGE_RESOURCE_DIRECTORY_ENTRY { + // union + int NameOffset; // DWORD 31 bits + boolean NameIsString; // DWORD 1 bit + int Name; // DWORD + int Id; // WORD + // union + int OffsetToData; // DWORD + int OffsetToDirectory; // DWORD 31 bits + boolean DataIsDirectory; // DWORD 1 bit + } + + public static class IMAGE_RESOURCE_DATA_ENTRY { + int OffsetToData; // DWORD + int Size; // DWORD + int CodePage; // DWORD + int Reserved; // DWORD + } + + public static class NEWHEADER { + int Reserved; // WORD + int ResType; // WORD + int ResCount; // WORD + } + + public static class ICONRESDIR { + int Width; // BYTE + int Height; // BYTE + int ColorCount; // BYTE + int reserved; // BYTE + } + + public static class CURSORDIR { + int Width; // WORD + int Height; // WORD + } + + public static class RESDIR { + // union + ICONRESDIR Icon = new ICONRESDIR(); + CURSORDIR Cursor = new CURSORDIR(); + int Planes; // WORD + int BitCount; // WORD + int BytesInRes; // DWORD + int IconCursorId; // WORD + } + + public static class BITMAPINFOHEADER { + int biSize; // DWORD + int biWidth; // LONG + int biHeight; // LONG + int biPlanes; // WORD + int biBitCount; // WORD + int biCompression; // DWORD + int biSizeImage; // DWORD + int biXPelsPerMeter; // LONG + int biYPelsPerMeter; // LONG + int biClrUsed; // DWORD + int biClrImportant; // DWORD + } + + static class RGBQUAD { + int rgBlue; // BYTE + int rgbGreen; // BYTE + int rgbRed; // BYTE + int rgbReserved; // BYTE + } + + static class BITMAPINFO { + BITMAPINFOHEADER bmiHeader = new BITMAPINFOHEADER(); + RGBQUAD[] bmiColors = null; + } + + static void read(RandomAccessFile raf, BITMAPINFOHEADER bih) throws IOException { + bih.biSize = read4(raf); + bih.biWidth = read4(raf); + bih.biHeight = read4(raf); + bih.biPlanes = readU2(raf); + bih.biBitCount = readU2(raf); + bih.biCompression = read4(raf); + bih.biSizeImage = read4(raf); + bih.biXPelsPerMeter = read4(raf); + bih.biYPelsPerMeter = read4(raf); + bih.biClrUsed = read4(raf); + bih.biClrImportant = read4(raf); + } + + static void read(RandomAccessFile raf, BITMAPINFO bi) throws IOException { + read(raf, bi.bmiHeader); + } + + /* Little Endian helpers */ + static int readU2(RandomAccessFile raf) throws IOException { + int b0 = raf.readByte() & 0xFF; + int b1 = raf.readByte() & 0xFF; + return (b1 << 8 | b0); + } + + static int read4(RandomAccessFile raf) throws IOException { + int b0 = raf.readByte() & 0xFF; + int b1 = raf.readByte() & 0xFF; + int b2 = raf.readByte() & 0xFF; + int b3 = raf.readByte() & 0xFF; + return b3 << 24 | b2 << 16 | b1 << 8 | b0; + } + + static long read8(RandomAccessFile raf) throws IOException { + int b0 = raf.readByte() & 0xFF; + int b1 = raf.readByte() & 0xFF; + int b2 = raf.readByte() & 0xFF; + int b3 = raf.readByte() & 0xFF; + int b4 = raf.readByte() & 0xFF; + int b5 = raf.readByte() & 0xFF; + int b6 = raf.readByte() & 0xFF; + int b7 = raf.readByte() & 0xFF; + return b7 << 56 | b6 << 48 | b5 << 40 | b4 << 32 | b3 << 24 | b2 << 16 | b1 << 8 | b0; + } + + static void write4(RandomAccessFile raf, int value) throws IOException { + raf.write(value & 0xFF); + raf.write((value >> 8) & 0xFF); + raf.write((value >> 16) & 0xFF); + raf.write((value >> 24) & 0xFF); + } + + static void writeU2(RandomAccessFile raf, int value) throws IOException { + raf.write(value & 0xFF); + raf.write((value >> 8) & 0xFF); + } + + static void read(RandomAccessFile raf, IMAGE_DOS_HEADER idh) throws IOException { + idh.e_magic = readU2(raf); + idh.e_cblp = readU2(raf); + idh.e_cp = readU2(raf); + idh.e_crlc = readU2(raf); + idh.e_cparhdr = readU2(raf); + idh.e_minalloc = readU2(raf); + idh.e_maxalloc = readU2(raf); + idh.e_ss = readU2(raf); + idh.e_sp = readU2(raf); + idh.e_csum = readU2(raf); + idh.e_ip = readU2(raf); + idh.e_cs = readU2(raf); + idh.e_lfarlc = readU2(raf); + idh.e_ovno = readU2(raf); + for (int i = 0; i < idh.e_res.length; i++) + idh.e_res[i] = readU2(raf); + idh.e_oemid = readU2(raf); + idh.e_oeminfo = readU2(raf); + for (int i = 0; i < idh.e_res2.length; i++) + idh.e_res2[i] = readU2(raf); + idh.e_lfanew = read4(raf); + } + + static void read(RandomAccessFile raf, IMAGE_FILE_HEADER ifh) throws IOException { + ifh.Machine = readU2(raf); + ifh.NumberOfSections = readU2(raf); + ifh.TimeDateStamp = read4(raf); + ifh.PointerToSymbolTable = read4(raf); + ifh.NumberOfSymbols = read4(raf); + ifh.SizeOfOptionalHeader = readU2(raf); + ifh.Characteristics = readU2(raf); + } + + static void read(RandomAccessFile raf, IMAGE_DATA_DIRECTORY idd) throws IOException { + idd.VirtualAddress = read4(raf); + idd.Size = read4(raf); + } + + static void read(RandomAccessFile raf, IMAGE_OPTIONAL_HEADER ioh) throws IOException { + ioh.Magic = readU2(raf); + + // Assume file is 32bit executable unless x64 magic is present + boolean is32 = !(ioh.Magic == IMAGE_NT_OPTIONAL_HDR64_MAGIC); + + ioh.MajorLinkerVersion = raf.read(); + ioh.MinorLinkerVersion = raf.read(); + ioh.SizeOfCode = read4(raf); + ioh.SizeOfInitializedData = read4(raf); + ioh.SizeOfUninitializedData = read4(raf); + ioh.AddressOfEntryPoint = read4(raf); + ioh.BaseOfCode = read4(raf); + + if (is32) { + ioh.BaseOfData = read4(raf); + ioh.ImageBase = read4(raf); + } else { + // BaseOfData deleted in the 64 bit version PE32+ + ioh.ImageBase = read8(raf); + } -//$NON-NLS-4$ -} + ioh.SectionAlignment = read4(raf); + ioh.FileAlignment = read4(raf); + ioh.MajorOperatingSystemVersion = readU2(raf); + ioh.MinorOperatingSystemVersion = readU2(raf); + ioh.MajorImageVersion = readU2(raf); + ioh.MinorImageVersion = readU2(raf); + ioh.MajorSubsystemVersion = readU2(raf); + ioh.MinorSubsystemVersion = readU2(raf); + ioh.Win32VersionValue = read4(raf); + ioh.SizeOfImage = read4(raf); + ioh.SizeOfHeaders = read4(raf); + ioh.CheckSum = read4(raf); + ioh.Subsystem = readU2(raf); + ioh.DllCharacteristics = readU2(raf); + + if (is32) { + ioh.SizeOfStackReserve = read4(raf); + ioh.SizeOfStackCommit = read4(raf); + ioh.SizeOfHeapReserve = read4(raf); + ioh.SizeOfHeapCommit = read4(raf); + } else { + ioh.SizeOfStackReserve = read8(raf); + ioh.SizeOfStackCommit = read8(raf); + ioh.SizeOfHeapReserve = read8(raf); + ioh.SizeOfHeapCommit = read8(raf); + } -} -static class PaletteData { - - /** - * true if the receiver is a direct palette, - * and false otherwise - */ - public boolean isDirect; - - /** - * the RGB values for an indexed palette, where the - * indices of the array correspond to pixel values - */ - public RGB[] colors; - - /** - * the red mask for a direct palette - */ - public int redMask; - - /** - * the green mask for a direct palette - */ - public int greenMask; - - /** - * the blue mask for a direct palette - */ - public int blueMask; - - /** - * the red shift for a direct palette - */ - public int redShift; - - /** - * the green shift for a direct palette - */ - public int greenShift; - - /** - * the blue shift for a direct palette - */ - public int blueShift; + ioh.LoaderFlags = read4(raf); + ioh.NumberOfRvaAndSizes = read4(raf); + for (int i = 0; i < ioh.DataDirectory.length; i++) { + ioh.DataDirectory[i] = new IMAGE_DATA_DIRECTORY(); + read(raf, ioh.DataDirectory[i]); + } + } -/** - * Constructs a new indexed palette given an array of RGB values. - * - * @param colors the array of <code>RGB</code>s for the palette - * - * @exception IllegalArgumentException <ul> - * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> - * </ul> - */ -public PaletteData(RGB[] colors) { - if (colors == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - this.colors = colors; - this.isDirect = false; -} + static void read(RandomAccessFile raf, IMAGE_NT_HEADERS inh) throws IOException { + inh.Signature = read4(raf); + read(raf, inh.FileHeader); + read(raf, inh.OptionalHeader); + } -/** - * Constructs a new direct palette given the red, green and blue masks. - * - * @param redMask the red mask - * @param greenMask the green mask - * @param blueMask the blue mask - */ -public PaletteData(int redMask, int greenMask, int blueMask) { - this.redMask = redMask; - this.greenMask = greenMask; - this.blueMask = blueMask; - this.isDirect = true; - this.redShift = shiftForMask(redMask); - this.greenShift = shiftForMask(greenMask); - this.blueShift = shiftForMask(blueMask); -} + static void read(RandomAccessFile raf, IMAGE_SECTION_HEADER ish) throws IOException { + for (int i = 0; i < ish.Name.length; i++) + ish.Name[i] = raf.read(); + ish.Misc_VirtualSize = read4(raf); + ish.VirtualAddress = read4(raf); + ish.SizeOfRawData = read4(raf); + ish.PointerToRawData = read4(raf); + ish.PointerToRelocations = read4(raf); + ish.PointerToLinenumbers = read4(raf); + ish.NumberOfRelocations = readU2(raf); + ish.NumberOfLinenumbers = readU2(raf); + ish.Characteristics = read4(raf); + } -/** - * Returns the pixel value corresponding to the given <code>RBG</code>. - * - * @param rgb the RGB to get the pixel value for - * @return the pixel value for the given RGB - * - * @exception IllegalArgumentException <ul> - * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> - * <li>ERROR_INVALID_ARGUMENT - if the RGB is not found in the palette</li> - * </ul> - */ -public int getPixel(RGB rgb) { - if (rgb == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - if (isDirect) { - int pixel = 0; - pixel |= (redShift < 0 ? rgb.red << -redShift : rgb.red >>> redShift) & redMask; - pixel |= (greenShift < 0 ? rgb.green << -greenShift : rgb.green >>> greenShift) & greenMask; - pixel |= (blueShift < 0 ? rgb.blue << -blueShift : rgb.blue >>> blueShift) & blueMask; - return pixel; - } - - for (int i = 0; i < colors.length; i++) { - if (colors[i].equals(rgb)) return i; - } - /* The RGB did not exist in the palette */ - SWT.error(SWT.ERROR_INVALID_ARGUMENT); - return 0; -} + static void read(RandomAccessFile raf, IMAGE_RESOURCE_DIRECTORY ird) throws IOException { + ird.Characteristics = read4(raf); + ird.TimeDateStamp = read4(raf); + ird.MajorVersion = readU2(raf); + ird.MinorVersion = readU2(raf); + ird.NumberOfNamedEntries = readU2(raf); + ird.NumberOfIdEntries = readU2(raf); + } -/** - * Returns an <code>RGB</code> corresponding to the given pixel value. - * - * @param pixel the pixel to get the RGB value for - * @return the RGB value for the given pixel - * - * @exception IllegalArgumentException <ul> - * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> - * <li>ERROR_INVALID_ARGUMENT - if the pixel does not exist in the palette</li> - * </ul> - */ -public RGB getRGB(int pixel) { - if (isDirect) { - int r = pixel & redMask; - r = (redShift < 0) ? r >>> -redShift : r << redShift; - int g = pixel & greenMask; - g = (greenShift < 0) ? g >>> -greenShift : g << greenShift; - int b = pixel & blueMask; - b = (blueShift < 0) ? b >>> -blueShift : b << blueShift; - return new RGB(r, g, b); - } - if (pixel < 0 || pixel >= colors.length) { - SWT.error(SWT.ERROR_INVALID_ARGUMENT); - } - return colors[pixel]; -} + static void read(RandomAccessFile raf, IMAGE_RESOURCE_DIRECTORY_ENTRY irde) throws IOException { + irde.Name = read4(raf); + irde.OffsetToData = read4(raf); + // construct other union members + irde.NameOffset = irde.Name & ~(1 << 31); + irde.NameIsString = (irde.Name & (1 << 31)) != 0; + irde.Id = irde.Name & 0xFFFF; + irde.OffsetToDirectory = irde.OffsetToData & ~(1 << 31); + irde.DataIsDirectory = (irde.OffsetToData & (1 << 31)) != 0; + } -/** - * Returns all the RGB values in the receiver if it is an - * indexed palette, or null if it is a direct palette. - * - * @return the <code>RGB</code>s for the receiver or null - */ -public RGB[] getRGBs() { - return colors; -} + static void read(RandomAccessFile raf, IMAGE_RESOURCE_DATA_ENTRY irde) throws IOException { + irde.OffsetToData = read4(raf); + irde.Size = read4(raf); + irde.CodePage = read4(raf); + irde.Reserved = read4(raf); + } -/** - * Computes the shift value for a given mask. - * - * @param mask the mask to compute the shift for - * @return the shift amount - * - * @see IconExe.PaletteData - */ -int shiftForMask(int mask) { - for (int i = 31; i >= 0; i--) { - if (((mask >> i) & 0x1) != 0) return 7 - i; + static void read(RandomAccessFile raf, NEWHEADER nh) throws IOException { + nh.Reserved = readU2(raf); + nh.ResType = readU2(raf); + nh.ResCount = readU2(raf); } - return 32; -} -} -static class ImageLoader { - - /** - * the array of ImageData objects in this ImageLoader. - * This array is read in when the load method is called, - * and it is written out when the save method is called - */ - public ImageData[] data; - - /** - * the width of the logical screen on which the images - * reside, in pixels (this corresponds to the GIF89a - * Logical Screen Width value) - */ - public int logicalScreenWidth; + static void read(RandomAccessFile raf, ICONRESDIR i) throws IOException { + i.Width = raf.read(); + i.Height = raf.read(); + i.ColorCount = raf.read(); + i.reserved = raf.read(); + } - /** - * the height of the logical screen on which the images - * reside, in pixels (this corresponds to the GIF89a - * Logical Screen Height value) - */ - public int logicalScreenHeight; + static void read(RandomAccessFile raf, CURSORDIR c) throws IOException { + c.Width = readU2(raf); + c.Height = readU2(raf); + } - /** - * the background pixel for the logical screen (this - * corresponds to the GIF89a Background Color Index value). - * The default is -1 which means 'unspecified background' - * - */ - public int backgroundPixel; + static void read(RandomAccessFile raf, RESDIR rs) throws IOException { + long start = raf.getFilePointer(); + read(raf, rs.Icon); + raf.seek(start); + read(raf, rs.Cursor); + rs.Planes = readU2(raf); + rs.BitCount = readU2(raf); + rs.BytesInRes = read4(raf); + rs.IconCursorId = readU2(raf); + } - /** - * the number of times to repeat the display of a sequence - * of animated images (this corresponds to the commonly-used - * GIF application extension for "NETSCAPE 2.0 01") + /* ImageData and Image Decoder inlining to avoid dependency on SWT + * The following section can be entirely removed if SWT can be used. */ - public int repeatCount; - - /* - * the set of ImageLoader event listeners, created on demand - */ - Vector imageLoaderListeners; - -/** - * Construct a new empty ImageLoader. - */ -public ImageLoader() { - reset(); -} -/** - * Resets the fields of the ImageLoader, except for the - * <code>imageLoaderListeners</code> field. - */ -void reset() { - data = null; - logicalScreenWidth = 0; - logicalScreenHeight = 0; - backgroundPixel = -1; - repeatCount = 1; -} + static class RGB { + + /** + * the red component of the RGB + */ + public int red; + + /** + * the green component of the RGB + */ + public int green; + + /** + * the blue component of the RGB + */ + public int blue; + + static final long serialVersionUID = 3258415023461249074L; + + /** + * Constructs an instance of this class with the given + * red, green and blue values. + * + * @param red the red component of the new instance + * @param green the green component of the new instance + * @param blue the blue component of the new instance + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_INVALID_ARGUMENT - if the red, green or blue argument is not between 0 and 255</li> + * </ul> + */ + public RGB(int red, int green, int blue) { + if ((red > 255) || (red < 0) || (green > 255) || (green < 0) || (blue > 255) || (blue < 0)) + SWT.error(SWT.ERROR_INVALID_ARGUMENT); + this.red = red; + this.green = green; + this.blue = blue; + } -/** - * Loads an array of <code>ImageData</code> objects from the - * specified input stream. Throws an error if either an error - * occurs while loading the images, or if the images are not - * of a supported type. Returns the loaded image data array. - * - * @param stream the input stream to load the images from - * @return an array of <code>ImageData</code> objects loaded from the specified input stream - * - * @exception IllegalArgumentException <ul> - * <li>ERROR_NULL_ARGUMENT - if the stream is null</li> - * </ul> - * @exception RuntimeException <ul> - * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li> - * <li>ERROR_IO - if an input/output error occurs while reading data</li> - * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li> - * </ul> - */ -public ImageData[] load(InputStream stream) { - if (stream == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - reset(); - data = FileFormat.load(stream, this); - return data; -} + /** + * Compares the argument to the receiver, and returns true + * if they represent the <em>same</em> object using a class + * specific comparison. + * + * @param object the object to compare with this object + * @return <code>true</code> if the object is the same as this object and <code>false</code> otherwise + * + * @see #hashCode() + */ + public boolean equals(Object object) { + if (object == this) + return true; + if (!(object instanceof RGB)) + return false; + RGB rgb = (RGB) object; + return (rgb.red == this.red) && (rgb.green == this.green) && (rgb.blue == this.blue); + } -/** - * Loads an array of <code>ImageData</code> objects from the - * file with the specified name. Throws an error if either - * an error occurs while loading the images, or if the images are - * not of a supported type. Returns the loaded image data array. - * - * @param filename the name of the file to load the images from - * @return an array of <code>ImageData</code> objects loaded from the specified file - * - * @exception IllegalArgumentException <ul> - * <li>ERROR_NULL_ARGUMENT - if the file name is null</li> - * </ul> - * @exception RuntimeException <ul> - * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li> - * <li>ERROR_IO - if an IO error occurs while reading data</li> - * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li> - * </ul> - */ -public ImageData[] load(String filename) { - if (filename == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - InputStream stream = null; - try { - stream = new BufferedInputStream(new FileInputStream(filename)); - return load(stream); - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); - } finally { - try { - if (stream != null) stream.close(); - } catch (IOException e) { - // Ignore error + /** + * Returns an integer hash code for the receiver. Any two + * objects which return <code>true</code> when passed to + * <code>equals</code> must return the same value for this + * method. + * + * @return the receiver's hash + * + * @see #equals(Object) + */ + public int hashCode() { + return (blue << 16) | (green << 8) | red; } - } - return null; -} -} -static class ImageData { - - /** - * The width of the image, in pixels. - */ - public int width; - /** - * The height of the image, in pixels. - */ - public int height; + /** + * Returns a string containing a concise, human-readable + * description of the receiver. + * + * @return a string representation of the <code>RGB</code> + */ + public String toString() { + return "RGB {" + red + ", " + green + ", " + blue + "}"; //$NON-NLS-1$//$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$ - /** - * The color depth of the image, in bits per pixel. - * <p> - * Note that a depth of 8 or less does not necessarily - * mean that the image is palette indexed, or - * conversely that a depth greater than 8 means that - * the image is direct color. Check the associated - * PaletteData's isDirect field for such determinations. - */ - public int depth; + //$NON-NLS-4$ + } - /** - * The scanline padding. - * <p> - * If one scanline of the image is not a multiple of - * this number, it will be padded with zeros until it is. - * </p> - */ - public int scanlinePad; + } - /** - * The number of bytes per scanline. - * <p> - * This is a multiple of the scanline padding. - * </p> - */ - public int bytesPerLine; + static class PaletteData { + + /** + * true if the receiver is a direct palette, + * and false otherwise + */ + public boolean isDirect; + + /** + * the RGB values for an indexed palette, where the + * indices of the array correspond to pixel values + */ + public RGB[] colors; + + /** + * the red mask for a direct palette + */ + public int redMask; + + /** + * the green mask for a direct palette + */ + public int greenMask; + + /** + * the blue mask for a direct palette + */ + public int blueMask; + + /** + * the red shift for a direct palette + */ + public int redShift; + + /** + * the green shift for a direct palette + */ + public int greenShift; + + /** + * the blue shift for a direct palette + */ + public int blueShift; + + /** + * Constructs a new indexed palette given an array of RGB values. + * + * @param colors the array of <code>RGB</code>s for the palette + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> + * </ul> + */ + public PaletteData(RGB[] colors) { + if (colors == null) + SWT.error(SWT.ERROR_NULL_ARGUMENT); + this.colors = colors; + this.isDirect = false; + } - /** - * The pixel data of the image. - * <p> - * Note that for 16 bit depth images the pixel data is stored - * in least significant byte order; however, for 24bit and - * 32bit depth images the pixel data is stored in most - * significant byte order. - * </p> - */ - public byte[] data; + /** + * Constructs a new direct palette given the red, green and blue masks. + * + * @param redMask the red mask + * @param greenMask the green mask + * @param blueMask the blue mask + */ + public PaletteData(int redMask, int greenMask, int blueMask) { + this.redMask = redMask; + this.greenMask = greenMask; + this.blueMask = blueMask; + this.isDirect = true; + this.redShift = shiftForMask(redMask); + this.greenShift = shiftForMask(greenMask); + this.blueShift = shiftForMask(blueMask); + } - /** - * The color table for the image. - */ - public PaletteData palette; + /** + * Returns the pixel value corresponding to the given <code>RBG</code>. + * + * @param rgb the RGB to get the pixel value for + * @return the pixel value for the given RGB + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the RGB is not found in the palette</li> + * </ul> + */ + public int getPixel(RGB rgb) { + if (rgb == null) + SWT.error(SWT.ERROR_NULL_ARGUMENT); + if (isDirect) { + int pixel = 0; + pixel |= (redShift < 0 ? rgb.red << -redShift : rgb.red >>> redShift) & redMask; + pixel |= (greenShift < 0 ? rgb.green << -greenShift : rgb.green >>> greenShift) & greenMask; + pixel |= (blueShift < 0 ? rgb.blue << -blueShift : rgb.blue >>> blueShift) & blueMask; + return pixel; + } - /** - * The transparent pixel. - * <p> - * Pixels with this value are transparent. - * </p><p> - * The default is -1 which means 'no transparent pixel'. - * </p> - */ - public int transparentPixel; + for (int i = 0; i < colors.length; i++) { + if (colors[i].equals(rgb)) + return i; + } + /* The RGB did not exist in the palette */ + SWT.error(SWT.ERROR_INVALID_ARGUMENT); + return 0; + } - /** - * An icon-specific field containing the data from the icon mask. - * <p> - * This is a 1 bit bitmap stored with the most significant - * bit first. The number of bytes per scanline is - * '((width + 7) / 8 + (maskPad - 1)) / maskPad * maskPad'. - * </p><p> - * The default is null which means 'no transparency mask'. - * </p> - */ - public byte[] maskData; + /** + * Returns an <code>RGB</code> corresponding to the given pixel value. + * + * @param pixel the pixel to get the RGB value for + * @return the RGB value for the given pixel + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the argument is null</li> + * <li>ERROR_INVALID_ARGUMENT - if the pixel does not exist in the palette</li> + * </ul> + */ + public RGB getRGB(int pixel) { + if (isDirect) { + int r = pixel & redMask; + r = (redShift < 0) ? r >>> -redShift : r << redShift; + int g = pixel & greenMask; + g = (greenShift < 0) ? g >>> -greenShift : g << greenShift; + int b = pixel & blueMask; + b = (blueShift < 0) ? b >>> -blueShift : b << blueShift; + return new RGB(r, g, b); + } + if (pixel < 0 || pixel >= colors.length) { + SWT.error(SWT.ERROR_INVALID_ARGUMENT); + } + return colors[pixel]; + } - /** - * An icon-specific field containing the scanline pad of the mask. - * <p> - * If one scanline of the transparency mask is not a - * multiple of this number, it will be padded with zeros until - * it is. - * </p> - */ - public int maskPad; - - /** - * The alpha data of the image. - * <p> - * Every pixel can have an <em>alpha blending</em> value that - * varies from 0, meaning fully transparent, to 255 meaning - * fully opaque. The number of bytes per scanline is - * 'width'. - * </p> - */ - public byte[] alphaData; - - /** - * The global alpha value to be used for every pixel. - * <p> - * If this value is set, the <code>alphaData</code> field - * is ignored and when the image is rendered each pixel - * will be blended with the background an amount - * proportional to this value. - * </p><p> - * The default is -1 which means 'no global alpha value' - * </p> - */ - public int alpha; + /** + * Returns all the RGB values in the receiver if it is an + * indexed palette, or null if it is a direct palette. + * + * @return the <code>RGB</code>s for the receiver or null + */ + public RGB[] getRGBs() { + return colors; + } - /** - * The type of file from which the image was read. - * - * It is expressed as one of the following values: - * <dl> - * <dt><code>IMAGE_BMP</code></dt> - * <dd>Windows BMP file format, no compression</dd> - * <dt><code>IMAGE_BMP_RLE</code></dt> - * <dd>Windows BMP file format, RLE compression if appropriate</dd> - * <dt><code>IMAGE_GIF</code></dt> - * <dd>GIF file format</dd> - * <dt><code>IMAGE_ICO</code></dt> - * <dd>Windows ICO file format</dd> - * <dt><code>IMAGE_JPEG</code></dt> - * <dd>JPEG file format</dd> - * <dt><code>IMAGE_PNG</code></dt> - * <dd>PNG file format</dd> - * </dl> - */ - public int type; + /** + * Computes the shift value for a given mask. + * + * @param mask the mask to compute the shift for + * @return the shift amount + * + * @see IconExe.PaletteData + */ + int shiftForMask(int mask) { + for (int i = 31; i >= 0; i--) { + if (((mask >> i) & 0x1) != 0) + return 7 - i; + } + return 32; + } - /** - * The x coordinate of the top left corner of the image - * within the logical screen (this field corresponds to - * the GIF89a Image Left Position value). - */ - public int x; + } - /** - * The y coordinate of the top left corner of the image - * within the logical screen (this field corresponds to - * the GIF89a Image Top Position value). - */ - public int y; + static class ImageLoader { + + /** + * the array of ImageData objects in this ImageLoader. + * This array is read in when the load method is called, + * and it is written out when the save method is called + */ + public ImageData[] data; + + /** + * the width of the logical screen on which the images + * reside, in pixels (this corresponds to the GIF89a + * Logical Screen Width value) + */ + public int logicalScreenWidth; + + /** + * the height of the logical screen on which the images + * reside, in pixels (this corresponds to the GIF89a + * Logical Screen Height value) + */ + public int logicalScreenHeight; + + /** + * the background pixel for the logical screen (this + * corresponds to the GIF89a Background Color Index value). + * The default is -1 which means 'unspecified background' + * + */ + public int backgroundPixel; + + /** + * the number of times to repeat the display of a sequence + * of animated images (this corresponds to the commonly-used + * GIF application extension for "NETSCAPE 2.0 01") + */ + public int repeatCount; + + /* + * the set of ImageLoader event listeners, created on demand + */ + Vector<?> imageLoaderListeners; + + /** + * Construct a new empty ImageLoader. + */ + public ImageLoader() { + reset(); + } - /** - * A description of how to dispose of the current image - * before displaying the next. - * - * It is expressed as one of the following values: - * <dl> - * <dt><code>DM_UNSPECIFIED</code></dt> - * <dd>disposal method not specified</dd> - * <dt><code>DM_FILL_NONE</code></dt> - * <dd>do nothing - leave the image in place</dd> - * <dt><code>DM_FILL_BACKGROUND</code></dt> - * <dd>fill with the background color</dd> - * <dt><code>DM_FILL_PREVIOUS</code></dt> - * <dd>restore the previous picture</dd> - * </dl> - * (this field corresponds to the GIF89a Disposal Method value) - */ - public int disposalMethod; + /** + * Resets the fields of the ImageLoader, except for the + * <code>imageLoaderListeners</code> field. + */ + void reset() { + data = null; + logicalScreenWidth = 0; + logicalScreenHeight = 0; + backgroundPixel = -1; + repeatCount = 1; + } - /** - * The time to delay before displaying the next image - * in an animation (this field corresponds to the GIF89a - * Delay Time value). - */ - public int delayTime; + /** + * Loads an array of <code>ImageData</code> objects from the + * specified input stream. Throws an error if either an error + * occurs while loading the images, or if the images are not + * of a supported type. Returns the loaded image data array. + * + * @param stream the input stream to load the images from + * @return an array of <code>ImageData</code> objects loaded from the specified input stream + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the stream is null</li> + * </ul> + * @exception RuntimeException <ul> + * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li> + * <li>ERROR_IO - if an input/output error occurs while reading data</li> + * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li> + * </ul> + */ + public ImageData[] load(InputStream stream) { + if (stream == null) + SWT.error(SWT.ERROR_NULL_ARGUMENT); + reset(); + data = FileFormat.load(stream, this); + return data; + } - /** - * Arbitrary channel width data to 8-bit conversion table. - */ - static final byte[][] ANY_TO_EIGHT = new byte[9][]; - static { - for (int b = 0; b < 9; ++b) { - byte[] data = ANY_TO_EIGHT[b] = new byte[1 << b]; - if (b == 0) continue; - int inc = 0; - for (int bit = 0x10000; (bit >>= b) != 0;) inc |= bit; - for (int v = 0, p = 0; v < 0x10000; v+= inc) data[p++] = (byte)(v >> 8); + /** + * Loads an array of <code>ImageData</code> objects from the + * file with the specified name. Throws an error if either + * an error occurs while loading the images, or if the images are + * not of a supported type. Returns the loaded image data array. + * + * @param filename the name of the file to load the images from + * @return an array of <code>ImageData</code> objects loaded from the specified file + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the file name is null</li> + * </ul> + * @exception RuntimeException <ul> + * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li> + * <li>ERROR_IO - if an IO error occurs while reading data</li> + * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li> + * </ul> + */ + public ImageData[] load(String filename) { + if (filename == null) + SWT.error(SWT.ERROR_NULL_ARGUMENT); + InputStream stream = null; + try { + stream = new BufferedInputStream(new FileInputStream(filename)); + return load(stream); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } finally { + try { + if (stream != null) + stream.close(); + } catch (IOException e) { + // Ignore error + } + } + return null; } } - static final byte[] ONE_TO_ONE_MAPPING = ANY_TO_EIGHT[8]; - /** - * Scaled 8x8 Bayer dither matrix. - */ - static final int[][] DITHER_MATRIX = { - { 0xfc0000, 0x7c0000, 0xdc0000, 0x5c0000, 0xf40000, 0x740000, 0xd40000, 0x540000 }, - { 0x3c0000, 0xbc0000, 0x1c0000, 0x9c0000, 0x340000, 0xb40000, 0x140000, 0x940000 }, - { 0xcc0000, 0x4c0000, 0xec0000, 0x6c0000, 0xc40000, 0x440000, 0xe40000, 0x640000 }, - { 0x0c0000, 0x8c0000, 0x2c0000, 0xac0000, 0x040000, 0x840000, 0x240000, 0xa40000 }, - { 0xf00000, 0x700000, 0xd00000, 0x500000, 0xf80000, 0x780000, 0xd80000, 0x580000 }, - { 0x300000, 0xb00000, 0x100000, 0x900000, 0x380000, 0xb80000, 0x180000, 0x980000 }, - { 0xc00000, 0x400000, 0xe00000, 0x600000, 0xc80000, 0x480000, 0xe80000, 0x680000 }, - { 0x000000, 0x800000, 0x200000, 0xa00000, 0x080000, 0x880000, 0x280000, 0xa80000 } - }; + static class ImageData { + + /** + * The width of the image, in pixels. + */ + public int width; + + /** + * The height of the image, in pixels. + */ + public int height; + + /** + * The color depth of the image, in bits per pixel. + * <p> + * Note that a depth of 8 or less does not necessarily + * mean that the image is palette indexed, or + * conversely that a depth greater than 8 means that + * the image is direct color. Check the associated + * PaletteData's isDirect field for such determinations. + */ + public int depth; + + /** + * The scanline padding. + * <p> + * If one scanline of the image is not a multiple of + * this number, it will be padded with zeros until it is. + * </p> + */ + public int scanlinePad; + + /** + * The number of bytes per scanline. + * <p> + * This is a multiple of the scanline padding. + * </p> + */ + public int bytesPerLine; + + /** + * The pixel data of the image. + * <p> + * Note that for 16 bit depth images the pixel data is stored + * in least significant byte order; however, for 24bit and + * 32bit depth images the pixel data is stored in most + * significant byte order. + * </p> + */ + public byte[] data; + + /** + * The color table for the image. + */ + public PaletteData palette; + + /** + * The transparent pixel. + * <p> + * Pixels with this value are transparent. + * </p><p> + * The default is -1 which means 'no transparent pixel'. + * </p> + */ + public int transparentPixel; + + /** + * An icon-specific field containing the data from the icon mask. + * <p> + * This is a 1 bit bitmap stored with the most significant + * bit first. The number of bytes per scanline is + * '((width + 7) / 8 + (maskPad - 1)) / maskPad * maskPad'. + * </p><p> + * The default is null which means 'no transparency mask'. + * </p> + */ + public byte[] maskData; + + /** + * An icon-specific field containing the scanline pad of the mask. + * <p> + * If one scanline of the transparency mask is not a + * multiple of this number, it will be padded with zeros until + * it is. + * </p> + */ + public int maskPad; + + /** + * The alpha data of the image. + * <p> + * Every pixel can have an <em>alpha blending</em> value that + * varies from 0, meaning fully transparent, to 255 meaning + * fully opaque. The number of bytes per scanline is + * 'width'. + * </p> + */ + public byte[] alphaData; + + /** + * The global alpha value to be used for every pixel. + * <p> + * If this value is set, the <code>alphaData</code> field + * is ignored and when the image is rendered each pixel + * will be blended with the background an amount + * proportional to this value. + * </p><p> + * The default is -1 which means 'no global alpha value' + * </p> + */ + public int alpha; + + /** + * The type of file from which the image was read. + * + * It is expressed as one of the following values: + * <dl> + * <dt><code>IMAGE_BMP</code></dt> + * <dd>Windows BMP file format, no compression</dd> + * <dt><code>IMAGE_BMP_RLE</code></dt> + * <dd>Windows BMP file format, RLE compression if appropriate</dd> + * <dt><code>IMAGE_GIF</code></dt> + * <dd>GIF file format</dd> + * <dt><code>IMAGE_ICO</code></dt> + * <dd>Windows ICO file format</dd> + * <dt><code>IMAGE_JPEG</code></dt> + * <dd>JPEG file format</dd> + * <dt><code>IMAGE_PNG</code></dt> + * <dd>PNG file format</dd> + * </dl> + */ + public int type; + + /** + * The x coordinate of the top left corner of the image + * within the logical screen (this field corresponds to + * the GIF89a Image Left Position value). + */ + public int x; + + /** + * The y coordinate of the top left corner of the image + * within the logical screen (this field corresponds to + * the GIF89a Image Top Position value). + */ + public int y; + + /** + * A description of how to dispose of the current image + * before displaying the next. + * + * It is expressed as one of the following values: + * <dl> + * <dt><code>DM_UNSPECIFIED</code></dt> + * <dd>disposal method not specified</dd> + * <dt><code>DM_FILL_NONE</code></dt> + * <dd>do nothing - leave the image in place</dd> + * <dt><code>DM_FILL_BACKGROUND</code></dt> + * <dd>fill with the background color</dd> + * <dt><code>DM_FILL_PREVIOUS</code></dt> + * <dd>restore the previous picture</dd> + * </dl> + * (this field corresponds to the GIF89a Disposal Method value) + */ + public int disposalMethod; + + /** + * The time to delay before displaying the next image + * in an animation (this field corresponds to the GIF89a + * Delay Time value). + */ + public int delayTime; + + /** + * Arbitrary channel width data to 8-bit conversion table. + */ + static final byte[][] ANY_TO_EIGHT = new byte[9][]; + static { + for (int b = 0; b < 9; ++b) { + byte[] data = ANY_TO_EIGHT[b] = new byte[1 << b]; + if (b == 0) + continue; + int inc = 0; + for (int bit = 0x10000; (bit >>= b) != 0;) + inc |= bit; + for (int v = 0, p = 0; v < 0x10000; v += inc) + data[p++] = (byte) (v >> 8); + } + } + static final byte[] ONE_TO_ONE_MAPPING = ANY_TO_EIGHT[8]; + + /** + * Scaled 8x8 Bayer dither matrix. + */ + static final int[][] DITHER_MATRIX = { {0xfc0000, 0x7c0000, 0xdc0000, 0x5c0000, 0xf40000, 0x740000, 0xd40000, 0x540000}, {0x3c0000, 0xbc0000, 0x1c0000, 0x9c0000, 0x340000, 0xb40000, 0x140000, 0x940000}, {0xcc0000, 0x4c0000, 0xec0000, 0x6c0000, 0xc40000, 0x440000, 0xe40000, 0x640000}, {0x0c0000, 0x8c0000, 0x2c0000, 0xac0000, 0x040000, 0x840000, 0x240000, 0xa40000}, {0xf00000, 0x700000, 0xd00000, 0x500000, 0xf80000, 0x780000, 0xd80000, 0x580000}, {0x300000, 0xb00000, 0x100000, 0x900000, 0x380000, 0xb80000, 0x180000, 0x980000}, {0xc00000, 0x400000, 0xe00000, 0x600000, 0xc80000, 0x480000, 0xe80000, 0x680000}, {0x000000, 0x800000, 0x200000, 0xa00000, 0x080000, 0x880000, 0x280000, 0xa80000}}; + + /** + * Constructs a new, empty ImageData with the given width, height, + * depth and palette. The data will be initialized to an (all zero) + * array of the appropriate size. + * + * @param width the width of the image + * @param height the height of the image + * @param depth the depth of the image + * @param palette the palette of the image (must not be null) + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_INVALID_ARGUMENT - if the width or height is negative, or if the depth is not + * one of 1, 2, 4, 8, 16, 24 or 32</li> + * <li>ERROR_NULL_ARGUMENT - if the palette is null</li> + * </ul> + */ + public ImageData(int width, int height, int depth, PaletteData palette) { + this(width, height, depth, palette, 4, null, 0, null, null, -1, -1, SWT.IMAGE_UNDEFINED, 0, 0, 0, 0); + } -/** - * Constructs a new, empty ImageData with the given width, height, - * depth and palette. The data will be initialized to an (all zero) - * array of the appropriate size. - * - * @param width the width of the image - * @param height the height of the image - * @param depth the depth of the image - * @param palette the palette of the image (must not be null) - * - * @exception IllegalArgumentException <ul> - * <li>ERROR_INVALID_ARGUMENT - if the width or height is negative, or if the depth is not - * one of 1, 2, 4, 8, 16, 24 or 32</li> - * <li>ERROR_NULL_ARGUMENT - if the palette is null</li> - * </ul> - */ -public ImageData(int width, int height, int depth, PaletteData palette) { - this(width, height, depth, palette, - 4, null, 0, null, - null, -1, -1, SWT.IMAGE_UNDEFINED, - 0, 0, 0, 0); -} + /** + * Constructs a new, empty ImageData with the given width, height, + * depth, palette, scanlinePad and data. + * + * @param width the width of the image + * @param height the height of the image + * @param depth the depth of the image + * @param palette the palette of the image + * @param scanlinePad the padding of each line, in bytes + * @param data the data of the image + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_INVALID_ARGUMENT - if the width or height is negative, or if the depth is not + * one of 1, 2, 4, 8, 16, 24 or 32</li> + * <li>ERROR_NULL_ARGUMENT - if the palette or data is null</li> + * <li>ERROR_CANNOT_BE_ZERO - if the scanlinePad is zero</li> + * </ul> + */ + public ImageData(int width, int height, int depth, PaletteData palette, int scanlinePad, byte[] data) { + this(width, height, depth, palette, scanlinePad, checkData(data), 0, null, null, -1, -1, SWT.IMAGE_UNDEFINED, 0, 0, 0, 0); + } -/** - * Constructs a new, empty ImageData with the given width, height, - * depth, palette, scanlinePad and data. - * - * @param width the width of the image - * @param height the height of the image - * @param depth the depth of the image - * @param palette the palette of the image - * @param scanlinePad the padding of each line, in bytes - * @param data the data of the image - * - * @exception IllegalArgumentException <ul> - * <li>ERROR_INVALID_ARGUMENT - if the width or height is negative, or if the depth is not - * one of 1, 2, 4, 8, 16, 24 or 32</li> - * <li>ERROR_NULL_ARGUMENT - if the palette or data is null</li> - * <li>ERROR_CANNOT_BE_ZERO - if the scanlinePad is zero</li> - * </ul> - */ -public ImageData(int width, int height, int depth, PaletteData palette, int scanlinePad, byte[] data) { - this(width, height, depth, palette, - scanlinePad, checkData(data), 0, null, - null, -1, -1, SWT.IMAGE_UNDEFINED, - 0, 0, 0, 0); -} + /** + * Constructs an <code>ImageData</code> loaded from a file with the + * specified name. Throws an error if an error occurs loading the + * image, or if the image has an unsupported type. + * <p> + * This constructor is provided for convenience when loading a single + * image only. If the file contains multiple images, only the first + * one will be loaded. To load multiple images, use + * <code>ImageLoader.load()</code>. + * </p> + * + * @param filename the name of the file to load the image from (must not be null) + * + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if the file name is null</li> + * </ul> + * @exception RuntimeException <ul> + * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li> + * <li>ERROR_IO if an IO error occurs while reading data</li> + * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li> + * </ul> + */ + public ImageData(String filename) { + ImageData[] data = new ImageLoader().load(filename); + if (data.length < 1) + SWT.error(SWT.ERROR_INVALID_IMAGE); + ImageData i = data[0]; + setAllFields(i.width, i.height, i.depth, i.scanlinePad, i.bytesPerLine, i.data, i.palette, i.transparentPixel, i.maskData, i.maskPad, i.alphaData, i.alpha, i.type, i.x, i.y, i.disposalMethod, i.delayTime); + } -/** - * Constructs an <code>ImageData</code> loaded from a file with the - * specified name. Throws an error if an error occurs loading the - * image, or if the image has an unsupported type. - * <p> - * This constructor is provided for convenience when loading a single - * image only. If the file contains multiple images, only the first - * one will be loaded. To load multiple images, use - * <code>ImageLoader.load()</code>. - * </p> - * - * @param filename the name of the file to load the image from (must not be null) - * - * @exception IllegalArgumentException <ul> - * <li>ERROR_NULL_ARGUMENT - if the file name is null</li> - * </ul> - * @exception RuntimeException <ul> - * <li>ERROR_INVALID_IMAGE - if the image file contains invalid data</li> - * <li>ERROR_IO if an IO error occurs while reading data</li> - * <li>ERROR_UNSUPPORTED_FORMAT - if the image file contains an unrecognized format</li> - * </ul> - */ -public ImageData(String filename) { - ImageData[] data = new ImageLoader().load(filename); - if (data.length < 1) SWT.error(SWT.ERROR_INVALID_IMAGE); - ImageData i = data[0]; - setAllFields( - i.width, - i.height, - i.depth, - i.scanlinePad, - i.bytesPerLine, - i.data, - i.palette, - i.transparentPixel, - i.maskData, - i.maskPad, - i.alphaData, - i.alpha, - i.type, - i.x, - i.y, - i.disposalMethod, - i.delayTime); -} + /** + * Prevents uninitialized instances from being created outside the package. + */ + ImageData() { + //empty constructor + } -/** - * Prevents uninitialized instances from being created outside the package. - */ -ImageData() { - //empty constructor -} + /** + * Constructs an image data by giving values for all non-computable fields. + * <p> + * This method is for internal use, and is not described further. + * </p> + */ + ImageData(int width, int height, int depth, PaletteData palette, int scanlinePad, byte[] data, int maskPad, byte[] maskData, byte[] alphaData, int alpha, int transparentPixel, int type, int x, int y, int disposalMethod, int delayTime) { + + if (palette == null) + SWT.error(SWT.ERROR_NULL_ARGUMENT); + if (!(depth == 1 || depth == 2 || depth == 4 || depth == 8 || depth == 16 || depth == 24 || depth == 32)) { + SWT.error(SWT.ERROR_INVALID_ARGUMENT); + } + if (width <= 0 || height <= 0) { + SWT.error(SWT.ERROR_INVALID_ARGUMENT); + } + if (scanlinePad == 0) + SWT.error(SWT.ERROR_CANNOT_BE_ZERO); -/** - * Constructs an image data by giving values for all non-computable fields. - * <p> - * This method is for internal use, and is not described further. - * </p> - */ -ImageData( - int width, int height, int depth, PaletteData palette, - int scanlinePad, byte[] data, int maskPad, byte[] maskData, - byte[] alphaData, int alpha, int transparentPixel, int type, - int x, int y, int disposalMethod, int delayTime) -{ - - if (palette == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - if (!(depth == 1 || depth == 2 || depth == 4 || depth == 8 - || depth == 16 || depth == 24 || depth == 32)) { - SWT.error(SWT.ERROR_INVALID_ARGUMENT); - } - if (width <= 0 || height <= 0) { - SWT.error(SWT.ERROR_INVALID_ARGUMENT); - } - if (scanlinePad == 0) SWT.error (SWT.ERROR_CANNOT_BE_ZERO); - - int bytesPerLine = (((width * depth + 7) / 8) + (scanlinePad - 1)) - / scanlinePad * scanlinePad; - setAllFields( - width, - height, - depth, - scanlinePad, - bytesPerLine, - data != null ? data : new byte[bytesPerLine * height], - palette, - transparentPixel, - maskData, - maskPad, - alphaData, - alpha, - type, - x, - y, - disposalMethod, - delayTime); -} + int bytesPerLine = (((width * depth + 7) / 8) + (scanlinePad - 1)) / scanlinePad * scanlinePad; + setAllFields(width, height, depth, scanlinePad, bytesPerLine, data != null ? data : new byte[bytesPerLine * height], palette, transparentPixel, maskData, maskPad, alphaData, alpha, type, x, y, disposalMethod, delayTime); + } -/** - * Initializes all fields in the receiver. This method must be called - * by all public constructors to ensure that all fields are initialized - * for a new ImageData object. If a new field is added to the class, - * then it must be added to this method. - * <p> - * This method is for internal use, and is not described further. - * </p> - */ -void setAllFields(int width, int height, int depth, int scanlinePad, - int bytesPerLine, byte[] data, PaletteData palette, int transparentPixel, - byte[] maskData, int maskPad, byte[] alphaData, int alpha, - int type, int x, int y, int disposalMethod, int delayTime) { - - this.width = width; - this.height = height; - this.depth = depth; - this.scanlinePad = scanlinePad; - this.bytesPerLine = bytesPerLine; - this.data = data; - this.palette = palette; - this.transparentPixel = transparentPixel; - this.maskData = maskData; - this.maskPad = maskPad; - this.alphaData = alphaData; - this.alpha = alpha; - this.type = type; - this.x = x; - this.y = y; - this.disposalMethod = disposalMethod; - this.delayTime = delayTime; -} + /** + * Initializes all fields in the receiver. This method must be called + * by all public constructors to ensure that all fields are initialized + * for a new ImageData object. If a new field is added to the class, + * then it must be added to this method. + * <p> + * This method is for internal use, and is not described further. + * </p> + */ + void setAllFields(int width, int height, int depth, int scanlinePad, int bytesPerLine, byte[] data, PaletteData palette, int transparentPixel, byte[] maskData, int maskPad, byte[] alphaData, int alpha, int type, int x, int y, int disposalMethod, int delayTime) { + + this.width = width; + this.height = height; + this.depth = depth; + this.scanlinePad = scanlinePad; + this.bytesPerLine = bytesPerLine; + this.data = data; + this.palette = palette; + this.transparentPixel = transparentPixel; + this.maskData = maskData; + this.maskPad = maskPad; + this.alphaData = alphaData; + this.alpha = alpha; + this.type = type; + this.x = x; + this.y = y; + this.disposalMethod = disposalMethod; + this.delayTime = delayTime; + } -/** - * Invokes internal SWT functionality to create a new instance of - * this class. - * <p> - * <b>IMPORTANT:</b> This method is <em>not</em> part of the public - * API for <code>ImageData</code>. It is marked public only so that it - * can be shared within the packages provided by SWT. It is subject - * to change without notice, and should never be called from - * application code. - * </p> - * <p> - * This method is for internal use, and is not described further. - * </p> - */ -public static ImageData internal_new( - int width, int height, int depth, PaletteData palette, - int scanlinePad, byte[] data, int maskPad, byte[] maskData, - byte[] alphaData, int alpha, int transparentPixel, int type, - int x, int y, int disposalMethod, int delayTime) -{ - return new ImageData( - width, height, depth, palette, scanlinePad, data, maskPad, maskData, - alphaData, alpha, transparentPixel, type, x, y, disposalMethod, delayTime); -} + /** + * Invokes internal SWT functionality to create a new instance of + * this class. + * <p> + * <b>IMPORTANT:</b> This method is <em>not</em> part of the public + * API for <code>ImageData</code>. It is marked public only so that it + * can be shared within the packages provided by SWT. It is subject + * to change without notice, and should never be called from + * application code. + * </p> + * <p> + * This method is for internal use, and is not described further. + * </p> + */ + public static ImageData internal_new(int width, int height, int depth, PaletteData palette, int scanlinePad, byte[] data, int maskPad, byte[] maskData, byte[] alphaData, int alpha, int transparentPixel, int type, int x, int y, int disposalMethod, int delayTime) { + return new ImageData(width, height, depth, palette, scanlinePad, data, maskPad, maskData, alphaData, alpha, transparentPixel, type, x, y, disposalMethod, delayTime); + } -ImageData colorMaskImage(int pixel) { - ImageData mask = new ImageData(width, height, 1, bwPalette(), - 2, null, 0, null, null, -1, -1, SWT.IMAGE_UNDEFINED, - 0, 0, 0, 0); - int[] row = new int[width]; - for (int y = 0; y < height; y++) { - getPixels(0, y, width, row, 0); - for (int i = 0; i < width; i++) { - if (pixel != -1 && row[i] == pixel) { - row[i] = 0; - } else { - row[i] = 1; + ImageData colorMaskImage(int pixel) { + ImageData mask = new ImageData(width, height, 1, bwPalette(), 2, null, 0, null, null, -1, -1, SWT.IMAGE_UNDEFINED, 0, 0, 0, 0); + int[] row = new int[width]; + for (int y = 0; y < height; y++) { + getPixels(0, y, width, row, 0); + for (int i = 0; i < width; i++) { + if (pixel != -1 && row[i] == pixel) { + row[i] = 0; + } else { + row[i] = 1; + } + } + mask.setPixels(0, y, width, row, 0); } + return mask; } - mask.setPixels(0, y, width, row, 0); - } - return mask; -} -static byte[] checkData(byte [] data) { - if (data == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - return data; -} + static byte[] checkData(byte[] data) { + if (data == null) + SWT.error(SWT.ERROR_NULL_ARGUMENT); + return data; + } -/** - * Returns <code>getWidth</code> pixel values starting at offset - * <code>x</code> in scanline <code>y</code> in the receiver's - * data starting at <code>startIndex</code>. - * - * @param x the x position of the first pixel to get - * @param y the y position of the first pixel to get - * @param getWidth the width of the data to get - * @param pixels the buffer in which to put the pixels - * @param startIndex the offset into the byte array to begin storing pixels - * - * @exception IndexOutOfBoundsException if getWidth is too large - * @exception IllegalArgumentException <ul> - * <li>ERROR_NULL_ARGUMENT - if pixels is null</li> - * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> - * <li>ERROR_INVALID_ARGUMENT - if getWidth is negative</li> - * </ul> - * @exception RuntimeException <ul> - * <li>ERROR_UNSUPPORTED_DEPTH - if the depth is not one of 1, 2, 4 or 8 - * (For higher depths, use the int[] version of this method.)</li> - * </ul> - */ -public void getPixels(int x, int y, int getWidth, byte[] pixels, int startIndex) { - if (pixels == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error - -(SWT.ERROR_INVALID_ARGUMENT); - if (getWidth == 0) return; - int index; - int theByte; - int mask = 0; - int n = getWidth; - int i = startIndex; - int srcX = x, srcY = y; - if (depth == 1) { - index = (y * bytesPerLine) + (x >> 3); - theByte = data[index] & 0xFF; - while (n > 0) { - mask = 1 << (7 - (srcX & 0x7)); - if ((theByte & mask) == 0) { - pixels[i] = 0; - } else { - pixels[i] = 1; - } - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - if (n > 0) theByte = data[index] & 0xFF; - srcX = 0; - } else { - if (mask == 1) { - index++; - if (n > 0) theByte = data[index] & 0xFF; + /** + * Returns <code>getWidth</code> pixel values starting at offset + * <code>x</code> in scanline <code>y</code> in the receiver's + * data starting at <code>startIndex</code>. + * + * @param x the x position of the first pixel to get + * @param y the y position of the first pixel to get + * @param getWidth the width of the data to get + * @param pixels the buffer in which to put the pixels + * @param startIndex the offset into the byte array to begin storing pixels + * + * @exception IndexOutOfBoundsException if getWidth is too large + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if pixels is null</li> + * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> + * <li>ERROR_INVALID_ARGUMENT - if getWidth is negative</li> + * </ul> + * @exception RuntimeException <ul> + * <li>ERROR_UNSUPPORTED_DEPTH - if the depth is not one of 1, 2, 4 or 8 + * (For higher depths, use the int[] version of this method.)</li> + * </ul> + */ + public void getPixels(int x, int y, int getWidth, byte[] pixels, int startIndex) { + if (pixels == null) + SWT.error(SWT.ERROR_NULL_ARGUMENT); + if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) + SWT.error + + (SWT.ERROR_INVALID_ARGUMENT); + if (getWidth == 0) + return; + int index; + int theByte; + int mask = 0; + int n = getWidth; + int i = startIndex; + int srcX = x, srcY = y; + if (depth == 1) { + index = (y * bytesPerLine) + (x >> 3); + theByte = data[index] & 0xFF; + while (n > 0) { + mask = 1 << (7 - (srcX & 0x7)); + if ((theByte & mask) == 0) { + pixels[i] = 0; + } else { + pixels[i] = 1; + } + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + if (n > 0) + theByte = data[index] & 0xFF; + srcX = 0; + } else { + if (mask == 1) { + index++; + if (n > 0) + theByte = data[index] & 0xFF; + } + } } + return; } - } - return; - } - if (depth == 2) { - index = (y * bytesPerLine) + (x >> 2); - theByte = data[index] & 0xFF; - int offset; - while (n > 0) { - offset = 3 - (srcX % 4); - mask = 3 << (offset * 2); - pixels[i] = (byte)((theByte & mask) >> (offset * 2)); - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - if (n > 0) theByte = data[index] & 0xFF; - srcX = 0; - } else { - if (offset == 0) { - index++; - theByte = data[index] & 0xFF; + if (depth == 2) { + index = (y * bytesPerLine) + (x >> 2); + theByte = data[index] & 0xFF; + int offset; + while (n > 0) { + offset = 3 - (srcX % 4); + mask = 3 << (offset * 2); + pixels[i] = (byte) ((theByte & mask) >> (offset * 2)); + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + if (n > 0) + theByte = data[index] & 0xFF; + srcX = 0; + } else { + if (offset == 0) { + index++; + theByte = data[index] & 0xFF; + } + } } + return; } - } - return; - } - if (depth == 4) { - index = (y * bytesPerLine) + (x >> 1); - if ((x & 0x1) == 1) { - theByte = data[index] & 0xFF; - pixels[i] = (byte)(theByte & 0x0F); - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index++; - } - } - while (n > 1) { - theByte = data[index] & 0xFF; - pixels[i] = (byte)(theByte >> 4); - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - pixels[i] = (byte)(theByte & 0x0F); - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index++; + if (depth == 4) { + index = (y * bytesPerLine) + (x >> 1); + if ((x & 0x1) == 1) { + theByte = data[index] & 0xFF; + pixels[i] = (byte) (theByte & 0x0F); + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index++; + } + } + while (n > 1) { + theByte = data[index] & 0xFF; + pixels[i] = (byte) (theByte >> 4); + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + pixels[i] = (byte) (theByte & 0x0F); + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index++; + } + } + } + if (n > 0) { + theByte = data[index] & 0xFF; + pixels[i] = (byte) (theByte >> 4); } + return; } - } - if (n > 0) { - theByte = data[index] & 0xFF; - pixels[i] = (byte)(theByte >> 4); - } - return; - } - if (depth == 8) { - index = (y * bytesPerLine) + x; - for (int j = 0; j < getWidth; j++) { - pixels[i] = data[index]; - i++; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index++; + if (depth == 8) { + index = (y * bytesPerLine) + x; + for (int j = 0; j < getWidth; j++) { + pixels[i] = data[index]; + i++; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index++; + } + } + return; } + SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); } - return; - } - SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); -} -/** - * Returns <code>getWidth</code> pixel values starting at offset - * <code>x</code> in scanline <code>y</code> in the receiver's - * data starting at <code>startIndex</code>. - * - * @param x the x position of the first pixel to get - * @param y the y position of the first pixel to get - * @param getWidth the width of the data to get - * @param pixels the buffer in which to put the pixels - * @param startIndex the offset into the buffer to begin storing pixels - * - * @exception IndexOutOfBoundsException if getWidth is too large - * @exception IllegalArgumentException <ul> - * <li>ERROR_NULL_ARGUMENT - if pixels is null</li> - * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> - * <li>ERROR_INVALID_ARGUMENT - if getWidth is negative</li> - * </ul> - * @exception RuntimeException <ul> - * <li>ERROR_UNSUPPORTED_DEPTH - if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li> - * </ul> - */ -public void getPixels(int x, int y, int getWidth, int[] pixels, int startIndex) { - if (pixels == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error - -(SWT.ERROR_INVALID_ARGUMENT); - if (getWidth == 0) return; - int index; - int theByte; - int mask; - int n = getWidth; - int i = startIndex; - int srcX = x, srcY = y; - if (depth == 1) { - index = (y * bytesPerLine) + (x >> 3); - theByte = data[index] & 0xFF; - while (n > 0) { - mask = 1 << (7 - (srcX & 0x7)); - if ((theByte & mask) == 0) { - pixels[i] = 0; - } else { - pixels[i] = 1; - } - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - if (n > 0) theByte = data[index] & 0xFF; - srcX = 0; - } else { - if (mask == 1) { - index++; - if (n > 0) theByte = data[index] & 0xFF; + /** + * Returns <code>getWidth</code> pixel values starting at offset + * <code>x</code> in scanline <code>y</code> in the receiver's + * data starting at <code>startIndex</code>. + * + * @param x the x position of the first pixel to get + * @param y the y position of the first pixel to get + * @param getWidth the width of the data to get + * @param pixels the buffer in which to put the pixels + * @param startIndex the offset into the buffer to begin storing pixels + * + * @exception IndexOutOfBoundsException if getWidth is too large + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if pixels is null</li> + * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> + * <li>ERROR_INVALID_ARGUMENT - if getWidth is negative</li> + * </ul> + * @exception RuntimeException <ul> + * <li>ERROR_UNSUPPORTED_DEPTH - if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li> + * </ul> + */ + public void getPixels(int x, int y, int getWidth, int[] pixels, int startIndex) { + if (pixels == null) + SWT.error(SWT.ERROR_NULL_ARGUMENT); + if (getWidth < 0 || x >= width || y >= height || x < 0 || y < 0) + SWT.error + + (SWT.ERROR_INVALID_ARGUMENT); + if (getWidth == 0) + return; + int index; + int theByte; + int mask; + int n = getWidth; + int i = startIndex; + int srcX = x, srcY = y; + if (depth == 1) { + index = (y * bytesPerLine) + (x >> 3); + theByte = data[index] & 0xFF; + while (n > 0) { + mask = 1 << (7 - (srcX & 0x7)); + if ((theByte & mask) == 0) { + pixels[i] = 0; + } else { + pixels[i] = 1; + } + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + if (n > 0) + theByte = data[index] & 0xFF; + srcX = 0; + } else { + if (mask == 1) { + index++; + if (n > 0) + theByte = data[index] & 0xFF; + } + } } + return; } - } - return; - } - if (depth == 2) { - index = (y * bytesPerLine) + (x >> 2); - theByte = data[index] & 0xFF; - int offset; - while (n > 0) { - offset = 3 - (srcX % 4); - mask = 3 << (offset * 2); - pixels[i] = (byte)((theByte & mask) >> (offset * 2)); - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - if (n > 0) theByte = data[index] & 0xFF; - srcX = 0; - } else { - if (offset == 0) { - index++; + if (depth == 2) { + index = (y * bytesPerLine) + (x >> 2); + theByte = data[index] & 0xFF; + int offset; + while (n > 0) { + offset = 3 - (srcX % 4); + mask = 3 << (offset * 2); + pixels[i] = (byte) ((theByte & mask) >> (offset * 2)); + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + if (n > 0) + theByte = data[index] & 0xFF; + srcX = 0; + } else { + if (offset == 0) { + index++; + theByte = data[index] & 0xFF; + } + } + } + return; + } + if (depth == 4) { + index = (y * bytesPerLine) + (x >> 1); + if ((x & 0x1) == 1) { theByte = data[index] & 0xFF; + pixels[i] = theByte & 0x0F; + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index++; + } } + while (n > 1) { + theByte = data[index] & 0xFF; + pixels[i] = theByte >> 4; + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + pixels[i] = theByte & 0x0F; + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index++; + } + } + } + if (n > 0) { + theByte = data[index] & 0xFF; + pixels[i] = theByte >> 4; + } + return; } - } - return; - } - if (depth == 4) { - index = (y * bytesPerLine) + (x >> 1); - if ((x & 0x1) == 1) { - theByte = data[index] & 0xFF; - pixels[i] = theByte & 0x0F; - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index++; - } - } - while (n > 1) { - theByte = data[index] & 0xFF; - pixels[i] = theByte >> 4; - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - pixels[i] = theByte & 0x0F; - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index++; + if (depth == 8) { + index = (y * bytesPerLine) + x; + for (int j = 0; j < getWidth; j++) { + pixels[i] = data[index] & 0xFF; + i++; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index++; + } + } + return; + } + if (depth == 16) { + index = (y * bytesPerLine) + (x * 2); + for (int j = 0; j < getWidth; j++) { + pixels[i] = ((data[index + 1] & 0xFF) << 8) + (data[index] & 0xFF); + i++; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index += 2; + } + } + return; + } + if (depth == 24) { + index = (y * bytesPerLine) + (x * 3); + for (int j = 0; j < getWidth; j++) { + pixels[i] = ((data[index] & 0xFF) << 16) | ((data[index + 1] & 0xFF) << 8) | (data[index + 2] & 0xFF); + i++; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index += 3; + } + } + return; + } + if (depth == 32) { + index = (y * bytesPerLine) + (x * 4); + i = startIndex; + for (int j = 0; j < getWidth; j++) { + pixels[i] = ((data[index] & 0xFF) << 24) | ((data[index + 1] & 0xFF) << 16) | ((data[index + 2] & 0xFF) << 8) | (data[index + 3] & 0xFF); + i++; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index += 4; + } } + return; } + SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); } - if (n > 0) { - theByte = data[index] & 0xFF; - pixels[i] = theByte >> 4; + + /** + * Returns an array of <code>RGB</code>s which comprise the + * indexed color table of the receiver, or null if the receiver + * has a direct color model. + * + * @return the RGB values for the image or null if direct color + * + * @see IconExe.PaletteData#getRGBs() + */ + public RGB[] getRGBs() { + return palette.getRGBs(); } - return; - } - if (depth == 8) { - index = (y * bytesPerLine) + x; - for (int j = 0; j < getWidth; j++) { - pixels[i] = data[index] & 0xFF; - i++; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index++; + + /** + * Returns an <code>ImageData</code> which specifies the + * transparency mask information for the receiver, or null if the + * receiver has no transparency and is not an icon. + * + * @return the transparency mask or null if none exists + */ + public ImageData getTransparencyMask() { + if (getTransparencyType() == SWT.TRANSPARENCY_MASK) { + return new ImageData(width, height, 1, bwPalette(), maskPad, maskData); } + return colorMaskImage(transparentPixel); } - return; - } - if (depth == 16) { - index = (y * bytesPerLine) + (x * 2); - for (int j = 0; j < getWidth; j++) { - pixels[i] = ((data[index+1] & 0xFF) << 8) + (data[index] & 0xFF); - i++; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index += 2; - } - } - return; - } - if (depth == 24) { - index = (y * bytesPerLine) + (x * 3); - for (int j = 0; j < getWidth; j++) { - pixels[i] = ((data[index] & 0xFF) << 16) | ((data[index+1] & 0xFF) << 8) - | (data[index+2] & 0xFF); - i++; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index += 3; - } - } - return; - } - if (depth == 32) { - index = (y * bytesPerLine) + (x * 4); - i = startIndex; - for (int j = 0; j < getWidth; j++) { - pixels[i] = ((data[index] & 0xFF) << 24) | ((data[index+1] & 0xFF) << 16) - | ((data[index+2] & 0xFF) << 8) | (data[index+3] & 0xFF); - i++; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index += 4; + + /** + * Returns the image transparency type. + * + * @return the receiver's transparency type + */ + public int getTransparencyType() { + if (maskData != null) + return SWT.TRANSPARENCY_MASK; + if (transparentPixel != -1) + return SWT.TRANSPARENCY_PIXEL; + if (alphaData != null) + return SWT.TRANSPARENCY_ALPHA; + return SWT.TRANSPARENCY_NONE; + } + + /** + * Returns the byte order of the receiver. + * + * @return MSB_FIRST or LSB_FIRST + */ + int getByteOrder() { + return depth != 16 ? MSB_FIRST : LSB_FIRST; + } + + /** + * Sets the pixel values starting at offset <code>x</code> in + * scanline <code>y</code> in the receiver's data to the + * values from the array <code>pixels</code> starting at + * <code>startIndex</code>. + * + * @param x the x position of the pixel to set + * @param y the y position of the pixel to set + * @param putWidth the width of the pixels to set + * @param pixels the pixels to set + * @param startIndex the index at which to begin setting + * + * @exception IndexOutOfBoundsException if putWidth is too large + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if pixels is null</li> + * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> + * <li>ERROR_INVALID_ARGUMENT - if putWidth is negative</li> + * </ul> + * @exception RuntimeException <ul> + * <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8 + * (For higher depths, use the int[] version of this method.)</li> + * </ul> + */ + public void setPixels(int x, int y, int putWidth, byte[] pixels, int startIndex) { + if (pixels == null) + SWT.error(SWT.ERROR_NULL_ARGUMENT); + if (putWidth < 0 || x >= width || y >= height || x < 0 || y < 0) + SWT.error(SWT.ERROR_INVALID_ARGUMENT); + if (putWidth == 0) + return; + int index; + int theByte; + int mask; + int n = putWidth; + int i = startIndex; + int srcX = x, srcY = y; + if (depth == 1) { + index = (y * bytesPerLine) + (x >> 3); + while (n > 0) { + mask = 1 << (7 - (srcX & 0x7)); + if ((pixels[i] & 0x1) == 1) { + data[index] = (byte) ((data[index] & 0xFF) | mask); + } else { + data[index] = (byte) ((data[index] & 0xFF) & (mask ^ -1)); + } + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + if (mask == 1) { + index++; + } + } + } + return; + } + if (depth == 2) { + byte[] masks = {(byte) 0xFC, (byte) 0xF3, (byte) 0xCF, (byte) 0x3F}; + index = (y * bytesPerLine) + (x >> 2); + int offset = 3 - (x % 4); + while (n > 0) { + theByte = pixels[i] & 0x3; + data[index] = (byte) ((data[index] & masks[offset]) | (theByte << (offset * 2))); + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + offset = 0; + srcX = 0; + } else { + if (offset == 0) { + index++; + offset = 3; + } else { + offset--; + } + } + } + return; + } + if (depth == 4) { + index = (y * bytesPerLine) + (x >> 1); + boolean high = (x & 0x1) == 0; + while (n > 0) { + theByte = pixels[i] & 0x0F; + if (high) { + data[index] = (byte) ((data[index] & 0x0F) | (theByte << 4)); + } else { + data[index] = (byte) ((data[index] & 0xF0) | theByte); + } + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + high = true; + srcX = 0; + } else { + if (!high) + index++; + high = !high; + } + } + return; + } + if (depth == 8) { + index = (y * bytesPerLine) + x; + for (int j = 0; j < putWidth; j++) { + data[index] = (byte) (pixels[i] & 0xFF); + i++; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index++; + } + } + return; } + SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); } - return; - } - SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); -} -/** - * Returns an array of <code>RGB</code>s which comprise the - * indexed color table of the receiver, or null if the receiver - * has a direct color model. - * - * @return the RGB values for the image or null if direct color - * - * @see IconExe.PaletteData#getRGBs() - */ -public RGB[] getRGBs() { - return palette.getRGBs(); -} + /** + * Sets the pixel values starting at offset <code>x</code> in + * scanline <code>y</code> in the receiver's data to the + * values from the array <code>pixels</code> starting at + * <code>startIndex</code>. + * + * @param x the x position of the pixel to set + * @param y the y position of the pixel to set + * @param putWidth the width of the pixels to set + * @param pixels the pixels to set + * @param startIndex the index at which to begin setting + * + * @exception IndexOutOfBoundsException if putWidth is too large + * @exception IllegalArgumentException <ul> + * <li>ERROR_NULL_ARGUMENT - if pixels is null</li> + * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> + * <li>ERROR_INVALID_ARGUMENT - if putWidth is negative</li> + * </ul> + * @exception RuntimeException <ul> + * <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li> + * </ul> + */ + public void setPixels(int x, int y, int putWidth, int[] pixels, int startIndex) { + if (pixels == null) + SWT.error(SWT.ERROR_NULL_ARGUMENT); + if (putWidth < 0 || x >= width || y >= height || x < 0 || y < 0) + SWT.error(SWT.ERROR_INVALID_ARGUMENT); + if (putWidth == 0) + return; + int index; + int theByte; + int mask; + int n = putWidth; + int i = startIndex; + int pixel; + int srcX = x, srcY = y; + if (depth == 1) { + index = (y * bytesPerLine) + (x >> 3); + while (n > 0) { + mask = 1 << (7 - (srcX & 0x7)); + if ((pixels[i] & 0x1) == 1) { + data[index] = (byte) ((data[index] & 0xFF) | mask); + } else { + data[index] = (byte) ((data[index] & 0xFF) & (mask ^ -1)); + } + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + if (mask == 1) { + index++; + } + } + } + return; + } + if (depth == 2) { + byte[] masks = {(byte) 0xFC, (byte) 0xF3, (byte) 0xCF, (byte) 0x3F}; + index = (y * bytesPerLine) + (x >> 2); + int offset = 3 - (x % 4); + while (n > 0) { + theByte = pixels[i] & 0x3; + data[index] = (byte) ((data[index] & masks[offset]) | (theByte << (offset * 2))); + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + offset = 3; + srcX = 0; + } else { + if (offset == 0) { + index++; + offset = 3; + } else { + offset--; + } + } + } + return; + } + if (depth == 4) { + index = (y * bytesPerLine) + (x >> 1); + boolean high = (x & 0x1) == 0; + while (n > 0) { + theByte = pixels[i] & 0x0F; + if (high) { + data[index] = (byte) ((data[index] & 0x0F) | (theByte << 4)); + } else { + data[index] = (byte) ((data[index] & 0xF0) | theByte); + } + i++; + n--; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + high = true; + srcX = 0; + } else { + if (!high) + index++; + high = !high; + } + } + return; + } + if (depth == 8) { + index = (y * bytesPerLine) + x; + for (int j = 0; j < putWidth; j++) { + data[index] = (byte) (pixels[i] & 0xFF); + i++; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index++; + } + } + return; -/** - * Returns an <code>ImageData</code> which specifies the - * transparency mask information for the receiver, or null if the - * receiver has no transparency and is not an icon. - * - * @return the transparency mask or null if none exists - */ -public ImageData getTransparencyMask() { - if (getTransparencyType() == SWT.TRANSPARENCY_MASK) { - return new ImageData(width, height, 1, bwPalette(), maskPad, maskData); - } - return colorMaskImage(transparentPixel); -} + } + if (depth == 16) { + index = (y * bytesPerLine) + (x * 2); + for (int j = 0; j < putWidth; j++) { + pixel = pixels[i]; + data[index] = (byte) (pixel & 0xFF); + data[index + 1] = (byte) ((pixel >> 8) & 0xFF); + i++; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index += 2; + } + } + return; + } + if (depth == 24) { + index = (y * bytesPerLine) + (x * 3); + for (int j = 0; j < putWidth; j++) { + pixel = pixels[i]; + data[index] = (byte) ((pixel >> 16) & 0xFF); + data[index + 1] = (byte) ((pixel >> 8) & 0xFF); + data[index + 2] = (byte) (pixel & 0xFF); + i++; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index += 3; + } + } + return; + } + if (depth == 32) { + index = (y * bytesPerLine) + (x * 4); + for (int j = 0; j < putWidth; j++) { + pixel = pixels[i]; + data[index] = (byte) ((pixel >> 24) & 0xFF); + data[index + 1] = (byte) ((pixel >> 16) & 0xFF); + data[index + 2] = (byte) ((pixel >> 8) & 0xFF); + data[index + 3] = (byte) (pixel & 0xFF); + i++; + srcX++; + if (srcX >= width) { + srcY++; + index = srcY * bytesPerLine; + srcX = 0; + } else { + index += 4; + } + } + return; + } + SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); + } -/** - * Returns the image transparency type. - * - * @return the receiver's transparency type - */ -public int getTransparencyType() { - if (maskData != null) return SWT.TRANSPARENCY_MASK; - if (transparentPixel != -1) return SWT.TRANSPARENCY_PIXEL; - if (alphaData != null) return SWT.TRANSPARENCY_ALPHA; - return SWT.TRANSPARENCY_NONE; -} + /** + * Returns a palette with 2 colors: black & white. + */ + static PaletteData bwPalette() { + return new PaletteData(new RGB[] {new RGB(0, 0, 0), new RGB(255, 255, 255)}); + } -/** - * Returns the byte order of the receiver. - * - * @return MSB_FIRST or LSB_FIRST - */ -int getByteOrder() { - return depth != 16 ? MSB_FIRST : LSB_FIRST; -} + /** + * Gets the offset of the most significant bit for + * the given mask. + */ + static int getMSBOffset(int mask) { + for (int i = 31; i >= 0; i--) { + if (((mask >> i) & 0x1) != 0) + return i + 1; + } + return 0; + } -/** - * Sets the pixel values starting at offset <code>x</code> in - * scanline <code>y</code> in the receiver's data to the - * values from the array <code>pixels</code> starting at - * <code>startIndex</code>. - * - * @param x the x position of the pixel to set - * @param y the y position of the pixel to set - * @param putWidth the width of the pixels to set - * @param pixels the pixels to set - * @param startIndex the index at which to begin setting - * - * @exception IndexOutOfBoundsException if putWidth is too large - * @exception IllegalArgumentException <ul> - * <li>ERROR_NULL_ARGUMENT - if pixels is null</li> - * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> - * <li>ERROR_INVALID_ARGUMENT - if putWidth is negative</li> - * </ul> - * @exception RuntimeException <ul> - * <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8 - * (For higher depths, use the int[] version of this method.)</li> - * </ul> - */ -public void setPixels(int x, int y, int putWidth, byte[] pixels, int startIndex) { - if (pixels == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - if (putWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); - if (putWidth == 0) return; - int index; - int theByte; - int mask; - int n = putWidth; - int i = startIndex; - int srcX = x, srcY = y; - if (depth == 1) { - index = (y * bytesPerLine) + (x >> 3); - while (n > 0) { - mask = 1 << (7 - (srcX & 0x7)); - if ((pixels[i] & 0x1) == 1) { - data[index] = (byte)((data[index] & 0xFF) | mask); - } else { - data[index] = (byte)((data[index] & 0xFF) & (mask ^ -1)); - } - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - if (mask == 1) { - index++; + /** + * Finds the closest match. + */ + static int closestMatch(int depth, byte red, byte green, byte blue, int redMask, int greenMask, int blueMask, byte[] reds, byte[] greens, byte[] blues) { + if (depth > 8) { + int rshift = 32 - getMSBOffset(redMask); + int gshift = 32 - getMSBOffset(greenMask); + int bshift = 32 - getMSBOffset(blueMask); + return (((red << 24) >>> rshift) & redMask) | (((green << 24) >>> gshift) & greenMask) | (((blue << 24) >>> bshift) & blueMask); + } + int r, g, b; + int minDistance = 0x7fffffff; + int nearestPixel = 0; + int n = reds.length; + for (int j = 0; j < n; j++) { + r = (reds[j] & 0xFF) - (red & 0xFF); + g = (greens[j] & 0xFF) - (green & 0xFF); + b = (blues[j] & 0xFF) - (blue & 0xFF); + int distance = r * r + g * g + b * b; + if (distance < minDistance) { + nearestPixel = j; + if (distance == 0) + break; + minDistance = distance; } } + return nearestPixel; } - return; - } - if (depth == 2) { - byte [] masks = { (byte)0xFC, (byte)0xF3, (byte)0xCF, (byte)0x3F }; - index = (y * bytesPerLine) + (x >> 2); - int offset = 3 - (x % 4); - while (n > 0) { - theByte = pixels[i] & 0x3; - data[index] = (byte)((data[index] & masks[offset]) | (theByte << (offset * 2))); - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - offset = 0; - srcX = 0; - } else { - if (offset == 0) { - index++; - offset = 3; - } else { - offset--; + + static final ImageData convertMask(ImageData mask) { + if (mask.depth == 1) + return mask; + PaletteData palette = new PaletteData(new RGB[] {new RGB(0, 0, 0), new RGB(255, 255, 255)}); + ImageData newMask = new ImageData(mask.width, mask.height, 1, palette); + /* Find index of black in mask palette */ + int blackIndex = 0; + RGB[] rgbs = mask.getRGBs(); + if (rgbs != null) { + while (blackIndex < rgbs.length) { + if (rgbs[blackIndex].equals(palette.colors[0])) + break; + blackIndex++; + } + } + int[] pixels = new int[mask.width]; + for (int y = 0; y < mask.height; y++) { + mask.getPixels(0, y, mask.width, pixels, 0); + for (int i = 0; i < pixels.length; i++) { + if (pixels[i] == blackIndex) { + pixels[i] = 0; + } else { + pixels[i] = 1; + } } + newMask.setPixels(0, y, mask.width, pixels, 0); } + return newMask; } - return; - } - if (depth == 4) { - index = (y * bytesPerLine) + (x >> 1); - boolean high = (x & 0x1) == 0; - while (n > 0) { - theByte = pixels[i] & 0x0F; - if (high) { - data[index] = (byte)((data[index] & 0x0F) | (theByte << 4)); - } else { - data[index] = (byte)((data[index] & 0xF0) | theByte); - } - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - high = true; - srcX = 0; - } else { - if (!high) index++; - high = !high; - } - } - return; - } - if (depth == 8) { - index = (y * bytesPerLine) + x; - for (int j = 0; j < putWidth; j++) { - data[index] = (byte)(pixels[i] & 0xFF); - i++; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index++; + + static final byte[] convertPad(byte[] data, int width, int height, int depth, int pad, int newPad) { + if (pad == newPad) + return data; + int stride = (width * depth + 7) / 8; + int bpl = (stride + (pad - 1)) / pad * pad; + int newBpl = (stride + (newPad - 1)) / newPad * newPad; + byte[] newData = new byte[height * newBpl]; + int srcIndex = 0, destIndex = 0; + for (int y = 0; y < height; y++) { + System.arraycopy(data, srcIndex, newData, destIndex, stride); + srcIndex += bpl; + destIndex += newBpl; } + return newData; } - return; - } - SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); -} -/** - * Sets the pixel values starting at offset <code>x</code> in - * scanline <code>y</code> in the receiver's data to the - * values from the array <code>pixels</code> starting at - * <code>startIndex</code>. - * - * @param x the x position of the pixel to set - * @param y the y position of the pixel to set - * @param putWidth the width of the pixels to set - * @param pixels the pixels to set - * @param startIndex the index at which to begin setting - * - * @exception IndexOutOfBoundsException if putWidth is too large - * @exception IllegalArgumentException <ul> - * <li>ERROR_NULL_ARGUMENT - if pixels is null</li> - * <li>ERROR_INVALID_ARGUMENT - if x or y is out of bounds</li> - * <li>ERROR_INVALID_ARGUMENT - if putWidth is negative</li> - * </ul> - * @exception RuntimeException <ul> - * <li>ERROR_UNSUPPORTED_DEPTH if the depth is not one of 1, 2, 4, 8, 16, 24 or 32</li> - * </ul> - */ -public void setPixels(int x, int y, int putWidth, int[] pixels, int startIndex) { - if (pixels == null) SWT.error(SWT.ERROR_NULL_ARGUMENT); - if (putWidth < 0 || x >= width || y >= height || x < 0 || y < 0) SWT.error(SWT.ERROR_INVALID_ARGUMENT); - if (putWidth == 0) return; - int index; - int theByte; - int mask; - int n = putWidth; - int i = startIndex; - int pixel; - int srcX = x, srcY = y; - if (depth == 1) { - index = (y * bytesPerLine) + (x >> 3); - while (n > 0) { - mask = 1 << (7 - (srcX & 0x7)); - if ((pixels[i] & 0x1) == 1) { - data[index] = (byte)((data[index] & 0xFF) | mask); - } else { - data[index] = (byte)((data[index] & 0xFF) & (mask ^ -1)); - } - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - if (mask == 1) { - index++; - } + /** + * Blit operation bits to be OR'ed together to specify the desired operation. + */ + static final int BLIT_SRC = 1, // copy source directly, else applies logic operations + BLIT_ALPHA = 2, // enable alpha blending + BLIT_DITHER = 4; // enable dithering in low color modes + + /** + * Alpha mode, values 0 - 255 specify global alpha level + */ + static final int ALPHA_OPAQUE = 255, // Fully opaque (ignores any alpha data) + ALPHA_TRANSPARENT = 0, // Fully transparent (ignores any alpha data) + ALPHA_CHANNEL_SEPARATE = -1, // Use alpha channel from separate alphaData + ALPHA_CHANNEL_SOURCE = -2, // Use alpha channel embedded in sourceData + ALPHA_MASK_UNPACKED = -3, // Use transparency mask formed by bytes in alphaData (non-zero is opaque) + ALPHA_MASK_PACKED = -4, // Use transparency mask formed by packed bits in alphaData + ALPHA_MASK_INDEX = -5, // Consider source palette indices transparent if in alphaData array + ALPHA_MASK_RGB = -6; // Consider source RGBs transparent if in RGB888 format alphaData array + + /** + * Byte and bit order constants. + */ + static final int LSB_FIRST = 0; + static final int MSB_FIRST = 1; + + /** + * Data types (internal) + */ + /* + private static final int + // direct / true color formats with arbitrary masks & shifts + TYPE_GENERIC_8 = 0, + TYPE_GENERIC_16_MSB = 1, + TYPE_GENERIC_16_LSB = 2, + TYPE_GENERIC_24 = 3, + TYPE_GENERIC_32_MSB = 4, + TYPE_GENERIC_32_LSB = 5, + // palette indexed color formats + TYPE_INDEX_8 = 6, + TYPE_INDEX_4 = 7, + TYPE_INDEX_2 = 8, + TYPE_INDEX_1_MSB = 9, + TYPE_INDEX_1_LSB = 10; + */ + /** + * Computes the required channel shift from a mask. + */ + static int getChannelShift(int mask) { + if (mask == 0) + return 0; + int i; + for (i = 0; ((mask & 1) == 0) && (i < 32); ++i) { + mask >>>= 1; + } + return i; + } + + /** + * Computes the required channel width (depth) from a mask. + */ + static int getChannelWidth(int mask, int shift) { + if (mask == 0) + return 0; + int i; + mask >>>= shift; + for (i = shift; ((mask & 1) != 0) && (i < 32); ++i) { + mask >>>= 1; } + return i - shift; + } + + /** + * Extracts a field from packed RGB data given a mask for that field. + */ + static byte getChannelField(int data, int mask) { + final int shift = getChannelShift(mask); + return ANY_TO_EIGHT[getChannelWidth(mask, shift)][(data & mask) >>> shift]; } - return; - } - if (depth == 2) { - byte [] masks = { (byte)0xFC, (byte)0xF3, (byte)0xCF, (byte)0x3F }; - index = (y * bytesPerLine) + (x >> 2); - int offset = 3 - (x % 4); - while (n > 0) { - theByte = pixels[i] & 0x3; - data[index] = (byte)((data[index] & masks[offset]) | (theByte << (offset * 2))); - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - offset = 3; - srcX = 0; + + /* + * Fill in dithered gradated values for a color channel + */ + static final void buildDitheredGradientChannel(int from, int to, int steps, int bandWidth, int bandHeight, boolean vertical, byte[] bitmapData, int dp, int bytesPerLine, int bits) { + final int mask = 0xff00 >>> bits; + int val = from << 16; + final int inc = ((to << 16) - val) / steps + 1; + if (vertical) { + for (int dy = 0; dy < bandHeight; ++dy, dp += bytesPerLine) { + for (int dx = 0, dptr = dp; dx < bandWidth; ++dx, dptr += 4) { + final int thresh = DITHER_MATRIX[dy & 7][dx] >>> bits; + int temp = val + thresh; + if (temp > 0xffffff) + bitmapData[dptr] = -1; + else + bitmapData[dptr] = (byte) ((temp >>> 16) & mask); + } + val += inc; + } } else { - if (offset == 0) { - index++; - offset = 3; - } else { - offset--; + for (int dx = 0; dx < bandWidth; ++dx, dp += 4) { + for (int dy = 0, dptr = dp; dy < bandHeight; ++dy, dptr += bytesPerLine) { + final int thresh = DITHER_MATRIX[dy][dx & 7] >>> bits; + int temp = val + thresh; + if (temp > 0xffffff) + bitmapData[dptr] = -1; + else + bitmapData[dptr] = (byte) ((temp >>> 16) & mask); + } + val += inc; } } } - return; } - if (depth == 4) { - index = (y * bytesPerLine) + (x >> 1); - boolean high = (x & 0x1) == 0; - while (n > 0) { - theByte = pixels[i] & 0x0F; - if (high) { - data[index] = (byte)((data[index] & 0x0F) | (theByte << 4)); - } else { - data[index] = (byte)((data[index] & 0xF0) | theByte); - } - i++; - n--; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - high = true; - srcX = 0; - } else { - if (!high) index++; - high = !high; - } - } - return; - } - if (depth == 8) { - index = (y * bytesPerLine) + x; - for (int j = 0; j < putWidth; j++) { - data[index] = (byte)(pixels[i] & 0xFF); - i++; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index++; - } + + static class LEDataInputStream extends InputStream { + int position; + InputStream in; + + /** + * The byte array containing the bytes to read. + */ + protected byte[] buf; + + /** + * The current position within the byte array <code>buf</code>. A value + * equal to buf.length indicates no bytes available. A value of + * 0 indicates the buffer is full. + */ + protected int pos; + + public LEDataInputStream(InputStream input) { + this(input, 512); } - return; - } - if (depth == 16) { - index = (y * bytesPerLine) + (x * 2); - for (int j = 0; j < putWidth; j++) { - pixel = pixels[i]; - data[index] = (byte)(pixel & 0xFF); - data[index + 1] = (byte)((pixel >> 8) & 0xFF); - i++; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index += 2; - } - } - return; - } - if (depth == 24) { - index = (y * bytesPerLine) + (x * 3); - for (int j = 0; j < putWidth; j++) { - pixel = pixels[i]; - data[index] = (byte)((pixel >> 16) & 0xFF); - data[index + 1] = (byte)((pixel >> 8) & 0xFF); - data[index + 2] = (byte)(pixel & 0xFF); - i++; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index += 3; - } - } - return; - } - if (depth == 32) { - index = (y * bytesPerLine) + (x * 4); - for (int j = 0; j < putWidth; j++) { - pixel = pixels[i]; - data[index] = (byte)((pixel >> 24) & 0xFF); - data[index + 1] = (byte)((pixel >> 16) & 0xFF); - data[index + 2] = (byte)((pixel >> 8) & 0xFF); - data[index + 3] = (byte)(pixel & 0xFF); - i++; - srcX++; - if (srcX >= width) { - srcY++; - index = srcY * bytesPerLine; - srcX = 0; - } else { - index += 4; + public LEDataInputStream(InputStream input, int bufferSize) { + this.in = input; + if (bufferSize > 0) { + buf = new byte[bufferSize]; + pos = bufferSize; + } else + throw new IllegalArgumentException(); + } + + public void close() throws IOException { + buf = null; + if (in != null) { + in.close(); + in = null; } } - return; - } - SWT.error(SWT.ERROR_UNSUPPORTED_DEPTH); -} -/** - * Returns a palette with 2 colors: black & white. - */ -static PaletteData bwPalette() { - return new PaletteData(new RGB[] {new RGB(0, 0, 0), new RGB(255, 255, 255)}); -} + /** + * Answer how many bytes were read. + */ + public int getPosition() { + return position; + } -/** - * Gets the offset of the most significant bit for - * the given mask. - */ -static int getMSBOffset(int mask) { - for (int i = 31; i >= 0; i--) { - if (((mask >> i) & 0x1) != 0) return i + 1; - } - return 0; -} + /** + * Answers how many bytes are available for reading without blocking + */ + public int available() throws IOException { + if (buf == null) + throw new IOException(); + return (buf.length - pos) + in.available(); + } -/** - * Finds the closest match. - */ -static int closestMatch(int depth, byte red, byte green, byte blue, int redMask, int greenMask, int blueMask, byte[] reds, byte[] greens, byte[] blues) { - if (depth > 8) { - int rshift = 32 - getMSBOffset(redMask); - int gshift = 32 - getMSBOffset(greenMask); - int bshift = 32 - getMSBOffset(blueMask); - return (((red << 24) >>> rshift) & redMask) | - (((green << 24) >>> gshift) & greenMask) | - (((blue << 24) >>> bshift) & blueMask); - } - int r, g, b; - int minDistance = 0x7fffffff; - int nearestPixel = 0; - int n = reds.length; - for (int j = 0; j < n; j++) { - r = (reds[j] & 0xFF) - (red & 0xFF); - g = (greens[j] & 0xFF) - (green & 0xFF); - b = (blues[j] & 0xFF) - (blue & 0xFF); - int distance = r*r + g*g + b*b; - if (distance < minDistance) { - nearestPixel = j; - if (distance == 0) break; - minDistance = distance; - } - } - return nearestPixel; -} + /** + * Answer the next byte of the input stream. + */ + public int read() throws IOException { + if (buf == null) + throw new IOException(); + position++; + if (pos < buf.length) + return (buf[pos++] & 0xFF); + return in.read(); + } -static final ImageData convertMask(ImageData mask) { - if (mask.depth == 1) return mask; - PaletteData palette = new PaletteData(new RGB[] {new RGB(0, 0, 0), new RGB(255,255,255)}); - ImageData newMask = new ImageData(mask.width, mask.height, 1, palette); - /* Find index of black in mask palette */ - int blackIndex = 0; - RGB[] rgbs = mask.getRGBs(); - if (rgbs != null) { - while (blackIndex < rgbs.length) { - if (rgbs[blackIndex].equals(palette.colors[0])) break; - blackIndex++; - } - } - int[] pixels = new int[mask.width]; - for (int y = 0; y < mask.height; y++) { - mask.getPixels(0, y, mask.width, pixels, 0); - for (int i = 0; i < pixels.length; i++) { - if (pixels[i] == blackIndex) { - pixels[i] = 0; - } else { - pixels[i] = 1; + /** + * Don't imitate the JDK behaviour of reading a random number + * of bytes when you can actually read them all. + */ + public int read(byte b[], int off, int len) throws IOException { + int result; + int left = len; + result = readData(b, off, len); + while (true) { + if (result == -1) + return -1; + position += result; + if (result == left) + return len; + left -= result; + off += result; + result = readData(b, off, left); } } - newMask.setPixels(0, y, mask.width, pixels, 0); - } - return newMask; -} -static final byte[] convertPad(byte[] data, int width, int height, int depth, int pad, int newPad) { - if (pad == newPad) return data; - int stride = (width * depth + 7) / 8; - int bpl = (stride + (pad - 1)) / pad * pad; - int newBpl = (stride + (newPad - 1)) / newPad * newPad; - byte[] newData = new byte[height * newBpl]; - int srcIndex = 0, destIndex = 0; - for (int y = 0; y < height; y++) { - System.arraycopy(data, srcIndex, newData, destIndex, stride); - srcIndex += bpl; - destIndex += newBpl; - } - return newData; -} + /** + * Reads at most <code>length</code> bytes from this LEDataInputStream and + * stores them in byte array <code>buffer</code> starting at <code>offset</code>. + * <p> + * Answer the number of bytes actually read or -1 if no bytes were read and + * end of stream was encountered. This implementation reads bytes from + * the pushback buffer first, then the target stream if more bytes are required + * to satisfy <code>count</code>. + * </p> + * @param buffer the byte array in which to store the read bytes. + * @param offset the offset in <code>buffer</code> to store the read bytes. + * @param length the maximum number of bytes to store in <code>buffer</code>. + * + * @return int the number of bytes actually read or -1 if end of stream. + * + * @exception java.io.IOException if an IOException occurs. + */ + private int readData(byte[] buffer, int offset, int length) throws IOException { + if (buf == null) + throw new IOException(); + if (offset < 0 || offset > buffer.length || length < 0 || (length > buffer.length - offset)) { + throw new ArrayIndexOutOfBoundsException(); + } -/** - * Blit operation bits to be OR'ed together to specify the desired operation. - */ -static final int - BLIT_SRC = 1, // copy source directly, else applies logic operations - BLIT_ALPHA = 2, // enable alpha blending - BLIT_DITHER = 4; // enable dithering in low color modes + int cacheCopied = 0; + int newOffset = offset; -/** - * Alpha mode, values 0 - 255 specify global alpha level - */ -static final int - ALPHA_OPAQUE = 255, // Fully opaque (ignores any alpha data) - ALPHA_TRANSPARENT = 0, // Fully transparent (ignores any alpha data) - ALPHA_CHANNEL_SEPARATE = -1, // Use alpha channel from separate alphaData - ALPHA_CHANNEL_SOURCE = -2, // Use alpha channel embedded in sourceData - ALPHA_MASK_UNPACKED = -3, // Use transparency mask formed by bytes in alphaData (non-zero is opaque) - ALPHA_MASK_PACKED = -4, // Use transparency mask formed by packed bits in alphaData - ALPHA_MASK_INDEX = -5, // Consider source palette indices transparent if in alphaData array - ALPHA_MASK_RGB = -6; // Consider source RGBs transparent if in RGB888 format alphaData array + // Are there pushback bytes available? + int available = buf.length - pos; + if (available > 0) { + cacheCopied = (available >= length) ? length : available; + System.arraycopy(buf, pos, buffer, newOffset, cacheCopied); + newOffset += cacheCopied; + pos += cacheCopied; + } -/** - * Byte and bit order constants. - */ -static final int LSB_FIRST = 0; -static final int MSB_FIRST = 1; + // Have we copied enough? + if (cacheCopied == length) + return length; -/** - * Data types (internal) - */ -/* -private static final int - // direct / true color formats with arbitrary masks & shifts - TYPE_GENERIC_8 = 0, - TYPE_GENERIC_16_MSB = 1, - TYPE_GENERIC_16_LSB = 2, - TYPE_GENERIC_24 = 3, - TYPE_GENERIC_32_MSB = 4, - TYPE_GENERIC_32_LSB = 5, - // palette indexed color formats - TYPE_INDEX_8 = 6, - TYPE_INDEX_4 = 7, - TYPE_INDEX_2 = 8, - TYPE_INDEX_1_MSB = 9, - TYPE_INDEX_1_LSB = 10; -*/ -/** - * Computes the required channel shift from a mask. - */ -static int getChannelShift(int mask) { - if (mask == 0) return 0; - int i; - for (i = 0; ((mask & 1) == 0) && (i < 32); ++i) { - mask >>>= 1; - } - return i; -} + int inCopied = in.read(buffer, newOffset, length - cacheCopied); -/** - * Computes the required channel width (depth) from a mask. - */ -static int getChannelWidth(int mask, int shift) { - if (mask == 0) return 0; - int i; - mask >>>= shift; - for (i = shift; ((mask & 1) != 0) && (i < 32); ++i) { - mask >>>= 1; - } - return i - shift; -} + if (inCopied > 0) + return inCopied + cacheCopied; + if (cacheCopied == 0) + return inCopied; + return cacheCopied; + } -/** - * Extracts a field from packed RGB data given a mask for that field. - */ -static byte getChannelField(int data, int mask) { - final int shift = getChannelShift(mask); - return ANY_TO_EIGHT[getChannelWidth(mask, shift)][(data & mask) >>> shift]; -} + /** + * Answer an integer comprised of the next + * four bytes of the input stream. + */ + public int readInt() throws IOException { + byte[] buf = new byte[4]; + read(buf); + return ((((((buf[3] & 0xFF) << 8) | (buf[2] & 0xFF)) << 8) | (buf[1] & 0xFF)) << 8) | (buf[0] & 0xFF); + } -/* - * Fill in dithered gradated values for a color channel - */ -static final void buildDitheredGradientChannel(int from, int to, int steps, - int bandWidth, int bandHeight, boolean vertical, - byte[] bitmapData, int dp, int bytesPerLine, int bits) { - final int mask = 0xff00 >>> bits; - int val = from << 16; - final int inc = ((to << 16) - val) / steps + 1; - if (vertical) { - for (int dy = 0; dy < bandHeight; ++dy, dp += bytesPerLine) { - for (int dx = 0, dptr = dp; dx < bandWidth; ++dx, dptr += 4) { - final int thresh = DITHER_MATRIX[dy & 7][dx] >>> bits; - int temp = val + thresh; - if (temp > 0xffffff) bitmapData[dptr] = -1; - else bitmapData[dptr] = (byte)((temp >>> 16) & mask); - } - val += inc; - } - } else { - for (int dx = 0; dx < bandWidth; ++dx, dp += 4) { - for (int dy = 0, dptr = dp; dy < bandHeight; ++dy, dptr += bytesPerLine) { - final int thresh = DITHER_MATRIX[dy][dx & 7] >>> bits; - int temp = val + thresh; - if (temp > 0xffffff) bitmapData[dptr] = -1; - else bitmapData[dptr] = (byte)((temp >>> 16) & mask); - } - val += inc; + /** + * Answer a short comprised of the next + * two bytes of the input stream. + */ + public short readShort() throws IOException { + byte[] buf = new byte[2]; + read(buf); + return (short) (((buf[1] & 0xFF) << 8) | (buf[0] & 0xFF)); + } + + /** + * Push back the entire content of the given buffer <code>b</code>. + * <p> + * The bytes are pushed so that they would be read back b[0], b[1], etc. + * If the push back buffer cannot handle the bytes copied from <code>b</code>, + * an IOException will be thrown and no byte will be pushed back. + * </p> + * + * @param b the byte array containing bytes to push back into the stream + * + * @exception java.io.IOException if the pushback buffer is too small + */ + public void unread(byte[] b) throws IOException { + int length = b.length; + if (length > pos) + throw new IOException(); + position -= length; + pos -= length; + System.arraycopy(b, 0, buf, pos, length); } } -} -} -static class LEDataInputStream extends InputStream { - int position; - InputStream in; + public static abstract class FileFormat { + LEDataInputStream inputStream; + ImageLoader loader; + int compression; - /** - * The byte array containing the bytes to read. - */ - protected byte[] buf; - - /** - * The current position within the byte array <code>buf</code>. A value - * equal to buf.length indicates no bytes available. A value of - * 0 indicates the buffer is full. - */ - protected int pos; - - - public LEDataInputStream(InputStream input) { - this(input, 512); - } - - public LEDataInputStream(InputStream input, int bufferSize) { - this.in = input; - if (bufferSize > 0) { - buf = new byte[bufferSize]; - pos = bufferSize; - } - else throw new IllegalArgumentException(); - } - - public void close() throws IOException { - buf = null; - if (in != null) { - in.close(); - in = null; - } - } - - /** - * Answer how many bytes were read. - */ - public int getPosition() { - return position; - } - - /** - * Answers how many bytes are available for reading without blocking - */ - public int available() throws IOException { - if (buf == null) throw new IOException(); - return (buf.length - pos) + in.available(); - } - - /** - * Answer the next byte of the input stream. - */ - public int read() throws IOException { - if (buf == null) throw new IOException(); - position++; - if (pos < buf.length) return (buf[pos++] & 0xFF); - return in.read(); - } - - /** - * Don't imitate the JDK behaviour of reading a random number - * of bytes when you can actually read them all. - */ - public int read(byte b[], int off, int len) throws IOException { - int result; - int left = len; - result = readData(b, off, len); - while (true) { - if (result == -1) return -1; - position += result; - if (result == left) return len; - left -= result; - off += result; - result = readData(b, off, left); - } - } - - /** - * Reads at most <code>length</code> bytes from this LEDataInputStream and - * stores them in byte array <code>buffer</code> starting at <code>offset</code>. - * <p> - * Answer the number of bytes actually read or -1 if no bytes were read and - * end of stream was encountered. This implementation reads bytes from - * the pushback buffer first, then the target stream if more bytes are required - * to satisfy <code>count</code>. - * </p> - * @param buffer the byte array in which to store the read bytes. - * @param offset the offset in <code>buffer</code> to store the read bytes. - * @param length the maximum number of bytes to store in <code>buffer</code>. - * - * @return int the number of bytes actually read or -1 if end of stream. - * - * @exception java.io.IOException if an IOException occurs. - */ - private int readData(byte[] buffer, int offset, int length) throws IOException { - if (buf == null) throw new IOException(); - if (offset < 0 || offset > buffer.length || - length < 0 || (length > buffer.length - offset)) { - throw new ArrayIndexOutOfBoundsException(); - } - - int cacheCopied = 0; - int newOffset = offset; - - // Are there pushback bytes available? - int available = buf.length - pos; - if (available > 0) { - cacheCopied = (available >= length) ? length : available; - System.arraycopy(buf, pos, buffer, newOffset, cacheCopied); - newOffset += cacheCopied; - pos += cacheCopied; - } - - // Have we copied enough? - if (cacheCopied == length) return length; - - int inCopied = in.read(buffer, newOffset, length - cacheCopied); - - if (inCopied > 0) return inCopied + cacheCopied; - if (cacheCopied == 0) return inCopied; - return cacheCopied; - } - - /** - * Answer an integer comprised of the next - * four bytes of the input stream. - */ - public int readInt() throws IOException { - byte[] buf = new byte[4]; - read(buf); - return ((((((buf[3] & 0xFF) << 8) | - (buf[2] & 0xFF)) << 8) | - (buf[1] & 0xFF)) << 8) | - (buf[0] & 0xFF); - } - - /** - * Answer a short comprised of the next - * two bytes of the input stream. - */ - public short readShort() throws IOException { - byte[] buf = new byte[2]; - read(buf); - return (short)(((buf[1] & 0xFF) << 8) | (buf[0] & 0xFF)); - } - - /** - * Push back the entire content of the given buffer <code>b</code>. - * <p> - * The bytes are pushed so that they would be read back b[0], b[1], etc. - * If the push back buffer cannot handle the bytes copied from <code>b</code>, - * an IOException will be thrown and no byte will be pushed back. - * </p> - * - * @param b the byte array containing bytes to push back into the stream - * - * @exception java.io.IOException if the pushback buffer is too small - */ - public void unread(byte[] b) throws IOException { - int length = b.length; - if (length > pos) throw new IOException(); - position -= length; - pos -= length; - System.arraycopy(b, 0, buf, pos, length); - } -} -public static abstract class FileFormat { - LEDataInputStream inputStream; - ImageLoader loader; - int compression; + byte[] bitInvertData(byte[] data, int startIndex, int endIndex) { + // Destructively bit invert data in the given byte array. + for (int i = startIndex; i < endIndex; i++) { + data[i] = (byte) (255 - data[i - startIndex]); + } + return data; + } -byte[] bitInvertData(byte[] data, int startIndex, int endIndex) { - // Destructively bit invert data in the given byte array. - for (int i = startIndex; i < endIndex; i++) { - data[i] = (byte)(255 - data[i - startIndex]); - } - return data; -} + /** + * Return whether or not the specified input stream + * represents a supported file format. + */ + abstract boolean isFileFormat(LEDataInputStream stream); -/** - * Return whether or not the specified input stream - * represents a supported file format. - */ -abstract boolean isFileFormat(LEDataInputStream stream); + abstract ImageData[] loadFromByteStream(); -abstract ImageData[] loadFromByteStream(); + public ImageData[] loadFromStream(LEDataInputStream stream) { + try { + inputStream = stream; + return loadFromByteStream(); + } catch (Exception e) { + SWT.error(SWT.ERROR_IO, e); + return null; + } + } -public ImageData[] loadFromStream(LEDataInputStream stream) { - try { - inputStream = stream; - return loadFromByteStream(); - } catch (Exception e) { - SWT.error(SWT.ERROR_IO, e); - return null; + public static ImageData[] load(InputStream is, ImageLoader loader) { + LEDataInputStream stream = new LEDataInputStream(is); + boolean isSupported = false; + FileFormat fileFormat = new WinICOFileFormat(); + if (fileFormat.isFileFormat(stream)) + isSupported = true; + else { + fileFormat = new WinBMPFileFormat(); + if (fileFormat.isFileFormat(stream)) + isSupported = true; + } + if (!isSupported) + SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); + fileFormat.loader = loader; + return fileFormat.loadFromStream(stream); + } } -} -public static ImageData[] load(InputStream is, ImageLoader loader) { - LEDataInputStream stream = new LEDataInputStream(is); - boolean isSupported = false; - FileFormat fileFormat = new WinICOFileFormat(); - if (fileFormat.isFileFormat(stream)) isSupported = true; - else { - fileFormat = new WinBMPFileFormat(); - if (fileFormat.isFileFormat(stream)) isSupported = true; - } - if (!isSupported) SWT.error(SWT.ERROR_UNSUPPORTED_FORMAT); - fileFormat.loader = loader; - return fileFormat.loadFromStream(stream); -} -} -static class WinBMPFileFormat extends FileFormat { - static final int BMPFileHeaderSize = 14; - static final int BMPHeaderFixedSize = 40; - int importantColors; + static class WinBMPFileFormat extends FileFormat { + static final int BMPFileHeaderSize = 14; + static final int BMPHeaderFixedSize = 40; + int importantColors; -void decompressData(byte[] src, byte[] dest, int stride, int cmp) { - if (cmp == 1) { // BMP_RLE8_COMPRESSION - if (decompressRLE8Data(src, src.length, stride, dest, dest.length) <= 0) - SWT.error(SWT.ERROR_INVALID_IMAGE); - return; - } - if (cmp == 2) { // BMP_RLE4_COMPRESSION - if (decompressRLE4Data(src, src.length, stride, dest, dest.length) <= 0) + void decompressData(byte[] src, byte[] dest, int stride, int cmp) { + if (cmp == 1) { // BMP_RLE8_COMPRESSION + if (decompressRLE8Data(src, src.length, stride, dest, dest.length) <= 0) + SWT.error(SWT.ERROR_INVALID_IMAGE); + return; + } + if (cmp == 2) { // BMP_RLE4_COMPRESSION + if (decompressRLE4Data(src, src.length, stride, dest, dest.length) <= 0) + SWT.error(SWT.ERROR_INVALID_IMAGE); + return; + } SWT.error(SWT.ERROR_INVALID_IMAGE); - return; - } - SWT.error(SWT.ERROR_INVALID_IMAGE); -} -int decompressRLE4Data(byte[] src, int numBytes, int stride, byte[] dest, int destSize) { - int sp = 0; - int se = numBytes; - int dp = 0; - int de = destSize; - int x = 0, y = 0; - while (sp < se) { - int len = src[sp] & 0xFF; - sp++; - if (len == 0) { - len = src[sp] & 0xFF; - sp++; - switch (len) { - case 0: /* end of line */ - y++; - x = 0; - dp = y * stride; - if (dp >= de) - return -1; - break; - case 1: /* end of bitmap */ - return 1; - case 2: /* delta */ - x += src[sp] & 0xFF; - sp++; - y += src[sp] & 0xFF; + } + + int decompressRLE4Data(byte[] src, int numBytes, int stride, byte[] dest, int destSize) { + int sp = 0; + int se = numBytes; + int dp = 0; + int de = destSize; + int x = 0, y = 0; + while (sp < se) { + int len = src[sp] & 0xFF; + sp++; + if (len == 0) { + len = src[sp] & 0xFF; sp++; - dp = y * stride + x / 2; - if (dp >= de) - return -1; - break; - default: /* absolute mode run */ - if ((len & 1) != 0) /* odd run lengths not currently supported */ + switch (len) { + case 0 : /* end of line */ + y++; + x = 0; + dp = y * stride; + if (dp >= de) + return -1; + break; + case 1 : /* end of bitmap */ + return 1; + case 2 : /* delta */ + x += src[sp] & 0xFF; + sp++; + y += src[sp] & 0xFF; + sp++; + dp = y * stride + x / 2; + if (dp >= de) + return -1; + break; + default : /* absolute mode run */ + if ((len & 1) != 0) /* odd run lengths not currently supported */ + return -1; + x += len; + len = len / 2; + if (len > (se - sp)) + return -1; + if (len > (de - dp)) + return -1; + for (int i = 0; i < len; i++) { + dest[dp] = src[sp]; + dp++; + sp++; + } + if ((sp & 1) != 0) + sp++; /* word align sp? */ + break; + } + } else { + if ((len & 1) != 0) return -1; x += len; len = len / 2; - if (len > (se - sp)) - return -1; + byte theByte = src[sp]; + sp++; if (len > (de - dp)) return -1; for (int i = 0; i < len; i++) { - dest[dp] = src[sp]; + dest[dp] = theByte; dp++; - sp++; } - if ((sp & 1) != 0) - sp++; /* word align sp? */ - break; - } - } else { - if ((len & 1) != 0) - return -1; - x += len; - len = len / 2; - byte theByte = src[sp]; - sp++; - if (len > (de - dp)) - return -1; - for (int i = 0; i < len; i++) { - dest[dp] = theByte; - dp++; + } } + return 1; } - } - return 1; -} -int decompressRLE8Data(byte[] src, int numBytes, int stride, byte[] dest, int destSize) { - int sp = 0; - int se = numBytes; - int dp = 0; - int de = destSize; - int x = 0, y = 0; - while (sp < se) { - int len = src[sp] & 0xFF; - sp++; - if (len == 0) { - len = src[sp] & 0xFF; - sp++; - switch (len) { - case 0: /* end of line */ - y++; - x = 0; - dp = y * stride; - if (dp >= de) - return -1; - break; - case 1: /* end of bitmap */ - return 1; - case 2: /* delta */ - x += src[sp] & 0xFF; + + int decompressRLE8Data(byte[] src, int numBytes, int stride, byte[] dest, int destSize) { + int sp = 0; + int se = numBytes; + int dp = 0; + int de = destSize; + int x = 0, y = 0; + while (sp < se) { + int len = src[sp] & 0xFF; + sp++; + if (len == 0) { + len = src[sp] & 0xFF; sp++; - y += src[sp] & 0xFF; + switch (len) { + case 0 : /* end of line */ + y++; + x = 0; + dp = y * stride; + if (dp >= de) + return -1; + break; + case 1 : /* end of bitmap */ + return 1; + case 2 : /* delta */ + x += src[sp] & 0xFF; + sp++; + y += src[sp] & 0xFF; + sp++; + dp = y * stride + x; + if (dp >= de) + return -1; + break; + default : /* absolute mode run */ + if (len > (se - sp)) + return -1; + if (len > (de - dp)) + return -1; + for (int i = 0; i < len; i++) { + dest[dp] = src[sp]; + dp++; + sp++; + } + if ((sp & 1) != 0) + sp++; /* word align sp? */ + x += len; + break; + } + } else { + byte theByte = src[sp]; sp++; - dp = y * stride + x; - if (dp >= de) - return -1; - break; - default: /* absolute mode run */ - if (len > (se - sp)) - return -1; if (len > (de - dp)) return -1; for (int i = 0; i < len; i++) { - dest[dp] = src[sp]; + dest[dp] = theByte; dp++; - sp++; } - if ((sp & 1) != 0) - sp++; /* word align sp? */ x += len; - break; + } } - } else { - byte theByte = src[sp]; - sp++; - if (len > (de - dp)) - return -1; - for (int i = 0; i < len; i++) { - dest[dp] = theByte; - dp++; + return 1; + } + + boolean isFileFormat(LEDataInputStream stream) { + try { + byte[] header = new byte[18]; + stream.read(header); + stream.unread(header); + int infoHeaderSize = (header[14] & 0xFF) | ((header[15] & 0xFF) << 8) | ((header[16] & 0xFF) << 16) | ((header[17] & 0xFF) << 24); + return header[0] == 0x42 && header[1] == 0x4D && infoHeaderSize >= BMPHeaderFixedSize; + } catch (Exception e) { + return false; } - x += len; } - } - return 1; -} -boolean isFileFormat(LEDataInputStream stream) { - try { - byte[] header = new byte[18]; - stream.read(header); - stream.unread(header); - int infoHeaderSize = (header[14] & 0xFF) | ((header[15] & 0xFF) << 8) | ((header[16] & 0xFF) << 16) | ((header[17] & 0xFF) << 24); - return header[0] == 0x42 && header[1] == 0x4D && infoHeaderSize >= BMPHeaderFixedSize; - } catch (Exception e) { - return false; - } -} -byte[] loadData(byte[] infoHeader) { - int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); - int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); - int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); - int stride = (width * bitCount + 7) / 8; - stride = (stride + 3) / 4 * 4; // Round up to 4 byte multiple - byte[] data = loadData(infoHeader, stride); - flipScanLines(data, stride, height); - return data; -} -byte[] loadData(byte[] infoHeader, int stride) { - int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); - int dataSize = height * stride; - byte[] data = new byte[dataSize]; - int cmp = (infoHeader[16] & 0xFF) | ((infoHeader[17] & 0xFF) << 8) | ((infoHeader[18] & 0xFF) << 16) | ((infoHeader[19] & 0xFF) << 24); - if (cmp == 0) { // BMP_NO_COMPRESSION - try { - if (inputStream.read(data) != dataSize) - SWT.error(SWT.ERROR_INVALID_IMAGE); - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); + + byte[] loadData(byte[] infoHeader) { + int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); + int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); + int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); + int stride = (width * bitCount + 7) / 8; + stride = (stride + 3) / 4 * 4; // Round up to 4 byte multiple + byte[] data = loadData(infoHeader, stride); + flipScanLines(data, stride, height); + return data; } - } else { - int compressedSize = (infoHeader[20] & 0xFF) | ((infoHeader[21] & 0xFF) << 8) | ((infoHeader[22] & 0xFF) << 16) | ((infoHeader[23] & 0xFF) << 24); - byte[] compressed = new byte[compressedSize]; - try { - if (inputStream.read(compressed) != compressedSize) + + byte[] loadData(byte[] infoHeader, int stride) { + int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); + int dataSize = height * stride; + byte[] data = new byte[dataSize]; + int cmp = (infoHeader[16] & 0xFF) | ((infoHeader[17] & 0xFF) << 8) | ((infoHeader[18] & 0xFF) << 16) | ((infoHeader[19] & 0xFF) << 24); + if (cmp == 0) { // BMP_NO_COMPRESSION + try { + if (inputStream.read(data) != dataSize) + SWT.error(SWT.ERROR_INVALID_IMAGE); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + } else { + int compressedSize = (infoHeader[20] & 0xFF) | ((infoHeader[21] & 0xFF) << 8) | ((infoHeader[22] & 0xFF) << 16) | ((infoHeader[23] & 0xFF) << 24); + byte[] compressed = new byte[compressedSize]; + try { + if (inputStream.read(compressed) != compressedSize) + SWT.error(SWT.ERROR_INVALID_IMAGE); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + decompressData(compressed, data, stride, cmp); + } + return data; + } + + int[] loadFileHeader() { + int[] header = new int[5]; + try { + header[0] = inputStream.readShort(); + header[1] = inputStream.readInt(); + header[2] = inputStream.readShort(); + header[3] = inputStream.readShort(); + header[4] = inputStream.readInt(); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + if (header[0] != 0x4D42) SWT.error(SWT.ERROR_INVALID_IMAGE); - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); + return header; } - decompressData(compressed, data, stride, cmp); - } - return data; -} -int[] loadFileHeader() { - int[] header = new int[5]; - try { - header[0] = inputStream.readShort(); - header[1] = inputStream.readInt(); - header[2] = inputStream.readShort(); - header[3] = inputStream.readShort(); - header[4] = inputStream.readInt(); - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); - } - if (header[0] != 0x4D42) - SWT.error(SWT.ERROR_INVALID_IMAGE); - return header; -} -ImageData[] loadFromByteStream() { - int[] fileHeader = loadFileHeader(); - byte[] infoHeader = new byte[BMPHeaderFixedSize]; - try { - inputStream.read(infoHeader); - } catch (Exception e) { - SWT.error(SWT.ERROR_IO, e); - } - int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); - int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); - int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); - PaletteData palette = loadPalette(infoHeader); - if (inputStream.getPosition() < fileHeader[4]) { - // Seek to the specified offset - try { - inputStream.skip(fileHeader[4] - inputStream.getPosition()); - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); + + ImageData[] loadFromByteStream() { + int[] fileHeader = loadFileHeader(); + byte[] infoHeader = new byte[BMPHeaderFixedSize]; + try { + inputStream.read(infoHeader); + } catch (Exception e) { + SWT.error(SWT.ERROR_IO, e); + } + int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); + int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); + int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); + PaletteData palette = loadPalette(infoHeader); + if (inputStream.getPosition() < fileHeader[4]) { + // Seek to the specified offset + try { + inputStream.skip(fileHeader[4] - inputStream.getPosition()); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + } + byte[] data = loadData(infoHeader); + this.compression = (infoHeader[16] & 0xFF) | ((infoHeader[17] & 0xFF) << 8) | ((infoHeader[18] & 0xFF) << 16) | ((infoHeader[19] & 0xFF) << 24); + this.importantColors = (infoHeader[36] & 0xFF) | ((infoHeader[37] & 0xFF) << 8) | ((infoHeader[38] & 0xFF) << 16) | ((infoHeader[39] & 0xFF) << 24); + // int xPelsPerMeter = (infoHeader[24] & 0xFF) | ((infoHeader[25] & 0xFF) << 8) | ((infoHeader[26] & 0xFF) << 16) | ((infoHeader[27] & 0xFF) << 24); + // int yPelsPerMeter = (infoHeader[28] & 0xFF) | ((infoHeader[29] & 0xFF) << 8) | ((infoHeader[30] & 0xFF) << 16) | ((infoHeader[31] & 0xFF) << 24); + int type = (this.compression == 1 /*BMP_RLE8_COMPRESSION*/) || (this.compression == 2 /*BMP_RLE4_COMPRESSION*/) ? SWT.IMAGE_BMP_RLE : SWT.IMAGE_BMP; + return new ImageData[] {ImageData.internal_new(width, height, bitCount, palette, 4, data, 0, null, null, -1, -1, type, 0, 0, 0, 0)}; } + + PaletteData loadPalette(byte[] infoHeader) { + int depth = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); + if (depth <= 8) { + int numColors = (infoHeader[32] & 0xFF) | ((infoHeader[33] & 0xFF) << 8) | ((infoHeader[34] & 0xFF) << 16) | ((infoHeader[35] & 0xFF) << 24); + if (numColors == 0) { + numColors = 1 << depth; + } else { + if (numColors > 256) + numColors = 256; + } + byte[] buf = new byte[numColors * 4]; + try { + if (inputStream.read(buf) != buf.length) + SWT.error(SWT.ERROR_INVALID_IMAGE); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + return paletteFromBytes(buf, numColors); + } + if (depth == 16) + return new PaletteData(0x7C00, 0x3E0, 0x1F); + if (depth == 24) + return new PaletteData(0xFF, 0xFF00, 0xFF0000); + return new PaletteData(0xFF00, 0xFF0000, 0xFF000000); + } + + PaletteData paletteFromBytes(byte[] bytes, int numColors) { + int bytesOffset = 0; + RGB[] colors = new RGB[numColors]; + for (int i = 0; i < numColors; i++) { + colors[i] = new RGB(bytes[bytesOffset + 2] & 0xFF, bytes[bytesOffset + 1] & 0xFF, bytes[bytesOffset] & 0xFF); + bytesOffset += 4; + } + return new PaletteData(colors); + } + + /** + * Answer a byte array containing the BMP representation of + * the given device independent palette. + */ + static byte[] paletteToBytes(PaletteData pal) { + int n = pal.colors == null ? 0 : (pal.colors.length < 256 ? pal.colors.length : 256); + byte[] bytes = new byte[n * 4]; + int offset = 0; + for (int i = 0; i < n; i++) { + RGB col = pal.colors[i]; + bytes[offset] = (byte) col.blue; + bytes[offset + 1] = (byte) col.green; + bytes[offset + 2] = (byte) col.red; + offset += 4; + } + return bytes; + } + + void flipScanLines(byte[] data, int stride, int height) { + int i1 = 0; + int i2 = (height - 1) * stride; + for (int i = 0; i < height / 2; i++) { + for (int index = 0; index < stride; index++) { + byte b = data[index + i1]; + data[index + i1] = data[index + i2]; + data[index + i2] = b; + } + i1 += stride; + i2 -= stride; + } + } + } - byte[] data = loadData(infoHeader); - this.compression = (infoHeader[16] & 0xFF) | ((infoHeader[17] & 0xFF) << 8) | ((infoHeader[18] & 0xFF) << 16) | ((infoHeader[19] & 0xFF) << 24); - this.importantColors = (infoHeader[36] & 0xFF) | ((infoHeader[37] & 0xFF) << 8) | ((infoHeader[38] & 0xFF) << 16) | ((infoHeader[39] & 0xFF) << 24); -// int xPelsPerMeter = (infoHeader[24] & 0xFF) | ((infoHeader[25] & 0xFF) << 8) | ((infoHeader[26] & 0xFF) << 16) | ((infoHeader[27] & 0xFF) << 24); -// int yPelsPerMeter = (infoHeader[28] & 0xFF) | ((infoHeader[29] & 0xFF) << 8) | ((infoHeader[30] & 0xFF) << 16) | ((infoHeader[31] & 0xFF) << 24); - int type = (this.compression == 1 /*BMP_RLE8_COMPRESSION*/) || (this.compression == 2 /*BMP_RLE4_COMPRESSION*/) ? SWT.IMAGE_BMP_RLE : SWT.IMAGE_BMP; - return new ImageData[] { - ImageData.internal_new( - width, - height, - bitCount, - palette, - 4, - data, - 0, - null, - null, - -1, - -1, - type, - 0, - 0, - 0, - 0) - }; -} -PaletteData loadPalette(byte[] infoHeader) { - int depth = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); - if (depth <= 8) { - int numColors = (infoHeader[32] & 0xFF) | ((infoHeader[33] & 0xFF) << 8) | ((infoHeader[34] & 0xFF) << 16) | ((infoHeader[35] & 0xFF) << 24); - if (numColors == 0) { - numColors = 1 << depth; - } else { - if (numColors > 256) - numColors = 256; + + static class WinICOFileFormat extends FileFormat { + + static final byte[] convertPad(byte[] data, int width, int height, int depth, int pad, int newPad) { + if (pad == newPad) + return data; + int stride = (width * depth + 7) / 8; + int bpl = (stride + (pad - 1)) / pad * pad; + int newBpl = (stride + (newPad - 1)) / newPad * newPad; + byte[] newData = new byte[height * newBpl]; + int srcIndex = 0, destIndex = 0; + for (int y = 0; y < height; y++) { + System.arraycopy(data, srcIndex, newData, destIndex, newBpl); + srcIndex += bpl; + destIndex += newBpl; + } + return newData; } - byte[] buf = new byte[numColors * 4]; - try { - if (inputStream.read(buf) != buf.length) + + /** + * Answer the size in bytes of the file representation of the given + * icon + */ + int iconSize(ImageData i) { + int shapeDataStride = (i.width * i.depth + 31) / 32 * 4; + int maskDataStride = (i.width + 31) / 32 * 4; + int dataSize = (shapeDataStride + maskDataStride) * i.height; + int paletteSize = i.palette.colors != null ? i.palette.colors.length * 4 : 0; + return WinBMPFileFormat.BMPHeaderFixedSize + paletteSize + dataSize; + } + + boolean isFileFormat(LEDataInputStream stream) { + try { + byte[] header = new byte[4]; + stream.read(header); + stream.unread(header); + return header[0] == 0 && header[1] == 0 && header[2] == 1 && header[3] == 0; + } catch (Exception e) { + return false; + } + } + + boolean isValidIcon(ImageData i) { + switch (i.depth) { + case 1 : + case 4 : + case 8 : + if (i.palette.isDirect) + return false; + int size = i.palette.colors.length; + return size == 2 || size == 16 || size == 32 || size == 256; + case 24 : + case 32 : + return i.palette.isDirect; + } + return false; + } + + int loadFileHeader(LEDataInputStream byteStream) { + int[] fileHeader = new int[3]; + try { + fileHeader[0] = byteStream.readShort(); + fileHeader[1] = byteStream.readShort(); + fileHeader[2] = byteStream.readShort(); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + if ((fileHeader[0] != 0) || (fileHeader[1] != 1)) SWT.error(SWT.ERROR_INVALID_IMAGE); - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); + int numIcons = fileHeader[2]; + if (numIcons <= 0) + SWT.error(SWT.ERROR_INVALID_IMAGE); + return numIcons; } - return paletteFromBytes(buf, numColors); - } - if (depth == 16) return new PaletteData(0x7C00, 0x3E0, 0x1F); - if (depth == 24) return new PaletteData(0xFF, 0xFF00, 0xFF0000); - return new PaletteData(0xFF00, 0xFF0000, 0xFF000000); -} -PaletteData paletteFromBytes(byte[] bytes, int numColors) { - int bytesOffset = 0; - RGB[] colors = new RGB[numColors]; - for (int i = 0; i < numColors; i++) { - colors[i] = new RGB(bytes[bytesOffset + 2] & 0xFF, - bytes[bytesOffset + 1] & 0xFF, - bytes[bytesOffset] & 0xFF); - bytesOffset += 4; - } - return new PaletteData(colors); -} -/** - * Answer a byte array containing the BMP representation of - * the given device independent palette. - */ -static byte[] paletteToBytes(PaletteData pal) { - int n = pal.colors == null ? 0 : (pal.colors.length < 256 ? pal.colors.length : 256); - byte[] bytes = new byte[n * 4]; - int offset = 0; - for (int i = 0; i < n; i++) { - RGB col = pal.colors[i]; - bytes[offset] = (byte)col.blue; - bytes[offset + 1] = (byte)col.green; - bytes[offset + 2] = (byte)col.red; - offset += 4; - } - return bytes; -} -void flipScanLines(byte[] data, int stride, int height) { - int i1 = 0; - int i2 = (height - 1) * stride; - for (int i = 0; i < height / 2; i++) { - for (int index = 0; index < stride; index++) { - byte b = data[index + i1]; - data[index + i1] = data[index + i2]; - data[index + i2] = b; + int loadFileHeader(LEDataInputStream byteStream, boolean hasHeader) { + int[] fileHeader = new int[3]; + try { + if (hasHeader) { + fileHeader[0] = byteStream.readShort(); + fileHeader[1] = byteStream.readShort(); + } else { + fileHeader[0] = 0; + fileHeader[1] = 1; + } + fileHeader[2] = byteStream.readShort(); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + if ((fileHeader[0] != 0) || (fileHeader[1] != 1)) + SWT.error(SWT.ERROR_INVALID_IMAGE); + int numIcons = fileHeader[2]; + if (numIcons <= 0) + SWT.error(SWT.ERROR_INVALID_IMAGE); + return numIcons; } - i1 += stride; - i2 -= stride; - } -} -} + ImageData[] loadFromByteStream() { + int numIcons = loadFileHeader(inputStream); + int[][] headers = loadIconHeaders(numIcons); + ImageData[] icons = new ImageData[headers.length]; + for (int i = 0; i < icons.length; i++) { + icons[i] = loadIcon(headers[i]); + } + return icons; + } -static class WinICOFileFormat extends FileFormat { - -static final byte[] convertPad(byte[] data, int width, int height, int depth, int pad, int newPad) { - if (pad == newPad) return data; - int stride = (width * depth + 7) / 8; - int bpl = (stride + (pad - 1)) / pad * pad; - int newBpl = (stride + (newPad - 1)) / newPad * newPad; - byte[] newData = new byte[height * newBpl]; - int srcIndex = 0, destIndex = 0; - for (int y = 0; y < height; y++) { - System.arraycopy(data, srcIndex, newData, destIndex, newBpl); - srcIndex += bpl; - destIndex += newBpl; - } - return newData; -} -/** - * Answer the size in bytes of the file representation of the given - * icon - */ -int iconSize(ImageData i) { - int shapeDataStride = (i.width * i.depth + 31) / 32 * 4; - int maskDataStride = (i.width + 31) / 32 * 4; - int dataSize = (shapeDataStride + maskDataStride) * i.height; - int paletteSize = i.palette.colors != null ? i.palette.colors.length * 4 : 0; - return WinBMPFileFormat.BMPHeaderFixedSize + paletteSize + dataSize; -} -boolean isFileFormat(LEDataInputStream stream) { - try { - byte[] header = new byte[4]; - stream.read(header); - stream.unread(header); - return header[0] == 0 && header[1] == 0 && header[2] == 1 && header[3] == 0; - } catch (Exception e) { - return false; - } -} -boolean isValidIcon(ImageData i) { - switch (i.depth) { - case 1: - case 4: - case 8: - if (i.palette.isDirect) return false; - int size = i.palette.colors.length; - return size == 2 || size == 16 || size == 32 || size == 256; - case 24: - case 32: - return i.palette.isDirect; - } - return false; -} -int loadFileHeader(LEDataInputStream byteStream) { - int[] fileHeader = new int[3]; - try { - fileHeader[0] = byteStream.readShort(); - fileHeader[1] = byteStream.readShort(); - fileHeader[2] = byteStream.readShort(); - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); - } - if ((fileHeader[0] != 0) || (fileHeader[1] != 1)) - SWT.error(SWT.ERROR_INVALID_IMAGE); - int numIcons = fileHeader[2]; - if (numIcons <= 0) - SWT.error(SWT.ERROR_INVALID_IMAGE); - return numIcons; -} -int loadFileHeader(LEDataInputStream byteStream, boolean hasHeader) { - int[] fileHeader = new int[3]; - try { - if (hasHeader) { - fileHeader[0] = byteStream.readShort(); - fileHeader[1] = byteStream.readShort(); - } else { - fileHeader[0] = 0; - fileHeader[1] = 1; - } - fileHeader[2] = byteStream.readShort(); - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); - } - if ((fileHeader[0] != 0) || (fileHeader[1] != 1)) - SWT.error(SWT.ERROR_INVALID_IMAGE); - int numIcons = fileHeader[2]; - if (numIcons <= 0) - SWT.error(SWT.ERROR_INVALID_IMAGE); - return numIcons; -} -ImageData[] loadFromByteStream() { - int numIcons = loadFileHeader(inputStream); - int[][] headers = loadIconHeaders(numIcons); - ImageData[] icons = new ImageData[headers.length]; - for (int i = 0; i < icons.length; i++) { - icons[i] = loadIcon(headers[i]); - } - return icons; -} -/** - * Load one icon from the byte stream. - */ -ImageData loadIcon(int[] iconHeader) { - byte[] infoHeader = loadInfoHeader(iconHeader); - WinBMPFileFormat bmpFormat = new WinBMPFileFormat(); - bmpFormat.inputStream = inputStream; - PaletteData palette = bmpFormat.loadPalette(infoHeader); - byte[] shapeData = bmpFormat.loadData(infoHeader); - int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); - int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); - int depth = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); - infoHeader[14] = 1; - infoHeader[15] = 0; - byte[] maskData = bmpFormat.loadData(infoHeader); - maskData = convertPad(maskData, width, height, 1, 4, 2); - bitInvertData(maskData, 0, maskData.length); - return ImageData.internal_new( - width, - height, - depth, - palette, - 4, - shapeData, - 2, - maskData, - null, - -1, - -1, - SWT.IMAGE_ICO, - 0, - 0, - 0, - 0); -} -int[][] loadIconHeaders(int numIcons) { - int[][] headers = new int[numIcons][7]; - try { - for (int i = 0; i < numIcons; i++) { - headers[i][0] = inputStream.read(); - headers[i][1] = inputStream.read(); - headers[i][2] = inputStream.readShort(); - headers[i][3] = inputStream.readShort(); - headers[i][4] = inputStream.readShort(); - headers[i][5] = inputStream.readInt(); - headers[i][6] = inputStream.readInt(); - } - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); - } - return headers; -} -byte[] loadInfoHeader(int[] iconHeader) { - int width = iconHeader[0]; - int height = iconHeader[1]; - int numColors = iconHeader[2]; // the number of colors is in the low byte, but the high byte must be 0 - if (numColors == 0) numColors = 256; // this is specified: '00' represents '256' (0x100) colors - if ((numColors != 2) && (numColors != 8) && (numColors != 16) && - (numColors != 32) && (numColors != 256)) - SWT.error(SWT.ERROR_INVALID_IMAGE); - if (inputStream.getPosition() < iconHeader[6]) { - // Seek to the specified offset - try { - inputStream.skip(iconHeader[6] - inputStream.getPosition()); - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); - return null; + /** + * Load one icon from the byte stream. + */ + ImageData loadIcon(int[] iconHeader) { + byte[] infoHeader = loadInfoHeader(iconHeader); + WinBMPFileFormat bmpFormat = new WinBMPFileFormat(); + bmpFormat.inputStream = inputStream; + PaletteData palette = bmpFormat.loadPalette(infoHeader); + byte[] shapeData = bmpFormat.loadData(infoHeader); + int width = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); + int height = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); + int depth = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); + infoHeader[14] = 1; + infoHeader[15] = 0; + byte[] maskData = bmpFormat.loadData(infoHeader); + maskData = convertPad(maskData, width, height, 1, 4, 2); + bitInvertData(maskData, 0, maskData.length); + return ImageData.internal_new(width, height, depth, palette, 4, shapeData, 2, maskData, null, -1, -1, SWT.IMAGE_ICO, 0, 0, 0, 0); + } + + int[][] loadIconHeaders(int numIcons) { + int[][] headers = new int[numIcons][7]; + try { + for (int i = 0; i < numIcons; i++) { + headers[i][0] = inputStream.read(); + headers[i][1] = inputStream.read(); + headers[i][2] = inputStream.readShort(); + headers[i][3] = inputStream.readShort(); + headers[i][4] = inputStream.readShort(); + headers[i][5] = inputStream.readInt(); + headers[i][6] = inputStream.readInt(); + } + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + return headers; + } + + byte[] loadInfoHeader(int[] iconHeader) { + int width = iconHeader[0]; + int height = iconHeader[1]; + int numColors = iconHeader[2]; // the number of colors is in the low byte, but the high byte must be 0 + if (numColors == 0) + numColors = 256; // this is specified: '00' represents '256' (0x100) colors + if ((numColors != 2) && (numColors != 8) && (numColors != 16) && (numColors != 32) && (numColors != 256)) + SWT.error(SWT.ERROR_INVALID_IMAGE); + if (inputStream.getPosition() < iconHeader[6]) { + // Seek to the specified offset + try { + inputStream.skip(iconHeader[6] - inputStream.getPosition()); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + return null; + } + } + byte[] infoHeader = new byte[WinBMPFileFormat.BMPHeaderFixedSize]; + try { + inputStream.read(infoHeader); + } catch (IOException e) { + SWT.error(SWT.ERROR_IO, e); + } + if (((infoHeader[12] & 0xFF) | ((infoHeader[13] & 0xFF) << 8)) != 1) + SWT.error(SWT.ERROR_INVALID_IMAGE); + int infoWidth = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); + int infoHeight = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); + int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); + if (height == infoHeight && bitCount == 1) + height /= 2; + if (!((width == infoWidth) && (height * 2 == infoHeight) && (bitCount == 1 || bitCount == 4 || bitCount == 8 || bitCount == 24 || bitCount == 32))) + SWT.error(SWT.ERROR_INVALID_IMAGE); + infoHeader[8] = (byte) (height & 0xFF); + infoHeader[9] = (byte) ((height >> 8) & 0xFF); + infoHeader[10] = (byte) ((height >> 16) & 0xFF); + infoHeader[11] = (byte) ((height >> 24) & 0xFF); + return infoHeader; } } - byte[] infoHeader = new byte[WinBMPFileFormat.BMPHeaderFixedSize]; - try { - inputStream.read(infoHeader); - } catch (IOException e) { - SWT.error(SWT.ERROR_IO, e); - } - if (((infoHeader[12] & 0xFF) | ((infoHeader[13] & 0xFF) << 8)) != 1) - SWT.error(SWT.ERROR_INVALID_IMAGE); - int infoWidth = (infoHeader[4] & 0xFF) | ((infoHeader[5] & 0xFF) << 8) | ((infoHeader[6] & 0xFF) << 16) | ((infoHeader[7] & 0xFF) << 24); - int infoHeight = (infoHeader[8] & 0xFF) | ((infoHeader[9] & 0xFF) << 8) | ((infoHeader[10] & 0xFF) << 16) | ((infoHeader[11] & 0xFF) << 24); - int bitCount = (infoHeader[14] & 0xFF) | ((infoHeader[15] & 0xFF) << 8); - if (height == infoHeight && bitCount == 1) height /= 2; - if (!((width == infoWidth) && (height * 2 == infoHeight) && - (bitCount == 1 || bitCount == 4 || bitCount == 8 || bitCount == 24 || bitCount == 32))) - SWT.error(SWT.ERROR_INVALID_IMAGE); - infoHeader[8] = (byte)(height & 0xFF); - infoHeader[9] = (byte)((height >> 8) & 0xFF); - infoHeader[10] = (byte)((height >> 16) & 0xFF); - infoHeader[11] = (byte)((height >> 24) & 0xFF); - return infoHeader; -} -} -static class SWT { - public static final int IMAGE_ICO = 3; - public static final int ERROR_IO = 39; - public static final int ERROR_INVALID_IMAGE = 40; - public static final int ERROR_NULL_ARGUMENT = 4; - public static final int ERROR_INVALID_ARGUMENT = 5; - public static final int ERROR_CANNOT_BE_ZERO = 7; - public static final int IMAGE_UNDEFINED = -1; - public static final int ERROR_UNSUPPORTED_DEPTH = 38; - public static final int TRANSPARENCY_MASK = 1 << 1; - public static final int ERROR_UNSUPPORTED_FORMAT = 42; - public static final int TRANSPARENCY_ALPHA = 1 << 0; - public static final int TRANSPARENCY_NONE = 0x0; - public static final int TRANSPARENCY_PIXEL = 1 << 2; - public static final int IMAGE_BMP = 0; - public static final int IMAGE_BMP_RLE = 1; - - public static void error(int code) { - throw new RuntimeException("Error "+code); //$NON-NLS-1$ - } - public static void error(int code, Throwable t) { - throw new RuntimeException(t); + + static class SWT { + public static final int IMAGE_ICO = 3; + public static final int ERROR_IO = 39; + public static final int ERROR_INVALID_IMAGE = 40; + public static final int ERROR_NULL_ARGUMENT = 4; + public static final int ERROR_INVALID_ARGUMENT = 5; + public static final int ERROR_CANNOT_BE_ZERO = 7; + public static final int IMAGE_UNDEFINED = -1; + public static final int ERROR_UNSUPPORTED_DEPTH = 38; + public static final int TRANSPARENCY_MASK = 1 << 1; + public static final int ERROR_UNSUPPORTED_FORMAT = 42; + public static final int TRANSPARENCY_ALPHA = 1 << 0; + public static final int TRANSPARENCY_NONE = 0x0; + public static final int TRANSPARENCY_PIXEL = 1 << 2; + public static final int IMAGE_BMP = 0; + public static final int IMAGE_BMP_RLE = 1; + + public static void error(int code) { + throw new RuntimeException("Error " + code); //$NON-NLS-1$ + } + + public static void error(int code, Throwable t) { + throw new RuntimeException(t); + } } } -} diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AbstractAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AbstractAdvice.java index 28761b9d9..457323fc7 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AbstractAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AbstractAdvice.java @@ -9,8 +9,8 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; -import org.eclipse.equinox.internal.provisional.p2.metadata.VersionRange; +import org.eclipse.equinox.p2.metadata.Version; +import org.eclipse.equinox.p2.metadata.VersionRange; public class AbstractAdvice implements IPublisherAdvice { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AbstractPublisherAction.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AbstractPublisherAction.java index ca70250c1..4674bd3ba 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AbstractPublisherAction.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AbstractPublisherAction.java @@ -10,28 +10,30 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher; -import org.eclipse.equinox.internal.provisional.p2.metadata.IVersionedId; - -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; -import org.eclipse.equinox.internal.provisional.p2.metadata.VersionRange; - import java.io.*; import java.util.*; +import java.util.Map.Entry; import org.eclipse.core.runtime.*; -import org.eclipse.equinox.internal.p2.core.helpers.FileUtils; -import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; +import org.eclipse.equinox.internal.p2.artifact.repository.simple.SimpleArtifactDescriptor; +import org.eclipse.equinox.internal.p2.core.helpers.*; import org.eclipse.equinox.internal.p2.core.helpers.FileUtils.IPathComputer; -import org.eclipse.equinox.internal.p2.publisher.*; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.*; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.processing.ProcessingStepDescriptor; -import org.eclipse.equinox.internal.provisional.p2.core.*; -import org.eclipse.equinox.internal.provisional.p2.metadata.*; +import org.eclipse.equinox.internal.p2.metadata.IRequiredCapability; +import org.eclipse.equinox.internal.p2.metadata.query.LatestIUVersionQuery; +import org.eclipse.equinox.internal.p2.publisher.Activator; +import org.eclipse.equinox.internal.p2.publisher.QuotedTokenizer; +import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; -import org.eclipse.equinox.internal.provisional.p2.metadata.query.*; +import org.eclipse.equinox.p2.core.ProvisionException; +import org.eclipse.equinox.p2.metadata.*; +import org.eclipse.equinox.p2.metadata.expression.ExpressionUtil; +import org.eclipse.equinox.p2.metadata.query.InstallableUnitQuery; import org.eclipse.equinox.p2.publisher.actions.*; +import org.eclipse.equinox.p2.query.*; +import org.eclipse.equinox.p2.repository.artifact.*; +import org.eclipse.equinox.p2.repository.artifact.spi.ArtifactDescriptor; +import org.eclipse.equinox.p2.repository.artifact.spi.ProcessingStepDescriptor; import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; import org.osgi.framework.Filter; -import org.osgi.framework.InvalidSyntaxException; public abstract class AbstractPublisherAction implements IPublisherAction { public static final String CONFIG_ANY = "ANY"; //$NON-NLS-1$ @@ -46,13 +48,13 @@ public abstract class AbstractPublisherAction implements IPublisherAction { public static String[] getArrayFromString(String list, String separator) { if (list == null || list.trim().equals("")) //$NON-NLS-1$ return new String[0]; - List result = new ArrayList(); + List<String> result = new ArrayList<String>(); for (QuotedTokenizer tokens = new QuotedTokenizer(list, separator); tokens.hasMoreTokens();) { String token = tokens.nextToken().trim(); if (!token.equals("")) //$NON-NLS-1$ result.add(token); } - return (String[]) result.toArray(new String[result.size()]); + return result.toArray(new String[result.size()]); } /** @@ -101,7 +103,7 @@ public abstract class AbstractPublisherAction implements IPublisherAction { * @return the LDAP filter for the given spec. <code>null</code> if the given spec does not * parse into a filter. */ - protected String createFilterSpec(String configSpec) { + protected Filter createFilterSpec(String configSpec) { String[] config = parseConfigSpec(configSpec); if (config[0] != null || config[1] != null || config[2] != null) { String filterWs = config[0] != null && config[0] != CONFIG_ANY ? "(osgi.ws=" + config[0] + ")" : ""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ @@ -109,29 +111,21 @@ public abstract class AbstractPublisherAction implements IPublisherAction { String filterArch = config[2] != null && config[2] != CONFIG_ANY ? "(osgi.arch=" + config[2] + ")" : ""; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ if (filterWs.length() == 0 && filterOs.length() == 0 && filterArch.length() == 0) return null; - return "(& " + filterWs + filterOs + filterArch + ")"; //$NON-NLS-1$ //$NON-NLS-2$ + return ExpressionUtil.parseLDAP("(& " + filterWs + filterOs + filterArch + ")"); //$NON-NLS-1$ //$NON-NLS-2$ } return null; } - protected boolean filterMatches(String filter, String configSpec) { + protected boolean filterMatches(Filter filter, String configSpec) { if (filter == null) return true; - Filter ldapFilter = null; - try { - ldapFilter = Activator.context.createFilter(filter); - } catch (InvalidSyntaxException e) { - // TODO true or false on error? - return true; - } - String[] config = parseConfigSpec(configSpec); - Dictionary environment = new Hashtable(3); + Dictionary<String, String> environment = new Hashtable<String, String>(3); environment.put("osgi.ws", config[0]); //$NON-NLS-1$ environment.put("osgi.os", config[1]); //$NON-NLS-1$ environment.put("osgi.arch", config[2]); //$NON-NLS-1$ - return ldapFilter.match(environment); + return filter.match(environment); } /** @@ -157,32 +151,29 @@ public abstract class AbstractPublisherAction implements IPublisherAction { * @param children descriptions of the IUs on which requirements are to be made * @return a collection of RequiredCapabilities representing the given IUs */ - protected Collection createIURequirements(Collection children) { - ArrayList result = new ArrayList(children.size()); - for (Iterator i = children.iterator(); i.hasNext();) { - Object next = i.next(); + protected Collection<IRequirement> createIURequirements(Collection<? extends IVersionedId> children) { + ArrayList<IRequirement> result = new ArrayList<IRequirement>(children.size()); + for (IVersionedId next : children) { if (next instanceof IInstallableUnit) { IInstallableUnit iu = (IInstallableUnit) next; VersionRange range = new VersionRange(iu.getVersion(), true, iu.getVersion(), true); - result.add(MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, iu.getId(), range, iu.getFilter(), false, false)); - } else if (next instanceof IVersionedId) { - IVersionedId name = (IVersionedId) next; - Version version = name.getVersion(); + result.add(MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, iu.getId(), range, iu.getFilter() == null ? null : iu.getFilter(), false, false)); + } else { + Version version = next.getVersion(); VersionRange range = (version == null || Version.emptyVersion.equals(version)) ? VersionRange.emptyRange : new VersionRange(version, true, version, true); - String filter = getFilterAdvice(name); - result.add(MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, name.getId(), range, filter, false, false)); + Filter filter = getFilterAdvice(next); + result.add(MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, next.getId(), range, filter, false, false)); } } return result; } - private String getFilterAdvice(IVersionedId name) { + private Filter getFilterAdvice(IVersionedId name) { if (info == null) return null; - Collection filterAdvice = info.getAdvice(CONFIG_ANY, true, name.getId(), name.getVersion(), IFilterAdvice.class); - for (Iterator i = filterAdvice.iterator(); i.hasNext();) { - IFilterAdvice advice = (IFilterAdvice) i.next(); - String result = advice.getFilter(name.getId(), name.getVersion(), false); + Collection<IFilterAdvice> filterAdvice = info.getAdvice(CONFIG_ANY, true, name.getId(), name.getVersion(), IFilterAdvice.class); + for (IFilterAdvice advice : filterAdvice) { + Filter result = advice.getFilter(name.getId(), name.getVersion(), false); if (result != null) return result; } @@ -197,7 +188,6 @@ public abstract class AbstractPublisherAction implements IPublisherAction { } protected IArtifactDescriptor createPack200ArtifactDescriptor(IArtifactKey key, File pathOnDisk, String installSize) { - final String PACKED_FORMAT = "packed"; //$NON-NLS-1$ //TODO this size calculation is bogus ArtifactDescriptor result = new ArtifactDescriptor(key); if (pathOnDisk != null) { @@ -205,13 +195,13 @@ public abstract class AbstractPublisherAction implements IPublisherAction { // TODO - this is wrong but I'm testing a work-around for bug 205842 result.setProperty(IArtifactDescriptor.DOWNLOAD_SIZE, Long.toString(pathOnDisk.length())); } - ProcessingStepDescriptor[] steps = new ProcessingStepDescriptor[] {new ProcessingStepDescriptor("org.eclipse.equinox.p2.processing.Pack200Unpacker", null, true)}; //$NON-NLS-1$ + IProcessingStepDescriptor[] steps = new IProcessingStepDescriptor[] {new ProcessingStepDescriptor("org.eclipse.equinox.p2.processing.Pack200Unpacker", null, true)}; //$NON-NLS-1$ result.setProcessingSteps(steps); - result.setProperty(IArtifactDescriptor.FORMAT, PACKED_FORMAT); + result.setProperty(IArtifactDescriptor.FORMAT, IArtifactDescriptor.FORMAT_PACKED); return result; } - protected InstallableUnitDescription createParentIU(Collection children, String id, Version version) { + protected InstallableUnitDescription createParentIU(Collection<? extends IVersionedId> children, String id, Version version) { InstallableUnitDescription root = createIUShell(id, version); root.addRequiredCapabilities(createIURequirements(children)); addSelfCapability(root); @@ -233,18 +223,17 @@ public abstract class AbstractPublisherAction implements IPublisherAction { } protected static InstallableUnitDescription[] processAdditionalInstallableUnitsAdvice(IInstallableUnit iu, IPublisherInfo publisherInfo) { - Collection advice = publisherInfo.getAdvice(null, false, iu.getId(), iu.getVersion(), IAdditionalInstallableUnitAdvice.class); + Collection<IAdditionalInstallableUnitAdvice> advice = publisherInfo.getAdvice(null, false, iu.getId(), iu.getVersion(), IAdditionalInstallableUnitAdvice.class); if (advice.isEmpty()) return null; - List ius = new ArrayList(); - for (Iterator iterator = advice.iterator(); iterator.hasNext();) { - IAdditionalInstallableUnitAdvice entry = (IAdditionalInstallableUnitAdvice) iterator.next(); + List<InstallableUnitDescription> ius = new ArrayList<InstallableUnitDescription>(); + for (IAdditionalInstallableUnitAdvice entry : advice) { InstallableUnitDescription[] others = entry.getAdditionalInstallableUnitDescriptions(iu); if (others != null) ius.addAll(Arrays.asList(others)); } - return (InstallableUnitDescription[]) ius.toArray(new InstallableUnitDescription[ius.size()]); + return ius.toArray(new InstallableUnitDescription[ius.size()]); } /** @@ -253,16 +242,17 @@ public abstract class AbstractPublisherAction implements IPublisherAction { * @param descriptor the descriptor to decorate * @param info the publisher info supplying the advice */ - protected static void processArtifactPropertiesAdvice(IInstallableUnit iu, ArtifactDescriptor descriptor, IPublisherInfo info) { - Collection advice = info.getAdvice(null, false, iu.getId(), iu.getVersion(), IPropertyAdvice.class); - for (Iterator i = advice.iterator(); i.hasNext();) { - IPropertyAdvice entry = (IPropertyAdvice) i.next(); - Properties props = entry.getArtifactProperties(iu, descriptor); + protected static void processArtifactPropertiesAdvice(IInstallableUnit iu, IArtifactDescriptor descriptor, IPublisherInfo info) { + if (!(descriptor instanceof SimpleArtifactDescriptor)) + return; + + Collection<IPropertyAdvice> advice = info.getAdvice(null, false, iu.getId(), iu.getVersion(), IPropertyAdvice.class); + for (IPropertyAdvice entry : advice) { + Map<String, String> props = entry.getArtifactProperties(iu, descriptor); if (props == null) continue; - for (Iterator j = props.keySet().iterator(); j.hasNext();) { - String key = (String) j.next(); - descriptor.setRepositoryProperty(key, props.getProperty(key)); + for (Entry<String, String> pe : props.entrySet()) { + ((SimpleArtifactDescriptor) descriptor).setRepositoryProperty(pe.getKey(), pe.getValue()); } } } @@ -273,15 +263,13 @@ public abstract class AbstractPublisherAction implements IPublisherAction { * @param info the publisher info supplying the advice */ protected static void processInstallableUnitPropertiesAdvice(InstallableUnitDescription iu, IPublisherInfo info) { - Collection advice = info.getAdvice(null, false, iu.getId(), iu.getVersion(), IPropertyAdvice.class); - for (Iterator i = advice.iterator(); i.hasNext();) { - IPropertyAdvice entry = (IPropertyAdvice) i.next(); - Properties props = entry.getInstallableUnitProperties(iu); + Collection<IPropertyAdvice> advice = info.getAdvice(null, false, iu.getId(), iu.getVersion(), IPropertyAdvice.class); + for (IPropertyAdvice entry : advice) { + Map<String, String> props = entry.getInstallableUnitProperties(iu); if (props == null) continue; - for (Iterator j = props.keySet().iterator(); j.hasNext();) { - String key = (String) j.next(); - iu.setProperty(key, props.getProperty(key)); + for (Entry<String, String> pe : props.entrySet()) { + iu.setProperty(pe.getKey(), pe.getValue()); } } } @@ -292,24 +280,32 @@ public abstract class AbstractPublisherAction implements IPublisherAction { * @param info the publisher info supplying the advice */ protected static void processCapabilityAdvice(InstallableUnitDescription iu, IPublisherInfo info) { - Collection advice = info.getAdvice(null, false, iu.getId(), iu.getVersion(), ICapabilityAdvice.class); + Collection<ICapabilityAdvice> advice = info.getAdvice(null, false, iu.getId(), iu.getVersion(), ICapabilityAdvice.class); if (advice.isEmpty()) return; - for (Iterator i = advice.iterator(); i.hasNext();) { - ICapabilityAdvice entry = (ICapabilityAdvice) i.next(); + for (ICapabilityAdvice entry : advice) { //process required capabilities - IRequiredCapability[] requiredAdvice = entry.getRequiredCapabilities(iu); + IRequirement[] requiredAdvice = entry.getRequiredCapabilities(iu); if (requiredAdvice != null) { - IRequiredCapability[] current = iu.getRequiredCapabilities(); - Set resultRequiredCapabilities = new HashSet(Arrays.asList(current)); + List<IRequirement> current = iu.getRequiredCapabilities(); + Set<IRequirement> resultRequiredCapabilities = new HashSet<IRequirement>(current); // remove current required capabilities that match (same name and namespace) advice. - for (int j = 0; j < current.length; j++) { - IRequiredCapability currentRequiredCapability = current[j]; + for (int j = 0; j < current.size(); j++) { + IRequirement curr = current.get(j); + IRequiredCapability currentRequiredCapability = null; + if (curr instanceof IRequiredCapability) + currentRequiredCapability = (IRequiredCapability) curr; + else + continue; for (int k = 0; k < requiredAdvice.length; k++) { - IRequiredCapability requiredCapability = requiredAdvice[k]; + IRequiredCapability requiredCapability = null; + if (requiredAdvice[k] instanceof IRequiredCapability) + requiredCapability = (IRequiredCapability) requiredAdvice[k]; + else + continue; if (requiredCapability.getNamespace().equals(currentRequiredCapability.getNamespace()) && requiredCapability.getName().equals(currentRequiredCapability.getName())) { resultRequiredCapabilities.remove(currentRequiredCapability); break; @@ -318,20 +314,28 @@ public abstract class AbstractPublisherAction implements IPublisherAction { } // add all advice resultRequiredCapabilities.addAll(Arrays.asList(requiredAdvice)); - iu.setRequiredCapabilities((IRequiredCapability[]) resultRequiredCapabilities.toArray(new IRequiredCapability[resultRequiredCapabilities.size()])); + iu.setRequiredCapabilities(resultRequiredCapabilities.toArray(new IRequirement[resultRequiredCapabilities.size()])); } //process meta required capabilities - IRequiredCapability[] metaRequiredAdvice = entry.getMetaRequiredCapabilities(iu); + IRequirement[] metaRequiredAdvice = entry.getMetaRequiredCapabilities(iu); if (metaRequiredAdvice != null) { - IRequiredCapability[] current = iu.getMetaRequiredCapabilities(); - Set resultMetaRequiredCapabilities = new HashSet(Arrays.asList(current)); + Collection<IRequirement> current = iu.getMetaRequiredCapabilities(); + Set<IRequirement> resultMetaRequiredCapabilities = new HashSet<IRequirement>(current); // remove current meta-required capabilities that match (same name and namespace) advice. - for (int j = 0; j < current.length; j++) { - IRequiredCapability currentMetaRequiredCapability = current[j]; + for (IRequirement curr : current) { + IRequiredCapability currentMetaRequiredCapability = null; + if (curr instanceof IRequiredCapability) + currentMetaRequiredCapability = (IRequiredCapability) curr; + else + continue; for (int k = 0; k < metaRequiredAdvice.length; k++) { - IRequiredCapability metaRequiredCapability = metaRequiredAdvice[k]; + IRequiredCapability metaRequiredCapability = null; + if (metaRequiredAdvice[k] instanceof IRequiredCapability) + metaRequiredCapability = (IRequiredCapability) metaRequiredAdvice[k]; + else + continue; if (metaRequiredCapability.getNamespace().equals(currentMetaRequiredCapability.getNamespace()) && metaRequiredCapability.getName().equals(currentMetaRequiredCapability.getName())) { resultMetaRequiredCapabilities.remove(currentMetaRequiredCapability); break; @@ -341,16 +345,15 @@ public abstract class AbstractPublisherAction implements IPublisherAction { // add all advice resultMetaRequiredCapabilities.addAll(Arrays.asList(metaRequiredAdvice)); - iu.setMetaRequiredCapabilities((IRequiredCapability[]) resultMetaRequiredCapabilities.toArray(new IRequiredCapability[resultMetaRequiredCapabilities.size()])); + iu.setMetaRequiredCapabilities(resultMetaRequiredCapabilities.toArray(new IRequirement[resultMetaRequiredCapabilities.size()])); } //process provided capabilities IProvidedCapability[] providedAdvice = entry.getProvidedCapabilities(iu); if (providedAdvice != null) { - IProvidedCapability[] current = iu.getProvidedCapabilities(); - Set resultProvidedCapabilities = new HashSet(Arrays.asList(current)); - for (int j = 0; j < current.length; j++) { - IProvidedCapability currentProvidedCapability = current[j]; + Collection<IProvidedCapability> current = iu.getProvidedCapabilities(); + Set<IProvidedCapability> resultProvidedCapabilities = new HashSet<IProvidedCapability>(current); + for (IProvidedCapability currentProvidedCapability : current) { for (int k = 0; k < providedAdvice.length; k++) { IProvidedCapability providedCapability = providedAdvice[k]; if (providedCapability.getNamespace().equals(currentProvidedCapability.getNamespace()) && providedCapability.getName().equals(currentProvidedCapability.getName())) { @@ -360,7 +363,7 @@ public abstract class AbstractPublisherAction implements IPublisherAction { } } resultProvidedCapabilities.addAll(Arrays.asList(providedAdvice)); - iu.setCapabilities((IProvidedCapability[]) resultProvidedCapabilities.toArray(new IProvidedCapability[resultProvidedCapabilities.size()])); + iu.setCapabilities(resultProvidedCapabilities.toArray(new IProvidedCapability[resultProvidedCapabilities.size()])); } } } @@ -371,19 +374,23 @@ public abstract class AbstractPublisherAction implements IPublisherAction { * @param currentInstructions The set of touchpoint instructions assembled for this IU so far * @param info The publisher info */ - protected static void processTouchpointAdvice(InstallableUnitDescription iu, Map currentInstructions, IPublisherInfo info) { - Collection advice = info.getAdvice(null, false, iu.getId(), iu.getVersion(), ITouchpointAdvice.class); + protected static void processTouchpointAdvice(InstallableUnitDescription iu, Map<String, ? extends Object> currentInstructions, IPublisherInfo info) { + processTouchpointAdvice(iu, currentInstructions, info, null); + } + + protected static void processTouchpointAdvice(InstallableUnitDescription iu, Map<String, ? extends Object> currentInstructions, IPublisherInfo info, String configSpec) { + Collection<ITouchpointAdvice> advice = info.getAdvice(configSpec, false, iu.getId(), iu.getVersion(), ITouchpointAdvice.class); if (currentInstructions == null) { - if (advice.isEmpty()) + if (advice == null || advice.isEmpty()) return; - - currentInstructions = Collections.EMPTY_MAP; + currentInstructions = CollectionUtils.emptyMap(); } ITouchpointData result = MetadataFactory.createTouchpointData(currentInstructions); - for (Iterator i = advice.iterator(); i.hasNext();) { - ITouchpointAdvice entry = (ITouchpointAdvice) i.next(); - result = entry.getTouchpointData(result); + if (advice != null) { + for (ITouchpointAdvice entry : advice) { + result = entry.getTouchpointData(result); + } } iu.addTouchpointData(result); } @@ -499,26 +506,24 @@ public abstract class AbstractPublisherAction implements IPublisherAction { * @return the first matching IU or <code>null</code> if none. */ protected IInstallableUnit queryForIU(IPublisherResult publisherResult, String iuId, Version version) { - Query query = null; - Collector collector = null; + IQuery<IInstallableUnit> query = null; + IQueryResult<IInstallableUnit> collector = Collector.emptyCollector(); if (version != null && !Version.emptyVersion.equals(version)) { - query = new InstallableUnitQuery(iuId, version); - collector = new SingleElementCollector(); + query = new LimitQuery<IInstallableUnit>(new InstallableUnitQuery(iuId, version), 1); } else { - query = new CompositeQuery(new Query[] {new InstallableUnitQuery(iuId), new LatestIUVersionQuery()}); - collector = new Collector(); + query = new PipedQuery<IInstallableUnit>(new InstallableUnitQuery(iuId), new LatestIUVersionQuery<IInstallableUnit>()); } NullProgressMonitor progress = new NullProgressMonitor(); if (publisherResult != null) - collector = publisherResult.query(query, collector, progress); + collector = publisherResult.query(query, progress); if (collector.isEmpty() && info.getMetadataRepository() != null) - collector = info.getMetadataRepository().query(query, collector, progress); + collector = info.getMetadataRepository().query(query, progress); if (collector.isEmpty() && info.getContextMetadataRepository() != null) - collector = info.getContextMetadataRepository().query(query, collector, progress); + collector = info.getContextMetadataRepository().query(query, progress); if (!collector.isEmpty()) - return (IInstallableUnit) collector.iterator().next(); + return collector.iterator().next(); return null; } @@ -529,21 +534,18 @@ public abstract class AbstractPublisherAction implements IPublisherAction { * @param versionRange the version range to consider * @return The the IUs with the matching ids in the given range */ - protected IInstallableUnit[] queryForIUs(IPublisherResult publisherResult, String iuId, VersionRange versionRange) { - Query query = null; - Collector collector = new Collector(); + protected IQueryResult<IInstallableUnit> queryForIUs(IPublisherResult publisherResult, String iuId, VersionRange versionRange) { + IQuery<IInstallableUnit> query = null; + IQueryResult<IInstallableUnit> queryResult = Collector.emptyCollector(); query = new InstallableUnitQuery(iuId, versionRange); NullProgressMonitor progress = new NullProgressMonitor(); if (publisherResult != null) - collector = publisherResult.query(query, collector, progress); - if (collector.isEmpty() && info.getMetadataRepository() != null) - collector = info.getMetadataRepository().query(query, collector, progress); - if (collector.isEmpty() && info.getContextMetadataRepository() != null) - collector = info.getContextMetadataRepository().query(query, collector, progress); - - if (!collector.isEmpty()) - return (IInstallableUnit[]) collector.toArray(IInstallableUnit.class); - return new IInstallableUnit[0]; + queryResult = publisherResult.query(query, progress); + if (queryResult.isEmpty() && info.getMetadataRepository() != null) + queryResult = info.getMetadataRepository().query(query, progress); + if (queryResult.isEmpty() && info.getContextMetadataRepository() != null) + queryResult = info.getContextMetadataRepository().query(query, progress); + return queryResult; } public abstract IStatus perform(IPublisherInfo publisherInfo, IPublisherResult results, IProgressMonitor monitor); diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AbstractPublisherApplication.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AbstractPublisherApplication.java index d731d785b..8b6e0dfc0 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AbstractPublisherApplication.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/AbstractPublisherApplication.java @@ -17,22 +17,15 @@ import java.net.URISyntaxException; import org.eclipse.core.runtime.*; import org.eclipse.equinox.app.IApplication; import org.eclipse.equinox.app.IApplicationContext; -import org.eclipse.equinox.internal.p2.artifact.repository.ArtifactRepositoryManager; import org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository; -import org.eclipse.equinox.internal.p2.core.ProvisioningEventBus; -import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; import org.eclipse.equinox.internal.p2.metadata.repository.CompositeMetadataRepository; -import org.eclipse.equinox.internal.p2.metadata.repository.MetadataRepositoryManager; import org.eclipse.equinox.internal.p2.publisher.Activator; import org.eclipse.equinox.internal.p2.publisher.Messages; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepository; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepositoryManager; -import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException; -import org.eclipse.equinox.internal.provisional.p2.core.eventbus.IProvisioningEventBus; -import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository; -import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager; +import org.eclipse.equinox.p2.core.*; +import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; +import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; import org.eclipse.osgi.util.NLS; -import org.osgi.framework.ServiceRegistration; +import org.osgi.framework.ServiceReference; public abstract class AbstractPublisherApplication implements IApplication { @@ -47,12 +40,6 @@ public abstract class AbstractPublisherApplication implements IApplication { static final public String PUBLISH_PACK_FILES_AS_SIBLINGS = "publishPackFilesAsSiblings"; //$NON-NLS-1$ - private ArtifactRepositoryManager defaultArtifactManager; - private ServiceRegistration registrationDefaultArtifactManager; - private MetadataRepositoryManager defaultMetadataManager; - private ServiceRegistration registrationDefaultMetadataManager; - private IProvisioningEventBus bus; - private ServiceRegistration registrationBus; protected PublisherInfo info; protected String source; protected URI metadataLocation; @@ -69,6 +56,10 @@ public abstract class AbstractPublisherApplication implements IApplication { protected String[] configurations; private IStatus status; + private ServiceReference agentRef; + + private IProvisioningAgent agent; + /** * Returns the error message for this application, or the empty string * if the application terminated successfully. @@ -95,15 +86,15 @@ public abstract class AbstractPublisherApplication implements IApplication { protected void initializeRepositories(PublisherInfo publisherInfo) throws ProvisionException { if (artifactLocation != null) - publisherInfo.setArtifactRepository(Publisher.createArtifactRepository(artifactLocation, artifactRepoName, append, compress, reusePackedFiles)); + publisherInfo.setArtifactRepository(Publisher.createArtifactRepository(agent, artifactLocation, artifactRepoName, append, compress, reusePackedFiles)); else if ((publisherInfo.getArtifactOptions() & IPublisherInfo.A_PUBLISH) > 0) throw new ProvisionException(createConfigurationEror(Messages.exception_noArtifactRepo)); if (metadataLocation == null) throw new ProvisionException(createConfigurationEror(Messages.exception_noMetadataRepo)); - publisherInfo.setMetadataRepository(Publisher.createMetadataRepository(metadataLocation, metadataRepoName, append, compress)); + publisherInfo.setMetadataRepository(Publisher.createMetadataRepository(agent, metadataLocation, metadataRepoName, append, compress)); if (contextMetadataRepositories != null && contextMetadataRepositories.length > 0) { - CompositeMetadataRepository contextMetadata = CompositeMetadataRepository.createMemoryComposite(); + CompositeMetadataRepository contextMetadata = CompositeMetadataRepository.createMemoryComposite(agent); if (contextMetadata != null) { for (int i = 0; i < contextMetadataRepositories.length; i++) contextMetadata.addChild(contextMetadataRepositories[i]); @@ -112,7 +103,7 @@ public abstract class AbstractPublisherApplication implements IApplication { } } if (contextArtifactRepositories != null && contextArtifactRepositories.length > 0) { - CompositeArtifactRepository contextArtifact = CompositeArtifactRepository.createMemoryComposite(); + CompositeArtifactRepository contextArtifact = CompositeArtifactRepository.createMemoryComposite(agent); if (contextArtifact != null) { for (int i = 0; i < contextArtifactRepositories.length; i++) contextArtifact.addChild(contextArtifactRepositories[i]); @@ -178,7 +169,7 @@ public abstract class AbstractPublisherApplication implements IApplication { if (list == null || list.length == 0) return null; - CompositeArtifactRepository result = CompositeArtifactRepository.createMemoryComposite(); + CompositeArtifactRepository result = CompositeArtifactRepository.createMemoryComposite(agent); if (result != null) { for (int i = 0; i < list.length; i++) { try { @@ -197,7 +188,7 @@ public abstract class AbstractPublisherApplication implements IApplication { if (list == null || list.length == 0) return null; - CompositeMetadataRepository result = CompositeMetadataRepository.createMemoryComposite(); + CompositeMetadataRepository result = CompositeMetadataRepository.createMemoryComposite(agent); if (result != null) { for (int i = 0; i < list.length; i++) { try { @@ -233,25 +224,22 @@ public abstract class AbstractPublisherApplication implements IApplication { inplace = true; } - private void registerDefaultArtifactRepoManager() { - if (ServiceHelper.getService(Activator.getContext(), IArtifactRepositoryManager.class.getName()) == null) { - defaultArtifactManager = new ArtifactRepositoryManager(); - registrationDefaultArtifactManager = Activator.getContext().registerService(IArtifactRepositoryManager.class.getName(), defaultArtifactManager, null); - } - } - - private void registerDefaultMetadataRepoManager() { - if (ServiceHelper.getService(Activator.getContext(), IMetadataRepositoryManager.class.getName()) == null) { - defaultMetadataManager = new MetadataRepositoryManager(); - registrationDefaultMetadataManager = Activator.getContext().registerService(IMetadataRepositoryManager.class.getName(), defaultMetadataManager, null); - } - } - - private void registerEventBus() { - if (ServiceHelper.getService(Activator.getContext(), IProvisioningEventBus.SERVICE_NAME) == null) { - bus = new ProvisioningEventBus(); - registrationBus = Activator.getContext().registerService(IProvisioningEventBus.SERVICE_NAME, bus, null); + private void setupAgent() throws ProvisionException { + agentRef = Activator.getContext().getServiceReference(IProvisioningAgent.SERVICE_NAME); + if (agentRef != null) { + agent = (IProvisioningAgent) Activator.getContext().getService(agentRef); + if (agent != null) + return; } + ServiceReference providerRef = Activator.getContext().getServiceReference(IProvisioningAgentProvider.SERVICE_NAME); + if (providerRef == null) + throw new RuntimeException("No provisioning agent provider is available"); //$NON-NLS-1$ + IProvisioningAgentProvider provider = (IProvisioningAgentProvider) Activator.getContext().getService(providerRef); + if (provider == null) + throw new RuntimeException("No provisioning agent provider is available"); //$NON-NLS-1$ + //obtain agent for currently running system + agent = provider.createAgent(null); + Activator.getContext().ungetService(providerRef); } public Object run(String args[]) throws Exception { @@ -279,9 +267,7 @@ public abstract class AbstractPublisherApplication implements IApplication { public Object run(PublisherInfo publisherInfo) throws Exception { try { this.info = publisherInfo; - registerEventBus(); - registerDefaultMetadataRepoManager(); - registerDefaultArtifactRepoManager(); + setupAgent(); initialize(publisherInfo); System.out.println(NLS.bind(Messages.message_generatingMetadata, publisherInfo.getSummary())); @@ -316,17 +302,9 @@ public abstract class AbstractPublisherApplication implements IApplication { } public void stop() { - if (registrationDefaultMetadataManager != null) { - registrationDefaultMetadataManager.unregister(); - registrationDefaultMetadataManager = null; - } - if (registrationDefaultArtifactManager != null) { - registrationDefaultArtifactManager.unregister(); - registrationDefaultArtifactManager = null; - } - if (registrationBus != null) { - registrationBus.unregister(); - registrationBus = null; + if (agentRef != null) { + Activator.getContext().ungetService(agentRef); + agentRef = null; } } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/IPublisherAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/IPublisherAdvice.java index 6ee425c54..a0777d456 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/IPublisherAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/IPublisherAdvice.java @@ -9,7 +9,7 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; +import org.eclipse.equinox.p2.metadata.Version; public interface IPublisherAdvice { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/IPublisherInfo.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/IPublisherInfo.java index d4515e003..e804e2d08 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/IPublisherInfo.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/IPublisherInfo.java @@ -10,11 +10,10 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; - import java.util.Collection; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepository; -import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository; +import org.eclipse.equinox.p2.metadata.Version; +import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; +import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; public interface IPublisherInfo { @@ -66,7 +65,7 @@ public interface IPublisherInfo { * @param includeDefault whether or not to merge in the advice common to all configurations * @return the set of advice of the given type for the given configuration */ - public Collection getAdvice(String configSpec, boolean includeDefault, String id, Version version, Class type); + public <T extends IPublisherAdvice> Collection<T> getAdvice(String configSpec, boolean includeDefault, String id, Version version, Class<T> type); /** * Add the given advice to the set of publishing advices. diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/IPublisherResult.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/IPublisherResult.java index 0bf3d9ca6..44a56d4b0 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/IPublisherResult.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/IPublisherResult.java @@ -10,11 +10,10 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; - import java.util.Collection; -import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; -import org.eclipse.equinox.internal.provisional.p2.metadata.query.IQueryable; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; +import org.eclipse.equinox.p2.metadata.Version; +import org.eclipse.equinox.p2.query.IQueryable; /** * Publisher results represent the result of running a publishing operation. A result is a @@ -23,7 +22,7 @@ import org.eclipse.equinox.internal.provisional.p2.metadata.query.IQueryable; * determined by the actions involved in the operation and it is up to the consumer of the * result to interpret the collections. */ -public interface IPublisherResult extends IQueryable { +public interface IPublisherResult extends IQueryable<IInstallableUnit> { /** * Merge mode setting that causes all root results to be merged into * the root of the merged results and all non-roots to become non-roots. @@ -70,7 +69,7 @@ public interface IPublisherResult extends IQueryable { * @param ius the IUs to add * @param type the type of the IUs in this result */ - public void addIUs(Collection ius, String type); + public void addIUs(Collection<IInstallableUnit> ius, String type); /** * Returns the IUs of the given type with the given id in this result. @@ -80,7 +79,7 @@ public interface IPublisherResult extends IQueryable { * @see #ROOT * @see #NON_ROOT */ - public Collection getIUs(String id, String type); + public Collection<IInstallableUnit> getIUs(String id, String type); /** * Returns the first available IU of the given type with the given id in this result. diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/Publisher.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/Publisher.java index 19467b970..fefe3cec6 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/Publisher.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/Publisher.java @@ -13,17 +13,17 @@ package org.eclipse.equinox.p2.publisher; import java.net.URI; import java.util.Collection; import org.eclipse.core.runtime.*; -import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; import org.eclipse.equinox.internal.p2.core.helpers.Tracing; import org.eclipse.equinox.internal.p2.publisher.Activator; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepository; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepositoryManager; -import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException; -import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; -import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository; -import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepositoryManager; -import org.eclipse.equinox.internal.provisional.p2.repository.IRepository; -import org.eclipse.equinox.internal.provisional.p2.repository.IRepositoryManager; +import org.eclipse.equinox.p2.core.IProvisioningAgent; +import org.eclipse.equinox.p2.core.ProvisionException; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; +import org.eclipse.equinox.p2.repository.IRepository; +import org.eclipse.equinox.p2.repository.IRepositoryManager; +import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; +import org.eclipse.equinox.p2.repository.artifact.IArtifactRepositoryManager; +import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; +import org.eclipse.equinox.p2.repository.metadata.IMetadataRepositoryManager; public class Publisher { static final public String PUBLISH_PACK_FILES_AS_SIBLINGS = "publishPackFilesAsSiblings"; //$NON-NLS-1$ @@ -42,9 +42,9 @@ public class Publisher { * @return the discovered or created repository * @throws ProvisionException */ - public static IMetadataRepository createMetadataRepository(URI location, String name, boolean append, boolean compress) throws ProvisionException { + public static IMetadataRepository createMetadataRepository(IProvisioningAgent agent, URI location, String name, boolean append, boolean compress) throws ProvisionException { try { - IMetadataRepository result = loadMetadataRepository(location, true, true); + IMetadataRepository result = loadMetadataRepository(agent, location, true, true); if (result != null && result.isModifiable()) { result.setProperty(IRepository.PROP_COMPRESSED, compress ? "true" : "false"); //$NON-NLS-1$//$NON-NLS-2$ if (!append) @@ -56,7 +56,7 @@ public class Publisher { } // the given repo location is not an existing repo so we have to create something - IMetadataRepositoryManager manager = (IMetadataRepositoryManager) ServiceHelper.getService(Activator.context, IMetadataRepositoryManager.class.getName()); + IMetadataRepositoryManager manager = (IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME); String repositoryName = name == null ? location + " - metadata" : name; //$NON-NLS-1$ IMetadataRepository result = manager.createRepository(location, repositoryName, IMetadataRepositoryManager.TYPE_SIMPLE_REPOSITORY, null); if (result != null) { @@ -77,8 +77,8 @@ public class Publisher { * @return the loaded repository * @throws ProvisionException */ - public static IMetadataRepository loadMetadataRepository(URI location, boolean modifiable, boolean removeFromManager) throws ProvisionException { - IMetadataRepositoryManager manager = (IMetadataRepositoryManager) ServiceHelper.getService(Activator.context, IMetadataRepositoryManager.class.getName()); + public static IMetadataRepository loadMetadataRepository(IProvisioningAgent agent, URI location, boolean modifiable, boolean removeFromManager) throws ProvisionException { + IMetadataRepositoryManager manager = (IMetadataRepositoryManager) agent.getService(IMetadataRepositoryManager.SERVICE_NAME); boolean existing = manager.contains(location); IMetadataRepository result = manager.loadRepository(location, modifiable ? IRepositoryManager.REPOSITORY_HINT_MODIFIABLE : 0, null); @@ -99,9 +99,9 @@ public class Publisher { * @return the discovered or created repository * @throws ProvisionException */ - public static IArtifactRepository createArtifactRepository(URI location, String name, boolean append, boolean compress, boolean reusePackedFiles) throws ProvisionException { + public static IArtifactRepository createArtifactRepository(IProvisioningAgent agent, URI location, String name, boolean append, boolean compress, boolean reusePackedFiles) throws ProvisionException { try { - IArtifactRepository result = loadArtifactRepository(location, true, true); + IArtifactRepository result = loadArtifactRepository(agent, location, true, true); if (result != null && result.isModifiable()) { result.setProperty(IRepository.PROP_COMPRESSED, compress ? "true" : "false"); //$NON-NLS-1$//$NON-NLS-2$ if (reusePackedFiles) @@ -114,7 +114,7 @@ public class Publisher { //fall through and create a new repository } - IArtifactRepositoryManager manager = (IArtifactRepositoryManager) ServiceHelper.getService(Activator.context, IArtifactRepositoryManager.class.getName()); + IArtifactRepositoryManager manager = (IArtifactRepositoryManager) agent.getService(IArtifactRepositoryManager.SERVICE_NAME); String repositoryName = name != null ? name : location + " - artifacts"; //$NON-NLS-1$ IArtifactRepository result = manager.createRepository(location, repositoryName, IArtifactRepositoryManager.TYPE_SIMPLE_REPOSITORY, null); if (result != null) { @@ -137,8 +137,8 @@ public class Publisher { * @return the loaded repository * @throws ProvisionException */ - public static IArtifactRepository loadArtifactRepository(URI location, boolean modifiable, boolean removeFromManager) throws ProvisionException { - IArtifactRepositoryManager manager = (IArtifactRepositoryManager) ServiceHelper.getService(Activator.context, IArtifactRepositoryManager.class.getName()); + public static IArtifactRepository loadArtifactRepository(IProvisioningAgent agent, URI location, boolean modifiable, boolean removeFromManager) throws ProvisionException { + IArtifactRepositoryManager manager = (IArtifactRepositoryManager) agent.getService(IArtifactRepositoryManager.SERVICE_NAME); boolean existing = manager.contains(location); IArtifactRepository result = manager.loadRepository(location, modifiable ? IRepositoryManager.REPOSITORY_HINT_MODIFIABLE : 0, null); @@ -183,8 +183,8 @@ public class Publisher { // if there were no errors, publish all the ius. IMetadataRepository metadataRepository = info.getMetadataRepository(); if (metadataRepository != null) { - Collection ius = results.getIUs(null, null); - metadataRepository.addInstallableUnits((IInstallableUnit[]) ius.toArray(new IInstallableUnit[ius.size()])); + Collection<IInstallableUnit> ius = results.getIUs(null, null); + metadataRepository.addInstallableUnits(ius.toArray(new IInstallableUnit[ius.size()])); } return Status.OK_STATUS; } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/PublisherInfo.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/PublisherInfo.java index bee4129f0..a72a78a7b 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/PublisherInfo.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/PublisherInfo.java @@ -11,9 +11,9 @@ package org.eclipse.equinox.p2.publisher; import java.util.*; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactRepository; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; -import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository; +import org.eclipse.equinox.p2.metadata.Version; +import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; +import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; public class PublisherInfo implements IPublisherInfo { @@ -23,18 +23,23 @@ public class PublisherInfo implements IPublisherInfo { private IMetadataRepository contextMetadataRepository; private IArtifactRepository contextArtifactRepository; private String[] configurations = new String[0]; - private List adviceList = new ArrayList(11); + private List<IPublisherAdvice> adviceList = new ArrayList<IPublisherAdvice>(11); public void addAdvice(IPublisherAdvice advice) { adviceList.add(advice); } - public Collection getAdvice(String configSpec, boolean includeDefault, String id, Version version, Class type) { - ArrayList result = new ArrayList(); - for (Iterator i = adviceList.iterator(); i.hasNext();) { - Object object = i.next(); - if (type.isInstance(object) && ((IPublisherAdvice) object).isApplicable(configSpec, includeDefault, id, version)) - result.add(object); + public List<IPublisherAdvice> getAdvice() { + return adviceList; + } + + @SuppressWarnings("unchecked") + public <T extends IPublisherAdvice> Collection<T> getAdvice(String configSpec, boolean includeDefault, String id, Version version, Class<T> type) { + ArrayList<T> result = new ArrayList<T>(); + for (IPublisherAdvice advice : adviceList) { + if (type.isInstance(advice) && advice.isApplicable(configSpec, includeDefault, id, version)) + // Ideally, we would use Class.cast here but it was introduced in Java 1.5 + result.add((T) advice); } return result; } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/PublisherResult.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/PublisherResult.java index 6428eda8a..ae358d85b 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/PublisherResult.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/PublisherResult.java @@ -12,15 +12,15 @@ package org.eclipse.equinox.p2.publisher; import java.util.*; import org.eclipse.core.runtime.IProgressMonitor; -import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; -import org.eclipse.equinox.internal.provisional.p2.metadata.query.*; +import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils; +import org.eclipse.equinox.p2.metadata.*; +import org.eclipse.equinox.p2.metadata.query.InstallableUnitQuery; +import org.eclipse.equinox.p2.query.*; public class PublisherResult implements IPublisherResult { - private static final Collection EMPTY_COLLECTION = new ArrayList(0); - final Map rootIUs = new HashMap(); - final Map nonRootIUs = new HashMap(); + final Map<String, Set<IInstallableUnit>> rootIUs = new HashMap<String, Set<IInstallableUnit>>(); + final Map<String, Set<IInstallableUnit>> nonRootIUs = new HashMap<String, Set<IInstallableUnit>>(); public void addIU(IInstallableUnit iu, String type) { if (type == ROOT) @@ -29,15 +29,15 @@ public class PublisherResult implements IPublisherResult { addIU(nonRootIUs, iu.getId(), iu); } - public void addIUs(Collection ius, String type) { - for (Iterator i = ius.iterator(); i.hasNext();) - addIU((IInstallableUnit) i.next(), type); + public void addIUs(Collection<IInstallableUnit> ius, String type) { + for (IInstallableUnit iu : ius) + addIU(iu, type); } - private void addIU(Map map, String id, IInstallableUnit iu) { - Set ius = (Set) map.get(id); + private void addIU(Map<String, Set<IInstallableUnit>> map, String id, IInstallableUnit iu) { + Set<IInstallableUnit> ius = map.get(id); if (ius == null) { - ius = new HashSet(11); + ius = new HashSet<IInstallableUnit>(11); map.put(id, ius); } ius.add(iu); @@ -45,17 +45,15 @@ public class PublisherResult implements IPublisherResult { public IInstallableUnit getIU(String id, Version version, String type) { if (type == null || type == ROOT) { - Collection ius = (Collection) rootIUs.get(id); - for (Iterator i = ius.iterator(); i.hasNext();) { - IInstallableUnit iu = (IInstallableUnit) i.next(); + Collection<IInstallableUnit> ius = rootIUs.get(id); + for (IInstallableUnit iu : ius) { if (iu.getVersion().equals(version)) return iu; } } if (type == null || type == NON_ROOT) { - Collection ius = (Collection) nonRootIUs.get(id); - for (Iterator i = ius.iterator(); i.hasNext();) { - IInstallableUnit iu = (IInstallableUnit) i.next(); + Collection<IInstallableUnit> ius = nonRootIUs.get(id); + for (IInstallableUnit iu : ius) { if (iu.getVersion().equals(version)) return iu; } @@ -67,14 +65,14 @@ public class PublisherResult implements IPublisherResult { // matching IU non-deterministically. public IInstallableUnit getIU(String id, String type) { if (type == null || type == ROOT) { - Collection ius = (Collection) rootIUs.get(id); + Collection<IInstallableUnit> ius = rootIUs.get(id); if (ius != null && ius.size() > 0) - return (IInstallableUnit) ius.iterator().next(); + return ius.iterator().next(); } if (type == null || type == NON_ROOT) { - Collection ius = (Collection) nonRootIUs.get(id); + Collection<IInstallableUnit> ius = nonRootIUs.get(id); if (ius != null && ius.size() > 0) - return (IInstallableUnit) ius.iterator().next(); + return ius.iterator().next(); } return null; } @@ -82,30 +80,30 @@ public class PublisherResult implements IPublisherResult { /** * Returns the IUs in this result with the given id. */ - public Collection getIUs(String id, String type) { + public Collection<IInstallableUnit> getIUs(String id, String type) { if (type == null) { - ArrayList result = new ArrayList(); + ArrayList<IInstallableUnit> result = new ArrayList<IInstallableUnit>(); result.addAll(id == null ? flatten(rootIUs.values()) : getIUs(rootIUs, id)); result.addAll(id == null ? flatten(nonRootIUs.values()) : getIUs(nonRootIUs, id)); return result; } if (type == ROOT) - return id == null ? flatten(rootIUs.values()) : (Collection) rootIUs.get(id); + return id == null ? flatten(rootIUs.values()) : rootIUs.get(id); if (type == NON_ROOT) - return id == null ? flatten(nonRootIUs.values()) : (Collection) nonRootIUs.get(id); + return id == null ? flatten(nonRootIUs.values()) : nonRootIUs.get(id); return null; } - private Collection getIUs(Map ius, String id) { - Collection result = (Collection) ius.get(id); - return result == null ? EMPTY_COLLECTION : result; + private Collection<IInstallableUnit> getIUs(Map<String, Set<IInstallableUnit>> ius, String id) { + Collection<IInstallableUnit> result = ius.get(id); + return result == null ? CollectionUtils.<IInstallableUnit> emptyList() : result; } - protected List flatten(Collection values) { - ArrayList result = new ArrayList(); - for (Iterator i = values.iterator(); i.hasNext();) - for (Iterator j = ((HashSet) i.next()).iterator(); j.hasNext();) - result.add(j.next()); + protected List<IInstallableUnit> flatten(Collection<Set<IInstallableUnit>> values) { + ArrayList<IInstallableUnit> result = new ArrayList<IInstallableUnit>(); + for (Set<IInstallableUnit> iuSet : values) + for (IInstallableUnit iu : iuSet) + result.add(iu); return result; } @@ -122,24 +120,39 @@ public class PublisherResult implements IPublisherResult { } } - class QueryableMap implements IQueryable { - private Map map; + class QueryableMap implements IQueryable<IInstallableUnit> { + private Map<String, Set<IInstallableUnit>> map; - public QueryableMap(Map map) { + public QueryableMap(Map<String, Set<IInstallableUnit>> map) { this.map = map; } - public Collector query(Query query, Collector collector, IProgressMonitor monitor) { - return query.perform(flatten(this.map.values()).iterator(), collector); + public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IProgressMonitor monitor) { + return query.perform(flatten(this.map.values()).iterator()); } } /** * Queries both the root and non root IUs */ - public Collector query(Query query, Collector collector, IProgressMonitor monitor) { - IQueryable nonRootQueryable = new QueryableMap(nonRootIUs); - IQueryable rootQueryable = new QueryableMap(rootIUs); - return new CompoundQueryable(new IQueryable[] {nonRootQueryable, rootQueryable}).query(query, collector, monitor); + public IQueryResult<IInstallableUnit> query(IQuery<IInstallableUnit> query, IProgressMonitor monitor) { + //optimize for installable unit query + if (query instanceof InstallableUnitQuery) + return queryIU((InstallableUnitQuery) query, monitor); + IQueryable<IInstallableUnit> nonRootQueryable = new QueryableMap(nonRootIUs); + IQueryable<IInstallableUnit> rootQueryable = new QueryableMap(rootIUs); + return new CompoundQueryable<IInstallableUnit>(nonRootQueryable, rootQueryable).query(query, monitor); + } + + private IQueryResult<IInstallableUnit> queryIU(InstallableUnitQuery query, IProgressMonitor monitor) { + Collector<IInstallableUnit> result = new Collector<IInstallableUnit>(); + Collection<IInstallableUnit> matches = getIUs(query.getId(), null); + VersionRange queryRange = query.getRange(); + for (IInstallableUnit match : matches) { + if (queryRange == null || queryRange.isIncluded(match.getVersion())) + if (!result.accept(match)) + break; + } + return result; } } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IAdditionalInstallableUnitAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IAdditionalInstallableUnitAdvice.java index a853d13be..1e5459442 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IAdditionalInstallableUnitAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IAdditionalInstallableUnitAdvice.java @@ -10,8 +10,8 @@ *******************************************************************************/ package org.eclipse.equinox.p2.publisher.actions; -import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; import org.eclipse.equinox.p2.publisher.IPublisherAdvice; public interface IAdditionalInstallableUnitAdvice extends IPublisherAdvice { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/ICapabilityAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/ICapabilityAdvice.java index 93c390214..8615d72a2 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/ICapabilityAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/ICapabilityAdvice.java @@ -11,16 +11,16 @@ *******************************************************************************/ package org.eclipse.equinox.p2.publisher.actions; -import org.eclipse.equinox.internal.provisional.p2.metadata.IProvidedCapability; -import org.eclipse.equinox.internal.provisional.p2.metadata.IRequiredCapability; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; +import org.eclipse.equinox.p2.metadata.IProvidedCapability; +import org.eclipse.equinox.p2.metadata.IRequirement; import org.eclipse.equinox.p2.publisher.IPublisherAdvice; public interface ICapabilityAdvice extends IPublisherAdvice { public IProvidedCapability[] getProvidedCapabilities(InstallableUnitDescription iu); - public IRequiredCapability[] getRequiredCapabilities(InstallableUnitDescription iu); + public IRequirement[] getRequiredCapabilities(InstallableUnitDescription iu); - public IRequiredCapability[] getMetaRequiredCapabilities(InstallableUnitDescription iu); + public IRequirement[] getMetaRequiredCapabilities(InstallableUnitDescription iu); } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IFeatureRootAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IFeatureRootAdvice.java index 7a8e448b0..732826f68 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IFeatureRootAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IFeatureRootAdvice.java @@ -1,5 +1,5 @@ /******************************************************************************* - * Copyright (c) 2009 IBM Corporation and others. + * Copyright (c) 2009, 2010 IBM Corporation and others. * 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 @@ -8,7 +8,6 @@ * Contributors: * IBM Corporation - initial API and implementation *******************************************************************************/ - package org.eclipse.equinox.p2.publisher.actions; import org.eclipse.equinox.internal.p2.core.helpers.FileUtils.IPathComputer; @@ -19,7 +18,7 @@ public interface IFeatureRootAdvice extends IPublisherAdvice { /** * Return an array of configSpecs for which this advice is applicable. - * @see IPublisherAdvice#isApplicable(String, boolean, String, org.eclipse.equinox.internal.provisional.p2.metadata.Version) + * @see IPublisherAdvice#isApplicable(String, boolean, String, org.eclipse.equinox.p2.metadata.Version) * @return String [] : Array of configSpec ("ws,os,arch") */ public String[] getConfigurations(); diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IFilterAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IFilterAdvice.java index 55687f58e..8e7d376ad 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IFilterAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IFilterAdvice.java @@ -9,9 +9,9 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher.actions; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; - +import org.eclipse.equinox.p2.metadata.Version; import org.eclipse.equinox.p2.publisher.IPublisherAdvice; +import org.osgi.framework.Filter; /** * Filter advice helps actions figure out where an IU with a given id and version @@ -35,6 +35,6 @@ public interface IFilterAdvice extends IPublisherAdvice { * @return the filter to use when depending on the given IU or <code>null</code> * if none. */ - public String getFilter(String id, Version version, boolean exact); + public Filter getFilter(String id, Version version, boolean exact); } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/ILicenseAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/ILicenseAdvice.java index 03868e07f..a6deae374 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/ILicenseAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/ILicenseAdvice.java @@ -9,10 +9,12 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher.actions; +import org.eclipse.equinox.p2.publisher.IPublisherAdvice; + /** * Provides advice about a license. */ -public interface ILicenseAdvice { +public interface ILicenseAdvice extends IPublisherAdvice { public String getLicenseURL(); diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IPropertyAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IPropertyAdvice.java index 86a03671d..18ef45493 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IPropertyAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IPropertyAdvice.java @@ -10,22 +10,22 @@ *******************************************************************************/ package org.eclipse.equinox.p2.publisher.actions; -import java.util.Properties; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactDescriptor; -import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; +import java.util.Map; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; import org.eclipse.equinox.p2.publisher.IPublisherAdvice; +import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; public interface IPropertyAdvice extends IPublisherAdvice { /** * Returns the set of extra properties to be associated with the IU */ - public Properties getInstallableUnitProperties(InstallableUnitDescription iu); + public Map<String, String> getInstallableUnitProperties(InstallableUnitDescription iu); /** * Returns the set of extra properties to be associated with the artifact descriptor * being published */ - public Properties getArtifactProperties(IInstallableUnit iu, IArtifactDescriptor descriptor); + public Map<String, String> getArtifactProperties(IInstallableUnit iu, IArtifactDescriptor descriptor); } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IRootIUAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IRootIUAdvice.java index 2c5cad689..9cd432b1c 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IRootIUAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IRootIUAdvice.java @@ -11,9 +11,10 @@ package org.eclipse.equinox.p2.publisher.actions; import java.util.Collection; +import org.eclipse.equinox.p2.publisher.IPublisherAdvice; import org.eclipse.equinox.p2.publisher.IPublisherResult; -public interface IRootIUAdvice { +public interface IRootIUAdvice extends IPublisherAdvice { /** * Returns the list of children of the root for this publishing operation. @@ -22,5 +23,5 @@ public interface IRootIUAdvice { * @param result * @return the collection of children */ - public Collection getChildren(IPublisherResult result); + public Collection<? extends Object> getChildren(IPublisherResult result); }
\ No newline at end of file diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/ITouchpointAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/ITouchpointAdvice.java index 0a800d976..1551b7237 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/ITouchpointAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/ITouchpointAdvice.java @@ -10,7 +10,7 @@ *******************************************************************************/ package org.eclipse.equinox.p2.publisher.actions; -import org.eclipse.equinox.internal.provisional.p2.metadata.ITouchpointData; +import org.eclipse.equinox.p2.metadata.ITouchpointData; import org.eclipse.equinox.p2.publisher.IPublisherAdvice; /** diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IVersionAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IVersionAdvice.java index a2be89d91..b2e0d1d49 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IVersionAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/IVersionAdvice.java @@ -10,8 +10,7 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher.actions; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; - +import org.eclipse.equinox.p2.metadata.Version; import org.eclipse.equinox.p2.publisher.IPublisherAdvice; public interface IVersionAdvice extends IPublisherAdvice { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/JREAction.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/JREAction.java index 6614ee918..035349624 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/JREAction.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/JREAction.java @@ -12,15 +12,18 @@ package org.eclipse.equinox.p2.publisher.actions; import java.io.*; import java.net.URL; -import java.util.*; +import java.util.HashMap; +import java.util.Map; import org.eclipse.core.runtime.*; +import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils; import org.eclipse.equinox.internal.p2.metadata.ArtifactKey; import org.eclipse.equinox.internal.p2.publisher.Activator; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactDescriptor; -import org.eclipse.equinox.internal.provisional.p2.metadata.*; +import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitFragmentDescription; +import org.eclipse.equinox.p2.metadata.*; import org.eclipse.equinox.p2.publisher.*; +import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; import org.eclipse.osgi.util.ManifestElement; import org.osgi.framework.BundleException; @@ -36,7 +39,7 @@ public class JREAction extends AbstractPublisherAction { private File jreLocation; private String environment; - private Properties profileProperties; + private Map<String, String> profileProperties; public JREAction(File location) { this.jreLocation = location; @@ -73,11 +76,12 @@ public class JREAction extends AbstractPublisherAction { String configId = "config." + iu.getId();//$NON-NLS-1$ cu.setId(configId); cu.setVersion(iu.getVersion()); - cu.setHost(new IRequiredCapability[] {MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, iu.getId(), new VersionRange(iu.getVersion(), true, PublisherHelper.versionMax, true), null, false, false)}); - cu.setProperty(IInstallableUnit.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); + VersionRange range = iu.getVersion() == Version.emptyVersion ? VersionRange.emptyRange : new VersionRange(iu.getVersion(), true, Version.MAX_VERSION, true); + cu.setHost(new IRequirement[] {MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, iu.getId(), range, null, false, false)}); + cu.setProperty(InstallableUnitDescription.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); cu.setCapabilities(new IProvidedCapability[] {PublisherHelper.createSelfCapability(configId, iu.getVersion())}); cu.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE); - Map touchpointData = new HashMap(); + Map<String, String> touchpointData = new HashMap<String, String>(); if (jreLocation == null || !jreLocation.exists()) { touchpointData.put("install", ""); //$NON-NLS-1$ //$NON-NLS-2$ @@ -101,7 +105,7 @@ public class JREAction extends AbstractPublisherAction { results.addIU(MetadataFactory.createInstallableUnit(cu), IPublisherResult.ROOT); //Create the artifact descriptor - return PublisherHelper.createArtifactDescriptor(key, jreLocation); + return PublisherHelper.createArtifactDescriptor(info.getArtifactRepository(), key, jreLocation); } private IProvidedCapability[] generateJRECapability(String id, Version version) { @@ -109,7 +113,7 @@ public class JREAction extends AbstractPublisherAction { return new IProvidedCapability[0]; try { - ManifestElement[] jrePackages = ManifestElement.parseHeader(PROFILE_SYSTEM_PACKAGES, (String) profileProperties.get(PROFILE_SYSTEM_PACKAGES)); + ManifestElement[] jrePackages = ManifestElement.parseHeader(PROFILE_SYSTEM_PACKAGES, profileProperties.get(PROFILE_SYSTEM_PACKAGES)); IProvidedCapability[] exportedPackageAsCapabilities = new IProvidedCapability[jrePackages.length + 1]; exportedPackageAsCapabilities[0] = PublisherHelper.createSelfCapability(id, version); for (int i = 1; i <= jrePackages.length; i++) { @@ -127,9 +131,9 @@ public class JREAction extends AbstractPublisherAction { if (profileProperties == null || profileProperties.size() == 0) return; //got nothing - String profileLocation = profileProperties.getProperty(PROFILE_LOCATION); + String profileLocation = profileProperties.get(PROFILE_LOCATION); - String profileName = profileLocation != null ? new Path(profileLocation).lastSegment() : profileProperties.getProperty(PROFILE_NAME); + String profileName = profileLocation != null ? new Path(profileLocation).lastSegment() : profileProperties.get(PROFILE_NAME); if (profileName.endsWith(".profile")) //$NON-NLS-1$ profileName = profileName.substring(0, profileName.length() - 8); Version version = null; @@ -144,7 +148,7 @@ public class JREAction extends AbstractPublisherAction { } if (version == null) { try { - String targetVersion = profileProperties.getProperty(PROFILE_TARGET_VERSION); + String targetVersion = profileProperties.get(PROFILE_TARGET_VERSION); version = targetVersion != null ? Version.parseVersion(targetVersion) : null; } catch (IllegalArgumentException e) { //ignore @@ -186,13 +190,13 @@ public class JREAction extends AbstractPublisherAction { } } - private Properties loadProfile(File profileFile) { + private Map<String, String> loadProfile(File profileFile) { if (profileFile == null || !profileFile.exists()) return null; try { InputStream stream = new BufferedInputStream(new FileInputStream(profileFile)); - Properties properties = loadProfile(stream); + Map<String, String> properties = loadProfile(stream); if (properties != null) properties.put(PROFILE_LOCATION, profileFile.getAbsolutePath()); return properties; @@ -202,7 +206,7 @@ public class JREAction extends AbstractPublisherAction { return null; } - private Properties loadProfile(URL profileURL) { + private Map<String, String> loadProfile(URL profileURL) { if (profileURL == null) return null; @@ -218,12 +222,10 @@ public class JREAction extends AbstractPublisherAction { /** * Always closes the stream when done */ - private Properties loadProfile(InputStream stream) { + private Map<String, String> loadProfile(InputStream stream) { if (stream != null) { try { - Properties properties = new Properties(); - properties.load(stream); - return properties; + return CollectionUtils.loadProperties(stream); } catch (IOException e) { return null; } finally { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/QueryableFilterAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/QueryableFilterAdvice.java index 01b34b4aa..9a068b790 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/QueryableFilterAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/QueryableFilterAdvice.java @@ -9,10 +9,12 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher.actions; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; - -import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; -import org.eclipse.equinox.internal.provisional.p2.metadata.query.*; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; +import org.eclipse.equinox.p2.metadata.Version; +import org.eclipse.equinox.p2.metadata.query.InstallableUnitQuery; +import org.eclipse.equinox.p2.query.IQueryResult; +import org.eclipse.equinox.p2.query.IQueryable; +import org.osgi.framework.Filter; /** * An IFilterAdvice that looks up the desired IU in the publisher's input metadata @@ -20,23 +22,24 @@ import org.eclipse.equinox.internal.provisional.p2.metadata.query.*; */ public class QueryableFilterAdvice implements IFilterAdvice { - private IQueryable queryable; + private IQueryable<IInstallableUnit> queryable; - public QueryableFilterAdvice(IQueryable queryable) { + public QueryableFilterAdvice(IQueryable<IInstallableUnit> queryable) { this.queryable = queryable; } - public String getFilter(String id, Version version, boolean exact) { + public Filter getFilter(String id, Version version, boolean exact) { InstallableUnitQuery query = new InstallableUnitQuery(id, version); - Collector result = queryable.query(query, new Collector(), null); + IQueryResult<IInstallableUnit> result = queryable.query(query, null); if (!result.isEmpty()) - return (((IInstallableUnit) result.iterator().next()).getFilter()); + return result.iterator().next().getFilter(); if (exact) return null; + query = new InstallableUnitQuery(id); - result = queryable.query(query, new Collector(), null); + result = queryable.query(query, null); if (!result.isEmpty()) - return (((IInstallableUnit) result.iterator().next()).getFilter()); + return result.iterator().next().getFilter(); return null; } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootFilesAction.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootFilesAction.java index 27395b106..5ce821f25 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootFilesAction.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootFilesAction.java @@ -14,18 +14,20 @@ import java.io.File; import java.util.*; import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.core.helpers.FileUtils.IPathComputer; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactDescriptor; -import org.eclipse.equinox.internal.provisional.p2.metadata.*; +import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitFragmentDescription; +import org.eclipse.equinox.p2.metadata.*; import org.eclipse.equinox.p2.publisher.*; +import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; +import org.osgi.framework.Filter; -// TODO need to merge this functionality with the FeaturesAction work on root files public class RootFilesAction extends AbstractPublisherAction { private String idBase; private Version version; private String flavor; + private boolean createParent = true; /** * Returns the id of the top level IU published by this action for the given id and flavor. @@ -43,6 +45,10 @@ public class RootFilesAction extends AbstractPublisherAction { this.flavor = flavor; } + public void setCreateParent(boolean createParent) { + this.createParent = createParent; + } + public IStatus perform(IPublisherInfo publisherInfo, IPublisherResult results, IProgressMonitor monitor) { setPublisherInfo(publisherInfo); IPublisherResult innerResult = new PublisherResult(); @@ -56,13 +62,14 @@ public class RootFilesAction extends AbstractPublisherAction { } // merge the IUs into the final result as non-roots and create a parent IU that captures them all results.merge(innerResult, IPublisherResult.MERGE_ALL_NON_ROOT); - publishTopLevelRootFilesIU(innerResult.getIUs(null, IPublisherResult.ROOT), results); + if (createParent) + publishTopLevelRootFilesIU(innerResult.getIUs(null, IPublisherResult.ROOT), results); if (monitor.isCanceled()) return Status.CANCEL_STATUS; return Status.OK_STATUS; } - private void publishTopLevelRootFilesIU(Collection children, IPublisherResult result) { + private void publishTopLevelRootFilesIU(Collection<? extends IVersionedId> children, IPublisherResult result) { InstallableUnitDescription descriptor = createParentIU(children, computeIUId(idBase, flavor), version); descriptor.setSingleton(true); IInstallableUnit rootIU = MetadataFactory.createInstallableUnit(descriptor); @@ -83,7 +90,7 @@ public class RootFilesAction extends AbstractPublisherAction { String iuId = idPrefix + '.' + createIdString(configSpec); iu.setId(iuId); iu.setVersion(version); - String filter = createFilterSpec(configSpec); + Filter filter = createFilterSpec(configSpec); iu.setFilter(filter); IArtifactKey key = PublisherHelper.createBinaryArtifactKey(iuId, version); iu.setArtifacts(new IArtifactKey[] {key}); @@ -98,25 +105,25 @@ public class RootFilesAction extends AbstractPublisherAction { cu.setId(configUnitId); cu.setVersion(version); cu.setFilter(filter); - cu.setHost(new IRequiredCapability[] {MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, iuId, new VersionRange(version, true, version, true), null, false, false)}); - cu.setProperty(IInstallableUnit.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); + cu.setHost(new IRequirement[] {MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, iuId, new VersionRange(version, true, version, true), null, false, false)}); + cu.setProperty(InstallableUnitDescription.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); //TODO bug 218890, would like the fragment to provide the launcher capability as well, but can't right now. cu.setCapabilities(new IProvidedCapability[] {PublisherHelper.createSelfCapability(configUnitId, version)}); cu.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE); - Map touchpointData = new HashMap(); + Map<String, String> touchpointData = new HashMap<String, String>(); String configurationData = "unzip(source:@artifact, target:${installFolder});"; //$NON-NLS-1$ touchpointData.put("install", configurationData); //$NON-NLS-1$ String unConfigurationData = "cleanupzip(source:@artifact, target:${installFolder});"; //$NON-NLS-1$ touchpointData.put("uninstall", unConfigurationData); //$NON-NLS-1$ - cu.addTouchpointData(MetadataFactory.createTouchpointData(touchpointData)); + processTouchpointAdvice(cu, touchpointData, info, configSpec); IInstallableUnit unit = MetadataFactory.createInstallableUnit(cu); result.addIU(unit, IPublisherResult.ROOT); if ((info.getArtifactOptions() & (IPublisherInfo.A_INDEX | IPublisherInfo.A_PUBLISH)) > 0) { // Create the artifact descriptor. we have several files so no path on disk - IArtifactDescriptor descriptor = PublisherHelper.createArtifactDescriptor(key, null); + IArtifactDescriptor descriptor = PublisherHelper.createArtifactDescriptor(info.getArtifactRepository(), key, null); IRootFilesAdvice advice = getAdvice(configSpec); publishArtifact(descriptor, advice.getIncludedFiles(), advice.getExcludedFiles(), info, createPrefixComputer(advice.getRoot())); } @@ -135,12 +142,11 @@ public class RootFilesAction extends AbstractPublisherAction { * @return a compilation of <class>IRootfilesAdvice</class> from the <code>info</code>. */ private IRootFilesAdvice getAdvice(String configSpec) { - Collection advice = info.getAdvice(configSpec, true, null, null, IRootFilesAdvice.class); - ArrayList inclusions = new ArrayList(); - ArrayList exclusions = new ArrayList(); + Collection<IRootFilesAdvice> advice = info.getAdvice(configSpec, true, null, null, IRootFilesAdvice.class); + ArrayList<File> inclusions = new ArrayList<File>(); + ArrayList<File> exclusions = new ArrayList<File>(); File root = null; - for (Iterator i = advice.iterator(); i.hasNext();) { - IRootFilesAdvice entry = (IRootFilesAdvice) i.next(); + for (IRootFilesAdvice entry : advice) { // TODO for now we simply get root from the first advice that has one if (root == null) root = entry.getRoot(); @@ -151,8 +157,8 @@ public class RootFilesAction extends AbstractPublisherAction { if (list != null) exclusions.addAll(Arrays.asList(list)); } - File[] includeList = (File[]) inclusions.toArray(new File[inclusions.size()]); - File[] excludeList = (File[]) exclusions.toArray(new File[exclusions.size()]); + File[] includeList = inclusions.toArray(new File[inclusions.size()]); + File[] excludeList = exclusions.toArray(new File[exclusions.size()]); return new RootFilesAdvice(root, includeList, excludeList, configSpec); } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootIUAction.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootIUAction.java index d1bce4eab..7d97d7926 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootIUAction.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootIUAction.java @@ -12,12 +12,14 @@ package org.eclipse.equinox.p2.publisher.actions; import java.net.URI; import java.net.URISyntaxException; -import java.util.*; +import java.util.Collection; +import java.util.HashSet; import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.publisher.Activator; import org.eclipse.equinox.internal.p2.publisher.Messages; -import org.eclipse.equinox.internal.provisional.p2.metadata.*; +import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; +import org.eclipse.equinox.p2.metadata.*; import org.eclipse.equinox.p2.publisher.*; import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; import org.eclipse.osgi.util.NLS; @@ -31,7 +33,7 @@ public class RootIUAction extends AbstractPublisherAction { private Version version; private String id; private String name; - private Collection versionAdvice; + private Collection<IVersionAdvice> versionAdvice; public RootIUAction(String id, Version version, String name) { this.id = id; @@ -45,7 +47,7 @@ public class RootIUAction extends AbstractPublisherAction { } protected IStatus generateRootIU(IPublisherResult result) { - Collection children = getChildren(result); + Collection<? extends IVersionedId> children = getChildren(result); InstallableUnitDescription descriptor = createTopLevelIUDescription(children, null, false); processCapabilityAdvice(descriptor, info); processTouchpointAdvice(descriptor, null, info); @@ -67,14 +69,14 @@ public class RootIUAction extends AbstractPublisherAction { } protected static void processLicense(InstallableUnitDescription iu, IPublisherInfo info) { - Collection advice = info.getAdvice(null, true, iu.getId(), iu.getVersion(), ILicenseAdvice.class); + Collection<ILicenseAdvice> advice = info.getAdvice(null, true, iu.getId(), iu.getVersion(), ILicenseAdvice.class); if (advice.size() > 0) { // Only process the first license we find for this IU. - ILicenseAdvice entry = (ILicenseAdvice) advice.iterator().next(); + ILicenseAdvice entry = advice.iterator().next(); String licenseText = entry.getLicenseText() == null ? "" : entry.getLicenseText(); //$NON-NLS-1$ String licenseUrl = entry.getLicenseURL() == null ? "" : entry.getLicenseURL(); //$NON-NLS-1$ if (licenseText.length() > 0 || licenseUrl.length() > 0) - iu.setLicense(MetadataFactory.createLicense(toURIOrNull(licenseUrl), licenseText)); + iu.setLicenses(new ILicense[] {MetadataFactory.createLicense(toURIOrNull(licenseUrl), licenseText)}); } } @@ -116,19 +118,17 @@ public class RootIUAction extends AbstractPublisherAction { // cat.setProperty(IInstallableUnit.PROP_TYPE_CATEGORY, "true"); //$NON-NLS-1$ // return MetadataFactory.createInstallableUnit(cat); // } - private Collection getChildren(IPublisherResult result) { + private Collection<? extends IVersionedId> getChildren(IPublisherResult result) { // get any roots that we have accummulated so far and search for // children from the advice. - HashSet children = new HashSet(); - Collection rootAdvice = info.getAdvice(null, true, null, null, IRootIUAdvice.class); + HashSet<IVersionedId> children = new HashSet<IVersionedId>(); + Collection<IRootIUAdvice> rootAdvice = info.getAdvice(null, true, null, null, IRootIUAdvice.class); if (rootAdvice == null) return children; - for (Iterator i = rootAdvice.iterator(); i.hasNext();) { - IRootIUAdvice advice = (IRootIUAdvice) i.next(); - Collection list = advice.getChildren(result); + for (IRootIUAdvice advice : rootAdvice) { + Collection<? extends Object> list = advice.getChildren(result); if (list != null) - for (Iterator j = list.iterator(); j.hasNext();) { - Object object = j.next(); + for (Object object : list) { // if the advice is a string, look it up in the result. if not there then // query the known metadata repos if (object instanceof String) { @@ -137,29 +137,29 @@ public class RootIUAction extends AbstractPublisherAction { if (iu != null) children.add(iu); } else if (object instanceof IVersionedId) { - children.add(object); + children.add((IVersionedId) object); } } } return children; } - private InstallableUnitDescription createTopLevelIUDescription(Collection children, Collection requires, boolean configureLauncherData) { + private InstallableUnitDescription createTopLevelIUDescription(Collection<? extends IVersionedId> children, Collection<IRequirement> requires, boolean configureLauncherData) { InstallableUnitDescription root = new MetadataFactory.InstallableUnitDescription(); root.setSingleton(true); root.setId(id); root.setVersion(version); root.setProperty(IInstallableUnit.PROP_NAME, name); - Collection requiredCapabilities = createIURequirements(children); + Collection<IRequirement> requiredCapabilities = createIURequirements(children); if (requires != null) requiredCapabilities.addAll(requires); - root.setRequiredCapabilities((IRequiredCapability[]) requiredCapabilities.toArray(new IRequiredCapability[requiredCapabilities.size()])); + root.setRequiredCapabilities(requiredCapabilities.toArray(new IRequirement[requiredCapabilities.size()])); root.setArtifacts(new IArtifactKey[0]); root.setProperty("lineUp", "true"); //$NON-NLS-1$ //$NON-NLS-2$ root.setUpdateDescriptor(MetadataFactory.createUpdateDescriptor(id, VersionRange.emptyRange, IUpdateDescriptor.NORMAL, null)); - root.setProperty(IInstallableUnit.PROP_TYPE_GROUP, Boolean.TRUE.toString()); + root.setProperty(InstallableUnitDescription.PROP_TYPE_GROUP, Boolean.TRUE.toString()); root.setCapabilities(new IProvidedCapability[] {createSelfCapability(id, version)}); // TODO why is the type OSGI? root.setTouchpointType(PublisherHelper.TOUCHPOINT_OSGI); @@ -172,8 +172,7 @@ public class RootIUAction extends AbstractPublisherAction { if (versionAdvice == null) return null; } - for (Iterator i = versionAdvice.iterator(); i.hasNext();) { - IVersionAdvice advice = (IVersionAdvice) i.next(); + for (IVersionAdvice advice : versionAdvice) { // TODO have to figure a way to know the namespace here. for now just look everywhere Version result = advice.getVersion(IInstallableUnit.NAMESPACE_IU_ID, iuID); if (result == null) diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootIUAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootIUAdvice.java index 85d6051cb..5dbfb5274 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootIUAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootIUAdvice.java @@ -20,13 +20,13 @@ import org.eclipse.equinox.p2.publisher.IPublisherResult; */ public class RootIUAdvice extends AbstractAdvice implements IRootIUAdvice { - private Collection children; + private Collection<? extends Object> children; - public RootIUAdvice(Collection children) { + public RootIUAdvice(Collection<? extends Object> children) { this.children = children; } - public Collection getChildren(IPublisherResult result) { + public Collection<? extends Object> getChildren(IPublisherResult result) { return children; } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootIUResultFilterAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootIUResultFilterAdvice.java index b3205462b..bb858bc93 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootIUResultFilterAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/RootIUResultFilterAdvice.java @@ -9,23 +9,24 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher.actions; +import java.util.Arrays; import java.util.Collection; -import org.eclipse.equinox.internal.provisional.p2.metadata.query.Collector; -import org.eclipse.equinox.internal.provisional.p2.metadata.query.Query; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; import org.eclipse.equinox.p2.publisher.AbstractAdvice; import org.eclipse.equinox.p2.publisher.IPublisherResult; +import org.eclipse.equinox.p2.query.IQuery; public class RootIUResultFilterAdvice extends AbstractAdvice implements IRootIUAdvice { - private Query query; + private IQuery<IInstallableUnit> query; - public RootIUResultFilterAdvice(Query query) { + public RootIUResultFilterAdvice(IQuery<IInstallableUnit> query) { this.query = query; } - public Collection getChildren(IPublisherResult result) { - Collection value = result.getIUs(null, IPublisherResult.ROOT); + public Collection<IInstallableUnit> getChildren(IPublisherResult result) { + Collection<IInstallableUnit> value = result.getIUs(null, IPublisherResult.ROOT); if (query == null) return value; - return query.perform(value.iterator(), new Collector()).toCollection(); + return Arrays.asList(query.perform(value.iterator()).toArray(IInstallableUnit.class)); } } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/VersionAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/VersionAdvice.java index 629d2f45b..491c40f95 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/VersionAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/actions/VersionAdvice.java @@ -11,14 +11,17 @@ package org.eclipse.equinox.p2.publisher.actions; import java.io.*; -import java.util.*; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; +import java.util.HashMap; +import java.util.Map; +import java.util.Map.Entry; +import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils; +import org.eclipse.equinox.p2.metadata.Version; import org.eclipse.equinox.p2.publisher.AbstractAdvice; import org.eclipse.equinox.p2.publisher.IPublisherAdvice; public class VersionAdvice extends AbstractAdvice implements IVersionAdvice { - Map versions = new HashMap(11); + Map<String, Map<String, Version>> versions = new HashMap<String, Map<String, Version>>(11); /** * Load the given namespace with version mappings from the properties file at @@ -33,35 +36,30 @@ public class VersionAdvice extends AbstractAdvice implements IVersionAdvice { } public void load(String namespace, String location, String idSuffix) { + File file = new File(location); if (namespace == null) namespace = "null"; //$NON-NLS-1$ - Properties properties = new Properties(); - File file = new File(location); - if (file.exists()) { - InputStream stream = null; - try { - stream = new BufferedInputStream(new FileInputStream(file)); - properties.load(stream); - } catch (IOException e) { - // nothing - } finally { - if (stream != null) - try { - stream.close(); - } catch (IOException e) { - //nothing - } - } + + Map<String, String> properties; + InputStream stream = null; + try { + stream = new BufferedInputStream(new FileInputStream(file)); + properties = CollectionUtils.loadProperties(stream); + } catch (IOException e) { + return; + } finally { + if (stream != null) + try { + stream.close(); + } catch (IOException e) { + //nothing + } } - if (properties.size() > 0) { - Enumeration enumeration = properties.keys(); - while (enumeration.hasMoreElements()) { - String key = (String) enumeration.nextElement(); - if (idSuffix != null) - key += idSuffix; - String value = properties.getProperty(key); - setVersion(namespace, key, Version.parseVersion(value)); - } + for (Entry<String, String> entry : properties.entrySet()) { + String key = entry.getKey(); + if (idSuffix != null) + key += idSuffix; + setVersion(namespace, key, Version.parseVersion(entry.getValue())); } } @@ -72,18 +70,18 @@ public class VersionAdvice extends AbstractAdvice implements IVersionAdvice { * @return the version advice found or <code>null</code> if none */ public Version getVersion(String namespace, String id) { - Map values = (Map) versions.get(namespace); + Map<String, Version> values = versions.get(namespace); // if no one says anything then don't say anything. someone else might have an opinion if (values != null) { - Version result = (Version) values.get(id); + Version result = values.get(id); if (result != null) return result; } - values = (Map) versions.get("null"); //$NON-NLS-1$ + values = versions.get("null"); //$NON-NLS-1$ if (values == null) return null; - return (Version) values.get(id); + return values.get(id); } /** @@ -93,12 +91,12 @@ public class VersionAdvice extends AbstractAdvice implements IVersionAdvice { * @param version the version advice for the given id or <code>null</code> to remove advice */ public void setVersion(String namespace, String id, Version version) { - Map values = (Map) versions.get(namespace); + Map<String, Version> values = versions.get(namespace); if (values == null) { // if we are clearing values then there is nothing to do if (version == null) return; - values = new HashMap(); + values = new HashMap<String, Version>(); versions.put(namespace, values); } if (version == null) @@ -111,10 +109,9 @@ public class VersionAdvice extends AbstractAdvice implements IVersionAdvice { if (!(advice instanceof VersionAdvice)) return this; VersionAdvice source = (VersionAdvice) advice; - for (Iterator i = source.versions.keySet().iterator(); i.hasNext();) { - String namespace = (String) i.next(); - Map myValues = (Map) versions.get(namespace); - Map sourceValues = (Map) source.versions.get(namespace); + for (String namespace : source.versions.keySet()) { + Map<String, Version> myValues = versions.get(namespace); + Map<String, Version> sourceValues = source.versions.get(namespace); if (myValues == null) versions.put(namespace, sourceValues); else if (sourceValues != null) @@ -123,10 +120,9 @@ public class VersionAdvice extends AbstractAdvice implements IVersionAdvice { return this; } - private Map merge(Map myValues, Map sourceValues) { - Map result = new HashMap(myValues); - for (Iterator i = sourceValues.keySet().iterator(); i.hasNext();) { - String key = (String) i.next(); + private Map<String, Version> merge(Map<String, Version> myValues, Map<String, Version> sourceValues) { + Map<String, Version> result = new HashMap<String, Version>(myValues); + for (String key : sourceValues.keySet()) { if (result.get(key) == null) result.put(key, sourceValues.get(key)); } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/AdviceFileAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/AdviceFileAdvice.java index 45bb067ea..82076f2e2 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/AdviceFileAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/AdviceFileAdvice.java @@ -10,20 +10,20 @@ *******************************************************************************/ package org.eclipse.equinox.p2.publisher.eclipse; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; - import java.io.*; -import java.util.*; +import java.util.Map; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; import org.eclipse.core.runtime.*; +import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils; import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; import org.eclipse.equinox.internal.p2.publisher.Activator; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactDescriptor; -import org.eclipse.equinox.internal.provisional.p2.metadata.*; +import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; +import org.eclipse.equinox.p2.metadata.*; import org.eclipse.equinox.p2.publisher.AbstractAdvice; import org.eclipse.equinox.p2.publisher.actions.*; +import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; /** * Publishing advice from a p2 advice file. An advice file (p2.inf) can be embedded @@ -40,11 +40,11 @@ public class AdviceFileAdvice extends AbstractAdvice implements ITouchpointAdvic private final String id; private final Version version; - private Map touchpointInstructions; + private Map<String, ITouchpointInstruction> touchpointInstructions; private IProvidedCapability[] providedCapabilities; - private IRequiredCapability[] requiredCapabilities; - private IRequiredCapability[] metaRequiredCapabilities; - private Properties iuProperties; + private IRequirement[] requiredCapabilities; + private IRequirement[] metaRequiredCapabilities; + private Map<String, String> iuProperties; private InstallableUnitDescription[] additionalIUs; private boolean containsAdvice = false; @@ -69,7 +69,7 @@ public class AdviceFileAdvice extends AbstractAdvice implements ITouchpointAdvic this.id = id; this.version = version; - Map advice = loadAdviceMap(basePath, adviceFilePath); + Map<String, String> advice = loadAdviceMap(basePath, adviceFilePath); if (advice.isEmpty()) return; @@ -98,10 +98,10 @@ public class AdviceFileAdvice extends AbstractAdvice implements ITouchpointAdvic /** * Loads the advice file and returns it in map form. */ - private static Map loadAdviceMap(IPath basePath, IPath adviceFilePath) { + private static Map<String, String> loadAdviceMap(IPath basePath, IPath adviceFilePath) { File location = basePath.toFile(); if (location == null || !location.exists()) - return Collections.EMPTY_MAP; + return CollectionUtils.emptyMap(); ZipFile jar = null; InputStream stream = null; @@ -109,25 +109,22 @@ public class AdviceFileAdvice extends AbstractAdvice implements ITouchpointAdvic if (location.isDirectory()) { File adviceFile = new File(location, adviceFilePath.toString()); if (!adviceFile.isFile()) - return Collections.EMPTY_MAP; + return CollectionUtils.emptyMap(); stream = new BufferedInputStream(new FileInputStream(adviceFile)); } else if (location.isFile()) { jar = new ZipFile(location); ZipEntry entry = jar.getEntry(adviceFilePath.toString()); if (entry == null) - return Collections.EMPTY_MAP; + return CollectionUtils.emptyMap(); stream = new BufferedInputStream(jar.getInputStream(entry)); } - - Properties advice = new Properties(); - advice.load(stream); - return (advice != null ? advice : Collections.EMPTY_MAP); + return CollectionUtils.loadProperties(stream); } catch (IOException e) { String message = "An error occured while reading advice file: basePath=" + basePath + ", adviceFilePath=" + adviceFilePath + "."; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$ IStatus status = new Status(IStatus.ERROR, Activator.ID, message, e); LogHelper.log(status); - return Collections.EMPTY_MAP; + return CollectionUtils.emptyMap(); } finally { if (stream != null) try { @@ -152,49 +149,18 @@ public class AdviceFileAdvice extends AbstractAdvice implements ITouchpointAdvic * @see org.eclipse.equinox.p2.publisher.eclipse.ITouchpointAdvice#getTouchpointData() */ public ITouchpointData getTouchpointData(ITouchpointData existing) { - if (touchpointInstructions == null) - return existing; - - Map resultInstructions = new HashMap(existing.getInstructions()); - for (Iterator iterator = touchpointInstructions.keySet().iterator(); iterator.hasNext();) { - String key = (String) iterator.next(); - ITouchpointInstruction instruction = (ITouchpointInstruction) touchpointInstructions.get(key); - ITouchpointInstruction existingInstruction = (ITouchpointInstruction) resultInstructions.get(key); - - if (existingInstruction != null) { - String body = existingInstruction.getBody(); - if (body == null || body.length() == 0) - body = instruction.getBody(); - else if (instruction.getBody() != null) { - if (!body.endsWith(";")) //$NON-NLS-1$ - body += ';'; - body += instruction.getBody(); - } - - String importAttribute = existingInstruction.getImportAttribute(); - if (importAttribute == null || importAttribute.length() == 0) - importAttribute = instruction.getImportAttribute(); - else if (instruction.getImportAttribute() != null) { - if (!importAttribute.endsWith(",")) //$NON-NLS-1$ - importAttribute += ','; - importAttribute += instruction.getBody(); - } - instruction = MetadataFactory.createTouchpointInstruction(body, importAttribute); - } - resultInstructions.put(key, instruction); - } - return MetadataFactory.createTouchpointData(resultInstructions); + return MetadataFactory.mergeTouchpointData(existing, touchpointInstructions); } public IProvidedCapability[] getProvidedCapabilities(InstallableUnitDescription iu) { return providedCapabilities; } - public IRequiredCapability[] getRequiredCapabilities(InstallableUnitDescription iu) { + public IRequirement[] getRequiredCapabilities(InstallableUnitDescription iu) { return requiredCapabilities; } - public IRequiredCapability[] getMetaRequiredCapabilities(InstallableUnitDescription iu) { + public IRequirement[] getMetaRequiredCapabilities(InstallableUnitDescription iu) { return metaRequiredCapabilities; } @@ -202,11 +168,11 @@ public class AdviceFileAdvice extends AbstractAdvice implements ITouchpointAdvic return additionalIUs; } - public Properties getArtifactProperties(IInstallableUnit iu, IArtifactDescriptor descriptor) { + public Map<String, String> getArtifactProperties(IInstallableUnit iu, IArtifactDescriptor descriptor) { return null; } - public Properties getInstallableUnitProperties(InstallableUnitDescription iu) { + public Map<String, String> getInstallableUnitProperties(InstallableUnitDescription iu) { return iuProperties; } } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/AdviceFileParser.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/AdviceFileParser.java index 289ce57d0..9a1fe9ded 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/AdviceFileParser.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/AdviceFileParser.java @@ -13,9 +13,10 @@ import java.net.URISyntaxException; import java.util.*; import java.util.Map.Entry; import org.eclipse.equinox.internal.p2.metadata.ArtifactKey; -import org.eclipse.equinox.internal.provisional.p2.metadata.*; +import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitFragmentDescription; +import org.eclipse.equinox.p2.metadata.*; public class AdviceFileParser { @@ -60,31 +61,31 @@ public class AdviceFileParser { public static final Version COMPATIBLE_VERSION = Version.createOSGi(1, 0, 0); public static final VersionRange VERSION_TOLERANCE = new VersionRange(COMPATIBLE_VERSION, true, Version.createOSGi(2, 0, 0), false); - private Properties adviceProperties = new Properties(); - private List adviceProvides = new ArrayList(); - private List adviceRequires = new ArrayList(); - private List adviceMetaRequires = new ArrayList(); - private Map adviceInstructions = new HashMap(); - private List adviceOtherIUs = new ArrayList(); + private Map<String, String> adviceProperties = new HashMap<String, String>(); + private List<IProvidedCapability> adviceProvides = new ArrayList<IProvidedCapability>(); + private List<IRequirement> adviceRequires = new ArrayList<IRequirement>(); + private List<IRequirement> adviceMetaRequires = new ArrayList<IRequirement>(); + private Map<String, ITouchpointInstruction> adviceInstructions = new HashMap<String, ITouchpointInstruction>(); + private List<InstallableUnitDescription> adviceOtherIUs = new ArrayList<InstallableUnitDescription>(); - private final Map advice; - private Iterator keysIterator; + private final Map<String, String> advice; + private Iterator<String> keysIterator; private String current; // private String hostId; not currently used private Version hostVersion; - public AdviceFileParser(String id, Version version, Map advice) { + public AdviceFileParser(String id, Version version, Map<String, String> advice) { // this.hostId = id; not currently used this.hostVersion = version; this.advice = advice; } public void parse() { - String adviceVersion = (String) advice.get(ADVICE_VERSION); + String adviceVersion = advice.get(ADVICE_VERSION); if (adviceVersion != null) checkAdviceVersion(adviceVersion); - List keys = new ArrayList(advice.keySet()); + List<String> keys = new ArrayList<String>(advice.keySet()); Collections.sort(keys); keysIterator = keys.iterator(); @@ -119,14 +120,14 @@ public class AdviceFileParser { } private void next() { - current = (String) (keysIterator.hasNext() ? keysIterator.next() : null); + current = keysIterator.hasNext() ? keysIterator.next() : null; } private String currentValue() { - return ((String) advice.get(current)).trim(); + return advice.get(current).trim(); } - private void parseProperties(String prefix, Map properties) { + private void parseProperties(String prefix, Map<String, String> properties) { while (current != null && current.startsWith(prefix)) { int dotIndex = current.indexOf('.', prefix.length()); if (dotIndex == -1) @@ -136,7 +137,7 @@ public class AdviceFileParser { } } - private void parseProperty(String prefix, Map properties) { + private void parseProperty(String prefix, Map<String, String> properties) { String propertyName = null; String propertyValue = null; while (current != null && current.startsWith(prefix)) { @@ -154,7 +155,7 @@ public class AdviceFileParser { properties.put(propertyName, propertyValue); } - private void parseProvides(String prefix, List provides) { + private void parseProvides(String prefix, List<IProvidedCapability> provides) { while (current != null && current.startsWith(prefix)) { int dotIndex = current.indexOf('.', prefix.length()); if (dotIndex == -1) @@ -164,7 +165,7 @@ public class AdviceFileParser { } } - private void parseProvided(String prefix, List provides) { + private void parseProvided(String prefix, List<IProvidedCapability> provides) { String namespace = null; String name = null; Version capabilityVersion = null; @@ -186,7 +187,7 @@ public class AdviceFileParser { provides.add(capability); } - private void parseRequires(String prefix, List requires) { + private void parseRequires(String prefix, List<IRequirement> requires) { while (current != null && current.startsWith(prefix)) { int dotIndex = current.indexOf('.', prefix.length()); if (dotIndex == -1) @@ -196,7 +197,7 @@ public class AdviceFileParser { } } - private void parseRequired(String prefix, List requires) { + private void parseRequired(String prefix, List<IRequirement> requires) { String namespace = null; String name = null; @@ -227,11 +228,11 @@ public class AdviceFileParser { } next(); } - IRequiredCapability capability = MetadataFactory.createRequiredCapability(namespace, name, range, filter, optional, multiple, greedy); + IRequirement capability = MetadataFactory.createRequiredCapability(namespace, name, range, filter, optional, multiple, greedy); requires.add(capability); } - private void parseInstructions(String prefix, Map instructions) { + private void parseInstructions(String prefix, Map<String, ITouchpointInstruction> instructions) { while (current != null && current.startsWith(prefix)) { int dotIndex = current.indexOf('.', prefix.length()); if (dotIndex != -1) @@ -241,7 +242,7 @@ public class AdviceFileParser { } } - private void parseInstruction(String prefix, Map instructions) { + private void parseInstruction(String prefix, Map<String, ITouchpointInstruction> instructions) { String phase = current.substring(current.lastIndexOf('.') + 1); String body = currentValue(); next(); @@ -260,7 +261,7 @@ public class AdviceFileParser { instructions.put(phase, instruction); } - private void parseUnits(String prefix, List ius) { + private void parseUnits(String prefix, List<InstallableUnitDescription> ius) { while (current != null && current.startsWith(prefix)) { int dotIndex = current.indexOf('.', prefix.length()); if (dotIndex == -1) @@ -270,7 +271,7 @@ public class AdviceFileParser { } } - private void parseUnit(String prefix, List units) { + private void parseUnit(String prefix, List<InstallableUnitDescription> units) { String unitId = null; Version unitVersion = null; boolean unitSingleton = false; @@ -285,14 +286,14 @@ public class AdviceFileParser { int unitUpdateSeverity = 0; String unitUpdateDescription = null; - List unitArtifacts = new ArrayList(); - Properties unitProperties = new Properties(); - List unitHostRequirements = new ArrayList(); - List unitProvides = new ArrayList(); - List unitRequires = new ArrayList(); - List unitMetaRequirements = new ArrayList(); - List unitLicenses = new ArrayList(); - Map unitInstructions = new HashMap(); + List<IArtifactKey> unitArtifacts = new ArrayList<IArtifactKey>(); + Map<String, String> unitProperties = new HashMap<String, String>(); + List<IRequirement> unitHostRequirements = new ArrayList<IRequirement>(); + List<IProvidedCapability> unitProvides = new ArrayList<IProvidedCapability>(); + List<IRequirement> unitRequires = new ArrayList<IRequirement>(); + List<IRequirement> unitMetaRequirements = new ArrayList<IRequirement>(); + List<ILicense> unitLicenses = new ArrayList<ILicense>(); + Map<String, ITouchpointInstruction> unitInstructions = new HashMap<String, ITouchpointInstruction>(); // updatedescriptor ?? while (current != null && current.startsWith(prefix)) { @@ -375,29 +376,28 @@ public class AdviceFileParser { description.setUpdateDescriptor(MetadataFactory.createUpdateDescriptor(unitUpdateId, unitUpdateRange, unitUpdateSeverity, unitUpdateDescription)); if (!unitLicenses.isEmpty()) - description.setLicense((ILicense) unitLicenses.get(0)); + description.setLicenses(unitLicenses.toArray(new ILicense[unitLicenses.size()])); if (!unitArtifacts.isEmpty()) - description.setArtifacts((IArtifactKey[]) unitArtifacts.toArray(new IArtifactKey[unitArtifacts.size()])); + description.setArtifacts(unitArtifacts.toArray(new IArtifactKey[unitArtifacts.size()])); if (!unitHostRequirements.isEmpty()) - ((InstallableUnitFragmentDescription) description).setHost((IRequiredCapability[]) unitHostRequirements.toArray(new IRequiredCapability[unitHostRequirements.size()])); + ((InstallableUnitFragmentDescription) description).setHost(unitHostRequirements.toArray(new IRequirement[unitHostRequirements.size()])); if (!unitProperties.isEmpty()) { - for (Iterator iterator = unitProperties.entrySet().iterator(); iterator.hasNext();) { - Entry entry = (Entry) iterator.next(); - description.setProperty((String) entry.getKey(), (String) entry.getValue()); + for (Entry<String, String> entry : unitProperties.entrySet()) { + description.setProperty(entry.getKey(), entry.getValue()); } } if (!unitProvides.isEmpty()) - description.setCapabilities((IProvidedCapability[]) unitProvides.toArray(new IProvidedCapability[unitProvides.size()])); + description.setCapabilities(unitProvides.toArray(new IProvidedCapability[unitProvides.size()])); if (!unitRequires.isEmpty()) - description.setRequiredCapabilities((IRequiredCapability[]) unitRequires.toArray(new IRequiredCapability[unitRequires.size()])); + description.setRequiredCapabilities(unitRequires.toArray(new IRequirement[unitRequires.size()])); if (!unitMetaRequirements.isEmpty()) - description.setMetaRequiredCapabilities((IRequiredCapability[]) unitMetaRequirements.toArray(new IRequiredCapability[unitMetaRequirements.size()])); + description.setMetaRequiredCapabilities(unitMetaRequirements.toArray(new IRequirement[unitMetaRequirements.size()])); if (!unitInstructions.isEmpty()) description.addTouchpointData(MetadataFactory.createTouchpointData(unitInstructions)); @@ -405,7 +405,7 @@ public class AdviceFileParser { adviceOtherIUs.add(description); } - private void parseLicenses(String prefix, List licenses) { + private void parseLicenses(String prefix, List<ILicense> licenses) { while (current != null && current.startsWith(prefix)) { int dotIndex = current.indexOf('.', prefix.length()); if (dotIndex != -1) @@ -415,7 +415,7 @@ public class AdviceFileParser { } } - private void parseLicense(String prefix, List licenses) { + private void parseLicense(String prefix, List<ILicense> licenses) { String body = currentValue(); next(); @@ -439,7 +439,7 @@ public class AdviceFileParser { } } - private void parseArtifacts(String prefix, List artifacts) { + private void parseArtifacts(String prefix, List<IArtifactKey> artifacts) { while (current != null && current.startsWith(prefix)) { int dotIndex = current.indexOf('.', prefix.length()); if (dotIndex == -1) @@ -449,7 +449,7 @@ public class AdviceFileParser { } } - private void parseArtifact(String prefix, List artifacts) { + private void parseArtifact(String prefix, List<IArtifactKey> artifacts) { String artifactClassifier = null; String artifactId = null; Version artifactVersion = null; @@ -477,15 +477,19 @@ public class AdviceFileParser { } if (version.indexOf(QUALIFIER_SUBSTITUTION) != -1) { - String qualifier = hostVersion.getQualifier(); - if (qualifier == null) - qualifier = ""; //$NON-NLS-1$ - if (qualifier.length() == 0) { - // Note: this works only for OSGi versions and version ranges - // where the qualifier if present must be at the end of a version string - version = replace(version, "." + QUALIFIER_SUBSTITUTION, ""); //$NON-NLS-1$ //$NON-NLS-2$ + try { + String qualifier = Version.toOSGiVersion(hostVersion).getQualifier(); + if (qualifier == null) + qualifier = ""; //$NON-NLS-1$ + if (qualifier.length() == 0) { + // Note: this works only for OSGi versions and version ranges + // where the qualifier if present must be at the end of a version string + version = replace(version, "." + QUALIFIER_SUBSTITUTION, ""); //$NON-NLS-1$ //$NON-NLS-2$ + } + version = replace(version, QUALIFIER_SUBSTITUTION, qualifier); + } catch (UnsupportedOperationException e) { + // Version cannot be converted to OSGi } - version = replace(version, QUALIFIER_SUBSTITUTION, qualifier); } return version; } @@ -510,27 +514,27 @@ public class AdviceFileParser { return buffer.toString(); } - public Properties getProperties() { + public Map<String, String> getProperties() { if (adviceProperties.isEmpty()) return null; return adviceProperties; } - public IRequiredCapability[] getRequiredCapabilities() { + public IRequirement[] getRequiredCapabilities() { if (adviceRequires.isEmpty()) return null; - return (IRequiredCapability[]) adviceRequires.toArray(new IRequiredCapability[adviceRequires.size()]); + return adviceRequires.toArray(new IRequirement[adviceRequires.size()]); } public IProvidedCapability[] getProvidedCapabilities() { if (adviceProvides.isEmpty()) return null; - return (IProvidedCapability[]) adviceProvides.toArray(new IProvidedCapability[adviceProvides.size()]); + return adviceProvides.toArray(new IProvidedCapability[adviceProvides.size()]); } - public Map getTouchpointInstructions() { + public Map<String, ITouchpointInstruction> getTouchpointInstructions() { if (adviceInstructions.isEmpty()) return null; @@ -541,13 +545,13 @@ public class AdviceFileParser { if (adviceOtherIUs.isEmpty()) return null; - return (InstallableUnitDescription[]) adviceOtherIUs.toArray(new InstallableUnitDescription[adviceOtherIUs.size()]); + return adviceOtherIUs.toArray(new InstallableUnitDescription[adviceOtherIUs.size()]); } - public IRequiredCapability[] getMetaRequiredCapabilities() { + public IRequirement[] getMetaRequiredCapabilities() { if (adviceMetaRequires.isEmpty()) return null; - return (IRequiredCapability[]) adviceMetaRequires.toArray(new IRequiredCapability[adviceMetaRequires.size()]); + return adviceMetaRequires.toArray(new IRequirement[adviceMetaRequires.size()]); } }
\ No newline at end of file diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ApplicationLauncherAction.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ApplicationLauncherAction.java index b9272ded1..d0528cbbf 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ApplicationLauncherAction.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ApplicationLauncherAction.java @@ -10,15 +10,14 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher.eclipse; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; - import java.io.File; import java.util.*; import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.publisher.eclipse.ExecutablesDescriptor; -import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; +import org.eclipse.equinox.p2.metadata.*; +import org.eclipse.equinox.p2.metadata.query.FragmentQuery; import org.eclipse.equinox.p2.publisher.*; import org.eclipse.equinox.p2.publisher.actions.VersionAdvice; @@ -57,14 +56,14 @@ public class ApplicationLauncherAction extends AbstractPublisherAction { public IStatus perform(IPublisherInfo publisherInfo, IPublisherResult results, IProgressMonitor monitor) { // Create the basic actions and run them putting the IUs in a temporary result - Collection actions = createActions(publisherInfo); + Collection<IPublisherAction> actions = createActions(publisherInfo); createAdvice(publisherInfo, results); IPublisherResult innerResult = new PublisherResult(); MultiStatus finalStatus = new MultiStatus(ApplicationLauncherAction.class.getName(), 0, "publishing result", null); //$NON-NLS-1$ - for (Iterator i = actions.iterator(); i.hasNext();) { + for (IPublisherAction action : actions) { if (monitor.isCanceled()) return Status.CANCEL_STATUS; - finalStatus.merge(((IPublisherAction) i.next()).perform(publisherInfo, innerResult, monitor)); + finalStatus.merge(action.perform(publisherInfo, innerResult, monitor)); } if (!finalStatus.isOK()) return finalStatus; @@ -86,14 +85,13 @@ public class ApplicationLauncherAction extends AbstractPublisherAction { * fragments are in use in this particular result. */ private void createLauncherAdvice(IPublisherInfo publisherInfo, IPublisherResult results) { - Collection ius = getIUs(results.getIUs(null, null), EquinoxLauncherCUAction.ORG_ECLIPSE_EQUINOX_LAUNCHER); + Collection<IInstallableUnit> ius = getIUs(results.getIUs(null, null), EquinoxLauncherCUAction.ORG_ECLIPSE_EQUINOX_LAUNCHER); VersionAdvice advice = new VersionAdvice(); boolean found = false; - for (Iterator i = ius.iterator(); i.hasNext();) { - IInstallableUnit iu = (IInstallableUnit) i.next(); + for (IInstallableUnit iu : ius) { // skip over source bundles and fragments // TODO should we use the source property here rather than magic name matching? - if (iu.getId().endsWith(".source") || iu.isFragment()) //$NON-NLS-1$ + if (iu.getId().endsWith(".source") || FragmentQuery.isFragment(iu)) //$NON-NLS-1$ continue; advice.setVersion(IInstallableUnit.NAMESPACE_IU_ID, iu.getId(), iu.getVersion()); found = true; @@ -102,17 +100,16 @@ public class ApplicationLauncherAction extends AbstractPublisherAction { publisherInfo.addAdvice(advice); } - private Collection getIUs(Collection ius, String prefix) { - Set result = new HashSet(); - for (Iterator iterator = ius.iterator(); iterator.hasNext();) { - IInstallableUnit tmp = (IInstallableUnit) iterator.next(); + private Collection<IInstallableUnit> getIUs(Collection<IInstallableUnit> ius, String prefix) { + Set<IInstallableUnit> result = new HashSet<IInstallableUnit>(); + for (IInstallableUnit tmp : ius) { if (tmp.getId().startsWith(prefix)) result.add(tmp); } return result; } - private void publishApplicationLauncherIU(Collection children, IPublisherResult result) { + private void publishApplicationLauncherIU(Collection<? extends IVersionedId> children, IPublisherResult result) { InstallableUnitDescription descriptor = createParentIU(children, computeIUId(id, flavor), version); descriptor.setSingleton(true); IInstallableUnit rootIU = MetadataFactory.createInstallableUnit(descriptor); @@ -121,15 +118,15 @@ public class ApplicationLauncherAction extends AbstractPublisherAction { result.addIU(rootIU, IPublisherResult.ROOT); } - private Collection createActions(IPublisherInfo publisherInfo) { - Collection actions = new ArrayList(); + private Collection<IPublisherAction> createActions(IPublisherInfo publisherInfo) { + Collection<IPublisherAction> actions = new ArrayList<IPublisherAction>(); actions.add(new EquinoxLauncherCUAction(flavor, configSpecs)); actions.addAll(createExecutablesActions(configSpecs)); return actions; } - protected Collection createExecutablesActions(String[] configs) { - Collection actions = new ArrayList(configs.length); + protected Collection<IPublisherAction> createExecutablesActions(String[] configs) { + Collection<IPublisherAction> actions = new ArrayList<IPublisherAction>(configs.length); for (int i = 0; i < configs.length; i++) { ExecutablesDescriptor executables = computeExecutables(configs[i]); IPublisherAction action = new EquinoxExecutableAction(executables, configs[i], id, version, flavor); diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/BundleShapeAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/BundleShapeAdvice.java index 9c809c9ef..510da514e 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/BundleShapeAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/BundleShapeAdvice.java @@ -9,8 +9,7 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher.eclipse; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; - +import org.eclipse.equinox.p2.metadata.Version; import org.eclipse.equinox.p2.publisher.AbstractAdvice; public class BundleShapeAdvice extends AbstractAdvice implements IBundleShapeAdvice { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/BundlesAction.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/BundlesAction.java index 4eb4bda37..e1a2683d6 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/BundlesAction.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/BundlesAction.java @@ -12,6 +12,7 @@ package org.eclipse.equinox.p2.publisher.eclipse; import java.io.*; import java.util.*; +import java.util.Map.Entry; import java.util.jar.JarFile; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; @@ -23,14 +24,17 @@ import org.eclipse.equinox.internal.p2.publisher.Activator; import org.eclipse.equinox.internal.p2.publisher.Messages; import org.eclipse.equinox.internal.p2.publisher.eclipse.GeneratorBundleInfo; import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.*; -import org.eclipse.equinox.internal.provisional.p2.metadata.*; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; -import org.eclipse.equinox.internal.provisional.p2.metadata.VersionRange; +import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitFragmentDescription; +import org.eclipse.equinox.p2.metadata.*; +import org.eclipse.equinox.p2.metadata.Version; +import org.eclipse.equinox.p2.metadata.VersionRange; import org.eclipse.equinox.p2.publisher.*; import org.eclipse.equinox.p2.publisher.actions.*; +import org.eclipse.equinox.p2.query.IQueryResult; +import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; +import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; import org.eclipse.equinox.spi.p2.publisher.LocalizationHelper; import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; import org.eclipse.osgi.service.pluginconversion.PluginConversionException; @@ -63,7 +67,6 @@ public class BundlesAction extends AbstractPublisherAction { /** * A capability name in the {@link PublisherHelper#NAMESPACE_ECLIPSE_TYPE} namespace * representing and OSGi bundle resource - * @see IRequiredCapability#getName() * @see IProvidedCapability#getName() */ public static final String TYPE_ECLIPSE_BUNDLE = "bundle"; //$NON-NLS-1$ @@ -71,7 +74,7 @@ public class BundlesAction extends AbstractPublisherAction { /** * A capability name in the {@link PublisherHelper#NAMESPACE_ECLIPSE_TYPE} namespace * representing a source bundle - * @see IRequiredCapability#getName() + * @see IProvidedCapability#getName() */ public static final String TYPE_ECLIPSE_SOURCE = "source"; //$NON-NLS-1$ @@ -100,7 +103,7 @@ public class BundlesAction extends AbstractPublisherAction { return new ArtifactKey(OSGI_BUNDLE_CLASSIFIER, bsn, Version.parseVersion(version)); } - public static IInstallableUnit createBundleConfigurationUnit(String hostId, Version cuVersion, boolean isBundleFragment, GeneratorBundleInfo configInfo, String configurationFlavor, String filter) { + public static IInstallableUnit createBundleConfigurationUnit(String hostId, Version cuVersion, boolean isBundleFragment, GeneratorBundleInfo configInfo, String configurationFlavor, Filter filter) { if (configInfo == null) return null; @@ -111,15 +114,16 @@ public class BundlesAction extends AbstractPublisherAction { //Indicate the IU to which this CU apply Version hostVersion = Version.parseVersion(configInfo.getVersion()); - cu.setHost(new IRequiredCapability[] { // - MetadataFactory.createRequiredCapability(CAPABILITY_NS_OSGI_BUNDLE, hostId, new VersionRange(hostVersion, true, PublisherHelper.versionMax, true), null, false, false, true), // + VersionRange range = hostVersion == Version.emptyVersion ? VersionRange.emptyRange : new VersionRange(hostVersion, true, Version.MAX_VERSION, true); + cu.setHost(new IRequirement[] { // + MetadataFactory.createRequiredCapability(CAPABILITY_NS_OSGI_BUNDLE, hostId, range, null, false, false, true), // MetadataFactory.createRequiredCapability(PublisherHelper.NAMESPACE_ECLIPSE_TYPE, TYPE_ECLIPSE_BUNDLE, new VersionRange(Version.createOSGi(1, 0, 0), true, Version.createOSGi(2, 0, 0), false), null, false, false, false)}); //Adds capabilities for fragment, self, and describing the flavor supported - cu.setProperty(IInstallableUnit.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); - cu.setCapabilities(new IProvidedCapability[] {PublisherHelper.createSelfCapability(configUnitId, cuVersion), MetadataFactory.createProvidedCapability(IInstallableUnit.NAMESPACE_FLAVOR, configurationFlavor, Version.createOSGi(1, 0, 0))}); + cu.setProperty(InstallableUnitDescription.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); + cu.setCapabilities(new IProvidedCapability[] {PublisherHelper.createSelfCapability(configUnitId, cuVersion), MetadataFactory.createProvidedCapability(PublisherHelper.NAMESPACE_FLAVOR, configurationFlavor, Version.createOSGi(1, 0, 0))}); - Map touchpointData = new HashMap(); + Map<String, String> touchpointData = new HashMap<String, String>(); touchpointData.put("install", "installBundle(bundle:${artifact})"); //$NON-NLS-1$ //$NON-NLS-2$ touchpointData.put("uninstall", "uninstallBundle(bundle:${artifact})"); //$NON-NLS-1$ //$NON-NLS-2$ touchpointData.put("configure", createConfigScript(configInfo, isBundleFragment)); //$NON-NLS-1$ @@ -130,8 +134,9 @@ public class BundlesAction extends AbstractPublisherAction { } public static IInstallableUnit createBundleIU(BundleDescription bd, IArtifactKey key, IPublisherInfo info) { - Map manifest = (Map) bd.getUserObject(); - Map manifestLocalizations = null; + @SuppressWarnings("unchecked") + Map<String, String> manifest = (Map<String, String>) bd.getUserObject(); + Map<Locale, Map<String, String>> manifestLocalizations = null; if (manifest != null && bd.getLocation() != null) manifestLocalizations = getManifestLocalizations(manifest, new File(bd.getLocation())); InstallableUnitDescription iu = new MetadataFactory.InstallableUnitDescription(); @@ -148,7 +153,7 @@ public class BundlesAction extends AbstractPublisherAction { //Process the required bundles BundleSpecification requiredBundles[] = bd.getRequiredBundles(); - ArrayList reqsDeps = new ArrayList(); + ArrayList<IRequirement> reqsDeps = new ArrayList<IRequirement>(); // if (requiresAFragment) // reqsDeps.add(MetadataFactory.createRequiredCapability(CAPABILITY_TYPE_OSGI_FRAGMENTS, bd.getSymbolicName(), VersionRange.emptyRange, null, false, false)); if (isFragment) @@ -168,10 +173,10 @@ public class BundlesAction extends AbstractPublisherAction { //TODO this needs to be refined to take into account all the attribute handled by imports reqsDeps.add(MetadataFactory.createRequiredCapability(PublisherHelper.CAPABILITY_NS_JAVA_PACKAGE, importPackageName, versionRange, null, isOptional(importSpec), false)); } - iu.setRequiredCapabilities((IRequiredCapability[]) reqsDeps.toArray(new IRequiredCapability[reqsDeps.size()])); + iu.setRequiredCapabilities(reqsDeps.toArray(new IRequirement[reqsDeps.size()])); // Create set of provided capabilities - ArrayList providedCapabilities = new ArrayList(); + ArrayList<IProvidedCapability> providedCapabilities = new ArrayList<IProvidedCapability>(); providedCapabilities.add(PublisherHelper.createSelfCapability(bd.getSymbolicName(), Version.fromOSGiVersion(bd.getVersion()))); providedCapabilities.add(MetadataFactory.createProvidedCapability(CAPABILITY_NS_OSGI_BUNDLE, bd.getSymbolicName(), Version.fromOSGiVersion(bd.getVersion()))); @@ -190,18 +195,16 @@ public class BundlesAction extends AbstractPublisherAction { providedCapabilities.add(MetadataFactory.createProvidedCapability(CAPABILITY_NS_OSGI_FRAGMENT, bd.getHost().getName(), Version.fromOSGiVersion(bd.getVersion()))); if (manifestLocalizations != null) { - for (Iterator iter = manifestLocalizations.keySet().iterator(); iter.hasNext();) { - Locale locale = (Locale) iter.next(); - Properties translatedStrings = (Properties) manifestLocalizations.get(locale); - Enumeration propertyKeys = translatedStrings.propertyNames(); - while (propertyKeys.hasMoreElements()) { - String nextKey = (String) propertyKeys.nextElement(); - iu.setProperty(locale.toString() + '.' + nextKey, translatedStrings.getProperty(nextKey)); + for (Entry<Locale, Map<String, String>> locEntry : manifestLocalizations.entrySet()) { + Locale locale = locEntry.getKey(); + Map<String, String> translatedStrings = locEntry.getValue(); + for (Entry<String, String> entry : translatedStrings.entrySet()) { + iu.setProperty(locale.toString() + '.' + entry.getKey(), entry.getValue()); } providedCapabilities.add(PublisherHelper.makeTranslationCapability(bd.getSymbolicName(), locale)); } } - iu.setCapabilities((IProvidedCapability[]) providedCapabilities.toArray(new IProvidedCapability[providedCapabilities.size()])); + iu.setCapabilities(providedCapabilities.toArray(new IProvidedCapability[providedCapabilities.size()])); processCapabilityAdvice(iu, info); // Set certain properties from the manifest header attributes as IU properties. @@ -212,7 +215,7 @@ public class BundlesAction extends AbstractPublisherAction { int i = 0; while (i < BUNDLE_IU_PROPERTY_MAP.length) { if (manifest.containsKey(BUNDLE_IU_PROPERTY_MAP[i])) { - String value = (String) manifest.get(BUNDLE_IU_PROPERTY_MAP[i]); + String value = manifest.get(BUNDLE_IU_PROPERTY_MAP[i]); if (value != null && value.length() > 0) { iu.setProperty(BUNDLE_IU_PROPERTY_MAP[i + 1], value); } @@ -223,7 +226,7 @@ public class BundlesAction extends AbstractPublisherAction { // Define the immutable metadata for this IU. In this case immutable means // that this is something that will not impact the configuration. - Map touchpointData = new HashMap(); + Map<String, String> touchpointData = new HashMap<String, String>(); touchpointData.put("manifest", toManifestString(manifest)); //$NON-NLS-1$ if (isDir(bd, info)) touchpointData.put("zipped", "true"); //$NON-NLS-1$ //$NON-NLS-2$ @@ -238,13 +241,13 @@ public class BundlesAction extends AbstractPublisherAction { if (!base.equals(org.osgi.framework.Version.emptyVersion)) { updateRange = new VersionRange(Version.emptyVersion, true, Version.fromOSGiVersion(base), false); } else { - updateRange = new VersionRange("0.0.0"); //$NON-NLS-1$ + updateRange = VersionRange.emptyRange; } return updateRange; } private IInstallableUnitFragment createHostLocalizationFragment(IInstallableUnit bundleIU, BundleDescription bd, String hostId, String[] hostBundleManifestValues) { - Map hostLocalizations = getHostLocalizations(new File(bd.getLocation()), hostBundleManifestValues); + Map<Locale, Map<String, String>> hostLocalizations = getHostLocalizations(new File(bd.getLocation()), hostBundleManifestValues); if (hostLocalizations != null) { return createLocalizationFragmentOfHost(bd, hostId, hostBundleManifestValues, hostLocalizations); } @@ -258,33 +261,30 @@ public class BundlesAction extends AbstractPublisherAction { * @param localizedStrings * @return installableUnitFragment */ - private static IInstallableUnitFragment createLocalizationFragmentOfHost(BundleDescription bd, String hostId, String[] hostManifestValues, Map hostLocalizations) { + private static IInstallableUnitFragment createLocalizationFragmentOfHost(BundleDescription bd, String hostId, String[] hostManifestValues, Map<Locale, Map<String, String>> hostLocalizations) { InstallableUnitFragmentDescription fragment = new MetadataFactory.InstallableUnitFragmentDescription(); String fragmentId = makeHostLocalizationFragmentId(bd.getSymbolicName()); fragment.setId(fragmentId); fragment.setVersion(Version.fromOSGiVersion(bd.getVersion())); // TODO: is this a meaningful version? HostSpecification hostSpec = bd.getHost(); - IRequiredCapability[] hostReqs = new IRequiredCapability[] {MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, hostSpec.getName(), VersionRange.fromOSGiVersionRange(hostSpec.getVersionRange()), null, false, false, false)}; + IRequirement[] hostReqs = new IRequirement[] {MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, hostSpec.getName(), VersionRange.fromOSGiVersionRange(hostSpec.getVersionRange()), null, false, false, false)}; fragment.setHost(hostReqs); fragment.setSingleton(true); - fragment.setProperty(IInstallableUnit.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); + fragment.setProperty(InstallableUnitDescription.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); // Create a provided capability for each locale and add the translated properties. - ArrayList providedCapabilities = new ArrayList(hostLocalizations.keySet().size()); - for (Iterator iter = hostLocalizations.keySet().iterator(); iter.hasNext();) { - Locale locale = (Locale) iter.next(); - Properties translatedStrings = (Properties) hostLocalizations.get(locale); - - Enumeration propertyKeys = translatedStrings.propertyNames(); - while (propertyKeys.hasMoreElements()) { - String nextKey = (String) propertyKeys.nextElement(); - fragment.setProperty(locale.toString() + '.' + nextKey, translatedStrings.getProperty(nextKey)); + ArrayList<IProvidedCapability> providedCapabilities = new ArrayList<IProvidedCapability>(hostLocalizations.keySet().size()); + for (Entry<Locale, Map<String, String>> localeEntry : hostLocalizations.entrySet()) { + Locale locale = localeEntry.getKey(); + Map<String, String> translatedStrings = localeEntry.getValue(); + for (Entry<String, String> entry : translatedStrings.entrySet()) { + fragment.setProperty(locale.toString() + '.' + entry.getKey(), entry.getValue()); } providedCapabilities.add(PublisherHelper.makeTranslationCapability(hostId, locale)); } - fragment.setCapabilities((IProvidedCapability[]) providedCapabilities.toArray(new IProvidedCapability[providedCapabilities.size()])); + fragment.setCapabilities(providedCapabilities.toArray(new IProvidedCapability[providedCapabilities.size()])); return MetadataFactory.createInstallableUnitFragment(fragment); } @@ -329,13 +329,13 @@ public class BundlesAction extends AbstractPublisherAction { cu.setVersion(configUnitVersion); // Add capabilities for fragment, self, and describing the flavor supported - cu.setProperty(IInstallableUnit.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); - cu.setCapabilities(new IProvidedCapability[] {PublisherHelper.createSelfCapability(configUnitId, configUnitVersion), MetadataFactory.createProvidedCapability(IInstallableUnit.NAMESPACE_FLAVOR, configurationFlavor, Version.createOSGi(1, 0, 0))}); + cu.setProperty(InstallableUnitDescription.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); + cu.setCapabilities(new IProvidedCapability[] {PublisherHelper.createSelfCapability(configUnitId, configUnitVersion), MetadataFactory.createProvidedCapability(PublisherHelper.NAMESPACE_FLAVOR, configurationFlavor, Version.createOSGi(1, 0, 0))}); // Create a required capability on bundles - IRequiredCapability[] reqs = new IRequiredCapability[] {MetadataFactory.createRequiredCapability(PublisherHelper.NAMESPACE_ECLIPSE_TYPE, TYPE_ECLIPSE_BUNDLE, VersionRange.emptyRange, null, false, true, false)}; + IRequirement[] reqs = new IRequirement[] {MetadataFactory.createRequiredCapability(PublisherHelper.NAMESPACE_ECLIPSE_TYPE, TYPE_ECLIPSE_BUNDLE, VersionRange.emptyRange, null, false, true, false)}; cu.setHost(reqs); - Map touchpointData = new HashMap(); + Map<String, String> touchpointData = new HashMap<String, String>(); touchpointData.put("install", "installBundle(bundle:${artifact})"); //$NON-NLS-1$ //$NON-NLS-2$ touchpointData.put("uninstall", "uninstallBundle(bundle:${artifact})"); //$NON-NLS-1$ //$NON-NLS-2$ @@ -373,13 +373,11 @@ public class BundlesAction extends AbstractPublisherAction { return false; } - private static String toManifestString(Map p) { + private static String toManifestString(Map<String, String> p) { if (p == null) return null; - Collection properties = p.entrySet(); StringBuffer result = new StringBuffer(); - for (Iterator iterator = properties.iterator(); iterator.hasNext();) { - Map.Entry aProperty = (Map.Entry) iterator.next(); + for (Entry<String, String> aProperty : p.entrySet()) { if (aProperty.getKey().equals(BUNDLE_SHAPE)) continue; result.append(aProperty.getKey()).append(": ").append(aProperty.getValue()).append('\n'); //$NON-NLS-1$ @@ -390,8 +388,8 @@ public class BundlesAction extends AbstractPublisherAction { // Return a map from locale to property set for the manifest localizations // from the given bundle directory and given bundle localization path/name // manifest property value. - private static Map getManifestLocalizations(Map manifest, File bundleLocation) { - Map localizations; + private static Map<Locale, Map<String, String>> getManifestLocalizations(Map<String, String> manifest, File bundleLocation) { + Map<Locale, Map<String, String>> localizations; Locale defaultLocale = null; // = Locale.ENGLISH; // TODO: get this from GeneratorInfo String[] bundleManifestValues = getManifestCachedValues(manifest); String bundleLocalization = bundleManifestValues[BUNDLE_LOCALIZATION_INDEX]; // Bundle localization is the last one in the list @@ -423,10 +421,10 @@ public class BundlesAction extends AbstractPublisherAction { return result; } - public static String[] getManifestCachedValues(Map manifest) { + public static String[] getManifestCachedValues(Map<String, String> manifest) { String[] cachedValues = new String[PublisherHelper.BUNDLE_LOCALIZED_PROPERTIES.length]; for (int j = 0; j < PublisherHelper.BUNDLE_LOCALIZED_PROPERTIES.length; j++) { - String value = (String) manifest.get(PublisherHelper.BUNDLE_LOCALIZED_PROPERTIES[j]); + String value = manifest.get(PublisherHelper.BUNDLE_LOCALIZED_PROPERTIES[j]); if (PublisherHelper.BUNDLE_LOCALIZED_PROPERTIES[j].equals(Constants.BUNDLE_LOCALIZATION)) { if (value == null) value = DEFAULT_BUNDLE_LOCALIZATION; @@ -441,8 +439,8 @@ public class BundlesAction extends AbstractPublisherAction { // Return a map from locale to property set for the manifest localizations // from the given bundle directory and given bundle localization path/name // manifest property value. - public static Map getHostLocalizations(File bundleLocation, String[] hostBundleManifestValues) { - Map localizations; + public static Map<Locale, Map<String, String>> getHostLocalizations(File bundleLocation, String[] hostBundleManifestValues) { + Map<Locale, Map<String, String>> localizations; Locale defaultLocale = null; // = Locale.ENGLISH; // TODO: get this from GeneratorInfo String hostBundleLocalization = hostBundleManifestValues[BUNDLE_LOCALIZATION_INDEX]; if (hostBundleLocalization == null) @@ -464,7 +462,7 @@ public class BundlesAction extends AbstractPublisherAction { return (PluginConverter) ServiceHelper.getService(Activator.getContext(), PluginConverter.class.getName()); } - private static Dictionary convertPluginManifest(File bundleLocation, boolean logConversionException) { + private static Dictionary<String, String> convertPluginManifest(File bundleLocation, boolean logConversionException) { PluginConverter converter; try { converter = acquirePluginConverter(); @@ -488,7 +486,7 @@ public class BundlesAction extends AbstractPublisherAction { } } - public static BundleDescription createBundleDescription(Dictionary enhancedManifest, File bundleLocation) { + public static BundleDescription createBundleDescription(Dictionary<String, String> enhancedManifest, File bundleLocation) { try { BundleDescription descriptor = StateObjectFactory.defaultFactory.createBundleDescription(null, enhancedManifest, bundleLocation == null ? null : bundleLocation.getAbsolutePath(), 1); //TODO Do we need to have a real bundle id descriptor.setUserObject(enhancedManifest); @@ -502,14 +500,14 @@ public class BundlesAction extends AbstractPublisherAction { } public static BundleDescription createBundleDescription(File bundleLocation) { - Dictionary manifest = loadManifest(bundleLocation); + Dictionary<String, String> manifest = loadManifest(bundleLocation); if (manifest == null) return null; return createBundleDescription(manifest, bundleLocation); } - public static Dictionary loadManifest(File bundleLocation) { - Dictionary manifest = basicLoadManifest(bundleLocation); + public static Dictionary<String, String> loadManifest(File bundleLocation) { + Dictionary<String, String> manifest = basicLoadManifest(bundleLocation); if (manifest == null) return null; // if the bundle itself does not define its shape, infer the shape from the current form @@ -518,7 +516,7 @@ public class BundlesAction extends AbstractPublisherAction { return manifest; } - public static Dictionary basicLoadManifest(File bundleLocation) { + public static Dictionary<String, String> basicLoadManifest(File bundleLocation) { InputStream manifestStream = null; ZipFile jarFile = null; try { @@ -537,32 +535,34 @@ public class BundlesAction extends AbstractPublisherAction { String message = NLS.bind(Messages.exception_errorLoadingManifest, bundleLocation); LogHelper.log(new Status(IStatus.WARNING, Activator.ID, message, e)); } - - Dictionary manifest = null; - if (manifestStream != null) { - try { - Map manifestMap = ManifestElement.parseBundleManifest(manifestStream, null); - // TODO temporary hack. We are reading a Map but everyone wants a Dictionary so convert. - // real answer is to have people expect a Map but that is a wider change. - manifest = new Hashtable(manifestMap); - } catch (IOException e) { - String message = NLS.bind(Messages.exception_errorReadingManifest, bundleLocation, e.getMessage()); - LogHelper.log(new Status(IStatus.ERROR, Activator.ID, message, e)); - return null; - } catch (BundleException e) { - String message = NLS.bind(Messages.exception_errorReadingManifest, bundleLocation, e.getMessage()); - LogHelper.log(new Status(IStatus.ERROR, Activator.ID, message, e)); - return null; - } finally { + Dictionary<String, String> manifest = null; + try { + if (manifestStream != null) { try { - if (jarFile != null) - jarFile.close(); - } catch (IOException e2) { - //Ignore + @SuppressWarnings("unchecked") + Map<String, String> manifestMap = ManifestElement.parseBundleManifest(manifestStream, null); + // TODO temporary hack. We are reading a Map but everyone wants a Dictionary so convert. + // real answer is to have people expect a Map but that is a wider change. + manifest = new Hashtable<String, String>(manifestMap); + } catch (IOException e) { + String message = NLS.bind(Messages.exception_errorReadingManifest, bundleLocation, e.getMessage()); + LogHelper.log(new Status(IStatus.ERROR, Activator.ID, message, e)); + return null; + } catch (BundleException e) { + String message = NLS.bind(Messages.exception_errorReadingManifest, bundleLocation, e.getMessage()); + LogHelper.log(new Status(IStatus.ERROR, Activator.ID, message, e)); + return null; } + } else { + manifest = convertPluginManifest(bundleLocation, true); + } + } finally { + try { + if (jarFile != null) + jarFile.close(); + } catch (IOException e2) { + //Ignore } - } else { - manifest = convertPluginManifest(bundleLocation, true); } if (manifest == null) @@ -634,12 +634,12 @@ public class BundlesAction extends AbstractPublisherAction { } private File[] expandLocations(File[] list) { - ArrayList result = new ArrayList(); + ArrayList<File> result = new ArrayList<File>(); expandLocations(list, result); - return (File[]) result.toArray(new File[result.size()]); + return result.toArray(new File[result.size()]); } - private void expandLocations(File[] list, ArrayList result) { + private void expandLocations(File[] list, ArrayList<File> result) { if (list == null) return; for (int i = 0; i < list.length; i++) { @@ -678,8 +678,8 @@ public class BundlesAction extends AbstractPublisherAction { } File location = new File(bd.getLocation()); - IArtifactDescriptor ad = PublisherHelper.createArtifactDescriptor(key, location); - processArtifactPropertiesAdvice(bundleIU, (ArtifactDescriptor) ad, info); + IArtifactDescriptor ad = PublisherHelper.createArtifactDescriptor(info.getArtifactRepository(), key, location); + processArtifactPropertiesAdvice(bundleIU, ad, info); // Publish according to the shape on disk File bundleLocation = new File(bd.getLocation()); @@ -693,13 +693,13 @@ public class BundlesAction extends AbstractPublisherAction { // TODO: Need a test case for multiple hosts String hostId = bd.getHost().getName(); VersionRange hostVersionRange = VersionRange.fromOSGiVersionRange(bd.getHost().getVersionRange()); - IInstallableUnit[] hosts = queryForIUs(result, hostId, hostVersionRange); + IQueryResult<IInstallableUnit> hosts = queryForIUs(result, hostId, hostVersionRange); - for (int j = 0; j < hosts.length; j++) { + for (Iterator<IInstallableUnit> itor = hosts.iterator(); itor.hasNext();) { String fragmentId = makeHostLocalizationFragmentId(bd.getSymbolicName()); fragment = queryForIU(result, fragmentId, Version.fromOSGiVersion(bd.getVersion())); if (fragment == null) { - String[] externalizedStrings = getExternalizedStrings(hosts[j]); + String[] externalizedStrings = getExternalizedStrings(itor.next()); fragment = createHostLocalizationFragment(bundleIU, bd, hostId, externalizedStrings); } } @@ -733,17 +733,18 @@ public class BundlesAction extends AbstractPublisherAction { } private static boolean isDir(BundleDescription bundle, IPublisherInfo info) { - Collection advice = info.getAdvice(null, true, bundle.getSymbolicName(), Version.fromOSGiVersion(bundle.getVersion()), IBundleShapeAdvice.class); + Collection<IBundleShapeAdvice> advice = info.getAdvice(null, true, bundle.getSymbolicName(), Version.fromOSGiVersion(bundle.getVersion()), IBundleShapeAdvice.class); // if the advice has a shape, use it if (advice != null && !advice.isEmpty()) { // we know there is some advice but if there is more than one, take the first. - String shape = ((IBundleShapeAdvice) advice.iterator().next()).getShape(); + String shape = advice.iterator().next().getShape(); if (shape != null) return shape.equals(IBundleShapeAdvice.DIR); } // otherwise go with whatever we figured out from the manifest or the shape on disk - Map manifest = (Map) bundle.getUserObject(); - String format = (String) manifest.get(BUNDLE_SHAPE); + @SuppressWarnings("unchecked") + Map<String, String> manifest = (Map<String, String>) bundle.getUserObject(); + String format = manifest.get(BUNDLE_SHAPE); return DIR.equals(format); } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ConfigAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ConfigAdvice.java index 2e25a1b97..e6a5497e8 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ConfigAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ConfigAdvice.java @@ -9,7 +9,8 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher.eclipse; -import java.util.Properties; +import java.util.Map; +import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils; import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo; import org.eclipse.equinox.internal.provisional.frameworkadmin.ConfigData; import org.eclipse.equinox.p2.publisher.AbstractAdvice; @@ -32,10 +33,8 @@ public class ConfigAdvice extends AbstractAdvice implements IConfigAdvice { return configSpec; } - public Properties getProperties() { - Properties result = new Properties(); - result.putAll(data.getProperties()); - return result; + public Map<String, String> getProperties() { + return CollectionUtils.toMap(data.getProperties()); } } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ConfigCUsAction.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ConfigCUsAction.java index 0a494365a..7f795640f 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ConfigCUsAction.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ConfigCUsAction.java @@ -16,13 +16,14 @@ import java.util.Map.Entry; import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.publisher.eclipse.GeneratorBundleInfo; import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo; -import org.eclipse.equinox.internal.provisional.p2.metadata.*; +import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; +import org.eclipse.equinox.p2.metadata.*; +import org.eclipse.equinox.p2.metadata.Version; import org.eclipse.equinox.p2.publisher.*; import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; import org.eclipse.osgi.util.ManifestElement; -import org.osgi.framework.BundleException; -import org.osgi.framework.Constants; +import org.osgi.framework.*; /** * Publish CUs for all the configuration data in the current result. @@ -32,8 +33,9 @@ import org.osgi.framework.Constants; public class ConfigCUsAction extends AbstractPublisherAction { protected static final String ORG_ECLIPSE_UPDATE_CONFIGURATOR = "org.eclipse.update.configurator"; //$NON-NLS-1$ - private static Collection PROPERTIES_TO_SKIP; - private static HashSet PROGRAM_ARGS_TO_SKIP; + protected static final String DEFAULT_START_LEVEL = "osgi.bundles.defaultStartLevel"; //$NON-NLS-1$ + private static Collection<String> PROPERTIES_TO_SKIP; + private static HashSet<String> PROGRAM_ARGS_TO_SKIP; protected Version version; protected String id; protected String flavor; @@ -42,7 +44,7 @@ public class ConfigCUsAction extends AbstractPublisherAction { // TODO consider moving this filtering to the LaunchingAdvice and ConfigAdvice so // it is not hardcoded in the action. static { - PROPERTIES_TO_SKIP = new HashSet(); + PROPERTIES_TO_SKIP = new HashSet<String>(); PROPERTIES_TO_SKIP.add("osgi.frameworkClassPath"); //$NON-NLS-1$ PROPERTIES_TO_SKIP.add("osgi.framework"); //$NON-NLS-1$ PROPERTIES_TO_SKIP.add("osgi.bundles"); //$NON-NLS-1$ @@ -53,7 +55,7 @@ public class ConfigCUsAction extends AbstractPublisherAction { PROPERTIES_TO_SKIP.add("org.eclipse.update.reconcile"); //$NON-NLS-1$ PROPERTIES_TO_SKIP.add("org.eclipse.equinox.simpleconfigurator.configUrl"); //$NON-NLS-1$ - PROGRAM_ARGS_TO_SKIP = new HashSet(); + PROGRAM_ARGS_TO_SKIP = new HashSet<String>(); PROGRAM_ARGS_TO_SKIP.add("--launcher.library"); //$NON-NLS-1$ PROGRAM_ARGS_TO_SKIP.add("-startup"); //$NON-NLS-1$ PROGRAM_ARGS_TO_SKIP.add("-configuration"); //$NON-NLS-1$ @@ -94,11 +96,11 @@ public class ConfigCUsAction extends AbstractPublisherAction { if (monitor.isCanceled()) return Status.CANCEL_STATUS; String configSpec = configSpecs[i]; - Collection configAdvice = publisherInfo.getAdvice(configSpec, false, id, version, IConfigAdvice.class); + Collection<IConfigAdvice> configAdvice = publisherInfo.getAdvice(configSpec, false, id, version, IConfigAdvice.class); BundleInfo[] bundles = fillInBundles(configAdvice, results); publishBundleCUs(publisherInfo, bundles, configSpec, innerResult); publishConfigIUs(configAdvice, innerResult, configSpec); - Collection launchingAdvice = publisherInfo.getAdvice(configSpec, false, id, version, IExecutableAdvice.class); + Collection<IExecutableAdvice> launchingAdvice = publisherInfo.getAdvice(configSpec, false, id, version, IExecutableAdvice.class); publishIniIUs(launchingAdvice, innerResult, configSpec); } // merge the IUs into the final result as non-roots and create a parent IU that captures them all @@ -107,7 +109,7 @@ public class ConfigCUsAction extends AbstractPublisherAction { return Status.OK_STATUS; } - private void publishTopLevelConfigurationIU(Collection children, IPublisherResult result) { + private void publishTopLevelConfigurationIU(Collection<? extends IVersionedId> children, IPublisherResult result) { InstallableUnitDescription descriptor = createParentIU(children, computeIUId(id, flavor), version); descriptor.setSingleton(true); IInstallableUnit rootIU = MetadataFactory.createInstallableUnit(descriptor); @@ -118,26 +120,41 @@ public class ConfigCUsAction extends AbstractPublisherAction { // there seem to be cases where the bundle infos are not filled in with symbolic name and version. // fill in the missing data. - private BundleInfo[] fillInBundles(Collection configAdvice, IPublisherResult results) { - ArrayList result = new ArrayList(); - for (Iterator j = configAdvice.iterator(); j.hasNext();) { - IConfigAdvice advice = (IConfigAdvice) j.next(); + private BundleInfo[] fillInBundles(Collection<IConfigAdvice> configAdvice, IPublisherResult results) { + ArrayList<BundleInfo> result = new ArrayList<BundleInfo>(); + for (IConfigAdvice advice : configAdvice) { + + int defaultStart = BundleInfo.NO_LEVEL; + Map<String, String> adviceProperties = advice.getProperties(); + if (adviceProperties.containsKey(DEFAULT_START_LEVEL)) { + try { + defaultStart = Integer.parseInt(adviceProperties.get(DEFAULT_START_LEVEL)); + } catch (NumberFormatException e) { + //don't know default + } + } + BundleInfo[] bundles = advice.getBundles(); for (int i = 0; i < bundles.length; i++) { BundleInfo bundleInfo = bundles[i]; + + if (bundleInfo.getStartLevel() != BundleInfo.NO_LEVEL && bundleInfo.getStartLevel() == defaultStart) { + bundleInfo.setStartLevel(BundleInfo.NO_LEVEL); + } + // prime the result with the current info. This will be replaced if there is more info... if ((bundleInfo.getSymbolicName() != null && bundleInfo.getVersion() != null) || bundleInfo.getLocation() == null) result.add(bundles[i]); else { try { File location = new File(bundleInfo.getLocation()); - Dictionary manifest = BundlesAction.loadManifest(location); + Dictionary<String, String> manifest = BundlesAction.loadManifest(location); if (manifest == null) continue; GeneratorBundleInfo newInfo = new GeneratorBundleInfo(bundleInfo); - ManifestElement[] element = ManifestElement.parseHeader("dummy-bsn", (String) manifest.get(Constants.BUNDLE_SYMBOLICNAME)); //$NON-NLS-1$ + ManifestElement[] element = ManifestElement.parseHeader("dummy-bsn", manifest.get(Constants.BUNDLE_SYMBOLICNAME)); //$NON-NLS-1$ newInfo.setSymbolicName(element[0].getValue()); - newInfo.setVersion((String) manifest.get(Constants.BUNDLE_VERSION)); + newInfo.setVersion(manifest.get(Constants.BUNDLE_VERSION)); result.add(newInfo); } catch (BundleException e) { // TODO Auto-generated catch block @@ -146,13 +163,13 @@ public class ConfigCUsAction extends AbstractPublisherAction { } } } - return (BundleInfo[]) result.toArray(new BundleInfo[result.size()]); + return result.toArray(new BundleInfo[result.size()]); } /** * Publish the IUs that capture the eclipse.ini information such as vmargs and program args, etc */ - private void publishIniIUs(Collection launchingAdvice, IPublisherResult results, String configSpec) { + private void publishIniIUs(Collection<IExecutableAdvice> launchingAdvice, IPublisherResult results, String configSpec) { if (launchingAdvice.isEmpty()) return; @@ -167,7 +184,7 @@ public class ConfigCUsAction extends AbstractPublisherAction { if (configureData.length() == 0 && unconfigureData.length() == 0) return; - Map touchpointData = new HashMap(); + Map<String, String> touchpointData = new HashMap<String, String>(); touchpointData.put("configure", configureData); //$NON-NLS-1$ touchpointData.put("unconfigure", unconfigureData); //$NON-NLS-1$ IInstallableUnit cu = createCU(id, version, "ini", flavor, configSpec, touchpointData); //$NON-NLS-1$ @@ -177,7 +194,7 @@ public class ConfigCUsAction extends AbstractPublisherAction { /** * Publish the IUs that capture the config.ini information such as properties etc */ - private void publishConfigIUs(Collection configAdvice, IPublisherResult results, String configSpec) { + private void publishConfigIUs(Collection<IConfigAdvice> configAdvice, IPublisherResult results, String configSpec) { if (configAdvice.isEmpty()) return; @@ -192,7 +209,7 @@ public class ConfigCUsAction extends AbstractPublisherAction { if (configureData.length() == 0 && unconfigureData.length() == 0) return; - Map touchpointData = new HashMap(); + Map<String, String> touchpointData = new HashMap<String, String>(); touchpointData.put("configure", configureData); //$NON-NLS-1$ touchpointData.put("unconfigure", unconfigureData); //$NON-NLS-1$ IInstallableUnit cu = createCU(id, version, "config", flavor, configSpec, touchpointData); //$NON-NLS-1$ @@ -205,13 +222,12 @@ public class ConfigCUsAction extends AbstractPublisherAction { * with the name id.type and the given version. This allows others to create an abstract * dependency on having one of these things around but not having to list out the configs. */ - private IInstallableUnit createCU(String id, Version version, String type, String flavor, String configSpec, Map touchpointData) { + private IInstallableUnit createCU(String id, Version version, String type, String flavor, String configSpec, Map<String, String> touchpointData) { InstallableUnitDescription cu = new InstallableUnitDescription(); String resultId = createCUIdString(id, type, flavor, configSpec); cu.setId(resultId); cu.setVersion(version); cu.setFilter(createFilterSpec(configSpec)); - cu.setProperty(IInstallableUnit.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); IProvidedCapability selfCapability = PublisherHelper.createSelfCapability(resultId, version); String namespace = getAbstractCUCapabilityNamespace(id, type, flavor, configSpec); String abstractId = getAbstractCUCapabilityId(id, type, flavor, configSpec); @@ -222,18 +238,16 @@ public class ConfigCUsAction extends AbstractPublisherAction { return MetadataFactory.createInstallableUnit(cu); } - protected String[] getConfigurationStrings(Collection configAdvice) { + protected String[] getConfigurationStrings(Collection<IConfigAdvice> configAdvice) { String configurationData = ""; //$NON-NLS-1$ String unconfigurationData = ""; //$NON-NLS-1$ - Set properties = new HashSet(); - for (Iterator i = configAdvice.iterator(); i.hasNext();) { - IConfigAdvice advice = (IConfigAdvice) i.next(); - for (Iterator iterator = advice.getProperties().entrySet().iterator(); iterator.hasNext();) { - Entry aProperty = (Entry) iterator.next(); - String key = ((String) aProperty.getKey()); + Set<String> properties = new HashSet<String>(); + for (IConfigAdvice advice : configAdvice) { + for (Entry<String, String> aProperty : advice.getProperties().entrySet()) { + String key = aProperty.getKey(); if (shouldPublishProperty(key) && !properties.contains(key)) { properties.add(key); - configurationData += "setProgramProperty(propName:" + key + ", propValue:" + ((String) aProperty.getValue()) + ");"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ + configurationData += "setProgramProperty(propName:" + key + ", propValue:" + aProperty.getValue() + ");"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ unconfigurationData += "setProgramProperty(propName:" + key + ", propValue:);"; //$NON-NLS-1$ //$NON-NLS-2$ } } @@ -253,14 +267,13 @@ public class ConfigCUsAction extends AbstractPublisherAction { return !PROGRAM_ARGS_TO_SKIP.contains(key); } - protected String[] getLauncherConfigStrings(Collection launchingAdvice) { + protected String[] getLauncherConfigStrings(Collection<IExecutableAdvice> launchingAdvice) { String configurationData = ""; //$NON-NLS-1$ String unconfigurationData = ""; //$NON-NLS-1$ - Set jvmSet = new HashSet(); - Set programSet = new HashSet(); - for (Iterator j = launchingAdvice.iterator(); j.hasNext();) { - IExecutableAdvice advice = (IExecutableAdvice) j.next(); + Set<String> jvmSet = new HashSet<String>(); + Set<String> programSet = new HashSet<String>(); + for (IExecutableAdvice advice : launchingAdvice) { String[] jvmArgs = advice.getVMArguments(); for (int i = 0; i < jvmArgs.length; i++) if (shouldPublishJvmArg(jvmArgs[i]) && !jvmSet.contains(jvmArgs[i])) { @@ -293,7 +306,7 @@ public class ConfigCUsAction extends AbstractPublisherAction { return; String cuIdPrefix = ""; //$NON-NLS-1$ - String filter = null; + Filter filter = null; if (configSpec != null) { cuIdPrefix = createIdString(configSpec); filter = createFilterSpec(configSpec); @@ -307,7 +320,7 @@ public class ConfigCUsAction extends AbstractPublisherAction { IInstallableUnit iu = bundle.getIU(); // If there is no host, or the filters don't match, skip this one. - if (iu == null || !filterMatches(iu.getFilter(), configSpec)) + if (iu == null || !filterMatches(iu.getFilter() == null ? null : iu.getFilter(), configSpec)) continue; // TODO need to factor this out into its own action diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/EclipseInstallAction.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/EclipseInstallAction.java index 57a314618..8ed541287 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/EclipseInstallAction.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/EclipseInstallAction.java @@ -14,8 +14,8 @@ import java.io.File; import java.util.*; import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.publisher.eclipse.ExecutablesDescriptor; -import org.eclipse.equinox.internal.provisional.p2.metadata.IVersionedId; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; +import org.eclipse.equinox.p2.metadata.IVersionedId; +import org.eclipse.equinox.p2.metadata.Version; import org.eclipse.equinox.p2.publisher.*; import org.eclipse.equinox.p2.publisher.actions.*; @@ -63,7 +63,7 @@ public class EclipseInstallAction extends AbstractPublisherAction { protected IPublisherAction[] createActions() { createAdvice(); - ArrayList actions = new ArrayList(); + ArrayList<IPublisherAction> actions = new ArrayList<IPublisherAction>(); // create an action that just publishes the raw bundles and features IPublisherAction action = new MergeResultsAction(new IPublisherAction[] {createFeaturesAction(), createBundlesAction()}, IPublisherResult.MERGE_ALL_NON_ROOT); actions.add(action); @@ -74,7 +74,7 @@ public class EclipseInstallAction extends AbstractPublisherAction { actions.add(createConfigCUsAction()); actions.add(createDefaultCUsAction()); actions.add(createRootIUAction()); - return (IPublisherAction[]) actions.toArray(new IPublisherAction[actions.size()]); + return actions.toArray(new IPublisherAction[actions.size()]); } private void createAdvice() { @@ -96,7 +96,7 @@ public class EclipseInstallAction extends AbstractPublisherAction { return new RootIUAction(id, version, name); } - protected Collection getTopLevel() { + protected Collection<IVersionedId> getTopLevel() { return Arrays.asList(topLevel); } @@ -108,8 +108,8 @@ public class EclipseInstallAction extends AbstractPublisherAction { return new ApplicationLauncherAction(id, version, flavor, executableName, getExecutablesLocation(), configSpecs); } - protected Collection createAccumulateConfigDataActions(String[] configs) { - Collection result = new ArrayList(configs.length); + protected Collection<IPublisherAction> createAccumulateConfigDataActions(String[] configs) { + Collection<IPublisherAction> result = new ArrayList<IPublisherAction>(configs.length); for (int i = 0; i < configs.length; i++) { String configSpec = configs[i]; File configuration = computeConfigurationLocation(configSpec); @@ -133,8 +133,8 @@ public class EclipseInstallAction extends AbstractPublisherAction { return new FeaturesAction(new File[] {new File(source, "features")}); //$NON-NLS-1$ } - protected Collection createExecutablesActions(String[] configSpecs) { - Collection result = new ArrayList(configSpecs.length); + protected Collection<IPublisherAction> createExecutablesActions(String[] configSpecs) { + Collection<IPublisherAction> result = new ArrayList<IPublisherAction>(configSpecs.length); for (int i = 0; i < configSpecs.length; i++) { ExecutablesDescriptor executables = computeExecutables(configSpecs[i]); IPublisherAction action = new EquinoxExecutableAction(executables, configSpecs[i], id, version, flavor); @@ -175,7 +175,7 @@ public class EclipseInstallAction extends AbstractPublisherAction { protected File[] computeRootFileExclusions() { if (nonRootFiles == null || nonRootFiles.length == 0) return null; - ArrayList result = new ArrayList(); + ArrayList<File> result = new ArrayList<File>(); for (int i = 0; i < nonRootFiles.length; i++) { String filename = nonRootFiles[i]; File file = new File(filename); @@ -184,7 +184,7 @@ public class EclipseInstallAction extends AbstractPublisherAction { else result.add(new File(source, filename)); } - return (File[]) result.toArray(new File[result.size()]); + return result.toArray(new File[result.size()]); } protected ExecutablesDescriptor computeExecutables(String configSpec) { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/EquinoxExecutableAction.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/EquinoxExecutableAction.java index ef78520a7..c76156368 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/EquinoxExecutableAction.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/EquinoxExecutableAction.java @@ -16,13 +16,15 @@ import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.core.helpers.FileUtils; import org.eclipse.equinox.internal.p2.publisher.eclipse.BrandingIron; import org.eclipse.equinox.internal.p2.publisher.eclipse.ExecutablesDescriptor; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactDescriptor; -import org.eclipse.equinox.internal.provisional.p2.metadata.*; +import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitFragmentDescription; +import org.eclipse.equinox.p2.metadata.*; import org.eclipse.equinox.p2.publisher.*; +import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; import org.eclipse.osgi.service.environment.Constants; +import org.osgi.framework.Filter; /** * Given the description of an executable, this action publishes optionally @@ -84,11 +86,8 @@ public class EquinoxExecutableAction extends AbstractPublisherAction { iud.setVersion(version); iud.setTouchpointType(PublisherHelper.TOUCHPOINT_OSGI); iud.setCapabilities(new IProvidedCapability[] {createSelfCapability(id, version)}); - - String filter = createFilterSpec(configSpec); - if (filter.length() > 0) - iud.setFilter(filter); - Map touchpointData = new HashMap(); + iud.setFilter(createFilterSpec(configSpec)); + Map<String, String> touchpointData = new HashMap<String, String>(); touchpointData.put("configure", "setLauncherName(name:" + executableName + ")"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ touchpointData.put("unconfigure", "setLauncherName()"); //$NON-NLS-1$ //$NON-NLS-2$ iud.addTouchpointData(MetadataFactory.createTouchpointData(touchpointData)); @@ -105,7 +104,7 @@ public class EquinoxExecutableAction extends AbstractPublisherAction { String id = getExecutableId(); iu.setId(id); iu.setVersion(version); - String filter = createFilterSpec(configSpec); + Filter filter = createFilterSpec(configSpec); iu.setFilter(filter); iu.setSingleton(true); iu.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE); @@ -118,7 +117,7 @@ public class EquinoxExecutableAction extends AbstractPublisherAction { //Create the artifact descriptor. we have several files so no path on disk IArtifactKey key = PublisherHelper.createBinaryArtifactKey(id, version); iu.setArtifacts(new IArtifactKey[] {key}); - IArtifactDescriptor descriptor = PublisherHelper.createArtifactDescriptor(key, null); + IArtifactDescriptor descriptor = PublisherHelper.createArtifactDescriptor(info.getArtifactRepository(), key, null); publishArtifact(descriptor, execDescriptor.getFiles(), null, info, createRootPrefixComputer(execDescriptor.getLocation())); if (execDescriptor.isTemporary()) FileUtils.deleteAll(execDescriptor.getLocation()); @@ -131,7 +130,7 @@ public class EquinoxExecutableAction extends AbstractPublisherAction { String launcherFragment = EquinoxLauncherCUAction.ORG_ECLIPSE_EQUINOX_LAUNCHER + '.' + ws + '.' + os; if (!(Constants.OS_MACOSX.equals(os) && !Constants.ARCH_X86_64.equals(arch))) launcherFragment += '.' + arch; - iu.setRequiredCapabilities(new IRequiredCapability[] {MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, launcherFragment, VersionRange.emptyRange, filter, false, false)}); + iu.setRequiredCapabilities(new IRequirement[] {MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, launcherFragment, VersionRange.emptyRange, filter, false, false)}); result.addIU(MetadataFactory.createInstallableUnit(iu), IPublisherResult.ROOT); } @@ -147,21 +146,21 @@ public class EquinoxExecutableAction extends AbstractPublisherAction { cu.setVersion(version); cu.setFilter(createFilterSpec(configSpec)); String executableId = getExecutableId(); - cu.setHost(new IRequiredCapability[] {MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, executableId, new VersionRange(version, true, version, true), null, false, false)}); - cu.setProperty(IInstallableUnit.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); + cu.setHost(new IRequirement[] {MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, executableId, new VersionRange(version, true, version, true), null, false, false)}); + cu.setProperty(InstallableUnitDescription.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); //TODO bug 218890, would like the fragment to provide the launcher capability as well, but can't right now. cu.setCapabilities(new IProvidedCapability[] {PublisherHelper.createSelfCapability(id, version)}); cu.setTouchpointType(PublisherHelper.TOUCHPOINT_NATIVE); String[] config = parseConfigSpec(configSpec); String os = config[1]; - Map touchpointData = computeInstallActions(execDescriptor, os); + Map<String, String> touchpointData = computeInstallActions(execDescriptor, os); cu.addTouchpointData(MetadataFactory.createTouchpointData(touchpointData)); IInstallableUnit unit = MetadataFactory.createInstallableUnit(cu); result.addIU(unit, IPublisherResult.ROOT); } - private Map computeInstallActions(ExecutablesDescriptor execDescriptor, String os) { - Map touchpointData = new HashMap(); + private Map<String, String> computeInstallActions(ExecutablesDescriptor execDescriptor, String os) { + Map<String, String> touchpointData = new HashMap<String, String>(); String configurationData = "unzip(source:@artifact, target:${installFolder});"; //$NON-NLS-1$ if (Constants.OS_MACOSX.equals(os)) { String execName = execDescriptor.getExecutableName(); @@ -200,9 +199,9 @@ public class EquinoxExecutableAction extends AbstractPublisherAction { private IBrandingAdvice getBrandingAdvice() { // there is expected to only be one branding advice for a given configspec so // just return the first one we find. - Collection advice = info.getAdvice(configSpec, true, null, null, IBrandingAdvice.class); - for (Iterator i = advice.iterator(); i.hasNext();) - return (IBrandingAdvice) i.next(); + Collection<IBrandingAdvice> advice = info.getAdvice(configSpec, true, null, null, IBrandingAdvice.class); + for (Iterator<IBrandingAdvice> i = advice.iterator(); i.hasNext();) + return i.next(); return null; } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/EquinoxLauncherCUAction.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/EquinoxLauncherCUAction.java index 46dd2d2e2..271f874c6 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/EquinoxLauncherCUAction.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/EquinoxLauncherCUAction.java @@ -11,13 +11,13 @@ package org.eclipse.equinox.p2.publisher.eclipse; import java.util.Collection; -import java.util.Iterator; import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.publisher.eclipse.GeneratorBundleInfo; -import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; +import org.eclipse.equinox.p2.metadata.Version; import org.eclipse.equinox.p2.publisher.*; import org.eclipse.equinox.p2.publisher.actions.IVersionAdvice; +import org.osgi.framework.Filter; /** * Create CUs for all Equinox launcher related IUs for the given set of configurations @@ -65,9 +65,8 @@ public class EquinoxLauncherCUAction extends AbstractPublisherAction { * configure it in as the launcher.library for this configuration. */ private void publishCU(String id, String configSpec, IPublisherResult results) { - Collection advice = info.getAdvice(configSpec, true, id, null, IVersionAdvice.class); - for (Iterator j = advice.iterator(); j.hasNext();) { - IVersionAdvice versionSpec = (IVersionAdvice) j.next(); + Collection<IVersionAdvice> advice = info.getAdvice(configSpec, true, id, null, IVersionAdvice.class); + for (IVersionAdvice versionSpec : advice) { Version version = versionSpec.getVersion(IInstallableUnit.NAMESPACE_IU_ID, id); if (version == null) continue; @@ -81,7 +80,7 @@ public class EquinoxLauncherCUAction extends AbstractPublisherAction { bundle.setSpecialConfigCommands("addProgramArg(programArg:--launcher.library);addProgramArg(programArg:@artifact);"); //$NON-NLS-1$ bundle.setSpecialUnconfigCommands("removeProgramArg(programArg:--launcher.library);removeProgramArg(programArg:@artifact);"); //$NON-NLS-1$ } - String filter = configSpec == null ? null : createFilterSpec(configSpec); + Filter filter = configSpec == null ? null : createFilterSpec(configSpec); IInstallableUnit cu = BundlesAction.createBundleConfigurationUnit(id, version, false, bundle, flavor, filter); if (cu != null) results.addIU(cu, IPublisherResult.ROOT); diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/Feature.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/Feature.java index 844505056..b57f0eafa 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/Feature.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/Feature.java @@ -10,8 +10,7 @@ *******************************************************************************/ package org.eclipse.equinox.p2.publisher.eclipse; -import java.util.ArrayList; -import java.util.Map; +import java.util.*; /** * @@ -38,9 +37,9 @@ public class Feature { private String installHandlerLibrary; private URLEntry updateSite; - private ArrayList discoverySites; + private ArrayList<URLEntry> discoverySites; - private ArrayList entries; + private ArrayList<FeatureEntry> entries; private String providerName; private String os; private String ws; @@ -49,7 +48,7 @@ public class Feature { private String location; - private Map localizations; + private Map<Locale, Map<String, String>> localizations; public Feature(String id, String version) { if (id == null) @@ -63,7 +62,7 @@ public class Feature { return; if (this.discoverySites == null) - this.discoverySites = new ArrayList(); + this.discoverySites = new ArrayList<URLEntry>(); URLEntry entry = new URLEntry(url, siteLabel); this.discoverySites.add(entry); @@ -71,7 +70,7 @@ public class Feature { public void addEntry(FeatureEntry plugin) { if (entries == null) - entries = new ArrayList(); + entries = new ArrayList<FeatureEntry>(); entries.add(plugin); } @@ -114,13 +113,13 @@ public class Feature { public URLEntry[] getDiscoverySites() { if (discoverySites == null) return new URLEntry[0]; - return (URLEntry[]) discoverySites.toArray(new URLEntry[discoverySites.size()]); + return discoverySites.toArray(new URLEntry[discoverySites.size()]); } public FeatureEntry[] getEntries() { if (entries == null) return new FeatureEntry[0]; - return (FeatureEntry[]) entries.toArray(new FeatureEntry[entries.size()]); + return entries.toArray(new FeatureEntry[entries.size()]); } public String getId() { @@ -159,7 +158,7 @@ public class Feature { return null; } - public Map getLocalizations() { + public Map<Locale, Map<String, String>> getLocalizations() { return this.localizations; } @@ -278,7 +277,7 @@ public class Feature { this.license.setURL(licenseURL); } - public void setLocalizations(Map localizations) { + public void setLocalizations(Map<Locale, Map<String, String>> localizations) { this.localizations = localizations; } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/FeatureEntry.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/FeatureEntry.java index 48e8f545a..8ef764a36 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/FeatureEntry.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/FeatureEntry.java @@ -10,7 +10,7 @@ *******************************************************************************/ package org.eclipse.equinox.p2.publisher.eclipse; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; +import org.eclipse.equinox.p2.metadata.Version; /** */ diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/FeaturesAction.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/FeaturesAction.java index 2a5ceac54..543047155 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/FeaturesAction.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/FeaturesAction.java @@ -10,13 +10,11 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher.eclipse; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; -import org.eclipse.equinox.internal.provisional.p2.metadata.VersionRange; - import java.io.File; import java.net.URI; import java.net.URISyntaxException; import java.util.*; +import java.util.Map.Entry; import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.core.helpers.FileUtils; import org.eclipse.equinox.internal.p2.core.helpers.LogHelper; @@ -24,17 +22,20 @@ import org.eclipse.equinox.internal.p2.core.helpers.FileUtils.IPathComputer; import org.eclipse.equinox.internal.p2.metadata.ArtifactKey; import org.eclipse.equinox.internal.p2.publisher.*; import org.eclipse.equinox.internal.p2.publisher.eclipse.FeatureParser; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.ArtifactDescriptor; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactDescriptor; -import org.eclipse.equinox.internal.provisional.p2.metadata.*; +import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitPatchDescription; -import org.eclipse.equinox.internal.provisional.p2.metadata.repository.IMetadataRepository; -import org.eclipse.equinox.internal.provisional.p2.repository.IRepository; +import org.eclipse.equinox.p2.metadata.*; +import org.eclipse.equinox.p2.metadata.expression.ExpressionUtil; import org.eclipse.equinox.p2.publisher.*; import org.eclipse.equinox.p2.publisher.actions.IFeatureRootAdvice; +import org.eclipse.equinox.p2.repository.IRepository; +import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; +import org.eclipse.equinox.p2.repository.artifact.spi.ArtifactDescriptor; +import org.eclipse.equinox.p2.repository.metadata.IMetadataRepository; import org.eclipse.equinox.spi.p2.publisher.PublisherHelper; import org.eclipse.osgi.util.NLS; +import org.osgi.framework.Filter; /** * Publish IUs for all of the features in the given set of locations. The locations can @@ -70,7 +71,7 @@ public class FeaturesAction extends AbstractPublisherAction { if (feature.getProviderName() != null) iu.setProperty(IInstallableUnit.PROP_PROVIDER, feature.getProviderName()); if (feature.getLicense() != null) - iu.setLicense(MetadataFactory.createLicense(toURIOrNull(feature.getLicenseURL()), feature.getLicense())); + iu.setLicenses(new ILicense[] {MetadataFactory.createLicense(toURIOrNull(feature.getLicenseURL()), feature.getLicense())}); if (feature.getCopyright() != null) iu.setCopyright(MetadataFactory.createCopyright(toURIOrNull(feature.getCopyrightURL()), feature.getCopyright())); if (feature.getApplication() != null) @@ -99,7 +100,7 @@ public class FeaturesAction extends AbstractPublisherAction { iu.setProperty(PublisherHelper.ECLIPSE_INSTALL_HANDLER_PROP, installHandlerProperty); } - ArrayList providedCapabilities = new ArrayList(); + ArrayList<IProvidedCapability> providedCapabilities = new ArrayList<IProvidedCapability>(); providedCapabilities.add(PublisherHelper.createSelfCapability(id, version)); providedCapabilities.add(PublisherHelper.FEATURE_CAPABILITY); providedCapabilities.add(MetadataFactory.createProvidedCapability(PublisherHelper.CAPABILITY_NS_UPDATE_FEATURE, feature.getId(), version)); @@ -107,19 +108,17 @@ public class FeaturesAction extends AbstractPublisherAction { iu.setCapabilities(new IProvidedCapability[] {PublisherHelper.createSelfCapability(id, version), PublisherHelper.FEATURE_CAPABILITY, MetadataFactory.createProvidedCapability(PublisherHelper.CAPABILITY_NS_UPDATE_FEATURE, feature.getId(), version)}); iu.setArtifacts(new IArtifactKey[] {createFeatureArtifactKey(feature.getId(), version.toString())}); - Map touchpointData = new HashMap(); + Map<String, String> touchpointData = new HashMap<String, String>(); touchpointData.put("zipped", "true"); //$NON-NLS-1$ //$NON-NLS-2$ iu.addTouchpointData(MetadataFactory.createTouchpointData(touchpointData)); - Map localizations = feature.getLocalizations(); + Map<Locale, Map<String, String>> localizations = feature.getLocalizations(); if (localizations != null) { - for (Iterator iter = localizations.keySet().iterator(); iter.hasNext();) { - Locale locale = (Locale) iter.next(); - Properties translatedStrings = (Properties) localizations.get(locale); - Enumeration propertyKeys = translatedStrings.propertyNames(); - while (propertyKeys.hasMoreElements()) { - String nextKey = (String) propertyKeys.nextElement(); - iu.setProperty(locale.toString() + '.' + nextKey, translatedStrings.getProperty(nextKey)); + for (Entry<Locale, Map<String, String>> locEntry : localizations.entrySet()) { + Locale locale = locEntry.getKey(); + Map<String, String> translatedStrings = locEntry.getValue(); + for (Entry<String, String> entry : translatedStrings.entrySet()) { + iu.setProperty(locale.toString() + '.' + entry.getKey(), entry.getValue()); } providedCapabilities.add(PublisherHelper.makeTranslationCapability(id, locale)); } @@ -198,7 +197,7 @@ public class FeaturesAction extends AbstractPublisherAction { if (configSpec != null && configSpec.length() > 0) iu.setFilter(createFilterSpec(configSpec)); - Map touchpointData = new HashMap(2); + Map<String, String> touchpointData = new HashMap<String, String>(2); String configurationData = "unzip(source:@artifact, target:${installFolder});"; //$NON-NLS-1$ touchpointData.put("install", configurationData); //$NON-NLS-1$ String unConfigurationData = "cleanupzip(source:@artifact, target:${installFolder});"; //$NON-NLS-1$ @@ -216,7 +215,7 @@ public class FeaturesAction extends AbstractPublisherAction { return iuResult; } - protected IInstallableUnit createGroupIU(Feature feature, List childIUs, IPublisherInfo publisherInfo) { + protected IInstallableUnit createGroupIU(Feature feature, List<IInstallableUnit> childIUs, IPublisherInfo publisherInfo) { if (isPatch(feature)) return createPatchIU(feature, childIUs, publisherInfo); InstallableUnitDescription iu = new MetadataFactory.InstallableUnitDescription(); @@ -233,13 +232,13 @@ public class FeaturesAction extends AbstractPublisherAction { if (feature.getProviderName() != null) iu.setProperty(IInstallableUnit.PROP_PROVIDER, feature.getProviderName()); if (feature.getLicense() != null) - iu.setLicense(MetadataFactory.createLicense(toURIOrNull(feature.getLicenseURL()), feature.getLicense())); + iu.setLicenses(new ILicense[] {MetadataFactory.createLicense(toURIOrNull(feature.getLicenseURL()), feature.getLicense())}); if (feature.getCopyright() != null) iu.setCopyright(MetadataFactory.createCopyright(toURIOrNull(feature.getCopyrightURL()), feature.getCopyright())); iu.setUpdateDescriptor(MetadataFactory.createUpdateDescriptor(id, BundlesAction.computeUpdateRange(new org.osgi.framework.Version(feature.getVersion())), IUpdateDescriptor.NORMAL, null)); FeatureEntry entries[] = feature.getEntries(); - List required = new ArrayList(entries.length + (childIUs == null ? 0 : childIUs.size())); + List<IRequirement> required = new ArrayList<IRequirement>(entries.length + (childIUs == null ? 0 : childIUs.size())); for (int i = 0; i < entries.length; i++) { VersionRange range = getVersionRange(entries[i]); String requiredId = getTransformedId(entries[i].getId(), entries[i].isPlugin(), /*isGroup*/true); @@ -250,42 +249,39 @@ public class FeaturesAction extends AbstractPublisherAction { // TODO consider if these should be linked as exact version numbers. Should be ok but may be brittle. if (childIUs != null) { for (int i = 0; i < childIUs.size(); i++) { - IInstallableUnit child = (IInstallableUnit) childIUs.get(i); - required.add(MetadataFactory.createRequiredCapability(PublisherHelper.IU_NAMESPACE, child.getId(), new VersionRange(child.getVersion(), true, child.getVersion(), true), child.getFilter(), false, false)); + IInstallableUnit child = childIUs.get(i); + Filter filter = child.getFilter(); + required.add(MetadataFactory.createRequiredCapability(PublisherHelper.IU_NAMESPACE, child.getId(), new VersionRange(child.getVersion(), true, child.getVersion(), true), filter, false, false)); } } - iu.setRequiredCapabilities((IRequiredCapability[]) required.toArray(new IRequiredCapability[required.size()])); + iu.setRequiredCapabilities(required.toArray(new IRequirement[required.size()])); iu.setTouchpointType(ITouchpointType.NONE); - iu.setProperty(IInstallableUnit.PROP_TYPE_GROUP, Boolean.TRUE.toString()); + iu.setProperty(InstallableUnitDescription.PROP_TYPE_GROUP, Boolean.TRUE.toString()); processTouchpointAdvice(iu, null, publisherInfo); processInstallableUnitPropertiesAdvice(iu, publisherInfo); //Create a fake entry to reuse the logic to create the filters FeatureEntry entry = new FeatureEntry("fake", "0.0.0", false); //$NON-NLS-1$ //$NON-NLS-2$ entry.setEnvironment(feature.getOS(), feature.getWS(), feature.getArch(), feature.getNL()); - String filter = getFilter(entry); - if (filter != null) - iu.setFilter(filter); + iu.setFilter(getFilter(entry)); // Create set of provided capabilities - ArrayList providedCapabilities = new ArrayList(); + ArrayList<IProvidedCapability> providedCapabilities = new ArrayList<IProvidedCapability>(); providedCapabilities.add(createSelfCapability(id, version)); - Map localizations = feature.getLocalizations(); + Map<Locale, Map<String, String>> localizations = feature.getLocalizations(); if (localizations != null) { - for (Iterator iter = localizations.keySet().iterator(); iter.hasNext();) { - Locale locale = (Locale) iter.next(); - Properties translatedStrings = (Properties) localizations.get(locale); - Enumeration propertyKeys = translatedStrings.propertyNames(); - while (propertyKeys.hasMoreElements()) { - String nextKey = (String) propertyKeys.nextElement(); - iu.setProperty(locale.toString() + '.' + nextKey, translatedStrings.getProperty(nextKey)); + for (Entry<Locale, Map<String, String>> locEntry : localizations.entrySet()) { + Locale locale = locEntry.getKey(); + Map<String, String> translatedStrings = locEntry.getValue(); + for (Entry<String, String> e : translatedStrings.entrySet()) { + iu.setProperty(locale.toString() + '.' + e.getKey(), e.getValue()); } providedCapabilities.add(PublisherHelper.makeTranslationCapability(id, locale)); } } - iu.setCapabilities((IProvidedCapability[]) providedCapabilities.toArray(new IProvidedCapability[providedCapabilities.size()])); + iu.setCapabilities(providedCapabilities.toArray(new IProvidedCapability[providedCapabilities.size()])); processCapabilityAdvice(iu, publisherInfo); return MetadataFactory.createInstallableUnit(iu); } @@ -294,7 +290,7 @@ public class FeaturesAction extends AbstractPublisherAction { return getTransformedId(featureId, /*isPlugin*/false, /*isGroup*/true); } - private IInstallableUnit createPatchIU(Feature feature, List childIUs, IPublisherInfo publisherInfo) { + private IInstallableUnit createPatchIU(Feature feature, List<IInstallableUnit> childIUs, IPublisherInfo publisherInfo) { InstallableUnitPatchDescription iu = new MetadataFactory.InstallableUnitPatchDescription(); String id = getTransformedId(feature.getId(), /*isPlugin*/false, /*isGroup*/true); iu.setId(id); @@ -308,24 +304,27 @@ public class FeaturesAction extends AbstractPublisherAction { if (feature.getProviderName() != null) iu.setProperty(IInstallableUnit.PROP_PROVIDER, feature.getProviderName()); if (feature.getLicense() != null) - iu.setLicense(MetadataFactory.createLicense(toURIOrNull(feature.getLicenseURL()), feature.getLicense())); + iu.setLicenses(new ILicense[] {MetadataFactory.createLicense(toURIOrNull(feature.getLicenseURL()), feature.getLicense())}); if (feature.getCopyright() != null) iu.setCopyright(MetadataFactory.createCopyright(toURIOrNull(feature.getCopyrightURL()), feature.getCopyright())); iu.setUpdateDescriptor(MetadataFactory.createUpdateDescriptor(id, BundlesAction.computeUpdateRange(new org.osgi.framework.Version(feature.getVersion())), IUpdateDescriptor.NORMAL, null)); FeatureEntry entries[] = feature.getEntries(); - ArrayList applicabilityScope = new ArrayList(); - ArrayList patchRequirements = new ArrayList(); - ArrayList requirementChanges = new ArrayList(); + ArrayList<IRequirement> applicabilityScope = new ArrayList<IRequirement>(); + ArrayList<IRequirement> patchRequirements = new ArrayList<IRequirement>(); + ArrayList<IRequirementChange> requirementChanges = new ArrayList<IRequirementChange>(); for (int i = 0; i < entries.length; i++) { VersionRange range = getVersionRange(entries[i]); - IRequiredCapability req = MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, getTransformedId(entries[i].getId(), entries[i].isPlugin(), /*isGroup*/true), range, getFilter(entries[i]), entries[i].isOptional(), false); + IRequirement req = MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, getTransformedId(entries[i].getId(), entries[i].isPlugin(), /*isGroup*/true), range, getFilter(entries[i]), entries[i].isOptional(), false); if (entries[i].isRequires()) { applicabilityScope.add(req); + if (applicabilityScope.size() == 1) { + iu.setLifeCycle(MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, getTransformedId(entries[i].getId(), entries[i].isPlugin(), /*isGroup*/true), range, null, false, false, false)); + } continue; } if (entries[i].isPlugin()) { - IRequiredCapability from = MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, getTransformedId(entries[i].getId(), entries[i].isPlugin(), /*isGroup*/true), VersionRange.emptyRange, getFilter(entries[i]), entries[i].isOptional(), false); + IRequirement from = MetadataFactory.createRequiredCapability(IInstallableUnit.NAMESPACE_IU_ID, getTransformedId(entries[i].getId(), entries[i].isPlugin(), /*isGroup*/true), VersionRange.emptyRange, getFilter(entries[i]), entries[i].isOptional(), false); requirementChanges.add(MetadataFactory.createRequirementChange(from, req)); continue; } @@ -335,61 +334,51 @@ public class FeaturesAction extends AbstractPublisherAction { //Always add a requirement on the IU containing the feature jar if (childIUs != null) { for (int i = 0; i < childIUs.size(); i++) { - IInstallableUnit child = (IInstallableUnit) childIUs.get(i); + IInstallableUnit child = childIUs.get(i); patchRequirements.add(MetadataFactory.createRequiredCapability(PublisherHelper.IU_NAMESPACE, child.getId(), new VersionRange(child.getVersion(), true, child.getVersion(), true), child.getFilter(), false, false)); } } - iu.setRequiredCapabilities((IRequiredCapability[]) patchRequirements.toArray(new IRequiredCapability[patchRequirements.size()])); - iu.setApplicabilityScope(new IRequiredCapability[][] {(IRequiredCapability[]) applicabilityScope.toArray(new IRequiredCapability[applicabilityScope.size()])}); - iu.setRequirementChanges((IRequirementChange[]) requirementChanges.toArray(new IRequirementChange[requirementChanges.size()])); - - //Generate lifecycle - IRequiredCapability lifeCycle = null; - if (applicabilityScope.size() > 0) { - IRequiredCapability req = (IRequiredCapability) applicabilityScope.get(0); - lifeCycle = MetadataFactory.createRequiredCapability(req.getNamespace(), req.getName(), req.getRange(), null, false, false, false); - iu.setLifeCycle(lifeCycle); - } + iu.setRequiredCapabilities(patchRequirements.toArray(new IRequirement[patchRequirements.size()])); + iu.setApplicabilityScope(new IRequirement[][] {applicabilityScope.toArray(new IRequirement[applicabilityScope.size()])}); + iu.setRequirementChanges(requirementChanges.toArray(new IRequirementChange[requirementChanges.size()])); iu.setTouchpointType(ITouchpointType.NONE); processTouchpointAdvice(iu, null, publisherInfo); processInstallableUnitPropertiesAdvice(iu, publisherInfo); - iu.setProperty(IInstallableUnit.PROP_TYPE_GROUP, Boolean.TRUE.toString()); - iu.setProperty(IInstallableUnit.PROP_TYPE_PATCH, Boolean.TRUE.toString()); + iu.setProperty(InstallableUnitDescription.PROP_TYPE_GROUP, Boolean.TRUE.toString()); + iu.setProperty(InstallableUnitDescription.PROP_TYPE_PATCH, Boolean.TRUE.toString()); // TODO: shouldn't the filter for the group be constructed from os, ws, arch, nl // of the feature? // iu.setFilter(filter); // Create set of provided capabilities - ArrayList providedCapabilities = new ArrayList(); + ArrayList<IProvidedCapability> providedCapabilities = new ArrayList<IProvidedCapability>(); providedCapabilities.add(createSelfCapability(id, version)); - Map localizations = feature.getLocalizations(); + Map<Locale, Map<String, String>> localizations = feature.getLocalizations(); if (localizations != null) { - for (Iterator iter = localizations.keySet().iterator(); iter.hasNext();) { - Locale locale = (Locale) iter.next(); - Properties translatedStrings = (Properties) localizations.get(locale); - Enumeration propertyKeys = translatedStrings.propertyNames(); - while (propertyKeys.hasMoreElements()) { - String nextKey = (String) propertyKeys.nextElement(); - iu.setProperty(locale.toString() + '.' + nextKey, translatedStrings.getProperty(nextKey)); + for (Entry<Locale, Map<String, String>> locEntry : localizations.entrySet()) { + Locale locale = locEntry.getKey(); + Map<String, String> translatedStrings = locEntry.getValue(); + for (Entry<String, String> e : translatedStrings.entrySet()) { + iu.setProperty(locale.toString() + '.' + e.getKey(), e.getValue()); } providedCapabilities.add(PublisherHelper.makeTranslationCapability(id, locale)); } } - iu.setCapabilities((IProvidedCapability[]) providedCapabilities.toArray(new IProvidedCapability[providedCapabilities.size()])); + iu.setCapabilities(providedCapabilities.toArray(new IProvidedCapability[providedCapabilities.size()])); processCapabilityAdvice(iu, publisherInfo); return MetadataFactory.createInstallableUnitPatch(iu); } private File[] expandLocations(File[] list) { - ArrayList result = new ArrayList(); + ArrayList<File> result = new ArrayList<File>(); expandLocations(list, result); - return (File[]) result.toArray(new File[result.size()]); + return result.toArray(new File[result.size()]); } - private void expandLocations(File[] list, ArrayList result) { + private void expandLocations(File[] list, ArrayList<File> result) { if (list == null) return; for (int i = 0; i < list.length; i++) { @@ -414,7 +403,7 @@ public class FeaturesAction extends AbstractPublisherAction { createBundleShapeAdvice(feature, info); createAdviceFileAdvice(feature, info); - ArrayList childIUs = new ArrayList(); + ArrayList<IInstallableUnit> childIUs = new ArrayList<IInstallableUnit>(); IInstallableUnit featureJarIU = queryForIU(result, getTransformedId(feature.getId(), false, false), Version.parseVersion(feature.getVersion())); if (featureJarIU == null) @@ -446,14 +435,14 @@ public class FeaturesAction extends AbstractPublisherAction { return createFeatureJarIU(feature, publisherInfo); } - protected ArrayList generateRootFileIUs(Feature feature, IPublisherResult result, IPublisherInfo publisherInfo) { - ArrayList ius = new ArrayList(); + protected ArrayList<IInstallableUnit> generateRootFileIUs(Feature feature, IPublisherResult result, IPublisherInfo publisherInfo) { + ArrayList<IInstallableUnit> ius = new ArrayList<IInstallableUnit>(); - Collection collection = publisherInfo.getAdvice(null, false, feature.getId(), Version.parseVersion(feature.getVersion()), IFeatureRootAdvice.class); + Collection<IFeatureRootAdvice> collection = publisherInfo.getAdvice(null, false, feature.getId(), Version.parseVersion(feature.getVersion()), IFeatureRootAdvice.class); if (collection.size() == 0) return ius; - IFeatureRootAdvice advice = (IFeatureRootAdvice) collection.iterator().next(); + IFeatureRootAdvice advice = collection.iterator().next(); String[] configs = advice.getConfigurations(); for (int i = 0; i < configs.length; i++) { String config = configs[i]; @@ -463,7 +452,7 @@ public class FeaturesAction extends AbstractPublisherAction { IInstallableUnit iu = createFeatureRootFileIU(feature.getId(), feature.getVersion(), null, descriptor); File[] files = descriptor.getFiles(); - IArtifactKey artifactKey = iu.getArtifacts()[0]; + IArtifactKey artifactKey = iu.getArtifacts().iterator().next(); ArtifactDescriptor artifactDescriptor = new ArtifactDescriptor(artifactKey); IPathComputer computer = advice.getRootFileComputer(config); if (computer == null) @@ -515,7 +504,7 @@ public class FeaturesAction extends AbstractPublisherAction { } protected Feature[] getFeatures(File[] featureLocations) { - ArrayList result = new ArrayList(featureLocations.length); + ArrayList<Feature> result = new ArrayList<Feature>(featureLocations.length); for (int i = 0; i < featureLocations.length; i++) { Feature feature = new FeatureParser().parse(featureLocations[i]); if (feature != null) { @@ -523,10 +512,10 @@ public class FeaturesAction extends AbstractPublisherAction { result.add(feature); } } - return (Feature[]) result.toArray(new Feature[result.size()]); + return result.toArray(new Feature[result.size()]); } - private String getFilter(FeatureEntry entry) { + private Filter getFilter(FeatureEntry entry) { StringBuffer result = new StringBuffer(); result.append("(&"); //$NON-NLS-1$ if (entry.getFilter() != null) @@ -538,7 +527,7 @@ public class FeaturesAction extends AbstractPublisherAction { if (result.length() == 2) return null; result.append(')'); - return result.toString(); + return ExpressionUtil.parseLDAP(result.toString()); } private void expandFilter(String filter, String osgiFilterValue, StringBuffer result) { @@ -571,12 +560,14 @@ public class FeaturesAction extends AbstractPublisherAction { return null; if (match.equals("perfect")) //$NON-NLS-1$ return new VersionRange(version, true, version, true); + + org.osgi.framework.Version osgiVersion = Version.toOSGiVersion(version); if (match.equals("equivalent")) { //$NON-NLS-1$ - Version upper = Version.createOSGi(version.getMajor(), version.getMinor() + 1, 0); + Version upper = Version.createOSGi(osgiVersion.getMajor(), osgiVersion.getMinor() + 1, 0); return new VersionRange(version, true, upper, false); } if (match.equals("compatible")) { //$NON-NLS-1$ - Version upper = Version.createOSGi(version.getMajor() + 1, 0, 0); + Version upper = Version.createOSGi(osgiVersion.getMajor() + 1, 0, 0); return new VersionRange(version, true, upper, false); } if (match.equals("greaterOrEqual")) //$NON-NLS-1$ @@ -608,10 +599,10 @@ public class FeaturesAction extends AbstractPublisherAction { // TODO this is a little strange. If there are several artifacts, how do we know which files go with // which artifacts when we publish them? For now it would be surprising to have more than one // artifact per feature IU. - IArtifactKey[] artifacts = featureIU.getArtifacts(); - for (int j = 0; j < artifacts.length; j++) { + Collection<IArtifactKey> artifacts = featureIU.getArtifacts(); + for (IArtifactKey artifactKey : artifacts) { File file = new File(feature.getLocation()); - ArtifactDescriptor ad = (ArtifactDescriptor) PublisherHelper.createArtifactDescriptor(artifacts[j], file); + ArtifactDescriptor ad = (ArtifactDescriptor) PublisherHelper.createArtifactDescriptor(info.getArtifactRepository(), artifactKey, file); processArtifactPropertiesAdvice(featureIU, ad, publisherInfo); ad.setProperty(IArtifactDescriptor.DOWNLOAD_CONTENTTYPE, IArtifactDescriptor.TYPE_ZIP); // if the artifact is a dir then zip it up. @@ -640,7 +631,7 @@ public class FeaturesAction extends AbstractPublisherAction { } if (linkActions.length() > 0) { - Map touchpointData = new HashMap(); + Map<String, String> touchpointData = new HashMap<String, String>(); //we do ln during configure to avoid complicating branding which uses the install phase touchpointData.put("configure", linkActions.toString()); //$NON-NLS-1$ iu.addTouchpointData(MetadataFactory.createTouchpointData(touchpointData)); @@ -648,7 +639,7 @@ public class FeaturesAction extends AbstractPublisherAction { } private void setupPermissions(InstallableUnitDescription iu, FileSetDescriptor descriptor) { - Map touchpointData = new HashMap(); + Map<String, String> touchpointData = new HashMap<String, String>(); String[][] permsList = descriptor.getPermissions(); for (int i = 0; i < permsList.length; i++) { String[] permSpec = permsList[i]; diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/FeaturesAndBundlesPublisherApplication.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/FeaturesAndBundlesPublisherApplication.java index a58f5f0a0..f838580dc 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/FeaturesAndBundlesPublisherApplication.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/FeaturesAndBundlesPublisherApplication.java @@ -52,13 +52,13 @@ public class FeaturesAndBundlesPublisherApplication extends AbstractPublisherApp } protected IPublisherAction[] createActions() { - ArrayList result = new ArrayList(); + ArrayList<IPublisherAction> result = new ArrayList<IPublisherAction>(); if (features == null) features = new File[] {new File(source, "features")}; //$NON-NLS-1$ result.add(new FeaturesAction(features)); if (bundles == null) bundles = new File[] {new File(source, "plugins")}; //$NON-NLS-1$ result.add(new BundlesAction(bundles)); - return (IPublisherAction[]) result.toArray(new IPublisherAction[result.size()]); + return result.toArray(new IPublisherAction[result.size()]); } } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/IBrandingAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/IBrandingAdvice.java index 3e92dc84c..6f4952079 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/IBrandingAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/IBrandingAdvice.java @@ -9,10 +9,12 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher.eclipse; +import org.eclipse.equinox.p2.publisher.IPublisherAdvice; + /** * Advice for branding executables and other element while publishing. */ -public interface IBrandingAdvice { +public interface IBrandingAdvice extends IPublisherAdvice { /** * Returns the OS that this branding advice is relevant for. diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/IConfigAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/IConfigAdvice.java index 5e673ea92..a8826d153 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/IConfigAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/IConfigAdvice.java @@ -9,7 +9,7 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher.eclipse; -import java.util.Properties; +import java.util.Map; import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo; import org.eclipse.equinox.p2.publisher.IPublisherAdvice; @@ -17,5 +17,5 @@ public interface IConfigAdvice extends IPublisherAdvice { public BundleInfo[] getBundles(); - public Properties getProperties(); + public Map<String, String> getProperties(); } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/InstallPublisherApplication.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/InstallPublisherApplication.java index a6d484cda..8e071f4b6 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/InstallPublisherApplication.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/InstallPublisherApplication.java @@ -12,7 +12,9 @@ package org.eclipse.equinox.p2.publisher.eclipse; import java.net.URISyntaxException; import java.util.ArrayList; -import org.eclipse.equinox.internal.provisional.p2.metadata.*; +import org.eclipse.equinox.internal.p2.metadata.VersionedId; +import org.eclipse.equinox.p2.metadata.IVersionedId; +import org.eclipse.equinox.p2.metadata.Version; import org.eclipse.equinox.p2.publisher.*; public class InstallPublisherApplication extends AbstractPublisherApplication { @@ -71,9 +73,9 @@ public class InstallPublisherApplication extends AbstractPublisherApplication { } protected IPublisherAction[] createActions() { - ArrayList result = new ArrayList(); + ArrayList<IPublisherAction> result = new ArrayList<IPublisherAction>(); result.add(createEclipseInstallAction()); - return (IPublisherAction[]) result.toArray(new IPublisherAction[result.size()]); + return result.toArray(new IPublisherAction[result.size()]); } private IPublisherAction createEclipseInstallAction() { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ProductAction.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ProductAction.java index a82b326de..773692691 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ProductAction.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ProductAction.java @@ -10,16 +10,12 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher.eclipse; -import org.eclipse.equinox.internal.provisional.p2.metadata.IVersionedId; - -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; - import java.io.File; import java.util.*; import org.eclipse.core.runtime.*; +import org.eclipse.equinox.internal.p2.metadata.VersionedId; import org.eclipse.equinox.internal.p2.publisher.eclipse.IProductDescriptor; -import org.eclipse.equinox.internal.provisional.p2.metadata.VersionedId; -import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; +import org.eclipse.equinox.p2.metadata.*; import org.eclipse.equinox.p2.publisher.*; import org.eclipse.equinox.p2.publisher.actions.*; @@ -50,7 +46,7 @@ public class ProductAction extends AbstractPublisherAction { createAdvice(); // create all the actions needed to publish a product - ArrayList actions = new ArrayList(); + ArrayList<IPublisherAction> actions = new ArrayList<IPublisherAction>(); // products include the executable so add actions to publish them if (getExecutablesLocation() != null) actions.add(createApplicationExecutableAction(info.getConfigurations())); @@ -59,7 +55,7 @@ public class ProductAction extends AbstractPublisherAction { actions.add(createJREAction()); actions.add(createDefaultCUsAction()); actions.add(createRootIUAction()); - return (IPublisherAction[]) actions.toArray(new IPublisherAction[actions.size()]); + return actions.toArray(new IPublisherAction[actions.size()]); } protected IPublisherAction createApplicationExecutableAction(String[] configSpecs) { @@ -122,7 +118,7 @@ public class ProductAction extends AbstractPublisherAction { } private void createRootAdvice() { - Collection list; + Collection<IVersionedId> list; if (product.useFeatures()) // TODO: We need a real namespace here list = versionElements(listElements(product.getFeatures(), ".feature.group"), IInstallableUnit.NAMESPACE_IU_ID); //$NON-NLS-1$ @@ -144,16 +140,15 @@ public class ProductAction extends AbstractPublisherAction { info.addAdvice(new ProductFileAdvice(product, configSpecs[i])); } - private Collection versionElements(Collection elements, String namespace) { - Collection versionAdvice = info.getAdvice(null, true, null, null, IVersionAdvice.class); - List result = new ArrayList(); - for (Iterator i = elements.iterator(); i.hasNext();) { - IVersionedId element = (IVersionedId) i.next(); + private Collection<IVersionedId> versionElements(Collection<IVersionedId> elements, String namespace) { + Collection<IVersionAdvice> versionAdvice = info.getAdvice(null, true, null, null, IVersionAdvice.class); + List<IVersionedId> result = new ArrayList<IVersionedId>(); + for (IVersionedId element : elements) { Version elementVersion = element.getVersion(); if (elementVersion == null || Version.emptyVersion.equals(elementVersion)) { - Iterator advice = versionAdvice.iterator(); + Iterator<IVersionAdvice> advice = versionAdvice.iterator(); while (advice.hasNext()) { - elementVersion = ((IVersionAdvice) advice.next()).getVersion(namespace, element.getId()); + elementVersion = advice.next().getVersion(namespace, element.getId()); break; } } @@ -172,12 +167,11 @@ public class ProductAction extends AbstractPublisherAction { return result; } - private Collection listElements(List elements, String suffix) { + private Collection<IVersionedId> listElements(List<IVersionedId> elements, String suffix) { if (suffix == null || suffix.length() == 0) return elements; - ArrayList result = new ArrayList(elements.size()); - for (Iterator i = elements.iterator(); i.hasNext();) { - IVersionedId elementName = (IVersionedId) i.next(); + ArrayList<IVersionedId> result = new ArrayList<IVersionedId>(elements.size()); + for (IVersionedId elementName : elements) { result.add(new VersionedId(elementName.getId() + suffix, elementName.getVersion())); } return result; diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ProductFileAdvice.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ProductFileAdvice.java index 31cfa07ad..deeca8326 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ProductFileAdvice.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ProductFileAdvice.java @@ -11,16 +11,15 @@ ******************************************************************************/ package org.eclipse.equinox.p2.publisher.eclipse; -import org.eclipse.equinox.internal.provisional.p2.metadata.IVersionedId; - -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; - import java.io.File; import java.util.*; +import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils; import org.eclipse.equinox.internal.p2.publisher.eclipse.DataLoader; import org.eclipse.equinox.internal.p2.publisher.eclipse.IProductDescriptor; import org.eclipse.equinox.internal.provisional.frameworkadmin.BundleInfo; import org.eclipse.equinox.internal.provisional.frameworkadmin.ConfigData; +import org.eclipse.equinox.p2.metadata.IVersionedId; +import org.eclipse.equinox.p2.metadata.Version; import org.eclipse.equinox.p2.publisher.AbstractAdvice; import org.eclipse.equinox.p2.publisher.AbstractPublisherAction; import org.eclipse.equinox.p2.publisher.actions.ILicenseAdvice; @@ -104,9 +103,9 @@ public class ProductFileAdvice extends AbstractAdvice implements ILicenseAdvice, * properties may be defined in the .product file, the config.ini * file, or both. */ - public Properties getProperties() { - Properties result = new Properties(); - result.putAll(configData.getProperties()); + public Map<String, String> getProperties() { + Map<String, String> result = new HashMap<String, String>(); + CollectionUtils.putAll(configData.getProperties(), result); result.putAll(product.getConfigurationProperties()); return result; } @@ -188,8 +187,8 @@ public class ProductFileAdvice extends AbstractAdvice implements ILicenseAdvice, } private void addProductFileConfigBundles(ConfigData data) { - Set versionBoundBundles = new HashSet(); - Map unboundedBundles = new HashMap(); + Set<BundleInfo> versionBoundBundles = new HashSet<BundleInfo>(); + Map<String, List<BundleInfo>> unboundedBundles = new HashMap<String, List<BundleInfo>>(); BundleInfo[] bundles = data.getBundles(); for (int i = 0; i < bundles.length; i++) { @@ -205,26 +204,23 @@ public class ProductFileAdvice extends AbstractAdvice implements ILicenseAdvice, } } - List bundleInfos = product.getBundleInfos(); - for (Iterator i = bundleInfos.iterator(); i.hasNext();) { + List<BundleInfo> bundleInfos = product.getBundleInfos(); + for (BundleInfo bundleInfo : bundleInfos) { // For each bundle that has configuration information, if the bundle is in the // bundles bound by version, add the "configured" bundle instead // If the bundle is not bound to a version, then replace all bounded versions // with this one. Otherwise, just add this one (don't replace) - BundleInfo bundleInfo = (BundleInfo) i.next(); - if (versionBoundBundles.contains(bundleInfo)) { // If we found a version with the same name and version, replace it with the "configured" bundle data.removeBundle(bundleInfo); data.addBundle(bundleInfo); } else if (bundleInfo.getVersion() == null || bundleInfo.getVersion().equals("0.0.0")) {//$NON-NLS-1$ // If we don't have a version number, look for all bundles that match by name - List list = (List) unboundedBundles.get(bundleInfo.getSymbolicName()); + List<BundleInfo> list = unboundedBundles.get(bundleInfo.getSymbolicName()); if (list == null) data.addBundle(bundleInfo); else - for (Iterator iterator = list.iterator(); iterator.hasNext();) { - BundleInfo target = (BundleInfo) iterator.next(); + for (BundleInfo target : list) { target.setStartLevel(bundleInfo.getStartLevel()); target.setMarkedAsStarted(bundleInfo.isMarkedAsStarted()); } @@ -236,19 +232,18 @@ public class ProductFileAdvice extends AbstractAdvice implements ILicenseAdvice, } } - private void addUnboundedBundle(Map data, BundleInfo bundleInfo) { + private void addUnboundedBundle(Map<String, List<BundleInfo>> data, BundleInfo bundleInfo) { if (!data.containsKey(bundleInfo.getSymbolicName())) - data.put(bundleInfo.getSymbolicName(), new LinkedList()); - ((LinkedList) data.get(bundleInfo.getSymbolicName())).add(bundleInfo); + data.put(bundleInfo.getSymbolicName(), new LinkedList<BundleInfo>()); + data.get(bundleInfo.getSymbolicName()).add(bundleInfo); } private void addProductFileBundles(ConfigData data) { - List bundles = product.getBundles(true); - Set set = new HashSet(); + List<IVersionedId> bundles = product.getBundles(true); + Set<BundleInfo> set = new HashSet<BundleInfo>(); set.addAll(Arrays.asList(data.getBundles())); - for (Iterator i = bundles.iterator(); i.hasNext();) { - IVersionedId vid = (IVersionedId) i.next(); + for (IVersionedId vid : bundles) { BundleInfo bundleInfo = new BundleInfo(); bundleInfo.setSymbolicName(vid.getId()); bundleInfo.setVersion(vid.getVersion().toString()); @@ -264,9 +259,8 @@ public class ProductFileAdvice extends AbstractAdvice implements ILicenseAdvice, // Add all the bundles here. We replace / update them later // if we find configuration information - List bundles = product.getBundles(true); - for (Iterator i = bundles.iterator(); i.hasNext();) { - IVersionedId vid = (IVersionedId) i.next(); + List<IVersionedId> bundles = product.getBundles(true); + for (IVersionedId vid : bundles) { BundleInfo bundleInfo = new BundleInfo(); bundleInfo.setSymbolicName(vid.getId()); bundleInfo.setVersion(vid.getVersion().toString()); diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ProductPublisherApplication.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ProductPublisherApplication.java index 1f96c5c02..1ecd7582e 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ProductPublisherApplication.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/p2/publisher/eclipse/ProductPublisherApplication.java @@ -28,9 +28,9 @@ public class ProductPublisherApplication extends AbstractPublisherApplication { } protected IPublisherAction[] createActions() { - ArrayList result = new ArrayList(); + ArrayList<IPublisherAction> result = new ArrayList<IPublisherAction>(); result.add(createProductAction()); - return (IPublisherAction[]) result.toArray(new IPublisherAction[result.size()]); + return result.toArray(new IPublisherAction[result.size()]); } private IPublisherAction createProductAction() { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/LocalizationHelper.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/LocalizationHelper.java index 4dcf75669..c05b73206 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/LocalizationHelper.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/LocalizationHelper.java @@ -16,6 +16,7 @@ import java.net.URLConnection; import java.util.*; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; +import org.eclipse.equinox.internal.p2.core.helpers.CollectionUtils; /** * Helper functions supporting the processing of localized @@ -56,23 +57,23 @@ public final class LocalizationHelper { // For the given root directory and path to localization files within that directory // get a map from locale to property set for the localization property files. - public static Map getDirPropertyLocalizations(File root, String localizationPath, Locale defaultLocale, String[] propertyKeys) { + public static Map<Locale, Map<String, String>> getDirPropertyLocalizations(File root, String localizationPath, Locale defaultLocale, String[] propertyKeys) { File fullPath = new File(root, localizationPath); File localizationDir = fullPath.getParentFile(); final String localizationFile = fullPath.getName(); String[] localizationFiles = LocalizationHelper.getLocalizationFiles(localizationDir, localizationFile); - HashMap localizations = null; + HashMap<Locale, Map<String, String>> localizations = null; if (localizationFiles != null) { - localizations = new HashMap(localizationFiles.length); + localizations = new HashMap<Locale, Map<String, String>>(localizationFiles.length); for (int i = 0; i < localizationFiles.length; i++) { String nextFile = localizationFiles[i]; Locale nextLocale = getLocale(LocalizationHelper.getLocaleString(nextFile, localizationFile)); try { - Properties properties = loadProperties(root, nextFile); - Properties localizedStrings = getLocalizedProperties(propertyKeys, properties); + Map<String, String> properties = loadProperties(root, nextFile); + Map<String, String> localizedStrings = getLocalizedProperties(propertyKeys, properties); if (localizedStrings.size() > 0) { localizations.put(nextLocale, localizedStrings); if (DEFAULT_LOCALE.equals(nextLocale) && defaultLocale != null) { @@ -88,13 +89,13 @@ public final class LocalizationHelper { return localizations; } - public static Map getJarPropertyLocalizations(File root, String localizationPath, Locale defaultLocale, String[] propertyKeys) { + public static Map<Locale, Map<String, String>> getJarPropertyLocalizations(File root, String localizationPath, Locale defaultLocale, String[] propertyKeys) { ZipFile jarFile = null; - Map localizations = new HashMap(4); + Map<Locale, Map<String, String>> localizations = new HashMap<Locale, Map<String, String>>(4); try { jarFile = new ZipFile(root, ZipFile.OPEN_READ); - for (Enumeration entries = jarFile.entries(); entries.hasMoreElements();) { - ZipEntry nextEntry = (ZipEntry) entries.nextElement(); + for (Enumeration<? extends ZipEntry> entries = jarFile.entries(); entries.hasMoreElements();) { + ZipEntry nextEntry = entries.nextElement(); String nextName = nextEntry.getName(); String localeString = LocalizationHelper.getLocaleString(nextName, localizationPath); @@ -103,9 +104,8 @@ public final class LocalizationHelper { InputStream stream = null; try { stream = jarFile.getInputStream(nextEntry); - Properties properties = new Properties(); - properties.load(stream); - Properties localizedStrings = LocalizationHelper.getLocalizedProperties(propertyKeys, properties); + Map<String, String> properties = CollectionUtils.loadProperties(stream); + Map<String, String> localizedStrings = LocalizationHelper.getLocalizedProperties(propertyKeys, properties); if (localizedStrings.size() > 0) { localizations.put(nextLocale, localizedStrings); if (DEFAULT_LOCALE.equals(nextLocale) && defaultLocale != null) { @@ -134,8 +134,7 @@ public final class LocalizationHelper { } // Load a property set from given root and file with the given name - private static Properties loadProperties(File root, String propertyFilename) throws IOException { - Properties result = new Properties(); + private static Map<String, String> loadProperties(File root, String propertyFilename) throws IOException { InputStream propertyStream = null; try { try { @@ -148,24 +147,23 @@ public final class LocalizationHelper { } } catch (FileNotFoundException e) { // if there is no messages file then just return; - return result; + return CollectionUtils.emptyMap(); } - result.load(propertyStream); + return CollectionUtils.loadProperties(propertyStream); } finally { if (propertyStream != null) propertyStream.close(); } - return result; } // Given a list of keys and the corresponding localized property set, // return a new property set with those keys and the localized values. - static public Properties getLocalizedProperties(String[] propertyKeys, Properties properties) { - Properties localizedProperties = new Properties(); + static public Map<String, String> getLocalizedProperties(String[] propertyKeys, Map<String, String> properties) { + Map<String, String> localizedProperties = new HashMap<String, String>(); for (int i = 0; i < propertyKeys.length; i++) { String key = propertyKeys[i]; if (key != null) { - String localizedValue = properties.getProperty(key); + String localizedValue = properties.get(key); if (localizedValue != null) localizedProperties.put(key, localizedValue); } diff --git a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/PublisherHelper.java b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/PublisherHelper.java index d3dc25338..74a292be8 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/PublisherHelper.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src/org/eclipse/equinox/spi/p2/publisher/PublisherHelper.java @@ -16,15 +16,19 @@ import java.io.*; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.*; +import java.util.Map.Entry; import org.eclipse.equinox.internal.p2.metadata.ArtifactKey; import org.eclipse.equinox.internal.p2.metadata.InstallableUnit; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.ArtifactDescriptor; -import org.eclipse.equinox.internal.provisional.p2.artifact.repository.IArtifactDescriptor; -import org.eclipse.equinox.internal.provisional.p2.metadata.*; +import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory; +import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitDescription; import org.eclipse.equinox.internal.provisional.p2.metadata.MetadataFactory.InstallableUnitFragmentDescription; +import org.eclipse.equinox.p2.metadata.*; import org.eclipse.equinox.p2.publisher.IPublisherInfo; import org.eclipse.equinox.p2.publisher.PublisherInfo; import org.eclipse.equinox.p2.publisher.eclipse.*; +import org.eclipse.equinox.p2.repository.artifact.IArtifactDescriptor; +import org.eclipse.equinox.p2.repository.artifact.IArtifactRepository; +import org.eclipse.equinox.p2.repository.artifact.spi.ArtifactDescriptor; import org.eclipse.osgi.service.resolver.BundleDescription; import org.osgi.framework.Constants; @@ -36,29 +40,28 @@ import org.osgi.framework.Constants; public class PublisherHelper { /** * A capability namespace representing the type of Eclipse resource (bundle, feature, source bundle, etc) - * @see IRequiredCapability#getNamespace() * @see IProvidedCapability#getNamespace() */ public static final String NAMESPACE_ECLIPSE_TYPE = "org.eclipse.equinox.p2.eclipse.type"; //$NON-NLS-1$ + public static final String NAMESPACE_FLAVOR = "org.eclipse.equinox.p2.flavor"; //$NON-NLS-1$" /** * A capability name in the {@link #NAMESPACE_ECLIPSE_TYPE} namespace * representing a feature - * @see IRequiredCapability#getName() + * @see IProvidedCapability#getName() */ public static final String TYPE_ECLIPSE_FEATURE = "feature"; //$NON-NLS-1$ /** * A capability name in the {@link #NAMESPACE_ECLIPSE_TYPE} namespace * representing a source bundle - * @see IRequiredCapability#getName() + * @see IProvidedCapability#getName() */ public static final String TYPE_ECLIPSE_SOURCE = "source"; //$NON-NLS-1$ /** * A capability namespace representing the localization (translation) * of strings from a specified IU in a specified locale - * @see IRequiredCapability#getNamespace() * @see IProvidedCapability#getNamespace() * TODO: this should be in API, probably in IInstallableUnit */ @@ -81,24 +84,27 @@ public class PublisherHelper { public static final String ECLIPSE_INSTALL_HANDLER_PROP = "org.eclipse.update.installHandler"; //$NON-NLS-1$ - public static final Version versionMax = Version.createOSGi(Integer.MAX_VALUE, Integer.MAX_VALUE, Integer.MAX_VALUE); - public static final ITouchpointType TOUCHPOINT_NATIVE = MetadataFactory.createTouchpointType("org.eclipse.equinox.p2.native", Version.createOSGi(1, 0, 0)); //$NON-NLS-1$ public static final ITouchpointType TOUCHPOINT_OSGI = MetadataFactory.createTouchpointType("org.eclipse.equinox.p2.osgi", Version.createOSGi(1, 0, 0)); //$NON-NLS-1$ public static final IProvidedCapability FEATURE_CAPABILITY = MetadataFactory.createProvidedCapability(NAMESPACE_ECLIPSE_TYPE, TYPE_ECLIPSE_FEATURE, Version.createOSGi(1, 0, 0)); public static IArtifactDescriptor createArtifactDescriptor(IArtifactKey key, File pathOnDisk) { - //TODO this size calculation is bogus - ArtifactDescriptor result = new ArtifactDescriptor(key); - if (pathOnDisk != null) { - result.setProperty(IArtifactDescriptor.ARTIFACT_SIZE, Long.toString(pathOnDisk.length())); - // TODO - this is wrong but I'm testing a work-around for bug 205842 - result.setProperty(IArtifactDescriptor.DOWNLOAD_SIZE, Long.toString(pathOnDisk.length())); + return createArtifactDescriptor(null, key, pathOnDisk); + } + + public static IArtifactDescriptor createArtifactDescriptor(IArtifactRepository artifactRepo, IArtifactKey key, File pathOnDisk) { + IArtifactDescriptor result = artifactRepo != null ? artifactRepo.createArtifactDescriptor(key) : new ArtifactDescriptor(key); + if (result instanceof ArtifactDescriptor) { + ArtifactDescriptor descriptor = (ArtifactDescriptor) result; + if (pathOnDisk != null) { + descriptor.setProperty(IArtifactDescriptor.ARTIFACT_SIZE, Long.toString(pathOnDisk.length())); + descriptor.setProperty(IArtifactDescriptor.DOWNLOAD_SIZE, Long.toString(pathOnDisk.length())); + } + String md5 = computeMD5(pathOnDisk); + if (md5 != null) + descriptor.setProperty(IArtifactDescriptor.DOWNLOAD_MD5, md5); } - String md5 = computeMD5(pathOnDisk); - if (md5 != null) - result.setProperty(IArtifactDescriptor.DOWNLOAD_MD5, md5); return result; } @@ -156,15 +162,15 @@ public class PublisherHelper { cu.setVersion(configUnitVersion); // Add capabilities for fragment, self, and describing the flavor supported - cu.setProperty(IInstallableUnit.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); - cu.setCapabilities(new IProvidedCapability[] {createSelfCapability(configUnitId, configUnitVersion), MetadataFactory.createProvidedCapability(IInstallableUnit.NAMESPACE_FLAVOR, configurationFlavor, Version.createOSGi(1, 0, 0))}); + cu.setProperty(InstallableUnitDescription.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); + cu.setCapabilities(new IProvidedCapability[] {createSelfCapability(configUnitId, configUnitVersion), MetadataFactory.createProvidedCapability(NAMESPACE_FLAVOR, configurationFlavor, Version.createOSGi(1, 0, 0))}); // Create a required capability on features - IRequiredCapability[] reqs = new IRequiredCapability[] {MetadataFactory.createRequiredCapability(NAMESPACE_ECLIPSE_TYPE, TYPE_ECLIPSE_FEATURE, VersionRange.emptyRange, null, true, true, false)}; + IRequirement[] reqs = new IRequirement[] {MetadataFactory.createRequiredCapability(NAMESPACE_ECLIPSE_TYPE, TYPE_ECLIPSE_FEATURE, VersionRange.emptyRange, null, true, true, false)}; cu.setHost(reqs); cu.setFilter(INSTALL_FEATURES_FILTER); - Map touchpointData = new HashMap(); + Map<String, String> touchpointData = new HashMap<String, String>(); touchpointData.put("install", "installFeature(feature:${artifact},featureId:default,featureVersion:default)"); //$NON-NLS-1$//$NON-NLS-2$ touchpointData.put("uninstall", "uninstallFeature(feature:${artifact},featureId:default,featureVersion:default)"); //$NON-NLS-1$//$NON-NLS-2$ cu.addTouchpointData(MetadataFactory.createTouchpointData(touchpointData)); @@ -180,13 +186,13 @@ public class PublisherHelper { cu.setVersion(configUnitVersion); // Add capabilities for fragment, self, and describing the flavor supported - cu.setProperty(IInstallableUnit.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); - cu.setCapabilities(new IProvidedCapability[] {createSelfCapability(configUnitId, configUnitVersion), MetadataFactory.createProvidedCapability(IInstallableUnit.NAMESPACE_FLAVOR, configurationFlavor, Version.createOSGi(1, 0, 0))}); + cu.setProperty(InstallableUnitDescription.PROP_TYPE_FRAGMENT, Boolean.TRUE.toString()); + cu.setCapabilities(new IProvidedCapability[] {createSelfCapability(configUnitId, configUnitVersion), MetadataFactory.createProvidedCapability(NAMESPACE_FLAVOR, configurationFlavor, Version.createOSGi(1, 0, 0))}); // Create a required capability on source providers - IRequiredCapability[] reqs = new IRequiredCapability[] {MetadataFactory.createRequiredCapability(NAMESPACE_ECLIPSE_TYPE, TYPE_ECLIPSE_SOURCE, VersionRange.emptyRange, null, true, true, false)}; + IRequirement[] reqs = new IRequirement[] {MetadataFactory.createRequiredCapability(NAMESPACE_ECLIPSE_TYPE, TYPE_ECLIPSE_SOURCE, VersionRange.emptyRange, null, true, true, false)}; cu.setHost(reqs); - Map touchpointData = new HashMap(); + Map<String, String> touchpointData = new HashMap<String, String>(); touchpointData.put("install", "addSourceBundle(bundle:${artifact})"); //$NON-NLS-1$ //$NON-NLS-2$ touchpointData.put("uninstall", "removeSourceBundle(bundle:${artifact})"); //$NON-NLS-1$ //$NON-NLS-2$ @@ -194,26 +200,25 @@ public class PublisherHelper { return MetadataFactory.createInstallableUnit(cu); } - private static void addExtraProperties(IInstallableUnit iiu, Properties extraProperties) { + private static void addExtraProperties(IInstallableUnit iiu, Map<String, String> extraProperties) { if (iiu instanceof InstallableUnit) { InstallableUnit iu = (InstallableUnit) iiu; - for (Enumeration e = extraProperties.propertyNames(); e.hasMoreElements();) { - String name = (String) e.nextElement(); - iu.setProperty(name, extraProperties.getProperty(name)); + for (Entry<String, String> entry : extraProperties.entrySet()) { + iu.setProperty(entry.getKey(), entry.getValue()); } } } - public static IInstallableUnit[] createEclipseIU(BundleDescription bd, boolean isFolderPlugin, IArtifactKey key, Properties extraProperties) { - ArrayList iusCreated = new ArrayList(1); + public static IInstallableUnit[] createEclipseIU(BundleDescription bd, boolean isFolderPlugin, IArtifactKey key, Map<String, String> extraProperties) { + ArrayList<IInstallableUnit> iusCreated = new ArrayList<IInstallableUnit>(1); IPublisherInfo info = new PublisherInfo(); String shape = isFolderPlugin ? IBundleShapeAdvice.DIR : IBundleShapeAdvice.JAR; info.addAdvice(new BundleShapeAdvice(bd.getSymbolicName(), Version.fromOSGiVersion(bd.getVersion()), shape)); IInstallableUnit iu = BundlesAction.createBundleIU(bd, key, info); addExtraProperties(iu, extraProperties); iusCreated.add(iu); - return (IInstallableUnit[]) (iusCreated.toArray(new IInstallableUnit[iusCreated.size()])); + return (iusCreated.toArray(new IInstallableUnit[iusCreated.size()])); } public static ArtifactKey createBinaryArtifactKey(String id, Version version) { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/AbstractPublishTask.java b/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/AbstractPublishTask.java index 3da757a52..c0ab308ac 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/AbstractPublishTask.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/AbstractPublishTask.java @@ -10,13 +10,17 @@ package org.eclipse.equinox.internal.p2.publisher.ant; import java.net.URI; import java.net.URISyntaxException; -import java.util.*; +import java.util.ArrayList; +import java.util.List; import org.apache.tools.ant.Task; import org.eclipse.core.runtime.*; import org.eclipse.equinox.internal.p2.artifact.repository.CompositeArtifactRepository; +import org.eclipse.equinox.internal.p2.core.helpers.ServiceHelper; import org.eclipse.equinox.internal.p2.metadata.repository.CompositeMetadataRepository; +import org.eclipse.equinox.internal.p2.publisher.Activator; import org.eclipse.equinox.internal.p2.publisher.Messages; -import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException; +import org.eclipse.equinox.p2.core.IProvisioningAgent; +import org.eclipse.equinox.p2.core.ProvisionException; import org.eclipse.equinox.p2.publisher.*; public abstract class AbstractPublishTask extends Task { @@ -82,7 +86,11 @@ public abstract class AbstractPublishTask extends Task { protected URI artifactLocation; protected String artifactRepoName; protected PublisherInfo provider = null; - protected List contextRepositories = new ArrayList(); + protected List<RepoEntry> contextRepositories = new ArrayList<RepoEntry>(); + + protected IProvisioningAgent getProvisioningAgent() { + return (IProvisioningAgent) ServiceHelper.getService(Activator.context, IProvisioningAgent.SERVICE_NAME); + } protected IStatus createConfigurationEror(String message) { return new Status(IStatus.ERROR, "org.eclipse.equinox.p2.publisher", message); //$NON-NLS-1$ @@ -90,19 +98,18 @@ public abstract class AbstractPublishTask extends Task { protected void initializeRepositories(PublisherInfo info) throws ProvisionException { if (artifactLocation != null) - info.setArtifactRepository(Publisher.createArtifactRepository(artifactLocation, artifactRepoName, append, compress, reusePackedFiles)); + info.setArtifactRepository(Publisher.createArtifactRepository(getProvisioningAgent(), artifactLocation, artifactRepoName, append, compress, reusePackedFiles)); else if ((info.getArtifactOptions() & IPublisherInfo.A_PUBLISH) > 0) throw new ProvisionException(createConfigurationEror(Messages.exception_noArtifactRepo)); if (metadataLocation == null) throw new ProvisionException(createConfigurationEror(Messages.exception_noMetadataRepo)); - info.setMetadataRepository(Publisher.createMetadataRepository(metadataLocation, metadataRepoName, append, compress)); + info.setMetadataRepository(Publisher.createMetadataRepository(getProvisioningAgent(), metadataLocation, metadataRepoName, append, compress)); if (contextRepositories.size() > 0) { - CompositeMetadataRepository contextMetadata = CompositeMetadataRepository.createMemoryComposite(); - CompositeArtifactRepository contextArtifact = CompositeArtifactRepository.createMemoryComposite(); + CompositeMetadataRepository contextMetadata = CompositeMetadataRepository.createMemoryComposite(getProvisioningAgent()); + CompositeArtifactRepository contextArtifact = CompositeArtifactRepository.createMemoryComposite(getProvisioningAgent()); - for (Iterator iterator = contextRepositories.iterator(); iterator.hasNext();) { - RepoEntry entry = (RepoEntry) iterator.next(); + for (RepoEntry entry : contextRepositories) { if (contextMetadata != null && entry.isMetadataRepository()) contextMetadata.addChild(entry.getRepositoryLocation()); if (contextArtifact != null && entry.isArtifactRepository()) diff --git a/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/FeaturesAndBundlesPublisherTask.java b/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/FeaturesAndBundlesPublisherTask.java index bd276c160..495071c2d 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/FeaturesAndBundlesPublisherTask.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/FeaturesAndBundlesPublisherTask.java @@ -9,20 +9,21 @@ package org.eclipse.equinox.internal.p2.publisher.ant; import java.io.File; -import java.util.*; +import java.util.ArrayList; +import java.util.List; import org.apache.tools.ant.BuildException; import org.apache.tools.ant.DirectoryScanner; import org.apache.tools.ant.types.FileSet; import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException; +import org.eclipse.equinox.p2.core.ProvisionException; import org.eclipse.equinox.p2.publisher.IPublisherAction; import org.eclipse.equinox.p2.publisher.Publisher; import org.eclipse.equinox.p2.publisher.eclipse.BundlesAction; import org.eclipse.equinox.p2.publisher.eclipse.FeaturesAction; public class FeaturesAndBundlesPublisherTask extends AbstractPublishTask { - private ArrayList features = new ArrayList(); - private ArrayList bundles = new ArrayList(); + private ArrayList<Object> features = new ArrayList<Object>(); + private ArrayList<Object> bundles = new ArrayList<Object>(); public void execute() throws BuildException { try { @@ -34,20 +35,19 @@ public class FeaturesAndBundlesPublisherTask extends AbstractPublishTask { File[] f = getLocations(features); File[] b = getLocations(bundles); - ArrayList actions = new ArrayList(); + ArrayList<IPublisherAction> actions = new ArrayList<IPublisherAction>(); if (f.length > 0) actions.add(new FeaturesAction(f)); if (b.length > 0) actions.add(new BundlesAction(b)); if (actions.size() > 0) - new Publisher(getInfo()).publish((IPublisherAction[]) actions.toArray(new IPublisherAction[actions.size()]), new NullProgressMonitor()); + new Publisher(getInfo()).publish(actions.toArray(new IPublisherAction[actions.size()]), new NullProgressMonitor()); } - private File[] getLocations(List collection) { - ArrayList results = new ArrayList(); - for (Iterator iterator = collection.iterator(); iterator.hasNext();) { - Object obj = iterator.next(); + private File[] getLocations(List<Object> collection) { + ArrayList<Object> results = new ArrayList<Object>(); + for (Object obj : collection) { if (obj instanceof FileSet) { FileSet set = (FileSet) obj; @@ -62,7 +62,7 @@ public class FeaturesAndBundlesPublisherTask extends AbstractPublishTask { results.add(obj); } } - return (File[]) results.toArray(new File[results.size()]); + return results.toArray(new File[results.size()]); } public FileSet createFeatures() { diff --git a/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/ProductPublisherTask.java b/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/ProductPublisherTask.java index ba8cc4817..4b9a3fd3c 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/ProductPublisherTask.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/ProductPublisherTask.java @@ -9,13 +9,14 @@ package org.eclipse.equinox.internal.p2.publisher.ant; import java.io.File; -import java.util.*; +import java.util.ArrayList; +import java.util.List; import org.apache.tools.ant.BuildException; import org.eclipse.core.runtime.NullProgressMonitor; import org.eclipse.equinox.internal.p2.publisher.eclipse.IProductDescriptor; import org.eclipse.equinox.internal.p2.publisher.eclipse.ProductFile; -import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException; -import org.eclipse.equinox.internal.provisional.p2.metadata.IInstallableUnit; +import org.eclipse.equinox.p2.core.ProvisionException; +import org.eclipse.equinox.p2.metadata.IInstallableUnit; import org.eclipse.equinox.p2.publisher.*; import org.eclipse.equinox.p2.publisher.actions.VersionAdvice; import org.eclipse.equinox.p2.publisher.eclipse.ProductAction; @@ -61,8 +62,8 @@ public class ProductPublisherTask extends AbstractPublishTask { private String productFile; private String executables; private String source; - private List configurations = new ArrayList(3); - private List advice = new ArrayList(3); + private List<ConfigElement> configurations = new ArrayList<ConfigElement>(3); + private List<AdviceElement> advice = new ArrayList<AdviceElement>(3); public void execute() throws BuildException { try { @@ -99,8 +100,7 @@ public class ProductPublisherTask extends AbstractPublishTask { } protected void processAdvice(PublisherInfo info) { - for (Iterator iterator = advice.iterator(); iterator.hasNext();) { - AdviceElement element = (AdviceElement) iterator.next(); + for (AdviceElement element : advice) { if (element.kind == null || element.file == null) continue; diff --git a/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/PublisherTask.java b/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/PublisherTask.java index 34efd4857..3fb4fa88f 100644 --- a/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/PublisherTask.java +++ b/bundles/org.eclipse.equinox.p2.publisher/src_ant/org/eclipse/equinox/internal/p2/publisher/ant/PublisherTask.java @@ -13,9 +13,9 @@ package org.eclipse.equinox.internal.p2.publisher.ant; import java.io.File; import org.apache.tools.ant.BuildException; import org.eclipse.core.runtime.NullProgressMonitor; -import org.eclipse.equinox.internal.provisional.p2.core.ProvisionException; -import org.eclipse.equinox.internal.provisional.p2.metadata.IVersionedId; -import org.eclipse.equinox.internal.provisional.p2.metadata.Version; +import org.eclipse.equinox.p2.core.ProvisionException; +import org.eclipse.equinox.p2.metadata.IVersionedId; +import org.eclipse.equinox.p2.metadata.Version; import org.eclipse.equinox.p2.publisher.*; import org.eclipse.equinox.p2.publisher.eclipse.EclipseInstallAction; |