1 4 5 package com.sun.j2ee.blueprints.stringposervice; 6 7 import java.rmi.*; 8 import java.io.*; 9 10 import javax.ejb.*; 11 import javax.xml.parsers.*; 12 13 import org.w3c.dom.*; 14 import org.xml.sax.*; 15 16 20 public class StringPurchaseOrderServiceBean implements SessionBean { 21 22 private SessionContext sc; 23 private DocumentBuilderFactory docBuilderFactory; 24 25 public StringPurchaseOrderServiceBean(){} 26 27 public String submitPO(String xmlPO) throws InvalidPOException, RemoteException { 28 String id =null; 29 if (!(validate(xmlPO))) { 30 throw new InvalidPOException("Error parsing the purchase order XML document!!!"); 31 } 32 try { 33 id = getID(xmlPO); 35 } catch (Exception e) { 36 throw new EJBException("StringPurchaseOrderService implementation had trouble parsing PO.xml, some system or configuration problem " + e.getMessage(), e); 37 } 38 if(id.equals("100")) 40 throw new InvalidPOException("Invalid ID for the purchase order!!! " + 41 "For demo purposes, we throw " + 42 "an application defined exception for the ID value of 100."); 43 return id; 44 } 45 46 51 private String getID(String xmlDoc) throws org.xml.sax.SAXException , javax.xml.parsers.ParserConfigurationException , java.io.IOException { 52 String ret = ""; 53 DocumentBuilder db = null; 54 if (docBuilderFactory != null) 55 db = docBuilderFactory.newDocumentBuilder(); 56 InputSource is = new InputSource(new StringReader(xmlDoc)); 57 Document doc = db.parse(is); 58 Element root = doc.getDocumentElement(); 59 NodeList list = root.getElementsByTagName("poId"); 60 for (int loop = 0; loop < list.getLength(); loop++) { 61 Node node = list.item(loop); 62 if (node != null) { 63 Node child = node.getFirstChild(); 64 if ((child != null) && child.getNodeValue() != null) return child.getNodeValue(); 65 } 66 } 67 return ret; 68 } 69 70 75 private boolean validate(String xmlDoc) { 76 Document doc = null; 77 try { 78 DocumentBuilderFactory dbf = null; 79 DocumentBuilder db = null; 80 try { 81 dbf = DocumentBuilderFactory.newInstance(); 82 dbf.setValidating(true); 83 dbf.setNamespaceAware(true); 84 dbf.setAttribute("http://java.sun.com/xml/jaxp/properties/schemaLanguage", 85 "http://www.w3.org/2001/XMLSchema"); 86 if (dbf != null){ 87 db = dbf.newDocumentBuilder(); 88 } 89 db.setEntityResolver(new POEntityResolver()); 90 db.setErrorHandler(new POXMLErrorHandler()); 91 } catch ( javax.xml.parsers.ParserConfigurationException pce) { 92 System.err.println(pce); 93 } 94 InputSource is = new InputSource(new StringReader(xmlDoc)); 95 doc = db.parse(is); 96 return true; 97 } catch (Exception e) { 98 System.err.println("XML Validation Error " + e); 99 } 100 return false; 101 } 102 103 public void ejbCreate() throws CreateException { 105 docBuilderFactory = DocumentBuilderFactory.newInstance(); 106 } 107 108 public void setSessionContext(SessionContext sc) { 109 this.sc = sc; 110 } 111 112 public void ejbRemove() { 113 docBuilderFactory = null; 114 } 115 116 public void ejbActivate() {} 117 118 public void ejbPassivate() {} 119 } 120 | Popular Tags |