001/*
002 * (C) Copyright 2006-2014 Nuxeo SA (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-2.1.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 *     Nuxeo - initial API and implementation
016 *
017 */
018package org.nuxeo.template.processors;
019
020import java.io.File;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024
025import org.nuxeo.common.utils.FileUtils;
026import org.nuxeo.ecm.core.api.Blob;
027import org.nuxeo.runtime.api.Framework;
028import org.nuxeo.template.api.TemplateProcessor;
029import org.nuxeo.template.api.adapters.TemplateBasedDocument;
030
031/**
032 * Common code between the implementations of {@link TemplateProcessor}
033 *
034 * @author Tiry (tdelprat@nuxeo.com)
035 */
036public abstract class AbstractTemplateProcessor implements TemplateProcessor {
037
038    protected static final int BUFFER_SIZE = 1024 * 64; // 64K
039
040    protected static final Log log = LogFactory.getLog(AbstractTemplateProcessor.class);
041
042    protected File getWorkingDir() {
043        String dirPath = System.getProperty("java.io.tmpdir") + "/NXTemplateProcessor" + System.currentTimeMillis();
044        File workingDir = new File(dirPath);
045        if (workingDir.exists()) {
046            FileUtils.deleteTree(workingDir);
047        }
048        workingDir.mkdir();
049        Framework.trackFile(workingDir, workingDir);
050        return workingDir;
051    }
052
053    protected Blob getSourceTemplateBlob(TemplateBasedDocument templateBasedDocument, String templateName) {
054        return templateBasedDocument.getTemplateBlob(templateName);
055    }
056
057}