001: package org.drools.eclipse.wizard.rule;
002:
003: import java.io.BufferedReader;
004: import java.io.ByteArrayInputStream;
005: import java.io.IOException;
006: import java.io.InputStream;
007: import java.io.InputStreamReader;
008: import java.io.UnsupportedEncodingException;
009: import java.text.DateFormat;
010: import java.util.Date;
011: import java.util.regex.Pattern;
012:
013: /**
014: * This will generate DRL bits and bobs based on various templates.
015: * For use by the wizards only.
016: * TODO: move this to string template (as it is being used elsewhere in drools)
017: * @author Michael Neale
018: */
019: public class DRLGenerator {
020:
021: private static final Pattern packageDec = Pattern
022: .compile("\\$package\\$");
023: private static final Pattern dateDec = Pattern
024: .compile("\\$date\\$");
025: private static final Pattern functionsDec = Pattern
026: .compile("\\$functions\\$");
027: private static final Pattern expanderDec = Pattern
028: .compile("\\$expander\\$");
029:
030: public InputStream generateRule(String packageName,
031: InputStream template) throws IOException {
032:
033: String temp = readTemplate(template);
034: temp = doHeader(packageName, temp);
035:
036: return toStream(temp);
037:
038: }
039:
040: public InputStream generatePackage(String packageName,
041: boolean functions, boolean expander, InputStream template)
042: throws IOException {
043: String temp = readTemplate(template);
044: temp = doHeader(packageName, temp);
045: if (functions) {
046: temp = functionsDec
047: .matcher(temp)
048: .replaceFirst(
049: "function myFunction( ... ) "
050: + System
051: .getProperty("line.separator")
052: + " #function content (can have multiple functions) "
053: + System
054: .getProperty("line.separator")
055: + "end"
056: + System
057: .getProperty("line.separator"));
058: } else {
059: temp = functionsDec.matcher(temp).replaceFirst("");
060: }
061: if (expander) {
062: temp = expanderDec
063: .matcher(temp)
064: .replaceFirst(
065: "expander customLanguage.dsl"
066: + System
067: .getProperty("line.separator")
068: + "");
069: } else {
070: temp = expanderDec.matcher(temp).replaceFirst("");
071: }
072: return toStream(temp);
073:
074: }
075:
076: private ByteArrayInputStream toStream(String temp)
077: throws UnsupportedEncodingException {
078: ByteArrayInputStream stream = new ByteArrayInputStream(temp
079: .getBytes("UTF-8"));
080: return stream;
081: }
082:
083: private String doHeader(String packageName, String temp) {
084: temp = packageDec.matcher(temp).replaceFirst(
085: "package " + packageName
086: + System.getProperty("line.separator"));
087: temp = dateDec.matcher(temp).replaceFirst(
088: DateFormat.getDateInstance().format(new Date()));
089: return temp;
090: }
091:
092: private String readTemplate(InputStream template)
093: throws IOException {
094: BufferedReader reader = new BufferedReader(
095: new InputStreamReader(template));
096: String line = null;
097: StringBuffer buf = new StringBuffer();
098: while ((line = reader.readLine()) != null) {
099: buf.append(line + System.getProperty("line.separator"));
100: }
101: String temp = buf.toString();
102: return temp;
103: }
104:
105: }
|