1 4 package org.oddjob.doclet; 5 6 import com.sun.javadoc.ClassDoc; 7 import com.sun.javadoc.Doc; 8 import com.sun.javadoc.FieldDoc; 9 import com.sun.javadoc.MethodDoc; 10 import com.sun.javadoc.SeeTag; 11 import com.sun.javadoc.Tag; 12 13 19 public class Processor { 20 21 private final ClassDoc classDoc; 22 private final String rootDir; 23 private final JobsAndTypes jats; 24 25 32 public Processor(JobsAndTypes jats, ClassDoc classDoc) { 33 this.jats = jats; 34 this.classDoc = classDoc; 35 String [] packages = classDoc.containingPackage().name().split(("[.]")); 36 String rootDir = ""; 37 for (int i = 0; i < packages.length; ++i) { 38 rootDir = rootDir + (i == 0 ? "" : "/") + ".."; 39 } 40 this.rootDir = rootDir; 41 } 42 43 54 String processSingleTextTag(Tag[] tags, String replacement) { 55 if (tags.length == 0) { 56 return null; 57 } 58 if (tags.length > 1) { 59 System.err.println("More than one tag for [" + replacement 60 + "]. Ignoring all others"); 61 } 62 63 String text = tags[0].text(); 64 65 if (text == null || text.trim().equals("")) { 66 return replacement; 67 } 68 69 return text; 70 } 71 72 78 void processFieldOrMethod(PageData pageData, Doc doc) { 79 Property prop = null; 80 Tag[] propertyTags = doc.tags("@oddjob.property"); 81 Tag[] elementTags = doc.tags("@oddjob.element"); 82 83 String propertyText = processSingleTextTag( 84 propertyTags, doc.name()); 85 String elementText = processSingleTextTag( 86 elementTags, doc.name()); 87 88 if (propertyText != null) { 89 prop = new Property(propertyText); 90 pageData.addAttribute(prop); 91 } 92 else if (elementText != null) { 93 prop = new Property(elementText); 94 pageData.addElement(prop); 95 } 96 else { 97 return; 100 } 101 102 Tag[] descriptionTags = doc.tags("@oddjob.description"); 103 prop.setDescription(processMajorTags(descriptionTags)); 104 105 Tag[] rtags = doc.tags("@oddjob.required"); 106 if (rtags.length != 0) { 107 prop.setRequired(rtags[0].text()); 108 } 109 } 110 111 117 String processInlineTags(Tag[] inlines) { 118 StringBuffer buffer = new StringBuffer (); 119 for (int i = 0; i < inlines.length; ++i) { 120 Tag it = inlines[i]; 121 if (it instanceof SeeTag) { 122 SeeTag seeTag = (SeeTag) it; 123 String ref = seeTag.referencedClassName(); 124 String otherName = jats.nameFor(ref); 125 if (otherName == null) { 126 otherName = ref; 127 } 128 buffer.append("<a HREF='" + rootDir + "/" 129 + ref.replace('.', '/') + ".html'>" 130 + otherName + "</a>"); 131 } 132 else { 133 buffer.append(it.text()); 134 } 135 } 136 137 return buffer.toString(); 138 } 139 140 141 147 String processMajorTags(Tag[] tags) { 148 if (tags == null) { 149 return null; 150 } 151 StringBuffer result = new StringBuffer (); 152 for (int i = 0; i < tags.length; ++i) { 153 result.append(processInlineTags( 154 tags[i].inlineTags())); 155 } 156 return result.toString(); 157 } 158 159 166 String processFirstLine(Tag[] tags) { 167 if (tags == null) { 168 return null; 169 } 170 if (tags.length == 0) { 171 return null; 172 } 173 return processInlineTags(tags[0].firstSentenceTags()); 174 } 175 176 181 public String getFileName() { 182 String dir = classDoc.containingPackage().name().replace('.', '/'); 183 return dir + "/" + classDoc.name() + ".html"; 184 185 } 186 187 193 void processAllMembers(PageData pageData, ClassDoc classDoc) { 194 if (classDoc == null) { 195 return; 196 } 197 processAllMembers(pageData, classDoc.superclass()); 198 199 FieldDoc[] fds = classDoc.fields(); 200 for (int i = 0; i < fds.length; ++i) { 201 processFieldOrMethod(pageData, fds[i]); 202 } 203 MethodDoc[] mds = classDoc.methods(); 204 for (int i = 0; i < mds.length; ++i) { 205 processFieldOrMethod(pageData, mds[i]); 206 } 207 208 } 209 210 216 public PageData process() { 217 String name = jats.nameFor(fqcnFor(classDoc)); 218 String filename = getFileName(); 219 System.out.println("Processing: " + name); 220 PageData pageData = new PageData(name, filename); 221 222 Tag[] descriptionTags = classDoc.tags("@oddjob.description"); 223 pageData.setFirstSentence(processFirstLine(descriptionTags)); 224 pageData.setDescription(processMajorTags(descriptionTags)); 225 226 Tag[] exampleTags = classDoc.tags("@oddjob.example"); 227 for (int i = 0; i < exampleTags.length; ++i) { 228 pageData.addExample( 229 processInlineTags(exampleTags[i].inlineTags())); 230 } 231 232 processAllMembers(pageData, classDoc); 233 return pageData; 234 } 235 236 public static String fqcnFor(ClassDoc classDoc) { 237 return classDoc.containingPackage().name() + "." + classDoc.name(); 238 } 239 240 } 241 | Popular Tags |