001    /* PAVLOV -- Multiple Choice Study System
002     * Copyright (C) 2000 - 2004 T.J. Willis
003     * 
004     * This program is free software; you can redistribute it and/or
005     * modify it under the terms of the GNU General Public License
006     * as published by the Free Software Foundation; either version 2
007     * of the License, or (at your option) any later version.
008     *      
009     * This program is distributed in the hope that it will be useful,
010     * but WITHOUT ANY WARRANTY; without even the implied warranty of
011     * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
012     * GNU General Public License for more details.
013     *          
014     * You should have received a copy of the GNU General Public License
015     * along with this program; if not, write to the Free Software
016     * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111, USA.
017     *  
018     * $Header: /cvsroot/pavlov/net/sourceforge/pavlov/user/UserXMLHandler.java,v 1.5 2004/05/19 03:12:14 tj_willis Exp $
019     */ 
020    package net.sourceforge.pavlov.user;
021    
022    import org.xml.sax.*;
023    import org.xml.sax.helpers.*;
024    import org.apache.log4j.*;
025    
026    
027    
028    /**
029     * Loads a User from an XML file.
030     * @author <a href="mailto:tj_willis@users.sourceforge.net"> T.J. Willis </a>
031     * @version 1.0
032     */
033    public final  class UserXMLHandler extends DefaultHandler {
034    
035        private User ud;
036        private BookData bd;
037        private ChapterData cd;
038        private QuizData qz;
039        private String qid;
040        private static Category cat 
041            = Category.getInstance(UserXMLHandler.class.getName());    
042    
043        // Override methods of the DefaultHandler class
044        // to gain notification of SAX Events.
045        //
046        // See org.xml.sax.ContentHandler for all available events.
047        //
048        /**
049         * Describe <code>startDocument</code> method here.
050         *
051         * @exception SAXException if an error occurs
052         */
053        public void startDocument() throws SAXException {
054            ud = new User();
055            cat.setLevel(Level.WARN);
056        }
057            
058        /**
059         * Describe <code>endDocument</code> method here.
060         *
061         * @exception SAXException if an error occurs
062         */
063        public void endDocument() throws SAXException {
064        }
065    
066        /**
067         * Describe <code>startElement</code> method here.
068         *
069         * @param namespaceURI a <code>String</code> value
070         * @param localName a <code>String</code> value
071         * @param qName a <code>String</code> value
072         * @param attr an <code>Attributes</code> value
073         * @exception SAXException if an error occurs
074         */
075        public void startElement(String namespaceURI,
076            String localName,
077            String qName,
078            Attributes attr) throws SAXException {
079    
080            if (localName.equals("USER")) {
081                String key, value;
082    
083                for (int i = 0; i < attr.getLength(); i++) {
084                    key = attr.getLocalName(i);
085                    value = attr.getValue(i);
086                    if (key.equals("SN")) ud.setName(value);
087                    else if (key.equals("PASSWORD")) ud.setPassword(value);
088                }
089            } else if (localName.equals("BOOK")) {
090                bd = new BookData();
091                String key, value;
092    
093                for (int i = 0; i < attr.getLength(); i++) {
094                    key = attr.getLocalName(i);
095                    value = attr.getValue(i);
096                    if (key.equals("TITLE")) bd.setTitle(value);
097                    //else if (key.equals("LAST")) bd.setLast(value);
098                }
099                        
100            } 
101            else if (localName.equals("QUIZ")) {
102                qz = new QuizData();
103                String key, value;
104    
105                for (int i = 0; i < attr.getLength(); i++) {
106                    key = attr.getLocalName(i);
107                    value = attr.getValue(i);
108                    if (key.equals("DATE")) qz.setStart(Long.parseLong(value));
109                    if (key.equals("RIGHT")) qz.setRight(Integer.parseInt(value));
110                    if (key.equals("WRONG")) qz.setWrong(Integer.parseInt(value));
111                    if (key.equals("TOTAL")) qz.setTotal(Integer.parseInt(value));
112                    //else if (key.equals("LAST")) bd.setLast(value);
113                }
114                        
115            } else if (localName.equals("CHAPTER")) {
116                cd = new ChapterData();
117                String key, value;
118    
119                for (int i = 0; i < attr.getLength(); i++) {
120                    key = attr.getLocalName(i);
121                    value = attr.getValue(i);
122                    if (key.equals("TITLE")) cd.setTitle(value);
123                    //else if (key.equals("LAST")) cd.setLast(value);
124                }
125                        
126            } else if (localName.equals("QUESTION")) {
127                //qd = new QuestionData();
128                String key, value;
129    
130                for (int i = 0; i < attr.getLength(); i++) {
131                    key = attr.getLocalName(i);
132                    value = attr.getValue(i);
133                    if (key.equals("SN")) qid = value;
134                    //else if (key.equals("DATE")) qd.setLast(value);
135                }
136                        
137            } else if (localName.equals("RIGHT")) {
138                String key, value;
139    
140                for (int i = 0; i < attr.getLength(); i++) {
141                    key = attr.getLocalName(i);
142                    value = attr.getValue(i);
143                    if (key.equals("DATE")) {
144                        long foo = -1;
145    
146                        try {
147                            foo = Long.parseLong(value);
148                        } catch (Exception e) {
149                            foo = -1;
150                        }
151                        assert cd != null: "Chapter is null";
152                        assert qid != null: "QuestionID is null";
153                        cd.addRight(qid, foo);
154                    }
155                }
156                        
157            } else if (localName.equals("WRONG")) {
158                String key, value;
159    
160                for (int i = 0; i < attr.getLength(); i++) {
161                    key = attr.getLocalName(i);
162                    value = attr.getValue(i);
163                    if (key.equals("DATE")) {
164                        long foo = -1;
165                        try {
166                            foo = Long.parseLong(value);
167                        } catch (Exception e) {
168                            foo = -1;
169                        }
170                        assert cd != null: "Chapter is null";
171                        assert qid != null: "QuestionID is null";
172                        cd.addWrong(qid, foo);
173                    }
174                }
175            } else if (localName.equals("BOOKS")) {
176                // DO NOTHING  
177            } else if (localName.equals("BOOKMARKS")) {
178                        // DO NOTHING  
179    
180            }
181            else {
182    
183                cat.warn("Unrecognized XML element: [ " + localName + " ]");                
184    
185                // Also, let's print the attributes if 
186                // there are any...
187                for (int i = 0; i < attr.getLength(); i++) {
188                    cat.warn(" Unrecognized XML attribute: " +
189                        attr.getLocalName(i) +
190                        " VALUE: " + 
191                        attr.getValue(i));
192                }
193                        
194            } 
195        }
196            
197        /**
198         * Describe <code>endElement</code> method here.
199         *
200         * @param namespaceURI a <code>String</code> value
201         * @param localName a <code>String</code> value
202         * @param qName a <code>String</code> value
203         * @exception SAXException if an error occurs
204         */
205        public void endElement(String namespaceURI,
206            String localName,
207            String qName) throws SAXException {
208    
209            if (localName.equals("USER")) {
210                // do nothing
211            } else if (localName.equals("BOOK")) {
212                assert ud != null: "User is null";
213                ud.addBook(bd);
214            } else if (localName.equals("QUIZ")) {
215                assert cd != null: "Chapter is null";
216                cd.addQuizData(qz);
217            } else if (localName.equals("CHAPTER")) {
218                assert bd != null: "BookData is null";
219                assert cd != null: "ChapterData is null";
220                bd.addChapter(cd);
221            } else if (localName.equals("QUESTION")) {// NOT NECESSARY
222                //cd.addQuestion(qd);
223            } else if (localName.equals("RIGHT")) {// RIGHT & WRONG ARE ALREADY CLOSED UP
224            } else if (localName.equals("WRONG")) {// RIGHT & WRONG ARE ALREADY CLOSED UP
225            } else if (localName.equals("BOOKS")) {
226                // DO NOTHING  
227            }
228            else if (localName.equals("BOOKMARKS")) {
229                        // DO NOTHING  
230            } 
231            else {
232                cat.warn("Unrecognized XML end element[ " + localName + " ]");
233            }
234        } 
235            
236        /**
237         * Describe <code>characters</code> method here.
238         *
239         * @param ch a <code>char[]</code> value
240         * @param start an <code>int</code> value
241         * @param length an <code>int</code> value
242         * @exception SAXException if an error occurs
243         */
244        public void characters(char[] ch, int start, int length) 
245            throws SAXException {
246        }
247    
248    
249        /**
250         * Return the loaded user
251         * @return an <code>User</code> value
252         */
253        public User getUser() {
254            return ud;
255        }
256    }
257