Skip to content
Permalink
Browse files
minor refactor: formatting
  • Loading branch information
paulk-asert committed Apr 15, 2022
1 parent 66b79c8 commit dcdb5fdaeed44e5f42a3f673317cbf596fce0a9b
Showing 1 changed file with 34 additions and 37 deletions.
@@ -85,7 +85,7 @@
*
* print template.make(binding)
* </pre>
*
* <p>
* This example uses a mix of the JSP style and GString style
* placeholders but you can typically use just one style if you wish. Running
* this example will produce this output:
@@ -171,7 +171,7 @@ public StreamingTemplateEngine(ClassLoader parentLoader) {

/**
* Creates a template instance using the template source from the provided Reader.
*
* <p>
* The template can be applied repeatedly on different bindings to produce custom output.
*
* <strong>Technical detail</strong><br />
@@ -186,7 +186,7 @@ public StreamingTemplateEngine(ClassLoader parentLoader) {
* //code generated by parsing the template data
* }
* </pre>
*
* <p>
* We then curry in the parentClass and stringSectionList arguments so that the StreamingTemplate
* instance returned from 'createTemplate' internally contains a template closure on the form:
*
@@ -195,7 +195,7 @@ public StreamingTemplateEngine(ClassLoader parentLoader) {
* //code generated by parsing the template data
* }
* </pre>
*
* <p>
* Calling {@code template.make(binding)}, curries in the 'binding' argument:
*
* <pre>
@@ -204,7 +204,7 @@ public StreamingTemplateEngine(ClassLoader parentLoader) {
* return (Writable) template;
* }
* </pre>
*
* <p>
* This only leaves the 'out' argument unbound. The only method on the {@link groovy.lang.Writable writable} interface is
* {@link groovy.lang.Writable#writeTo writeTo(Writer out)} so groovy rules about casting a closure to a one-method-interface
* apply and the above works. I.e. we return the now one argument closure as the Writable
@@ -219,7 +219,6 @@ public Template createTemplate(final Reader reader) throws CompilationFailedExce

/**
* The class used to implement the Template interface for the StreamingTemplateEngine
*
*/
private static class StreamingTemplate implements Template {
/**
@@ -308,7 +307,7 @@ public String toString() {
* <pre>
* Alice why is a $bird like a writing desk
* </pre>
*
* <p>
* Would produce a string section "Alice why is a " followed by
* a dollar identifier expression followed by another string
* section " like a writing desk".
@@ -333,11 +332,11 @@ public String toString() {
/**
* Called to handle the ending of a string section.
*
* @param sections The list of string sections. The current section gets added to this section.
* @param currentSection The current string section.
* @param sections The list of string sections. The current section gets added to this section.
* @param currentSection The current string section.
* @param templateExpressions Template expressions
* @param lastSourcePosition The last read position in the source template stream.
* @param targetPosition The last written to position in the target script stream.
* @param lastSourcePosition The last read position in the source template stream.
* @param targetPosition The last written to position in the target script stream.
*/
private void finishStringSection(List<StringSection> sections, StringSection currentSection,
StringBuilder templateExpressions,
@@ -387,13 +386,13 @@ private int getLinesInSource() throws IOException {
}

private String getErrorContext(int actualLine) throws IOException {
int minLine = Math.max(0, actualLine -1);
int minLine = Math.max(0, actualLine - 1);
int maxLine = Math.min(getLinesInSource(), actualLine + 1);

LineNumberReader r = new LineNumberReader(new StringReader(templateSource.toString()));
int lineNr;
StringBuilder result = new StringBuilder();
while ((lineNr = r.getLineNumber()+1) <= maxLine) {
while ((lineNr = r.getLineNumber() + 1) <= maxLine) {
String line = r.readLine();
if (lineNr < minLine) continue;

@@ -431,7 +430,8 @@ private String padLeft(String s, int len) {
* <pre>
* { out -> out << "${"test"} of expr and "; test = 1 ; out << "${test} script."}.asWritable()
* </pre>
* @param source A reader into the template source data
*
* @param source A reader into the template source data
* @param parentLoader A class loader we use
* @throws CompilationFailedException
* @throws ClassNotFoundException
@@ -561,8 +561,7 @@ private void handleEscaping(final Reader source,
int d = read(source, sourcePosition, lookAhead);
c = read(source, sourcePosition, lookAhead);
clear(lookAhead);
if ((d == '$' && c == '{') ||
(d == '<' && c == '%')) {
if ((d == '$' && c == '{') || (d == '<' && c == '%')) {
source.reset();
currentSection.data.append('\\');
return;
@@ -627,34 +626,32 @@ private Closure createTemplateClosure(List<StringSection> sections, final ClassL
* <pre>
* Alice why is a $bird like a writing desk
* </pre>
*
* <p>
* which would produce the following template data:
*
* <pre>
* out << "Alice why is a ";
* out << bird;
* out << " like a writing desk";
* </pre>
*
* <p>
* This method is given the 'b' in 'bird' in argument c, checks if it is a valid
* java identifier start (we assume groovy did not mangle the java
* identifier rules). If so it proceeds to parse characters from the input
* until it encounters a non-java-identifier character. At that point
*
* @param c The first letter of the potential identifier, 'b' in the above example
* @param reader The reader reading from the template source
* @param target The target groovy script source we write to
* @param c The first letter of the potential identifier, 'b' in the above example
* @param reader The reader reading from the template source
* @param target The target groovy script source we write to
* @param sourcePosition The reader position in the source stream
* @param targetPosition The writer position in the target stream
* @return true if a valid dollar preceded identifier was found, false otherwise. More
* specifically, returns true if the first character after the dollar sign is
* a valid java identifier. Note that the dollar curly syntax is handled by
* another method.
*
* specifically, returns true if the first character after the dollar sign is a valid
* java identifier. Note that the dollar curly syntax is handled by another method.
* @throws IOException
* @throws FinishedReadingException If we encountered the end of the source stream.
*/
private int parseDollarIdentifier(int c ,
private int parseDollarIdentifier(int c,
final Reader reader,
final StringBuilder target,
final Position sourcePosition,
@@ -682,22 +679,22 @@ private int parseDollarIdentifier(int c ,
* <pre>
* Alice why is a ${bird} like a writing desk
* </pre>
*
* <p>
* which would produce the following template data:
*
* <pre>
* out << "Alice why is a ";
* out << """${bird}""";
* out << " like a writing desk";
* </pre>
*
* <p>
* This method is given the 'b' in 'bird' in argument c, checks if it is a valid
* java identifier start (we assume groovy did not mangle the java
* identifier rules). If so it proceeds to parse characters from the input
* until it encounters a non-java-identifier character. At that point
*
* @param reader The reader reading from the template source
* @param target The target groovy script source we write to
* @param reader The reader reading from the template source
* @param target The target groovy script source we write to
* @param sourcePosition The reader position in the source stream
* @param targetPosition The writer position in the target stream
* @throws IOException
@@ -723,10 +720,10 @@ private void parseDollarCurlyIdentifier(final Reader reader,
* append ';' then write the section as a statement
*/
private void parseSection(final int pendingC,
final Reader reader,
final StringBuilder target,
final Position sourcePosition,
final Position targetPosition) throws IOException, FinishedReadingException {
final Reader reader,
final StringBuilder target,
final Position sourcePosition,
final Position targetPosition) throws IOException, FinishedReadingException {
//the below is a quirk, we do this so that every non-string-section is prefixed by
//the same number of characters (the others have "out<<\"\"\"${"), this allows us to
//figure out the exception row and column later on
@@ -754,9 +751,9 @@ private void readAndAppend(Reader reader, StringBuilder target, Position sourceP
* Parse a &lt;%= .... %&gt; expression
*/
private void parseExpression(final Reader reader,
final StringBuilder target,
final Position sourcePosition,
final Position targetPosition) throws IOException, FinishedReadingException {
final StringBuilder target,
final Position sourcePosition,
final Position targetPosition) throws IOException, FinishedReadingException {
append(target, targetPosition, "out<<\"\"\"${");

readAndAppend(reader, target, sourcePosition, targetPosition);

0 comments on commit dcdb5fd

Please sign in to comment.