1 52 53 package com.go.beandoc; 54 55 import com.go.beandoc.teadoc.*; 56 57 import java.io.*; 58 59 66 public class BeanDocContext extends com.go.tea.runtime.DefaultContext { 67 68 69 private BeanDoc mBeanDoc; 70 71 72 private File mDest; 73 74 75 private Writer mOut; 76 77 private MethodFinder mCommentedMethodFinder; 78 private CommentedParamFinder mCommentedParamFinder; 79 80 86 public BeanDocContext(BeanDoc beandoc, File dest) throws IOException { 87 super(); 88 89 mBeanDoc = beandoc; 90 91 String parentDir = dest.getParent(); 92 if (parentDir != null) { 93 File dir = new File(parentDir); 94 if (!dir.exists() || !dir.isDirectory()) { 95 dir.mkdirs(); 96 } 97 } 98 99 mOut = new BufferedWriter(new FileWriter(dest)); 100 101 mCommentedMethodFinder = new MethodFinder() { 102 public boolean checkMethod(MethodDoc md) { 103 return (md != null && md.getCommentText() != null); 104 } 105 }; 106 107 mCommentedParamFinder = new CommentedParamFinder(); 108 109 } 110 111 114 public void print(Object obj) throws IOException { 115 116 if (mOut != null) { 117 mOut.write(toString(obj)); 119 } 120 } 121 122 125 public void close() throws IOException { 126 127 if (mOut != null) { 128 mOut.close(); 129 mOut = null; 130 } 131 } 132 133 134 public String getMethodComment(ClassDoc classDoc, MethodDoc md) { 135 136 String comment = md.getCommentText(); 137 if (comment == null) { 138 md = classDoc.findMatchingMethod(md, mCommentedMethodFinder); 139 if (md != null) { 140 comment = md.getCommentText(); 141 } 142 } 143 144 if (comment != null) { 145 comment = formatForLiteral(comment.trim()); 146 } 147 148 return comment; 149 } 150 151 public String getMethodParamComment(ClassDoc classDoc, 152 MethodDoc md, 153 String paramName) { 154 155 if (paramName == null) { 156 return null; 157 } 158 159 mCommentedParamFinder.paramName = paramName; 160 161 classDoc.getMatchingMethod(md, mCommentedParamFinder); 162 String comment = mCommentedParamFinder.comment; 163 164 if (comment == null) { 165 classDoc.findMatchingMethod(md, mCommentedParamFinder); 166 comment = mCommentedParamFinder.comment; 167 } 168 169 if (comment != null) { 170 comment = formatForLiteral(comment.trim()); 171 } 172 173 return comment; 174 } 175 176 183 public String formatForLiteral(String s) { 184 185 if (s == null) { 186 return ""; 187 } 188 189 StringBuffer sb = new StringBuffer (s.length()); 190 char[] chars = s.toCharArray(); 191 for (int i = 0; i < chars.length; i++) { 192 char c = chars[i]; 193 switch (c) { 194 case '\"': 195 sb.append("\\\""); 196 break; 197 case '\'': 198 sb.append("\\\'"); 199 break; 200 case '\t': 201 sb.append("\\t"); 202 break; 203 case '\r': 204 sb.append("\\r"); 205 break; 206 case '\n': 207 sb.append("\\n"); 208 break; 209 case '\f': 210 sb.append("\\f"); 211 break; 212 case '\b': 213 sb.append("\\b"); 214 break; 215 case '\\': 216 sb.append("\\\\"); 217 break; 218 default: 219 sb.append(c); 220 break; 221 } 222 } 223 224 return sb.toString(); 225 } 226 227 231 public void printError(String msg) { 232 mBeanDoc.printError(msg); 233 } 234 235 239 public void printWarning(String msg) { 240 mBeanDoc.printWarning(msg); 241 } 242 243 247 public void printNotice(String msg) { 248 mBeanDoc.printNotice(msg); 249 } 250 251 252 private static class CommentedParamFinder implements MethodFinder { 253 254 public String paramName; 255 public String comment; 256 257 public boolean checkMethod(MethodDoc md) { 258 comment = null; 259 260 ParamTag[] paramTags = md.getParamTags(); 261 if (paramTags != null) { 262 for (int i = 0; i < paramTags.length; i++) { 263 if (paramName.equals(paramTags[i].getParameterName())) { 264 comment = paramTags[i].getParameterComment(); 265 if (comment != null) { 266 return true; 267 } 268 } 269 } 270 } 271 272 return false; 273 } 274 } 275 276 } 277 | Popular Tags |