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