comparison flys-client/src/main/java/de/intevation/flys/client/server/FixingsOverviewServiceImpl.java @ 2915:d20f16a430a9

Removed some XPath misuse. flys-client/trunk@4737 c6561f87-3c4e-4783-a992-168aeb5c3f6f
author Sascha L. Teichmann <sascha.teichmann@intevation.de>
date Thu, 21 Jun 2012 11:04:46 +0000
parents 256b458cc7ea
children 1f63e2ef03d2
comparison
equal deleted inserted replaced
2914:7d69e570e79b 2915:d20f16a430a9
1 package de.intevation.flys.client.server; 1 package de.intevation.flys.client.server;
2 2
3 import java.util.List;
4 import java.util.ArrayList;
5
6 import org.w3c.dom.Document;
7 import org.w3c.dom.Node;
8 import org.w3c.dom.NodeList;
9
10 import javax.xml.xpath.XPathConstants;
11
12 import com.google.gwt.user.server.rpc.RemoteServiceServlet; 3 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
13 4
14 import de.intevation.artifacts.common.utils.XMLUtils; 5 import de.intevation.artifacts.common.utils.XMLUtils;
15
16 import de.intevation.artifacts.common.utils.XSLTransformer; 6 import de.intevation.artifacts.common.utils.XSLTransformer;
17 7
18 import de.intevation.artifacts.httpclient.exceptions.ConnectionException; 8 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
19 9
20 import de.intevation.artifacts.httpclient.http.HttpClient; 10 import de.intevation.artifacts.httpclient.http.HttpClient;
22 12
23 import de.intevation.flys.client.client.services.FixingsOverviewService; 13 import de.intevation.flys.client.client.services.FixingsOverviewService;
24 14
25 import de.intevation.flys.client.shared.exceptions.ServerException; 15 import de.intevation.flys.client.shared.exceptions.ServerException;
26 16
27 import de.intevation.flys.client.shared.model.FixingsOverviewInfo;
28 import de.intevation.flys.client.shared.model.FixingsOverviewInfo.FixEvent; 17 import de.intevation.flys.client.shared.model.FixingsOverviewInfo.FixEvent;
29 import de.intevation.flys.client.shared.model.FixingsOverviewInfo.Sector; 18 import de.intevation.flys.client.shared.model.FixingsOverviewInfo.Sector;
30 19
20 import de.intevation.flys.client.shared.model.FixingsOverviewInfo;
21
31 import java.io.IOException; 22 import java.io.IOException;
32 import java.io.InputStream; 23 import java.io.InputStream;
33 24
25 import java.util.ArrayList;
26 import java.util.List;
27
28 import javax.xml.xpath.XPathConstants;
29
34 import org.apache.log4j.Logger; 30 import org.apache.log4j.Logger;
31
32 import org.w3c.dom.Document;
33 import org.w3c.dom.Element;
34 import org.w3c.dom.NodeList;
35 35
36 public class FixingsOverviewServiceImpl 36 public class FixingsOverviewServiceImpl
37 extends RemoteServiceServlet 37 extends RemoteServiceServlet
38 implements FixingsOverviewService 38 implements FixingsOverviewService
39 { 39 {
49 protected static final String XPATH_RIVER = "/fixings/river/@name"; 49 protected static final String XPATH_RIVER = "/fixings/river/@name";
50 protected static final String XPATH_RFROM = "/fixings/river/@from"; 50 protected static final String XPATH_RFROM = "/fixings/river/@from";
51 protected static final String XPATH_RTO = "/fixings/river/@to"; 51 protected static final String XPATH_RTO = "/fixings/river/@to";
52 52
53 protected static final String XPATH_EVENT = "/fixings/events/event"; 53 protected static final String XPATH_EVENT = "/fixings/events/event";
54 protected static final String XPATH_SECTORS = "sector"; 54
55
56 protected static final String XPATH_CID = "@cid";
57 protected static final String XPATH_DATE = "@date";
58 protected static final String XPATH_DESCRIPTION = "@description";
59 protected static final String XPATH_CLS = "@class";
60 protected static final String XPATH_FROM = "@from";
61 protected static final String XPATH_TO = "@to";
62 55
63 @Override 56 @Override
64 public FixingsOverviewInfo generateOverview( 57 public FixingsOverviewInfo generateOverview(
65 String locale, 58 String locale,
66 String uuid, 59 String uuid,
107 } 100 }
108 101
109 102
110 protected FixingsOverviewInfo getInfo( 103 protected FixingsOverviewInfo getInfo(
111 Document doc, 104 Document doc,
112 String uuid, 105 String uuid,
113 boolean checkboxes, 106 boolean checkboxes,
114 String callback 107 String callback
115 ) { 108 ) {
116 InputStream transform = 109
117 getServletContext().getResourceAsStream(XSL_TRANSFORM); 110 InputStream transform = getServletContext()
118 XSLTransformer xformer = new XSLTransformer(); 111 .getResourceAsStream(XSL_TRANSFORM);
119 xformer.addParameter("project-uuid", uuid); 112
120 xformer.addParameter( 113 if (transform == null) {
121 "render-checkboxes", checkboxes ? Boolean.TRUE : Boolean.FALSE); 114 log.warn("transform not found");
122 xformer.addParameter("callback", callback); 115 return null;
123 String result = xformer.transform(doc, transform); 116 }
124 117
125 try { transform.close(); } 118 String result = null;
126 catch (IOException ioe) {} 119 try {
127 120 XSLTransformer xformer = new XSLTransformer();
128 int rid = -1; 121 xformer.addParameter("project-uuid", uuid);
122 xformer.addParameter(
123 "render-checkboxes",
124 checkboxes ? Boolean.TRUE : Boolean.FALSE);
125 xformer.addParameter("callback", callback);
126 result = xformer.transform(doc, transform);
127 }
128 finally {
129 try { transform.close(); }
130 catch (IOException ioe) {}
131 }
132
133 int rid = -1;
129 double from = -1; 134 double from = -1;
130 double to = -1; 135 double to = -1;
131 String rid_str = XMLUtils.xpathString(doc, XPATH_RID, null); 136
132 String river = XMLUtils.xpathString(doc, XPATH_RIVER, null); 137 String rid_str = XMLUtils.xpathString(doc, XPATH_RID, null);
138 String river = XMLUtils.xpathString(doc, XPATH_RIVER, null);
133 String from_str = XMLUtils.xpathString(doc, XPATH_RFROM, null); 139 String from_str = XMLUtils.xpathString(doc, XPATH_RFROM, null);
134 String to_str = XMLUtils.xpathString(doc, XPATH_RTO, null); 140 String to_str = XMLUtils.xpathString(doc, XPATH_RTO, null);
141
135 try { 142 try {
136 rid = Integer.valueOf(rid_str).intValue(); 143 rid = Integer.parseInt(rid_str);
137 from = Double.valueOf(from_str).doubleValue(); 144 from = Double.parseDouble(from_str);
138 to = Double.valueOf(to_str).doubleValue(); 145 to = Double.parseDouble(to_str);
139 } 146 }
140 catch(NumberFormatException nfe) { 147 catch(NumberFormatException nfe) {
141 log.warn(nfe, nfe); 148 log.warn(nfe, nfe);
142 } 149 }
143 150
162 if (events == null || events.getLength() == 0) { 169 if (events == null || events.getLength() == 0) {
163 log.warn("No events in Overview!"); 170 log.warn("No events in Overview!");
164 return null; 171 return null;
165 } 172 }
166 173
167 List<FixEvent> list = 174 List<FixEvent> list = new ArrayList<FixEvent>();
168 new ArrayList<FixEvent>(); 175 for (int i = 0, E = events.getLength(); i < E; i++) {
169 for (int i = 0; i < events.getLength(); i++) { 176 Element n = (Element)events.item(i);
170 Node n = events.item(i);
171 List<Sector> sectors = getSectors(n); 177 List<Sector> sectors = getSectors(n);
172 String cid = XMLUtils.xpathString(n, XPATH_CID, null); 178 String cid = n.getAttribute("cid");
173 log.debug("'" + cid + "'"); 179 String date = n.getAttribute("date");;
174 String date = XMLUtils.xpathString(n, XPATH_DATE, null); 180 String name = n.getAttribute("description");
175 String name = XMLUtils.xpathString(n, XPATH_DESCRIPTION, null); 181 list.add(new FixEvent( cid, date, name, sectors));
176 list.add(new FixEvent(
177 cid,
178 date,
179 name,
180 sectors));
181 } 182 }
182 return list; 183 return list;
183 } 184 }
184 185
185 protected List<Sector> getSectors(Node event) { 186 protected List<Sector> getSectors(Element event) {
186 NodeList sectors = (NodeList) XMLUtils.xpath( 187 NodeList sectors = event.getElementsByTagName("sector");
187 event, 188
188 XPATH_SECTORS, 189 if (sectors.getLength() == 0) {
189 XPathConstants.NODESET,
190 null);
191 if (sectors == null || sectors.getLength() == 0) {
192 log.warn("No Sectors in Event!"); 190 log.warn("No Sectors in Event!");
193 return null; 191 return null;
194 } 192 }
195 193
196 List<Sector> list = 194 List<Sector> list = new ArrayList<Sector>();
197 new ArrayList<Sector>(); 195 for (int i = 0, S = sectors.getLength(); i < S; i++) {
198 for (int i = 0; i < sectors.getLength(); i++) { 196 Element n = (Element)sectors.item(i);
199 Node n = sectors.item(i); 197 int cls = -1;
200 int cls = -1;
201 double from = -1; 198 double from = -1;
202 double to = -1; 199 double to = -1;
203 String cls_str = XMLUtils.xpathString(n, XPATH_CLS, null); 200 String cls_str = n.getAttribute("class");
204 String from_str = XMLUtils.xpathString(n, XPATH_FROM, null); 201 String from_str = n.getAttribute("from");
205 String to_str = XMLUtils.xpathString(n, XPATH_TO, null); 202 String to_str = n.getAttribute("to");
206 try { 203 try {
207 cls = Integer.valueOf(cls_str).intValue(); 204 cls = Integer.parseInt(cls_str);
208 from = Double.valueOf(from_str).doubleValue(); 205 from = Double.parseDouble(from_str);
209 to = Double.valueOf(to_str).doubleValue(); 206 to = Double.parseDouble(to_str);
210 } 207 }
211 catch(NumberFormatException nfe) { 208 catch(NumberFormatException nfe) {
212 log.warn(nfe, nfe); 209 log.warn(nfe, nfe);
213 } 210 }
214 list.add(new Sector( 211 list.add(new Sector(cls, from, to));
215 cls,
216 from,
217 to));
218 } 212 }
219 return list; 213 return list;
220 } 214 }
221 } 215 }
222 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 : 216 // vim:set ts=4 sw=4 si et sta sts=4 fenc=utf8 :

http://dive4elements.wald.intevation.org