OSDN Git Service

fixed: GUIを一新した
[importpicture/importpicture.git] / src / main / java / osm / jp / gpx / TagTrkpt.java
1 package osm.jp.gpx;
2
3 import java.text.ParseException;
4 import java.util.Date;
5
6 import org.w3c.dom.Document;
7 import org.w3c.dom.Element;
8 import org.w3c.dom.NamedNodeMap;
9 import org.w3c.dom.Node;
10 import org.w3c.dom.NodeList;
11
12 /**
13  * @code{
14  * <trkpt lat="35.32123832" lon="139.56965631">
15  *              <ele>47.20000076293945</ele>
16  *              <time>2012-06-15T03:00:29Z</time>
17  *              <magvar></magvar>
18  *              <speed></speed>
19  *      </trkpt>
20  * }
21  *
22  */
23 public class TagTrkpt {
24     public Element trkpt = null;
25     public Double lat = null;
26     public Double lon = null;
27     public String eleStr = null;
28     public Date time = null;
29     public String magvarStr = null;
30     public String speedStr = null;
31
32     public TagTrkpt(Element trkpt) {
33         this.trkpt = (Element) trkpt.cloneNode(true);
34                 
35         NamedNodeMap nodeMap = trkpt.getAttributes();
36         for (int j=0; j < nodeMap.getLength(); j++ ) {
37             switch (nodeMap.item(j).getNodeName()) {
38                 case "lat":
39                     String latStr = nodeMap.item(j).getNodeValue();
40                     this.lat = new Double(latStr);
41                     break;
42                 case "lon":
43                     String lonStr = nodeMap.item(j).getNodeValue();
44                     this.lon = new Double(lonStr);
45                     break;
46             }
47         }
48                 
49         NodeList nodes1 = trkpt.getChildNodes();
50         for (int i1=0; i1 < nodes1.getLength(); i1++) {
51             Node node1 = nodes1.item(i1);
52             NodeList nodes2 = node1.getChildNodes();
53             switch (node1.getNodeName()) {
54             case "ele":
55                 for (int i2=0; i2 < nodes2.getLength(); i2++) {
56                     Node node2 = nodes2.item(i2);
57                     if (node2 != null) {
58                         if (node2.getNodeType() == Node.TEXT_NODE) {
59                             this.eleStr = node2.getNodeValue();
60                         }
61                     }
62                 }
63                 break;
64             case "time":
65                 for (int i2=0; i2 < nodes2.getLength(); i2++) {
66                     Node node2 = nodes2.item(i2);
67                     if (node2 != null) {
68                         if (node2.getNodeType() == Node.TEXT_NODE) {
69                             try {
70                                 this.time = ImportPicture.toUTCDate(node2.getNodeValue());
71                             } catch (ParseException e) {
72                                 this.time = null;
73                             }
74                         }
75                     }
76                 }
77                 break;
78             case "magvar":
79                 for (int i2=0; i2 < nodes2.getLength(); i2++) {
80                     Node node2 = nodes2.item(i2);
81                     if (node2 != null) {
82                         if (node2.getNodeType() == Node.TEXT_NODE) {
83                             this.magvarStr = node2.getNodeValue();
84                         }
85                     }
86                 }
87                 break;
88             case "speed":
89                 for (int i2=0; i2 < nodes2.getLength(); i2++) {
90                     Node node2 = nodes2.item(i2);
91                     if (node2 != null) {
92                         if (node2.getNodeType() == Node.TEXT_NODE) {
93                             this.speedStr = node2.getNodeValue();
94                         }
95                     }
96                 }
97                 break;
98             }
99         }
100     }
101         
102     public void removeElement(String eleName) {
103         Node child;
104         for (child = trkpt.getFirstChild(); child != null; child = child.getNextSibling()) {
105             NodeList nodeList = child.getChildNodes();
106             for(int i = 0; i < nodeList.getLength(); i++) {
107                 Node grandChild = child.getChildNodes().item(i);
108                 if (grandChild.getNodeName().equals(eleName)) {
109                     child.removeChild(grandChild);
110                 }
111             }
112         }
113     }
114     
115     public void appendElement(String eleName, String valueStr) {
116         Document doc = trkpt.getOwnerDocument();
117         Element newElement = doc.createElement(eleName);
118         newElement.setTextContent(valueStr);
119         trkpt.appendChild(newElement);
120     }
121 }