-
Notifications
You must be signed in to change notification settings - Fork 1
/
QuoteSaxHandler.java
114 lines (99 loc) · 2.71 KB
/
QuoteSaxHandler.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.*;
/**
* SAX handler for SAX Parser
* @author Mongkoldech Rajapakdee & Jeff Offutt
* Date: Nov 2009
* Uses the sax parser to parse an XML file into a list of quotes
*/
public class QuoteSaxHandler extends DefaultHandler
{
private QuoteList quoteList = new QuoteList();
private Quote quoteTmp = null; // temporary Quote
private String currentElement = null; // current element name
// Node names in XML file
private final String QuoteListElem = "quote-list";
private final String QuoteElem = "quote";
private final String QuoteAuthorElem = "author";
private final String id = "id";
private final String keywords = "keywords";
private final String QuoteTextElem = "quote-text";
public QuoteSaxHandler()
{
super();
}
public QuoteList getQuoteList()
{
return quoteList;
}
@Override
public void startDocument ()
{
// System.out.println ("Start document"); // For testing
}
@Override
public void endDocument ()
{
// System.out.println ("End document"); // For testing
}
@Override
public void startElement (String uri, String name, String qName, Attributes atts)
{
if (qName.equalsIgnoreCase (QuoteListElem))
{
currentElement = QuoteListElem;
}
else if (qName.equalsIgnoreCase(QuoteElem))
{
currentElement = QuoteElem;
quoteTmp = new Quote();
}
else if (qName.equalsIgnoreCase (QuoteAuthorElem))
{
currentElement = QuoteAuthorElem;
}
else if (qName.equalsIgnoreCase (QuoteTextElem))
{
currentElement = QuoteTextElem;
}
else if(qName.equalsIgnoreCase(id)){
currentElement = id;
}
else if(qName.equalsIgnoreCase(keywords)){
currentElement = keywords;
}
}
@Override
public void endElement (String uri, String name, String qName)
{
if (qName.equalsIgnoreCase (QuoteElem))
{
quoteList.setQuote (quoteTmp);
quoteTmp = null;
}
}
@Override
public void characters (char ch[], int start, int length)
{
String value = new String (ch, start, length);
if (!value.trim().equals(""))
{
if (currentElement.equalsIgnoreCase (QuoteTextElem))
{
quoteTmp.setQuoteText (value);
}
else if (currentElement.equalsIgnoreCase (QuoteAuthorElem))
{
quoteTmp.setAuthor (value);
}
else if (currentElement.equalsIgnoreCase (id))
{
quoteTmp.setId (value);
}
else if (currentElement.equalsIgnoreCase (keywords))
{
quoteTmp.setKeywords ( Engine.strToArray(value).toArray(new String[Engine.strToArray(value).size()]));
}
}
}
}