Skip to content

Commit

Permalink
mulesoft-labs#246 : Types order in final raml
Browse files Browse the repository at this point in the history
  • Loading branch information
barnabef committed May 15, 2017
1 parent f3ccd3b commit 6901db4
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,13 @@ private void writeApi(RamlApi api) throws IOException {
writeBaseUri(api.getBaseUri());
writeDefaultMediaType(api.getDefaultMediaType());
writeSupportedAnnotations(api.getSupportedAnnotation());

writer.deferAppends();
for (RamlResource resource : api.getResources()) {
writeResource(resource);
}

writer.stopDeferAppends();
writeTypes();
writer.flushDeferredContent();
}

private void writeSupportedAnnotations(List<RamlSupportedAnnotation> supportedAnnotation) throws IOException {
Expand Down
27 changes: 22 additions & 5 deletions utilities/src/main/java/org/raml/utilities/IndentedAppendable.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,11 +32,15 @@ public class IndentedAppendable {

private final String indent;
private final Appendable appendable;
private StringBuilder deferredContent = new StringBuilder();
private Appendable currentAppendable;
// private boolean isDeferred = false;
private String currentIndent = "";

private IndentedAppendable(String indent, Appendable appendable) {
this.indent = indent;
this.appendable = appendable;
currentAppendable = appendable;
}

public static IndentedAppendable forNoSpaces(int noSpaces, Appendable appendable) {
Expand All @@ -46,6 +50,19 @@ public static IndentedAppendable forNoSpaces(int noSpaces, Appendable appendable
return new IndentedAppendable(Strings.repeat(" ", noSpaces), appendable);
}

public void deferAppends() {
currentAppendable = deferredContent;
}

public void stopDeferAppends() {
currentAppendable = appendable;
}

public void flushDeferredContent() throws IOException {
appendable.append(deferredContent);
deferredContent = new StringBuilder();
}

public void indent() {
currentIndent += indent;
}
Expand All @@ -57,17 +74,17 @@ public void outdent() {
}

public IndentedAppendable withIndent() throws IOException {
this.appendable.append(currentIndent);
this.currentAppendable.append(currentIndent);
return this;
}

public IndentedAppendable appendLine(String content) throws IOException {
this.appendable.append(currentIndent).append(content).append("\n");
this.currentAppendable.append(currentIndent).append(content).append("\n");
return this;
}

public IndentedAppendable appendLine(String tag, String content) throws IOException {
this.appendable.append(currentIndent).append(tag).append(": ").append(content).append("\n");
this.currentAppendable.append(currentIndent).append(tag).append(": ").append(content).append("\n");
return this;
}

Expand All @@ -91,12 +108,12 @@ public IndentedAppendable appendList(String tag, String... content) throws IOExc
}

public IndentedAppendable endOfLine() throws IOException {
this.appendable.append(END_OF_LINE);
this.currentAppendable.append(END_OF_LINE);
return this;
}

public String toString() {
return this.appendable.toString();
return this.currentAppendable.toString();
}

private String quoteIfSpecialCharacter(String value) {
Expand Down

0 comments on commit 6901db4

Please sign in to comment.