1 19 package org.netbeans.modules.xml.tools.doclet; 20 21 import java.util.*; 22 import java.text.DateFormat ; 23 24 import org.xml.sax.Locator ; 25 import org.xml.sax.InputSource ; 26 import org.xml.sax.SAXException ; 27 import org.xml.sax.SAXParseException ; 28 import org.xml.sax.AttributeList ; 29 30 import org.xml.sax.DocumentHandler ; 31 import org.xml.sax.DTDHandler ; 32 import org.xml.sax.ErrorHandler ; 33 import org.xml.sax.EntityResolver ; 34 35 import org.netbeans.tax.*; 36 import org.netbeans.tax.decl.*; 37 38 49 public class DTDDoclet { 50 51 private RefList s; 53 private RefList elementIndex = new RefList(); 54 55 private String comment; 57 60 private String currentElement = null; 61 62 65 private HashMap elementRefs = new HashMap(); 66 67 69 private final String HEADLINE1 = "h2"; private final String HEADLINE2 = "b"; 72 private final String LIST = "ul"; private final String LIST_ITEM = "li"; 75 private final String PAR = "p"; 77 private final String ROOT = "html"; private final String BODY = "body"; 80 81 public DTDDoclet() { 82 83 } 84 85 88 public String createDoclet (TreeDTD dtd) { 89 if (dtd == null) 90 return ""; 92 Iterator it; 93 s = new RefList(); 94 95 s.append("<!DOCTYPE html PUBLIC \"-//W3C//DTD XHTML 1.0 Strict//EN\" \"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd\">\n"); 97 s.appendStartTag(ROOT).append("\n<head>\n<title>" + Util.THIS.getString("PAGE_TITLE") + "</title>\n"); s.append("<meta http-equiv=\"Content-Type\" content=\"text/xhtml; charset=UTF-8\" />\n"); 99 s.append("</head>\n"); 100 s.append("\n<!-- Generated on " + DateFormat.getDateInstance().format(new Date()) + " by NetBeans XML module. -->\n"); s.appendStartTag(BODY); 102 103 headline1(Util.THIS.getString("TEXT_Element_Index")); 104 105 s.append(elementIndex); 106 107 headline1(Util.THIS.getString("TEXT_Element_Details")); 108 109 it = dtd.getChildNodes(TreeChild.class, true).iterator(); 110 111 Vector list = new Vector(); 112 while (it.hasNext()) { 113 TreeNode child = (TreeNode)it.next(); 114 if (child instanceof TreeElementDecl) { 115 commentElement((TreeElementDecl) child); 116 comment = null; 117 } else if (child instanceof TreeComment) { 118 comment = decodeComment((TreeComment) child); 119 } else if (child instanceof TreeText || child instanceof TreeParameterEntityReference) { 120 } else { 122 comment = null; 124 } 125 } 126 127 128 131 TreeSet index = new TreeSet(); 132 it = elementRefs.keySet().iterator(); 133 134 while (it.hasNext()) { 135 index.add(it.next()); 136 } 137 138 elementIndex.appendStartTag(LIST); 139 it = index.iterator(); 140 while (it.hasNext()) { 141 Object next = it.next(); 142 listitem(elementIndex, "<a HREF=\"#" + next + "\">" + next + "</a>"); } 144 elementIndex.appendEndTag(LIST); 145 146 147 150 s.appendEndTag(BODY).appendEndTag(ROOT); 151 return s.toString(); 152 } 153 154 156 157 160 private String decodeComment(TreeComment node) { 161 return node.getData().startsWith("-") ? node.getData().substring(1).trim() : null; } 163 164 165 166 private void commentElement(TreeElementDecl node) { 167 String tag = node.getName(); 168 currentElement = tag; 169 170 headline1(tag, tag); 171 s.append(comment == null ? "" : "<div>" + comment + "</div>"); commentAttributes(node); 174 175 headline2(Util.THIS.getString("TEXT_ContentModel")); 176 TreeElementDecl.ContentType type = node.getContentType(); 177 s.append("<p><tt>"); commentContentModel(type); 179 s.append("</tt></p>"); 181 headline2(Util.THIS.getString("TEXT_Referenced_by")); 182 s.append("<p><tt>"); s.append(getRefList(tag)); 184 s.append("</tt></p>"); s.append("\n"); } 187 188 192 private void commentAttributes(TreeElementDecl element) { 193 194 Iterator it = element.getAttributeDefs().iterator(); 195 196 if (!!! it.hasNext()) 197 return; 198 199 headline2(Util.THIS.getString("TEXT_Declared_Attributes")); 200 s.appendStartTag(LIST); 202 while (it.hasNext()) { 203 TreeAttlistDeclAttributeDef next = (TreeAttlistDeclAttributeDef) it.next(); 204 205 String defVal = next.getDefaultValue(); 206 if ( ( defVal == null ) || 207 ( defVal.length() == 0 ) ) { 208 defVal = ""; 209 } else { 210 defVal = " = " + defVal; } 212 213 String defType = next.getDefaultTypeName() == null ? "#DEFAULT" : next.getDefaultTypeName(); 215 String text = ""; 217 if (next.getType() == next.TYPE_ENUMERATED) { 218 text = defType + " ENUMERATION " + next.getEnumeratedTypeString(); } else { 220 text = defType + " " + next.getTypeName(); } 222 223 text += " " + next.getName() + defVal; 225 listitem(s, text); 226 } 227 228 s.appendEndTag(LIST); } 230 231 235 private void commentContentModel (TreeElementDecl.ContentType type) { 236 237 if (type instanceof EMPTYType) { 238 239 s.append("EMPTY"); return; 241 242 } else if (type instanceof ANYType) { 243 244 s.append("ANY"); return; 246 247 } 248 249 250 if (type instanceof LeafType) { 251 252 LeafType leaf = (LeafType)type; 253 String tag = leaf.getName(); 254 s.append("<a HREF=\"#").append(tag).append("\">"); s.append(tag).append("</a>").append(leaf.getMultiplicity()); 257 RefList refs = getRefList(tag); 260 String prefixSeparator = ", "; if (refs.isEmpty()) { 262 prefixSeparator = ""; } 264 265 refs.appendUniq(prefixSeparator + "<a HREF=\"#" + currentElement + "\">" + currentElement + "</a>"); 267 return; 268 } 269 270 271 s.append("("); 273 if (type.allowText()) { 274 275 s.append("#PCDATA"); 277 } 278 279 280 282 String separator = ""; if (type instanceof ChildrenType) { 284 String sepChar = ((ChildrenType)type).getSeparator(); 285 286 String prefix = sepChar.equals(",") == false ? " " : ""; separator = prefix + sepChar + " "; } 290 291 boolean prependSeparator = type.allowText(); 294 296 if (type instanceof TypeCollection) { 297 TypeCollection col = (TypeCollection) type; 298 Collection types = col.getTypes(); 299 300 Iterator it = types.iterator(); 301 while (it.hasNext()) { 302 TreeElementDecl.ContentType next = (TreeElementDecl.ContentType) it.next(); 303 304 if (prependSeparator) { 305 s.append (separator); 306 } 307 308 prependSeparator = true; 309 310 commentContentModel(next); 311 } 312 } 313 314 s.append(")" + type.getMultiplicity()); 316 } 317 318 private void headline1(String text, String id) { 319 s.append("\n\n<hr />\n").appendStartTag(HEADLINE1); if (id != null) s.append("<a name=\"" + id + "\"></a>"); s.append(text).appendEndTag(HEADLINE1).append("\n"); } 323 324 private void headline1(String text) { 325 headline1(text, null); 326 } 327 328 private void headline2(String text) { 329 s.append("\n").appendStartTag(PAR).appendStartTag(HEADLINE2).append(text).appendEndTag(HEADLINE2).appendEndTag(PAR).append("\n"); } 331 332 private void listitem(RefList s, String text) { 333 s.appendStartTag(LIST_ITEM).append("<tt>").append(text).append("</tt>").appendEndTag(LIST_ITEM).append("\n"); } 335 336 338 339 340 341 344 private RefList getRefList(String element) { 345 346 RefList toret = (RefList) elementRefs.get(element); 347 348 if (toret == null) { 349 toret = new RefList(); 350 elementRefs.put(element, toret); 351 } 352 353 return toret; 354 } 355 356 357 360 private class RefList extends LinkedList { 361 362 363 private static final long serialVersionUID = 4291872863957329823L; 364 365 368 public boolean add(Object obj) { 369 if (obj instanceof StringBuffer || obj instanceof RefList || obj instanceof String ) { 370 return super.add(obj); 371 } else { 372 throw new ClassCastException ("String or RefList required."); } 374 } 375 376 public RefList append(String s) { 377 if (size() != 0 && (getLast() instanceof StringBuffer )) { 378 StringBuffer last = (StringBuffer ) getLast(); 379 last.append(s); 380 } else { 381 add(new StringBuffer (s)); 382 } 383 return this; 384 } 385 386 public RefList append(RefList s) { 387 add(s); 388 return this; 389 } 390 391 392 public RefList appendUniq(String fragment) { 393 if (!contains(fragment)) add(fragment); 394 return this; 395 } 396 397 RefList appendStartTag(String tag) { 399 return append("<" + tag + ">"); } 401 402 RefList appendEndTag(String tag) { 403 return append("</" + tag + ">"); } 405 406 407 public String toString() { 408 Iterator it = iterator(); 409 StringBuffer buf = new StringBuffer (); 410 while(it.hasNext()) { 411 buf.append(it.next().toString()); 412 } 413 return buf.toString(); 414 } 415 416 } 417 } 418 | Popular Tags |