1616
1717package com .mycompany .hr .ws ;
1818
19+ import java .text .ParseException ;
1920import java .text .SimpleDateFormat ;
21+ import java .util .Arrays ;
2022import java .util .Date ;
2123
2224import org .springframework .beans .factory .annotation .Autowired ;
2527import org .springframework .ws .server .endpoint .annotation .RequestPayload ;
2628
2729import com .mycompany .hr .service .HumanResourceService ;
28- import org .jdom .Element ;
29- import org .jdom .JDOMException ;
30- import org .jdom .Namespace ;
31- import org .jdom .xpath .XPath ;
30+ import org .jdom2 .Element ;
31+ import org .jdom2 .JDOMException ;
32+ import org .jdom2 .Namespace ;
33+ import org .jdom2 .filter .Filters ;
34+ import org .jdom2 .xpath .XPathExpression ;
35+ import org .jdom2 .xpath .XPathFactory ;
3236
3337/**
3438 * This endpoint handles holiday requests. It uses a combination of JDOM and XPath to extract interesting pieces of XML
@@ -41,34 +45,44 @@ public class HolidayEndpoint {
4145
4246 private static final String NAMESPACE_URI = "http://mycompany.com/hr/schemas" ;
4347
44- private XPath startDateExpression ;
48+ private XPathExpression < Element > startDateExpression ;
4549
46- private XPath endDateExpression ;
50+ private XPathExpression < Element > endDateExpression ;
4751
48- private XPath nameExpression ;
52+ private XPathExpression <Element > firstNameExpression ;
53+
54+ private XPathExpression <Element > lastNameExpression ;
4955
5056 private HumanResourceService humanResourceService ;
5157
5258 @ Autowired
5359 public HolidayEndpoint (HumanResourceService humanResourceService ) throws JDOMException {
5460 this .humanResourceService = humanResourceService ;
5561 Namespace namespace = Namespace .getNamespace ("hr" , NAMESPACE_URI );
56- startDateExpression = XPath .newInstance ("//hr:StartDate" );
57- startDateExpression .addNamespace (namespace );
58- endDateExpression = XPath .newInstance ("//hr:EndDate" );
59- endDateExpression .addNamespace (namespace );
60- nameExpression = XPath .newInstance ("concat(//hr:FirstName,' ',//hr:LastName)" );
61- nameExpression .addNamespace (namespace );
62+ XPathFactory xPathFactory = XPathFactory .instance ();
63+ startDateExpression = xPathFactory .compile ("//hr:StartDate" , Filters .element (), null , namespace );
64+ endDateExpression = xPathFactory .compile ("//hr:EndDate" , Filters .element (), null , namespace );
65+ firstNameExpression = xPathFactory .compile ("//hr:FirstName" , Filters .element (), null , namespace );
66+ lastNameExpression = xPathFactory .compile ("//hr:LastName" , Filters .element (), null , namespace );
6267 }
6368
6469 @ PayloadRoot (namespace = NAMESPACE_URI , localPart = "HolidayRequest" )
6570 public void handleHolidayRequest (@ RequestPayload Element holidayRequest ) throws Exception {
66- SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd" );
67- Date startDate = dateFormat .parse (startDateExpression .valueOf (holidayRequest ));
68- Date endDate = dateFormat .parse (endDateExpression .valueOf (holidayRequest ));
69- String name = nameExpression .valueOf (holidayRequest );
71+ Date startDate = parseDate (startDateExpression , holidayRequest );
72+ Date endDate = parseDate (endDateExpression , holidayRequest );
73+ String name = firstNameExpression .evaluateFirst (holidayRequest ).getText () + " " + lastNameExpression .evaluateFirst (holidayRequest ).getText ();
7074
7175 humanResourceService .bookHoliday (startDate , endDate , name );
7276 }
7377
78+ private Date parseDate (XPathExpression <Element > expression , Element element ) throws ParseException {
79+ Element result = expression .evaluateFirst (element );
80+ if (result != null ) {
81+ SimpleDateFormat dateFormat = new SimpleDateFormat ("yyyy-MM-dd" );
82+ return dateFormat .parse (result .getText ());
83+ } else {
84+ throw new IllegalArgumentException ("Could not evaluate [" + expression + "] on [" + element + "]" );
85+ }
86+ }
87+
7488}
0 commit comments