* @return
* @throws ServiceException
*/
private MockDefinition createMock(Document doc) throws ServiceException {
try {
final MockDefinition definition = new MockDefinition();
// get body from document
final XPath xPath = XPathFactory.newInstance().newXPath();
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]);