1 package org.netbeans.modules.tasklist.javadoc; 2 3 import java.beans.PropertyChangeEvent ; 4 import java.beans.PropertyChangeListener ; 5 import java.util.ArrayList ; 6 import java.util.List ; 7 import org.openide.cookies.OpenCookie; 8 import org.openide.src.ClassElement; 9 import org.openide.src.ElementFormat; 10 import org.openide.src.JavaDoc; 11 import org.openide.src.JavaDocTag; 12 import org.openide.src.MemberElement; 13 import org.openide.src.SourceElement; 14 import org.openide.src.SourceException; 15 import org.openide.util.NbBundle; 16 import org.netbeans.modules.tasklist.javadoc.ext.JavaTagNames; 17 import org.openide.util.WeakListeners; 18 19 22 public abstract class AutoCommenterElement { 23 protected ArrayList errorList; 24 protected List resolutionList = null; 25 26 MemberElement srcElement = null; 27 int srcError = AutoCommenter.JDC_OK; 28 29 public AutoCommenterElement( MemberElement srcElement ) { 30 this.srcElement = srcElement; 31 PropertyChangeListener pp = new PropertyChangeListener (){ 32 public void propertyChange(PropertyChangeEvent evt){ 33 38 } 41 }; 42 srcElement.addPropertyChangeListener( 44 WeakListeners.propertyChange(pp, srcElement)); 45 checkError(); 46 } 47 48 String getName() { 49 return getNameFormat().format( srcElement ); 50 } 51 52 MemberElement getSrcElement() { 53 return srcElement; 54 } 55 56 int getModifiers() { 57 return srcElement.getModifiers(); 58 } 59 60 int getErrorNumber() { 61 return srcError; 62 } 63 64 void viewSource() { 65 OpenCookie oc = ((OpenCookie)srcElement.getCookie( OpenCookie.class )); 66 oc.open(); 67 } 68 69 ArrayList getErrorList() { 70 return errorList; 71 } 72 73 List getResolutionList() { 74 return resolutionList; 75 } 76 77 79 SourceElement findSource() { 80 ClassElement decl = srcElement.getDeclaringClass(); 81 if (decl == null && (srcElement instanceof ClassElement)) { 82 decl = (ClassElement)srcElement; 83 } 84 if (decl == null) return null; 85 return decl.getSource(); 86 } 87 88 abstract String [] getNotPermittedTags(); 89 90 abstract boolean elementTagsOk(); 91 92 abstract void autoCorrect() throws SourceException; 93 94 abstract JavaDoc getJavaDoc(); 95 96 abstract ElementFormat getNameFormat(); 97 98 abstract String typeToString(); 99 100 static boolean isPermittedTag( JavaDocTag tag, String [] notPermittedTags ) { 101 String tagName = tag.name(); 102 103 for ( int i = 0; i < notPermittedTags.length; i++ ) { 104 if ( tagName.equals( notPermittedTags[i] ) ) 105 return false; 106 } 107 108 return true; 109 } 110 111 void modifyJavaDoc(Runnable mutator) throws SourceException { 112 SourceElement src = findSource(); 113 if (src == null) { 114 mutator.run(); 115 } else { 116 src.runAtomicAsUser(mutator); 117 } 118 } 119 120 private static boolean isEmptyString( String string ) { 121 return string == null || string.trim().length() <= 0; 122 } 123 124 126 127 boolean isOkTag( JavaDocTag tag ) { 128 if ( isEmptyString( tag.text() ) ) { 129 errorList.add(NbBundle.getMessage(AutoCommenterElement.class, "ERR_EmptyTag", tag.name())); 130 return false; 133 } 134 135 if ( tag instanceof JavaDocTag.See ) { 136 int len; 137 String text; 138 JavaDocTag.See seetag = (JavaDocTag.See) tag; 139 140 if ((seetag.referencedClassName() != null) || (seetag.referencedMemberName() != null)) 141 return true; 142 text=tag.text(); 143 len = text.length(); 144 if (len >= 2) { 145 char first=text.charAt(0); 146 char last=text.charAt(len-1); 147 148 if (first=='"' && last==first) 149 return true; 150 if (first=='<' && last=='>') 151 return true; 152 } 153 errorList.add(NbBundle.getMessage(AutoCommenterElement.class, "ERR_InvalidTag", seetag)); resolutionList.add(NbBundle.getMessage(AutoCommenterElement.class, "FIX_InvalidTag", seetag)); return false; 156 } 157 else if ( tag instanceof JavaDocTag.Param ) { 158 if ( isEmptyString( ((JavaDocTag.Param)tag).parameterName() ) ) { 159 errorList.add( NbBundle.getMessage(AutoCommenterElement.class, "ERR_ParamNoName", tag.name())); 160 return false; 161 } 162 if ( isEmptyString( ((JavaDocTag.Param)tag).parameterComment() ) ) { 163 errorList.add( NbBundle.getMessage(AutoCommenterElement.class, "ERR_ParamNoDescr", tag.name(), ((JavaDocTag.Param)tag).parameterName())); 164 return false; 165 } 166 } 167 else if ( tag instanceof JavaDocTag.Throws ) { 168 if ( isEmptyString( ((JavaDocTag.Throws)tag).exceptionName() ) ) { 169 errorList.add( NbBundle.getMessage(AutoCommenterElement.class, "ERR_ThrowsNoName", tag.name())); 170 return false; 171 } 172 if ( isEmptyString( ((JavaDocTag.Throws)tag).exceptionComment() ) ) { 173 errorList.add(NbBundle.getMessage(AutoCommenterElement.class, "ERR_ThrowsNoDescr", tag.name(), ((JavaDocTag.Throws)tag).exceptionName())); 174 return false; 175 } 176 } 177 else if ( tag instanceof JavaDocTag.SerialField ) { 178 if ( isEmptyString( ((JavaDocTag.SerialField)tag).fieldName() ) ) { 179 errorList.add(NbBundle.getMessage(AutoCommenterElement.class, "ERR_SerialFieldNoName", tag.name())); 180 return false; 181 } 182 if ( isEmptyString( ((JavaDocTag.SerialField)tag).fieldType() ) ) { 183 errorList.add(NbBundle.getMessage(AutoCommenterElement.class, "ERR_SerialFieldNoType", tag.name(), ((JavaDocTag.SerialField)tag).fieldName())); 184 return false; 185 } 186 187 if ( isEmptyString( ((JavaDocTag.SerialField)tag).description() ) ) { 188 errorList.add(NbBundle.getMessage(AutoCommenter.class, "ERR_SerialFieldNoDescr", tag.name(), ((JavaDocTag.SerialField)tag).fieldName())); 189 return false; 190 } 191 } 192 return true; 193 } 194 195 boolean isMultipleTags(String tag) { 196 boolean error = false; 198 JavaDocTag[] tags = getJavaDoc().getTags(tag); 199 if ( tags.length > 1) { 200 errorList.add( NbBundle.getMessage(AutoCommenterElement.class, "ERR_DuplicatedTag", tags[0].name())); 201 resolutionList.add(NbBundle.getMessage(AutoCommenterElement.class, "FIX_DuplicatedTag", tags[0].name())); 202 error = true; 203 } 204 return error; 205 } 206 207 void checkError() { 208 209 errorList = new ArrayList (); 210 resolutionList = new ArrayList (); 211 212 JavaDoc jdoc = getJavaDoc(); 213 214 if ( jdoc.isEmpty() ) { 215 srcError = AutoCommenter.JDC_MISSING; 216 errorList.add( NbBundle.getMessage(AutoCommenterElement.class, "ERR_JavadocMissing")); return; 219 } 220 221 JavaDocTag[] tags = jdoc.getTags(); 222 boolean error = false; 223 224 if ( jdoc.getText() == null || jdoc.getText().trim().length() <= 0 ) { 225 errorList.add(NbBundle.getMessage(AutoCommenterElement.class, "ERR_EmptyText")); error = true; 227 } 228 229 for ( int i = 0; i < tags.length; i ++ ) { 230 if ( !AutoCommenterElement.isPermittedTag( tags[i], getNotPermittedTags() ) ) { 231 errorList.add(NbBundle.getMessage(AutoCommenterElement.class, "ERR_BadTag", tags[i].name(), typeToString())); 232 resolutionList.add(NbBundle.getMessage(AutoCommenterElement.class, "FIX_BadTag", tags[i].name(), typeToString())); 233 234 error = true; 235 continue; 236 } 237 238 if ( !isOkTag( tags[i] ) ) { 239 error = true; 240 continue; 241 } 242 } 243 244 if (isMultipleTags(JavaTagNames.TAG_SINCE)) { 245 error = true; 246 } 247 248 if (isMultipleTags(JavaTagNames.TAG_DEPRECATED)) { 249 error = true; 250 } 251 252 if ( !elementTagsOk( ) ) { 253 error = true; 254 } 255 256 if ( !error ) { 257 errorList.add(NbBundle.getMessage(AutoCommenterElement.class, "ERR_JavadocOK")); } 259 260 srcError = error ? AutoCommenter.JDC_ERROR : AutoCommenter.JDC_OK; 261 262 } 263 264 boolean isCorrectable() { 265 266 JavaDocTag[] tags = getJavaDoc().getTags(); 267 268 for ( int i = 0; i < tags.length; i ++ ) { 269 if ( !AutoCommenterElement.isPermittedTag( tags[i], getNotPermittedTags() ) ) { 270 return true; 271 } 272 } 273 274 return false; 275 } 276 277 void autoCorrect( JavaDoc jdoc ) throws SourceException { 278 JavaDocTag[] tags = jdoc.getTags(); 279 ArrayList correctedTags = new ArrayList ( tags.length ); 280 String correctedText; 281 282 correctedText = jdoc.getText(); 283 284 if ( correctedText == null ) { 285 correctedText = ""; } 287 288 for ( int i = 0; i < tags.length; i ++ ) { 289 if ( !AutoCommenterElement.isPermittedTag( tags[i], getNotPermittedTags() ) ) { 290 continue; 291 } 292 correctedTags.add( tags[i] ); 293 } 294 295 jdoc.changeTags( (JavaDocTag[])correctedTags.toArray( new JavaDocTag[ correctedTags.size() ] ), JavaDoc.SET ); 297 } 298 } 299 | Popular Tags |