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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/*
* 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.render.intermediate;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;
import org.xml.sax.Attributes;
import org.xml.sax.ContentHandler;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.AttributesImpl;
import org.xml.sax.helpers.DefaultHandler;
import org.apache.fop.accessibility.StructureTree2SAXEventAdapter;
import org.apache.fop.accessibility.StructureTreeElement;
import org.apache.fop.accessibility.StructureTreeEventHandler;
import org.apache.fop.fo.extensions.InternalElementMapping;
import org.apache.fop.util.XMLConstants;
import org.apache.fop.util.XMLUtil;
/**
* Saves structure tree events as SAX events in order to replay them when it's
* time to stream the structure tree to the output.
*/
final class IFStructureTreeBuilder implements StructureTreeEventHandler {
static final class IFStructureTreeElement implements StructureTreeElement {
private final String id;
IFStructureTreeElement() {
this.id = null;
}
IFStructureTreeElement(String id) {
this.id = id;
}
public String getId() {
return id;
}
}
/** A SAX handler that records events to replay them later. */
static class SAXEventRecorder extends DefaultHandler {
private final List<SAXEventRecorder.Event> events = new ArrayList<SAXEventRecorder.Event>();
private abstract static class Event {
abstract void replay(ContentHandler handler) throws SAXException;
}
private abstract static class Element extends SAXEventRecorder.Event {
protected final String uri;
protected final String localName;
protected final String qName;
private Element(String uri, String localName, String qName) {
this.uri = uri;
this.localName = localName;
this.qName = qName;
}
}
private static final class StartElement extends SAXEventRecorder.Element {
private final Attributes attributes;
private StartElement(String uri, String localName, String qName,
Attributes attributes) {
super(uri, localName, qName);
this.attributes = attributes;
}
@Override
void replay(ContentHandler handler) throws SAXException {
handler.startElement(uri, localName, qName, attributes);
}
}
private static final class EndElement extends SAXEventRecorder.Element {
private EndElement(String uri, String localName, String qName) {
super(uri, localName, qName);
}
@Override
void replay(ContentHandler handler) throws SAXException {
handler.endElement(uri, localName, qName);
}
}
private static final class StartPrefixMapping extends SAXEventRecorder.Event {
private final String prefix;
private final String uri;
private StartPrefixMapping(String prefix, String uri) {
this.prefix = prefix;
this.uri = uri;
}
@Override
void replay(ContentHandler handler) throws SAXException {
handler.startPrefixMapping(prefix, uri);
}
}
private static final class EndPrefixMapping extends SAXEventRecorder.Event {
private final String prefix;
private EndPrefixMapping(String prefix) {
this.prefix = prefix;
}
@Override
void replay(ContentHandler handler) throws SAXException {
handler.endPrefixMapping(prefix);
}
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
events.add(new StartElement(uri, localName, qName, attributes));
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
events.add(new EndElement(uri, localName, qName));
}
@Override
public void startPrefixMapping(String prefix, String uri) throws SAXException {
events.add(new StartPrefixMapping(prefix, uri));
}
@Override
public void endPrefixMapping(String prefix) throws SAXException {
events.add(new EndPrefixMapping(prefix));
}
/**
* Replays the recorded events.
*
* @param handler {@code ContentHandler} to replay events on
*/
public void replay(ContentHandler handler) throws SAXException {
for (SAXEventRecorder.Event e : events) {
e.replay(handler);
}
}
}
private StructureTreeEventHandler delegate;
private final List<SAXEventRecorder> pageSequenceEventRecorders
= new ArrayList<SAXEventRecorder>();
private SAXEventRecorder retrievedMarkersEventRecorder;
private int idCounter;
/**
* Replay SAX events for a page sequence.
* @param handler The handler that receives SAX events
* @param pageSequenceIndex The index of the page sequence
* @throws SAXException
*/
public void replayEventsForPageSequence(ContentHandler handler,
int pageSequenceIndex) throws SAXException {
pageSequenceEventRecorders.get(pageSequenceIndex).replay(handler);
}
public void replayEventsForRetrievedMarkers(ContentHandler handler) throws SAXException {
if (!retrievedMarkersEventRecorder.events.isEmpty()) {
delegate = StructureTree2SAXEventAdapter.newInstance(handler);
delegate.startPageSequence(null, null);
retrievedMarkersEventRecorder.replay(handler);
delegate.endPageSequence();
prepareRetrievedMarkersEventRecorder();
}
}
public void startPageSequence(Locale locale, String role) {
SAXEventRecorder eventRecorder = new SAXEventRecorder();
pageSequenceEventRecorders.add(eventRecorder);
delegate = StructureTree2SAXEventAdapter.newInstance(eventRecorder);
delegate.startPageSequence(locale, role);
}
public void endPageSequence() {
delegate.endPageSequence();
prepareRetrievedMarkersEventRecorder();
}
private void prepareRetrievedMarkersEventRecorder() {
SAXEventRecorder eventRecorder = new SAXEventRecorder();
retrievedMarkersEventRecorder = eventRecorder;
delegate = StructureTree2SAXEventAdapter.newInstance(eventRecorder);
}
public StructureTreeElement startNode(String name, Attributes attributes, StructureTreeElement parent) {
if (parent != null) {
attributes = addParentAttribute(new AttributesImpl(attributes), parent);
}
delegate.startNode(name, attributes, null);
return new IFStructureTreeElement();
}
private AttributesImpl addParentAttribute(AttributesImpl attributes, StructureTreeElement parent) {
if (parent != null) {
attributes.addAttribute(InternalElementMapping.URI,
InternalElementMapping.STRUCT_REF,
InternalElementMapping.STANDARD_PREFIX + ":" + InternalElementMapping.STRUCT_REF,
XMLConstants.CDATA,
((IFStructureTreeElement) parent).getId());
}
return attributes;
}
public void endNode(String name) {
delegate.endNode(name);
}
public StructureTreeElement startImageNode(String name, Attributes attributes, StructureTreeElement parent) {
String id = getNextID();
AttributesImpl atts = addIDAttribute(attributes, id);
addParentAttribute(atts, parent);
delegate.startImageNode(name, atts, null);
return new IFStructureTreeElement(id);
}
public StructureTreeElement startReferencedNode(String name, Attributes attributes, StructureTreeElement parent) {
String id = getNextID();
AttributesImpl atts = addIDAttribute(attributes, id);
addParentAttribute(atts, parent);
delegate.startReferencedNode(name, atts, null);
return new IFStructureTreeElement(id);
}
private String getNextID() {
return Integer.toHexString(idCounter++);
}
private AttributesImpl addIDAttribute(Attributes attributes, String id) {
AttributesImpl atts = new AttributesImpl(attributes);
atts.addAttribute(InternalElementMapping.URI,
InternalElementMapping.STRUCT_ID,
InternalElementMapping.STANDARD_PREFIX + ":" + InternalElementMapping.STRUCT_ID,
XMLUtil.CDATA,
id);
return atts;
}
}
|