Skip to content
This repository has been archived by the owner on Mar 27, 2018. It is now read-only.

Commit

Permalink
Merge branch 'SMA-18'
Browse files Browse the repository at this point in the history
  • Loading branch information
wilkinsona committed Oct 29, 2012
2 parents 6aabd74 + 43ab507 commit 3e15dbd
Show file tree
Hide file tree
Showing 17 changed files with 215 additions and 105 deletions.
Expand Up @@ -17,8 +17,10 @@
package org.springframework.migrationanalyzer.render.support.html;

import java.io.File;
import java.io.FileOutputStream;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;

import org.slf4j.Logger;
Expand All @@ -29,29 +31,42 @@
import org.springframework.stereotype.Component;

@Component
final class FileWriterFactory implements WriterFactory {
final class FileOutputFactory implements OutputFactory {

private final String rootPath;

private final Logger logger = LoggerFactory.getLogger(FileWriterFactory.class);
private final Logger logger = LoggerFactory.getLogger(FileOutputFactory.class);

@Autowired
FileWriterFactory(@Value("#{@configuration.outputPath}") String rootPath) {
FileOutputFactory(@Value("#{@configuration.outputPath}") String rootPath) {
this.rootPath = rootPath;
}

@Override
public Writer createWriter(String path, String pathPrefix) {
File file = new File(this.rootPath, new File(pathPrefix, path).getPath());
try {
IoUtils.createDirectoryIfNecessary(file.getParentFile());
return new FileWriter(createAndPrepareFile(path, pathPrefix));
} catch (IOException ioe) {
this.logger.error(String.format("Failed to create directory '%s'. Please check the output path and try again.", file.getParentFile()));
throw new OutputCreationFailedException("Failed to create writer", ioe);
}
}

@Override
public OutputStream createOutputStream(String path, String pathPrefix) {
try {
return new FileOutputStream(createAndPrepareFile(path, pathPrefix));
} catch (IOException ioe) {
throw new OutputCreationFailedException("Failed to create output stream", ioe);
}
}

private File createAndPrepareFile(String path, String pathPrefix) {
File file = new File(this.rootPath, new File(pathPrefix, path).getPath());
try {
return new FileWriter(file);
IoUtils.createDirectoryIfNecessary(file.getParentFile());
} catch (IOException ioe) {
throw new WriterCreationFailedException("Failed to create writer", ioe);
this.logger.error(String.format("Failed to create directory '%s'. Please check the output path and try again.", file.getParentFile()));
}
return file;
}
}
Expand Up @@ -17,7 +17,9 @@
package org.springframework.migrationanalyzer.render.support.html;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.Reader;
import java.io.Writer;

Expand Down Expand Up @@ -47,17 +49,17 @@ final class HtmlRenderEngine implements RenderEngine {

private final OutputPathGenerator outputPathGenerator;

private final WriterFactory writerFactory;
private final OutputFactory outputFactory;

@Autowired
HtmlRenderEngine(HtmlIndexRenderer indexRenderer, HtmlFileSystemEntryRenderer fileSystemEntryRenderer, HtmlSummaryRenderer summaryRenderer,
HtmlResultTypeRenderer resultTypeRenderer, OutputPathGenerator outputPathGenerator, WriterFactory writerFactory) {
HtmlResultTypeRenderer resultTypeRenderer, OutputPathGenerator outputPathGenerator, OutputFactory outputFactory) {
this.indexRenderer = indexRenderer;
this.fileSystemEntryRenderer = fileSystemEntryRenderer;
this.summaryRenderer = summaryRenderer;
this.resultTypeRenderer = resultTypeRenderer;
this.outputPathGenerator = outputPathGenerator;
this.writerFactory = writerFactory;
this.outputFactory = outputFactory;
}

@Override
Expand All @@ -81,24 +83,36 @@ public void render(AnalysisResult analysisResult, String outputPath) {

private void copyStaticResources(String archiveName) {
copyResource("css/style.css", archiveName);
copyResource("img/ModHdr_BG.png", archiveName);
copyResource("img/hdr-background.png", archiveName);
copyResource("img/hdr-glow.png", archiveName);
copyResource("img/springsource-logo.png", archiveName);
copyResource("img/title-background.png", archiveName);
copyBinaryResource("img/ModHdr_BG.png", archiveName);
copyBinaryResource("img/hdr-background.png", archiveName);
copyBinaryResource("img/hdr-glow.png", archiveName);
copyBinaryResource("img/springsource-logo.png", archiveName);
copyBinaryResource("img/title-background.png", archiveName);
copyResource("js/script.js", archiveName);
copyResource("banner.html", archiveName);
}

private void copyBinaryResource(String resource, String archiveName) {
InputStream input = getClass().getResourceAsStream(resource);
OutputStream output = this.outputFactory.createOutputStream(this.outputPathGenerator.generatePathFor(resource), archiveName);
try {
IoUtils.copy(input, output);
} catch (IOException ioe) {
throw new ResourceCopyFailedException(ioe);
} finally {
IoUtils.closeQuietly(input, output);
}
}

private void copyResource(String resource, String archiveName) {
Reader reader = new InputStreamReader(getClass().getResourceAsStream(resource));
Writer writer = this.writerFactory.createWriter(this.outputPathGenerator.generatePathFor(resource), archiveName);
Reader input = new InputStreamReader(getClass().getResourceAsStream(resource));
Writer output = this.outputFactory.createWriter(this.outputPathGenerator.generatePathFor(resource), archiveName);
try {
IoUtils.copy(reader, writer);
IoUtils.copy(input, output);
} catch (IOException ioe) {
throw new ResourceCopyFailedException(ioe);
} finally {
IoUtils.closeQuietly(reader, writer);
IoUtils.closeQuietly(input, output);
}
}

Expand Down
Expand Up @@ -16,11 +16,11 @@

package org.springframework.migrationanalyzer.render.support.html;

final class WriterCreationFailedException extends RuntimeException {
final class OutputCreationFailedException extends RuntimeException {

private static final long serialVersionUID = 8267925121266569818L;

WriterCreationFailedException(String message, Throwable cause) {
OutputCreationFailedException(String message, Throwable cause) {
super(message, cause);
}

Expand Down
Expand Up @@ -16,9 +16,10 @@

package org.springframework.migrationanalyzer.render.support.html;

import java.io.OutputStream;
import java.io.Writer;

interface WriterFactory {
interface OutputFactory {

/**
* Creates a writer that outputs to a file at a specified path
Expand All @@ -27,4 +28,12 @@ interface WriterFactory {
* @return The writer
*/
Writer createWriter(String path, String pathPrefix);

/**
* Creates an OutputStream that outputs to a file at a specified path
*
* @param path The path to the file to be written
* @return The output stream
*/
OutputStream createOutputStream(String path, String pathPrefix);
}
Expand Up @@ -54,15 +54,15 @@ final class StandardHtmlFileSystemEntryRenderer implements HtmlFileSystemEntryRe

private final ViewRenderer viewRenderer;

private final WriterFactory writerFactory;
private final OutputFactory outputFactory;

@Autowired
StandardHtmlFileSystemEntryRenderer(Set<ByFileSystemEntryController> fileSystemEntryControllers, ViewRenderer viewRenderer,
RootAwareOutputPathGenerator outputPathGenerator, WriterFactory writerFactory, SourceAccessor sourceAccessor) {
RootAwareOutputPathGenerator outputPathGenerator, OutputFactory outputFactory, SourceAccessor sourceAccessor) {
this.fileSystemEntryControllers = fileSystemEntryControllers;
this.viewRenderer = viewRenderer;
this.outputPathGenerator = outputPathGenerator;
this.writerFactory = writerFactory;
this.outputFactory = outputFactory;
this.sourceAccessor = sourceAccessor;
}

Expand All @@ -75,7 +75,7 @@ public void renderFileSystemEntries(AnalysisResult analysisResult) {
for (FileSystemEntry fileSystemEntry : fileSystemEntries) {
Writer writer = null;
try {
writer = this.writerFactory.createWriter(this.outputPathGenerator.generatePathFor(fileSystemEntry), analysisResult.getArchiveName());
writer = this.outputFactory.createWriter(this.outputPathGenerator.generatePathFor(fileSystemEntry), analysisResult.getArchiveName());
AnalysisResult entryResult = analysisResult.getResultForEntry(fileSystemEntry);
renderByFileHeader(fileSystemEntry, writer);
for (Class<?> resultType : entryResult.getResultTypes()) {
Expand All @@ -99,7 +99,7 @@ private void renderFileSystemEntryContents(Set<FileSystemEntry> fileSystemEntrie
Writer writer = null;

try {
writer = this.writerFactory.createWriter(contentsPath, archiveName);
writer = this.outputFactory.createWriter(contentsPath, archiveName);
this.viewRenderer.renderViewWithModel(VIEW_NAME_FILE_CONTENTS, model, writer);
} finally {
IoUtils.closeQuietly(writer);
Expand Down
Expand Up @@ -33,20 +33,20 @@ final class StandardHtmlIndexRenderer implements HtmlIndexRenderer {

private final ViewRenderer viewRenderer;

private final WriterFactory writerFactory;
private final OutputFactory outputFactory;

@Autowired
StandardHtmlIndexRenderer(ViewRenderer viewRenderer, OutputPathGenerator outputPathGenerator, WriterFactory writerFactory) {
StandardHtmlIndexRenderer(ViewRenderer viewRenderer, OutputPathGenerator outputPathGenerator, OutputFactory outputFactory) {
this.viewRenderer = viewRenderer;
this.outputPathGenerator = outputPathGenerator;
this.writerFactory = writerFactory;
this.outputFactory = outputFactory;
}

@Override
public void renderIndex(AnalysisResult analysisResult) {
Writer writer = null;
try {
writer = this.writerFactory.createWriter(this.outputPathGenerator.generatePathForIndex(), analysisResult.getArchiveName());
writer = this.outputFactory.createWriter(this.outputPathGenerator.generatePathForIndex(), analysisResult.getArchiveName());
this.viewRenderer.renderViewWithEmptyModel(VIEW_NAME_INDEX, writer);
} finally {
IoUtils.closeQuietly(writer);
Expand Down
Expand Up @@ -46,19 +46,19 @@ final class StandardHtmlResultTypeRenderer implements HtmlResultTypeRenderer {

private final Set<ByResultTypeController> resultTypeControllers;

private final WriterFactory writerFactory;
private final OutputFactory outputFactory;

private final ViewRenderer viewRenderer;

private final ResultTypeDisplayNameResolver resultTypeDisplayNameResolver;

@Autowired
StandardHtmlResultTypeRenderer(Set<ByResultTypeController> resultTypeControllers, ViewRenderer viewRenderer,
RootAwareOutputPathGenerator outputPathGenerator, WriterFactory writerFactory, ResultTypeDisplayNameResolver resultTypeDisplayNameResolver) {
RootAwareOutputPathGenerator outputPathGenerator, OutputFactory outputFactory, ResultTypeDisplayNameResolver resultTypeDisplayNameResolver) {
this.resultTypeControllers = resultTypeControllers;
this.viewRenderer = viewRenderer;
this.outputPathGenerator = outputPathGenerator;
this.writerFactory = writerFactory;
this.outputFactory = outputFactory;
this.resultTypeDisplayNameResolver = resultTypeDisplayNameResolver;
}

Expand All @@ -69,7 +69,7 @@ public void renderResultTypes(AnalysisResult analysisResult) {
for (Class<?> resultType : resultTypes) {
Writer writer = null;
try {
writer = this.writerFactory.createWriter(this.outputPathGenerator.generatePathFor(resultType), analysisResult.getArchiveName());
writer = this.outputFactory.createWriter(this.outputPathGenerator.generatePathFor(resultType), analysisResult.getArchiveName());
renderByResultTypeHeader(resultType, writer);
this.viewRenderer.render(resultType, analysisResult.getResultEntries(resultType), this.resultTypeControllers, writer,
new LocationAwareOutputPathGenerator(this.outputPathGenerator, resultType), REPORT_TYPE);
Expand All @@ -85,7 +85,7 @@ private void renderResultTypeContents(Set<Class<?>> resultTypes, String archiveN
try {
String contentsPath = this.outputPathGenerator.generatePathForResultTypeContents();
OutputPathGenerator locationAwarePathGenerator = new LocationAwareOutputPathGenerator(this.outputPathGenerator, contentsPath);
writer = this.writerFactory.createWriter(contentsPath, archiveName);
writer = this.outputFactory.createWriter(contentsPath, archiveName);
Map<String, Object> model = createContentsModel(resultTypes, locationAwarePathGenerator);
this.viewRenderer.renderViewWithModel(VIEW_NAME_RESULT_CONTENTS, model, writer);
} finally {
Expand Down
Expand Up @@ -58,27 +58,27 @@ final class StandardHtmlSummaryRenderer implements HtmlSummaryRenderer {

private final RootAwareOutputPathGenerator outputPathGenerator;

private final WriterFactory writerFactory;
private final OutputFactory outputFactory;

private final Set<SummaryController> summaryControllers;

private final ViewRenderer viewRenderer;

@Autowired
StandardHtmlSummaryRenderer(Set<SummaryController> summaryControllers, ViewRenderer viewRenderer,
RootAwareOutputPathGenerator outputPathGenerator, WriterFactory writerFactory) {
RootAwareOutputPathGenerator outputPathGenerator, OutputFactory outputFactory) {
this.summaryControllers = summaryControllers;
this.viewRenderer = viewRenderer;
this.outputPathGenerator = outputPathGenerator;
this.writerFactory = writerFactory;
this.outputFactory = outputFactory;
}

@Override
public void renderSummary(AnalysisResult analysisResult) {
Writer writer = null;
try {
String summaryPath = this.outputPathGenerator.generatePathForSummary();
writer = this.writerFactory.createWriter(summaryPath, analysisResult.getArchiveName());
writer = this.outputFactory.createWriter(summaryPath, analysisResult.getArchiveName());

this.viewRenderer.renderViewWithEmptyModel(VIEW_NAME_SUMMARY_HEADER, writer);

Expand Down
@@ -0,0 +1,79 @@
/*
* Copyright 2010 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.springframework.migrationanalyzer.render.support.html;

import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Writer;

import org.junit.Test;
import org.springframework.migrationanalyzer.util.IoUtils;

public final class FileOutputFactoryTests {

private final FileOutputFactory outputFactory = new FileOutputFactory("build");

@Test
public void createWriter() throws IOException {
File dir = new File(new File("build", "prefix"), "alpha");
File expectedOutput = new File(dir, "bravo.txt");

if (expectedOutput.exists()) {
assertTrue(expectedOutput.delete() && dir.delete());
}

assertFalse(expectedOutput.exists());

Writer writer = this.outputFactory.createWriter("alpha/bravo.txt", "prefix");

try {
assertTrue(dir.isDirectory());
writer.append("hello world");

assertTrue(expectedOutput.exists());
} finally {
IoUtils.closeQuietly(writer);
}
}

@Test
public void createOutputStream() throws IOException {
File dir = new File(new File("build", "prefix"), "charlie");
File expectedOutput = new File(dir, "delta.txt");

if (expectedOutput.exists()) {
assertTrue(expectedOutput.delete() && dir.delete());
}

assertFalse(expectedOutput.exists());

OutputStream output = this.outputFactory.createOutputStream("charlie/delta.txt", "prefix");

try {
assertTrue(dir.isDirectory());
output.write(new byte[] { 1, 2, 3, 4 });
assertTrue(expectedOutput.exists());
} finally {
IoUtils.closeQuietly(output);
}
}

}

0 comments on commit 3e15dbd

Please sign in to comment.