1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.jboss.as.quickstart.xml;
18
19 import java.io.InputStream;
20 import java.text.ParseException;
21 import java.text.SimpleDateFormat;
22 import java.util.ArrayList;
23 import java.util.Date;
24 import java.util.List;
25
26 import javax.enterprise.context.RequestScoped;
27 import javax.enterprise.inject.Alternative;
28 import javax.inject.Inject;
29 import javax.xml.parsers.SAXParser;
30 import javax.xml.parsers.SAXParserFactory;
31
32 import org.xml.sax.Attributes;
33 import org.xml.sax.SAXException;
34 import org.xml.sax.SAXParseException;
35 import org.xml.sax.helpers.DefaultHandler;
36
37
38
39
40
41
42
43 @RequestScoped
44 @Alternative
45 public class SAXXMLParser extends XMLParser {
46
47 private static final SimpleDateFormat DATE_FORMATTER = new SimpleDateFormat("yyyy-MM-dd");
48
49
50 @Inject
51 private Errors errorHolder;
52
53 private SAXParser parser;
54 private SAXHandler saxHandler;
55
56 SAXXMLParser() throws Exception {
57 SAXParserFactory factory = SAXParserFactory.newInstance();
58 factory.setValidating(false);
59 this.parser = factory.newSAXParser();
60 this.saxHandler = new SAXHandler();
61 }
62
63 @Override
64 public List<Book> parseInternal(InputStream is) throws Exception {
65
66 this.parser.parse(is, this.saxHandler);
67 return this.saxHandler.catalog;
68 }
69
70 private class SAXHandler extends DefaultHandler {
71
72 private List<Book> catalog;
73 private Book book;
74
75 private String currentElementValue;
76
77 @Override
78 public void startDocument() throws SAXException {
79 System.out.println("Parsing the document using the SAXXMLParser!");
80 this.catalog = new ArrayList<Book>();
81 this.book = null;
82 super.startDocument();
83 }
84
85 @Override
86 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
87 if (qName.equals("book")) {
88 this.book = new Book();
89 }
90 }
91
92 @Override
93 public void endElement(String uri, String localName, String qName) throws SAXException {
94 if (qName.equals("book")) {
95 this.catalog.add(this.book);
96 this.book = null;
97 } else if (qName.equals("author")) {
98
99 this.book.setAuthor(this.currentElementValue);
100 } else if (qName.equals("title")) {
101
102 this.book.setTitle(this.currentElementValue);
103 } else if (qName.equals("genre")) {
104
105 this.book.setGenre(this.currentElementValue);
106 } else if (qName.equals("price")) {
107
108 this.book.setPrice(Float.parseFloat(this.currentElementValue));
109 } else if (qName.equals("publish_date")) {
110
111 Date d;
112 try {
113
114 d = DATE_FORMATTER.parse(this.currentElementValue);
115 this.book.setPublishDate(d);
116 } catch (ParseException e) {
117 throw new RuntimeException(e);
118 }
119
120 } else if (qName.equals("description")) {
121
122 this.book.setDescription(this.currentElementValue);
123 }
124 }
125
126 @Override
127 public void characters(char[] ch, int start, int length) throws SAXException {
128 this.currentElementValue = new String(ch, start, length);
129 }
130
131 @Override
132 public void warning(SAXParseException e) throws SAXException {
133 errorHolder.addErrorMessage("warning", e);
134 super.warning(e);
135 }
136
137 @Override
138 public void error(SAXParseException e) throws SAXException {
139 errorHolder.addErrorMessage("error", e);
140 super.error(e);
141 }
142
143 @Override
144 public void fatalError(SAXParseException e) throws SAXException {
145 errorHolder.addErrorMessage("fatal error", e);
146 super.fatalError(e);
147 }
148
149 }
150 }