001package org.hl7.fhir.utilities.xml;
002
003/*
004  Copyright (c) 2011+, HL7, Inc.
005  All rights reserved.
006  
007  Redistribution and use in source and binary forms, with or without modification, 
008  are permitted provided that the following conditions are met:
009    
010   * Redistributions of source code must retain the above copyright notice, this 
011     list of conditions and the following disclaimer.
012   * Redistributions in binary form must reproduce the above copyright notice, 
013     this list of conditions and the following disclaimer in the documentation 
014     and/or other materials provided with the distribution.
015   * Neither the name of HL7 nor the names of its contributors may be used to 
016     endorse or promote products derived from this software without specific 
017     prior written permission.
018  
019  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 
020  ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 
021  WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 
022  IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 
023  INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 
024  NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 
025  PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 
026  WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 
027  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 
028  POSSIBILITY OF SUCH DAMAGE.
029  
030 */
031
032
033
034import java.io.ByteArrayOutputStream;
035import java.io.File;
036import java.io.FileOutputStream;
037import java.io.IOException;
038import java.io.OutputStream;
039
040import org.hl7.fhir.exceptions.FHIRException;
041import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
042import org.w3c.dom.Comment;
043import org.w3c.dom.DOMException;
044import org.w3c.dom.Element;
045import org.w3c.dom.NamedNodeMap;
046import org.w3c.dom.Node;
047
048public class XmlGenerator {
049
050
051        private XMLWriter xml;
052        
053        public void generate(Element element, File file, String defaultNamespace, String elementName) throws FHIRException, IOException  {
054                
055                OutputStream stream = ManagedFileAccess.outStream(file);
056                
057                
058                xml = new XMLWriter(stream, "UTF-8");
059                xml.start();
060                xml.setDefaultNamespace(defaultNamespace);
061
062                xml.enter(defaultNamespace, elementName);
063                processContents(element);
064                xml.exit();
065                xml.end();
066                xml.flush();
067                stream.close();
068        }
069        
070  public String generate(Element element) throws IOException, FHIRException  {
071    ByteArrayOutputStream stream = new ByteArrayOutputStream();
072    generate(element, stream);
073    return new String(stream.toByteArray());
074  } 
075  
076        public void generate(Element element, File file) throws IOException, FHIRException  {
077                OutputStream stream = ManagedFileAccess.outStream(file);
078                try {
079                  generate(element, stream);
080    } finally {
081      stream.close();
082    }
083        }       
084        
085        public void generate(Element element, OutputStream stream) throws IOException, FHIRException  {
086                xml = new XMLWriter(stream, "UTF-8");
087                xml.start();
088                xml.setDefaultNamespace(element.getNamespaceURI());
089                processElement(element);
090                xml.end();
091        }
092        
093        private void processContents(Element element) throws FHIRException, IOException  {
094                Node node = element.getFirstChild();
095                while (node != null) {
096                        switch (node.getNodeType()) {
097                        case Node.ELEMENT_NODE:
098                                processElement((Element) node);
099                                break;
100                        case Node.TEXT_NODE:
101                                processText(node);
102                                break;
103                        case Node.COMMENT_NODE:
104                                processComment((Comment) node);
105                                break;
106                        default:
107                                throw new FHIRException("unhandled node type "+Integer.toString(node.getNodeType()));
108                        }
109                                
110                    node = node.getNextSibling();
111                }
112        }
113        
114        private void processComment(Comment node) throws DOMException, IOException  {
115                xml.comment(node.getNodeValue(), true);
116        }
117
118        private void processElement(Element element) throws IOException, FHIRException  {
119                if (xml.getDefaultNamespace() == null || !xml.getDefaultNamespace().equals(element.getNamespaceURI()))
120                        xml.setDefaultNamespace(element.getNamespaceURI());
121
122                processAttributes(element);
123                xml.enter(element.getNamespaceURI(), element.getLocalName());
124        
125                processContents(element);
126                
127                xml.exit();
128        }
129
130        private void processText(Node node) throws DOMException, IOException  {
131                xml.text(node.getNodeValue());
132        }
133
134        private void processAttributes(Element element) throws DOMException, IOException  {
135                NamedNodeMap nodes = element.getAttributes();
136                for (int i = 0; i < nodes.getLength(); i++) {
137                        Node attr = nodes.item(i);
138                        if (attr.getNamespaceURI() != null) {
139                                //xml.namespace(attr.getNamespaceURI());
140                        //xml.attribute(attr.getNamespaceURI(), attr.getLocalName(), attr.getNodeValue());
141                        } else if (attr.getLocalName() != null)
142//        xml.attribute("xmlns", attr.getNodeValue());
143//                      else
144                        xml.attribute(attr.getLocalName(), attr.getNodeValue());
145                        else if (attr.getNodeName() != null && !"xmlns".equals(attr.getNodeName()))
146        xml.attribute(attr.getNodeName(), attr.getNodeValue());
147                }
148                
149        }
150
151
152}