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 static java.nio.charset.StandardCharsets.UTF_8;
025
026import java.io.File;
027import java.io.FileOutputStream;
028import java.io.IOException;
029
030import org.apache.commons.io.FileUtils;
031import org.nuxeo.common.xmap.annotation.XNode;
032import org.nuxeo.common.xmap.annotation.XObject;
033
034/**
035 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
036 */
037@XObject("append")
038public class AppendOperation {
039
040    @XNode("@path")
041    protected String path;
042
043    @XNode("@target")
044    protected String target;
045
046    @XNode("@appendNewLine")
047    protected boolean appendNewLine = true;
048
049    public void run(Installer installer, File bundleDir, File installDir) throws IOException {
050        // ctx.getBundle().getEntryPaths(path);
051        File src = new File(bundleDir, path);
052        if (src.isFile()) {
053            String text = FileUtils.readFileToString(src, UTF_8);
054            if (appendNewLine) {
055                String crlf = System.lineSeparator();
056                text = crlf + text + crlf;
057            }
058            File file = new File(installDir, target);
059            File parent = file.getParentFile();
060            if (!parent.isDirectory()) {
061                parent.mkdirs();
062            }
063            boolean append = file.exists();
064            try (FileOutputStream out = new FileOutputStream(file, append)) {
065                out.write(text.getBytes());
066            }
067        } else {
068            installer.logWarning("Could not find path: " + path + " to append");
069        }
070    }
071
072}