1 19 20 package org.netbeans.modules.tasklist.bugs.scarab; 21 22 import org.xml.sax.*; 23 24 import java.util.*; 25 26 import java.io.*; 27 import java.text.SimpleDateFormat ; 28 import org.xml.sax.helpers.DefaultHandler ; 29 30 37 final class ScarabXMLHandler extends DefaultHandler { 38 39 40 private static final String DTD_VERSION = "$Revision: 1.2 $"; 41 42 43 private static final String DTD_VERSION_NAME = "dtd_version"; 44 45 private static final String SCARAB_ISSUES = "scarab-issues"; 46 private static final String ISSUE = "issue"; 47 private static final String ACTIVITY_SET = "activity-set"; 48 private static final String ACTIVITY = "activity"; 49 private static final String ATTACHMENT = "attachment"; 50 51 private boolean inIssue = false; 52 private boolean inActivitySet = false; 53 private boolean inActivity = false; 54 private boolean inActivitySetForCreateIssue = false; 55 56 private StringBuffer buffer = new StringBuffer (); 57 58 62 private List issues; 63 64 65 private Issue issue; 66 67 68 private ArrayList openedTags; 69 70 private String dateFormat; 71 private String attribute; 72 73 74 public ScarabXMLHandler() { 75 } 76 77 78 public void setDocumentLocator (org.xml.sax.Locator locator) { 79 } 80 81 82 public void startDocument () 83 throws org.xml.sax.SAXException { 84 85 issues = new ArrayList(); 86 openedTags = new ArrayList(); 87 } 88 89 90 public void endDocument () 91 throws org.xml.sax.SAXException { 92 } 93 94 public void startElement (final String n, 95 final String l, 96 final String q, 97 final org.xml.sax.Attributes atts) 98 throws org.xml.sax.SAXException { 99 100 openedTags.add(q); 101 102 if (q.equals(SCARAB_ISSUES)) { 103 checkDTDVersion(atts); 104 105 } else if ( q.equals(ISSUE)) { 106 inIssue = true; 107 issue = new Issue(); 108 issues.add(issue); 109 110 } else if ( inIssue && q.equals(ACTIVITY_SET)) { 111 inActivitySet = true; 112 113 } else if ( inActivitySet && q.equals(ACTIVITY)) { 114 inActivity = true; 115 } 116 } 117 118 public void endElement (final String n, 119 final String l, 120 final String q) 121 throws org.xml.sax.SAXException { 122 123 if (!currentTag().equals(q)) { 124 throw new SAXException( 125 "An error while parsing the XML file near the closing "+q+" tag"); 126 } 127 openedTags.remove(openedTags.size() - 1); 128 129 if (q.equals(SCARAB_ISSUES)) { 130 131 } else if (q.equals(ISSUE)) { 132 inIssue = false; 133 134 } else if ( inIssue && !inActivitySet && q.equals("id") ){ 135 issue.setAttribute(Issue.ISSUE_ID,buffer.toString().trim()); 136 137 } else if ( inIssue && q.equals("artifact-type") ){ 138 issue.setAttribute(Issue.ISSUE_TYPE,buffer.toString().trim()); 139 140 } else if ( inActivitySet && q.equals("type")) { 141 final String type = buffer.toString().trim(); 142 if( "Create Issue".equalsIgnoreCase(type) ){ 143 inActivitySetForCreateIssue = true; 144 } 145 146 } else if ( inActivitySetForCreateIssue && q.equals("created-by") ){ 147 issue.setAttribute(Issue.REPORTER, buffer.toString().trim()); 148 149 } else if ( inActivitySetForCreateIssue && q.equals("format") ){ 150 dateFormat = buffer.toString().trim(); 151 152 } else if ( inActivitySetForCreateIssue && q.equals("timestamp") ){ 153 issue.setAttribute(Issue.CREATED,toDate(buffer.toString().trim(),dateFormat)); 154 inActivitySetForCreateIssue = false; 155 156 } else if ( inActivity && q.equals("attribute") ){ 157 attribute = buffer.toString().trim(); 158 159 } else if ( inActivity && q.equals("new-value") ){ 160 if( buffer.toString() != null ){ 161 issue.setAttribute(attribute,buffer.toString().trim()); 162 } 163 164 } else if ( inActivity && q.equals("new-option") ){ 165 if( buffer.toString() != null ){ 166 issue.setAttribute(attribute,buffer.toString().trim()); 167 } 168 169 } else if ( inIssue && q.equals(ACTIVITY_SET)) { 170 inActivitySet = false; 171 172 } else if ( inActivitySet && q.equals(ACTIVITY)) { 173 inActivity = false; 174 175 } 176 buffer.setLength(0); 177 } 178 179 180 private String currentTag() { 181 if (openedTags.size() == 0) { 182 return null; 183 } 184 return (String ) openedTags.get(openedTags.size()-1); 185 } 186 187 193 public List getIssueList() { 194 return issues; 195 } 196 197 198 202 private void checkDTDVersion(final Attributes atts) throws SAXException { 203 204 final String dtdVersion = atts.getValue(DTD_VERSION_NAME); 205 if ( (dtdVersion == null) || (!dtdVersion.equals(DTD_VERSION))) { 206 System.out.println("Warning: Wrong DTD version: " + dtdVersion 209 + "; expected " + DTD_VERSION); 210 } 211 } 212 213 215 public java.util.Date toDate (final String date, final String format) { 216 217 final SimpleDateFormat dateFormat = new SimpleDateFormat (format); 218 try { 219 return dateFormat.parse (date); 220 } catch (java.text.ParseException ex) { 221 ex.printStackTrace(); 222 return null; 223 } 224 } 225 226 227 public void characters (char ch[], int start, int length) 228 throws org.xml.sax.SAXException { 229 230 final String s = new String (ch, start, length); 231 buffer.append(s); 232 } 233 234 235 public void ignorableWhitespace (char ch[], int start, int length) 236 throws org.xml.sax.SAXException { 237 } 238 239 240 public void processingInstruction (String target, String data) 241 throws org.xml.sax.SAXException { 242 } 243 244 } 245 | Popular Tags |