📄 extension.java
字号:
/* * Licensed to the Apache Software Foundation (ASF) under one or more * contributor license agreements. See the NOTICE file distributed with * this work for additional information regarding copyright ownership. * The ASF licenses this file to You under the Apache License, Version 2.0 * (the "License"); you may not use this file except in compliance with * the License. You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * */package org.apache.tools.ant.taskdefs.optional.extension;import java.util.ArrayList;import java.util.Iterator;import java.util.Map;import java.util.StringTokenizer;import java.util.jar.Attributes;import java.util.jar.Manifest;import org.apache.tools.ant.util.StringUtils;/** * <p>Utility class that represents either an available "Optional Package" * (formerly known as "Standard Extension") as described in the manifest * of a JAR file, or the requirement for such an optional package.</p> * * <p>For more information about optional packages, see the document * <em>Optional Package Versioning</em> in the documentation bundle for your * Java2 Standard Edition package, in file * <code>guide/extensions/versioning.html</code>.</p> * */public final class Extension { /** * Manifest Attribute Name object for EXTENSION_LIST. */ public static final Attributes.Name EXTENSION_LIST = new Attributes.Name("Extension-List"); /** * <code>Name</code> object for <code>Optional-Extension-List</code> * manifest attribute used for declaring optional dependencies on * installed extensions. Note that the dependencies declared by this method * are not required for the library to operate but if present will be used. * It is NOT part of the official "Optional Package" specification. * * @see <a href="http://java.sun.com/j2se/1.3/docs/guide/extensions/spec.html#dependnecy"> * Installed extension dependency</a> */ public static final Attributes.Name OPTIONAL_EXTENSION_LIST = new Attributes.Name("Optional-Extension-List"); /** * Manifest Attribute Name object for EXTENSION_NAME. */ public static final Attributes.Name EXTENSION_NAME = new Attributes.Name("Extension-Name"); /** * Manifest Attribute Name object for SPECIFICATION_VERSION. */ public static final Attributes.Name SPECIFICATION_VERSION = Attributes.Name.SPECIFICATION_VERSION; /** * Manifest Attribute Name object for SPECIFICATION_VENDOR. */ public static final Attributes.Name SPECIFICATION_VENDOR = Attributes.Name.SPECIFICATION_VENDOR; /** * Manifest Attribute Name object for IMPLEMENTATION_VERSION. */ public static final Attributes.Name IMPLEMENTATION_VERSION = Attributes.Name.IMPLEMENTATION_VERSION; /** * Manifest Attribute Name object for IMPLEMENTATION_VENDOR. */ public static final Attributes.Name IMPLEMENTATION_VENDOR = Attributes.Name.IMPLEMENTATION_VENDOR; /** * Manifest Attribute Name object for IMPLEMENTATION_URL. */ public static final Attributes.Name IMPLEMENTATION_URL = new Attributes.Name("Implementation-URL"); /** * Manifest Attribute Name object for IMPLEMENTATION_VENDOR_ID. */ public static final Attributes.Name IMPLEMENTATION_VENDOR_ID = new Attributes.Name("Implementation-Vendor-Id"); /** * Enum indicating that extension is compatible with other extension. */ public static final Compatibility COMPATIBLE = new Compatibility("COMPATIBLE"); /** * Enum indicating that extension requires an upgrade * of specification to be compatible with other extension. */ public static final Compatibility REQUIRE_SPECIFICATION_UPGRADE = new Compatibility("REQUIRE_SPECIFICATION_UPGRADE"); /** * Enum indicating that extension requires a vendor * switch to be compatible with other extension. */ public static final Compatibility REQUIRE_VENDOR_SWITCH = new Compatibility("REQUIRE_VENDOR_SWITCH"); /** * Enum indicating that extension requires an upgrade * of implementation to be compatible with other extension. */ public static final Compatibility REQUIRE_IMPLEMENTATION_UPGRADE = new Compatibility("REQUIRE_IMPLEMENTATION_UPGRADE"); /** * Enum indicating that extension is incompatible with * other extension in ways other than other enums * indicate). For example the other extension may have * a different ID. */ public static final Compatibility INCOMPATIBLE = new Compatibility("INCOMPATIBLE"); /** * The name of the optional package being made available, or required. */ private String extensionName; /** * The version number (dotted decimal notation) of the specification * to which this optional package conforms. */ private DeweyDecimal specificationVersion; /** * The name of the company or organization that originated the * specification to which this optional package conforms. */ private String specificationVendor; /** * The unique identifier of the company that produced the optional * package contained in this JAR file. */ private String implementationVendorID; /** * The name of the company or organization that produced this * implementation of this optional package. */ private String implementationVendor; /** * The version number (dotted decimal notation) for this implementation * of the optional package. */ private DeweyDecimal implementationVersion; /** * The URL from which the most recent version of this optional package * can be obtained if it is not already installed. */ private String implementationURL; /** * Return an array of <code>Extension</code> objects representing optional * packages that are available in the JAR file associated with the * specified <code>Manifest</code>. If there are no such optional * packages, a zero-length array is returned. * * @param manifest Manifest to be parsed * @return the "available" extensions in specified manifest */ public static Extension[] getAvailable(final Manifest manifest) { if (null == manifest) { return new Extension[ 0 ]; } final ArrayList results = new ArrayList(); final Attributes mainAttributes = manifest.getMainAttributes(); if (null != mainAttributes) { final Extension extension = getExtension("", mainAttributes); if (null != extension) { results.add(extension); } } final Map entries = manifest.getEntries(); final Iterator keys = entries.keySet().iterator(); while (keys.hasNext()) { final String key = (String) keys.next(); final Attributes attributes = (Attributes) entries.get(key); final Extension extension = getExtension("", attributes); if (null != extension) { results.add(extension); } } return (Extension[]) results.toArray(new Extension[results.size()]); } /** * Return the set of <code>Extension</code> objects representing optional * packages that are required by the application contained in the JAR * file associated with the specified <code>Manifest</code>. If there * are no such optional packages, a zero-length list is returned. * * @param manifest Manifest to be parsed * @return the dependencies that are specified in manifes */ public static Extension[] getRequired(final Manifest manifest) { return getListed(manifest, Attributes.Name.EXTENSION_LIST); } /** * Return the set of <code>Extension</code> objects representing "Optional * Packages" that the application declares they will use if present. If * there are no such optional packages, a zero-length list is returned. * * @param manifest Manifest to be parsed * @return the optional dependencies that are specified in manifest */ public static Extension[] getOptions(final Manifest manifest) { return getListed(manifest, OPTIONAL_EXTENSION_LIST); } /** * Add Extension to the specified manifest Attributes. * * @param attributes the attributes of manifest to add to * @param extension the extension */ public static void addExtension(final Extension extension, final Attributes attributes) { addExtension(extension, "", attributes); } /** * Add Extension to the specified manifest Attributes. * Use the specified prefix so that dependencies can added * with a prefix such as "java3d-" etc. * * @param attributes the attributes of manifest to add to * @param extension the extension * @param prefix the name to prefix to extension */ public static void addExtension(final Extension extension, final String prefix, final Attributes attributes) { attributes.putValue(prefix + EXTENSION_NAME, extension.getExtensionName()); final String specificationVendor = extension.getSpecificationVendor(); if (null != specificationVendor) { attributes.putValue(prefix + SPECIFICATION_VENDOR, specificationVendor); } final DeweyDecimal specificationVersion = extension.getSpecificationVersion(); if (null != specificationVersion) { attributes.putValue(prefix + SPECIFICATION_VERSION, specificationVersion.toString()); } final String implementationVendorID = extension.getImplementationVendorID(); if (null != implementationVendorID) { attributes.putValue(prefix + IMPLEMENTATION_VENDOR_ID, implementationVendorID); } final String implementationVendor = extension.getImplementationVendor(); if (null != implementationVendor) { attributes.putValue(prefix + IMPLEMENTATION_VENDOR, implementationVendor); } final DeweyDecimal implementationVersion = extension.getImplementationVersion(); if (null != implementationVersion) { attributes.putValue(prefix + IMPLEMENTATION_VERSION, implementationVersion.toString()); } final String implementationURL = extension.getImplementationURL(); if (null != implementationURL) { attributes.putValue(prefix + IMPLEMENTATION_URL, implementationURL); } } /** * The constructor to create Extension object. * Note that every component is allowed to be specified * but only the extensionName is mandatory. * * @param extensionName the name of extension. * @param specificationVersion the specification Version of extension. * @param specificationVendor the specification Vendor of extension. * @param implementationVersion the implementation Version of extension. * @param implementationVendor the implementation Vendor of extension. * @param implementationVendorId the implementation VendorId of extension. * @param implementationURL the implementation URL of extension. */ public Extension(final String extensionName, final String specificationVersion, final String specificationVendor, final String implementationVersion, final String implementationVendor, final String implementationVendorId, final String implementationURL) { this.extensionName = extensionName; this.specificationVendor = specificationVendor; if (null != specificationVersion) { try { this.specificationVersion = new DeweyDecimal(specificationVersion); } catch (final NumberFormatException nfe) { final String error = "Bad specification version format '" + specificationVersion + "' in '" + extensionName + "'. (Reason: " + nfe + ")"; throw new IllegalArgumentException(error); } } this.implementationURL = implementationURL; this.implementationVendor = implementationVendor; this.implementationVendorID = implementationVendorId;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -