/*
 * The MIT License
 *
 * Copyright (c) 2004-2009, Sun Microsystems, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice shall be included in
 * all copies or substantial portions of the Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
 * THE SOFTWARE.
 */
package org.jvnet.hudson.test;

import hudson.FilePath;
import hudson.Launcher;
import hudson.model.AbstractBuild;
import hudson.model.BuildListener;
import hudson.scm.ChangeLogParser;
import hudson.scm.NullSCM;
import hudson.scm.SCM;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.net.URL;
import java.util.zip.ZipEntry;
import java.util.zip.ZipInputStream;

/**
 * {@link SCM} useful for testing that extracts the given resource as a zip file.
 *
 * @author Kohsuke Kawaguchi
 */
public class ExtractResourceWithChangesSCM extends NullSCM {
    private final URL firstZip;
    private final URL secondZip;

    public ExtractResourceWithChangesSCM(URL firstZip, URL secondZip) {
        if ((firstZip == null) || (secondZip == null))
            throw new IllegalArgumentException();
        this.firstZip = firstZip;
        this.secondZip = secondZip;
    }

    @Override
    public boolean checkout(AbstractBuild build, Launcher launcher, FilePath workspace, BuildListener listener, File changeLogFile) throws IOException, InterruptedException {
        if (workspace.exists()) {
            listener.getLogger().println("Deleting existing workspace " + workspace.getRemote());
            workspace.deleteRecursive();
        }
        listener.getLogger().println("Staging first zip: " + firstZip);
        workspace.unzipFrom(firstZip.openStream());
        listener.getLogger().println("Staging second zip: " + secondZip);
        workspace.unzipFrom(secondZip.openStream());

        // Get list of files changed in secondZip.
        ZipInputStream zip = new ZipInputStream(secondZip.openStream());
        ZipEntry e;
        ExtractChangeLogParser.ExtractChangeLogEntry changeLog = new ExtractChangeLogParser.ExtractChangeLogEntry(secondZip.toString());

        try {
            while ((e = zip.getNextEntry()) != null) {
                if (!e.isDirectory())
                    changeLog.addFile(new ExtractChangeLogParser.FileInZip(e.getName()));
            }
        }
        finally {
            zip.close();
        }
        saveToChangeLog(changeLogFile, changeLog);

        return true;
    }

    @Override
    public ChangeLogParser createChangeLogParser() {
        return new ExtractChangeLogParser();
    }

    public static String escapeForXml(String string) {
        if (string == null) {
            return "";
        }

        // Loop through and replace the special chars.
        int size = string.length();
        char ch = 0;
        StringBuffer escapedString = new StringBuffer(size);
        for (int index = 0; index < size; index++) {
            // Convert special chars.
            ch = string.charAt(index);
            switch (ch) {
            case '&':
                escapedString.append("&amp;");
                break;
            case '<':
                escapedString.append("&lt;");
                break;
            case '>':
                escapedString.append("&gt;");
                break;
            case '\'':
                escapedString.append("&apos;");
                break;
            case '\"':
                escapedString.append("&quot;");
                break;
            default:
                escapedString.append(ch);
            }
        }

        return escapedString.toString().trim();
    }

    public void saveToChangeLog(File changeLogFile, ExtractChangeLogParser.ExtractChangeLogEntry changeLog) throws IOException {
        FileOutputStream outputStream = new FileOutputStream(changeLogFile);

        PrintStream stream = new PrintStream(outputStream, false, "UTF-8");

        stream.println("<?xml version='1.0' encoding='UTF-8'?>");
        stream.println("<extractChanges>");
        stream.println("<entry>");
        stream.println("<zipFile>" + escapeForXml(changeLog.getZipFile()) + "</zipFile>");
        stream.println("<file>");

        for (String fileName : changeLog.getAffectedPaths()) {
            stream.println("<fileName>" + escapeForXml(fileName) + "</fileName>");
        }

        stream.println("</file>");
        stream.println("</entry>");
        stream.println("</extractChanges>");

        stream.close();
    }
}

@@ -1224,24 +1224,44 @@
 n.Launcher;%0A
+import hudson.Util;%0A
 import hudso
@@ -3491,21 +3491,22 @@
 %7D%0A%0A    p
-ublic
+rivate
  static 
@@ -3554,988 +3554,50 @@
     
-if (string == null) %7B%0A            return %22%22;%0A        %7D%0A%0A        // Loop through and replace the special chars.%0A        int size = string.length();%0A        char ch = 0;%0A        StringBuffer escapedString = new StringBuffer(size);%0A        for (int index = 0; index %3C size; index++) %7B%0A            // Convert special chars.%0A            ch = string.charAt(index);%0A            switch (ch) %7B%0A            case '&':%0A                escapedString.append(%22&amp;%22);%0A                break;%0A            case '%3C':%0A                escapedString.append(%22&lt;%22);%0A                break;%0A            case '%3E':%0A                escapedString.append(%22&gt;%22);%0A                break;%0A            case '%5C'':%0A                escapedString.append(%22&apos;%22);%0A                break;%0A            case '%5C%22':%0A                escapedString.append(%22&quot;%22);%0A                break;%0A            default:%0A                escapedString.append(ch);%0A            %7D%0A        %7D%0A%0A        return escapedString.toString().trim(
+return Util.xmlEscape(Util.fixNull(string)
 );%0A 

simplified a bit.