1 19 20 package org.netbeans.modules.tasklist.bugs.bugzilla; 21 22 import org.xml.sax.*; 23 24 import java.util.*; 25 26 import java.io.*; 27 import java.text.SimpleDateFormat ; 28 29 55 final class BugzillaXMLHandler extends HandlerBase { 56 57 58 private static final String DTD_VERSION = "$Revision: 1.2 $"; 59 60 61 private static final String DTD_VERSION_NAME = "dtd_version"; 62 63 private static final String BUG_STATUS = "bug_status"; 64 65 private static final String RESOLUTION = "resolution"; 66 67 private static final String PRIORITY = "priority"; 68 69 private static final String BUG = "bug"; 70 71 private static final String BUGZILLA = "bugzilla"; 72 73 private static final String LONG_DESC = "long_desc"; 74 75 private static final String WHO = "who"; 76 77 private static final String BUG_WHEN = "bug_when"; 78 79 private static final String THETEXT = "thetext"; 80 81 86 private static final HashSet tagsInIssue; 87 88 private static final List tagsInLongDesc; 89 90 94 private List bugs; 95 96 97 private Map bug; 98 99 100 private Vector openedTags; 101 102 103 private StringBuffer buffer; 104 105 106 private StringBuffer longDescBuffer; 107 108 109 private List longDescriptionList; 110 111 private Issue.Description longDesc; 112 113 114 private SimpleDateFormat dateFormat; 115 116 private SimpleDateFormat dateFormat2; 117 118 119 static { 120 tagsInIssue = new HashSet(); 121 tagsInIssue.add("bug_id"); 122 tagsInIssue.add("bug_status"); 123 tagsInIssue.add("product"); 124 tagsInIssue.add("priority"); 125 tagsInIssue.add("version"); 126 tagsInIssue.add("rep_platform"); 127 tagsInIssue.add(Issue.ASSIGNED_TO); 128 tagsInIssue.add("delta_ts"); 129 tagsInIssue.add("component"); 130 tagsInIssue.add("reporter"); 131 tagsInIssue.add("target_milestone"); 132 tagsInIssue.add("bug_severity"); 133 tagsInIssue.add("creation_ts"); 134 tagsInIssue.add("op_sys"); 135 tagsInIssue.add("resolution"); 136 tagsInIssue.add("short_desc"); 137 144 tagsInLongDesc = new ArrayList(); 145 tagsInLongDesc.add(Issue.Description.WHO); 146 tagsInLongDesc.add(BUG_WHEN); 147 tagsInLongDesc.add(Issue.Description.THETEXT); 148 } 149 150 151 public BugzillaXMLHandler() { 152 } 153 154 155 public void setDocumentLocator (org.xml.sax.Locator locator) { 156 } 157 158 159 public void startDocument () 160 throws org.xml.sax.SAXException { 161 bugs = new ArrayList(); 162 openedTags = new Vector(); 163 } 164 165 166 public void endDocument () 167 throws org.xml.sax.SAXException { 168 } 169 170 public void startElement (String name, org.xml.sax.AttributeList atts) 171 throws org.xml.sax.SAXException { 172 openedTags.addElement(name); 173 if (name.equals(BUGZILLA)) { 174 checkDTDVersion(atts); 175 } else if (name.equals(BUG)) { 176 bug = new HashMap(); 177 bugs.add(bug); 178 longDescriptionList = new ArrayList(); 179 bug.put(Issue.LONG_DESC_LIST, longDescriptionList); 180 bug.put(Issue.CREATED, new java.util.Date (0)); 185 dealIssueAttributes(atts); 187 } else if (tagsInIssue.contains(name)) { 188 buffer = new StringBuffer (); 189 } else if (tagsInLongDesc.contains(name)) { 190 longDescBuffer = new StringBuffer (); 191 } else if (name.equals(LONG_DESC)) { 192 longDesc = new Issue.Description(); 193 longDescriptionList.add(longDesc); 194 } 195 } 196 197 198 public void endElement (String name) 199 throws org.xml.sax.SAXException { 200 if (!currentTag().equals(name)) { 201 throw new SAXException( 202 "An error while parsing the XML file near the closing " + name 203 + " tag"); 204 } 205 openedTags.remove(openedTags.size() - 1); 206 if (name.equals(BUGZILLA)) { 207 208 } else if (tagsInIssue.contains(name)) { 209 Object prev = bug.get (name); 210 if (prev instanceof List) { 211 ((List)prev).add (buffer.toString ()); 213 } else if (prev instanceof Date) { 214 bug.put (name, toDate (buffer.toString())); 216 } else { 217 bug.put(name, buffer.toString()); 218 } 219 buffer = null; 220 } else if (name.equalsIgnoreCase(LONG_DESC)) { 221 } else if (tagsInLongDesc.contains(name)) { 223 String s = longDescBuffer.toString (); 224 if (name.equals (BUG_WHEN)) { 225 longDesc.setIssueWhen (toDate (s)); 226 } else { 227 longDesc.setAtribute(name, s); 228 } 229 } 230 231 } 232 233 234 public void characters (char ch[], int start, int length) 235 throws org.xml.sax.SAXException { 236 String s = new String (ch, start, length); 237 if (!s.equals("")) { 238 if (tagsInLongDesc.contains(currentTag())) { 239 longDescBuffer.append(s); 240 } else if (buffer != null) { 241 buffer.append(s); 242 } 243 } 244 245 } 246 247 248 public void ignorableWhitespace (char ch[], int start, int length) 249 throws org.xml.sax.SAXException { 250 } 251 252 253 public void processingInstruction (String target, String data) 254 throws org.xml.sax.SAXException { 255 } 256 257 258 private String currentTag() { 259 if (openedTags.size() == 0) { 260 return null; 261 } 262 return (String ) openedTags.lastElement(); 263 } 264 265 271 public List getBugList() { 272 return bugs; 273 } 274 275 277 private void dealIssueAttributes(AttributeList atts) 278 throws SAXException { 279 String priority = atts.getValue(PRIORITY); 280 String issue_status = atts.getValue(BUG_STATUS); 281 String resolution = atts.getValue(RESOLUTION); 282 if (priority == null) { 283 priority = "P0"; 284 } 285 bug.put(PRIORITY, priority); 286 bug.put(BUG_STATUS, issue_status); 287 if (resolution != null) { 288 bug.put(RESOLUTION, resolution); 289 } 290 } 291 292 296 private void checkDTDVersion(AttributeList atts) throws SAXException { 297 String dtdVersion = atts.getValue(DTD_VERSION_NAME); 298 if ( (dtdVersion == null) || (!dtdVersion.equals(DTD_VERSION))) { 299 } 304 } 305 306 308 public java.util.Date toDate (String date) { 309 if (dateFormat == null) { 310 dateFormat = new SimpleDateFormat ("yyyy-mm-dd hh:mm:ss"); 311 } 312 if (dateFormat2 == null) { 313 dateFormat2 = new SimpleDateFormat ("yyyy-mm-dd hh:mm"); 314 } 315 try { 316 return dateFormat.parse (date); 317 } catch (java.text.ParseException ex1) { 318 try { 319 return dateFormat2.parse (date); 320 } catch (java.text.ParseException ex) { 321 ex.printStackTrace(); 322 return null; 323 } 324 } 325 } 326 } 327 | Popular Tags |