~ubuntu-branches/ubuntu/saucy/fop/saucy-proposed

« back to all changes in this revision

Viewing changes to test/java/org/apache/fop/check/ChecksFactory.java

  • Committer: Package Import Robot
  • Author(s): James Page
  • Date: 2013-05-21 12:21:26 UTC
  • mfrom: (15.1.7 sid)
  • Revision ID: package-import@ubuntu.com-20130521122126-3c9r5fo6ountjg6r
Tags: 1:1.1.dfsg-2ubuntu1
* Merge from Debian unstable.  Remaining changes:
  -  Transition libservlet2.5-java -> libservlet3.0-java.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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: ChecksFactory.java 1094690 2011-04-18 18:36:05Z vhennebert $ */
 
19
 
 
20
package org.apache.fop.check;
 
21
 
 
22
import java.util.ArrayList;
 
23
import java.util.HashMap;
 
24
import java.util.List;
 
25
import java.util.Map;
 
26
 
 
27
import org.w3c.dom.Element;
 
28
import org.w3c.dom.Node;
 
29
import org.w3c.dom.NodeList;
 
30
 
 
31
/**
 
32
 * A factory class for creating checks that belong to a same family.
 
33
 * @param <C> a family of checks
 
34
 */
 
35
public abstract class ChecksFactory<C extends Check> {
 
36
 
 
37
    /**
 
38
     * A factory to create a particular kind of check.
 
39
     */
 
40
    protected static interface CheckFactory<C> {
 
41
 
 
42
        /**
 
43
         * Creates a {@link Check} instance from the given XML element.
 
44
         *
 
45
         * @param element an element representing a check
 
46
         * @return the corresponding check
 
47
         */
 
48
        C createCheck(Element element);
 
49
    }
 
50
 
 
51
    private final Map<String, CheckFactory<C>> checkFactories
 
52
            = new HashMap<String, CheckFactory<C>>();
 
53
 
 
54
    /** Default constructor. */
 
55
    protected ChecksFactory() { }
 
56
 
 
57
    /**
 
58
     * Registers a factory for a new kind of check.
 
59
     *
 
60
     * @param elementName the name of the element under which the check is identified in
 
61
     * the XML test case
 
62
     * @param factory the corresponding factory
 
63
     */
 
64
    protected void registerCheckFactory(String elementName, CheckFactory<C> factory) {
 
65
        checkFactories.put(elementName, factory);
 
66
    }
 
67
 
 
68
    /**
 
69
     * Creates a new {@link Check} instance corresponding to the given element.
 
70
     *
 
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
 
74
     * has been found
 
75
     */
 
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);
 
81
        } else {
 
82
            return factory.createCheck(element);
 
83
        }
 
84
    }
 
85
 
 
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));
 
93
            }
 
94
        }
 
95
        return checks;
 
96
    }
 
97
}