1 16 17 package de.gulden.util.javasource; 18 19 import de.gulden.util.javasource.jjt.Node; 20 import de.gulden.util.javasource.jjt.*; 21 import de.gulden.util.xml.XMLToolbox; 22 import javax.xml.parsers.*; 23 import org.w3c.dom.*; 24 import java.io.*; 25 import java.util.*; 26 27 33 public class DocumentationDeclared extends Documentation { 34 35 39 42 private static final String workaroundReplaceSingleStar = "###SINGLE-STAR###"; 43 44 45 49 52 public Vector myDocumentationTagged; 53 54 57 public SourceObjectDeclared mySourceObjectDeclared; 58 59 60 64 67 public DocumentationDeclared() { 68 myDocumentationTagged=new Vector(); 69 } 70 71 72 76 79 public Enumeration getTags() { 80 return myDocumentationTagged.elements(); 81 } 82 83 public DocumentationTagged findTag(String tag, String item) { 84 for (Enumeration e=myDocumentationTagged.elements();e.hasMoreElements();) { 85 DocumentationTagged dt=(DocumentationTagged)e.nextElement(); 86 if ( 87 dt.getTag().equals(tag) 88 && 89 ( 90 ( (item==null) && (dt.getItem()==null) ) 91 || 92 ( (item!=null) && (dt.getItem()!=null) && (item.equals(dt.getItem())) ) 93 ) 94 ) { 95 return dt; 96 } 97 } 98 return null; 99 } 100 101 104 public void addTag(DocumentationTagged tag) { 105 myDocumentationTagged.addElement(tag); 106 } 107 108 111 public void removeTag(DocumentationTagged tag) { 112 myDocumentationTagged.removeElement(tag); 113 } 114 115 118 public void removeAllTags() { 119 myDocumentationTagged.removeAllElements(); 120 } 121 122 125 public SourceObjectDeclared getSourceObjectDeclared() { 126 return mySourceObjectDeclared; 127 } 128 129 132 public void setSourceObjectDeclared(SourceObjectDeclared sourceObjectDeclared) { 133 this.mySourceObjectDeclared = sourceObjectDeclared; 134 } 135 136 139 public void setRaw(String raw) { 140 super.setRaw(raw); 141 try { 142 raw = raw.trim(); 143 while (raw.charAt(raw.length()-3)=='*') { 145 raw = raw.substring(0, raw.length()-3) + "*/"; 146 } 147 if (raw.charAt(raw.length()-3)!=' ') { 149 raw = raw.substring(0, raw.length()-2) +" */"; 150 } 151 raw = workaroundAvoidSingleStar(raw); 153 Node docnode=JavadocParser.parse(raw); 155 this.text = getTextFromNode(docnode.getChild(JavadocParserTreeConstants.JJTDESCRIPTION)).trim(); 156 Node[] tags=docnode.getChildren(JavadocParserTreeConstants.JJTTAG); 157 for (int i=0;i<tags.length;i++) { 158 String tag; 159 String tagItem; 160 String tagText; 161 tag=tags[i].getValue(); 162 Node itemNode=tags[i].getChild(JavadocParserTreeConstants.JJTTAGITEM); 163 if (itemNode!=null) { 164 tagItem=itemNode.getChild(JavadocParserTreeConstants.JJTWORD).getValue(); 165 } 166 else { 167 tagItem=null; 168 } 169 tagText=getTextFromNode(tags[i]).trim(); 170 DocumentationTagged dt=new DocumentationTagged(); 171 dt.setTag(tag); 172 dt.setItem(tagItem); 173 dt.setText(tagText); 174 this.addTag(dt); 175 } 176 return; 177 } 178 catch (TokenMgrError te) { 179 } 181 catch (ParseException pe) { 182 } 184 text="... warning: could not parse javadoc comment ..."; 185 String msg = "warning: could not parse javadoc comment, warning message inserted instead"; 186 SourceObjectDeclared sourceObjectDeclared = getSourceObjectDeclared(); 187 if (sourceObjectDeclared != null) { 188 Class declaringClass = sourceObjectDeclared.getDeclaringClass(); 189 String n = sourceObjectDeclared.getName(); 190 msg += " ["+ ( declaringClass!=null ? declaringClass.getName()+(n!=null?".":"") : "") + (n!=null?n:"") + "]"; 191 } 192 System.err.println(msg); 193 } 194 195 201 public void initFromXML(Element element) throws IOException { 202 name=element.getAttribute("name"); setText(XMLToolbox.getText(element)); 205 myDocumentationTagged.removeAllElements(); 206 NodeList nl=XMLToolbox.getChildren(element,"tag"); 207 for (int i=0;i<nl.getLength();i++) { 208 DocumentationTagged dt=new DocumentationTagged(); 209 dt.initFromXML((Element)nl.item(i)); 210 myDocumentationTagged.addElement(dt); 211 } 212 } 213 214 220 public Element buildXML(Document d) { 221 Element e=super.buildXML(d); 222 for (Enumeration ee=getTags();ee.hasMoreElements();) { 223 DocumentationTagged dt=(DocumentationTagged)ee.nextElement(); 224 String tag=dt.getTag(); 225 if (!(tag.equals("@param") ||tag.equals("@exception") 227 ||tag.equals("@throws") 228 )) { 229 e.appendChild(dt.buildXML(d)); 230 } 231 } 232 return e; 233 } 234 235 236 240 protected static String workaroundAvoidSingleStar(String s) { 241 int pos = s.indexOf('*'); 242 while (pos != -1) { 243 if (s.charAt(pos+1)!='/') { int linestart = s.lastIndexOf(nl, pos) + 1; String beforeStar = s.substring(linestart, pos); 246 if ((beforeStar.indexOf('*')!=-1) && (!beforeStar.equals("/*"))) { 248 s = s.substring(0, pos) + workaroundReplaceSingleStar + s.substring(pos+1); 249 return workaroundAvoidSingleStar(s); 250 } 251 pos = s.indexOf('*', pos+1); 252 } else { 253 pos = -1; 254 } 255 } 256 return s; 257 } 258 259 protected static String workaroundRestoreSingleStar(String s) { 260 int pos = s.indexOf(workaroundReplaceSingleStar); 261 if (pos != -1) { 262 return s.substring(0, pos) + "*" + workaroundRestoreSingleStar(s.substring(pos+workaroundReplaceSingleStar.length())); 263 } else { 264 return s; 265 } 266 } 267 268 271 private static String getTextFromNode(Node n) { 272 StringBuffer sb=new StringBuffer (); 273 Node[] lines=n.getChildren(JavadocParserTreeConstants.JJTLINE); 274 for (int i=0;i<lines.length;i++) { 275 Node[] words=lines[i].getChildren(JavadocParserTreeConstants.JJTWORD); 276 for (int j=0;j<words.length;j++) { 277 sb.append( workaroundRestoreSingleStar( words[j].getValue() ) ); 278 if (j<words.length-1) { 279 sb.append(" "); 280 } 281 } 282 sb.append("\n"); 283 } 284 return sb.toString(); 285 } 286 287 } | Popular Tags |