001    /*
002     * Copyright 2010-2015 JetBrains s.r.o.
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    
017    package org.jetbrains.kotlin.cli.common.messages;
018    
019    import com.intellij.openapi.util.text.StringUtil;
020    import com.intellij.util.containers.ContainerUtil;
021    import org.jetbrains.annotations.NotNull;
022    import org.jetbrains.annotations.Nullable;
023    
024    import java.io.File;
025    import java.io.PrintWriter;
026    import java.io.Serializable;
027    import java.io.StringWriter;
028    import java.util.Collection;
029    
030    public class OutputMessageUtil {
031        private static final String SOURCE_FILES_PREFIX = "Sources:";
032        private static final String OUTPUT_FILES_PREFIX = "Output:";
033    
034        @NotNull
035        public static String renderException(@NotNull Throwable e) {
036            StringWriter out = new StringWriter();
037            e.printStackTrace(new PrintWriter(out));
038            return out.toString();
039        }
040    
041        @NotNull
042        public static String formatOutputMessage(Collection<File> sourceFiles, File outputFile) {
043            return OUTPUT_FILES_PREFIX + "\n" + outputFile.getPath() + "\n" +
044                   SOURCE_FILES_PREFIX + "\n" + StringUtil.join(sourceFiles, "\n");
045        }
046    
047        @Nullable
048        public static Output parseOutputMessage(@NotNull String message) {
049            String[] strings = message.split("\n");
050    
051            // Must have at least one line per prefix
052            if (strings.length <= 2) return null;
053    
054            if (!OUTPUT_FILES_PREFIX.equals(strings[0])) return null;
055    
056            if (SOURCE_FILES_PREFIX.equals(strings[1])) {
057                // Output:
058                // Sources:
059                // ...
060                return new Output(parseSourceFiles(strings, 2), null);
061            }
062            else {
063                File outputFile = new File(strings[1]);
064    
065                if (!SOURCE_FILES_PREFIX.equals(strings[2])) return null;
066    
067                return new Output(parseSourceFiles(strings, 3), outputFile);
068            }
069        }
070    
071        private static Collection<File> parseSourceFiles(String[] strings, int start) {
072            Collection<File> sourceFiles = ContainerUtil.newArrayList();
073            for (int i = start; i < strings.length; i++) {
074                sourceFiles.add(new File(strings[i]));
075            }
076            return sourceFiles;
077        }
078    
079        public static class Output implements Serializable {
080            @NotNull
081            public final Collection<File> sourceFiles;
082            @Nullable
083            public final File outputFile;
084    
085            public Output(@NotNull Collection<File> sourceFiles, @Nullable File outputFile) {
086                this.sourceFiles = sourceFiles;
087                this.outputFile = outputFile;
088            }
089    
090            static final long serialVersionUID = 0L;
091        }
092    }