}
protected Writer parse(Source source, Result result, ExecutionContext executionContext) throws SAXException, IOException {
Writer writer = getWriter(result, executionContext);
ContentDeliveryConfig deliveryConfig = executionContext.getDeliveryConfig();
XMLReader saxReader = getXMLReader(executionContext);
saxHandler = new SAXHandler(getExecContext(), writer);
try {
if(saxReader == null) {
saxReader = deliveryConfig.getXMLReader();
}
if(saxReader == null) {
saxReader = createXMLReader();
}
NamespaceDeclarationStack namespaceDeclarationStack = new NamespaceDeclarationStack();
NamespaceMappings.setNamespaceDeclarationStack(namespaceDeclarationStack, executionContext);
attachNamespaceDeclarationStack(saxReader, executionContext);
attachXMLReader(saxReader, executionContext);
configureReader(saxReader, saxHandler, executionContext, source);
if(executionContext != null) {
if(saxReader instanceof HierarchyChangeReader) {
((HierarchyChangeReader)saxReader).setHierarchyChangeListener(new XMLReaderHierarchyChangeListener(executionContext));
}
saxReader.parse(createInputSource(source, executionContext.getContentEncoding()));
} else {
saxReader.parse(createInputSource(source, Charset.defaultCharset().name()));
}
} finally {
try {
if(executionContext != null && saxReader instanceof HierarchyChangeReader) {
((HierarchyChangeReader)saxReader).setHierarchyChangeListener(null);
}
} finally {
try {
if(saxReader != null) {
try {
detachXMLReader(executionContext);
} finally {
deliveryConfig.returnXMLReader(saxReader);
}
}
} finally {
saxHandler.detachHandler();
}