View Javadoc

1   /*
2    * Copyright 2008 The Kuali Foundation
3    *
4    * Licensed under the Educational Community License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.opensource.org/licenses/ecl2.php
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  package org.kuali.ole.module.purap.util.cxml;
17  
18  import org.apache.log4j.Logger;
19  import org.apache.xml.serialize.OutputFormat;
20  import org.apache.xml.serialize.XMLSerializer;
21  import org.kuali.ole.sys.batch.service.BatchInputFileService;
22  import org.kuali.ole.sys.context.SpringContext;
23  import org.w3c.dom.Document;
24  import org.w3c.dom.Element;
25  import org.w3c.dom.Node;
26  
27  import javax.xml.parsers.DocumentBuilder;
28  import javax.xml.parsers.DocumentBuilderFactory;
29  import javax.xml.parsers.ParserConfigurationException;
30  import java.io.ByteArrayInputStream;
31  import java.io.ByteArrayOutputStream;
32  import java.io.IOException;
33  
34  public class B2BParserHelper {
35  
36      private static Logger log = Logger.getLogger(B2BParserHelper.class);
37  
38      private DocumentBuilder builder;
39      private static B2BParserHelper _this;
40  
41      private B2BParserHelper() {
42  
43          DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
44          builderFactory.setValidating(false); // It's not needed to validate here
45          builderFactory.setIgnoringElementContentWhitespace(true);
46  
47          try {
48              // This is a funky one. Without setting this "load-external-dtd" feature, even though we're
49              // explicitly setting non-validating, the parser will still reach out and retrieve that DTD. If
50              // the xml.cxml.org site happens to be down, it'll hang or fail on that dependency.
51              //
52              // http://xerces.apache.org/xerces2-j/features.html#nonvalidating.load-external-dtd
53              builderFactory.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);
54  
55              builder = builderFactory.newDocumentBuilder(); // Create the parser
56          } catch (ParserConfigurationException e) {
57              throw new RuntimeException(e);
58          }
59  
60      }
61  
62      public static B2BParserHelper getInstance() {
63          if (_this == null) {
64              _this = new B2BParserHelper();
65          }
66          return _this;
67      }
68  
69      public synchronized B2BShoppingCart parseShoppingCartXML(String xmlChunk) {
70  
71          Document xmlDoc = null;
72          try {
73              xmlDoc = builder.parse(new ByteArrayInputStream(xmlChunk.getBytes()));
74          } catch (Exception e) {
75              e.printStackTrace();
76              throw new RuntimeException(e);
77          }
78  
79          byte[] xmlDocAsBytes = addXMLNameSpace(xmlDoc, "http://www.kuali.org/ole/purap/b2bPunchOutOrder");
80  
81          B2BPunchOutOrderFileType fileType = SpringContext.getBean(B2BPunchOutOrderFileType.class);
82  
83          B2BShoppingCart cart = (B2BShoppingCart) SpringContext.getBean(BatchInputFileService.class).parse(fileType, xmlDocAsBytes);
84  
85          return cart;
86  
87      }
88  
89      public synchronized PunchOutSetupResponse parsePunchOutSetupResponse(String xmlChunk) {
90  
91          Document xmlDoc = null;
92          try {
93              xmlDoc = builder.parse(new ByteArrayInputStream(xmlChunk.getBytes()));
94          } catch (Exception e) {
95              e.printStackTrace();
96              throw new RuntimeException(e);
97          }
98  
99          byte[] xmlDocAsBytes = addXMLNameSpace(xmlDoc, "http://www.kuali.org/ole/purap/b2bPunchOutResponse");
100 
101         PunchOutSetupResponseFileType fileType = SpringContext.getBean(PunchOutSetupResponseFileType.class);
102 
103         PunchOutSetupResponse response = (PunchOutSetupResponse) SpringContext.getBean(BatchInputFileService.class).parse(fileType, xmlDocAsBytes);
104 
105         return response;
106 
107     }
108 
109     public synchronized PurchaseOrderResponse parsePurchaseOrderResponse(String xmlChunk) {
110 
111         Document xmlDoc = null;
112         try {
113             xmlDoc = builder.parse(new ByteArrayInputStream(xmlChunk.getBytes()));
114         } catch (Exception e) {
115             e.printStackTrace();
116             throw new RuntimeException(e);
117         }
118 
119         byte[] xmlDocAsBytes = addXMLNameSpace(xmlDoc, "http://www.kuali.org/ole/purap/b2bPOResponse");
120 
121         B2BPOResponseFileType fileType = SpringContext.getBean(B2BPOResponseFileType.class);
122 
123         PurchaseOrderResponse response = (PurchaseOrderResponse) SpringContext.getBean(BatchInputFileService.class).parse(fileType, xmlDocAsBytes);
124 
125         return response;
126 
127     }
128 
129     private byte[] addXMLNameSpace(Document xmlDoc,
130                                    String nameSpace) {
131 
132         Node node = xmlDoc.getDocumentElement();
133         Element element = (Element) node;
134 
135         element.setAttribute("xmlns", nameSpace);
136 
137         OutputFormat outputFormat = new OutputFormat(xmlDoc);
138         outputFormat.setOmitDocumentType(true);
139 
140         ByteArrayOutputStream out = new ByteArrayOutputStream();
141         XMLSerializer serializer = new XMLSerializer(out, outputFormat);
142         try {
143             serializer.asDOMSerializer();
144             serializer.serialize(xmlDoc.getDocumentElement());
145         } catch (IOException e) {
146             throw new RuntimeException(e);
147         }
148 
149         return out.toByteArray();
150     }
151 }