1 /*****************************************************************************
2 * Virtual Mockup for Machine Vision
3 * Copyright (C) 2001-2003 Fabio R. de Miranda, João E. Kogler Jr.,
4 * Carlos S. Santos.
5 * Virtual Mockup for Machine Vision Project funded by SENAC-SP
6 *
7 * Permission is granted to redistribute and/or modify this
8 * software under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This software is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License (http://www.gnu.org/copyleft/lesser.html)
16 * for more details.
17 *
18 *****************************************************************************/
19
20 package camera3d;
21
22 import camera3d.gui.*;
23 import java.awt.Point;
24 import java.awt.Dimension;
25 import java.util.*;
26 import java.net.URL;
27 import java.net.MalformedURLException;
28 import java.io.*;
29 import org.jdom.*;
30 import org.jdom.input.*;
31 import org.jdom.output.*;
32
33 /***
34 * A class for saving/retrieving information about the working environment. Currently,
35 * only information about objects native to the Virtual Camera (e.g. lights, cameras,
36 * distance measurers, etc) and graphical components is supported. All information about
37 * content imported into the the application is lost.
38 *
39 * @author Fábio Roberto de Miranda e Carlos da Silva dos Santos
40 */
41 public class XMLConverter {
42
43 /*** Provides information to be saved/receives objects loaded from file. */
44 J3DBase base;
45 private VcObjectXMLHandler handler;
46 /*** indent string*/
47 private static final String indentation = " ";
48 /*** Name of root element of valid documents. */
49 private static final String rootName = "VirtualCamera";
50 /*** Name of element under which are put scene graph elements. */
51 private static final String sceneRootName = "SceneElements";
52 /* string representations of boolean values. */
53 private static final String trueSt = "True";
54 private static final String falseSt = "False";
55 /*** Version attribute. */
56 private static final String versionAtt = "Version";
57 private static final String versionValue = "0.1";
58
59 // GUI section attributes
60 // number of viewports
61 private static final String vpNumberAtt = "VPNumber";
62 // view showed by a certain viewport
63 private static final String viewInVpAtt = "View";
64 // attribute to tell whether viewport is internal
65 private static final String internalAtt = "Internal";
66 // window width
67 private static final String widthAtt = "Width";
68 // window height
69 private static final String heightAtt = "Height";
70 // X position of window
71 private static final String xPosAtt = "XPosition";
72 // Y position of window
73 private static final String yPosAtt = "YPosition";
74 /// end of GUI attributes
75
76 private boolean newLine = true;
77 File file;
78 Document doc;
79
80 /* handlers of XML elements for individual types of objects, responsible for
81 converting object information into XML data and vice-versa. */
82 VcViewXMLHandler viewHandler;
83 VcLaserArrayXMLHandler arrayHandler;
84 DistanceMeasurerXMLHandler measurerHandler;
85 VcAmbientLightXMLHandler ambientHandler;
86 VcDirectLightXMLHandler directHandler;
87 VcPointLightXMLHandler pointHandler;
88 VcSpotLightXMLHandler spotHandler;
89 VcHelperPointXMLHandler ptHandler;
90
91 public XMLConverter() {
92 }
93
94 /***
95 * Creates new converter, setting the J3DBase object which will provide/receive
96 * scene graph information.
97 */
98 public XMLConverter(J3DBase base) {
99 this.base = base;
100 }
101
102 /***
103 * Sets the J3DBase which will provide/receive scene graph information.
104 */
105 public void setBase(J3DBase base){
106 this.base = base;
107 }
108
109 /***
110 * Returns the J3DBase which provides/receives scene graph information.
111 */
112 public J3DBase getBase(){
113 return this.base;
114 }
115
116 /***
117 * Prints a debug message.
118 */
119 void debugln(String s){
120 System.out.println(s);
121 }
122
123 /***
124 * Creates a new Document based on scene graph information and outputs it to a file.
125 */
126 public void write(String filename, GUIControl guiControl){
127 Vector VcObjVector;
128 VcObject obj;
129 Element element;
130 FileOutputStream outStream;
131 // creates element that serves as root for document
132 Element root = new Element(rootName);
133 root.setAttribute(versionAtt,versionValue);
134 doc = new Document(root);
135 try{
136 file = new File(filename);
137 }catch(NullPointerException npe){
138 npe.printStackTrace();
139 return;
140 }
141 try{
142 outStream = new FileOutputStream(file);
143 }catch(FileNotFoundException fnfe){
144 fnfe.printStackTrace();
145 return;
146 }
147 /*
148 Currently, all content is added under root element
149 */
150
151 Element sceneRoot = new Element(sceneRootName);
152 root.addContent(sceneRoot);
153 /* creates elements referring to objects present in the scene */
154 VcObjVector = guiControl.getJ3DBase().getObjectList();
155 Iterator iter = VcObjVector.iterator();
156 while(iter.hasNext()){
157 // for each element, gets the appropriate handler, then
158 // creates XML element and adds it to document
159 obj = (VcObject)iter.next();
160 handler = getHandler(obj);
161 if(handler!=null){
162 element = handler.createElement(obj);
163 sceneRoot.addContent(element);
164 }
165 }
166
167
168 /************************--start of GUI section--************************/
169 Element guiElement = new Element("GUI");
170 root.addContent(guiElement);
171 ViewportWindowManager manager = guiControl.getViewportManager();
172 List vpList = manager.getListOfFrames();
173 Element vpElement;
174 ViewportFrame vpFrame;
175 int cont = 0;
176 iter = vpList.iterator();
177 // creates an element which holds information about viewport size,
178 // position and view shown; and add it to guiElement
179 while(iter.hasNext()){
180 vpFrame = (ViewportFrame)iter.next();
181 vpElement = new Element("Viewport");
182 // number of viewport
183 vpElement.setAttribute(vpNumberAtt,Integer.toString(cont));
184 String internal;
185 if(vpFrame.isInternalFrame()) internal = trueSt;
186 else internal = falseSt;
187 // tells whether viewport is internal or not
188 vpElement.setAttribute(internalAtt,internal);
189 Point p2d = vpFrame.getPosition();
190 // position of viewport window
191 vpElement.setAttribute(xPosAtt,Integer.toString(p2d.x));
192 vpElement.setAttribute(yPosAtt,Integer.toString(p2d.y));
193 Dimension d = vpFrame.getSize();
194 // size of viewport window
195 vpElement.setAttribute(widthAtt,Integer.toString(d.width));
196 vpElement.setAttribute(heightAtt,Integer.toString(d.height));
197 // label of view shown in viewport
198 vpElement.setAttribute(viewInVpAtt,vpFrame.getViewport().getView().getLabel());
199 guiElement.addContent(vpElement);
200 cont++;
201 }
202
203 FrameSwitcherPane selPane = guiControl.getSelectionPane();
204 // creates element relating to selection list pane
205 Element selElement = new Element("SelectionPane");
206 String internal;
207 if(selPane.isInside()) internal = trueSt;
208 else internal = falseSt;
209 // tells whether selection list pane is internal or not
210 selElement.setAttribute(internalAtt,internal);
211 Point p2d = selPane.getPosition();
212 // position of selection list pane
213 selElement.setAttribute(xPosAtt,Integer.toString(p2d.x));
214 selElement.setAttribute(yPosAtt,Integer.toString(p2d.y));
215 Dimension d = selPane.getSize();
216 // size of selection list pane
217 selElement.setAttribute(widthAtt,Integer.toString(d.width));
218 selElement.setAttribute(heightAtt,Integer.toString(d.height));
219 guiElement.addContent(selElement);
220
221 FrameSwitcherPane edtPane = guiControl.getEditionPane();
222 // creates element relating to edition list pane
223 Element edtElement = new Element("EditionPane");
224 if(edtPane.isInside()) internal = trueSt;
225 else internal = falseSt;
226 // tells whether edition pane is internal or not
227 edtElement.setAttribute(internalAtt,internal);
228 p2d = edtPane.getPosition();
229 // position of edition list pane
230 edtElement.setAttribute(xPosAtt,Integer.toString(p2d.x));
231 edtElement.setAttribute(yPosAtt,Integer.toString(p2d.y));
232 d = selPane.getSize();
233 // size of selection list pane
234 edtElement.setAttribute(widthAtt,Integer.toString(d.width));
235 edtElement.setAttribute(heightAtt,Integer.toString(d.height));
236 guiElement.addContent(edtElement);
237
238 /*************************--end of GUI section--*************************/
239
240 XMLOutputter outputter = new XMLOutputter(indentation,newLine);
241 // outputs Document content to file
242 try{
243 outputter.output(doc,outStream);
244 outStream.close();
245 }catch(java.io.IOException ioe){
246 ioe.printStackTrace();
247 }
248
249 }
250
251 /***
252 * Loads the content of a file into the scene graph and configures graphical components.
253 * @param filename complete path to configuration file.
254 * @param guiControl serves as access point to scene graph and graphical components.
255 */
256 public void load(String filename, GUIControl guiControl){
257 this.base = guiControl.getJ3DBase();
258 try{
259 file = new File(filename);
260 }catch(NullPointerException npe){
261 npe.printStackTrace();
262 return;
263 }
264
265 SAXBuilder builder = new SAXBuilder();
266 try{
267 doc = builder.build(file);
268 }catch(JDOMException e){
269 e.printStackTrace();
270 return;
271 }
272
273 Element root = doc.getRootElement();
274 // checks whether this is really a Virtual Camera file
275 if(!(rootName.equals(root.getName()))){
276 System.out.println("Not a valid Virtual Camera file");
277 return;
278 }
279
280 List list;
281 initializeHandlers();
282 // gets element which serves as root to all scene graph elements
283 Element sceneRoot = root.getChild(sceneRootName);
284
285 // processes all different types of scene graph elements
286 list = sceneRoot.getChildren(viewHandler.getType());
287 viewHandler.processList(list,base);
288
289 list = sceneRoot.getChildren(arrayHandler.getType());
290 arrayHandler.processList(list,base);
291
292 list = sceneRoot.getChildren(pointHandler.getType());
293 pointHandler.processList(list,base);
294
295 list = sceneRoot.getChildren(ambientHandler.getType());
296 ambientHandler.processList(list,base);
297
298 list = sceneRoot.getChildren(spotHandler.getType());
299 spotHandler.processList(list,base);
300
301 list = sceneRoot.getChildren(directHandler.getType());
302 directHandler.processList(list,base);
303
304 list = sceneRoot.getChildren(measurerHandler.getType());
305 measurerHandler.processList(list,base);
306
307 list = sceneRoot.getChildren(ptHandler.getType());
308 ptHandler.processList(list,base);
309
310 /************************--start of GUI section--************************/
311 ViewportWindowManager manager = guiControl.getViewportManager();
312 Element guiElement = root.getChild("GUI");
313 if(guiElement==null) return;
314 List vpList = guiElement.getChildren("Viewport");
315 Iterator iter = vpList.iterator();
316 Element vpElement;
317 int index,x,y,width,height;
318 boolean internal;
319 String intSt;
320 String viewLabel;
321 VcView view;
322 while(iter.hasNext()){
323 vpElement = (Element) iter.next();
324 try{
325 index = vpElement.getAttribute(vpNumberAtt).getIntValue();
326 debugln("vp index "+index);
327 intSt = vpElement.getAttributeValue(internalAtt);
328 if(intSt.equalsIgnoreCase(falseSt)) internal = false;
329 else internal = true;
330
331 x = vpElement.getAttribute(xPosAtt).getIntValue();
332 y = vpElement.getAttribute(yPosAtt).getIntValue();
333 width = vpElement.getAttribute(widthAtt).getIntValue();
334 height = vpElement.getAttribute(heightAtt).getIntValue();
335
336 viewLabel = vpElement.getAttributeValue(viewInVpAtt);
337 debugln("View label "+ viewLabel);
338 VcObject obj = guiControl.getJ3DBase().getByLabel(viewLabel);
339 if((obj!=null)&&(obj instanceof VcView)) view = (VcView) obj;
340 else view = null;
341
342 guiControl.setViewInViewport(index,view);
343 guiControl.setViewportLocation(index,internal,x,y,width,height);
344
345 }catch(org.jdom.DataConversionException dce){
346 dce.printStackTrace();
347 }
348 }
349
350 Element selElement = guiElement.getChild("SelectionPane");
351 try{
352 intSt = selElement.getAttributeValue(internalAtt);
353 if(intSt.equalsIgnoreCase(falseSt)) internal = false;
354 else internal = true;
355
356 x = selElement.getAttribute(xPosAtt).getIntValue();
357 y = selElement.getAttribute(yPosAtt).getIntValue();
358 width = selElement.getAttribute(widthAtt).getIntValue();
359 height = selElement.getAttribute(heightAtt).getIntValue();
360
361 FrameSwitcherPane selPane = guiControl.getSelectionPane();
362 selPane.setInside(internal);
363 selPane.setPosition(x,y);
364 selPane.setSize(width,height);
365 }catch(org.jdom.DataConversionException dce){
366 dce.printStackTrace();
367 }
368
369
370 Element edtElement = guiElement.getChild("EditionPane");
371 try{
372 intSt = edtElement.getAttributeValue(internalAtt);
373 if(intSt.equalsIgnoreCase(falseSt)) internal = false;
374 else internal = true;
375
376 x = edtElement.getAttribute(xPosAtt).getIntValue();
377 y = edtElement.getAttribute(yPosAtt).getIntValue();
378 width = edtElement.getAttribute(widthAtt).getIntValue();
379 height = edtElement.getAttribute(heightAtt).getIntValue();
380
381 FrameSwitcherPane edtPane = guiControl.getEditionPane();
382 edtPane.setInside(internal);
383 edtPane.setPosition(x,y);
384 edtPane.setSize(width,height);
385 }catch(org.jdom.DataConversionException dce){
386 dce.printStackTrace();
387 }
388 /*************************--end of GUI section--*************************/
389 }
390
391 /***
392 * Returns the appropriate XML handler, according to the type of VcObject
393 * given as input.
394 */
395 private VcObjectXMLHandler getHandler(VcObject obj){
396 if(obj instanceof VcView){
397 if(viewHandler==null) viewHandler = new VcViewXMLHandler();
398 return viewHandler;
399 }
400 if(obj instanceof VcLaserArray){
401 if(arrayHandler==null) arrayHandler = new VcLaserArrayXMLHandler();
402 return arrayHandler;
403 }
404 if(obj instanceof DistanceMeasurer){
405 if(measurerHandler==null) measurerHandler = new DistanceMeasurerXMLHandler();
406 return measurerHandler;
407 }
408 if(obj instanceof VcAmbientLight){
409 if(ambientHandler==null) ambientHandler = new VcAmbientLightXMLHandler();
410 return ambientHandler;
411 }
412 if(obj instanceof VcDirectLight){
413 if(directHandler==null) directHandler = new VcDirectLightXMLHandler();
414 return directHandler;
415 }
416 if(obj instanceof VcPointLight){
417 if(obj instanceof VcSpotLight){
418 if(spotHandler==null) spotHandler = new VcSpotLightXMLHandler();
419 return spotHandler;
420 }
421 if(pointHandler==null) pointHandler = new VcPointLightXMLHandler();
422 return pointHandler;
423 }
424 if(obj instanceof VcHelperPoint){
425 if(ptHandler==null) ptHandler = new VcHelperPointXMLHandler();
426 return ptHandler;
427 }
428 // return null till we find a way to handle content
429 if(obj instanceof VcContent){
430 return null;
431 }
432 return null;
433 }
434
435 private void initializeHandlers(){
436 if(viewHandler==null) viewHandler = new VcViewXMLHandler();
437 if(arrayHandler==null) arrayHandler = new VcLaserArrayXMLHandler();
438 if(measurerHandler==null) measurerHandler = new DistanceMeasurerXMLHandler();
439 if(ambientHandler==null) ambientHandler = new VcAmbientLightXMLHandler();
440 if(directHandler==null) directHandler = new VcDirectLightXMLHandler();
441 if(pointHandler==null) pointHandler = new VcPointLightXMLHandler();
442 if(spotHandler==null) spotHandler = new VcSpotLightXMLHandler();
443 if(ptHandler==null) ptHandler = new VcHelperPointXMLHandler();
444 }
445
446 /*
447 public static void main(String[] args) {
448 XMLConverter XMLConverter1 = new XMLConverter();
449 }*/
450
451 }
This page was automatically generated by Maven