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: IFTester.java 1094690 2011-04-18 18:36:05Z vhennebert $ */
19 
20 package org.apache.fop.intermediate;
21 
22 import java.io.File;
23 import java.util.List;
24 
25 import javax.xml.transform.Result;
26 import javax.xml.transform.Source;
27 import javax.xml.transform.Transformer;
28 import javax.xml.transform.TransformerException;
29 import javax.xml.transform.TransformerFactory;
30 import javax.xml.transform.dom.DOMSource;
31 import javax.xml.transform.stream.StreamResult;
32 
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35 
36 /**
37  * Does tests on the intermediate format.
38  */
39 public class IFTester {
40 
41     private final IFChecksFactory ifChecksFactory = new IFChecksFactory();
42 
43     private final TransformerFactory tfactory;
44 
45     private File backupDir;
46 
47     /**
48      * Main constructor.
49      *
50      * @param transformerFactory the factory used to serialize the intermediate format files
51      * @param backupDir an optional directory in which to write the serialized
52      * IF files (may be null)
53      */
IFTester(TransformerFactory transformerFactory, File backupDir)54     public IFTester(TransformerFactory transformerFactory, File backupDir) {
55         this.tfactory = transformerFactory;
56         this.backupDir = backupDir;
57     }
58 
59     /**
60      * Runs the intermediate format checks.
61      * @param testName the name of the test case
62      * @param checksRoot the root element containing the IF checks
63      * @param ifDocument the IF XML
64      * @throws TransformerException if an error occurs while transforming the content
65      */
doIFChecks(String testName, Element checksRoot, Document ifDocument)66     public void doIFChecks(String testName, Element checksRoot, Document ifDocument)
67             throws TransformerException {
68         if (this.backupDir != null) {
69             Transformer transformer = tfactory.newTransformer();
70             Source src = new DOMSource(ifDocument);
71             File targetFile = new File(this.backupDir, testName + ".if.xml");
72             Result res = new StreamResult(targetFile);
73             transformer.transform(src, res);
74         }
75         List<IFCheck> checks = ifChecksFactory.createCheckList(checksRoot);
76         if (checks.size() == 0) {
77             throw new RuntimeException("No available IF check");
78         }
79         for (IFCheck check : checks) {
80             check.check(ifDocument);
81         }
82     }
83 
84 }
85