KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > bugs > bugzilla > BugzillaXMLHandler


1 /*
2  * The contents of this file are subject to the terms of the Common Development
3  * and Distribution License (the License). You may not use this file except in
4  * compliance with the License.
5  *
6  * You can obtain a copy of the License at http://www.netbeans.org/cddl.html
7  * or http://www.netbeans.org/cddl.txt.
8  *
9  * When distributing Covered Code, include this CDDL Header Notice in each file
10  * and include the License file at http://www.netbeans.org/cddl.txt.
11  * If applicable, add the following below the CDDL Header, with the fields
12  * enclosed by brackets [] replaced by your own identifying information:
13  * "Portions Copyrighted [year] [name of copyright owner]"
14  *
15  * The Original Software is NetBeans. The Initial Developer of the Original
16  * Software is Sun Microsystems, Inc. Portions Copyright 1997-2006 Sun
17  * Microsystems, Inc. All Rights Reserved.
18  */

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 JavaDoc;
28
29 /**
30  * The handler for parsing files containing XML representations of Issuezilla
31  * bugs.
32  *
33  * tor@netbeans.org:
34  * This class is virtually identical to
35  * nbbuild/antsrc/org/netbeans/nbbuild/IssuezillaXMLHandler.java
36  * At first, I inclouded its class file directly as part of
37  * the build. However, treating Issuezilla as a black box
38  * didn't work well because when connections fail (and are
39  * retried), or even during a query, there is no feedback - and
40  * since issuezilla is so slow, it's hard to know in the GUI
41  * that things are working. Therefore, I've modified the java
42  * file to give us a little bit more feedback.
43  * In CVS I stored the original file as the first revision,
44  * so you can easily diff to see what has changed - and generate
45  * a patch which you can then apply to an updated version
46  * of nbbuild/antsrc/ to keep the two in sync.
47  *
48  * serff@netbeans.org:
49  * This class is almost exactally the same as IzzuezillaXMLHandler but modified
50  * to work with Bugzilla. I basically just changed the column/field names.
51  *
52  * @author ibradac
53  * @version 1.0
54  */

55 final class BugzillaXMLHandler extends HandlerBase {
56     
57     /** The DTD version this parser is capable to work with. */
58     private static final String JavaDoc DTD_VERSION = "$Revision: 1.2 $";
59     
60     /** Name of the dtd_version attribute of the issuezilla tag */
61     private static final String JavaDoc DTD_VERSION_NAME = "dtd_version";
62     
63     private static final String JavaDoc BUG_STATUS = "bug_status";
64     
65     private static final String JavaDoc RESOLUTION = "resolution";
66     
67     private static final String JavaDoc PRIORITY = "priority";
68     
69     private static final String JavaDoc BUG = "bug";
70     
71     private static final String JavaDoc BUGZILLA = "bugzilla";
72     
73     private static final String JavaDoc LONG_DESC = "long_desc";
74     
75     private static final String JavaDoc WHO = "who";
76     
77     private static final String JavaDoc BUG_WHEN = "bug_when";
78     
79     private static final String JavaDoc THETEXT = "thetext";
80     
81     /**
82      * Contains names of all the XML tags which can be inside the
83      * <Issuezilla.Issue> tag. Should be probably fetched from the DTD.
84      * A constant for now.
85      */

86     private static final HashSet tagsInIssue;
87     
88     private static final List tagsInLongDesc;
89     
90     /**
91      * List of bugs created from the XML file. Items of the list are Maps
92      * containing the name/value pairs for the bug attributes.
93      */

94     private List bugs;
95     
96     /** The name/value pairs for the currently parsed bug */
97     private Map bug;
98     
99     /** Structure of currently opened tags */
100     private Vector openedTags;
101     
102     /** A StringBuffer containing the values inside the tags */
103     private StringBuffer JavaDoc buffer;
104     
105     /** A StringBuffer containing the value for the long_desc attribute */
106     private StringBuffer JavaDoc longDescBuffer;
107     
108     /** Contains the Issue.LongDescription object list */
109     private List longDescriptionList;
110     
111     private Issue.Description longDesc;
112
113     /** date format converter */
114     private SimpleDateFormat JavaDoc dateFormat;
115     /** date format converter for second format of time */
116     private SimpleDateFormat JavaDoc 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 // tagsInIssue.add(Issue.BLOCKS);
138
// tagsInIssue.add(Issue.CC);
139
// tagsInIssue.add(Issue.DEPENDS_ON);
140
// tagsInIssue.add(Issue.VOTES);
141
// tagsInIssue.add(Issue.KEYWORDS);
142
// tagsInIssue.add("long_desc");
143

144         tagsInLongDesc = new ArrayList();
145         tagsInLongDesc.add(Issue.Description.WHO);
146         tagsInLongDesc.add(BUG_WHEN);
147         tagsInLongDesc.add(Issue.Description.THETEXT);
148     }
149     
150     /** Creates new IssuezillaXMLHandler */
151     public BugzillaXMLHandler() {
152     }
153
154     
155     public void setDocumentLocator (org.xml.sax.Locator JavaDoc locator) {
156     }
157
158     
159     public void startDocument ()
160     throws org.xml.sax.SAXException JavaDoc {
161         bugs = new ArrayList();
162         openedTags = new Vector();
163     }
164
165     
166     public void endDocument ()
167     throws org.xml.sax.SAXException JavaDoc {
168     }
169     
170     public void startElement (String JavaDoc name, org.xml.sax.AttributeList JavaDoc atts)
171     throws org.xml.sax.SAXException JavaDoc {
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             // also CC is multivalue
181
// bug.put(Issue.CC, new ArrayList ());
182
// bug.put(Issue.DEPENDS_ON, new ArrayList ());
183
// bug.put(Issue.BLOCKS, new ArrayList ());
184
bug.put(Issue.CREATED, new java.util.Date JavaDoc (0));
185             //longDescBuffer = new StringBuffer();
186
dealIssueAttributes(atts);
187         } else if (tagsInIssue.contains(name)) {
188             buffer = new StringBuffer JavaDoc();
189         } else if (tagsInLongDesc.contains(name)) {
190             longDescBuffer = new StringBuffer JavaDoc();
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 JavaDoc name)
199     throws org.xml.sax.SAXException JavaDoc {
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 JavaDoc prev = bug.get (name);
210             if (prev instanceof List) {
211                 // expecting multivalue
212
((List)prev).add (buffer.toString ());
213             } else if (prev instanceof Date) {
214                 // convert to date
215
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             //longDescriptionList.add(longDesc);
222
} else if (tagsInLongDesc.contains(name)) {
223             String JavaDoc 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 JavaDoc {
236         String JavaDoc s = new String JavaDoc(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 JavaDoc {
250     }
251
252     
253     public void processingInstruction (String JavaDoc target, String JavaDoc data)
254     throws org.xml.sax.SAXException JavaDoc {
255     }
256     
257     /** Gets the current tag */
258     private String JavaDoc currentTag() {
259         if (openedTags.size() == 0) {
260             return null;
261         }
262         return (String JavaDoc) openedTags.lastElement();
263     }
264     
265     /**
266      * Gets the List of the bugs which is created during the parsing process.
267      * Must be called after the parsing (othervise returns null)
268      *
269      * @return a List containing the Maps of name/value pairs for the bugs
270      */

271     public List getBugList() {
272         return bugs;
273     }
274     
275     /**
276      * Sets the priority and resolution attributes and checks the DTD version */

277     private void dealIssueAttributes(AttributeList atts)
278     throws SAXException {
279         String JavaDoc priority = atts.getValue(PRIORITY);
280         String JavaDoc issue_status = atts.getValue(BUG_STATUS);
281         String JavaDoc 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     /**
293      * Checks whether the right DTD version is used. If not a SAXException
294      * to that effect is thrown.
295      */

296     private void checkDTDVersion(AttributeList atts) throws SAXException {
297         String JavaDoc dtdVersion = atts.getValue(DTD_VERSION_NAME);
298         if ( (dtdVersion == null) || (!dtdVersion.equals(DTD_VERSION))) {
299             //throw new SAXException("Wrong DTD version " + dtdVersion
300
// + "; expected " + DTD_VERSION);
301
//System.out.println("Warning: Wrong DTD version: " + dtdVersion
302
// + "; expected " + DTD_VERSION);
303
}
304     }
305     
306     /** Converts a string to date
307      */

308     public java.util.Date JavaDoc toDate (String JavaDoc date) {
309         if (dateFormat == null) {
310             dateFormat = new SimpleDateFormat JavaDoc ("yyyy-mm-dd hh:mm:ss");
311         }
312         if (dateFormat2 == null) {
313             dateFormat2 = new SimpleDateFormat JavaDoc ("yyyy-mm-dd hh:mm");
314         }
315         try {
316             return dateFormat.parse (date);
317         } catch (java.text.ParseException JavaDoc ex1) {
318             try {
319                 return dateFormat2.parse (date);
320             } catch (java.text.ParseException JavaDoc ex) {
321                 ex.printStackTrace();
322                 return null;
323             }
324         }
325     }
326 }
327
Popular Tags