You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

ProjectComponent.java 5.6 kB

8 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. /*
  2. * Licensed to the Apache Software Foundation (ASF) under one or more
  3. * contributor license agreements. See the NOTICE file distributed with
  4. * this work for additional information regarding copyright ownership.
  5. * The ASF licenses this file to You under the Apache License, Version 2.0
  6. * (the "License"); you may not use this file except in compliance with
  7. * the License. You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. package org.apache.tools.ant;
  19. /**
  20. * Base class for components of a project, including tasks and data types.
  21. * Provides common facilities.
  22. *
  23. */
  24. public abstract class ProjectComponent implements Cloneable {
  25. // CheckStyle:VisibilityModifier OFF - bc
  26. /**
  27. * Project object of this component.
  28. * @deprecated since 1.6.x.
  29. * You should not be directly accessing this variable directly.
  30. * You should access project object via the getProject()
  31. * or setProject() accessor/mutators.
  32. */
  33. @Deprecated
  34. protected Project project;
  35. /**
  36. * Location within the build file of this task definition.
  37. * @deprecated since 1.6.x.
  38. * You should not be accessing this variable directly.
  39. * Please use the {@link #getLocation()} method.
  40. */
  41. @Deprecated
  42. protected Location location = Location.UNKNOWN_LOCATION;
  43. /**
  44. * Description of this component, if any.
  45. * @deprecated since 1.6.x.
  46. * You should not be accessing this variable directly.
  47. */
  48. @Deprecated
  49. protected String description;
  50. // CheckStyle:VisibilityModifier ON
  51. /** Sole constructor. */
  52. public ProjectComponent() {
  53. }
  54. /**
  55. * Sets the project object of this component. This method is used by
  56. * Project when a component is added to it so that the component has
  57. * access to the functions of the project. It should not be used
  58. * for any other purpose.
  59. *
  60. * @param project Project in whose scope this component belongs.
  61. * Must not be <code>null</code>.
  62. */
  63. public void setProject(Project project) {
  64. this.project = project;
  65. }
  66. /**
  67. * Returns the project to which this component belongs.
  68. *
  69. * @return the components's project.
  70. */
  71. public Project getProject() {
  72. return project;
  73. }
  74. /**
  75. * Returns the file/location where this task was defined.
  76. *
  77. * @return the file/location where this task was defined.
  78. * Should not return <code>null</code>. Location.UNKNOWN_LOCATION
  79. * is used for unknown locations.
  80. *
  81. * @see Location#UNKNOWN_LOCATION
  82. */
  83. public Location getLocation() {
  84. return location;
  85. }
  86. /**
  87. * Sets the file/location where this task was defined.
  88. *
  89. * @param location The file/location where this task was defined.
  90. * Should not be <code>null</code>--use
  91. * Location.UNKNOWN_LOCATION if the location isn't known.
  92. *
  93. * @see Location#UNKNOWN_LOCATION
  94. */
  95. public void setLocation(Location location) {
  96. this.location = location;
  97. }
  98. /**
  99. * Sets a description of the current action. This may be used for logging
  100. * purposes.
  101. *
  102. * @param desc Description of the current action.
  103. * May be <code>null</code>, indicating that no description is
  104. * available.
  105. *
  106. */
  107. public void setDescription(String desc) {
  108. description = desc;
  109. }
  110. /**
  111. * Returns the description of the current action.
  112. *
  113. * @return the description of the current action, or <code>null</code> if
  114. * no description is available.
  115. */
  116. public String getDescription() {
  117. return description;
  118. }
  119. /**
  120. * Logs a message with the default (INFO) priority.
  121. *
  122. * @param msg The message to be logged. Should not be <code>null</code>.
  123. */
  124. public void log(String msg) {
  125. log(msg, Project.MSG_INFO);
  126. }
  127. /**
  128. * Logs a message with the given priority.
  129. *
  130. * @param msg The message to be logged. Should not be <code>null</code>.
  131. * @param msgLevel the message priority at which this message is
  132. * to be logged.
  133. */
  134. public void log(String msg, int msgLevel) {
  135. if (getProject() != null) {
  136. getProject().log(msg, msgLevel);
  137. } else {
  138. // 'reasonable' default, if the component is used without
  139. // a Project (for example as a standalone Bean).
  140. // Most ant components can be used this way.
  141. if (msgLevel <= Project.MSG_INFO) {
  142. System.err.println(msg);
  143. }
  144. }
  145. }
  146. /**
  147. * @since Ant 1.7
  148. * @return a shallow copy of this projectcomponent.
  149. * @throws CloneNotSupportedException does not happen,
  150. * but is declared to allow subclasses to do so.
  151. */
  152. @Override
  153. public Object clone() throws CloneNotSupportedException {
  154. ProjectComponent pc = (ProjectComponent) super.clone();
  155. pc.setLocation(getLocation());
  156. pc.setProject(getProject());
  157. return pc;
  158. }
  159. }