001/*
002 * (C) Copyright 2012-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.template.processors.xdocreport;
020
021import java.io.IOException;
022import java.io.InputStream;
023
024import org.apache.commons.io.IOUtils;
025import org.apache.logging.log4j.LogManager;
026import org.apache.logging.log4j.Logger;
027import org.nuxeo.ecm.core.api.Blob;
028import org.nuxeo.ecm.core.api.Blobs;
029import org.nuxeo.template.processors.AbstractBindingResolver;
030
031import fr.opensagres.xdocreport.core.document.SyntaxKind;
032import fr.opensagres.xdocreport.document.images.IImageProvider;
033import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
034import freemarker.template.TemplateModelException;
035
036/**
037 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
038 */
039public class XDocReportBindingResolver extends AbstractBindingResolver {
040
041    private static final Logger log = LogManager.getLogger(XDocReportBindingResolver.class);
042
043    protected final FieldsMetadata metadata;
044
045    public XDocReportBindingResolver(FieldsMetadata metadata) {
046        this.metadata = metadata;
047    }
048
049    @Override
050    protected String handleHtmlField(String paramName, String htmlValue) {
051        metadata.addFieldAsTextStyling(paramName, SyntaxKind.Html);
052        return super.handleHtmlField(paramName, htmlValue);
053    }
054
055    @Override
056    protected void handleBlobField(String paramName, Blob blobValue) {
057        if ("text/html".equals(blobValue.getMimeType())) {
058            metadata.addFieldAsTextStyling(paramName, SyntaxKind.Html);
059        }
060    }
061
062    @Override
063    protected Object handlePictureField(String paramName, Blob blobValue) {
064        if (blobValue == null) {
065            // manage a default picture : blank one :)
066            try (InputStream is = this.getClass().getClassLoader().getResourceAsStream("blank.png")) {
067                byte[] bin = IOUtils.toByteArray(is);
068                blobValue = Blobs.createBlob(bin, "image/png");
069                blobValue.setFilename("blank.png");
070            } catch (IOException e) {
071                log.error("Unable to read fake Blob", e);
072            }
073        }
074        IImageProvider imgBlob = new BlobImageProvider(blobValue);
075        metadata.addFieldAsImage(paramName);
076        return imgBlob;
077    }
078
079    @Override
080    protected Object handleLoop(String paramName, Object value) {
081        metadata.addFieldAsList(paramName);
082        try {
083            return getWrapper().wrap(value);
084        } catch (TemplateModelException e) {
085            return null;
086        }
087    }
088
089}