View Javadoc

1   /**
2    * 
3    */
4   package org.ximtec.igesture.util.additions3d.jdom;
5   
6   import org.jdom.Element;
7   import org.sigtec.jdom.element.JdomDoubleElement;
8   import org.sigtec.jdom.element.JdomLongElement;
9   import org.ximtec.igesture.util.additions3d.AccelerationSample;
10  
11  /**
12   * Constructs an XML representation for a AccelerationSample object. The 'timestamp' element is optional.
13   * @author Bjorn Puype, bpuype@gmail.com
14   *
15   */
16  public class JdomAccelerationSample extends Element {
17  	
18  	   public static final String ROOT_TAG = "sampleAcc";
19  	   public static final String ACC_ELEMENT_X = "x";
20  	   public static final String ACC_ELEMENT_Y = "y";
21  	   public static final String ACC_ELEMENT_Z = "z";
22  	   public static final String ACC_ELEMENT_TIMESTAMP = "timestamp";
23  
24  	   public JdomAccelerationSample(AccelerationSample sample) {
25  	      super(ROOT_TAG);
26  	      addContent(new JdomDoubleElement(ACC_ELEMENT_X, sample.getXAcceleration()));
27  	      addContent(new JdomDoubleElement(ACC_ELEMENT_Y, sample.getYAcceleration()));
28  	      addContent(new JdomDoubleElement(ACC_ELEMENT_Z, sample.getZAcceleration()));
29  
30  	      if (sample.hasTimeStamp()) {
31  	         addContent(new JdomLongElement(ACC_ELEMENT_TIMESTAMP, sample.getTimeStamp()));
32  	      }
33  
34  	   } // JdomAccelerationSample
35  
36  
37  	   public static AccelerationSample unmarshal(Element sample) {
38  		   AccelerationSample newSample = new AccelerationSample(Double.parseDouble(sample
39  	            .getChildText(ACC_ELEMENT_X)), Double.parseDouble(sample
40  	            .getChildText(ACC_ELEMENT_Y)), Double.parseDouble(sample
41  	                    .getChildText(ACC_ELEMENT_Z)), 0);
42  	      setTimestamp(newSample, sample);
43  	      return newSample;
44  	   } // unmarshal
45  
46  
47  	   private static void setTimestamp(AccelerationSample sample, Element source) {
48  	      Element timestamp = source.getChild(ACC_ELEMENT_TIMESTAMP);
49  
50  	      if (timestamp != null) {
51  	         sample.setTimeStamp(Long.parseLong(timestamp.getText()));
52  	      }
53  
54  	   } // setTimestamp
55  
56  }