View Javadoc
1   /*
2    * JBoss, Home of Professional Open Source
3    * Copyright 2014, Red Hat, Inc. and/or its affiliates, and individual
4    * contributors by the @authors tag. See the copyright.txt in the
5    * distribution for a full listing of individual contributors.
6    *
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   * http://www.apache.org/licenses/LICENSE-2.0
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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   * Implementation of parser based on JAXP DOM(W3C).
39   * 
40   * @author baranowb
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      // Inject instance of error holder
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 }