Skip to content
Snippets Groups Projects
Commit 519b10c7 authored by Christian Wulf's avatar Christian Wulf
Browse files

fixed read-only, classes are renamed now

parent f75d4145
No related branches found
No related tags found
No related merge requests found
......@@ -18,6 +18,10 @@
<logger name="de.chw.css.script.ProductLineGenerator" level="off" additivity="false">
<appender-ref ref="Console" />
<!-- <appender-ref ref="File" /> -->
</logger>
<logger name="de.chw.css.script.ProductLineTemplateEngine" level="debug" additivity="false">
<appender-ref ref="Console" />
<!-- <appender-ref ref="File" /> -->
</logger>
<logger name="de.chw.css.script" level="off" additivity="false">
<appender-ref ref="Console" />
......
/*
TEMPLATE
PRODUCTLINE default
VARIABLE active_bgcolor_from
#2884C4
VARIABLE bgcolor
#ffffff
TEMPLATE END
*/
/* active_bgcolor_from */
#2884C4
.ui-widget-header {
background: /* active_bgcolor_from */;
border-color: /* active_bgcolor_from */;
background: #2884C4;
border-color: #2884C4;
padding-left: 10px;
text-align: center;
}
.ui-widget-content {
background: /* bgcolor */;
background: #ffffff;
}
\ No newline at end of file
package de.chw.css.script;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.util.Map;
import java.util.regex.Matcher;
......@@ -10,6 +9,8 @@ import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.chw.util.FileHelper;
public class ProductLineGenerator {
private final static Logger LOG = LogManager.getLogger(ProductLineGenerator.class);
......@@ -31,7 +32,8 @@ public class ProductLineGenerator {
public void replaceVariables(String pattern) {
for (String key : variables.keySet()) {
String regex = pattern.replace("var", key);
// we do not use a simple String.replaceAll, because we want to log the single replacements
// we do not use a simple String.replaceAll, because we want to log
// the single replacements
Matcher matcher = Pattern.compile(regex).matcher(content);
StringBuffer sb = new StringBuffer();
......@@ -48,13 +50,9 @@ public class ProductLineGenerator {
public boolean generateFile(String filename) throws IOException {
File file = new File(filename);
file.delete();
FileWriter fw = new FileWriter(file);
try {
fw.write(content);
} finally {
fw.close();
}
boolean deleted = file.delete();
LOG.debug("Deleted '" + filename + "' => " + deleted);
FileHelper.saveTextFile(file, content);
return file.setReadOnly();
}
......
......@@ -10,6 +10,8 @@ import java.util.regex.Pattern;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import de.chw.util.FileHelper;
public class ProductLineTemplateEngine {
private final static Logger LOG = LogManager.getLogger(ProductLineTemplateEngine.class);
......@@ -23,6 +25,7 @@ public class ProductLineTemplateEngine {
}
private String generateFile(String templateFilename, String outputFilename) throws IOException {
LOG.debug("Generating '" + outputFilename + "' from '" + templateFilename + "'");
ProductLineTemplateReader reader = new ProductLineTemplateReader(templateFilename, chosenProduct);
ProductLineGenerator generator = new ProductLineGenerator(reader.getContent(), reader.getVariables());
generator.replaceVariables("/\\*\\s*var\\s*\\*/");
......@@ -34,21 +37,26 @@ public class ProductLineTemplateEngine {
return file.getName().matches(".*" + BINARY_FILENAME_PATTERN);
}
public void generateBinaryFile(File file) throws IOException {
public String generateBinaryFile(File file) throws IOException {
final String defaultTemplateFilename = file.getAbsolutePath();
Matcher matcher = Pattern.compile(BINARY_FILENAME_PATTERN).matcher(defaultTemplateFilename);
String templateFilename = matcher.replaceFirst("_template_" + chosenProduct + ".$1");
String templateFilename = matcher.replaceFirst("_template_" + chosenProduct + "\\.$1");
String outputFilename = matcher.replaceFirst(".$1");
File outputFile = new File(outputFilename);
LOG.debug("Copying '" + templateFilename + "'\n\tto '" + outputFile.getAbsolutePath() + "'...");
copyFile(templateFilename, outputFile);
LOG.debug("Copying '" + templateFilename + "'\n\tto '" + outputFilename + "'...");
copyFile(templateFilename, outputFilename);
renameClassInGroovyFile(file, outputFile);
return outputFilename;
}
private void copyFile(String templateFilename, String outputFilename) throws IOException {
private void copyFile(String templateFilename, File outputFile) throws IOException {
outputFile.delete();
FileInputStream inputStream = new FileInputStream(templateFilename);
try {
FileOutputStream outputStream = new FileOutputStream(outputFilename);
FileOutputStream outputStream = new FileOutputStream(outputFile);
try {
byte[] buf = new byte[1024];
int len;
......@@ -65,21 +73,39 @@ public class ProductLineTemplateEngine {
public String generateCss(File file) throws IOException {
String templateFilename = file.getAbsolutePath();
String outputFilename = templateFilename.replace("_template.css", ".css");
String outputFilename = templateFilename.replaceFirst("_template\\.css\\z", ".css");
return generateFile(templateFilename, outputFilename);
}
public String generateGroovy(File file) throws IOException {
String templateFilename = file.getAbsolutePath();
String outputFilename = templateFilename.replace("_template.groovy", ".groovy");
String outputFilename = templateFilename.replaceFirst("_template\\.groovy\\z", ".groovy");
return generateFile(templateFilename, outputFilename);
}
private void renameClassInGroovyFile(final File groovyTemplateFile, final File generatedFile) throws IOException {
if (!generatedFile.getName().endsWith(".groovy")) return;
String text = FileHelper.readTextFile(groovyTemplateFile.getAbsolutePath());
final String classNameToSearchFor = groovyTemplateFile.getName().replaceFirst("\\.groovy\\z", "");
// classNameToSearchFor: e.g., BootStrap_template_default
final String newClassName = generatedFile.getName().replaceFirst("\\.groovy\\z", "");
// classNameToSearchFor: e.g., BootStrap
text = text.replaceFirst("class\\s+" + Pattern.quote(classNameToSearchFor), "class " + newClassName);
LOG.debug("classNameToSearchFor: "+classNameToSearchFor);
LOG.debug("newClassName: "+classNameToSearchFor);
LOG.debug("text: "+text);
generatedFile.delete();
FileHelper.saveTextFile(generatedFile, text);
generatedFile.setReadOnly();
}
public String generateGsp(File file) throws IOException {
String templateFilename = file.getAbsolutePath();
String outputFilename = templateFilename.replace("_template.gsp", ".gsp");
String outputFilename = templateFilename.replaceFirst("_template\\.gsp\\z", ".gsp");
return generateFile(templateFilename, outputFilename);
}
......
......@@ -8,7 +8,7 @@ import java.io.IOException;
import org.antlr.runtime.RecognitionException;
import org.junit.Test;
import de.chw.util.TestHelper;
import de.chw.util.FileHelper;
public class ProductLineGeneratorTest {
......@@ -22,7 +22,7 @@ public class ProductLineGeneratorTest {
try {
generator.generateFile(generatedFilename);
String readContent = TestHelper.readTextFile(filenamePrefix + "_expected" + "." + ext);
String readContent = FileHelper.readTextFile(filenamePrefix + "_expected" + "." + ext);
assertEquals(readContent, generator.getContent());
} finally {
// clean up
......
......@@ -9,6 +9,8 @@ import java.io.IOException;
import org.junit.Test;
import de.chw.util.FileHelper;
public class ProductLineTemplateEngineTest {
private void testGenerateBinaryFile(String filenameToSearchFor, String expectedGeneratedFilename) throws IOException {
......@@ -31,6 +33,7 @@ public class ProductLineTemplateEngineTest {
ProductLineTemplateEngine engine = new ProductLineTemplateEngine("fm");
assertTrue(engine.isBinaryFile(new File("resource/logo_template_default.png")));
assertFalse(engine.isBinaryFile(new File("resource/DataSource.groovy")));
assertFalse(engine.isBinaryFile(new File("resource/dashboard_template.groovy")));
}
@Test
......@@ -64,6 +67,22 @@ public class ProductLineTemplateEngineTest {
generatedFile.delete();
}
@Test
public void testRenameGroovyWithClass() throws IOException {
File file = new File("resource/BootStrap_template_default.groovy");
ProductLineTemplateEngine engine = new ProductLineTemplateEngine("fm");
engine.generateBinaryFile(file);
File generatedFile = new File("resource/BootStrap.groovy");
String generatedText = FileHelper.readTextFile(generatedFile.getAbsolutePath());
assertTrue(generatedFile.exists());
assertTrue(generatedText.contains("class BootStrap"));
// clean up
generatedFile.delete();
}
@Test
public void testGenerateGsp() throws IOException {
fail("not implemented");
......
......@@ -8,7 +8,7 @@ import java.util.Map;
import org.junit.Test;
import de.chw.util.TestHelper;
import de.chw.util.FileHelper;
public class ProductLineTemplateReaderTest {
......@@ -21,7 +21,7 @@ public class ProductLineTemplateReaderTest {
String chosenProduct = "fm";
ProductLineTemplateReader reader = new ProductLineTemplateReader(filename, chosenProduct);
String expected = TestHelper.readTextFile("resource/" + filenamePrefix + "_content_expected" + "." + ext);
String expected = FileHelper.readTextFile("resource/" + filenamePrefix + "_content_expected" + "." + ext);
assertEquals(expected, reader.getContent());
}
......
package de.chw.util;
import java.io.FileReader;
import java.io.IOException;
public class TestHelper {
protected TestHelper() {
// utility class
}
public static String readTextFile(String filename) throws IOException {
char[] cbuf = new char[1024];
int len;
StringBuilder builder = new StringBuilder();
FileReader reader = new FileReader(filename);
try {
while ((len = reader.read(cbuf)) != -1) {
builder.append(cbuf, 0, len);
}
} finally {
reader.close();
}
return builder.toString();
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment