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
9
* http://www.apache.org/licenses/LICENSE-2.0
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.
18
/* $Id: ChecksFactory.java 1094690 2011-04-18 18:36:05Z vhennebert $ */
20
package org.apache.fop.check;
22
import java.util.ArrayList;
23
import java.util.HashMap;
24
import java.util.List;
27
import org.w3c.dom.Element;
28
import org.w3c.dom.Node;
29
import org.w3c.dom.NodeList;
32
* A factory class for creating checks that belong to a same family.
33
* @param <C> a family of checks
35
public abstract class ChecksFactory<C extends Check> {
38
* A factory to create a particular kind of check.
40
protected static interface CheckFactory<C> {
43
* Creates a {@link Check} instance from the given XML element.
45
* @param element an element representing a check
46
* @return the corresponding check
48
C createCheck(Element element);
51
private final Map<String, CheckFactory<C>> checkFactories
52
= new HashMap<String, CheckFactory<C>>();
54
/** Default constructor. */
55
protected ChecksFactory() { }
58
* Registers a factory for a new kind of check.
60
* @param elementName the name of the element under which the check is identified in
62
* @param factory the corresponding factory
64
protected void registerCheckFactory(String elementName, CheckFactory<C> factory) {
65
checkFactories.put(elementName, factory);
69
* Creates a new {@link Check} instance corresponding to the given element.
71
* @param element an element in the XML test case that identifies a particular check
72
* @return the corresponding check
73
* @throws IllegalArgumentException if not check corresponding to the given element
76
public final C createCheck(Element element) {
77
String name = element.getTagName();
78
CheckFactory<C> factory = checkFactories.get(name);
79
if (factory == null) {
80
throw new IllegalArgumentException("No check class found for " + name);
82
return factory.createCheck(element);
86
public final List<C> createCheckList(Element container) {
87
List<C> checks = new ArrayList<C>();
88
NodeList nodes = container.getChildNodes();
89
for (int i = 0; i < nodes.getLength(); i++) {
90
Node node = nodes.item(i);
91
if (node instanceof Element) {
92
checks.add(createCheck((Element) node));