KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > netbeans > modules > tasklist > bugs > scarab > ScarabXMLHandler


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.scarab;
21
22 import org.xml.sax.*;
23
24 import java.util.*;
25
26 import java.io.*;
27 import java.text.SimpleDateFormat JavaDoc;
28 import org.xml.sax.helpers.DefaultHandler JavaDoc;
29
30 /**
31  * The handler for parsing files containing XML representations of Scarab
32  * bugs.
33  *
34  * @author mick
35  * @version 1.0
36  */

37 final class ScarabXMLHandler extends DefaultHandler JavaDoc {
38
39     /** The DTD version this parser is capable to work with. */
40     private static final String JavaDoc DTD_VERSION = "$Revision: 1.2 $";
41
42     /** Name of the dtd_version attribute of the issuezilla tag */
43     private static final String JavaDoc DTD_VERSION_NAME = "dtd_version";
44     
45     private static final String JavaDoc SCARAB_ISSUES = "scarab-issues";
46     private static final String JavaDoc ISSUE = "issue";
47     private static final String JavaDoc ACTIVITY_SET = "activity-set";
48     private static final String JavaDoc ACTIVITY = "activity";
49     private static final String JavaDoc 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 JavaDoc buffer = new StringBuffer JavaDoc();
57     
58     /**
59      * List of bugs created from the XML file. Items of the list are Maps
60      * containing the name/value pairs for the bug attributes.
61      */

62     private List issues;
63     
64     /** The name/value pairs for the currently parsed bug */
65     private Issue issue;
66     
67     /** Structure of currently opened tags */
68     private ArrayList openedTags;
69
70     private String JavaDoc dateFormat;
71     private String JavaDoc attribute;
72
73     /** Creates new IssuezillaXMLHandler */
74     public ScarabXMLHandler() {
75     }
76
77     
78     public void setDocumentLocator (org.xml.sax.Locator JavaDoc locator) {
79     }
80
81     
82     public void startDocument ()
83             throws org.xml.sax.SAXException JavaDoc {
84         
85         issues = new ArrayList();
86         openedTags = new ArrayList();
87     }
88
89     
90     public void endDocument ()
91             throws org.xml.sax.SAXException JavaDoc {
92     }
93     
94     public void startElement (final String JavaDoc n,
95                 final String JavaDoc l,
96                 final String JavaDoc q,
97                 final org.xml.sax.Attributes JavaDoc atts)
98             throws org.xml.sax.SAXException JavaDoc {
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 JavaDoc n,
119                 final String JavaDoc l,
120                 final String JavaDoc q)
121             throws org.xml.sax.SAXException JavaDoc {
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 JavaDoc 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     /** Gets the current tag */
180     private String JavaDoc currentTag() {
181         if (openedTags.size() == 0) {
182             return null;
183         }
184         return (String JavaDoc) openedTags.get(openedTags.size()-1);
185     }
186     
187     /**
188      * Gets the List of the bugs which is created during the parsing process.
189      * Must be called after the parsing (othervise returns null)
190      *
191      * @return a List containing the Maps of name/value pairs for the bugs
192      */

193     public List getIssueList() {
194         return issues;
195     }
196     
197     
198     /**
199      * Checks whether the right DTD version is used. If not a SAXException
200      * to that effect is thrown.
201      */

202     private void checkDTDVersion(final Attributes atts) throws SAXException {
203         
204         final String JavaDoc dtdVersion = atts.getValue(DTD_VERSION_NAME);
205         if ( (dtdVersion == null) || (!dtdVersion.equals(DTD_VERSION))) {
206             //throw new SAXException("Wrong DTD version " + dtdVersion
207
// + "; expected " + DTD_VERSION);
208
System.out.println("Warning: Wrong DTD version: " + dtdVersion
209                                 + "; expected " + DTD_VERSION);
210         }
211     }
212     
213     /** Converts a string to date
214      */

215     public java.util.Date JavaDoc toDate (final String JavaDoc date, final String JavaDoc format) {
216         
217         final SimpleDateFormat JavaDoc dateFormat = new SimpleDateFormat JavaDoc (format);
218         try {
219             return dateFormat.parse (date);
220         } catch (java.text.ParseException JavaDoc 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 JavaDoc {
229         
230         final String JavaDoc s = new String JavaDoc(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 JavaDoc {
237     }
238
239     
240     public void processingInstruction (String JavaDoc target, String JavaDoc data)
241     throws org.xml.sax.SAXException JavaDoc {
242     }
243         
244 }
245
Popular Tags