1 /* 2 * Copyright 2012 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.Arguments; 26 import de.smartics.util.lang.NullArgumentException; 27 28 /** 29 * Provides access to configurations for the Bugzilla plugin. 30 */ 31 @ThreadSafe 32 public final class ConfigurationLoader 33 { 34 // ********************************* Fields ********************************* 35 36 // --- constants ------------------------------------------------------------ 37 38 // --- members -------------------------------------------------------------- 39 40 /** 41 * The XML loader. 42 */ 43 private final XStream xstream; 44 45 // ****************************** Initializer ******************************* 46 47 // ****************************** Constructors ****************************** 48 49 /** 50 * Default constructor. 51 */ 52 public ConfigurationLoader() 53 { 54 xstream = new XStream(); 55 xstream.alias("bugzilla-config", Configuration.class); 56 xstream.alias("component", Component.class); 57 58 xstream.useAttributeFor(Configuration.class, "id"); 59 60 xstream.aliasField("default-components", Configuration.class, 61 "defaultComponents"); 62 63 final NormalizeWhitespaceStringConverter normConverter = 64 new NormalizeWhitespaceStringConverter(); 65 xstream.registerLocalConverter(Component.class, "description", 66 normConverter); 67 } 68 69 // ****************************** Inner Classes ***************************** 70 71 // ********************************* Methods ******************************** 72 73 // --- init ----------------------------------------------------------------- 74 75 // --- factory -------------------------------------------------------------- 76 77 /** 78 * Loads an XML file conforming to 79 * <code>http://www.smartics.de/schema/bugzilla-configuration/1</code> from 80 * the given stream. 81 * 82 * @param input the stream to load the XML document. 83 * @return the configuration instance loaded from the stream. 84 * @throws NullArgumentException if {@code input} is <code>null</code>. 85 * @throws XStreamException if the configuration cannot be loaded. 86 */ 87 public Configuration load(final InputStream input) 88 throws NullArgumentException, XStreamException 89 { 90 Arguments.checkNotNull("input", input); 91 92 final Configuration config = (Configuration) xstream.fromXML(input); 93 return config; 94 } 95 96 // --- get&set -------------------------------------------------------------- 97 98 // --- business ------------------------------------------------------------- 99 100 // --- object basics -------------------------------------------------------- 101 102 }