|
What this is
Other links
The source code/* * Sun Public License Notice * * The contents of this file are subject to the Sun Public License * Version 1.0 (the "License"). You may not use this file except in * compliance with the License. A copy of the License is available at * http://www.sun.com/ * * The Original Code is NetBeans. The Initial Developer of the Original * Code is Sun Microsystems, Inc. Portions Copyright 1997-2004 Sun * Microsystems, Inc. All Rights Reserved. */ package org.netbeans.modules.javacore.parser; import java.util.Arrays; import java.util.HashMap; import java.util.List; import java.util.ArrayList; import java.io.BufferedInputStream; import java.io.FileInputStream; import java.io.File; /** JavaDoc Parser. * * @author Vladimir Hudec */ public class JavaDocParser { public static final String TEXT_TAG_NAME = "@@@@@"; // NOI18N protected String rawText; protected JavaDocTag[] tags; /** * Native line separator. */ private static final String lineSeparator; private static final int lineSeparatorLength; static { String sep = System.getProperty("line.separator"); // NOI18N if (sep == null || sep.equals("\n")) { // NOI18N lineSeparator = null; lineSeparatorLength = 0; } else { lineSeparator = sep; lineSeparatorLength = sep.length(); } } /** Constructs the JavaDoc object held in memory. Parses the tags from rawText. */ public JavaDocParser(String rawText) { this.rawText = rawText; this.tags = null; } /** Constructs the JavaDoc object held in memory. Used for rawText construction. */ public JavaDocParser(JavaDocTag[] tags) { this.rawText = null; this.tags = tags; } public JavaDocParser(String text, JavaDocTag[] tags) { this.rawText = null; if (text == null) { this.tags = tags; } else { int newSize = (tags != null) ? tags.length+1 : 1; JavaDocTag[] newTags = new JavaDocTag[newSize]; newTags[0] = new JavaDocTag(TEXT_TAG_NAME, text); if (tags != null) System.arraycopy(tags, 0, newTags, 1, tags.length); this.tags = newTags; } } /** Get the entire text of the comment. * @return the whole text */ public String getRawText() { return getRawText(false); } public String getRawText(boolean reorder) { if (rawText != null && !reorder) return rawText; if (rawText != null) tags = getTags(); if (reorder) Arrays.sort(tags); String javadocContent = buildComment(tags); //return surroundWithJavaDocStars(javadocContent); return javadocContent; } /** Set the raw text of the comment. * @param s the whole text to set */ //public void setRawText(String s) { // rawText = s; //} /** Clears the javadoc from the source. */ public void clearJavaDoc() { rawText = null; } /** Test if this javadoc is empty. * @return true if it is not generated to the source. */ public boolean isEmpty() { return rawText == null; } /** Gets all tags from comment. */ public JavaDocTag[] getTags() { if (tags != null) return tags; if ( rawText == null ) return null; List tagList = new ArrayList(); //System.out.println("getRawText(): "+getRawText()); int[] textIndexes = removeJavaDocStars(getRawText()); parseComment(tagList, getRawText(), textIndexes); JavaDocTag[] tagArray = new JavaDocTag[tagList.size()]; tagList.toArray(tagArray); return tagArray; } /** Gets all tags of given name */ public JavaDocTag[] getTags(String name) { JavaDocTag[] allTags = getTags(); ArrayList resultList = new ArrayList( allTags.length ); for( int i = 0; i < allTags.length; i++ ) { if (allTags[i].getName().equals(name)) resultList.add(allTags[i]); } JavaDocTag result[] = new JavaDocTag[resultList.size()]; resultList.toArray(result); return result; } // PRIVATE & UTILITY METHODS ---------------------------------------------------------- /** * Parses the rawText and generates list of tags; */ private void parseComment(List tagList, String textWithStars, int[] textIndexes) { final int IN_TEXT = 1; final int TAG_GAP = 2; final int TAG_NAME = 3; final int TAG_TEXT = 4; int state = IN_TEXT; boolean newLine = true; int tagStart = 0; int tagEnd = 0; int textStart = 0; int textEnd = 0; int lastNonWhite = -1; String rawText = rebuildText(textWithStars, textIndexes); //System.out.println("parseComment(): rawText=>"+rawText+"<"); //System.out.println("parseComment(): lineSeparator=>"+lineSeparator+"<"); int len = rawText.length(); for (int i = 0; i < len; ++i) { char ch = rawText.charAt(i); boolean isWhite = Character.isWhitespace(ch); switch (state) { case IN_TEXT: if (newLine && ch == '@') { parseCommentComponent(tagList, rawText, textIndexes, -1, -1, 0, textEnd); tagStart = i; state = TAG_NAME; } break; case TAG_NAME: if (isWhite) { tagEnd = i; state = TAG_GAP; } break; case TAG_GAP: if (isWhite) { break; } textStart = i; state = TAG_TEXT; // Fall through (in case of @tagname\n@anothertagname) case TAG_TEXT: if (newLine && ch == '@') { parseCommentComponent(tagList, rawText, textIndexes, tagStart, tagEnd, textStart, lastNonWhite + 1); tagStart = i; state = TAG_NAME; } break; } // more idiot-proof check for newline terminator: if (lineSeparator != null && i + lineSeparatorLength <= len && rawText.regionMatches(i, lineSeparator, 0, lineSeparatorLength)) { newLine = true; if (state == IN_TEXT) { textEnd = i; } // advance the scan pointer after the separator string. i += lineSeparatorLength - 1; } else if (ch == '\n') { newLine = true; if (state == IN_TEXT) { textEnd = i; } } else if (!isWhite) { lastNonWhite = i; newLine = false; } } // Finish what's currently being processed switch (state) { case TAG_NAME: tagEnd = len; /* fall thru */ case TAG_GAP: textStart = len; /* fall thru */ case TAG_TEXT: case IN_TEXT: parseCommentComponent(tagList, rawText, textIndexes, tagStart, tagEnd, textStart, lastNonWhite + 1); break; }; } /** * Parses the tag. * Saves away the last parsed item. */ private void parseCommentComponent(List tagList, String text, int[] textIndexes, int tagStart, int tagEnd, int textStart, int textEnd) { //System.out.println("parseCommentComponent: "+tagStart+","+tagEnd+","+textStart+","+textEnd); //System.out.println("parseCommentComponent: "+textIndex(textIndexes,tagStart)+","+textIndex(textIndexes,tagEnd)+","+textIndex(textIndexes,textStart)+","+textIndex(textIndexes,textEnd)); String tagName = (tagStart < 0 || tagEnd < 0 || tagStart > tagEnd || (tagStart == 0 && tagEnd == 0)) ? TEXT_TAG_NAME : text.substring(tagStart, tagEnd); String tx = (textStart < 0 || textEnd < 0 || textStart > textEnd) ? "" : text.substring(textStart, textEnd); if (TEXT_TAG_NAME.equals(tagName)) { JavaDocTag tag = new JavaDocTag(TEXT_TAG_NAME, tx, -1, -1, (textIndexes!=null)?textIndexes[textStart]:textStart, (textIndexes!=null)?textIndexes[textEnd]:textEnd); tagList.add(tag); } else { JavaDocTag tag = new JavaDocTag(tagName, tx, (textIndexes!=null)?textIndexes[tagStart]:tagStart, (textIndexes!=null)?textIndexes[tagEnd]:tagEnd, (textIndexes!=null)?textIndexes[textStart]:textStart, (textIndexes!=null)?textIndexes[textEnd]:textEnd); tagList.add(tag); } } public int[] removeJavaDocStars(String rawText) { final int SPACE_BEFORE=1; final int ASTERIX=2; final int SPACE_AFTER=3; final int TEXT=4; final int LINE_BREAK=5; if (rawText == null || rawText.length() < 4) return null; int len = rawText.length(); int text[] = new int[len]; int i, j = 0; int state = ASTERIX; int start = rawText.indexOf("/*"); int end = rawText.lastIndexOf("*/"); // NOI18N if (start == -1 || end == -1 || (start+2) > end) return null; int linebreak = -1; for (i=start+2; i |
... this post is sponsored by my books ... | |
#1 New Release! |
FP Best Seller |
Copyright 1998-2021 Alvin Alexander, alvinalexander.com
All Rights Reserved.
A percentage of advertising revenue from
pages under the /java/jwarehouse
URI on this website is
paid back to open source projects.