changeset 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 7d69e570e79b
children 1f63e2ef03d2
files flys-client/ChangeLog flys-client/src/main/java/de/intevation/flys/client/server/FixingsOverviewServiceImpl.java
diffstat 2 files changed, 74 insertions(+), 75 deletions(-) [+]
line wrap: on
line diff
--- a/flys-client/ChangeLog	Thu Jun 21 09:12:53 2012 +0000
+++ b/flys-client/ChangeLog	Thu Jun 21 11:04:46 2012 +0000
@@ -1,3 +1,8 @@
+2012-06-21	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
+
+	* src/main/java/de/intevation/flys/client/server/FixingsOverviewServiceImpl.java:
+	  Removed some XPath misuse.
+
 2012-06-21	Sascha L. Teichmann	<sascha.teichmann@intevation.de>
 
 	* src/main/java/de/intevation/flys/client/client/ui/fixation/FixationPanel.java:
--- a/flys-client/src/main/java/de/intevation/flys/client/server/FixingsOverviewServiceImpl.java	Thu Jun 21 09:12:53 2012 +0000
+++ b/flys-client/src/main/java/de/intevation/flys/client/server/FixingsOverviewServiceImpl.java	Thu Jun 21 11:04:46 2012 +0000
@@ -1,18 +1,8 @@
 package de.intevation.flys.client.server;
 
-import java.util.List;
-import java.util.ArrayList;
-
-import org.w3c.dom.Document;
-import org.w3c.dom.Node;
-import org.w3c.dom.NodeList;
-
-import javax.xml.xpath.XPathConstants;
-
 import com.google.gwt.user.server.rpc.RemoteServiceServlet;
 
 import de.intevation.artifacts.common.utils.XMLUtils;
-
 import de.intevation.artifacts.common.utils.XSLTransformer;
 
 import de.intevation.artifacts.httpclient.exceptions.ConnectionException;
@@ -24,15 +14,25 @@
 
 import de.intevation.flys.client.shared.exceptions.ServerException;
 
-import de.intevation.flys.client.shared.model.FixingsOverviewInfo;
 import de.intevation.flys.client.shared.model.FixingsOverviewInfo.FixEvent;
 import de.intevation.flys.client.shared.model.FixingsOverviewInfo.Sector;
 
+import de.intevation.flys.client.shared.model.FixingsOverviewInfo;
+
 import java.io.IOException;
 import java.io.InputStream;
 
+import java.util.ArrayList;
+import java.util.List;
+
+import javax.xml.xpath.XPathConstants;
+
 import org.apache.log4j.Logger;
 
+import org.w3c.dom.Document;
+import org.w3c.dom.Element;
+import org.w3c.dom.NodeList;
+
 public class FixingsOverviewServiceImpl
 extends      RemoteServiceServlet
 implements   FixingsOverviewService
@@ -51,14 +51,7 @@
     protected static final String XPATH_RTO = "/fixings/river/@to";
 
     protected static final String XPATH_EVENT = "/fixings/events/event";
-    protected static final String XPATH_SECTORS = "sector";
 
-    protected static final String XPATH_CID = "@cid";
-    protected static final String XPATH_DATE = "@date";
-    protected static final String XPATH_DESCRIPTION = "@description";
-    protected static final String XPATH_CLS = "@class";
-    protected static final String XPATH_FROM = "@from";
-    protected static final String XPATH_TO = "@to";
 
     @Override
     public FixingsOverviewInfo generateOverview(
@@ -109,33 +102,47 @@
 
     protected FixingsOverviewInfo getInfo(
         Document doc,
-        String uuid,
-        boolean checkboxes,
-        String callback
+        String   uuid,
+        boolean  checkboxes,
+        String   callback
     ) {
-        InputStream transform =
-            getServletContext().getResourceAsStream(XSL_TRANSFORM);
-        XSLTransformer xformer = new XSLTransformer();
-        xformer.addParameter("project-uuid", uuid);
-        xformer.addParameter(
-            "render-checkboxes", checkboxes ? Boolean.TRUE : Boolean.FALSE);
-        xformer.addParameter("callback", callback);
-        String result = xformer.transform(doc, transform);
 
-        try { transform.close(); }
-        catch (IOException ioe) {}
+        InputStream transform = getServletContext()
+            .getResourceAsStream(XSL_TRANSFORM);
 
-        int rid = -1;
+        if (transform == null) {
+            log.warn("transform not found");
+            return null;
+        }
+
+        String result = null;
+        try {
+            XSLTransformer xformer = new XSLTransformer();
+            xformer.addParameter("project-uuid", uuid);
+            xformer.addParameter(
+                "render-checkboxes",
+                checkboxes ? Boolean.TRUE : Boolean.FALSE);
+            xformer.addParameter("callback", callback);
+            result = xformer.transform(doc, transform);
+        }
+        finally {
+            try { transform.close(); }
+            catch (IOException ioe) {}
+        }
+
+        int    rid  = -1;
         double from = -1;
-        double to = -1;
-        String rid_str = XMLUtils.xpathString(doc, XPATH_RID, null);
-        String river = XMLUtils.xpathString(doc, XPATH_RIVER, null);
+        double to   = -1;
+
+        String rid_str  = XMLUtils.xpathString(doc, XPATH_RID, null);
+        String river    = XMLUtils.xpathString(doc, XPATH_RIVER, null);
         String from_str = XMLUtils.xpathString(doc, XPATH_RFROM, null);
-        String to_str = XMLUtils.xpathString(doc, XPATH_RTO, null);
+        String to_str   = XMLUtils.xpathString(doc, XPATH_RTO, null);
+
         try {
-            rid = Integer.valueOf(rid_str).intValue();
-            from = Double.valueOf(from_str).doubleValue();
-            to = Double.valueOf(to_str).doubleValue();
+            rid = Integer.parseInt(rid_str);
+            from = Double.parseDouble(from_str);
+            to = Double.parseDouble(to_str);
         }
         catch(NumberFormatException nfe) {
             log.warn(nfe, nfe);
@@ -164,57 +171,44 @@
             return null;
         }
 
-        List<FixEvent> list =
-            new ArrayList<FixEvent>();
-        for (int i = 0; i < events.getLength(); i++) {
-            Node n = events.item(i);
+        List<FixEvent> list = new ArrayList<FixEvent>();
+        for (int i = 0, E = events.getLength(); i < E; i++) {
+            Element n = (Element)events.item(i);
             List<Sector> sectors = getSectors(n);
-            String cid = XMLUtils.xpathString(n, XPATH_CID, null);
-            log.debug("'" + cid + "'");
-            String date = XMLUtils.xpathString(n, XPATH_DATE, null);
-            String name = XMLUtils.xpathString(n, XPATH_DESCRIPTION, null);
-            list.add(new FixEvent(
-                cid,
-                date,
-                name,
-                sectors));
+            String cid  = n.getAttribute("cid");
+            String date = n.getAttribute("date");;
+            String name = n.getAttribute("description");
+            list.add(new FixEvent( cid, date, name, sectors));
         }
         return list;
     }
 
-    protected List<Sector> getSectors(Node event) {
-        NodeList sectors = (NodeList) XMLUtils.xpath(
-            event,
-            XPATH_SECTORS,
-            XPathConstants.NODESET,
-            null);
-        if (sectors == null || sectors.getLength() == 0) {
+    protected List<Sector> getSectors(Element event) {
+        NodeList sectors = event.getElementsByTagName("sector");
+
+        if (sectors.getLength() == 0) {
             log.warn("No Sectors in Event!");
             return null;
         }
 
-        List<Sector> list =
-            new ArrayList<Sector>();
-        for (int i = 0; i < sectors.getLength(); i++) {
-            Node n = sectors.item(i);
-            int cls = -1;
+        List<Sector> list = new ArrayList<Sector>();
+        for (int i = 0, S = sectors.getLength(); i < S; i++) {
+            Element n = (Element)sectors.item(i);
+            int    cls  = -1;
             double from = -1;
-            double to = -1;
-            String cls_str = XMLUtils.xpathString(n, XPATH_CLS, null);
-            String from_str = XMLUtils.xpathString(n, XPATH_FROM, null);
-            String to_str = XMLUtils.xpathString(n, XPATH_TO, null);
+            double to   = -1;
+            String cls_str  = n.getAttribute("class");
+            String from_str = n.getAttribute("from");
+            String to_str   = n.getAttribute("to");
             try {
-                cls = Integer.valueOf(cls_str).intValue();
-                from = Double.valueOf(from_str).doubleValue();
-                to = Double.valueOf(to_str).doubleValue();
+                cls  = Integer.parseInt(cls_str);
+                from = Double.parseDouble(from_str);
+                to   = Double.parseDouble(to_str);
             }
             catch(NumberFormatException nfe) {
                 log.warn(nfe, nfe);
             }
-            list.add(new Sector(
-                cls,
-                from,
-                to));
+            list.add(new Sector(cls, from, to));
         }
         return list;
     }

http://dive4elements.wald.intevation.org