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

ElementListObserver.java 3.2KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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. /* $Id$ */
  18. package org.apache.fop.layoutmgr;
  19. import java.util.List;
  20. /**
  21. * This class is used to observe Knuth element lists generated within the layout managers. This
  22. * is mainly used for the purpose of automated testing. This implementation here does nothing.
  23. * Please see the subclass within the test code.
  24. */
  25. public final class ElementListObserver {
  26. private ElementListObserver() {
  27. }
  28. private static List activeObservers;
  29. /**
  30. * Adds a new Observer to the list.
  31. * @param observer the observer implementation
  32. */
  33. public static void addObserver(Observer observer) {
  34. if (!isObservationActive()) {
  35. activeObservers = new java.util.ArrayList();
  36. }
  37. activeObservers.add(observer);
  38. }
  39. /**
  40. * Removes an Observer from the list. This call simply returns if the observer was not on
  41. * the list and does nothing.
  42. * @param observer the observer to remove
  43. */
  44. public static void removeObserver(Observer observer) {
  45. if (isObservationActive()) {
  46. activeObservers.remove(observer);
  47. }
  48. }
  49. /**
  50. * Notifies all registered observers about the element list.
  51. * @param elementList the Knuth element list
  52. * @param category the category for the element list (example: main, static-content, table-cell)
  53. * @param id ID for the element list (may be null)
  54. */
  55. public static void observe(List elementList, String category, String id) {
  56. if (isObservationActive()) {
  57. if (category == null) {
  58. throw new NullPointerException("category must not be null");
  59. }
  60. for (Object activeObserver : activeObservers) {
  61. ((Observer) activeObserver).observe(elementList, category, id);
  62. }
  63. }
  64. }
  65. /** @return true if observation is active, i.e. Observers are registered. */
  66. public static boolean isObservationActive() {
  67. return activeObservers != null;
  68. }
  69. /**
  70. * Implement this interface to receive notifications on element lists.
  71. */
  72. public interface Observer {
  73. /**
  74. * Notifies the observer about the element list.
  75. * @param elementList the Knuth element list
  76. * @param category the category for the element list (example: main, static-content or
  77. * table-cell)
  78. * @param id ID for the element list (may be null)
  79. */
  80. void observe(List elementList, String category, String id);
  81. }
  82. }