/plugins/Code2HTML/trunk/code2html/generic/GenericPainter.java

# · Java · 203 lines · 141 code · 44 blank · 18 comment · 37 complexity · cd695ad05477217f92f42f2b37aa0a12 MD5 · raw file

  1. /*
  2. * GenericPainter.java
  3. * Copyright (c) 2009 Romain Francois <francoisromain@free.fr>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License
  7. * as published by the Free Software Foundation; either version 2
  8. * of the License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  18. */
  19. package code2html.generic ;
  20. import java.io.IOException;
  21. import java.io.Writer;
  22. import javax.swing.text.Segment;
  23. import org.gjt.sp.jedit.syntax.SyntaxStyle;
  24. import org.gjt.sp.jedit.syntax.Token;
  25. import code2html.line.LinePosition;
  26. import code2html.line.LineTabExpander;
  27. import code2html.line.LineWrapper;
  28. import code2html.services.LinkProvider ;
  29. public abstract class GenericPainter {
  30. protected Style style;
  31. protected GenericGutter gutter;
  32. protected LineTabExpander expander;
  33. protected LineWrapper wrapper;
  34. protected SyntaxStyle[] syntaxStyles;
  35. protected LinePosition position;
  36. protected boolean showGutter;
  37. protected int wrap;
  38. public GenericPainter( ){ }
  39. public GenericPainter(
  40. SyntaxStyle[] syntaxStyles,
  41. Style style,
  42. GenericGutter gutter,
  43. LineTabExpander expander,
  44. LineWrapper wrapper
  45. ) {
  46. this.syntaxStyles = syntaxStyles ;
  47. this.position = new LinePosition( ) ;
  48. this.style = style;
  49. this.gutter = gutter;
  50. this.expander = expander;
  51. this.wrapper = wrapper;
  52. this.showGutter = (gutter != null);
  53. this.wrap = (wrapper == null) ? 0 : wrapper.getWrapSize();
  54. }
  55. public SyntaxStyle[] getSyntaxStyles() {
  56. return this.syntaxStyles;
  57. }
  58. public void setPos(int pos) {
  59. this.position.setPos(pos);
  60. }
  61. public void paintPlainLine(Writer out, int lineNumber, Segment line, Token tokens)
  62. {
  63. try {
  64. if (this.showGutter) {
  65. out.write(this.gutter.format(lineNumber));
  66. }
  67. int pos = this.position.getPos();
  68. String expandedText = this.expander.expand(pos, line.array, line.offset, line.count);
  69. this.position.incPos(expandedText.length());
  70. int[] wraps = null;
  71. if (this.wrapper != null) {
  72. wraps = this.wrapper.wrap(pos, expandedText.length());
  73. if (pos > 0 && (pos % this.wrap) == 0) {
  74. out.write( newLine() );
  75. if (this.showGutter) {
  76. out.write(this.gutter.formatEmpty(lineNumber));
  77. }
  78. }
  79. }
  80. if (wraps == null) {
  81. out.write( format(expandedText) );
  82. } else {
  83. for (int i = 0; i < wraps.length - 1; i++) {
  84. if (i >= 1) {
  85. out.write("\n");
  86. if (this.showGutter) {
  87. out.write(this.gutter.formatEmpty(lineNumber));
  88. }
  89. }
  90. out.write( format(
  91. expandedText.substring(wraps[i], wraps[i + 1])
  92. ));
  93. }
  94. }
  95. } catch (IOException ioe) {}
  96. }
  97. public void paintSyntaxLine(
  98. Writer out, int lineNumber, Segment line, Token tokens
  99. ) {
  100. try {
  101. if (this.showGutter) {
  102. out.write(this.gutter.format(lineNumber));
  103. }
  104. } catch (IOException ioe) {}
  105. for (;;) {
  106. byte id = tokens.id;
  107. if (id == Token.END) {
  108. break;
  109. }
  110. int length = tokens.length;
  111. line.count = length;
  112. try {
  113. int pos = this.position.getPos();
  114. String expandedText = this.expander.expand(pos, line.array, line.offset, length);
  115. this.position.incPos(expandedText.length());
  116. int[] wraps = null;
  117. if (this.wrapper != null) {
  118. wraps = this.wrapper.wrap(pos, expandedText.length());
  119. if (pos > 0 && (pos % this.wrap) == 0) {
  120. out.write( newLine() );
  121. if (this.showGutter) {
  122. out.write(this.gutter.formatEmpty(lineNumber));
  123. }
  124. }
  125. }
  126. if (wraps == null) {
  127. String text = format(expandedText);
  128. if (id == Token.NULL) {
  129. out.write(text);
  130. } else {
  131. out.write(this.style.format(id, this.syntaxStyles[id], text));
  132. }
  133. } else {
  134. String text;
  135. for (int i = 0; i < wraps.length - 1; i++) {
  136. if (i >= 1) {
  137. out.write( newLine( ) );
  138. if (this.showGutter) {
  139. out.write(this.gutter.formatEmpty(lineNumber));
  140. }
  141. }
  142. text = format(
  143. expandedText.substring(wraps[i], wraps[i + 1])
  144. );
  145. if (id == Token.NULL) {
  146. out.write(text);
  147. } else {
  148. out.write(this.style.format(id, this.syntaxStyles[id], text));
  149. }
  150. }
  151. }
  152. } catch (IOException ioe) {}
  153. line.offset += length;
  154. tokens = tokens.next;
  155. }
  156. }
  157. protected abstract String newLine( ) ;
  158. protected abstract String format( String text) ;
  159. }