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.BufferedReader;
20  import java.io.ByteArrayInputStream;
21  import java.io.CharArrayReader;
22  import java.io.InputStream;
23  import java.io.InputStreamReader;
24  import java.net.URL;
25  import java.util.List;
26  
27  import javax.inject.Inject;
28  import javax.xml.transform.Source;
29  import javax.xml.transform.stream.StreamSource;
30  import javax.xml.validation.SchemaFactory;
31  import javax.xml.validation.Validator;
32  
33  /**
34   * Simple class to provide base for XML file parsing. It will validate any document using the schema produced by
35   * {@link Constants}
36   * 
37   * @author baranowb
38   * 
39   */
40  public abstract class XMLParser {
41  
42      @Inject
43      private Errors errorHolder;
44  
45      public List<Book> parse(InputStream is) throws Exception {
46          /*
47           * Validate against schema before it triggers implementation.
48           */
49          StringBuffer xmlFile = new StringBuffer();
50          BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(is));
51          String line = null;
52          while ((line = bufferedReader.readLine()) != null) {
53              xmlFile.append(line);
54          }
55          String xml = xmlFile.toString();
56          // validate against schema.
57          try {
58              URL schema = Resources.getResource("/catalog.xsd");
59              Validator validator = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema").newSchema(schema)
60                      .newValidator();
61              Source source = new StreamSource(new CharArrayReader(xml.toCharArray()));
62              validator.validate(source);
63          } catch (Exception e) {
64              this.errorHolder.addErrorMessage("Validation Error", e);
65              return null;
66          }
67          // parse file into catalog
68          ByteArrayInputStream bais = new ByteArrayInputStream(xml.getBytes());
69          // ask extending class to parse
70          return parseInternal(bais);
71      }
72  
73      /**
74       * 
75       * @param is
76       * @return
77       * @throws Exception
78       */
79      protected abstract List<Book> parseInternal(InputStream is) throws Exception;
80  
81  }