1. /*
  2. * Copyright 2002-2004 The Apache Software Foundation
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. *
  16. */
  17. package org.apache.tools.ant.taskdefs.optional.extension;
  18. import org.apache.tools.ant.BuildException;
  19. import org.apache.tools.ant.types.DataType;
  20. import org.apache.tools.ant.types.Reference;
  21. /**
  22. * Simple class that represents an Extension and conforms to Ants
  23. * patterns.
  24. *
  25. * @version $Revision: 1.8.2.4 $ $Date: 2004/03/09 17:01:45 $
  26. * @ant.data-type name="extension"
  27. */
  28. public class ExtensionAdapter extends DataType {
  29. /**
  30. * The name of the optional package being made available, or required.
  31. */
  32. private String extensionName;
  33. /**
  34. * The version number (dotted decimal notation) of the specification
  35. * to which this optional package conforms.
  36. */
  37. private DeweyDecimal specificationVersion;
  38. /**
  39. * The name of the company or organization that originated the
  40. * specification to which this optional package conforms.
  41. */
  42. private String specificationVendor;
  43. /**
  44. * The unique identifier of the company that produced the optional
  45. * package contained in this JAR file.
  46. */
  47. private String implementationVendorID;
  48. /**
  49. * The name of the company or organization that produced this
  50. * implementation of this optional package.
  51. */
  52. private String implementationVendor;
  53. /**
  54. * The version number (dotted decimal notation) for this implementation
  55. * of the optional package.
  56. */
  57. private DeweyDecimal implementationVersion;
  58. /**
  59. * The URL from which the most recent version of this optional package
  60. * can be obtained if it is not already installed.
  61. */
  62. private String implementationURL;
  63. /**
  64. * Set the name of extension.
  65. *
  66. * @param extensionName the name of extension
  67. */
  68. public void setExtensionName(final String extensionName) {
  69. verifyNotAReference();
  70. this.extensionName = extensionName;
  71. }
  72. /**
  73. * Set the specificationVersion of extension.
  74. *
  75. * @param specificationVersion the specificationVersion of extension
  76. */
  77. public void setSpecificationVersion(final String specificationVersion) {
  78. verifyNotAReference();
  79. this.specificationVersion = new DeweyDecimal(specificationVersion);
  80. }
  81. /**
  82. * Set the specificationVendor of extension.
  83. *
  84. * @param specificationVendor the specificationVendor of extension
  85. */
  86. public void setSpecificationVendor(final String specificationVendor) {
  87. verifyNotAReference();
  88. this.specificationVendor = specificationVendor;
  89. }
  90. /**
  91. * Set the implementationVendorID of extension.
  92. *
  93. * @param implementationVendorID the implementationVendorID of extension
  94. */
  95. public void setImplementationVendorId(final String implementationVendorID) {
  96. verifyNotAReference();
  97. this.implementationVendorID = implementationVendorID;
  98. }
  99. /**
  100. * Set the implementationVendor of extension.
  101. *
  102. * @param implementationVendor the implementationVendor of extension
  103. */
  104. public void setImplementationVendor(final String implementationVendor) {
  105. verifyNotAReference();
  106. this.implementationVendor = implementationVendor;
  107. }
  108. /**
  109. * Set the implementationVersion of extension.
  110. *
  111. * @param implementationVersion the implementationVersion of extension
  112. */
  113. public void setImplementationVersion(final String implementationVersion) {
  114. verifyNotAReference();
  115. this.implementationVersion = new DeweyDecimal(implementationVersion);
  116. }
  117. /**
  118. * Set the implementationURL of extension.
  119. *
  120. * @param implementationURL the implementationURL of extension
  121. */
  122. public void setImplementationUrl(final String implementationURL) {
  123. verifyNotAReference();
  124. this.implementationURL = implementationURL;
  125. }
  126. /**
  127. * Makes this instance in effect a reference to another ExtensionAdapter
  128. * instance.
  129. *
  130. * <p>You must not set another attribute or nest elements inside
  131. * this element if you make it a reference.</p>
  132. *
  133. * @param reference the reference to which this instance is associated
  134. * @exception BuildException if this instance already has been configured.
  135. */
  136. public void setRefid(final Reference reference)
  137. throws BuildException {
  138. if (null != extensionName
  139. || null != specificationVersion
  140. || null != specificationVendor
  141. || null != implementationVersion
  142. || null != implementationVendorID
  143. || null != implementationVendor
  144. || null != implementationURL) {
  145. throw tooManyAttributes();
  146. }
  147. // change this to get the objects from the other reference
  148. Object o = reference.getReferencedObject(getProject());
  149. if (o instanceof ExtensionAdapter) {
  150. final ExtensionAdapter other = (ExtensionAdapter) o;
  151. extensionName = other.extensionName;
  152. specificationVersion = other.specificationVersion;
  153. specificationVendor = other.specificationVendor;
  154. implementationVersion = other.implementationVersion;
  155. implementationVendorID = other.implementationVendorID;
  156. implementationVendor = other.implementationVendor;
  157. implementationURL = other.implementationURL;
  158. } else {
  159. final String message =
  160. reference.getRefId() + " doesn\'t refer to a Extension";
  161. throw new BuildException(message);
  162. }
  163. super.setRefid(reference);
  164. }
  165. private void verifyNotAReference()
  166. throws BuildException {
  167. if (isReference()) {
  168. throw tooManyAttributes();
  169. }
  170. }
  171. /**
  172. * Convert this adpater object into an extension object.
  173. *
  174. * @return the extension object
  175. */
  176. Extension toExtension()
  177. throws BuildException {
  178. if (null == extensionName) {
  179. final String message = "Extension is missing name.";
  180. throw new BuildException(message);
  181. }
  182. String specificationVersionString = null;
  183. if (null != specificationVersion) {
  184. specificationVersionString = specificationVersion.toString();
  185. }
  186. String implementationVersionString = null;
  187. if (null != implementationVersion) {
  188. implementationVersionString = implementationVersion.toString();
  189. }
  190. return new Extension(extensionName,
  191. specificationVersionString,
  192. specificationVendor,
  193. implementationVersionString,
  194. implementationVendor,
  195. implementationVendorID,
  196. implementationURL);
  197. }
  198. /**
  199. * @see java.lang.Object#toString()
  200. */
  201. public String toString() {
  202. return "{" + toExtension().toString() + "}";
  203. }
  204. }