source: vlo/trunk/vlo_importer/src/main/java/eu/clarin/cmdi/vlo/config/ConfigFilePersister.java @ 2774

Last change on this file since 2774 was 2774, checked in by keeloo, 11 years ago

Corrected typos in config package. Added list initialisation to VloConfig?. Also fixed ticket 297, Access to packaged National projects mapping file. Finally, made provisions displaying the landing page in the web application.

File size: 5.6 KB
Line 
1
2package eu.clarin.cmdi.vlo.config;
3
4import java.io.File;
5import java.io.InputStream;
6import org.simpleframework.xml.core.Persister;
7
8/**
9 * Mapping of XML definitions to class members<br><br>
10 *
11 * A ConfigFilePersister class object can, by means of the Simple framework,
12 * interpret definitions in an XML file and assign the values defined to members
13 * in a class that have been annotated according to the Simple framework
14 * specifications. Next to reading, a persister object can also turn annotated
15 * members and their values into an XML definition and write it to a
16 * file.<br><br>
17 *
18 * Annotating a class amounts to prepending annotations to members in it. When
19 * the framework applies an XML file containing the element<br><br>
20 *
21 * {@literal <text>this is an example</text>}<br><br>
22 *
23 * to a class containing<br><br>
24 *
25 * {@literal @Element}<br>
26 * {@literal String text;}<br><br>
27 *
28 * the value of this member will be equal to<br><br>
29 *
30 * {@literal 'this is an example'}<br><br>
31 *
32 * Apart from elements, the Simple framework can also generate attributes and
33 * lists. For more examples, please refer to the Simple web site.<br><br>
34 *
35 * Checklist<br><br>
36 *
37 * A ConfigFilePersister object can successfully process a message sent
38 * to it successfully, only if it's logger member has been initialized. In other
39 * words: before using a persister object, the interface defined here should
40 * be implemented and passed to the object.
41 *
42 * @author keeloo
43 */
44public class ConfigFilePersister {
45
46    /**
47     * Interface to a logger object<br><br>
48     *
49     * Definition of what the ConfigFilePersister class expects from a method
50     * that takes care of logging. All messages that will be send to a logger
51     * object are rated as severe.
52     */
53    public interface Logger {
54
55        /**
56         * @param object message to be logged
57         */
58        public void log(Object object);
59    }
60   
61    /**
62     * The type of the annotated object
63     */
64    private Class configClass;
65   
66    /**
67     * The annotated object
68     */
69    private Object configObject;
70   
71    /**
72     * The absolute name of the XML file defining the members of the annotated
73     * class.
74     */
75    private String fileName;
76   
77    /**
78     * Interface object taking care of logging<br><br>
79     *
80     * Before sending a ConfigFromFile or ConfigToFile message to a
81     * ConfigFilePersister object, initialize the logger member by sending the
82     * object a setLogger message first.
83     */
84    private static Logger logger;
85   
86    /**
87     * Interface object initialization
88     *
89     * @param someLogger
90     */
91    public static void setLogger (Logger someLogger){
92        logger = someLogger;
93    }
94   
95    private Persister persister;
96
97    /**
98     * Constructor method
99     *
100     * @param object an object whose annotated members will be initialized from
101     * or written to definitions in the XML file. On reading, an object may be
102     * equal to null.<br>
103     *
104     * @param name the name of the XML file which an annotated object is read
105     * from or written to.<br>
106     *
107     */
108    public ConfigFilePersister(Object object, String name) {
109        // associate the name of the file and the object with the persister
110        fileName = name;
111        configObject = object;
112       
113        // create the Simple framework object that ensures persistance
114        persister = new Persister();
115
116        // remember the class to which the object belongs
117        configClass = object.getClass();
118    }
119
120    /**
121     * Read definitions from an XML file<br><br>
122     *
123     * Assign the values defined in the elements of the XML file to the
124     * annotated members of the object.<br><br>
125     *
126     * If the file cannot be opened or the annotation of the class does not
127     * conform to the specifications of the Simple framework, an exception will
128     * be raised, and an error will be logged.<br><br>
129     *
130     * @return the object if the file conforms to the specification, null
131     * otherwise
132     */
133    public Object ConfigFromFile() {
134       
135        Object object = null;
136
137        // try to resolve the absolute name of the configuration file to a stream
138        InputStream sourceAsStream;
139        sourceAsStream = ConfigFilePersister.class.getResourceAsStream(fileName);
140
141        if (sourceAsStream == null) {
142           
143            // the resource cannot be found inside the package, try outside
144            File sourceAsFile; 
145           
146            sourceAsFile = new File(fileName);
147            try {
148                object = persister.read(configClass, sourceAsFile, true);
149            } catch (Exception e) {
150                logger.log(e);
151            }
152           
153        } else {
154            // the resource can be found in eu.clarin.cmdi.vlo.config
155            try {
156                object = persister.read(configClass, sourceAsStream, true);
157            } catch (Exception e) {
158                logger.log(e);
159            }
160        }
161
162        return object;
163    }
164
165    /**
166     * Write definitions to an XML file<br><br>
167     *
168     * Write the values of the annotated members of the object passed to the
169     * constructor to an XML file.<br><br>
170     *
171     * If the file cannot be created or the annotation of the class does not
172     * conform to the specifications of the Simple framework, an exception will
173     * be raised, and an error will be logged.
174     */
175    public void ConfigToFile() {
176
177        File configTarget;
178        configTarget = new File(fileName);
179
180        try {
181            persister.write(configObject, configTarget);
182        } catch (Exception e) {
183            logger.log(e);
184        }
185    }
186}
Note: See TracBrowser for help on using the repository browser.