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

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

Integrated parameter branche into trunk again

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