Break the package cycle caused by the introduction of SnippetFormat

This commit is contained in:
Andy Wilkinson
2016-02-08 10:56:12 +00:00
parent c0fac14fdf
commit cb9e10bf5a
94 changed files with 273 additions and 244 deletions

View File

@@ -104,7 +104,7 @@ public abstract class RestDocumentationConfigurer<S extends AbstractConfigurer,
.get(SnippetConfiguration.class.getName());
engineToUse = new MustacheTemplateEngine(
new StandardTemplateResourceResolver(
snippetConfiguration.getFormat()));
snippetConfiguration.getTemplateFormat()));
}
configuration.put(TemplateEngine.class.getName(), engineToUse);
}
@@ -129,7 +129,7 @@ public abstract class RestDocumentationConfigurer<S extends AbstractConfigurer,
resolverToUse = new StandardWriterResolver(
new RestDocumentationContextPlaceholderResolver(context),
snippetConfiguration.getEncoding(),
snippetConfiguration.getFormat());
snippetConfiguration.getTemplateFormat());
}
configuration.put(WriterResolver.class.getName(), resolverToUse);
}

View File

@@ -16,7 +16,7 @@
package org.springframework.restdocs.config;
import org.springframework.restdocs.snippet.SnippetFormat;
import org.springframework.restdocs.templates.TemplateFormat;
/**
* An encapsulation of the configuration for documentation snippets.
@@ -27,18 +27,18 @@ class SnippetConfiguration {
private final String encoding;
private final SnippetFormat format;
private final TemplateFormat format;
SnippetConfiguration(String encoding, SnippetFormat format) {
SnippetConfiguration(String encoding, TemplateFormat templateFormat) {
this.encoding = encoding;
this.format = format;
this.format = templateFormat;
}
String getEncoding() {
return this.encoding;
}
SnippetFormat getFormat() {
TemplateFormat getTemplateFormat() {
return this.format;
}

View File

@@ -24,8 +24,8 @@ import org.springframework.restdocs.RestDocumentationContext;
import org.springframework.restdocs.curl.CurlDocumentation;
import org.springframework.restdocs.http.HttpDocumentation;
import org.springframework.restdocs.snippet.Snippet;
import org.springframework.restdocs.snippet.SnippetFormat;
import org.springframework.restdocs.snippet.SnippetFormats;
import org.springframework.restdocs.templates.TemplateFormat;
import org.springframework.restdocs.templates.TemplateFormats;
/**
* A configurer that can be used to configure the generated documentation snippets.
@@ -55,14 +55,14 @@ public abstract class SnippetConfigurer<P, T> extends AbstractNestedConfigurer<P
/**
* The default format for documentation snippets.
*
* @see #withFormat(SnippetFormat)
* @see #withTemplateFormat(TemplateFormat)
*/
public static final SnippetFormat DEFAULT_SNIPPET_FORMAT = SnippetFormats
public static final TemplateFormat DEFAULT_TEMPLATE_FORMAT = TemplateFormats
.asciidoctor();
private String snippetEncoding = DEFAULT_SNIPPET_ENCODING;
private SnippetFormat snippetFormat = DEFAULT_SNIPPET_FORMAT;
private TemplateFormat templateFormat = DEFAULT_TEMPLATE_FORMAT;
/**
* Creates a new {@code SnippetConfigurer} with the given {@code parent}.
@@ -76,7 +76,7 @@ public abstract class SnippetConfigurer<P, T> extends AbstractNestedConfigurer<P
@Override
public void apply(Map<String, Object> configuration, RestDocumentationContext context) {
configuration.put(SnippetConfiguration.class.getName(), new SnippetConfiguration(
this.snippetEncoding, this.snippetFormat));
this.snippetEncoding, this.templateFormat));
configuration.put(ATTRIBUTE_DEFAULT_SNIPPETS, this.defaultSnippets);
}
@@ -106,14 +106,14 @@ public abstract class SnippetConfigurer<P, T> extends AbstractNestedConfigurer<P
}
/**
* Configures the format of the documentation snippets.
* Configures the format of the documentation snippet templates.
*
* @param format the snippet format
* @param format the snippet template format
* @return {@code this}
*/
@SuppressWarnings("unchecked")
public T withFormat(SnippetFormat format) {
this.snippetFormat = format;
public T withTemplateFormat(TemplateFormat format) {
this.templateFormat = format;
return (T) this;
}

View File

@@ -1,74 +0,0 @@
/*
* Copyright 2014-2016 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.restdocs.snippet;
/**
* An enumeration of the built-in snippet formats.
*
* @author Andy Wilkinson
*/
public abstract class SnippetFormats {
private static final SnippetFormat ASCIIDOCTOR = new AsciidoctorSnippetFormat();
private static final SnippetFormat MARKDOWN = new MarkdownSnippetFormat();
private SnippetFormats() {
}
/**
* Returns the Asciidoctor snippet format.
*
* @return the snippet format
*/
public static SnippetFormat asciidoctor() {
return ASCIIDOCTOR;
}
/**
* Returns the Markdown snippet format.
*
* @return the snippet format
*/
public static SnippetFormat markdown() {
return MARKDOWN;
}
private static final class AsciidoctorSnippetFormat implements SnippetFormat {
private static final String FILE_EXTENSION = "adoc";
@Override
public String getFileExtension() {
return FILE_EXTENSION;
}
}
private static final class MarkdownSnippetFormat implements SnippetFormat {
private static final String FILE_EXTENSION = "md";
@Override
public String getFileExtension() {
return FILE_EXTENSION;
}
}
}

View File

@@ -23,6 +23,8 @@ import java.io.OutputStreamWriter;
import java.io.Writer;
import org.springframework.restdocs.RestDocumentationContext;
import org.springframework.restdocs.templates.TemplateFormat;
import org.springframework.restdocs.templates.TemplateFormats;
import org.springframework.util.PropertyPlaceholderHelper;
import org.springframework.util.PropertyPlaceholderHelper.PlaceholderResolver;
@@ -40,7 +42,7 @@ public final class StandardWriterResolver implements WriterResolver {
private String encoding = "UTF-8";
private SnippetFormat snippetFormat;
private TemplateFormat templateFormat;
/**
* Creates a new {@code StandardWriterResolver} that will use the given
@@ -50,29 +52,29 @@ public final class StandardWriterResolver implements WriterResolver {
*
* @param placeholderResolver the placeholder resolver
* @deprecated since 1.1.0 in favor of
* {@link #StandardWriterResolver(PropertyPlaceholderHelper.PlaceholderResolver, String, SnippetFormat)}
* {@link #StandardWriterResolver(PropertyPlaceholderHelper.PlaceholderResolver, String, TemplateFormat)}
*/
@Deprecated
public StandardWriterResolver(PlaceholderResolver placeholderResolver) {
this(placeholderResolver, "UTF-8", SnippetFormats.asciidoctor());
this(placeholderResolver, "UTF-8", TemplateFormats.asciidoctor());
}
/**
* Creates a new {@code StandardWriterResolver} that will use the given
* {@code placeholderResolver} to resolve any placeholders in the
* {@code operationName}. Writers will use the given {@code encoding} and, when
* writing to a file, will use a filename appropriate for content in the given
* {@code snippetFormat}.
* writing to a file, will use a filename appropriate for content generated from
* templates in the given {@code templateFormat}.
*
* @param placeholderResolver the placeholder resolver
* @param encoding the encoding
* @param snippetFormat the snippet format
* @param templateFormat the snippet format
*/
public StandardWriterResolver(PlaceholderResolver placeholderResolver,
String encoding, SnippetFormat snippetFormat) {
String encoding, TemplateFormat templateFormat) {
this.placeholderResolver = placeholderResolver;
this.encoding = encoding;
this.snippetFormat = snippetFormat;
this.templateFormat = templateFormat;
}
@Override
@@ -80,7 +82,7 @@ public final class StandardWriterResolver implements WriterResolver {
RestDocumentationContext context) throws IOException {
File outputFile = resolveFile(this.propertyPlaceholderHelper.replacePlaceholders(
operationName, this.placeholderResolver), snippetName + "."
+ this.snippetFormat.getFileExtension(), context);
+ this.templateFormat.getFileExtension(), context);
if (outputFile != null) {
createDirectoriesIfNecessary(outputFile);

View File

@@ -18,8 +18,6 @@ package org.springframework.restdocs.templates;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.restdocs.snippet.SnippetFormat;
import org.springframework.restdocs.snippet.SnippetFormats;
/**
* Standard implementation of {@link TemplateResourceResolver}.
@@ -33,28 +31,28 @@ import org.springframework.restdocs.snippet.SnippetFormats;
*/
public class StandardTemplateResourceResolver implements TemplateResourceResolver {
private final SnippetFormat snippetFormat;
private final TemplateFormat templateFormat;
/**
* Creates a new {@code StandardTemplateResourceResolver} that will produce default
* template resources formatted with Asciidoctor.
*
* @deprecated since 1.1.0 in favour of
* {@link #StandardTemplateResourceResolver(SnippetFormat)}
* {@link #StandardTemplateResourceResolver(TemplateFormat)}
*/
@Deprecated
public StandardTemplateResourceResolver() {
this(SnippetFormats.asciidoctor());
this(TemplateFormats.asciidoctor());
}
/**
* Creates a new {@code StandardTemplateResourceResolver} that will produce default
* template resources formatted with the given {@code snippetFormat}.
* template resources formatted with the given {@code templateFormat}.
*
* @param snippetFormat the format for the default snippet templates
* @param templateFormat the format for the default snippet templates
*/
public StandardTemplateResourceResolver(SnippetFormat snippetFormat) {
this.snippetFormat = snippetFormat;
public StandardTemplateResourceResolver(TemplateFormat templateFormat) {
this.templateFormat = templateFormat;
}
@Override
@@ -64,8 +62,7 @@ public class StandardTemplateResourceResolver implements TemplateResourceResolve
if (!classPathResource.exists()) {
classPathResource = new ClassPathResource(
"org/springframework/restdocs/templates/"
+ this.snippetFormat.getFileExtension() + "/" + name
+ ".snippet");
+ this.templateFormat.getId() + "/" + name + ".snippet");
if (!classPathResource.exists()) {
throw new IllegalStateException("Template named '" + name
+ "' could not be resolved");

View File

@@ -14,18 +14,26 @@
* limitations under the License.
*/
package org.springframework.restdocs.snippet;
package org.springframework.restdocs.templates;
/**
* A {@link SnippetFormat} provides information about a particular snippet format, such as
* Asciidoctor or Markdown.
* A {@link TemplateFormat} provides information about a particular template format, such
* as Asciidoctor or Markdown.
*
* @author Andy Wilkinson
*/
public interface SnippetFormat {
public interface TemplateFormat {
/**
* Returns the snippet format's file extension.
* Returns the id of this template format.
*
* @return the id
*/
String getId();
/**
* Returns the file extension to use for files generated from templates in this
* format.
*
* @return the file extension
*/

View File

@@ -0,0 +1,93 @@
/*
* Copyright 2014-2016 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.restdocs.templates;
/**
* An enumeration of the built-in formats for which templates are provuded.
*
* @author Andy Wilkinson
*/
public abstract class TemplateFormats {
private static final TemplateFormat ASCIIDOCTOR = new AsciidoctorTemplateFormat();
private static final TemplateFormat MARKDOWN = new MarkdownTemplateFormat();
private TemplateFormats() {
}
/**
* Returns the Asciidoctor template format with the ID {@code asciidoctor} and the
* file extension {@code adoc}.
*
* @return the template format
*/
public static TemplateFormat asciidoctor() {
return ASCIIDOCTOR;
}
/**
* Returns the Markdown template format with the ID {@code markdown} and the file
* extension {@code md}.
*
* @return the template format
*/
public static TemplateFormat markdown() {
return MARKDOWN;
}
private abstract static class AbstractTemplateFormat implements TemplateFormat {
private final String name;
private final String fileExtension;
private AbstractTemplateFormat(String name, String fileExtension) {
this.name = name;
this.fileExtension = fileExtension;
}
@Override
public String getId() {
return this.name;
}
@Override
public String getFileExtension() {
return this.fileExtension;
}
}
private static final class AsciidoctorTemplateFormat extends AbstractTemplateFormat {
private AsciidoctorTemplateFormat() {
super("asciidoctor", "adoc");
}
}
private static final class MarkdownTemplateFormat extends AbstractTemplateFormat {
private MarkdownTemplateFormat() {
super("markdown", "md");
}
}
}