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.nuxeo.ecm.core.api.Blob;
026import org.nuxeo.ecm.core.api.Blobs;
027import org.nuxeo.template.processors.AbstractBindingResolver;
028
029import fr.opensagres.xdocreport.core.document.SyntaxKind;
030import fr.opensagres.xdocreport.document.images.IImageProvider;
031import fr.opensagres.xdocreport.template.formatter.FieldsMetadata;
032import freemarker.template.TemplateModelException;
033
034/**
035 * @author <a href="mailto:tdelprat@nuxeo.com">Tiry</a>
036 */
037public class XDocReportBindingResolver extends AbstractBindingResolver {
038
039    protected final FieldsMetadata metadata;
040
041    public XDocReportBindingResolver(FieldsMetadata metadata) {
042        this.metadata = metadata;
043    }
044
045    @Override
046    protected String handleHtmlField(String paramName, String htmlValue) {
047        metadata.addFieldAsTextStyling(paramName, SyntaxKind.Html);
048        return super.handleHtmlField(paramName, htmlValue);
049    }
050
051    @Override
052    protected void handleBlobField(String paramName, Blob blobValue) {
053        if ("text/html".equals(blobValue.getMimeType())) {
054            metadata.addFieldAsTextStyling(paramName, SyntaxKind.Html);
055        }
056    }
057
058    @Override
059    protected Object handlePictureField(String paramName, Blob blobValue) {
060        if (blobValue == null) {
061            // manage a default picture : blank one :)
062            try (InputStream is = this.getClass().getClassLoader().getResourceAsStream("blank.png")) {
063                byte[] bin = IOUtils.toByteArray(is);
064                blobValue = Blobs.createBlob(bin, "image/png");
065                blobValue.setFilename("blank.png");
066            } catch (IOException e) {
067                log.error("Unable to read fake Blob", e);
068            }
069        }
070        IImageProvider imgBlob = new BlobImageProvider(blobValue);
071        metadata.addFieldAsImage(paramName);
072        return imgBlob;
073    }
074
075    @Override
076    protected Object handleLoop(String paramName, Object value) {
077        metadata.addFieldAsList(paramName);
078        try {
079            return getWrapper().wrap(value);
080        } catch (TemplateModelException e) {
081            return null;
082        }
083    }
084
085}