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.StringWriter;
027    import java.util.Collection;
028    
029    public class OutputMessageUtil {
030        private static final String SOURCE_FILES_PREFIX = "Sources:";
031        private static final String OUTPUT_FILES_PREFIX = "Output:";
032    
033        @NotNull
034        public static String renderException(@NotNull Throwable e) {
035            StringWriter out = new StringWriter();
036            e.printStackTrace(new PrintWriter(out));
037            return out.toString();
038        }
039    
040        @NotNull
041        public static String formatOutputMessage(Collection<File> sourceFiles, File outputFile) {
042            return OUTPUT_FILES_PREFIX + "\n" + outputFile.getPath() + "\n" +
043                   SOURCE_FILES_PREFIX + "\n" + StringUtil.join(sourceFiles, "\n");
044        }
045    
046        @Nullable
047        public static Output parseOutputMessage(@NotNull String message) {
048            String[] strings = message.split("\n");
049    
050            // Must have at least one line per prefix
051            if (strings.length <= 2) return null;
052    
053            if (!OUTPUT_FILES_PREFIX.equals(strings[0])) return null;
054    
055            if (SOURCE_FILES_PREFIX.equals(strings[1])) {
056                // Output:
057                // Sources:
058                // ...
059                return new Output(parseSourceFiles(strings, 2), null);
060            }
061            else {
062                File outputFile = new File(strings[1]);
063    
064                if (!SOURCE_FILES_PREFIX.equals(strings[2])) return null;
065    
066                return new Output(parseSourceFiles(strings, 3), outputFile);
067            }
068        }
069    
070        private static Collection<File> parseSourceFiles(String[] strings, int start) {
071            Collection<File> sourceFiles = ContainerUtil.newArrayList();
072            for (int i = start; i < strings.length; i++) {
073                sourceFiles.add(new File(strings[i]));
074            }
075            return sourceFiles;
076        }
077    
078        public static class Output {
079            @NotNull
080            public final Collection<File> sourceFiles;
081            @Nullable
082            public final File outputFile;
083    
084            public Output(@NotNull Collection<File> sourceFiles, @Nullable File outputFile) {
085                this.sourceFiles = sourceFiles;
086                this.outputFile = outputFile;
087            }
088        }
089    }