KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > bugs > issuezilla > IssuezillaXMLHandler


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.issuezilla;
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  * @author ibradac
49  * @version 1.0
50  */

51 final class IssuezillaXMLHandler extends HandlerBase {
52     
53     /** The DTD version this parser is capable to work with. */
54     private static final String JavaDoc DTD_VERSION = "$Revision: 1.2 $";
55     
56     /** Name of the dtd_version attribute of the issuezilla tag */
57     private static final String JavaDoc DTD_VERSION_NAME = "dtd_version";
58     
59     private static final String JavaDoc ISSUE_STATUS = "issue_status";
60     
61     private static final String JavaDoc RESOLUTION = "resolution";
62     
63     private static final String JavaDoc PRIORITY = "priority";
64     
65     private static final String JavaDoc ISSUE = "issue";
66     
67     private static final String JavaDoc ISSUEZILLA = "issuezilla";
68     
69     private static final String JavaDoc LONG_DESC = "long_desc";
70     
71     private static final String JavaDoc WHO = "who";
72     
73     private static final String JavaDoc ISSUE_WHEN = "issue_when";
74     
75     private static final String JavaDoc THETEXT = "thetext";
76     
77     /**
78      * Contains names of all the XML tags which can be inside the
79      * <Issuezilla.Issue> tag. Should be probably fetched from the DTD.
80      * A constant for now.
81      */

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

90     private List bugs;
91     
92     /** The name/value pairs for the currently parsed bug */
93     private Map bug;
94     
95     /** Structure of currently opened tags */
96     private Vector openedTags;
97     
98     /** A StringBuffer containing the values inside the tags */
99     private StringBuffer JavaDoc buffer;
100     
101     /** A StringBuffer containing the value for the long_desc attribute */
102     private StringBuffer JavaDoc longDescBuffer;
103     
104     /** Contains the Issue.LongDescription object list */
105     private List longDescriptionList;
106     
107     private Issue.Description longDesc;
108
109     /** date format converter */
110     private SimpleDateFormat JavaDoc dateFormat;
111     /** date format converter for second format of time */
112     private SimpleDateFormat JavaDoc dateFormat2;
113     
114     
115     static {
116         tagsInIssue = new HashSet();
117         tagsInIssue.add("issue_id");
118         //tagsInIssue.add("issue_status");
119
tagsInIssue.add("component"); //tagsInIssue.add("product");
120
//tagsInIssue.add("priority");
121
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("component");
126
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("resolution");
134
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         //tagsInIssue.add("long_desc");
141

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     /** Creates new IssuezillaXMLHandler */
149     public IssuezillaXMLHandler() {
150     }
151
152     
153     public void setDocumentLocator (org.xml.sax.Locator JavaDoc locator) {
154     }
155
156     
157     public void startDocument ()
158     throws org.xml.sax.SAXException JavaDoc {
159         bugs = new ArrayList();
160         openedTags = new Vector();
161     }
162
163     
164     public void endDocument ()
165     throws org.xml.sax.SAXException JavaDoc {
166     }
167     
168     public void startElement (String JavaDoc name, org.xml.sax.AttributeList JavaDoc atts)
169     throws org.xml.sax.SAXException JavaDoc {
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             // also CC is multivalue
179
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 JavaDoc (0));
183             //longDescBuffer = new StringBuffer();
184
dealIssueAttributes(atts);
185         } else if (tagsInIssue.contains(name)) {
186             buffer = new StringBuffer JavaDoc();
187         } else if (tagsInLongDesc.contains(name)) {
188             longDescBuffer = new StringBuffer JavaDoc();
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 JavaDoc name)
197     throws org.xml.sax.SAXException JavaDoc {
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 JavaDoc prev = bug.get (name);
208             if (prev instanceof List) {
209                 // expecting multivalue
210
((List)prev).add (buffer.toString ());
211             } else if (prev instanceof Date) {
212                 // convert to date
213
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             //longDescriptionList.add(longDesc);
220
} else if (tagsInLongDesc.contains(name)) {
221             String JavaDoc 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 JavaDoc {
234         String JavaDoc s = new String JavaDoc(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 JavaDoc {
248     }
249
250     
251     public void processingInstruction (String JavaDoc target, String JavaDoc data)
252     throws org.xml.sax.SAXException JavaDoc {
253     }
254     
255     /** Gets the current tag */
256     private String JavaDoc currentTag() {
257         if (openedTags.size() == 0) {
258             return null;
259         }
260         return (String JavaDoc) openedTags.lastElement();
261     }
262     
263     /**
264      * Gets the List of the bugs which is created during the parsing process.
265      * Must be called after the parsing (othervise returns null)
266      *
267      * @return a List containing the Maps of name/value pairs for the bugs
268      */

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

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

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

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