1   /***
2    * Redistribution and use of this software and associated documentation
3    * ("Software"), with or without modification, are permitted provided
4    * that the following conditions are met:
5    *
6    * 1. Redistributions of source code must retain copyright
7    *    statements and notices.  Redistributions must also contain a
8    *    copy of this document.
9    *
10   * 2. Redistributions in binary form must reproduce the
11   *    above copyright notice, this list of conditions and the
12   *    following disclaimer in the documentation and/or other
13   *    materials provided with the distribution.
14   *
15   * 3. The name "Exolab" must not be used to endorse or promote
16   *    products derived from this Software without prior written
17   *    permission of Exoffice Technologies.  For written permission,
18   *    please contact tma@netspace.net.au.
19   *
20   * 4. Products derived from this Software may not be called "Exolab"
21   *    nor may "Exolab" appear in their names without prior written
22   *    permission of Exoffice Technologies. Exolab is a registered
23   *    trademark of Exoffice Technologies.
24   *
25   * 5. Due credit should be given to the Exolab Project
26   *    (http://www.exolab.org/).
27   *
28   * THIS SOFTWARE IS PROVIDED BY EXOFFICE TECHNOLOGIES AND CONTRIBUTORS
29   * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT
30   * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
31   * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL
32   * EXOFFICE TECHNOLOGIES OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
33   * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
34   * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
35   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
36   * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
37   * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
38   * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
39   * OF THE POSSIBILITY OF SUCH DAMAGE.
40   *
41   * Copyright 2001-2004 (C) Exoffice Technologies Inc. All Rights Reserved.
42   *
43   * $Id: RequirementsLoader.java,v 1.2 2004/02/02 03:51:07 tanderson Exp $
44   */
45  package org.exolab.jmscts.requirements;
46  
47  import java.io.FileNotFoundException;
48  import java.io.InputStream;
49  import java.io.InputStreamReader;
50  
51  import org.exolab.castor.xml.MarshalException;
52  import org.exolab.castor.xml.ValidationException;
53  
54  
55  /***
56   * Helper class used to load requirements documents
57   *
58   * @version     $Revision: 1.2 $ $Date: 2004/02/02 03:51:07 $
59   * @author      <a href="mailto:tma@netspace.net.au">Tim Anderson</a>
60   * @see         Requirements
61   */
62  public final class RequirementsLoader {
63  
64      /***
65       * The root requirements resource path
66       */
67      private static final String PATH = "/org/exolab/jmscts/requirements/";
68  
69      /***
70       * The root requirements document
71       */
72      private static final String FILE = PATH + "requirements.xml";
73  
74  
75      /***
76       * Prevent construction of utility class
77       */
78      private RequirementsLoader() {
79      }
80  
81      /***
82       * Load the requirements from a requirements document
83       *
84       * @return the requirements
85       * @throws FileNotFoundException if the requirements document cannot be
86       * found
87       * @throws MarshalException if the requirements document cannot be read
88       * @throws ValidationException if the requirements document doesn't
89       * comply with the XML Schema definition
90       */
91      public static Requirements load()
92          throws FileNotFoundException, MarshalException, ValidationException {
93          Requirements result = new Requirements();
94  
95          add(result, getResourceAsStream(FILE));
96          return result;
97      }
98  
99      /***
100      * Unmarshall a requirements document, adding it to the requirements
101      *
102      * @param result the requirements
103      * @param stream the stream to the requirements document
104      * @throws FileNotFoundException if an included requirements document
105      * cannot be found
106      * @throws MarshalException if the requirements document cannot be read
107      * @throws ValidationException if the requirements document doesn't
108      * comply with the XML Schema definition
109      */
110     private static void add(Requirements result, InputStreamReader stream)
111         throws FileNotFoundException, MarshalException, ValidationException {
112 
113         Document source = Document.unmarshal(stream);
114 
115         Include[] includes = source.getInclude();
116         if (includes != null) {
117             for (int i = 0; i < includes.length; ++i) {
118                 Include include = includes[i];
119 
120                 String path = PATH + include.getPath();
121                 add(result, getResourceAsStream(path));
122             }
123         }
124 
125         Reference[] references = source.getReference();
126         if (references != null) {
127             for (int i = 0; i < references.length; ++i) {
128                 result.addReference(references[i]);
129             }
130         }
131 
132         Requirement[] requirements = source.getRequirement();
133         if (requirements != null) {
134             for (int i = 0; i < requirements.length; ++i) {
135                 result.addRequirement(requirements[i]);
136             }
137         }
138     }
139 
140     /***
141     * Helper to return a stream to a resource
142     *
143     * @param path the resource path
144     * @return a stream to the resource
145     * @throws FileNotFoundException if the resource can't be found
146     */
147     private static InputStreamReader getResourceAsStream(String path)
148         throws FileNotFoundException {
149         InputStream stream =
150             RequirementsLoader.class.getResourceAsStream(path);
151         if (stream == null) {
152             throw new FileNotFoundException("Cannot locate resource: " + path);
153         }
154         return new InputStreamReader(stream);
155     }
156 
157 }
This page was automatically generated by Maven