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.

ChecksFactory.java 3.2KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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.check;
  19. import java.util.ArrayList;
  20. import java.util.HashMap;
  21. import java.util.List;
  22. import java.util.Map;
  23. import org.w3c.dom.Element;
  24. import org.w3c.dom.Node;
  25. import org.w3c.dom.NodeList;
  26. /**
  27. * A factory class for creating checks that belong to a same family.
  28. * @param <C> a family of checks
  29. */
  30. public abstract class ChecksFactory<C extends Check> {
  31. /**
  32. * A factory to create a particular kind of check.
  33. */
  34. protected interface CheckFactory<C> {
  35. /**
  36. * Creates a {@link Check} instance from the given XML element.
  37. *
  38. * @param element an element representing a check
  39. * @return the corresponding check
  40. */
  41. C createCheck(Element element);
  42. }
  43. private final Map<String, CheckFactory<C>> checkFactories
  44. = new HashMap<String, CheckFactory<C>>();
  45. /** Default constructor. */
  46. protected ChecksFactory() { }
  47. /**
  48. * Registers a factory for a new kind of check.
  49. *
  50. * @param elementName the name of the element under which the check is identified in
  51. * the XML test case
  52. * @param factory the corresponding factory
  53. */
  54. protected void registerCheckFactory(String elementName, CheckFactory<C> factory) {
  55. checkFactories.put(elementName, factory);
  56. }
  57. /**
  58. * Creates a new {@link Check} instance corresponding to the given element.
  59. *
  60. * @param element an element in the XML test case that identifies a particular check
  61. * @return the corresponding check
  62. * @throws IllegalArgumentException if not check corresponding to the given element
  63. * has been found
  64. */
  65. public final C createCheck(Element element) {
  66. String name = element.getTagName();
  67. CheckFactory<C> factory = checkFactories.get(name);
  68. if (factory == null) {
  69. throw new IllegalArgumentException("No check class found for " + name);
  70. } else {
  71. return factory.createCheck(element);
  72. }
  73. }
  74. public final List<C> createCheckList(Element container) {
  75. List<C> checks = new ArrayList<C>();
  76. NodeList nodes = container.getChildNodes();
  77. for (int i = 0; i < nodes.getLength(); i++) {
  78. Node node = nodes.item(i);
  79. if (node instanceof Element) {
  80. checks.add(createCheck((Element) node));
  81. }
  82. }
  83. return checks;
  84. }
  85. }