📄 commentstripper.java
字号:
/* * YALE - Yet Another Learning Environment * Copyright (C) 2002, 2003 * Simon Fischer, Ralf Klinkenberg, Ingo Mierswa, * Katharina Morik, Oliver Ritthoff * Artificial Intelligence Unit * Computer Science Department * University of Dortmund * 44221 Dortmund, Germany * email: yale@ls8.cs.uni-dortmund.de * web: http://yale.cs.uni-dortmund.de/ * * This program is free software; you can redistribute it and/or * modify it under the terms of the GNU General Public License as * published by the Free Software Foundation; either version 2 of the * License, or (at your option) any later version. * * This program is distributed in the hope that it will be useful, but * WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 * USA. */package edu.udo.cs.yale.doc;import edu.udo.cs.yale.tools.Tools;import java.io.File;import java.io.FileReader;import java.io.Reader;import java.io.BufferedReader;import java.io.IOException;import java.io.FileNotFoundException;public class CommentStripper { private File sourceDir; public CommentStripper(File sourceDir) { this.sourceDir = sourceDir; } public String stripClassComment(Class clazz) throws IOException { File sourceFile = new File(sourceDir, clazz.getName().replaceAll("\\.", File.separator) + ".java"); if (!sourceFile.exists()) throw new FileNotFoundException("No source file found for class "+clazz.getName()+ "; source was expected in '"+sourceFile+"'."); StringBuffer comment = null; BufferedReader reader = new BufferedReader(new FileReader(sourceFile)); String line = null; final int SCANNING_FOR_COMMENT = 0; final int READING_COMMENT = 1; final int FINISHED_COMMENT = 2; int currentState = SCANNING_FOR_COMMENT; while ((line = reader.readLine()) != null) { line = line.trim(); switch (currentState) { case SCANNING_FOR_COMMENT: if (line.startsWith("/**")) { comment = new StringBuffer(); line = line.substring("/**".length()); currentState = READING_COMMENT; } else { break; } // ATTENTION (in case someone wants to read this code): there is no break here! case READING_COMMENT: int end = line.indexOf("*/"); if (end != -1) { line = line.substring(0, end); currentState = FINISHED_COMMENT; } line = stripAsteriks(line); if (line.startsWith("@")) continue; comment.append(line + " "); break; case FINISHED_COMMENT: if (line.length() > 0) { int classIndex = line.indexOf("class"); int nameIndex = line.indexOf(Tools.classNameWOPackage(clazz)); if ((classIndex != -1) && (nameIndex > classIndex+"class".length())) { return comment.toString().trim(); } else { // this is not what we want comment = null; } } break; } } reader.close(); return null; } private static String stripAsteriks(String string) { while (string.startsWith("*")) string = string.substring(1).trim(); return string.trim(); }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -