/src/org/ooc/backend/TabbedWriter.java

http://github.com/nddrylliog/ooc · Java · 74 lines · 55 code · 16 blank · 3 comment · 2 complexity · abaa308b71dac0016c30f74d9094cca3 MD5 · raw file

  1. package org.ooc.backend;
  2. import java.io.IOException;
  3. import java.io.Writer;
  4. public class TabbedWriter implements Appendable {
  5. protected Appendable appendable;
  6. protected int tabLevel;
  7. protected String tab = "\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t\t";
  8. public TabbedWriter(Appendable appendable) {
  9. this.appendable = appendable;
  10. }
  11. public void close() throws IOException {
  12. if(appendable instanceof Writer) {
  13. ((Writer) appendable).close();
  14. } else {
  15. // well, do nothing, probably trying
  16. // to close a StringBuilder, which is
  17. // nonsense.
  18. }
  19. }
  20. public TabbedWriter append(char c) throws IOException {
  21. appendable.append(c);
  22. return this;
  23. }
  24. public TabbedWriter append(String s) throws IOException {
  25. appendable.append(s);
  26. return this;
  27. }
  28. public TabbedWriter writeTabs() throws IOException {
  29. appendable.append(tab, 0, tabLevel);
  30. return this;
  31. }
  32. public TabbedWriter newUntabbedLine() throws IOException {
  33. appendable.append('\n');
  34. return this;
  35. }
  36. public TabbedWriter nl() throws IOException {
  37. return newUntabbedLine().writeTabs();
  38. }
  39. public TabbedWriter newLine() throws IOException {
  40. return newUntabbedLine().writeTabs();
  41. }
  42. public TabbedWriter tab() {
  43. tabLevel++;
  44. return this;
  45. }
  46. public TabbedWriter untab() {
  47. tabLevel--;
  48. return this;
  49. }
  50. public TabbedWriter append(CharSequence csq) throws IOException {
  51. appendable.append(csq);
  52. return this;
  53. }
  54. public TabbedWriter append(CharSequence csq, int start, int end) throws IOException {
  55. appendable.append(csq, start, end);
  56. return this;
  57. }
  58. }