1 33 34 package net.percederberg.grammatica.code.java; 35 36 import java.io.PrintWriter ; 37 38 import net.percederberg.grammatica.code.CodeElement; 39 import net.percederberg.grammatica.code.CodeStyle; 40 41 47 public class JavaComment extends CodeElement { 48 49 52 public static final int DOCUMENTATION = 0; 53 54 57 public static final int BLOCK = 1; 58 59 64 public static final int SINGLELINE = 2; 65 66 69 private int type; 70 71 74 private String comment; 75 76 81 public JavaComment(String comment) { 82 this(DOCUMENTATION, comment); 83 } 84 85 95 public JavaComment(int type, String comment) { 96 if (type == BLOCK || type == SINGLELINE) { 97 this.type = type; 98 } else { 99 this.type = DOCUMENTATION; 100 } 101 this.comment = comment; 102 } 103 104 112 public int category() { 113 return 0; 114 } 115 116 123 public void print(PrintWriter out, CodeStyle style, int indent) { 124 String indentStr = style.getIndent(indent); 125 String firstLine; 126 String restLines; 127 int pos; 128 129 if (type == DOCUMENTATION) { 131 out.println(indentStr + "/**"); 132 } else if (type == BLOCK) { 133 out.println(indentStr + "/*"); 134 } 135 136 restLines = comment; 138 while ((pos = restLines.indexOf('\n')) >= 0) { 139 firstLine = restLines.substring(0, pos); 140 restLines = restLines.substring(pos + 1); 141 printLine(out, indentStr, firstLine); 142 } 143 printLine(out, indentStr, restLines); 144 145 if (type != SINGLELINE) { 147 out.println(indentStr + " */"); 148 } 149 } 150 151 158 private void printLine(PrintWriter out, String indent, String line) { 159 if (type == SINGLELINE) { 160 out.print(indent + "//"); 161 } else { 162 out.print(indent + " *"); 163 } 164 if (line.equals("")) { 165 out.println(); 166 } else { 167 out.println(" " + line); 168 } 169 } 170 } 171 | Popular Tags |