| 1 | |
package org.jbehave.core.reporters; |
| 2 | |
|
| 3 | |
import java.io.File; |
| 4 | |
import java.io.FileNotFoundException; |
| 5 | |
import java.io.FileOutputStream; |
| 6 | |
import java.io.PrintStream; |
| 7 | |
|
| 8 | |
import org.apache.commons.lang.StringUtils; |
| 9 | |
import org.apache.commons.lang.builder.ToStringBuilder; |
| 10 | |
import org.apache.commons.lang.builder.ToStringStyle; |
| 11 | |
import org.jbehave.core.io.StoryLocation; |
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
public class FilePrintStreamFactory implements PrintStreamFactory { |
| 19 | |
|
| 20 | |
private final StoryLocation storyLocation; |
| 21 | |
private FileConfiguration configuration; |
| 22 | |
private File outputFile; |
| 23 | |
|
| 24 | |
public FilePrintStreamFactory(StoryLocation storyLocation) { |
| 25 | 5 | this(storyLocation, new FileConfiguration()); |
| 26 | 5 | } |
| 27 | |
|
| 28 | 61 | public FilePrintStreamFactory(StoryLocation storyLocation, FileConfiguration configuration) { |
| 29 | 61 | this.storyLocation = storyLocation; |
| 30 | 61 | this.configuration = configuration; |
| 31 | 61 | } |
| 32 | |
|
| 33 | |
public PrintStream createPrintStream() { |
| 34 | |
try { |
| 35 | 27 | outputFile = outputFile(); |
| 36 | 26 | outputFile.getParentFile().mkdirs(); |
| 37 | 26 | return new FilePrintStream(outputFile, false); |
| 38 | 1 | } catch (Exception e) { |
| 39 | 1 | throw new PrintStreamCreationFailed(outputFile, e); |
| 40 | |
} |
| 41 | |
} |
| 42 | |
|
| 43 | |
public File getOutputFile() { |
| 44 | 5 | return outputFile; |
| 45 | |
} |
| 46 | |
|
| 47 | |
public void useConfiguration(FileConfiguration configuration) { |
| 48 | 25 | this.configuration = configuration; |
| 49 | 25 | this.outputFile = outputFile(); |
| 50 | 25 | } |
| 51 | |
|
| 52 | |
public FileConfiguration configuration() { |
| 53 | 5 | return configuration; |
| 54 | |
} |
| 55 | |
|
| 56 | |
protected File outputFile() { |
| 57 | 53 | return new File(outputDirectory(), outputName()); |
| 58 | |
} |
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
protected File outputDirectory() { |
| 67 | 71 | return new File(configuration.getPathResolver().resolveDirectory(storyLocation, |
| 68 | |
configuration.getRelativeDirectory())); |
| 69 | |
} |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
protected String outputName() { |
| 78 | 60 | return configuration.getPathResolver().resolveName(storyLocation, configuration.getExtension()); |
| 79 | |
} |
| 80 | |
|
| 81 | |
public static interface FilePathResolver { |
| 82 | |
|
| 83 | |
String resolveDirectory(StoryLocation storyLocation, String relativeDirectory); |
| 84 | |
|
| 85 | |
String resolveName(StoryLocation storyLocation, String extension); |
| 86 | |
|
| 87 | |
} |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | 515 | public static abstract class AbstractPathResolver implements FilePathResolver { |
| 93 | |
|
| 94 | |
public String resolveDirectory(StoryLocation storyLocation, String relativeDirectory) { |
| 95 | 71 | File parent = new File(storyLocation.getCodeLocation().getFile()).getParentFile(); |
| 96 | 71 | return parent.getPath().replace('\\', '/') + "/" + relativeDirectory; |
| 97 | |
} |
| 98 | |
|
| 99 | |
} |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | 513 | public static class ResolveToPackagedName extends AbstractPathResolver { |
| 105 | |
|
| 106 | |
public String resolveName(StoryLocation storyLocation, String extension) { |
| 107 | 60 | String name = storyLocation.getPath().replace('/', '.'); |
| 108 | 60 | if (name.startsWith(".")) { |
| 109 | 5 | name = name.substring(1); |
| 110 | |
} |
| 111 | 60 | return StringUtils.substringBeforeLast(name, ".") + "." + extension; |
| 112 | |
} |
| 113 | |
|
| 114 | |
} |
| 115 | |
|
| 116 | |
|
| 117 | |
|
| 118 | |
|
| 119 | 2 | public static class ResolveToSimpleName extends AbstractPathResolver { |
| 120 | |
|
| 121 | |
public String resolveName(StoryLocation storyLocation, String extension) { |
| 122 | 4 | String name = storyLocation.getPath(); |
| 123 | 4 | if ( StringUtils.contains(name, '/') ){ |
| 124 | 2 | name = StringUtils.substringAfterLast(name, "/"); |
| 125 | |
} |
| 126 | 4 | return StringUtils.substringBeforeLast(name, ".") + "." + extension; |
| 127 | |
} |
| 128 | |
|
| 129 | |
} |
| 130 | |
|
| 131 | |
public static class FilePrintStream extends PrintStream { |
| 132 | |
|
| 133 | |
private final File outputFile; |
| 134 | |
private final boolean append; |
| 135 | |
|
| 136 | |
public FilePrintStream(File outputFile, boolean append) throws FileNotFoundException { |
| 137 | 26 | super(new FileOutputStream(outputFile, append)); |
| 138 | 26 | this.outputFile = outputFile; |
| 139 | 26 | this.append = append; |
| 140 | 26 | } |
| 141 | |
|
| 142 | |
@Override |
| 143 | |
public String toString() { |
| 144 | 1 | return new ToStringBuilder(this, ToStringStyle.SHORT_PREFIX_STYLE).append(outputFile).append(append) |
| 145 | |
.toString(); |
| 146 | |
} |
| 147 | |
|
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public static class FileConfiguration { |
| 156 | |
public static final String RELATIVE_DIRECTORY = "jbehave"; |
| 157 | |
public static final String EXTENSION = "html"; |
| 158 | |
|
| 159 | |
private final String relativeDirectory; |
| 160 | |
private final String extension; |
| 161 | |
private final FilePathResolver pathResolver; |
| 162 | |
|
| 163 | |
public FileConfiguration() { |
| 164 | 498 | this(EXTENSION); |
| 165 | 498 | } |
| 166 | |
|
| 167 | |
public FileConfiguration(String extension) { |
| 168 | 509 | this(RELATIVE_DIRECTORY, extension, new ResolveToPackagedName()); |
| 169 | 509 | } |
| 170 | |
|
| 171 | 580 | public FileConfiguration(String relativeDirectory, String extension, FilePathResolver pathResolver) { |
| 172 | 580 | this.relativeDirectory = relativeDirectory; |
| 173 | 580 | this.extension = extension; |
| 174 | 580 | this.pathResolver = pathResolver; |
| 175 | 580 | } |
| 176 | |
|
| 177 | |
public String getRelativeDirectory() { |
| 178 | 322 | return relativeDirectory; |
| 179 | |
} |
| 180 | |
|
| 181 | |
public String getExtension() { |
| 182 | 63 | return extension; |
| 183 | |
} |
| 184 | |
|
| 185 | |
public FilePathResolver getPathResolver() { |
| 186 | 378 | return pathResolver; |
| 187 | |
} |
| 188 | |
|
| 189 | |
@Override |
| 190 | |
public String toString() { |
| 191 | 2 | return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE); |
| 192 | |
} |
| 193 | |
|
| 194 | |
} |
| 195 | |
|
| 196 | |
@SuppressWarnings("serial") |
| 197 | |
public class PrintStreamCreationFailed extends RuntimeException { |
| 198 | 1 | public PrintStreamCreationFailed(File file, Exception cause) { |
| 199 | 1 | super("Failed to create print stream for file " + file, cause); |
| 200 | 1 | } |
| 201 | |
} |
| 202 | |
} |