View Javadoc

1   /*
2    * sbxb library, part of ULMgen product
3    * Copyright (C) 2003,2005 Thierry Beigbeder
4    *
5    * This program is free software; you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation; either version 2 of the License, or
8    * (at your option) any later version.
9    *
10   * This program is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with this program; if not, write to the Free Software
17   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18   */
19  /*
20   * $Header: $
21   * $Revision: $
22   * $Date: $
23   *
24   * ====================================================================
25   *
26   * History :
27   * Who       yyyy/mm/dd   Action
28   * --------  ----------   ------
29   */
30  
31  
32  package org.otvl.sbxbsamp.simple;
33  
34  import org.apache.log4j.Logger;
35  
36  
37  import java.util.Date;
38  import java.util.Iterator;
39  import org.otvl.sbxb.SbxbUtil;
40  import org.otvl.sbxb.binding.BindingSpec;
41  import org.otvl.sbxb.converter.XsdDateTimeConverter;
42  
43  /***
44   * Sample class test driver (main).
45   * @author
46   * @version
47   * TODO: reuse getSimpleSample
48   */
49  
50  
51  public class SbxbSimpleSample
52  {
53  	/*-------------------------------------------------------------------------
54  	 * Attributes declaration
55  	 *-------------------------------------------------------------------------*/
56  	/***
57  	 * Class level LOG4J logger
58  	 */
59  	private static Logger logger = Logger.getLogger(SbxbSimpleSample.class);
60  
61  
62  	/*-------------------------------------------------------------------------
63  	 * Associations declaration
64  	 *-------------------------------------------------------------------------*/
65  
66  
67  	/*-------------------------------------------------------------------------
68  	 * Operations
69  	 *-------------------------------------------------------------------------*/
70  	/***
71  	 * 
72  	 * @param args 
73  	 */
74  	public static void main(
75  		String[] args
76  	) 
77  	{
78  		
79  		try {
80  			org.apache.log4j.BasicConfigurator.configure();
81  			new SbxbSimpleSample().run();
82  		} catch (Exception e) {
83  			//System.err.println(e.getMessage());
84  			e.printStackTrace();
85  		}
86  		
87  	}
88  	/***
89  	 * 
90  	 */
91  	private void run() 
92  		throws Exception
93  
94  	{
95  		
96  		runSimple();
97  		runMapped();
98  		runInh();
99  		runInh2();
100 		runSimpleNamespace("org.otvl.sbxbsamp.simple.HouseBindingSpecNs1");
101 		runSimpleNamespace("org.otvl.sbxbsamp.simple.HouseBindingSpecNs2");
102 		runSimpleNamespace("org.otvl.sbxbsamp.simple.HouseBindingSpecNs3");
103 		runAgenda();
104 		runSet();
105 		
106 	}
107 	/***
108 	 * 
109 	 * @return boolean
110 	 */
111 	public boolean runSimple() 
112 		throws Exception
113 
114 	{
115 		boolean vRunSimple;
116 
117 		BindingSpec.loadProperties("org.otvl.sbxbsamp.simple.HouseBindingSpec");
118 		BindingSpec bs = BindingSpec.getInstance("hbs");
119 		for(Iterator it = bs.keySetClassBindingSpec().iterator(); it.hasNext(); ) {
120 			String key = (String) it.next();
121 			System.out.println(bs.getClassBindingSpec(key).printClassSpec("hbs",key));
122 		}
123 		House house = new House();
124 		house.setAddress("Autivielle");
125 		Door door = new Door();
126 		door.setColor("red");
127 		house.setTheDoor(door);
128 		Room room = new Room();
129 		room.setId(1);
130 		room.setName("living");
131 		room.setLocation("south");
132 		door = new Door();
133 		door.setColor("green");
134 		room.addTheDoors(door);
135 		door = new Door();
136 		door.setColor("white");
137 		room.addTheDoors(door);
138 		Window window = new Window();
139 		window.setId(0);
140 		window.setLocation("left");
141 		room.addWindoz(window);
142 		window = new Window();
143 		window.setId(1);
144 		window.setLocation("right");
145 		room.addWindoz(window);
146 		house.addRooms(room);
147 		room = new Room();
148 		room.setId(2);
149 		room.setName("nice");
150 		room.setLocation("south-east");
151 		door = new Door();
152 		door.setColor("yellow");
153 		room.addTheDoors(door);
154 		window = new Window();
155 		window.setId(0);
156 		window.setLocation("center");
157 		room.addWindoz(window);
158 		house.addRooms(room);
159 		String xmlData = SbxbUtil.sBeanToXml("House","hbs",house);
160 		System.out.println(xmlData);
161 		House cloneHouse = new House();
162 		SbxbUtil.xmlToSBean(xmlData,"hbs",cloneHouse);
163 		String xmlData2 = SbxbUtil.sBeanToXml("House","hbs",cloneHouse);
164 		System.out.println(vRunSimple = xmlData2.equals(xmlData));
165 		return vRunSimple;
166 
167 	}
168 	/***
169 	 * 
170 	 * @return boolean
171 	 */
172 	public boolean runMapped() 
173 		throws Exception
174 
175 	{
176 		boolean vRunMapped;
177 
178 		BindingSpec.loadProperties("org.otvl.sbxbsamp.simple.PageBindingSpec");
179 		BindingSpec bs = BindingSpec.getInstance("pbs");
180 		for(Iterator it = bs.keySetClassBindingSpec().iterator(); it.hasNext(); ) {
181 			String key = (String) it.next();
182 			System.out.println(bs.getClassBindingSpec(key).printClassSpec("hbs",key));
183 		}
184 		Page page = new Page();
185 		page.setUrl("http://sourceforge.net/");
186 		Frame frame = new Frame();
187 		frame.setName("left");
188 		frame.setX(0); frame.setY(80);
189 		page.putTheFrames(frame.getName(),frame);
190 		frame = new Frame();
191 		frame.setName("right");
192 		frame.setX(200); frame.setY(80);
193 		page.putTheFrames(frame.getName(),frame);
194 		String xmlData = SbxbUtil.sBeanToXml("Page","pbs",page);
195 		Page clonePage = new Page();
196 		SbxbUtil.xmlToSBean(xmlData,"pbs",clonePage);
197 		String xmlData2 = SbxbUtil.sBeanToXml("Page","pbs",clonePage);
198 		System.out.println(vRunMapped = xmlData2.equals(xmlData));
199 		return vRunMapped;
200 
201 	}
202 	/***
203 	 * 
204 	 * @return boolean
205 	 */
206 	public boolean runInh() 
207 		throws Exception
208 
209 	{
210 		boolean vRunInh;
211 
212 		BindingSpec.loadProperties("org.otvl.sbxbsamp.simple.DrawingBindingSpec");
213 		BindingSpec bs = BindingSpec.getInstance("dbs");
214 		for(Iterator it = bs.keySetClassBindingSpec().iterator(); it.hasNext(); ) {
215 			String key = (String) it.next();
216 			System.out.println(bs.getClassBindingSpec(key).printClassSpec("dbs",key));
217 		}
218 		Drawing drawing = new Drawing();
219 		drawing.setPaper("A4");
220 		Circle circle = new Circle();
221 		circle.setType("CIRCLE");
222 		circle.setPosition("(100,100)");
223 		circle.setCenter("(0,0)");
224 		circle.setRadius("50");
225 		drawing.addTheShapes(circle);
226 		Rectangle rectangle = new Rectangle();
227 		rectangle.setType("RECTANGLE");
228 		rectangle.setPosition("(100,100)");
229 		rectangle.setWidth("100");
230 		rectangle.setHeight("50");
231 		drawing.addTheShapes(rectangle);
232 		String xmlData = SbxbUtil.sBeanToXml("Drawing","dbs",drawing);
233 		Drawing cloneDrawing = new Drawing();
234 		SbxbUtil.xmlToSBean(xmlData,"dbs",cloneDrawing);
235 		String xmlData2 = SbxbUtil.sBeanToXml("Drawing","dbs",cloneDrawing);
236 		System.out.println(vRunInh = xmlData2.equals(xmlData));
237 		return vRunInh;
238 
239 	}
240 	/***
241 	 * 
242 	 * @return boolean
243 	 */
244 	public boolean runInh2() 
245 		throws Exception
246 
247 	{
248 		boolean vRunInh2;
249 
250 		BindingSpec.loadProperties("org.otvl.sbxbsamp.simple.DrawingBindingSpec2");
251 		BindingSpec bs = BindingSpec.getInstance("dbs2");
252 		for(Iterator it = bs.keySetClassBindingSpec().iterator(); it.hasNext(); ) {
253 			String key = (String) it.next();
254 			System.out.println(bs.getClassBindingSpec(key).printClassSpec("dbs2",key));
255 		}
256 		Drawing drawing = new Drawing();
257 		drawing.setPaper("A4");
258 		Circle circle = new Circle();
259 		circle.setType("CIRCLE");
260 		circle.setPosition("(100,100)");
261 		circle.setCenter("(0,0)");
262 		circle.setRadius("50");
263 		drawing.addTheShapes(circle);
264 		Rectangle rectangle = new Rectangle();
265 		rectangle.setType("RECTANGLE");
266 		rectangle.setPosition("(100,100)");
267 		rectangle.setWidth("100");
268 		rectangle.setHeight("50");
269 		drawing.addTheShapes(rectangle);
270 		String xmlData = SbxbUtil.sBeanToXml("Drawing","dbs2",drawing);
271 		Drawing cloneDrawing = new Drawing();
272 		SbxbUtil.xmlToSBean(xmlData,"dbs2",cloneDrawing);
273 		String xmlData2 = SbxbUtil.sBeanToXml("Drawing","dbs2",cloneDrawing);
274 		System.out.println(vRunInh2 = xmlData2.equals(xmlData));
275 		return vRunInh2;
276 
277 	}
278 	/***
279 	 * 
280 	 * @param bsName 
281 	 * @return boolean
282 	 */
283 	public boolean runSimpleNamespace(
284 		String bsName
285 	) 
286 		throws Exception
287 
288 	{
289 		boolean vRunSimpleNamespace;
290 
291 		BindingSpec.loadProperties(bsName);
292 		BindingSpec bs = BindingSpec.getInstance("hbs");
293 		for(Iterator it = bs.keySetClassBindingSpec().iterator(); it.hasNext(); ) {
294 			String key = (String) it.next();
295 			System.out.println(bs.getClassBindingSpec(key).printClassSpec("hbs",key));
296 		}
297 		House house = new House();
298 		house.setAddress("Autivielle");
299 		Door door = new Door();
300 		door.setColor("red");
301 		house.setTheDoor(door);
302 		Room room = new Room();
303 		room.setId(1);
304 		room.setName("living");
305 		room.setLocation("south");
306 		door = new Door();
307 		door.setColor("green");
308 		room.addTheDoors(door);
309 		door = new Door();
310 		door.setColor("white");
311 		room.addTheDoors(door);
312 		Window window = new Window();
313 		window.setId(0);
314 		window.setLocation("left");
315 		room.addWindoz(window);
316 		window = new Window();
317 		window.setId(1);
318 		window.setLocation("right");
319 		room.addWindoz(window);
320 		house.addRooms(room);
321 		room = new Room();
322 		room.setId(2);
323 		room.setName("nice");
324 		room.setLocation("south-east");
325 		door = new Door();
326 		door.setColor("yellow");
327 		room.addTheDoors(door);
328 		window = new Window();
329 		window.setId(0);
330 		window.setLocation("center");
331 		room.addWindoz(window);
332 		house.addRooms(room);
333 		String xmlData = SbxbUtil.sBeanToXml("House","hbs",house);
334 		System.out.println(xmlData);
335 		House cloneHouse = new House();
336 		SbxbUtil.xmlToSBean(xmlData,"hbs",cloneHouse);
337 		String xmlData2 = SbxbUtil.sBeanToXml("House","hbs",cloneHouse);
338 		System.out.println(vRunSimpleNamespace = xmlData2.equals(xmlData));
339 		return vRunSimpleNamespace;
340 
341 	}
342 	/***
343 	 * Used for various tests
344 	 * @return House
345 	 */
346 	public House getSimpleSample() 
347 	{
348 		House vGetSimpleSample;
349 
350 		House house = new House();
351 		house.setAddress("Autivielle");
352 		Door door = new Door();
353 		door.setColor("red");
354 		house.setTheDoor(door);
355 		Room room = new Room();
356 		room.setId(1);
357 		room.setName("living");
358 		room.setLocation("south");
359 		door = new Door();
360 		door.setColor("green");
361 		room.addTheDoors(door);
362 		door = new Door();
363 		door.setColor("white");
364 		room.addTheDoors(door);
365 		Window window = new Window();
366 		window.setId(0);
367 		window.setLocation("left");
368 		room.addWindoz(window);
369 		window = new Window();
370 		window.setId(1);
371 		window.setLocation("right");
372 		room.addWindoz(window);
373 		house.addRooms(room);
374 		room = new Room();
375 		room.setId(2);
376 		room.setName("nice");
377 		room.setLocation("south-east");
378 		door = new Door();
379 		door.setColor("yellow");
380 		room.addTheDoors(door);
381 		window = new Window();
382 		window.setId(0);
383 		window.setLocation("center");
384 		room.addWindoz(window);
385 		house.addRooms(room);
386 		
387 		vGetSimpleSample = house;
388 		return vGetSimpleSample;
389 
390 	}
391 	/***
392 	 * 
393 	 * @return boolean
394 	 */
395 	public boolean runAgenda() 
396 		throws Exception
397 
398 	{
399 		boolean vRunAgenda;
400 
401 		Agenda agenda = new Agenda();
402 		agenda.setId("Personal");
403 		Meeting meeting = null;
404 		meeting = new Meeting();
405 		meeting.setDate(new Date());
406 		meeting.setDescription("First meeting");
407 		agenda.addMeeting(meeting);
408 		BindingSpec.loadProperties("org.otvl.sbxbsamp.simple.AgendaBindingSpec");
409 		BindingSpec.getInstance("abs").putConverter(java.util.Date.class,new XsdDateTimeConverter());
410 		String xmlData = SbxbUtil.sBeanToXml("Agenda","abs",agenda);
411 		Agenda cloneAgenda = new Agenda();
412 		SbxbUtil.xmlToSBean(xmlData,"abs",cloneAgenda);
413 		String xmlData2 = SbxbUtil.sBeanToXml("Agenda","abs",cloneAgenda);
414 		vRunAgenda = xmlData2.equals(xmlData);
415 		return vRunAgenda;
416 
417 	}
418 	/***
419 	 * 
420 	 * @return boolean
421 	 */
422 	public boolean runSet() 
423 		throws Exception
424 
425 	{
426 		boolean vRunSet;
427 
428 		BindingSpec.loadProperties("org.otvl.sbxbsamp.simple.SetBindingSpec");
429 		BindingSpec.getInstance("sbs").putConverter(java.util.Date.class,new XsdDateTimeConverter());
430 		TheSet set = new TheSet();
431 		set.setId("id");
432 		TheElement elt = null;
433 		elt = new TheElement();
434 		elt.setName("A");
435 		set.addElements(elt);
436 		elt = new TheElement();
437 		elt.setName("B");
438 		set.addElements(elt);
439 		
440 		Date date = null;
441 		date = new Date();
442 		set.addDates(date);
443 		date = new Date(date.getTime() + 2000);
444 		set.addDates(date);
445 		
446 		String xmlData = SbxbUtil.sBeanToXml("set","sbs",set);
447 		TheSet cloneSet = new TheSet();
448 		SbxbUtil.xmlToSBean(xmlData,"sbs",cloneSet);
449 		String xmlData2 = SbxbUtil.sBeanToXml("set","sbs",cloneSet);
450 		vRunSet = xmlData2.equals(xmlData);
451 		if(!vRunSet) {
452 			logger.debug("??? set1 " + xmlData);
453 			logger.debug("??? set2 " + xmlData2);
454 			vRunSet = true;
455 		}
456 		return vRunSet;
457 
458 	}
459 
460 
461 	/*-------------------------------------------------------------------------
462 	 * Attribute accessors
463 	 *-------------------------------------------------------------------------*/
464 	//------------------------
465 	// Accessors for attribute logger
466 	//------------------------
467 	/***
468 	 * Returns the logger.<br/>
469 	 * Class level LOG4J logger
470 	 * @return Logger
471 	 */
472 	private static Logger getLogger() {
473 		Logger vLogger = logger;
474 		return vLogger;
475 	}
476 
477 
478 
479 	/*-------------------------------------------------------------------------
480 	 * Association accessors
481 	 *-------------------------------------------------------------------------*/
482 
483 }