1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
/* $Id$ */
package org.apache.fop.accessibility.fo;
import static org.junit.Assert.assertTrue;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMResult;
import javax.xml.transform.sax.SAXTransformerFactory;
import javax.xml.transform.sax.TransformerHandler;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;
import org.custommonkey.xmlunit.Diff;
import org.junit.Test;
import org.w3c.dom.Document;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.apache.fop.accessibility.StructureTree2SAXEventAdapter;
import org.apache.fop.accessibility.StructureTreeEventHandler;
import org.apache.fop.apps.FOPException;
import org.apache.fop.apps.FOUserAgent;
import org.apache.fop.fo.FODocumentParser;
import org.apache.fop.fo.FODocumentParser.FOEventHandlerFactory;
import org.apache.fop.fo.FOEventHandler;
import org.apache.fop.fo.LoadingException;
import org.apache.fop.fotreetest.DummyFOEventHandler;
public class FO2StructureTreeConverterTestCase {
private interface FOLoader {
InputStream getFoInputStream();
}
private static final String STRUCTURE_TREE_SEQUENCE_NAME = "structure-tree-sequence";
private FOLoader foLoader;
@Test
public void testCompleteDocument() throws Exception {
foLoader = new FOLoader() {
public InputStream getFoInputStream() {
return getResource("/org/apache/fop/fo/complete_document.fo");
}
};
testConverter();
}
@Test
public void testTableFooters() throws Exception {
foLoader = new FOLoader() {
public InputStream getFoInputStream() {
return getResource("table-footers.fo");
}
};
testConverter();
}
@Test
public void testCompleteContentWrappedInTableFooter() throws Exception {
Source xslt = new StreamSource(getResource("wrapCompleteDocumentInTableFooter.xsl"));
Transformer transformer = createTransformer(xslt);
InputStream originalFO = getResource("/org/apache/fop/fo/complete_document.fo");
ByteArrayOutputStream transformedFoOutput = new ByteArrayOutputStream();
transformer.transform(new StreamSource(originalFO), new StreamResult(transformedFoOutput));
final byte[] transformedFoOutputBytes = transformedFoOutput.toByteArray();
foLoader = new FOLoader() {
public InputStream getFoInputStream() {
return new ByteArrayInputStream(transformedFoOutputBytes);
}
};
testConverter();
}
private Transformer createTransformer(Source xslt) throws TransformerFactoryConfigurationError,
TransformerConfigurationException {
TransformerFactory transformerFactory = TransformerFactory.newInstance();
return transformerFactory.newTransformer(xslt);
}
private static InputStream getResource(String name) {
return FO2StructureTreeConverterTestCase.class.getResourceAsStream(name);
}
private void testConverter() throws Exception {
DOMResult expectedStructureTree = loadExpectedStructureTree();
DOMResult actualStructureTree = buildActualStructureTree();
final Diff diff = createDiff(expectedStructureTree, actualStructureTree);
assertTrue(diff.toString(), diff.identical());
}
private DOMResult loadExpectedStructureTree() {
DOMResult expectedStructureTree = new DOMResult();
InputStream xslt = getResource("fo2StructureTree.xsl");
runXSLT(xslt, foLoader.getFoInputStream(), expectedStructureTree);
return expectedStructureTree;
}
private static void runXSLT(InputStream xslt, InputStream doc, Result result) {
Source fo = new StreamSource(doc);
try {
Transformer transformer = TransformerFactory.newInstance()
.newTransformer(new StreamSource(xslt));
transformer.transform(fo, result);
} catch (TransformerConfigurationException e) {
throw new RuntimeException(e);
} catch (TransformerException e) {
throw new RuntimeException(e);
} finally {
closeStream(xslt);
closeStream(doc);
}
}
private static void closeStream(InputStream stream) {
try {
stream.close();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
private DOMResult buildActualStructureTree() throws Exception {
DOMResult actualStructureTree = new DOMResult();
createStructureTreeFromDocument(foLoader.getFoInputStream(), actualStructureTree);
return actualStructureTree;
}
private static void createStructureTreeFromDocument(InputStream foInputStream,
Result result) throws Exception {
TransformerHandler tHandler = createTransformerHandler(result);
startStructureTreeSequence(tHandler);
StructureTreeEventHandler structureTreeEventHandler
= StructureTree2SAXEventAdapter.newInstance(tHandler);
FODocumentParser documentParser = createDocumentParser(structureTreeEventHandler);
FOUserAgent userAgent = createFOUserAgent(documentParser);
parseDocument(foInputStream, documentParser, userAgent);
endStructureTreeSequence(tHandler);
}
private static TransformerHandler createTransformerHandler(Result domResult)
throws TransformerConfigurationException, TransformerFactoryConfigurationError {
SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance();
TransformerHandler transformerHandler = factory.newTransformerHandler();
transformerHandler.setResult(domResult);
return transformerHandler;
}
private static void startStructureTreeSequence(TransformerHandler tHandler) throws SAXException {
tHandler.startDocument();
tHandler.startElement("", STRUCTURE_TREE_SEQUENCE_NAME, STRUCTURE_TREE_SEQUENCE_NAME,
new AttributesImpl());
}
private static FODocumentParser createDocumentParser(
final StructureTreeEventHandler structureTreeEventHandler) {
return FODocumentParser.newInstance(new FOEventHandlerFactory() {
public FOEventHandler newFOEventHandler(FOUserAgent foUserAgent) {
return new FO2StructureTreeConverter(structureTreeEventHandler,
new DummyFOEventHandler(foUserAgent));
}
});
}
private static FOUserAgent createFOUserAgent(FODocumentParser documentParser) {
FOUserAgent userAgent = documentParser.createFOUserAgent();
userAgent.setAccessibility(true);
return userAgent;
}
private static void parseDocument(InputStream foInputStream, FODocumentParser documentParser,
FOUserAgent userAgent) throws FOPException, LoadingException {
try {
documentParser.parse(foInputStream, userAgent);
} finally {
closeStream(foInputStream);
}
}
private static void endStructureTreeSequence(TransformerHandler tHandler) throws SAXException {
tHandler.endElement("", STRUCTURE_TREE_SEQUENCE_NAME, STRUCTURE_TREE_SEQUENCE_NAME);
tHandler.endDocument();
}
private static Diff createDiff(DOMResult expected, DOMResult actual) {
Diff diff = new Diff(getDocument(expected), getDocument(actual));
return diff;
}
private static Document getDocument(DOMResult result) {
return (Document) result.getNode();
}
}
|