001    /*
002     * Copyright 2011 The Kuali Foundation.
003     * 
004     * Licensed under the Educational Community License, Version 2.0 (the "License");
005     * you may not use this file except in compliance with the License.
006     * You may obtain a copy of the License at
007     * 
008     * http://www.opensource.org/licenses/ecl2.php
009     * 
010     * Unless required by applicable law or agreed to in writing, software
011     * distributed under the License is distributed on an "AS IS" BASIS,
012     * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013     * See the License for the specific language governing permissions and
014     * limitations under the License.
015     */
016    package org.kuali.kfs.module.purap.util.cxml;
017    
018    import java.io.ByteArrayInputStream;
019    import java.io.ByteArrayOutputStream;
020    import java.io.IOException;
021    
022    import javax.xml.parsers.DocumentBuilder;
023    import javax.xml.parsers.DocumentBuilderFactory;
024    import javax.xml.parsers.ParserConfigurationException;
025    
026    import org.apache.log4j.Logger;
027    import org.apache.xml.serialize.OutputFormat;
028    import org.apache.xml.serialize.XMLSerializer;
029    import org.kuali.kfs.sys.batch.service.BatchInputFileService;
030    import org.kuali.kfs.sys.context.SpringContext;
031    import org.w3c.dom.Document;
032    import org.w3c.dom.Element;
033    import org.w3c.dom.Node;
034    
035    public class B2BParserHelper {
036        
037        private static Logger log = Logger.getLogger(B2BParserHelper.class);
038        
039        private DocumentBuilder builder;
040        private static B2BParserHelper _this;
041        
042        private B2BParserHelper(){
043            
044            DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance();
045            builderFactory.setValidating(false); // It's not needed to validate here
046            builderFactory.setIgnoringElementContentWhitespace(true); 
047    
048            try {
049              builder = builderFactory.newDocumentBuilder();  // Create the parser
050            } catch(ParserConfigurationException e) {
051                throw new RuntimeException(e);
052            } 
053            
054        }
055    
056        public static B2BParserHelper getInstance(){
057            if (_this == null){
058                _this = new B2BParserHelper();
059            }
060            return _this;
061        }
062        
063        public synchronized B2BShoppingCart parseShoppingCartXML(String xmlChunk){
064            
065            Document xmlDoc = null;
066            try {
067                xmlDoc = builder.parse(new ByteArrayInputStream(xmlChunk.getBytes()));
068            } catch(Exception e) {
069                e.printStackTrace();
070                throw new RuntimeException(e);
071            }
072            
073            byte[] xmlDocAsBytes = addXMLNameSpace(xmlDoc,"http://www.kuali.org/kfs/purap/b2bPunchOutOrder");
074            
075            B2BPunchOutOrderFileType fileType = SpringContext.getBean(B2BPunchOutOrderFileType.class);
076            
077            B2BShoppingCart cart = (B2BShoppingCart) SpringContext.getBean(BatchInputFileService.class).parse(fileType,xmlDocAsBytes);
078            
079            return cart;
080            
081        }
082        
083        public synchronized PunchOutSetupResponse parsePunchOutSetupResponse(String xmlChunk){
084            
085            Document xmlDoc = null;
086            try {
087                xmlDoc = builder.parse(new ByteArrayInputStream(xmlChunk.getBytes()));
088            } catch(Exception e) {
089                e.printStackTrace();
090                throw new RuntimeException(e);
091            }
092            
093            byte[] xmlDocAsBytes = addXMLNameSpace(xmlDoc,"http://www.kuali.org/kfs/purap/b2bPunchOutResponse");
094            
095            PunchOutSetupResponseFileType fileType = SpringContext.getBean(PunchOutSetupResponseFileType.class);
096            
097            PunchOutSetupResponse response = (PunchOutSetupResponse) SpringContext.getBean(BatchInputFileService.class).parse(fileType,xmlDocAsBytes);
098            
099            return response;
100            
101        }
102        
103        public synchronized PurchaseOrderResponse parsePurchaseOrderResponse(String xmlChunk){
104            
105            Document xmlDoc = null;
106            try {
107                xmlDoc = builder.parse(new ByteArrayInputStream(xmlChunk.getBytes()));
108            } catch(Exception e) {
109                e.printStackTrace();
110                throw new RuntimeException(e);
111            }
112            
113            byte[] xmlDocAsBytes = addXMLNameSpace(xmlDoc,"http://www.kuali.org/kfs/purap/b2bPOResponse");
114            
115            B2BPOResponseFileType fileType = SpringContext.getBean(B2BPOResponseFileType.class);
116            
117            PurchaseOrderResponse response = (PurchaseOrderResponse) SpringContext.getBean(BatchInputFileService.class).parse(fileType,xmlDocAsBytes);
118            
119            return response;
120            
121        }
122        
123        private byte[] addXMLNameSpace(Document xmlDoc,
124                                       String nameSpace){
125            
126            Node node = xmlDoc.getDocumentElement();
127            Element element = (Element)node;
128            
129            element.setAttribute("xmlns", nameSpace);
130            
131            OutputFormat outputFormat = new OutputFormat(xmlDoc);
132            outputFormat.setOmitDocumentType(true);
133            
134            ByteArrayOutputStream out = new ByteArrayOutputStream();
135            XMLSerializer serializer = new XMLSerializer( out,outputFormat );
136            try {
137                serializer.asDOMSerializer();
138                serializer.serialize( xmlDoc.getDocumentElement());
139            }
140            catch (IOException e) {
141                throw new RuntimeException(e);
142            }
143            
144            return out.toByteArray();
145        }
146    }