View Javadoc

1   /*
2    * @(#)$Id: JdomGestureClass.java 794 2010-03-30 21:06:07Z bpuype $
3    *
4    * Author       :   Ueli Kurmann, igesture@uelikurmann.ch
5    *
6    * Purpose      : 	XML support for the GestureClass class.
7    *
8    * -----------------------------------------------------------------------
9    *
10   * Revision Information:
11   *
12   * Date             Who         Reason
13   *
14   * Dec 26, 2006     ukurmann    Initial Release
15   * Mar 22, 2007     bsigner     Cleanup
16   *
17   * -----------------------------------------------------------------------
18   *
19   * Copyright 1999-2009 ETH Zurich. All Rights Reserved.
20   *
21   * This software is the proprietary information of ETH Zurich.
22   * Use is subject to license terms.
23   * 
24   */
25  
26  
27  package org.ximtec.igesture.core.jdom;
28  
29  import java.util.List;
30  
31  import org.jdom.Element;
32  import org.ximtec.igesture.core.Descriptor;
33  import org.ximtec.igesture.core.GestureClass;
34  import org.ximtec.igesture.core.SampleDescriptor;
35  import org.ximtec.igesture.core.SampleDescriptor3D;
36  import org.ximtec.igesture.core.TextDescriptor;
37  
38  
39  /**
40   * XML support for the GestureClass class.
41   * 
42   * @version 1.0, Dec 2006
43   * @author Ueli Kurmann, igesture@uelikurmann.ch
44   * @author Beat Signer, signer@inf.ethz.ch
45   */
46  public class JdomGestureClass extends Element {
47  
48     public static final String ROOT_TAG = "class";
49  
50     public static final String NAME_ATTRIBUTE = "name";
51  
52     public static final String UUID_ATTRIBUTE = "id";
53  
54  
55     public JdomGestureClass(GestureClass gestureClass) {
56        super(ROOT_TAG);
57        setAttribute(NAME_ATTRIBUTE, gestureClass.getName());
58        setAttribute(UUID_ATTRIBUTE, gestureClass.getId());
59  
60        for (final Descriptor descriptor : gestureClass.getDescriptors()) {
61  
62           if (descriptor instanceof SampleDescriptor) {
63              addContent(new JdomSampleDescriptor((SampleDescriptor)descriptor));
64           }
65           else if(descriptor instanceof SampleDescriptor3D)
66           {
67          	addContent(new JdomSampleDescriptor3D((SampleDescriptor3D)descriptor));
68           }
69           else if (descriptor instanceof TextDescriptor) {
70              addContent(new JdomTextDescriptor((TextDescriptor)descriptor));
71           }
72           else
73           {
74          	 throw new RuntimeException("Could not save this descriptor type: "+descriptor.getType().getName());
75           }
76  
77        }
78  
79     }
80  
81  
82     @SuppressWarnings("unchecked")
83     public static GestureClass unmarshal(Element gestureClassElement) {
84        final String name = gestureClassElement.getAttributeValue(NAME_ATTRIBUTE);
85        final String uuid = gestureClassElement.getAttributeValue(UUID_ATTRIBUTE);
86        final GestureClass gestureClass = new GestureClass(name);
87        gestureClass.setId(uuid);
88  
89        for (final Element descriptorElement : (List<Element>)gestureClassElement
90              .getChildren(JdomSampleDescriptor.ROOT_TAG)) {
91           final SampleDescriptor sampleDescriptor = (SampleDescriptor)JdomSampleDescriptor
92                 .unmarshal(descriptorElement);
93           gestureClass.addDescriptor(sampleDescriptor);
94        }
95        
96        for (final Element descriptorElement : (List<Element>)gestureClassElement
97                .getChildren(JdomSampleDescriptor3D.ROOT_TAG)) {
98             final SampleDescriptor3D sampleDescriptor = (SampleDescriptor3D)JdomSampleDescriptor3D
99                   .unmarshal(descriptorElement);
100            gestureClass.addDescriptor(sampleDescriptor);
101         }
102 
103       for (final Element descriptorElement : (List<Element>)gestureClassElement
104             .getChildren(JdomTextDescriptor.ROOT_TAG)) {
105          final TextDescriptor textDescriptor = (TextDescriptor)JdomTextDescriptor
106                .unmarshal(descriptorElement);
107          gestureClass.addDescriptor(textDescriptor);
108       }
109 
110       return gestureClass;
111    } // unmarshal
112 
113 }