Mercurial > dive4elements > river
comparison artifacts/src/main/java/org/dive4elements/river/exports/injector/KmFromRequestInjector.java @ 8349:7341bebc79d0
Let injectors be strictly distinct from each other and give more meaningful names (they know what they do, not where they will be used. The latter is configuration).
author | Tom Gottfried <tom@intevation.de> |
---|---|
date | Wed, 01 Oct 2014 12:23:39 +0200 |
parents | artifacts/src/main/java/org/dive4elements/river/exports/injector/FixWQInjector.java@10917ec29625 |
children | 16dc2da05aef |
comparison
equal
deleted
inserted
replaced
8348:dc3f55141299 | 8349:7341bebc79d0 |
---|---|
1 package org.dive4elements.river.exports.injector; | |
2 | |
3 import org.apache.log4j.Logger; | |
4 import org.dive4elements.artifacts.Artifact; | |
5 import org.dive4elements.artifacts.ArtifactNamespaceContext; | |
6 import org.dive4elements.artifacts.CallContext; | |
7 import org.dive4elements.artifacts.ContextInjector; | |
8 import org.dive4elements.artifacts.common.utils.XMLUtils; | |
9 import org.dive4elements.river.artifacts.D4EArtifact; | |
10 import org.dive4elements.river.artifacts.access.RangeAccess; | |
11 import org.w3c.dom.Document; | |
12 import org.w3c.dom.Element; | |
13 | |
14 import static org.dive4elements.river.exports.injector.InjectorConstants.CURRENT_KM; | |
15 | |
16 | |
17 public class KmFromRequestInjector | |
18 implements ContextInjector | |
19 { | |
20 private static final Logger log = Logger.getLogger(KmFromRequestInjector.class); | |
21 | |
22 public static final Double INVALID_KM = Double.valueOf(-1d); | |
23 public static final String XPATH_CHART_CURRENTKM = | |
24 "/art:action/art:attributes/art:currentKm/@art:km"; | |
25 @Override | |
26 public void setup(Element cfg) { | |
27 | |
28 } | |
29 | |
30 @Override | |
31 public void injectContext( | |
32 CallContext ctx, | |
33 Artifact artifact, | |
34 Document request) { | |
35 Double km = getCurrentKmFromRequest(request); | |
36 if (Double.isNaN(km)) { | |
37 RangeAccess access = new RangeAccess((D4EArtifact)artifact); | |
38 km = access.getFrom(); | |
39 } | |
40 ctx.putContextValue(CURRENT_KM, km); | |
41 } | |
42 | |
43 private Double getCurrentKmFromRequest(Document request) { | |
44 | |
45 String km = XMLUtils.xpathString( | |
46 request, | |
47 XPATH_CHART_CURRENTKM, | |
48 ArtifactNamespaceContext.INSTANCE); | |
49 | |
50 if (km == null) { | |
51 return Double.NaN; | |
52 } | |
53 | |
54 try { | |
55 return Double.valueOf(km); | |
56 } | |
57 catch (NumberFormatException nfe) { | |
58 return Double.NaN; | |
59 } | |
60 } | |
61 } |