Search in sources :

Example 1 with MCRContentTransformer

use of org.mycore.common.content.transformer.MCRContentTransformer 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 MCRContentTransformer

use of org.mycore.common.content.transformer.MCRContentTransformer 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 MCRContentTransformer

use of org.mycore.common.content.transformer.MCRContentTransformer in project mycore by MyCoRe-Org.

the class MCRLayoutService method getTransformedContent.

public MCRContent getTransformedContent(HttpServletRequest req, HttpServletResponse res, MCRContent source) throws IOException, TransformerException, SAXException {
    String docType = source.getDocType();
    try {
        MCRParameterCollector parameter = new MCRParameterCollector(req);
        MCRContentTransformer transformer = getContentTransformer(docType, parameter);
        String filename = getFileName(req, parameter);
        return transform(transformer, source, parameter, filename);
    } catch (IOException | TransformerException | SAXException ex) {
        throw ex;
    } catch (MCRException ex) {
        // generating an error page when there is an error in the stylesheet
        if (!"mcr_error".equals(docType)) {
            throw ex;
        }
        String msg = "Error while generating error page!";
        LOGGER.warn(msg, ex);
        res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
        return null;
    } catch (Exception e) {
        throw new MCRException(e);
    }
}
Also used : MCRParameterCollector(org.mycore.common.xsl.MCRParameterCollector) MCRException(org.mycore.common.MCRException) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer) 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)

Example 4 with MCRContentTransformer

use of org.mycore.common.content.transformer.MCRContentTransformer in project mycore by MyCoRe-Org.

the class MCRLayoutService method doLayout.

public void doLayout(HttpServletRequest req, HttpServletResponse res, MCRContent source) throws IOException, TransformerException, SAXException {
    if (res.isCommitted()) {
        LOGGER.warn("Response already committed: {}:{}", res.getStatus(), res.getContentType());
        return;
    }
    String docType = source.getDocType();
    try {
        MCRParameterCollector parameter = new MCRParameterCollector(req);
        MCRContentTransformer transformer = getContentTransformer(docType, parameter);
        String filename = getFileName(req, parameter);
        transform(res, transformer, source, parameter, filename);
    } catch (IOException | TransformerException | SAXException ex) {
        throw ex;
    } catch (MCRException ex) {
        // generating an error page when there is an error in the stylesheet
        if (!"mcr_error".equals(docType)) {
            throw ex;
        }
        String msg = "Error while generating error page!";
        LOGGER.warn(msg, ex);
        res.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, msg);
    } catch (Exception e) {
        throw new MCRException(e);
    }
}
Also used : MCRParameterCollector(org.mycore.common.xsl.MCRParameterCollector) MCRException(org.mycore.common.MCRException) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer) 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)

Example 5 with MCRContentTransformer

use of org.mycore.common.content.transformer.MCRContentTransformer in project mycore by MyCoRe-Org.

the class MCRLayoutTransformerFactory method buildLayoutTransformer.

private static MCRContentTransformer buildLayoutTransformer(String id) throws Exception {
    String idStripped = id.replaceAll("-default$", "");
    LOGGER.info("Configure property MCR.ContentTransformer.{}.Class if you do not want to use default behaviour.", idStripped);
    String stylesheet = getResourceName(id);
    if (stylesheet == null) {
        LOGGER.info("Using noop transformer for {}", idStripped);
        return NOOP_TRANSFORMER;
    }
    String[] stylesheets = getStylesheets(idStripped, stylesheet);
    MCRContentTransformer transformer = MCRXSLTransformer.getInstance(stylesheets);
    String mimeType = transformer.getMimeType();
    if (isPDF(mimeType)) {
        transformer = new MCRTransformerPipe(transformer, fopper);
        LOGGER.info("Using stylesheet '{}' for {} and MCRFopper for PDF output.", Lists.newArrayList(stylesheets), idStripped);
    } else {
        LOGGER.info("Using stylesheet '{}' for {}", Lists.newArrayList(stylesheets), idStripped);
    }
    transformers.put(id, transformer);
    return transformer;
}
Also used : MCRTransformerPipe(org.mycore.common.content.transformer.MCRTransformerPipe) MCRContentTransformer(org.mycore.common.content.transformer.MCRContentTransformer)

Aggregations

MCRContentTransformer (org.mycore.common.content.transformer.MCRContentTransformer)12 MCRParameterCollector (org.mycore.common.xsl.MCRParameterCollector)6 MCRException (org.mycore.common.MCRException)5 MCRParameterizedTransformer (org.mycore.common.content.transformer.MCRParameterizedTransformer)4 IOException (java.io.IOException)3 MCRContent (org.mycore.common.content.MCRContent)3 MCRJDOMContent (org.mycore.common.content.MCRJDOMContent)3 SAXException (org.xml.sax.SAXException)3 TransformerException (javax.xml.transform.TransformerException)2 MCRDerivate (org.mycore.datamodel.metadata.MCRDerivate)2 MCRObjectID (org.mycore.datamodel.metadata.MCRObjectID)2 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1 StringWriter (java.io.StringWriter)1 Comment (org.jdom2.Comment)1 Document (org.jdom2.Document)1 Element (org.jdom2.Element)1 JDOMException (org.jdom2.JDOMException)1 XMLOutputter (org.jdom2.output.XMLOutputter)1 MCRConfigurationException (org.mycore.common.config.MCRConfigurationException)1 MCRPathContent (org.mycore.common.content.MCRPathContent)1