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.ByteArrayInputStream;
035import java.io.ByteArrayOutputStream;
036import java.io.FileInputStream;
037import java.io.IOException;
038import java.io.InputStream;
039import java.io.InputStreamReader;
040import java.io.OutputStream;
041import java.nio.charset.StandardCharsets;
042import java.util.ArrayList;
043import java.util.List;
044import java.util.Set;
045
046import javax.xml.XMLConstants;
047import javax.xml.parsers.*;
048import javax.xml.transform.Result;
049import javax.xml.transform.Source;
050import javax.xml.transform.Transformer;
051import javax.xml.transform.TransformerException;
052import javax.xml.transform.TransformerFactory;
053import javax.xml.transform.dom.DOMSource;
054import javax.xml.transform.stream.StreamResult;
055
056import org.hl7.fhir.exceptions.FHIRException;
057import org.hl7.fhir.utilities.Utilities;
058import org.hl7.fhir.utilities.filesystem.ManagedFileAccess;
059import org.w3c.dom.Attr;
060import org.w3c.dom.Document;
061import org.w3c.dom.Element;
062import org.w3c.dom.Node;
063import org.w3c.dom.NodeList;
064import org.w3c.dom.ls.DOMImplementationLS;
065import org.w3c.dom.ls.LSSerializer;
066import org.xml.sax.SAXException;
067import org.xml.sax.SAXNotRecognizedException;
068import org.xml.sax.SAXNotSupportedException;
069import org.xml.sax.XMLReader;
070
071public class XMLUtil {
072
073        public static final String SPACE_CHAR = "\u00A0";
074  public static final String SAX_FEATURES_EXTERNAL_GENERAL_ENTITIES = "http://xml.org/sax/features/external-general-entities";
075  public static final String APACHE_XML_FEATURES_DISALLOW_DOCTYPE_DECL = "http://apache.org/xml/features/disallow-doctype-decl";
076
077  public static boolean isNMToken(String name) {
078                if (name == null)
079                        return false;
080                for (int i = 0; i < name.length(); i++) 
081                        if (!isNMTokenChar(name.charAt(i)))
082                                return false;   
083                return name.length() > 0;
084        }
085
086        public static boolean isNMTokenChar(char c) {
087                return isLetter(c) || isDigit(c) || c == '.' || c == '-' || c == '_' || c == ':' || isCombiningChar(c) || isExtender(c);
088        }
089
090        private static boolean isDigit(char c) {
091                return (c >= '\u0030' && c <= '\u0039') || (c >= '\u0660' && c <= '\u0669') || (c >= '\u06F0' && c <= '\u06F9') || 
092                        (c >= '\u0966' && c <= '\u096F') || (c >= '\u09E6' && c <= '\u09EF') || (c >= '\u0A66' && c <= '\u0A6F') || 
093                        (c >= '\u0AE6' && c <= '\u0AEF') || (c >= '\u0B66' && c <= '\u0B6F') || (c >= '\u0BE7' && c <= '\u0BEF') || 
094                        (c >= '\u0C66' && c <= '\u0C6F') || (c >= '\u0CE6' && c <= '\u0CEF') || (c >= '\u0D66' && c <= '\u0D6F') || 
095                        (c >= '\u0E50' && c <= '\u0E59') || (c >= '\u0ED0' && c <= '\u0ED9') || (c >= '\u0F20' && c <= '\u0F29');
096        }
097
098        private static boolean isCombiningChar(char c) {
099                return (c >= '\u0300' && c <= '\u0345') || (c >= '\u0360' && c <= '\u0361') || (c >= '\u0483' && c <= '\u0486') || 
100                        (c >= '\u0591' && c <= '\u05A1') || (c >= '\u05A3' && c <= '\u05B9') || (c >= '\u05BB' && c <= '\u05BD') || 
101                        c == '\u05BF' || (c >= '\u05C1' && c <= '\u05C2') || c == '\u05C4' || (c >= '\u064B' && c <= '\u0652') || 
102                        c == '\u0670' || (c >= '\u06D6' && c <= '\u06DC') || (c >= '\u06DD' && c <= '\u06DF') || (c >= '\u06E0' && c <= '\u06E4') || 
103                        (c >= '\u06E7' && c <= '\u06E8') || (c >= '\u06EA' && c <= '\u06ED') || (c >= '\u0901' && c <= '\u0903') || c == '\u093C' || 
104                        (c >= '\u093E' && c <= '\u094C') || c == '\u094D' || (c >= '\u0951' && c <= '\u0954') || (c >= '\u0962' && c <= '\u0963') || 
105                        (c >= '\u0981' && c <= '\u0983') || c == '\u09BC' || c == '\u09BE' || c == '\u09BF' || (c >= '\u09C0' && c <= '\u09C4') || 
106                        (c >= '\u09C7' && c <= '\u09C8') || (c >= '\u09CB' && c <= '\u09CD') || c == '\u09D7' || (c >= '\u09E2' && c <= '\u09E3') || 
107                        c == '\u0A02' || c == '\u0A3C' || c == '\u0A3E' || c == '\u0A3F' || (c >= '\u0A40' && c <= '\u0A42') || 
108                        (c >= '\u0A47' && c <= '\u0A48') || (c >= '\u0A4B' && c <= '\u0A4D') || (c >= '\u0A70' && c <= '\u0A71') || 
109                        (c >= '\u0A81' && c <= '\u0A83') || c == '\u0ABC' || (c >= '\u0ABE' && c <= '\u0AC5') || (c >= '\u0AC7' && c <= '\u0AC9') || 
110                        (c >= '\u0ACB' && c <= '\u0ACD') || (c >= '\u0B01' && c <= '\u0B03') || c == '\u0B3C' || (c >= '\u0B3E' && c <= '\u0B43') || 
111                        (c >= '\u0B47' && c <= '\u0B48') || (c >= '\u0B4B' && c <= '\u0B4D') || (c >= '\u0B56' && c <= '\u0B57') || 
112                        (c >= '\u0B82' && c <= '\u0B83') || (c >= '\u0BBE' && c <= '\u0BC2') || (c >= '\u0BC6' && c <= '\u0BC8') || 
113                        (c >= '\u0BCA' && c <= '\u0BCD') || c == '\u0BD7' || (c >= '\u0C01' && c <= '\u0C03') || (c >= '\u0C3E' && c <= '\u0C44') || 
114                        (c >= '\u0C46' && c <= '\u0C48') || (c >= '\u0C4A' && c <= '\u0C4D') || (c >= '\u0C55' && c <= '\u0C56') || 
115                        (c >= '\u0C82' && c <= '\u0C83') || (c >= '\u0CBE' && c <= '\u0CC4') || (c >= '\u0CC6' && c <= '\u0CC8') || 
116                        (c >= '\u0CCA' && c <= '\u0CCD') || (c >= '\u0CD5' && c <= '\u0CD6') || (c >= '\u0D02' && c <= '\u0D03') || 
117                        (c >= '\u0D3E' && c <= '\u0D43') || (c >= '\u0D46' && c <= '\u0D48') || (c >= '\u0D4A' && c <= '\u0D4D') || c == '\u0D57' || 
118                        c == '\u0E31' || (c >= '\u0E34' && c <= '\u0E3A') || (c >= '\u0E47' && c <= '\u0E4E') || c == '\u0EB1' || 
119                        (c >= '\u0EB4' && c <= '\u0EB9') || (c >= '\u0EBB' && c <= '\u0EBC') || (c >= '\u0EC8' && c <= '\u0ECD') || 
120                        (c >= '\u0F18' && c <= '\u0F19') || c == '\u0F35' || c == '\u0F37' || c == '\u0F39' || c == '\u0F3E' || c == '\u0F3F' || 
121                        (c >= '\u0F71' && c <= '\u0F84') || (c >= '\u0F86' && c <= '\u0F8B') || (c >= '\u0F90' && c <= '\u0F95') || c == '\u0F97' || 
122                        (c >= '\u0F99' && c <= '\u0FAD') || (c >= '\u0FB1' && c <= '\u0FB7') || c == '\u0FB9' || (c >= '\u20D0' && c <= '\u20DC') ||
123                        c == '\u20E1' || (c >= '\u302A' && c <= '\u302F') || c == '\u3099' || c == '\u309A';
124        }
125
126        private static boolean isExtender(char c) {
127                return c == '\u00B7' || c == '\u02D0' || c == '\u02D1' || c == '\u0387' || c == '\u0640' || c == '\u0E46' || 
128                        c == '\u0EC6' || c == '\u3005' || (c >= '\u3031' && c <= '\u3035') || (c >= '\u309D' && c <= '\u309E') || 
129                        (c >= '\u30FC' && c <= '\u30FE');
130        }
131
132        private static boolean isLetter(char c) {
133                return isBaseChar(c) || isIdeographic(c);
134        }
135
136        private static boolean isBaseChar(char c) {
137                return (c >= '\u0041' && c <= '\u005A') || (c >= '\u0061' && c <= '\u007A') || (c >= '\u00C0' && c <= '\u00D6') || 
138                        (c >= '\u00D8' && c <= '\u00F6') || (c >= '\u00F8' && c <= '\u00FF') || (c >= '\u0100' && c <= '\u0131') || 
139                        (c >= '\u0134' && c <= '\u013E') || (c >= '\u0141' && c <= '\u0148') || (c >= '\u014A' && c <= '\u017E') || 
140                        (c >= '\u0180' && c <= '\u01C3') || (c >= '\u01CD' && c <= '\u01F0') || (c >= '\u01F4' && c <= '\u01F5') || 
141                        (c >= '\u01FA' && c <= '\u0217') || (c >= '\u0250' && c <= '\u02A8') || (c >= '\u02BB' && c <= '\u02C1') || 
142                        c == '\u0386' || (c >= '\u0388' && c <= '\u038A') || c == '\u038C' || (c >= '\u038E' && c <= '\u03A1') || 
143                        (c >= '\u03A3' && c <= '\u03CE') || (c >= '\u03D0' && c <= '\u03D6') || c == '\u03DA' || c == '\u03DC' || c == '\u03DE' || 
144                        c == '\u03E0' || (c >= '\u03E2' && c <= '\u03F3') || (c >= '\u0401' && c <= '\u040C') || (c >= '\u040E' && c <= '\u044F') || 
145                        (c >= '\u0451' && c <= '\u045C') || (c >= '\u045E' && c <= '\u0481') || (c >= '\u0490' && c <= '\u04C4') || 
146                        (c >= '\u04C7' && c <= '\u04C8') || (c >= '\u04CB' && c <= '\u04CC') || (c >= '\u04D0' && c <= '\u04EB') || 
147                        (c >= '\u04EE' && c <= '\u04F5') || (c >= '\u04F8' && c <= '\u04F9') || (c >= '\u0531' && c <= '\u0556') || 
148                        c == '\u0559' || (c >= '\u0561' && c <= '\u0586') || (c >= '\u05D0' && c <= '\u05EA') || (c >= '\u05F0' && c <= '\u05F2') || 
149                        (c >= '\u0621' && c <= '\u063A') || (c >= '\u0641' && c <= '\u064A') || (c >= '\u0671' && c <= '\u06B7') || 
150                        (c >= '\u06BA' && c <= '\u06BE') || (c >= '\u06C0' && c <= '\u06CE') || (c >= '\u06D0' && c <= '\u06D3') || 
151                        c == '\u06D5' || (c >= '\u06E5' && c <= '\u06E6') || (c >= '\u0905' && c <= '\u0939') || c == '\u093D' || 
152                        (c >= '\u0958' && c <= '\u0961') || (c >= '\u0985' && c <= '\u098C') || (c >= '\u098F' && c <= '\u0990') || 
153                        (c >= '\u0993' && c <= '\u09A8') || (c >= '\u09AA' && c <= '\u09B0') || c == '\u09B2' || 
154                        (c >= '\u09B6' && c <= '\u09B9') || (c >= '\u09DC' && c <= '\u09DD') || (c >= '\u09DF' && c <= '\u09E1') || 
155                        (c >= '\u09F0' && c <= '\u09F1') || (c >= '\u0A05' && c <= '\u0A0A') || (c >= '\u0A0F' && c <= '\u0A10') || 
156                        (c >= '\u0A13' && c <= '\u0A28') || (c >= '\u0A2A' && c <= '\u0A30') || (c >= '\u0A32' && c <= '\u0A33') || 
157                        (c >= '\u0A35' && c <= '\u0A36') || (c >= '\u0A38' && c <= '\u0A39') || (c >= '\u0A59' && c <= '\u0A5C') || 
158                        c == '\u0A5E' || (c >= '\u0A72' && c <= '\u0A74') || (c >= '\u0A85' && c <= '\u0A8B') || c == '\u0A8D' || 
159                        (c >= '\u0A8F' && c <= '\u0A91') || (c >= '\u0A93' && c <= '\u0AA8') || (c >= '\u0AAA' && c <= '\u0AB0') || 
160                        (c >= '\u0AB2' && c <= '\u0AB3') || (c >= '\u0AB5' && c <= '\u0AB9') || c == '\u0ABD' || c == '\u0AE0' || 
161                        (c >= '\u0B05' && c <= '\u0B0C') || (c >= '\u0B0F' && c <= '\u0B10') || (c >= '\u0B13' && c <= '\u0B28') || 
162                        (c >= '\u0B2A' && c <= '\u0B30') || (c >= '\u0B32' && c <= '\u0B33') || (c >= '\u0B36' && c <= '\u0B39') || 
163                        c == '\u0B3D' || (c >= '\u0B5C' && c <= '\u0B5D') || (c >= '\u0B5F' && c <= '\u0B61') || 
164                        (c >= '\u0B85' && c <= '\u0B8A') || (c >= '\u0B8E' && c <= '\u0B90') || (c >= '\u0B92' && c <= '\u0B95') || 
165                        (c >= '\u0B99' && c <= '\u0B9A') || c == '\u0B9C' || (c >= '\u0B9E' && c <= '\u0B9F') || 
166                        (c >= '\u0BA3' && c <= '\u0BA4') || (c >= '\u0BA8' && c <= '\u0BAA') || (c >= '\u0BAE' && c <= '\u0BB5') || 
167                        (c >= '\u0BB7' && c <= '\u0BB9') || (c >= '\u0C05' && c <= '\u0C0C') || (c >= '\u0C0E' && c <= '\u0C10') || 
168                        (c >= '\u0C12' && c <= '\u0C28') || (c >= '\u0C2A' && c <= '\u0C33') || (c >= '\u0C35' && c <= '\u0C39') || 
169                        (c >= '\u0C60' && c <= '\u0C61') || (c >= '\u0C85' && c <= '\u0C8C') || (c >= '\u0C8E' && c <= '\u0C90') || 
170                        (c >= '\u0C92' && c <= '\u0CA8') || (c >= '\u0CAA' && c <= '\u0CB3') || (c >= '\u0CB5' && c <= '\u0CB9') || 
171                        c == '\u0CDE' || (c >= '\u0CE0' && c <= '\u0CE1') || (c >= '\u0D05' && c <= '\u0D0C') || 
172                        (c >= '\u0D0E' && c <= '\u0D10') || (c >= '\u0D12' && c <= '\u0D28') || (c >= '\u0D2A' && c <= '\u0D39') || 
173                        (c >= '\u0D60' && c <= '\u0D61') || (c >= '\u0E01' && c <= '\u0E2E') || c == '\u0E30' || 
174                        (c >= '\u0E32' && c <= '\u0E33') || (c >= '\u0E40' && c <= '\u0E45') || (c >= '\u0E81' && c <= '\u0E82') || 
175                        c == '\u0E84' || (c >= '\u0E87' && c <= '\u0E88') || c == '\u0E8A' || c == '\u0E8D' || (c >= '\u0E94' && c <= '\u0E97') || 
176                        (c >= '\u0E99' && c <= '\u0E9F') || (c >= '\u0EA1' && c <= '\u0EA3') || c == '\u0EA5' || c == '\u0EA7' || 
177                        (c >= '\u0EAA' && c <= '\u0EAB') || (c >= '\u0EAD' && c <= '\u0EAE') || c == '\u0EB0' || 
178                        (c >= '\u0EB2' && c <= '\u0EB3') || c == '\u0EBD' || (c >= '\u0EC0' && c <= '\u0EC4') || 
179                        (c >= '\u0F40' && c <= '\u0F47') || (c >= '\u0F49' && c <= '\u0F69') || (c >= '\u10A0' && c <= '\u10C5') || 
180                        (c >= '\u10D0' && c <= '\u10F6') || c == '\u1100' || (c >= '\u1102' && c <= '\u1103') || 
181                        (c >= '\u1105' && c <= '\u1107') || c == '\u1109' || (c >= '\u110B' && c <= '\u110C') || 
182                        (c >= '\u110E' && c <= '\u1112') || c == '\u113C' || c == '\u113E' || c == '\u1140' || c == '\u114C' || 
183                        c == '\u114E' || c == '\u1150' || (c >= '\u1154' && c <= '\u1155') || c == '\u1159' || 
184                        (c >= '\u115F' && c <= '\u1161') || c == '\u1163' || c == '\u1165' || c == '\u1167' || c == '\u1169' || 
185                        (c >= '\u116D' && c <= '\u116E') || (c >= '\u1172' && c <= '\u1173') || c == '\u1175' || 
186                        c == '\u119E' || c == '\u11A8' || c == '\u11AB' || (c >= '\u11AE' && c <= '\u11AF') || 
187                        (c >= '\u11B7' && c <= '\u11B8') || c == '\u11BA' || (c >= '\u11BC' && c <= '\u11C2') || 
188                        c == '\u11EB' || c == '\u11F0' || c == '\u11F9' || (c >= '\u1E00' && c <= '\u1E9B') || (c >= '\u1EA0' && c <= '\u1EF9') || 
189                        (c >= '\u1F00' && c <= '\u1F15') || (c >= '\u1F18' && c <= '\u1F1D') || (c >= '\u1F20' && c <= '\u1F45') || 
190                        (c >= '\u1F48' && c <= '\u1F4D') || (c >= '\u1F50' && c <= '\u1F57') || c == '\u1F59' || c == '\u1F5B' || c == '\u1F5D' || 
191                        (c >= '\u1F5F' && c <= '\u1F7D') || (c >= '\u1F80' && c <= '\u1FB4') || (c >= '\u1FB6' && c <= '\u1FBC') || 
192                        c == '\u1FBE' || (c >= '\u1FC2' && c <= '\u1FC4') || (c >= '\u1FC6' && c <= '\u1FCC') || 
193                        (c >= '\u1FD0' && c <= '\u1FD3') || (c >= '\u1FD6' && c <= '\u1FDB') || (c >= '\u1FE0' && c <= '\u1FEC') || 
194                        (c >= '\u1FF2' && c <= '\u1FF4') || (c >= '\u1FF6' && c <= '\u1FFC') || c == '\u2126' || 
195                        (c >= '\u212A' && c <= '\u212B') || c == '\u212E' || (c >= '\u2180' && c <= '\u2182') || 
196                        (c >= '\u3041' && c <= '\u3094') || (c >= '\u30A1' && c <= '\u30FA') || (c >= '\u3105' && c <= '\u312C') || 
197                        (c >= '\uAC00' && c <= '\uD7A3');
198        }
199
200        private static boolean isIdeographic(char c) {
201                return (c >= '\u4E00' && c <= '\u9FA5') || c == '\u3007' || (c >= '\u3021' && c <= '\u3029');
202        }
203
204        public static String determineEncoding(InputStream stream) throws IOException {
205                stream.mark(20000);
206                try {
207                        int b0 = stream.read();
208                        int b1 = stream.read();
209                        int b2 = stream.read();
210                        int b3 = stream.read();
211
212                        if (b0 == 0xFE && b1 == 0xFF)
213                                return "UTF-16BE";
214                        else if (b0 == 0xFF && b1 == 0xFE)
215                                return "UTF-16LE";
216                        else if (b0 == 0xEF && b1 == 0xBB && b2 == 0xBF )
217                                return "UTF-8";
218                        else if (b0 == 0x00 && b1 == 0x3C && b2 == 0x00 && b3 == 0x3F)
219                                return "UTF-16BE";
220                        else if (b0 == 0x3C && b1 == 0x00 && b2 == 0x3F && b3 == 0x00)
221                                return "UTF-16LE";
222                        else if (b0 == 0x3C && b1 == 0x3F && b2 == 0x78 && b3 == 0x6D) {
223//                              UTF-8, ISO 646, ASCII, some part of ISO 8859, Shift-JIS, EUC, or any other 7-bit, 8-bit, or mixed-width encoding 
224//                              which ensures that the characters of ASCII have their normal positions, width, and values; the actual encoding 
225//                              declaration must be read to detect which of these applies, but since all of these encodings use the same bit patterns 
226//                              for the relevant ASCII characters, the encoding declaration itself may be read reliably
227                                InputStreamReader rdr = new InputStreamReader(stream, "US-ASCII");
228                                String hdr = readFirstLine(rdr);
229                                return extractEncoding(hdr); 
230                        } else
231                                return null;
232                } finally {
233                        stream.reset();
234                }
235        }
236
237        private static String extractEncoding(String hdr) {
238                int i = hdr.indexOf("encoding=");
239                if (i == -1)
240                        return null;
241                hdr = hdr.substring(i+9);
242                char sep = hdr.charAt(0);
243                hdr = hdr.substring(1);
244                i = hdr.indexOf(sep);
245                if (i == -1)
246                        return null;
247                return hdr.substring(0, i);
248        }
249
250        private static String readFirstLine(InputStreamReader rdr) throws IOException {
251                char[] buf = new char[1];
252                StringBuffer bldr = new StringBuffer();
253                rdr.read(buf);
254                while (buf[0] != '>') {
255                        bldr.append(buf[0]);
256                        rdr.read(buf);
257                }
258                return bldr.toString();
259        }
260
261        
262    public static boolean charSetImpliesAscii(String charset) {
263                return charset.equals("ISO-8859-1") || charset.equals("US-ASCII");
264        }
265
266        
267        /**
268         * Converts the raw characters to XML escape characters.
269         * 
270         * @param rawContent
271         * @param charset Null when charset is not known, so we assume it's unicode
272         * @param isNoLines
273         * @return escape string
274         */
275        public static String escapeXML(String rawContent, String charset, boolean isNoLines) {
276                if (rawContent == null)
277                        return "";
278                else {
279                        StringBuffer sb = new StringBuffer();
280
281                        for (int i = 0; i < rawContent.length(); i++) {
282                                char ch = rawContent.charAt(i);
283                                // We don't escape ' because our code always spits out attributes surrounded by "", which means
284                                // it's not necessary to escape ' - and it's *much* less ugly and more bandwidth-efficient when we don't.
285                                if (ch == '&')
286                                        sb.append("&amp;");
287                                else if (ch == '"')
288                                        sb.append("&quot;");
289                                else if (ch == '<')
290                                        sb.append("&lt;");
291                                else if (ch == '>')
292                                        sb.append("&gt;");
293                                else if (ch > '~' && charset != null && charSetImpliesAscii(charset)) 
294                                        // TODO - why is hashcode the only way to get the unicode number for the character
295                                        // in jre 5.0?
296                                        sb.append("&#x"+Integer.toHexString(ch).toUpperCase()+";");
297                                else if (isNoLines) {
298                                        if (ch == '\r')
299                                                sb.append("&#xA;");
300                                        else if (ch != '\n')
301                                                sb.append(ch);
302                                }
303                                else
304                                        sb.append(ch);
305                        }
306                        return sb.toString();
307                }
308        }
309
310  public static Element getFirstChild(Element e) {
311    if (e == null)
312      return null;
313    Node n = e.getFirstChild();
314    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
315      n = n.getNextSibling();
316    return (Element) n;
317  }
318
319  public static Element getNamedChild(Element e, String name) {
320    Element c = getFirstChild(e);
321    while (c != null && !name.equals(c.getLocalName()) && !name.equals(getLocalName(c.getNodeName())) && !name.equals(c.getNodeName()))
322      c = getNextSibling(c);
323    return c;
324  }
325
326  private static String getLocalName(String name) {
327    return name.contains(":") ? name.substring(name.indexOf(":")+1) : name;
328  }
329
330  public static Element getNamedChildByAttribute(Element e, String name, String nname, String nvalue) {
331    Element c = getFirstChild(e);
332    while (c != null && !((name.equals(c.getLocalName()) || name.equals(c.getNodeName())) && nvalue.equals(c.getAttribute(nname))))
333      c = getNextSibling(c);
334    return c;
335  }
336
337  public static Element getNextSibling(Element e) {
338    Node n = e.getNextSibling();
339    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
340      n = n.getNextSibling();
341    return (Element) n;
342  }
343
344  public static void getNamedChildren(Element e, String name, List<Element> set) {
345    Element c = getFirstChild(e);
346    while (c != null) {
347      if (name.equals(c.getLocalName()) || name.equals(c.getNodeName()) )
348        set.add(c);
349      c = getNextSibling(c);
350    }
351  }
352
353  public static List<Element> getNamedChildren(Element e, String name) {
354    List<Element> res = new ArrayList<Element>();
355    Element c = getFirstChild(e);
356    while (c != null) {
357      if (name.equals(c.getLocalName()) || name.equals(c.getNodeName()) )
358        res.add(c);
359      c = getNextSibling(c);
360    }
361    return res;
362  }
363
364  public static String htmlToXmlEscapedPlainText(Element r) {
365    StringBuilder s = new StringBuilder();
366    Node n = r.getFirstChild();
367    boolean ws = false;
368    while (n != null) {
369      if (n.getNodeType() == Node.TEXT_NODE) {
370        String t = n.getTextContent().trim();
371        if (Utilities.noString(t))
372          ws = true;
373        else {
374          if (ws)
375            s.append(" ");
376          ws = false;
377          s.append(t);
378        }
379      }
380      if (n.getNodeType() == Node.ELEMENT_NODE) {
381        if (ws)
382          s.append(" ");
383        ws = false;
384        s.append(htmlToXmlEscapedPlainText((Element) n));
385        if (r.getNodeName().equals("br") || r.getNodeName().equals("p"))
386          s.append("\r\n");
387      }
388      n = n.getNextSibling();      
389    }
390    return s.toString();
391  }
392
393  public static String htmlToXmlEscapedPlainText(String definition) throws ParserConfigurationException, SAXException, IOException  {
394    return htmlToXmlEscapedPlainText(parseToDom("<div>"+definition+"</div>").getDocumentElement());
395  }
396
397  public static String elementToString(Element el) {
398    if (el == null)
399      return "";
400    Document document = el.getOwnerDocument();
401    DOMImplementationLS domImplLS = (DOMImplementationLS) document
402        .getImplementation();
403    LSSerializer serializer = domImplLS.createLSSerializer();
404    return serializer.writeToString(el);
405  }
406
407  public static String getNamedChildValue(Element element, String name) {
408    Element e = getNamedChild(element, name);
409    return e == null ? null : e.getAttribute("value");
410  }
411
412  public static void setNamedChildValue(Element element, String name, String value) throws FHIRException  {
413    Element e = getNamedChild(element, name);
414    if (e == null)
415      throw new FHIRException("unable to find element "+name);
416    e.setAttribute("value", value);
417  }
418
419
420        public static void getNamedChildrenWithWildcard(Element focus, String name, List<Element> children) {
421    Element c = getFirstChild(focus);
422    while (c != null) {
423        String n = c.getLocalName() != null ? c.getLocalName() : c.getNodeName(); 
424      if (name.equals(n) || (name.endsWith("[x]") && n.startsWith(name.substring(0, name.length()-3))))
425        children.add(c);
426      c = getNextSibling(c);
427    }
428  }
429
430        public static void getNamedChildrenWithTails(Element focus, String name, List<Element> children, Set<String> typeTails) {
431    Element c = getFirstChild(focus);
432    while (c != null) {
433      String n = c.getLocalName() != null ? c.getLocalName() : c.getNodeName(); 
434      if (n.equals(name) || (!n.equals("responseCode") && (n.startsWith(name) && typeTails.contains(n.substring(name.length())))))
435        children.add(c);
436      c = getNextSibling(c);
437    }
438  }
439        
440  public static boolean hasNamedChild(Element e, String name) {
441    Element c = getFirstChild(e);
442    while (c != null && !name.equals(c.getLocalName()) && !name.equals(c.getNodeName()))
443      c = getNextSibling(c);
444    return c != null;
445  }
446
447  public static Document parseToDom(String content) throws ParserConfigurationException, SAXException, IOException  {
448    return parseToDom(content.getBytes(StandardCharsets.UTF_8));
449  }
450
451  public static Document parseToDom(byte[] content) throws ParserConfigurationException, SAXException, IOException  {
452    DocumentBuilderFactory factory = XMLUtil.newXXEProtectedDocumentBuilderFactory();
453    factory.setNamespaceAware(false);
454    DocumentBuilder builder = factory.newDocumentBuilder();
455    return builder.parse(new ByteArrayInputStream(content));
456  }
457
458  public static Document parseToDom(String content, boolean ns) throws ParserConfigurationException, SAXException, IOException  {
459    return parseToDom(content.getBytes(), ns);
460  }
461
462  public static Document parseToDom(byte[] content, boolean ns) throws ParserConfigurationException, SAXException, IOException  {
463    DocumentBuilderFactory factory = XMLUtil.newXXEProtectedDocumentBuilderFactory();
464    factory.setNamespaceAware(ns);
465    DocumentBuilder builder = factory.newDocumentBuilder();
466    return builder.parse(new ByteArrayInputStream(content));
467  }
468
469  public static Document parseFileToDom(String filename) throws ParserConfigurationException, SAXException, IOException  {
470    DocumentBuilderFactory factory = XMLUtil.newXXEProtectedDocumentBuilderFactory();
471    factory.setNamespaceAware(false);
472    DocumentBuilder builder = factory.newDocumentBuilder();
473    FileInputStream fs = ManagedFileAccess.inStream(filename);
474    try {
475      return builder.parse(fs);
476    } finally {
477      fs.close();
478    }  
479  }
480
481  public static Document parseFileToDom(String filename, boolean ns) throws ParserConfigurationException, SAXException, IOException  {
482    DocumentBuilderFactory factory = XMLUtil.newXXEProtectedDocumentBuilderFactory();
483    factory.setNamespaceAware(ns);
484    DocumentBuilder builder = factory.newDocumentBuilder();
485    FileInputStream fs = ManagedFileAccess.inStream(filename);
486    try {
487      return builder.parse(fs);
488    } finally {
489      fs.close();
490    }
491  }
492
493  public static Element getLastChild(Element e) {
494    if (e == null)
495      return null;
496    Node n = e.getLastChild();
497    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
498      n = n.getPreviousSibling();
499    return (Element) n;
500  }
501
502  public static Element getPrevSibling(Element e) {
503    Node n = e.getPreviousSibling();
504    while (n != null && n.getNodeType() != Node.ELEMENT_NODE)
505      n = n.getPreviousSibling();
506    return (Element) n;
507  }
508
509  public static String getNamedChildAttribute(Element element, String name, String aname) {
510    Element e = getNamedChild(element, name);
511    return e == null ? null : e.getAttribute(aname);
512  }
513
514  /**
515   * This method is used to create a new TransformerFactory instance with external processing features configured
516   * securely.
517   * <p/>
518   * <b>IMPORTANT</b> This method should be the only place where TransformerFactory is instantiated in this project.
519   *
520   * @return A TransformerFactory instance external processing features configured securely.
521   */
522  @SuppressWarnings("checkstyle:transformerFactoryNewInstance")
523  public static TransformerFactory newXXEProtectedTransformerFactory() {
524    final TransformerFactory transformerFactory = TransformerFactory.newInstance();
525    transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
526    transformerFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_STYLESHEET, "");
527    return transformerFactory;
528  }
529
530  /**
531   * This method is used to create a new DocumentBuilderFactory instance with external processing features configured
532   * securely.
533   * <p/>
534   * <b>IMPORTANT</b> This method should be the only place where DocumentBuilderFactory is instantiated in this project.
535   *
536   * @return A DocumentBuilderFactory instance external processing features configured securely.
537   * @throws ParserConfigurationException If a DocumentBuilder cannot be configured with the requested features.
538   */
539  @SuppressWarnings("checkstyle:documentBuilderFactoryNewInstance")
540  public static DocumentBuilderFactory newXXEProtectedDocumentBuilderFactory() throws ParserConfigurationException {
541    final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
542    documentBuilderFactory.setFeature(APACHE_XML_FEATURES_DISALLOW_DOCTYPE_DECL, true);
543    documentBuilderFactory.setXIncludeAware(false);
544    return documentBuilderFactory;
545  }
546
547  /**
548   * This method is used to create a new SAXParserFactory instance with external processing features configured
549   * securely.
550   * <p/>
551   * <b>IMPORTANT</b> This method should be the only place where SAXParserFactory is instantiated in this project.
552   *
553   * @return A SAXParserFactory instance external processing features configured securely.
554   * @throws SAXNotSupportedException When the underlying XMLReader recognizes the property name but doesn't support
555   * the property.
556   * @throws SAXNotRecognizedException When the underlying XMLReader does not recognize the property name.
557   * @throws ParserConfigurationException If a SAXParser cannot be configured with the requested features.
558   */
559  @SuppressWarnings("checkstyle:saxParserFactoryNewInstance")
560  public static SAXParserFactory newXXEProtectedSaxParserFactory() throws SAXNotSupportedException, SAXNotRecognizedException, ParserConfigurationException {
561    final SAXParserFactory spf = SAXParserFactory.newInstance();
562    spf.setFeature(SAX_FEATURES_EXTERNAL_GENERAL_ENTITIES, false);
563    spf.setFeature(APACHE_XML_FEATURES_DISALLOW_DOCTYPE_DECL, true);
564
565    return spf;
566  }
567
568  /**
569   * This method is used to create a new XMLReader instance from a passed SAXParserFactory with external processing
570   * features configured securely.
571   * <p/>
572   * <b>IMPORTANT</b> This method should be the only place where getXMLReader() is called in this project.
573   *
574   * @param spf The SAXParserFactory to create the XMLReader from.
575   * @return A XMLReader instance external processing features configured securely.
576   * @throws ParserConfigurationException If a SAXParser cannot be configured with the requested features.
577   * @throws SAXException If any SAX exceptions occur during the creation of the XMLReader.
578   */
579  @SuppressWarnings("checkstyle:getXMLReader")
580  public static XMLReader getXXEProtectedXMLReader(SAXParserFactory spf) throws ParserConfigurationException, SAXException {
581    final SAXParser saxParser = spf.newSAXParser();
582    final XMLReader xmlReader = saxParser.getXMLReader();
583
584    final boolean externalGeneralEntitiesFeatureValue = spf.getFeature(SAX_FEATURES_EXTERNAL_GENERAL_ENTITIES);
585    if (externalGeneralEntitiesFeatureValue) {
586      throw new IllegalArgumentException("SAXParserFactory has insecure feature setting:" + SAX_FEATURES_EXTERNAL_GENERAL_ENTITIES+ "=" + externalGeneralEntitiesFeatureValue);
587    }
588    final boolean disallowDocTypeDeclFeatureValue = spf.getFeature(APACHE_XML_FEATURES_DISALLOW_DOCTYPE_DECL);
589    if (!disallowDocTypeDeclFeatureValue) {
590      throw new IllegalArgumentException("SAXParserFactory has insecure feature setting:" + APACHE_XML_FEATURES_DISALLOW_DOCTYPE_DECL + "=" + disallowDocTypeDeclFeatureValue);
591    }
592    xmlReader.setFeature(SAX_FEATURES_EXTERNAL_GENERAL_ENTITIES, false);
593    xmlReader.setFeature(APACHE_XML_FEATURES_DISALLOW_DOCTYPE_DECL, true);
594    return xmlReader;
595  }
596  public static void writeDomToFile(Document doc, String filename) throws TransformerException, IOException {
597    TransformerFactory transformerFactory = XMLUtil.newXXEProtectedTransformerFactory();
598    Transformer transformer = transformerFactory.newTransformer();
599    DOMSource source = new DOMSource(doc);
600    StreamResult streamResult =  new StreamResult(ManagedFileAccess.file(filename));
601    transformer.transform(source, streamResult);    
602  }
603
604  public static String getXsiType(org.w3c.dom.Element element) {
605    Attr a = element.getAttributeNodeNS("http://www.w3.org/2001/XMLSchema-instance", "type");
606    return (a == null ? null : a.getTextContent());
607    
608  }
609
610        public static String getDirectText(org.w3c.dom.Element node) {
611    Node n = node.getFirstChild();
612    StringBuilder b = new StringBuilder();
613    while (n != null) {
614        if (n.getNodeType() == Node.TEXT_NODE) 
615                b.append(n.getTextContent());
616        n = n.getNextSibling();
617    }
618          return b.toString().trim();
619        }
620
621  public static void deleteByName(Element e, String name) {
622    List<Element> matches = getNamedChildren(e, name);
623    for (Element m : matches)
624      e.removeChild(m);    
625  }
626
627  public static void deleteAttr(Element e, String namespaceURI, String localName) {
628    if (e.hasAttributeNS(namespaceURI, localName))
629      e.removeAttributeNS(namespaceURI, localName);
630    
631  }
632
633  public static Node[] children(Element ed) {
634    Node[] res = new Node[ed.getChildNodes().getLength()];
635    for (int i = 0; i < ed.getChildNodes().getLength(); i++)
636      res[i] = ed.getChildNodes().item(i);
637    return res;
638  }
639
640  public static Element addChild(Document doc, Element element, String name, String namespace, int indent) {
641    Node node = doc.createTextNode("\n"+Utilities.padLeft("", ' ', indent));
642    Element child = doc.createElementNS(namespace, name);
643    element.appendChild(child);
644    element.appendChild(node);
645    return child;
646  }
647
648  public static Element insertChild(Document doc, Element element, String name, String namespace, int indent) {
649    Node node = doc.createTextNode("\n"+Utilities.padLeft("", ' ', indent));
650    Element child = doc.createElementNS(namespace, name);
651    element.insertBefore(child, element.getFirstChild());
652    element.insertBefore(node, element.getFirstChild());
653    return child;
654  }
655
656  public static Element insertChild(Document doc, Element element, String name, String namespace, Node before, int indent) {
657    if (before == null) {
658      Node node = doc.createTextNode("\n"+Utilities.padLeft("", ' ', indent));
659      element.insertBefore(node, before);
660    }
661    Element child = doc.createElementNS(namespace, name);
662    element.insertBefore(child, before);
663    if (before != null) {
664      Node node = doc.createTextNode("\n"+Utilities.padLeft("", ' ', indent));
665      element.insertBefore(node, before);
666    }
667    return child;
668  }
669
670  public static void addTextTag(Document doc, Element element, String name, String namespace, String text, int indent) {
671    Node node = doc.createTextNode("\n"+Utilities.padLeft("", ' ', indent));
672    element.appendChild(node);
673    Element child = doc.createElementNS(namespace, name);
674    element.appendChild(child);
675    child.setAttribute("value", text);    
676  }
677
678  public static Element addTextTag(Document doc, Element element, String name, String text, int indent) {
679    Node node = doc.createTextNode("\n"+Utilities.padLeft("", ' ', indent));
680    element.appendChild(node);
681    Element child = doc.createElement(name);
682    element.appendChild(child);
683    child.appendChild(doc.createTextNode(text));
684    return child;
685  }
686
687  public static void saveToFile(Element root, OutputStream stream) throws TransformerException {
688    Transformer transformer = XMLUtil.newXXEProtectedTransformerFactory().newTransformer();
689    Result output = new StreamResult(stream);
690    Source input = new DOMSource(root);
691
692    transformer.transform(input, output);
693  }
694
695  public static void spacer(Document doc, Element element, int indent) {
696    Node node = doc.createTextNode("\n"+Utilities.padLeft("", ' ', indent));
697    element.appendChild(node);
698   
699  }
700
701  public static String getNamedChildText(Element element, String name) {
702    Element e = getNamedChild(element, name);
703    return e == null ? null : e.getTextContent();
704  }
705
706  public static Element getFirstChild(Element res, String... names) {
707    Element node = getFirstChild(res);
708    while (node != null && !Utilities.existsInList(node.getLocalName(), names)) {
709      node = getNextSibling(node);
710    }
711    return node;
712  }
713
714  public static Element getLastChild(Element res, String... names) {
715    Element result = null;
716    Element node = getFirstChild(res);
717    while (node != null) {
718      if (Utilities.existsInList(node.getLocalName(), names)) {
719        result = node;
720      }
721      node = getNextSibling(node);
722    }
723    return result;
724  }
725
726  public static void clearChildren(Node node) {
727    NodeList nodeList = node.getChildNodes();
728    for (int i = nodeList.getLength() - 1; i >= 0; i--) {
729      Node item = nodeList.item(i);
730      if (item.hasChildNodes()) {
731        clearChildren(item);
732      }
733      node.removeChild(item);
734    };
735  }
736
737}