1 package org.opensync.engine.server.config;
2
3 import org.opensync.engine.server.OpenSyncException;
4 import org.opensync.engine.server.OpenSync;
5 import org.opensync.engine.server.Agendas;
6 import org.opensync.engine.server.Agenda;
7 import org.opensync.engine.server.Task;
8 import org.opensync.engine.server.Schedule;
9 import org.opensync.engine.server.Synchronizations;
10 import org.opensync.engine.server.Synchronization;
11 import org.opensync.engine.server.Notification;
12
13
14 import java.io.*;
15 import java.util.*;
16 import java.text.*;
17 import org.xml.sax.*;
18 import javax.xml.parsers.*;
19 import org.xml.sax.helpers.*;
20
21 /***
22 * This class represents the parser use to parse the agendas from the config file
23 *
24 * @version 1.0
25 * @author SOFTMED
26 */
27
28 public class AgendasSxPsr extends DefaultHandler {
29 /****/
30 private static final SimpleDateFormat dateFormat = new SimpleDateFormat("dd/MM/yyyy");
31 /****/
32 private static final SimpleDateFormat timeFormat = new SimpleDateFormat("HH:mm");
33 private XMLReader parser;
34 private Agendas agendas = new Agendas();
35 private Agenda agenda;
36 private Notification notification;
37 private Synchronizations synchronizations;
38 private Task task;
39
40 /***
41 * Construct a new AgendaSxPsr
42 *
43 * @param synchronizations
44 * @exception ParserConfigurationException
45 */
46 public AgendasSxPsr(Synchronizations synchronizations)throws ParserConfigurationException{
47 try {
48 this.synchronizations = synchronizations;
49 parser = SAXParserFactory.newInstance().newSAXParser().getXMLReader();
50 parser.setContentHandler(this);
51 }
52 catch (SAXException ex) {
53 throw new RuntimeException(ex.getMessage());
54 }
55 }
56 /***
57 * Parse all agendas
58 *
59 * @param xml all agendas in a xml stream
60 * @exception IOException
61 * @exception SAXException
62 */
63 public Agendas parseAgendas(String xml)throws SAXException,IOException{
64 parser.parse(new InputSource(new StringReader(xml)));
65 return agendas;
66 }
67 /***
68 * @param uri
69 * @param localName ?qName?
70 * @param qName
71 * @param attributes
72 * @exception SAXException
73 */
74 public void startElement(String uri, String localName, String qName, Attributes attributes)throws SAXException {
75 if(qName.equals("agenda")){
76 agenda = new Agenda();
77 agenda.setName(attributes.getValue("name"));
78 agendas.add(agenda);
79 task = null;
80 }
81 if(qName.equals("notification")){
82 String emailSpecFileName = "";
83 notification = new Notification();
84 notification.setName(attributes.getValue("name"));
85 notification.setLevel(attributes.getValue("level"));
86 try {
87 String fileSeparator = System.getProperty("file.separator");
88 emailSpecFileName = OpenSync.getInstance().getFilePath(fileSeparator+"etc"+fileSeparator+"notif"+fileSeparator+attributes.getValue("message"), true);
89 } catch (Exception ex) {
90 throw new SAXException(new OpenSyncException(
91 "error.config.schedule.notification.file-not-found",new Object[]{notification.getMode(), emailSpecFileName }
92 )
93 );
94 }
95 try {
96 notification.setMessageSpec(emailSpecFileName);
97 } catch (Exception ex) {
98 throw new SAXException(new OpenSyncException(
99 "error.config.schedule.notification.wrong-emailspec",new Object[]{notification.getMode(), emailSpecFileName }
100 )
101 );
102 }
103 notification.setMode(attributes.getValue("message"));
104 agenda.setNotification(notification);
105 }
106 else if(qName.equals("schedule")){
107 Schedule schedule = new Schedule();
108 String tmp = attributes.getValue("http-request");
109 schedule.setHttpRequest(tmp != null && tmp.equalsIgnoreCase("true"));
110 tmp = attributes.getValue("date");
111 if(tmp != null && !tmp.equals("")){
112 try {
113 schedule.setDate(dateFormat.parse(tmp));
114 }
115 catch (ParseException ex) {
116 throw new SAXException(new OpenSyncException(
117 "error.config.schedule.date.not-valid",new Object[]{agenda.getName()}
118 )
119 );
120 }
121 }
122 tmp = attributes.getValue("time");
123 if(tmp != null && !tmp.equals("")){
124 StringTokenizer tokens = new StringTokenizer(tmp,":");
125 try {
126 int hour = Integer.parseInt(tokens.nextToken());
127 if(hour < -1 || hour > 23){
128 throw new Exception();
129 }
130 schedule.setHour(hour);
131 int minute = Integer.parseInt(tokens.nextToken());
132 if(minute < 0 || minute > 59){
133 throw new Exception();
134 }
135 schedule.setMinute(minute);
136 }
137 catch (Exception ex) {
138 throw new SAXException(new OpenSyncException(
139 "error.config.schedule.time.not-valid",new Object[]{agenda.getName()}
140 )
141 );
142 }
143 }
144 String daysOfMonth = attributes.getValue("daysOfMonth");
145 if(daysOfMonth != null){
146 StringTokenizer tokens = new StringTokenizer(daysOfMonth,",");
147 if(tokens.hasMoreTokens()){
148 int[]days = new int[tokens.countTokens()];
149 for(int i=0;i<days.length;i++){
150 try {
151 int day = Integer.parseInt(tokens.nextToken());
152 if(day != -1 && day < 1 && day > 31){
153 throw new Exception();
154 }
155 days[i] = day;
156 }
157 catch (Exception ex) {
158 throw new SAXException(new OpenSyncException(
159 "error.config.schedule.days-of-month.not-valid",new Object[]{agenda.getName()}
160 )
161 );
162 }
163 }
164 schedule.setDaysOfMonth(days);
165 }
166 }
167 String daysOfWeek = attributes.getValue("daysOfWeek");
168 if(daysOfWeek != null){
169 StringTokenizer tokens = new StringTokenizer(daysOfWeek,",");
170 if(tokens.hasMoreTokens()){
171 int[]days = new int[tokens.countTokens()];
172 for(int i=0;i<days.length;i++){
173 try {
174 int day = Integer.parseInt(tokens.nextToken());
175 if(day != -1 && day < 1 && day > 7){
176 throw new Exception();
177 }
178 days[i] = day;
179 }
180 catch (Exception ex) {
181 throw new SAXException(new OpenSyncException(
182 "error.config.schedule.days-of-week.not-valid",new Object[]{agenda.getName()}
183 )
184 );
185 }
186 }
187 schedule.setDaysOfWeek(days);
188 }
189 }
190 tmp = attributes.getValue("repeat");
191 if(tmp != null && !tmp.equals("")){
192 try{
193 int repeat = Integer.parseInt(tmp);
194 if(repeat < 0){
195 throw new Exception();
196 }
197 schedule.setRepeat(repeat);
198 }
199 catch (Exception ex) {
200 throw new SAXException(new OpenSyncException(
201 "error.config.schedule.repeat.not-valid",new Object[]{agenda.getName()}
202 )
203 );
204 }
205 }
206 tmp = attributes.getValue("period");
207 if(tmp != null && !tmp.equals("")){
208 try{
209 int period = Integer.parseInt(tmp);
210 if(period < 0){
211 throw new Exception();
212 }
213 schedule.setPeriod(period);
214 }
215 catch (Exception ex) {
216 throw new SAXException(new OpenSyncException(
217 "error.config.schedule.period.not-valid",new Object[]{agenda.getName()}
218 )
219 );
220 }
221 }
222 if(!schedule.isValid()){
223 throw new SAXException(new OpenSyncException(
224 "error.config.schedule.not-valid",new Object[]{agenda.getName()}
225 )
226 );
227 }
228 agenda.addSchedule(schedule);
229 }
230 else if(qName.equals("task")){
231 Task subTask = new Task();
232 subTask.setName(attributes.getValue("name"));
233 String syncRef = attributes.getValue("synchronizationRef");
234 Synchronization synchronization = synchronizations.get(syncRef);
235 if(synchronization == null){
236 throw new SAXException(new OpenSyncException(
237 "error.config.synchronization.not-found",new Object[]{syncRef}
238 )
239 );
240 }
241 subTask.setSynchronization(synchronization);
242 if(task == null){
243 agenda.addTask(subTask);
244 }
245 else{
246 task.addSubTask(subTask);
247 }
248 task = subTask;
249 }
250 }
251 /***
252 * @param uri
253 * @param localName ?qName?
254 * @param qName
255 * @exception SAXException
256 */
257 public void endElement(String uri, String localName, String qName) throws org.xml.sax.SAXException {
258 if(qName.equals("task")){
259 task = task.getParentTask();
260 }
261 }
262 }