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.

ActionSet.java 3.0KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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.render.intermediate.extensions;
  19. import java.util.Iterator;
  20. import java.util.Map;
  21. /**
  22. * This class manages actions and action references. Some action (like {@link GoToXYAction}s)
  23. * cannot be fully resolved at the time they are needed, so they are deferred. This class
  24. * helps manages the references and resolution.
  25. */
  26. public class ActionSet {
  27. private int lastGeneratedID = 0;
  28. private Map actionRegistry = new java.util.HashMap();
  29. /**
  30. * Generates a new synthetic ID for an action.
  31. * @param action the action
  32. * @return the generated ID
  33. */
  34. public synchronized String generateNewID(AbstractAction action) {
  35. this.lastGeneratedID++;
  36. String prefix = action.getIdPrefix();
  37. if (prefix == null) {
  38. throw new IllegalArgumentException("Action class is not compatible");
  39. }
  40. return prefix + this.lastGeneratedID;
  41. }
  42. /**
  43. * Returns the action with the given ID.
  44. * @param id the ID
  45. * @return the action or null if no action with this ID is stored
  46. */
  47. public AbstractAction get(String id) {
  48. return (AbstractAction)this.actionRegistry.get(id);
  49. }
  50. /**
  51. * Puts an action into the set and returns the normalized instance (another one if the given
  52. * one is equal to another.
  53. * @param action the action
  54. * @return the action instance that should be used in place of the given one
  55. */
  56. public AbstractAction put(AbstractAction action) {
  57. if (!action.hasId()) {
  58. action.setId(generateNewID(action));
  59. }
  60. AbstractAction effAction = normalize(action);
  61. if (effAction == action) {
  62. this.actionRegistry.put(action.getId(), action);
  63. }
  64. return effAction;
  65. }
  66. /**
  67. * Clears the set.
  68. */
  69. public void clear() {
  70. this.actionRegistry.clear();
  71. }
  72. private AbstractAction normalize(AbstractAction action) {
  73. Iterator iter = this.actionRegistry.values().iterator();
  74. while (iter.hasNext()) {
  75. AbstractAction a = (AbstractAction)iter.next();
  76. if (a.isSame(action)) {
  77. return a;
  78. }
  79. }
  80. return action;
  81. }
  82. }