Guest User

TestingServlet.java

a guest
Aug 7th, 2014
343
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.49 KB | None | 0 0
  1. package test;
  2.  
  3.  
  4. import java.io.ByteArrayInputStream;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.StringReader;
  8. import java.net.MalformedURLException;
  9. import java.net.URL;
  10. import java.nio.charset.StandardCharsets;
  11.  
  12. import javax.servlet.ServletConfig;
  13. import javax.servlet.ServletException;
  14. import javax.servlet.http.HttpServlet;
  15. import javax.servlet.http.HttpServletRequest;
  16. import javax.servlet.http.HttpServletResponse;
  17. import javax.xml.parsers.DocumentBuilder;
  18. import javax.xml.parsers.DocumentBuilderFactory;
  19. import javax.xml.parsers.ParserConfigurationException;
  20. import javax.xml.transform.Source;
  21. import javax.xml.transform.stream.StreamSource;
  22. import javax.xml.validation.Schema;
  23. import javax.xml.validation.SchemaFactory;
  24. import javax.xml.validation.Validator;
  25. import javax.xml.xpath.XPath;
  26. import javax.xml.xpath.XPathFactory;
  27.  
  28. import org.w3c.dom.Document;
  29. import org.xml.sax.SAXException;
  30.  
  31.  
  32. /*
  33. * A blank servlet to make it easy to quickly test code snippets. For developers use.
  34. */
  35. public class TestingServlet extends HttpServlet {
  36.  
  37. String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><computer><keyboard>105 keys</keyboard><mouse>3 button</mouse><monitor>vga</monitor></computer>";
  38.  
  39. static URL xsdPath;
  40.  
  41. @Override public void init(ServletConfig config) throws ServletException {
  42. super.init(config);
  43. try {
  44. xsdPath = config.getServletContext().getResource("/WEB-INF/test.xsd");
  45. } catch (MalformedURLException e) {
  46. throw new ServletException("Exception during servlet initilization!", e);
  47. }
  48. }
  49.  
  50. @Override
  51. protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
  52. try {
  53. SchemaFactory factory = SchemaFactory.newInstance("http://www.w3.org/2001/XMLSchema");
  54. Schema schema = factory.newSchema(xsdPath);
  55. Validator validator = schema.newValidator();
  56. Source source = new StreamSource(new StringReader(xml));
  57. validator.validate(source);
  58.  
  59.  
  60. InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
  61.  
  62. DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
  63. dbf.setNamespaceAware(true);
  64. DocumentBuilder builder = dbf.newDocumentBuilder();
  65. Document doc = builder.parse(stream);
  66. XPathFactory xpf = XPathFactory.newInstance();
  67. XPath xpath = xpf.newXPath();
  68.  
  69. } catch (SAXException | ParserConfigurationException e) {
  70. throw new ServletException("Exception during testing!", e);
  71. }
  72. }
  73. }
Advertisement
Add Comment
Please, Sign In to add comment