package org.apache.fop.accessibility;
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.apache.fop.fo.FOElementMapping;
import org.apache.fop.fo.extensions.ExtensionElementMapping;
import org.apache.fop.fo.extensions.InternalElementMapping;
import org.apache.fop.render.intermediate.IFConstants;
import org.apache.fop.util.XMLConstants;
public final class StructureTree2SAXEventAdapter implements StructureTreeEventHandler {
private final ContentHandler contentHandler;
private StructureTree2SAXEventAdapter(ContentHandler currentContentHandler) {
this.contentHandler = currentContentHandler;
}
public static StructureTreeEventHandler newInstance(ContentHandler contentHandler) {
return new StructureTree2SAXEventAdapter(contentHandler);
}
public void startPageSequence(Locale locale, String role) {
try {
AttributesImpl attributes = new AttributesImpl();
if (role != null) {
attributes.addAttribute("", "type", "type", XMLConstants.CDATA, role);
}
contentHandler.startPrefixMapping(
InternalElementMapping.STANDARD_PREFIX, InternalElementMapping.URI);
contentHandler.startPrefixMapping(
ExtensionElementMapping.STANDARD_PREFIX, ExtensionElementMapping.URI);
contentHandler.startElement(IFConstants.NAMESPACE,
IFConstants.EL_STRUCTURE_TREE, IFConstants.EL_STRUCTURE_TREE,
attributes);
} catch (SAXException e) {
throw new RuntimeException(e);
}
}
public void endPageSequence() {
try {
contentHandler.endElement(IFConstants.NAMESPACE, IFConstants.EL_STRUCTURE_TREE,
IFConstants.EL_STRUCTURE_TREE);
contentHandler.endPrefixMapping(
ExtensionElementMapping.STANDARD_PREFIX);
contentHandler.endPrefixMapping(
InternalElementMapping.STANDARD_PREFIX);
} catch (SAXException e) {
throw new RuntimeException(e);
}
}
public StructureTreeElement startNode(String name, Attributes attributes, StructureTreeElement parent) {
try {
if (name.equals("#PCDATA")) {
name = "marked-content";
contentHandler.startElement(IFConstants.NAMESPACE, name,
name, attributes);
} else {
contentHandler.startElement(FOElementMapping.URI, name,
FOElementMapping.STANDARD_PREFIX + ":" + name,
attributes);
}
return null;
} catch (SAXException e) {
throw new RuntimeException(e);
}
}
public void endNode(String name) {
try {
contentHandler.endElement(FOElementMapping.URI, name,
FOElementMapping.STANDARD_PREFIX + ":" + name);
} catch (SAXException e) {
throw new RuntimeException(e);
}
}
public StructureTreeElement startImageNode(String name, Attributes attributes, StructureTreeElement parent) {
return startNode(name, attributes, null);
}
public StructureTreeElement startReferencedNode(String name, Attributes attributes, StructureTreeElement parent) {
return startNode(name, attributes, null);
}
}