Skip to content

Commit be5b079

Browse files
committed
Upgraded to JDOM 2.0.1
1 parent b76851f commit be5b079

File tree

2 files changed

+33
-19
lines changed

2 files changed

+33
-19
lines changed

samples/tutorial/src/main/java/com/mycompany/hr/ws/HolidayEndpoint.java

Lines changed: 31 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,9 @@
1616

1717
package com.mycompany.hr.ws;
1818

19+
import java.text.ParseException;
1920
import java.text.SimpleDateFormat;
21+
import java.util.Arrays;
2022
import java.util.Date;
2123

2224
import org.springframework.beans.factory.annotation.Autowired;
@@ -25,10 +27,12 @@
2527
import org.springframework.ws.server.endpoint.annotation.RequestPayload;
2628

2729
import 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
}

samples/tutorial/src/test/java/com/mycompany/hr/ws/HolidayEndpointTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,8 @@
2020
import java.util.Calendar;
2121

2222
import com.mycompany.hr.service.HumanResourceService;
23-
import org.jdom.Document;
24-
import org.jdom.input.SAXBuilder;
23+
import org.jdom2.Document;
24+
import org.jdom2.input.SAXBuilder;
2525
import org.junit.Before;
2626
import org.junit.Test;
2727

0 commit comments

Comments
 (0)