001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     bstefanescu
016 *
017 * $Id$
018 */
019
020package org.nuxeo.ecm.platform.rendering.impl;
021
022import java.io.ByteArrayInputStream;
023import java.io.InputStream;
024
025import org.nuxeo.ecm.platform.rendering.RenderingResult;
026
027/**
028 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
029 */
030public abstract class DefaultRenderingResult implements RenderingResult {
031
032    private static final long serialVersionUID = 6570212240033856735L;
033
034    protected final String formatName;
035
036    /**
037     * Constructor taking a rendering result name as argument.
038     *
039     * @param formatName
040     */
041    protected DefaultRenderingResult(String formatName) {
042        this.formatName = formatName;
043    }
044
045    /**
046     * @return name of the engine that created it
047     */
048    public String getFormatName() {
049        return formatName;
050    }
051
052    public abstract Object getOutcome();
053
054    public InputStream getStream() {
055        Object outcome = getOutcome();
056        if (outcome instanceof InputStream) {
057            return (InputStream) outcome;
058        } else if (outcome instanceof byte[]) {
059            return new ByteArrayInputStream((byte[]) outcome);
060        } else if (outcome instanceof CharSequence) {
061            return new ByteArrayInputStream(outcome.toString().getBytes());
062        }
063        return getAdapter(InputStream.class);
064    }
065
066    public <E> E getAdapter(Class<E> adapter) {
067        Object outcome = getOutcome();
068        if (adapter.isAssignableFrom(outcome.getClass())) {
069            return adapter.cast(outcome);
070        }
071        return null;
072    }
073
074}