1 /*
2 * Copyright 2007-2008 the original author or authors.
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
17 package org.gwe.utils.xstream;
18
19 import java.io.File;
20 import java.io.FileInputStream;
21 import java.io.FileNotFoundException;
22 import java.io.IOException;
23 import java.io.InputStream;
24
25 import com.thoughtworks.xstream.XStream;
26 import com.thoughtworks.xstream.io.xml.DomDriver;
27
28 /**
29 * @author Marco Ruiz
30 * @since Jan 15, 2009
31 */
32 public class XMLConfigFile<MODEL_TYPE> {
33
34 private MODEL_TYPE model;
35
36 public XMLConfigFile(String fileName, AliasTransf... transformations) throws FileNotFoundException {
37 XStream result = new XStream(new DomDriver());
38 for (AliasTransf transf : transformations) transf.transform(result);
39 // result.registerConverter(new RealmAccessConverter());
40
41 // InputStream is = getClass().getClassLoader().getResourceAsStream(fileName);
42 InputStream is = null;
43 if (is == null) is = new FileInputStream(new File(fileName));
44 model = (MODEL_TYPE) result.fromXML(is);
45 try {
46 is.close();
47 } catch (IOException e) {
48 // TODO Auto-generated catch block
49 e.printStackTrace();
50 }
51 }
52
53 public MODEL_TYPE getModel() {
54 return model;
55 }
56 }
57