KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > j2ee > blueprints > anytypeposervice > POXMLUtil


1 /* Copyright 2004 Sun Microsystems, Inc. All rights reserved. You may not modify, use, reproduce, or distribute this software except in compliance with the terms of the License at:
2  http://developer.sun.com/berkeley_license.html
3  $Id: POXMLUtil.java,v 1.4 2005/08/12 20:46:12 smitha Exp $ */

4
5 package com.sun.j2ee.blueprints.anytypeposervice;
6
7 import java.io.*;
8 import java.util.Date JavaDoc;
9
10 import javax.xml.soap.*;
11 import javax.xml.transform.*;
12 import javax.xml.parsers.*;
13 import javax.xml.transform.dom.*;
14
15 import org.w3c.dom.*;
16 import org.xml.sax.*;
17
18 public class POXMLUtil {
19     
20     private DocumentBuilderFactory factory;
21     
22     /** Creates a new instance of POXMLUtil */
23     public POXMLUtil() {
24         factory = DocumentBuilderFactory.newInstance();
25         factory.setValidating(false);
26         factory.setNamespaceAware(true);
27     }
28         
29     /**
30      * Method to create a SOAPElement
31      * @param poID the ID for the purchase order created
32      * @return SOAPElement
33      * @throws Exception
34      */

35     
36     public SOAPElement createSOAPMessage(String JavaDoc poID) throws Exception JavaDoc {
37         Document doc = createDOMFromString(poID);
38         if (doc == null)
39             throw new IOException("Problem reading cand creating DOM from file,readFileCreateDocument() returned null ");
40
41         SOAPElement parent = SOAPFactory.newInstance().createElement("dummy");
42         TransformerFactory factory = TransformerFactory.newInstance();
43         Transformer transformer = factory.newTransformer();
44         transformer.transform(new DOMSource(doc), new DOMResult(parent));
45         return (SOAPElement) parent.getChildElements().next();
46     }
47
48         
49     /**
50      * Creates a DOM from String
51      * @param poID the ID for the purchase order created
52      * @return Document
53      */

54     
55     private Document createDOMFromString(String JavaDoc poID) throws Exception JavaDoc {
56        
57         // Use the factory to create a DOM parser
58
DocumentBuilder parser = factory.newDocumentBuilder();
59         InputSource in = new InputSource();
60         String JavaDoc xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" +
61             "<tns:BusinessDocumentReply xmlns:tns=\"urn:AnyTypePurchaseOrderService\">\n" +
62             "<tns:Status xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\" xsi:schemaLocation=\"http://com.sun.j2ee.blueprints.anytypeposervice/types PurchaseOrderStatus.xsd\">\n" +
63             " <orderid> "+ poID + " </orderid>\n" +
64             " <timestamp>"+ new Date JavaDoc().toString()+"</timestamp>\n" +
65             "</tns:Status>\n" +
66             "</tns:BusinessDocumentReply>";
67         in.setCharacterStream(new StringReader(xml));
68         Document dom = parser.parse(in);
69         return dom;
70     }
71 }
72
Popular Tags