Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactoring #178

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 32 additions & 9 deletions pom.xml
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">

<modelVersion>4.0.0</modelVersion>
<artifactId>rocker</artifactId>
Expand All @@ -10,23 +12,23 @@
<artifactId>maven-parent</artifactId>
<version>2.5.0</version>
</parent>

<scm>
<url>https://github.com/fizzed/rocker</url>
<connection>scm:git:[email protected]:fizzed/rocker.git</connection>
<tag>HEAD</tag>
</scm>
<tag>HEAD</tag>
</scm>

<properties>
<main.java.package />
<slf4j.version>1.7.7</slf4j.version>
<antlr.version>4.5.3</antlr.version>
<java.version>1.8</java.version>
<license.skip>true</license.skip>
<!-- netbeans specific -->
<netbeans.compile.on.save>false</netbeans.compile.on.save>
<!-- workaround travis ci maven version requirement -->
<maven.enforce.version>3.2.5</maven.enforce.version>
<!-- JaCoCo Version -->
<jacoco.version>0.8.7</jacoco.version>
</properties>

<modules>
Expand All @@ -38,7 +40,7 @@
<module>rocker-test-reload</module>
<module>rocker-bom</module>
</modules>

<build>
<plugins>
<plugin>
Expand All @@ -63,9 +65,30 @@
</execution>
</executions>
</plugin>
<!-- JaCoCo plugin for code coverage -->
<plugin>
<groupId>org.jacoco</groupId>
<artifactId>jacoco-maven-plugin</artifactId>
<version>${jacoco.version}</version>
<executions>
<execution>
<goals>
<goal>prepare-agent</goal>
</goals>
</execution>
<!-- attached to Maven test phase -->
<execution>
<id>report</id>
<phase>prepare-package</phase>
<goals>
<goal>report</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>

<dependencyManagement>
<dependencies>
<!-- compile -->
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -430,7 +430,7 @@ else if (this.plainTextStrategy == PlainTextStrategy.STATIC_BYTE_ARRAYS_VIA_UNLO
tab(w, indent).append("public Template(").append(model.getName()).append(" model) {").append(CRLF);

tab(w, indent+1).append("super(model);").append(CRLF);
tab(w, indent+1).append("__internal.setCharset(\"").append(model.getOptions().getTargetCharset()).append("\");").append(CRLF);
tab(w, indent+1).append("__internal.setCharsetName(\"").append(model.getOptions().getTargetCharset()).append("\");").append(CRLF);
tab(w, indent+1).append("__internal.setContentType(getContentType());").append(CRLF);
tab(w, indent+1).append("__internal.setTemplateName(getTemplateName());").append(CRLF);
tab(w, indent+1).append("__internal.setTemplatePackageName(getTemplatePackageName());").append(CRLF);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,16 +313,24 @@ public void discardLogicWhitespace(TemplateModel model) {
}
}

// do we chop this line?
if ((prevPlain != null && prevUnitTrailingWhitespaceLengthToStartOfLine >= 0) &&
(lastUnit || (nextPlain != null && nextUnitLeadingWhitespaceLengthToEndOfLine >= 0))) {

boolean hasPreviousPlain = prevPlain != null;
boolean hasNextPlain = nextPlain != null;
boolean prevUnitHasTrailingWhitespace = prevUnitTrailingWhitespaceLengthToStartOfLine >= 0;
boolean nextUnitHasLeadingWhitespace = nextUnitLeadingWhitespaceLengthToEndOfLine >= 0;

boolean shouldChompPrevious = hasPreviousPlain && prevUnitHasTrailingWhitespace;
boolean shouldChompNext = lastUnit || (hasNextPlain && nextUnitHasLeadingWhitespace);

if (shouldChompPrevious && shouldChompNext) {
prevPlain.chompTrailingLength(prevUnitTrailingWhitespaceLengthToStartOfLine);
if (nextPlain != null && nextUnitLeadingWhitespaceLengthToEndOfLine > 0) {

boolean shouldChompLeadingNewline = hasNextPlain && nextUnitLeadingWhitespaceLengthToEndOfLine > 0;
if (shouldChompLeadingNewline) {
// we actually want to chop the newline char as well!
nextPlain.chompLeadingLength(nextUnitLeadingWhitespaceLengthToEndOfLine);
}
}

}

// remove any empty plain text units (since many above may have been chopped down to nothing)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -841,4 +841,11 @@ public void testIfElseIfStatement() throws Exception {
// Expected
}
}

@Test(expected = ParserException.class)
public void testMalformedTemplateParsing() throws Exception {
File f = findTemplate("malformed_template.rocker.html");
createParser().parse(f);
}

}
2 changes: 1 addition & 1 deletion rocker-compiler/src/test/resources/generated/Args.java.txt
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public class Args extends com.fizzed.rocker.runtime.DefaultRockerModel {

public Template(Args model) {
super(model);
__internal.setCharset("UTF-8");
__internal.setCharsetName("UTF-8");
__internal.setContentType(CONTENT_TYPE);
__internal.setTemplateName(TEMPLATE_NAME);
__internal.setTemplatePackageName(TEMPLATE_PACKAGE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ public interface RockerOutput<T extends RockerOutput> {

public ContentType getContentType();

public Charset getCharset();
public Charset getCharsetName();

/**
* Writes a String to the output. Implementations are responsible for handling
Expand Down
18 changes: 18 additions & 0 deletions rocker-runtime/src/main/java/com/fizzed/rocker/RockerTemplate.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
*/
package com.fizzed.rocker;

import com.fizzed.rocker.runtime.Raw;
import java.io.IOException;
public abstract class RockerTemplate {

/**
Expand Down Expand Up @@ -45,5 +47,21 @@ public RockerTemplate(RockerModel model) {
public String toString() {
throw new UnsupportedOperationException("toString() not permitted on a RockerTemplate. Use render() method.");
}

public Raw raw(Object obj) throws IOException {
if (obj == null) {
throw new NullPointerException("Value was null");
}

return Raw.of(obj.toString());
}

public Raw raw(String s) throws IOException {
if (s == null) {
throw new NullPointerException("Value was null");
}

return Raw.of(s);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ public ContentType getContentType() {
}

@Override
public Charset getCharset() {
public Charset getCharsetName() {
return this.charset;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package com.fizzed.rocker.runtime;

import java.io.IOException;
import com.fizzed.rocker.RenderingException;

public class DefaultRenderingStrategy implements RenderingStrategy {
@Override
public void render(DefaultRockerTemplate template) throws RenderingException, IOException {
template.__doRender(); // Calls the existing rendering logic
}
}
Loading