use of org.mycore.common.content.transformer.MCRParameterizedTransformer in project mycore by MyCoRe-Org.
the class MCRJerseyUtil method transform.
/**
* Transforms a jdom document to a <code>MCRContent</code> via the <code>MCRLayoutService</code>.
*
* @param document
* the document to transform
* @param request
* the http request
*/
public static MCRContent transform(Document document, HttpServletRequest request) throws Exception {
MCRParameterCollector parameter = new MCRParameterCollector(request);
MCRContent result;
MCRJDOMContent source = new MCRJDOMContent(document);
MCRContentTransformer transformer = MCRLayoutService.getContentTransformer(source.getDocType(), parameter);
if (transformer instanceof MCRParameterizedTransformer) {
result = ((MCRParameterizedTransformer) transformer).transform(source, parameter);
} else {
result = transformer.transform(source);
}
return result;
}
use of org.mycore.common.content.transformer.MCRParameterizedTransformer in project mycore by MyCoRe-Org.
the class MCRXEditorTransformer method transform.
public MCRContent transform(MCRContent editorSource) throws IOException, JDOMException, SAXException {
editorSession.getValidator().clearRules();
editorSession.getSubmission().clear();
MCRContentTransformer transformer = MCRContentTransformerFactory.getTransformer("xeditor");
if (transformer instanceof MCRParameterizedTransformer) {
String key = MCRXEditorTransformerStore.storeTransformer(this);
transformationParameters.setParameter("XEditorTransformerKey", key);
MCRContent result = ((MCRParameterizedTransformer) transformer).transform(editorSource, transformationParameters);
if (result instanceof MCRWrappedContent && result.getClass().getName().contains(MCRXSLTransformer.class.getName())) {
// lazy transformation make JUnit tests fail
result = ((MCRWrappedContent) result).getBaseContent();
}
editorSession.getValidator().clearValidationResults();
return result;
} else {
throw new MCRException("Xeditor needs parameterized MCRContentTransformer: " + transformer);
}
}
use of org.mycore.common.content.transformer.MCRParameterizedTransformer in project mycore by MyCoRe-Org.
the class MCRCompressServlet method getMetaDataContent.
private byte[] getMetaDataContent(MCRContent content, HttpServletRequest req) throws Exception {
// zip the object's Metadata
MCRParameterCollector parameters = new MCRParameterCollector(req);
if (parameters.getParameter("Style", null) == null) {
parameters.setParameter("Style", "compress");
}
MCRContentTransformer contentTransformer = MCRLayoutService.getContentTransformer(content.getDocType(), parameters);
ByteArrayOutputStream out = new ByteArrayOutputStream(32 * 1024);
if (contentTransformer instanceof MCRParameterizedTransformer) {
((MCRParameterizedTransformer) contentTransformer).transform(content, out, parameters);
} else {
contentTransformer.transform(content, out);
}
return out.toByteArray();
}
use of org.mycore.common.content.transformer.MCRParameterizedTransformer in project mycore by MyCoRe-Org.
the class MCRMetsIIIFPresentationImpl method getMets.
public Document getMets(String id) throws IOException, JDOMException, SAXException {
String objectid = MCRLinkTableManager.instance().getSourceOf(id).iterator().next();
MCRContentTransformer transformer = getTransformer();
MCRParameterCollector parameter = new MCRParameterCollector();
if (objectid != null && objectid.length() != 0) {
MCRDerivate derObj = MCRMetadataManager.retrieveMCRDerivate(MCRObjectID.getInstance(id));
MCRObjectID ownerID = derObj.getOwnerID();
objectid = ownerID.toString();
parameter.setParameter("objectID", objectid);
parameter.setParameter("derivateID", id);
}
MCRPath metsPath = MCRPath.getPath(id, "mets.xml");
if (!Files.exists(metsPath)) {
throw new MCRException("File not found: " + id);
}
MCRPathContent source = new MCRPathContent(metsPath);
MCRContent content = transformer instanceof MCRParameterizedTransformer ? ((MCRParameterizedTransformer) transformer).transform(source, parameter) : transformer.transform(source);
return content.asXML();
}
use of org.mycore.common.content.transformer.MCRParameterizedTransformer in project mycore by MyCoRe-Org.
the class MCRLayoutService method transform.
private void transform(HttpServletResponse response, MCRContentTransformer transformer, MCRContent source, MCRParameterCollector parameter, String filename) throws IOException, TransformerException, SAXException {
try {
String fileExtension = transformer.getFileExtension();
if (fileExtension != null && fileExtension.length() > 0) {
filename += "." + fileExtension;
}
response.setHeader("Content-Disposition", "inline;filename=\"" + filename + "\"");
String ct = transformer.getMimeType();
String enc = transformer.getEncoding();
if (enc != null) {
response.setCharacterEncoding(enc);
response.setContentType(ct + "; charset=" + enc);
} else {
response.setContentType(ct);
}
LOGGER.debug("MCRLayoutService starts to output {}", response.getContentType());
ServletOutputStream servletOutputStream = response.getOutputStream();
long start = System.currentTimeMillis();
try {
ByteArrayOutputStream bout = new ByteArrayOutputStream(INITIAL_BUFFER_SIZE);
if (transformer instanceof MCRParameterizedTransformer) {
MCRParameterizedTransformer paramTransformer = (MCRParameterizedTransformer) transformer;
paramTransformer.transform(source, bout, parameter);
} else {
transformer.transform(source, bout);
}
endCurrentTransaction();
response.setContentLength(bout.size());
bout.writeTo(servletOutputStream);
} finally {
LOGGER.debug("MCRContent transformation took {} ms.", System.currentTimeMillis() - start);
}
} catch (TransformerException | IOException | SAXException e) {
throw e;
} catch (Exception e) {
Throwable cause = e.getCause();
while (cause != null) {
if (cause instanceof TransformerException) {
throw (TransformerException) cause;
} else if (cause instanceof SAXException) {
throw (SAXException) cause;
} else if (cause instanceof IOException) {
throw (IOException) cause;
}
cause = cause.getCause();
}
throw new IOException(e);
}
}
Aggregations