1. /*
  2. * Copyright 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.util;
  18. import java.util.List;
  19. import java.util.Iterator;
  20. import java.util.ArrayList;
  21. import java.util.Collections;
  22. import org.apache.tools.ant.types.Mapper;
  23. /**
  24. * A <code>FileNameMapper</code> that contains
  25. * other <CODE>FileNameMapper</CODE>s.
  26. * @see FileNameMapper
  27. */
  28. public abstract class ContainerMapper implements FileNameMapper {
  29. private List mappers = new ArrayList();
  30. /**
  31. * Add a <code>Mapper</code>.
  32. * @param mapper the <code>Mapper</code> to add.
  33. */
  34. public void addConfiguredMapper(Mapper mapper) {
  35. add(mapper.getImplementation());
  36. }
  37. /**
  38. * Add a <code>FileNameMapper</code>.
  39. * @param fileNameMapper a <CODE>FileNameMapper</CODE>.
  40. * @throws <CODE>IllegalArgumentException</CODE> if attempting to add this
  41. * <CODE>ContainerMapper</CODE> to itself, or if the specified
  42. * <CODE>FileNameMapper</CODE> is itself a <CODE>ContainerMapper</CODE>
  43. * that contains this <CODE>ContainerMapper</CODE>.
  44. */
  45. public synchronized void add(FileNameMapper fileNameMapper) {
  46. if (this == fileNameMapper
  47. || (fileNameMapper instanceof ContainerMapper
  48. && ((ContainerMapper)fileNameMapper).contains(this))) {
  49. throw new IllegalArgumentException(
  50. "Circular mapper containment condition detected");
  51. } else {
  52. mappers.add(fileNameMapper);
  53. }
  54. }
  55. /**
  56. * Return <CODE>true</CODE> if this <CODE>ContainerMapper</CODE> or any of
  57. * its sub-elements contains the specified <CODE>FileNameMapper</CODE>.
  58. * @param fileNameMapper the <CODE>FileNameMapper</CODE> to search for.
  59. * @return <CODE>boolean</CODE>.
  60. */
  61. protected synchronized boolean contains(FileNameMapper fileNameMapper) {
  62. boolean foundit = false;
  63. for (Iterator iter = mappers.iterator(); iter.hasNext() && !foundit;) {
  64. FileNameMapper next = (FileNameMapper)(iter.next());
  65. foundit|= (next == fileNameMapper
  66. || (next instanceof ContainerMapper
  67. && ((ContainerMapper)next).contains(fileNameMapper)));
  68. }
  69. return foundit;
  70. }
  71. /**
  72. * Get the <CODE>List</CODE> of <CODE>FileNameMapper</CODE>s.
  73. * @return <CODE>List</CODE>.
  74. */
  75. public synchronized List getMappers() {
  76. return Collections.unmodifiableList(mappers);
  77. }
  78. /**
  79. * Empty implementation.
  80. */
  81. public void setFrom(String ignore) {
  82. }
  83. /**
  84. * Empty implementation.
  85. */
  86. public void setTo(String ignore) {
  87. }
  88. }