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