1 | package com.medias.xml.metadata; |
---|
2 | |
---|
3 | import com.medias.ConversionException; |
---|
4 | import com.medias.utils.Entity; |
---|
5 | import org.jdom.Element; |
---|
6 | |
---|
7 | import java.util.ArrayList; |
---|
8 | import java.util.Iterator; |
---|
9 | import java.util.List; |
---|
10 | |
---|
11 | /** |
---|
12 | * @author combaz |
---|
13 | * |
---|
14 | * Created on 24 févr. 2005 |
---|
15 | */ |
---|
16 | public class Contenu implements Entity |
---|
17 | { |
---|
18 | /** |
---|
19 | * |
---|
20 | */ |
---|
21 | private static final long serialVersionUID = 1L; |
---|
22 | private ArrayList<Plateforme> plateformes; |
---|
23 | private ArrayList<Parametre> params; |
---|
24 | private ArrayList<Capteur> capteurs; |
---|
25 | |
---|
26 | |
---|
27 | public Contenu() { |
---|
28 | super(); |
---|
29 | this.plateformes = new ArrayList<Plateforme>(); |
---|
30 | this.params = new ArrayList<Parametre>(); |
---|
31 | this.capteurs = new ArrayList<Capteur>(); |
---|
32 | } |
---|
33 | |
---|
34 | public ArrayList<Capteur> getCapteurs() { |
---|
35 | return this.capteurs; |
---|
36 | } |
---|
37 | |
---|
38 | /** |
---|
39 | * @param capteurs The capteurs to set. |
---|
40 | */ |
---|
41 | public void setCapteurs(ArrayList<Capteur> capteurs) { |
---|
42 | this.capteurs = capteurs; |
---|
43 | } |
---|
44 | |
---|
45 | public ArrayList<Parametre> getParams() { |
---|
46 | return this.params; |
---|
47 | } |
---|
48 | /** |
---|
49 | * @param params The params to set. |
---|
50 | */ |
---|
51 | public void setParams(ArrayList<Parametre> params) { |
---|
52 | this.params = params; |
---|
53 | } |
---|
54 | |
---|
55 | public ArrayList<Plateforme> getPlateformes() { |
---|
56 | return this.plateformes; |
---|
57 | } |
---|
58 | |
---|
59 | /** |
---|
60 | * @param plateformes The plateformes to set. |
---|
61 | */ |
---|
62 | public void setPlateformes(ArrayList<Plateforme> plateformes) { |
---|
63 | this.plateformes = plateformes; |
---|
64 | } |
---|
65 | |
---|
66 | public void addPlateforme() { |
---|
67 | Plateforme plat = new Plateforme(); |
---|
68 | this.plateformes.add(plat); |
---|
69 | } |
---|
70 | |
---|
71 | public void removePlateforme(Integer idx) { |
---|
72 | this.plateformes.remove(idx.intValue()); |
---|
73 | } |
---|
74 | |
---|
75 | public void removePlateforme(int idx) { |
---|
76 | this.plateformes.remove(idx); |
---|
77 | } |
---|
78 | |
---|
79 | public void addParam() { |
---|
80 | Parametre param = new Parametre(); |
---|
81 | this.params.add(param); |
---|
82 | } |
---|
83 | |
---|
84 | public void removeParam(Integer idx) { |
---|
85 | this.params.remove(idx.intValue()); |
---|
86 | } |
---|
87 | |
---|
88 | public void removeParam(int idx) { |
---|
89 | this.params.remove(idx); |
---|
90 | } |
---|
91 | |
---|
92 | public void addCapteur() { |
---|
93 | Capteur capt = new Capteur(); |
---|
94 | this.capteurs.add(capt); |
---|
95 | } |
---|
96 | |
---|
97 | public void removeCapteur(Integer idx) { |
---|
98 | this.capteurs.remove(idx.intValue()); |
---|
99 | } |
---|
100 | |
---|
101 | public void removeCapteur(int idx) { |
---|
102 | this.capteurs.remove(idx); |
---|
103 | } |
---|
104 | |
---|
105 | public Element toXmlElement(String title) { |
---|
106 | Element plat_elem = new Element("liste_plateformes"); |
---|
107 | ArrayList<Element> pl = new ArrayList<Element>(); |
---|
108 | for(int i = 0; i<this.plateformes.size(); i++) { |
---|
109 | Plateforme plat = (Plateforme)(this.plateformes.get(i)); |
---|
110 | Element elem = plat.toXmlElement("plateforme"); |
---|
111 | pl.add(elem); |
---|
112 | } |
---|
113 | plat_elem.addContent(pl); |
---|
114 | |
---|
115 | Element param_elem = new Element("liste_params"); |
---|
116 | ArrayList<Element> pa = new ArrayList<Element>(); |
---|
117 | for(int i = 0; i<this.params.size(); i++) { |
---|
118 | Parametre param = (Parametre)(this.params.get(i)); |
---|
119 | Element elem = param.toXmlElement("param"); |
---|
120 | pa.add(elem); |
---|
121 | } |
---|
122 | param_elem.addContent(pa); |
---|
123 | |
---|
124 | Element capt_elem = new Element("liste_capteurs"); |
---|
125 | ArrayList<Element> ca = new ArrayList<Element>(); |
---|
126 | for(int i = 0; i<this.capteurs.size(); i++) { |
---|
127 | Capteur capt = (Capteur)(this.capteurs.get(i)); |
---|
128 | Element elem = capt.toXmlElement("capteur"); |
---|
129 | ca.add(elem); |
---|
130 | } |
---|
131 | capt_elem.addContent(ca); |
---|
132 | |
---|
133 | Element metadata = new Element(title); |
---|
134 | metadata.addContent(plat_elem); |
---|
135 | metadata.addContent(param_elem); |
---|
136 | metadata.addContent(capt_elem); |
---|
137 | |
---|
138 | return metadata; |
---|
139 | } |
---|
140 | |
---|
141 | public void fromXmlElement(Element el) throws ConversionException { |
---|
142 | Element plat_elem = el.getChild("liste_plateformes"); |
---|
143 | Element par_elem = el.getChild("liste_params"); |
---|
144 | Element capt_elem = el.getChild("liste_capteurs"); |
---|
145 | |
---|
146 | if((plat_elem==null)||(par_elem==null)||(capt_elem==null)) |
---|
147 | throw new ConversionException(ConversionException.ERROR_fromXml + "Metadata !"); |
---|
148 | |
---|
149 | else { |
---|
150 | List<?> pl = plat_elem.getChildren("plateforme"); |
---|
151 | Iterator<?> plit = pl.iterator(); |
---|
152 | while(plit.hasNext()) { |
---|
153 | Element elem = (Element) plit.next(); |
---|
154 | Plateforme plat = new Plateforme(); |
---|
155 | plat.fromXmlElement(elem); |
---|
156 | this.plateformes.add(plat); |
---|
157 | } |
---|
158 | |
---|
159 | List<?> pa = par_elem.getChildren("param"); |
---|
160 | Iterator<?> pait = pa.iterator(); |
---|
161 | while(pait.hasNext()) { |
---|
162 | Element elem = (Element) pait.next(); |
---|
163 | Parametre param = new Parametre(); |
---|
164 | param.fromXmlElement(elem); |
---|
165 | this.params.add(param); |
---|
166 | } |
---|
167 | |
---|
168 | List<?> ca = capt_elem.getChildren("capteur"); |
---|
169 | Iterator<?> cait = ca.iterator(); |
---|
170 | while(cait.hasNext()) { |
---|
171 | Element elem = (Element) cait.next(); |
---|
172 | Capteur capt = new Capteur(); |
---|
173 | capt.fromXmlElement(elem); |
---|
174 | this.capteurs.add(capt); |
---|
175 | } |
---|
176 | } |
---|
177 | } |
---|
178 | } |
---|