Search in sources :

Example 1 with MCRParameterizedTransformer

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;
}
Also used : MCRParameterCollector(org.mycore.common.xsl.MCRParameterCollector) MCRParameterizedTransformer(org.mycore.common.content.transformer.MCRParameterizedTransformer) MCRJDOMContent(org.mycore.common.content.MCRJDOMContent) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer) MCRContent(org.mycore.common.content.MCRContent)

Example 2 with MCRParameterizedTransformer

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);
    }
}
Also used : MCRException(org.mycore.common.MCRException) MCRParameterizedTransformer(org.mycore.common.content.transformer.MCRParameterizedTransformer) MCRWrappedContent(org.mycore.common.content.MCRWrappedContent) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer) MCRContent(org.mycore.common.content.MCRContent)

Example 3 with MCRParameterizedTransformer

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();
}
Also used : MCRParameterCollector(org.mycore.common.xsl.MCRParameterCollector) MCRParameterizedTransformer(org.mycore.common.content.transformer.MCRParameterizedTransformer) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer) ByteArrayOutputStream(java.io.ByteArrayOutputStream)

Example 4 with MCRParameterizedTransformer

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();
}
Also used : MCRParameterCollector(org.mycore.common.xsl.MCRParameterCollector) MCRException(org.mycore.common.MCRException) MCRPathContent(org.mycore.common.content.MCRPathContent) MCRParameterizedTransformer(org.mycore.common.content.transformer.MCRParameterizedTransformer) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer) MCRDerivate(org.mycore.datamodel.metadata.MCRDerivate) MCRObjectID(org.mycore.datamodel.metadata.MCRObjectID) MCRPath(org.mycore.datamodel.niofs.MCRPath) MCRContent(org.mycore.common.content.MCRContent)

Example 5 with MCRParameterizedTransformer

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);
    }
}
Also used : ServletOutputStream(javax.servlet.ServletOutputStream) MCRParameterizedTransformer(org.mycore.common.content.transformer.MCRParameterizedTransformer) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) TransformerException(javax.xml.transform.TransformerException) TransformerException(javax.xml.transform.TransformerException) IOException(java.io.IOException) MCRException(org.mycore.common.MCRException) SAXException(org.xml.sax.SAXException) SAXException(org.xml.sax.SAXException)

Aggregations

MCRParameterizedTransformer (org.mycore.common.content.transformer.MCRParameterizedTransformer)6 MCRContentTransformer (org.mycore.common.content.transformer.MCRContentTransformer)4 MCRException (org.mycore.common.MCRException)3 MCRContent (org.mycore.common.content.MCRContent)3 MCRParameterCollector (org.mycore.common.xsl.MCRParameterCollector)3 ByteArrayOutputStream (java.io.ByteArrayOutputStream)2 IOException (java.io.IOException)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 TransformerException (javax.xml.transform.TransformerException)1 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)1 MCRPathContent (org.mycore.common.content.MCRPathContent)1 MCRWrappedContent (org.mycore.common.content.MCRWrappedContent)1 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)1 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)1 MCRPath (org.mycore.datamodel.niofs.MCRPath)1 SAXException (org.xml.sax.SAXException)1