getLogger().debug("Reusing Templates for " + id);
}
return handlerAndValidity;
}
} catch(Exception e) {
throw new XSLTProcessorException("Error retrieving template", e);
}
TraxErrorListener errorListener = new TraxErrorListener(getLogger(), stylesheet.getURI());
try{
if (getLogger().isDebugEnabled()) {
getLogger().debug("Creating new Templates for " + id);
}
m_factory.setErrorListener(errorListener);
// Create a Templates ContentHandler to handle parsing of the
// stylesheet.
TemplatesHandler templatesHandler = m_factory.newTemplatesHandler();
// Set the system ID for the template handler since some
// TrAX implementations (XSLTC) rely on this in order to obtain
// a meaningful identifier for the Templates instances.
templatesHandler.setSystemId(id);
if (filter != null) {
filter.setContentHandler(templatesHandler);
}
if (getLogger().isDebugEnabled()) {
getLogger().debug("Source = " + stylesheet + ", templatesHandler = " + templatesHandler);
}
// Initialize List for included validities
SourceValidity validity = stylesheet.getValidity();
if (validity != null && m_checkIncludes) {
m_includesMap.put(id, new ArrayList());
}
try {
// Process the stylesheet.
sourceToSAX(stylesheet, filter != null ? (ContentHandler) filter : (ContentHandler) templatesHandler);
// Get the Templates object (generated during the parsing of
// the stylesheet) from the TemplatesHandler.
final Templates template = templatesHandler.getTemplates();
if (null == template) {
throw new XSLTProcessorException("Unable to create templates for stylesheet: " + stylesheet.getURI());
}
// Must set base for Xalan stylesheet.
// Otherwise document('') in logicsheet causes NPE.
Class clazz = template.getClass();
if (clazz.getName().equals("org.apache.xalan.templates.StylesheetRoot")) {
Method method = clazz.getMethod("setHref", new Class[]{String.class});
method.invoke(template, new Object[]{id});
}
putTemplates(template, stylesheet, id);
// Create transformer handler
final TransformerHandler handler = m_factory.newTransformerHandler(template);
handler.getTransformer().setErrorListener(new TraxErrorListener(getLogger(), stylesheet.getURI()));
handler.getTransformer().setURIResolver(this);
// Create aggregated validity
AggregatedValidity aggregated = null;
if (validity != null && m_checkIncludes) {
List includes = (List) m_includesMap.get(id);
if (includes != null) {
aggregated = new AggregatedValidity();
aggregated.add(validity);
for (int i = includes.size() - 1; i >= 0; i--) {
aggregated.add((SourceValidity) ((Object[]) includes.get(i))[1]);
}
validity = aggregated;
}
}
// Create result
handlerAndValidity = new MyTransformerHandlerAndValidity(handler, validity);
} finally {
if (m_checkIncludes)
m_includesMap.remove(id);
}
return handlerAndValidity;
} catch (Exception e) {
Throwable realEx = errorListener.getThrowable();
if (realEx == null) realEx = e;
if (realEx instanceof RuntimeException) {
throw (RuntimeException)realEx;
}
if (realEx instanceof XSLTProcessorException) {
throw (XSLTProcessorException)realEx;
}
throw new XSLTProcessorException("Exception when creating Transformer from " + stylesheet.getURI(), realEx);
}
}