View Javadoc

1   /*
2    * Copyright 2012-2013 smartics, Kronseder & Reiner GmbH
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package de.smartics.maven.issue.config;
17  
18  import java.io.InputStream;
19  
20  import javax.annotation.concurrent.ThreadSafe;
21  
22  import com.thoughtworks.xstream.XStream;
23  import com.thoughtworks.xstream.XStreamException;
24  
25  import de.smartics.util.lang.Arg;
26  
27  /**
28   * Provides access to configurations for the Bugzilla plugin.
29   */
30  @ThreadSafe
31  public final class ConfigurationLoader
32  {
33    // ********************************* Fields *********************************
34  
35    // --- constants ------------------------------------------------------------
36  
37    // --- members --------------------------------------------------------------
38  
39    /**
40     * The XML loader.
41     */
42    private final XStream xstream;
43  
44    // ****************************** Initializer *******************************
45  
46    // ****************************** Constructors ******************************
47  
48    /**
49     * Default constructor.
50     */
51    public ConfigurationLoader()
52    {
53      xstream = new XStream();
54      xstream.alias("bugzilla-config", Configuration.class);
55      xstream.alias("component", Component.class);
56  
57      xstream.useAttributeFor(Configuration.class, "id");
58  
59      xstream.aliasField("default-components", Configuration.class,
60          "defaultComponents");
61  
62      final NormalizeWhitespaceStringConverter normConverter =
63          new NormalizeWhitespaceStringConverter();
64      xstream.registerLocalConverter(Component.class, "description",
65          normConverter);
66    }
67  
68    // ****************************** Inner Classes *****************************
69  
70    // ********************************* Methods ********************************
71  
72    // --- init -----------------------------------------------------------------
73  
74    // --- factory --------------------------------------------------------------
75  
76    /**
77     * Loads an XML file conforming to
78     * <code>http://www.smartics.de/schema/bugzilla-configuration/1</code> from
79     * the given stream.
80     *
81     * @param input the stream to load the XML document.
82     * @return the configuration instance loaded from the stream.
83     * @throws NullPointerException if {@code input} is <code>null</code>.
84     * @throws XStreamException if the configuration cannot be loaded.
85     */
86    public Configuration load(final InputStream input)
87      throws NullPointerException, XStreamException
88    {
89      Arg.checkNotNull("input", input);
90  
91      final Configuration config = (Configuration) xstream.fromXML(input);
92      return config;
93    }
94  
95    // --- get&set --------------------------------------------------------------
96  
97    // --- business -------------------------------------------------------------
98  
99    // --- object basics --------------------------------------------------------
100 
101 }