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   * History :
25   * Who       yyyy/mm/dd   Action
26   * --------  ----------   ------
27   */
28  
29  
30  package org.otvl.sbxbsamp.complex;
31  
32  
33  // Insert here additional imports
34  import org.otvl.sbxb.binding.BindingSpec;
35  
36  import java.io.InputStream;
37  import java.io.InputStreamReader;
38  import java.io.BufferedReader;
39  
40  import java.util.Iterator;
41  import java.util.MissingResourceException;
42  
43  import org.otvl.sbxb.SbxbUtil;
44  
45  /***
46   * Sample class test driver (main).
47   * @author
48   * @version
49   */
50  
51  
52  public class SbxbComplexSample
53  {
54  	/*-------------------------------------------------------------------------
55  	 * Attributes declaration
56  	 *-------------------------------------------------------------------------*/
57  
58  
59  	/*-------------------------------------------------------------------------
60  	 * Associations declaration
61  	 *-------------------------------------------------------------------------*/
62  
63  
64  	/*-------------------------------------------------------------------------
65  	 * Operations
66  	 *-------------------------------------------------------------------------*/
67  	/***
68  	 * 
69  	 * @param args 
70  	 */
71  	public static void main(
72  		String[] args
73  	) 
74  	{
75  		
76  		try {
77  			org.apache.log4j.BasicConfigurator.configure();
78  			new SbxbComplexSample().run();
79  		} catch (Exception e) {
80  			//System.err.println(e.getMessage());
81  			e.printStackTrace();
82  		}
83  		
84  	}
85  	/***
86  	 * 
87  	 * @return boolean
88  	 */
89  	public boolean run() 
90  		throws Exception
91  
92  	{
93  		boolean vRun;
94  
95  		BindingSpec.loadProperties("org.otvl.sbxbsamp.complex.ComplexBindingSpec");
96  		BindingSpec bs = BindingSpec.getInstance("mapx");
97  		for(Iterator it = bs.keySetClassBindingSpec().iterator(); it.hasNext(); ) {
98  			String key = (String) it.next();
99  			System.out.println(bs.getClassBindingSpec(key).printClassSpec("mapx",key));
100 		}
101 		StringBuffer sb = new StringBuffer();
102 		readFile("/org/otvl/sbxbsamp/complex/data.xml",sb);
103 		Object sBean = new ClsA();
104 		SbxbUtil.xmlToSBean(sb.toString(),"mapx",sBean);
105 		String xmlData = SbxbUtil.sBeanToXml("A","mapx",sBean);
106 		System.out.println(xmlData);
107 		
108 		Object sBean2 = new ClsA();
109 		SbxbUtil.xmlToSBean(xmlData,"mapx",sBean2);
110 		String xmlData2 = SbxbUtil.sBeanToXml("A","mapx",sBean2);
111 		System.out.println(xmlData2.equals(xmlData));
112 		
113 		String tmp1;
114 		String tmp2;
115 		Object sBeanTmp;
116 		ClsA sBean4;
117 		
118 		sBean4 = new ClsA();
119 		sBean4.setAtt1("contenu tmp att1");
120 		tmp1 = SbxbUtil.sBeanToXml("A","mapx",sBean4);
121 		SbxbUtil.xmlToSBean(tmp1,"mapx",sBeanTmp=new ClsA());
122 		tmp2 = SbxbUtil.sBeanToXml("A","mapx",sBeanTmp);
123 		System.out.println(tmp1);
124 		System.out.println(tmp1.equals(tmp2));
125 
126 		sBean4 = new ClsA();
127 		sBean4.setAtt2("contenu tmp att2");
128 		tmp1 = SbxbUtil.sBeanToXml("A","mapx",sBean4);
129 		SbxbUtil.xmlToSBean(tmp1,"mapx",sBeanTmp=new ClsA());
130 		tmp2 = SbxbUtil.sBeanToXml("A","mapx",sBeanTmp);
131 		System.out.println(tmp1);
132 		System.out.println(tmp1.equals(tmp2));
133 
134 		sBean4 = new ClsA();
135 		sBean4.setAtt3("");
136 		tmp1 = SbxbUtil.sBeanToXml("A","mapx",sBean4);
137 		SbxbUtil.xmlToSBean(tmp1,"mapx",sBeanTmp=new ClsA());
138 		tmp2 = SbxbUtil.sBeanToXml("A","mapx",sBeanTmp);
139 		System.out.println(tmp1);
140 		System.out.println(tmp1.equals(tmp2));
141 
142 		sBean4 = new ClsA();
143 		sBean4.setAtt3("tmp att3");
144 		tmp1 = SbxbUtil.sBeanToXml("A","mapx",sBean4);
145 		SbxbUtil.xmlToSBean(tmp1,"mapx",sBeanTmp=new ClsA());
146 		tmp2 = SbxbUtil.sBeanToXml("A","mapx",sBeanTmp);
147 		System.out.println(tmp1);
148 		System.out.println(tmp1.equals(tmp2));
149 
150 		sBean4 = new ClsA();
151 		sBean4.setAtt4("");
152 		tmp1 = SbxbUtil.sBeanToXml("A","mapx",sBean4);
153 		SbxbUtil.xmlToSBean(tmp1,"mapx",sBeanTmp=new ClsA());
154 		tmp2 = SbxbUtil.sBeanToXml("A","mapx",sBeanTmp);
155 		System.out.println(tmp1);
156 		System.out.println(tmp1.equals(tmp2));
157 
158 		sBean4 = new ClsA();
159 		sBean4.setAtt4("tmp att4");
160 		tmp1 = SbxbUtil.sBeanToXml("A","mapx",sBean4);
161 		SbxbUtil.xmlToSBean(tmp1,"mapx",sBeanTmp=new ClsA());
162 		tmp2 = SbxbUtil.sBeanToXml("A","mapx",sBeanTmp);
163 		System.out.println(tmp1);
164 		System.out.println(tmp1.equals(tmp2));
165 
166 		sBean4 = new ClsA();
167 		sBean4.setAtt3("tmp att3");
168 		sBean4.setAtt4("tmp att4");
169 		tmp1 = SbxbUtil.sBeanToXml("A","mapx",sBean4);
170 		SbxbUtil.xmlToSBean(tmp1,"mapx",sBeanTmp=new ClsA());
171 		tmp2 = SbxbUtil.sBeanToXml("A","mapx",sBeanTmp);
172 		System.out.println(tmp1);
173 		System.out.println(tmp1.equals(tmp2));
174 
175 		sBean4 = new ClsA();
176 		sBean4.setAtt1("");
177 		sBean4.setAtt5("");
178 		sBean4.setRoleB(new ClsB());
179 		sBean4.getRoleB().setB("");
180 		tmp1 = SbxbUtil.sBeanToXml("A","mapx",sBean4);
181 		SbxbUtil.xmlToSBean(tmp1,"mapx",sBeanTmp=new ClsA());
182 		tmp2 = SbxbUtil.sBeanToXml("A","mapx",sBeanTmp);
183 		System.out.println(tmp1);
184 		System.out.println(tmp2);
185 		System.out.println(vRun = tmp1.equals(tmp2));
186 		return vRun;
187 
188 	}
189 	/***
190 	 * Simple file content's reader.
191 	 * @param filePath 
192 	 * @param content 
193 	 */
194 	private void readFile(
195 		String filePath,
196 		StringBuffer content
197 	) 
198 		throws Exception
199 
200 	{
201 		
202 		InputStream is = getClass().getResourceAsStream(filePath);
203 		if(is == null) {
204 			throw new MissingResourceException("",getClass().getName(),filePath);
205 		}
206 		BufferedReader reader =
207 			 new BufferedReader (
208 					 new InputStreamReader (is,"ISO-8859-1") ) ;
209 		String line;
210 		content.setLength(0);
211 		while((line = reader.readLine()) != null) {
212 			content.append(line + "\n");
213 		}
214 		reader.close();
215 		
216 	}
217 
218 
219 	/*-------------------------------------------------------------------------
220 	 * Attribute accessors
221 	 *-------------------------------------------------------------------------*/
222 
223 
224 	/*-------------------------------------------------------------------------
225 	 * Association accessors
226 	 *-------------------------------------------------------------------------*/
227 
228 }