001/*
002 * (C) Copyright 2006-2008 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 *     bstefanescu
018 *
019 * $Id$
020 */
021
022package org.nuxeo.ecm.webengine.install;
023
024import java.io.File;
025import java.io.FileOutputStream;
026import java.io.IOException;
027
028import org.nuxeo.common.utils.FileUtils;
029import org.nuxeo.common.xmap.annotation.XNode;
030import org.nuxeo.common.xmap.annotation.XObject;
031
032/**
033 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
034 */
035@XObject("append")
036public class AppendOperation {
037
038    @XNode("@path")
039    protected String path;
040
041    @XNode("@target")
042    protected String target;
043
044    @XNode("@appendNewLine")
045    protected boolean appendNewLine = true;
046
047    public void run(Installer installer, File bundleDir, File installDir) throws IOException {
048        // ctx.getBundle().getEntryPaths(path);
049        File src = new File(bundleDir, path);
050        if (src.isFile()) {
051            String text = FileUtils.readFile(src);
052            if (appendNewLine) {
053                String crlf = System.getProperty("line.separator");
054                text = crlf + text + crlf;
055            }
056            File file = new File(installDir, target);
057            File parent = file.getParentFile();
058            if (!parent.isDirectory()) {
059                parent.mkdirs();
060            }
061            boolean append = file.exists();
062            FileOutputStream out = new FileOutputStream(file, append);
063            try {
064                out.write(text.getBytes());
065            } finally {
066                out.close();
067            }
068        } else {
069            installer.logWarning("Could not find path: " + path + " to append");
070        }
071    }
072
073}