diff options
| author | bculkin2442 <bjculkin@mix.wvu.edu> | 2019-07-28 15:29:56 -0400 |
|---|---|---|
| committer | bculkin2442 <bjculkin@mix.wvu.edu> | 2019-07-28 15:29:56 -0400 |
| commit | 7fa2b073420340e968d4afbfbe349fd4cfb30e05 (patch) | |
| tree | 0d9b268e0def39e6a754f3a76387461e117e0da7 /clformat/src/main/java/bjc/utils/ioutils | |
| parent | 2d64cff0fb9eb913bce44dc61d3274ee9de22682 (diff) | |
Implement compilation for CaseDirective
Diffstat (limited to 'clformat/src/main/java/bjc/utils/ioutils')
| -rw-r--r-- | clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java | 4 | ||||
| -rw-r--r-- | clformat/src/main/java/bjc/utils/ioutils/format/directives/CaseDirective.java | 137 |
2 files changed, 104 insertions, 37 deletions
diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java index 3368174..7a45442 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/AestheticDirective.java @@ -8,8 +8,10 @@ import bjc.utils.ioutils.format.*; /** * Implementation of the A directive. * - * @author student + * This is the directive that does general printing of things, and serves the same general purpose + * as the '%s' directive for printf etc. * + * @author Ben Culkin */ public class AestheticDirective implements Directive { diff --git a/clformat/src/main/java/bjc/utils/ioutils/format/directives/CaseDirective.java b/clformat/src/main/java/bjc/utils/ioutils/format/directives/CaseDirective.java index 4bb54b4..efdbb1c 100644 --- a/clformat/src/main/java/bjc/utils/ioutils/format/directives/CaseDirective.java +++ b/clformat/src/main/java/bjc/utils/ioutils/format/directives/CaseDirective.java @@ -7,22 +7,37 @@ import java.util.regex.*; import bjc.utils.ioutils.*; import bjc.utils.ioutils.format.*; +/** + * Implementation of the ( directive. + * + * This directive does case manipulation of the contained text. + * + * @author Ben Culkin + */ public class CaseDirective implements Directive { private static final Pattern wordPattern = Pattern.compile("(\\w+)(\\b*)"); @Override public void format(FormatParameters dirParams) throws IOException { - CLModifiers mods = dirParams.getMods(); + Edict edt = compile(dirParams.toCompileCTX()); + + edt.format(dirParams.toFormatCTX()); + } + + public Edict compile(CompileContext compCTX) { + CLModifiers mods = compCTX.decr.modifiers; List<Decree> condBody = new ArrayList<>(); int nestLevel = 1; - Iterator<Decree> dirIter = dirParams.dirIter; + Iterator<Decree> dirIter = compCTX.directives; while (dirIter.hasNext()) { Decree decr = dirIter.next(); + if (decr.isLiteral) { condBody.add(decr); + continue; } @@ -53,59 +68,109 @@ public class CaseDirective implements Directive { } } - ReportWriter nrw = dirParams.rw.duplicate(new StringWriter()); - - dirParams.fmt.doFormatString(condBody, nrw, dirParams.tParams, false); - - String strang = nrw.toString(); + CaseEdict.Mode mode; if (mods.colonMod && mods.atMod) { - strang = strang.toUpperCase(); + mode = CaseEdict.Mode.UPPERCASE; } else if (mods.colonMod) { - Matcher mat = wordPattern.matcher(strang); + mode = CaseEdict.Mode.WORD_UPPERCASE; + } else if (mods.atMod) { + mode = CaseEdict.Mode.FIRST_UPPERCASE; + } else { + mode = CaseEdict.Mode.LOWERCASE; + } - StringBuffer sb = new StringBuffer(); - while(mat.find()) { - mat.appendReplacement(sb, ""); + return new CaseEdict(condBody, mode, compCTX.formatter); + } +} - String word = mat.group(1); - String ward = word.substring(0, 1).toUpperCase() + word.substring(1); +class CaseEdict implements Edict { + public static enum Mode { + UPPERCASE, + WORD_UPPERCASE, + FIRST_UPPERCASE, + LOWERCASE + } - sb.append(ward); - sb.append(mat.group(2)); - } + private static final Pattern wordPattern = Pattern.compile("(\\w+)(\\b*)"); - mat.appendTail(sb); + private List<Decree> body; - strang = sb.toString(); - } else if (mods.atMod) { - Matcher mat = wordPattern.matcher(strang); + private Mode caseMode; + + private CLFormatter formatter; - StringBuffer sb = new StringBuffer(); - boolean doCap = true; - while(mat.find()) { - mat.appendReplacement(sb, ""); + public CaseEdict(List<Decree> body, Mode caseMode, CLFormatter fmt) { + this.body = body; + + this.caseMode = caseMode; - String word = mat.group(1); - - if (doCap) { - doCap = false; + this.formatter = fmt; + } + + @Override + public void format(FormatContext formCTX) throws IOException { + ReportWriter nrw = formCTX.writer.duplicate(new StringWriter()); + + formatter.doFormatString(body, nrw, formCTX.items, false); + + String strang = nrw.toString(); + + switch (caseMode) { + case UPPERCASE: + strang = strang.toUpperCase(); + break; + case WORD_UPPERCASE: + { + Matcher mat = wordPattern.matcher(strang); + + StringBuffer sb = new StringBuffer(); + while(mat.find()) { + mat.appendReplacement(sb, ""); + + String word = mat.group(1); + String ward = word.substring(0, 1).toUpperCase() + word.substring(1); - word = word.substring(0, 1).toUpperCase() + word.substring(1); + sb.append(ward); + sb.append(mat.group(2)); } - sb.append(word); - sb.append(mat.group(2)); + mat.appendTail(sb); + + strang = sb.toString(); } + break; + case FIRST_UPPERCASE: + { + Matcher mat = wordPattern.matcher(strang); - mat.appendTail(sb); + StringBuffer sb = new StringBuffer(); + boolean doCap = true; + while(mat.find()) { + mat.appendReplacement(sb, ""); - strang = sb.toString(); + String word = mat.group(1); - } else { + if (doCap) { + doCap = false; + + word = word.substring(0, 1).toUpperCase() + word.substring(1); + } + + sb.append(word); + sb.append(mat.group(2)); + } + + mat.appendTail(sb); + + strang = sb.toString(); + } + break; + case LOWERCASE: strang = strang.toLowerCase(); + break; } - dirParams.rw.write(strang); + formCTX.writer.write(strang); } } |
