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