final NodeList nodes = (NodeList) xPath.evaluate("/html/body",
doc.getDocumentElement(), XPathConstants.NODESET);
if (nodes.getLength() != 1) {
throw new ServiceException("XHTML should be exactly 1 body element");
}
final Node body = nodes.item(0);
// build endpoint definitions
if (body.hasChildNodes()) {
ResourceDefinition currentEndpoint = null;
for (int i = 0; i < body.getChildNodes().getLength(); i++) {
final Node child = body.getChildNodes().item(i);
LOGGER.debug("Found {}: {}", child.getNodeName(), child.getTextContent());
if (child.getNodeType() == Node.ELEMENT_NODE) {
// <h1>My API</h1>
if (isApiTitle(child.getNodeName())) {
definition.setName(child.getTextContent());
LOGGER.info("Found API: {}", definition.getName());
}
// <h2>GET /message</h2>
if (isEndpointHTTP(child.getNodeName())) {
// start of a new endpoint
currentEndpoint = new ResourceDefinition();
definition.addEndpoint(currentEndpoint);
LOGGER.info("Found endpoint: {}", currentEndpoint.getName());
final String[] http = child.getTextContent().split(" ");
currentEndpoint.setUrl(http[1]);
final RequestDefinition request = new RequestDefinition();
currentEndpoint.getRequests().add(request);
request.setVerb(http[0]);
}
// <ul><li><p>Response 200 (text/plain)</p>
if (isEndpointResponse(child.getNodeName())) {
final ResponseDefinition response = parseResponseDefinition(child);
populateResponseBody(response, child);
currentEndpoint.getResponses().put(response.getCode(), response);
}
}
}
}
LOGGER.debug("Document parsed: {}", definition);
return definition;
} catch (XPathExpressionException e) {
throw new ServiceException("Error creating mock from document: " + doc, e);
}
}