001package org.hl7.fhir.dstu2.formats; 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/* 034Copyright (c) 2011+, HL7, Inc 035All rights reserved. 036 037Redistribution and use in source and binary forms, with or without modification, 038are permitted provided that the following conditions are met: 039 040 * Redistributions of source code must retain the above copyright notice, this 041 list of conditions and the following disclaimer. 042 * Redistributions in binary form must reproduce the above copyright notice, 043 this list of conditions and the following disclaimer in the documentation 044 and/or other materials provided with the distribution. 045 * Neither the name of HL7 nor the names of its contributors may be used to 046 endorse or promote products derived from this software without specific 047 prior written permission. 048 049THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND 050ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 051WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 052IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 053INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 054NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 055PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 056WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 057ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 058POSSIBILITY OF SUCH DAMAGE. 059 060 */ 061 062import java.io.BufferedInputStream; 063import java.io.ByteArrayInputStream; 064import java.io.IOException; 065import java.io.InputStream; 066import java.io.OutputStream; 067import java.io.UnsupportedEncodingException; 068import java.util.ArrayList; 069import java.util.List; 070 071import org.hl7.fhir.dstu2.model.Base; 072import org.hl7.fhir.dstu2.model.DomainResource; 073import org.hl7.fhir.dstu2.model.Element; 074import org.hl7.fhir.dstu2.model.Resource; 075import org.hl7.fhir.dstu2.model.StringType; 076import org.hl7.fhir.dstu2.model.Type; 077import org.hl7.fhir.instance.model.api.IIdType; 078import org.hl7.fhir.exceptions.FHIRFormatError; 079import org.hl7.fhir.utilities.Utilities; 080import org.hl7.fhir.utilities.xhtml.NodeType; 081import org.hl7.fhir.utilities.xhtml.XhtmlComposer; 082import org.hl7.fhir.utilities.xhtml.XhtmlNode; 083import org.hl7.fhir.utilities.xhtml.XhtmlParser; 084import org.hl7.fhir.utilities.xml.IXMLWriter; 085import org.hl7.fhir.utilities.xml.XMLWriter; 086import org.xmlpull.v1.XmlPullParser; 087import org.xmlpull.v1.XmlPullParserException; 088import org.xmlpull.v1.XmlPullParserFactory; 089 090/** 091 * General parser for XML content. You instantiate an XmlParser of these, but you 092 * actually use parse or parseGeneral defined on this class 093 * 094 * The two classes are separated to keep generated and manually maintained code apart. 095 */ 096public abstract class XmlParserBase extends ParserBase implements IParser { 097 098 @Override 099 public ParserType getType() { 100 return ParserType.XML; 101 } 102 103 // -- in descendent generated code -------------------------------------- 104 105 abstract protected Resource parseResource(XmlPullParser xpp) throws XmlPullParserException, IOException, FHIRFormatError ; 106 abstract protected Type parseType(XmlPullParser xml, String type) throws XmlPullParserException, IOException, FHIRFormatError ; 107 abstract protected void composeType(String prefix, Type type) throws IOException ; 108 109 /* -- entry points --------------------------------------------------- */ 110 111 /** 112 * Parse content that is known to be a resource 113 * @ 114 */ 115 @Override 116 public Resource parse(InputStream input) throws IOException, FHIRFormatError { 117 try { 118 XmlPullParser xpp = loadXml(input); 119 return parse(xpp); 120 } catch (XmlPullParserException e) { 121 throw new FHIRFormatError(e.getMessage(), e); 122 } 123 } 124 125 /** 126 * parse xml that is known to be a resource, and that is already being read by an XML Pull Parser 127 * This is if a resource is in a bigger piece of XML. 128 * @ 129 */ 130 public Resource parse(XmlPullParser xpp) throws IOException, FHIRFormatError, XmlPullParserException { 131 if (xpp.getNamespace() == null) 132 throw new FHIRFormatError("This does not appear to be a FHIR resource (no namespace '"+xpp.getNamespace()+"') (@ /) "+Integer.toString(xpp.getEventType())); 133 if (!xpp.getNamespace().equals(FHIR_NS)) 134 throw new FHIRFormatError("This does not appear to be a FHIR resource (wrong namespace '"+xpp.getNamespace()+"') (@ /)"); 135 return parseResource(xpp); 136 } 137 138 @Override 139 public Type parseType(InputStream input, String knownType) throws IOException, FHIRFormatError { 140 try { 141 XmlPullParser xml = loadXml(input); 142 return parseType(xml, knownType); 143 } catch (XmlPullParserException e) { 144 throw new FHIRFormatError(e.getMessage(), e); 145 } 146 } 147 148 149 /** 150 * Compose a resource to a stream, possibly using pretty presentation for a human reader (used in the spec, for example, but not normally in production) 151 * @ 152 */ 153 @Override 154 public void compose(OutputStream stream, Resource resource) throws IOException { 155 XMLWriter writer = new XMLWriter(stream, "UTF-8"); 156 writer.setPretty(style == OutputStyle.PRETTY); 157 writer.start(); 158 compose(writer, resource, writer.isPretty()); 159 writer.end(); 160 } 161 162 /** 163 * Compose a resource to a stream, possibly using pretty presentation for a human reader, and maybe a different choice in the xhtml narrative (used in the spec in one place, but should not be used in production) 164 * @ 165 */ 166 public void compose(OutputStream stream, Resource resource, boolean htmlPretty) throws IOException { 167 XMLWriter writer = new XMLWriter(stream, "UTF-8"); 168 writer.setPretty(style == OutputStyle.PRETTY); 169 writer.start(); 170 compose(writer, resource, htmlPretty); 171 writer.end(); 172 } 173 174 175 /** 176 * Compose a type to a stream (used in the spec, for example, but not normally in production) 177 * @ 178 */ 179 public void compose(OutputStream stream, String rootName, Type type) throws IOException { 180 xml = new XMLWriter(stream, "UTF-8"); 181 xml.setPretty(style == OutputStyle.PRETTY); 182 xml.start(); 183 xml.setDefaultNamespace(FHIR_NS); 184 composeType(Utilities.noString(rootName) ? "value" : rootName, type); 185 xml.end(); 186 } 187 188 @Override 189 public void compose(OutputStream stream, Type type, String rootName) throws IOException { 190 xml = new XMLWriter(stream, "UTF-8"); 191 xml.setPretty(style == OutputStyle.PRETTY); 192 xml.start(); 193 xml.setDefaultNamespace(FHIR_NS); 194 composeType(Utilities.noString(rootName) ? "value" : rootName, type); 195 xml.end(); 196 } 197 198 199 200 /* -- xml routines --------------------------------------------------- */ 201 202 protected XmlPullParser loadXml(String source) throws UnsupportedEncodingException, XmlPullParserException, IOException { 203 return loadXml(new ByteArrayInputStream(source.getBytes("UTF-8"))); 204 } 205 206 protected XmlPullParser loadXml(InputStream stream) throws XmlPullParserException, IOException { 207 BufferedInputStream input = new BufferedInputStream(stream); 208 XmlPullParserFactory factory = XmlPullParserFactory.newInstance(System.getProperty(XmlPullParserFactory.PROPERTY_NAME), null); 209 factory.setNamespaceAware(true); 210 XmlPullParser xpp = factory.newPullParser(); 211 xpp.setInput(input, "UTF-8"); 212 next(xpp); 213 nextNoWhitespace(xpp); 214 215 return xpp; 216 } 217 218 protected int next(XmlPullParser xpp) throws XmlPullParserException, IOException { 219 if (handleComments) 220 return xpp.nextToken(); 221 else 222 return xpp.next(); 223 } 224 225 protected List<String> comments = new ArrayList<String>(); 226 227 protected int nextNoWhitespace(XmlPullParser xpp) throws XmlPullParserException, IOException { 228 int eventType = xpp.getEventType(); 229 while ((eventType == XmlPullParser.TEXT && xpp.isWhitespace()) || (eventType == XmlPullParser.COMMENT) 230 || (eventType == XmlPullParser.CDSECT) || (eventType == XmlPullParser.IGNORABLE_WHITESPACE) 231 || (eventType == XmlPullParser.PROCESSING_INSTRUCTION) || (eventType == XmlPullParser.DOCDECL)) { 232 if (eventType == XmlPullParser.COMMENT) { 233 comments.add(xpp.getText()); 234 } 235 eventType = next(xpp); 236 } 237 return eventType; 238 } 239 240 241 protected void skipElementWithContent(XmlPullParser xpp) throws XmlPullParserException, IOException { 242 // when this is called, we are pointing an element that may have content 243 while (xpp.getEventType() != XmlPullParser.END_TAG) { 244 next(xpp); 245 if (xpp.getEventType() == XmlPullParser.START_TAG) 246 skipElementWithContent(xpp); 247 } 248 next(xpp); 249 } 250 251 protected void skipEmptyElement(XmlPullParser xpp) throws XmlPullParserException, IOException { 252 while (xpp.getEventType() != XmlPullParser.END_TAG) 253 next(xpp); 254 next(xpp); 255 } 256 257 protected IXMLWriter xml; 258 protected boolean htmlPretty; 259 260 261 262 /* -- worker routines --------------------------------------------------- */ 263 264 protected void parseTypeAttributes(XmlPullParser xpp, Type t) { 265 parseElementAttributes(xpp, t); 266 } 267 268 protected void parseElementAttributes(XmlPullParser xpp, Element e) { 269 if (xpp.getAttributeValue(null, "id") != null) { 270 e.setId(xpp.getAttributeValue(null, "id")); 271 idMap.put(e.getId(), e); 272 } 273 if (!comments.isEmpty()) { 274 e.getFormatCommentsPre().addAll(comments); 275 comments.clear(); 276 } 277 } 278 279 protected void parseElementClose(Base e) { 280 if (!comments.isEmpty()) { 281 e.getFormatCommentsPost().addAll(comments); 282 comments.clear(); 283 } 284 } 285 286 protected void parseBackboneAttributes(XmlPullParser xpp, Element e) { 287 parseElementAttributes(xpp, e); 288 } 289 290 private String pathForLocation(XmlPullParser xpp) { 291 return xpp.getPositionDescription(); 292 } 293 294 295 protected void unknownContent(XmlPullParser xpp) throws FHIRFormatError { 296 if (!isAllowUnknownContent()) 297 throw new FHIRFormatError("Unknown Content "+xpp.getName()+" @ "+pathForLocation(xpp)); 298 } 299 300 protected XhtmlNode parseXhtml(XmlPullParser xpp) throws XmlPullParserException, IOException, FHIRFormatError { 301 XhtmlParser prsr = new XhtmlParser(); 302 return prsr.parseHtmlNode(xpp); 303 } 304 305 private String parseString(XmlPullParser xpp) throws XmlPullParserException, FHIRFormatError, IOException { 306 StringBuilder res = new StringBuilder(); 307 next(xpp); 308 while (xpp.getEventType() == XmlPullParser.TEXT || xpp.getEventType() == XmlPullParser.IGNORABLE_WHITESPACE || xpp.getEventType() == XmlPullParser.ENTITY_REF) { 309 res.append(xpp.getText()); 310 next(xpp); 311 } 312 if (xpp.getEventType() != XmlPullParser.END_TAG) 313 throw new FHIRFormatError("Bad String Structure - parsed "+res.toString()+" now found "+Integer.toString(xpp.getEventType())); 314 next(xpp); 315 return res.length() == 0 ? null : res.toString(); 316 } 317 318 private int parseInt(XmlPullParser xpp) throws FHIRFormatError, XmlPullParserException, IOException { 319 int res = -1; 320 String textNode = parseString(xpp); 321 res = java.lang.Integer.parseInt(textNode); 322 return res; 323 } 324 325 protected DomainResource parseDomainResourceContained(XmlPullParser xpp) throws IOException, FHIRFormatError, XmlPullParserException { 326 next(xpp); 327 int eventType = nextNoWhitespace(xpp); 328 if (eventType == XmlPullParser.START_TAG) { 329 DomainResource dr = (DomainResource) parseResource(xpp); 330 nextNoWhitespace(xpp); 331 next(xpp); 332 return dr; 333 } else { 334 unknownContent(xpp); 335 return null; 336 } 337 } 338 protected Resource parseResourceContained(XmlPullParser xpp) throws IOException, FHIRFormatError, XmlPullParserException { 339 next(xpp); 340 int eventType = nextNoWhitespace(xpp); 341 if (eventType == XmlPullParser.START_TAG) { 342 Resource r = (Resource) parseResource(xpp); 343 nextNoWhitespace(xpp); 344 next(xpp); 345 return r; 346 } else { 347 unknownContent(xpp); 348 return null; 349 } 350 } 351 352 public void compose(IXMLWriter writer, Resource resource, boolean htmlPretty) throws IOException { 353 this.htmlPretty = htmlPretty; 354 xml = writer; 355 xml.setDefaultNamespace(FHIR_NS); 356 composeResource(resource); 357 } 358 359 protected abstract void composeResource(Resource resource) throws IOException ; 360 361 protected void composeElementAttributes(Element element) throws IOException { 362 if (style != OutputStyle.CANONICAL) 363 for (String comment : element.getFormatCommentsPre()) 364 xml.comment(comment, getOutputStyle() == OutputStyle.PRETTY); 365 if (element.getId() != null) 366 xml.attribute("id", element.getId()); 367 } 368 369 protected void composeElementClose(Base base) throws IOException { 370 if (style != OutputStyle.CANONICAL) 371 for (String comment : base.getFormatCommentsPost()) 372 xml.comment(comment, getOutputStyle() == OutputStyle.PRETTY); 373 } 374 protected void composeTypeAttributes(Type type) throws IOException { 375 composeElementAttributes(type); 376 } 377 378 protected void composeXhtml(String name, XhtmlNode html) throws IOException { 379 if (!Utilities.noString(xhtmlMessage)) { 380 xml.enter(XhtmlComposer.XHTML_NS, name); 381 xml.comment(xhtmlMessage, false); 382 xml.exit(XhtmlComposer.XHTML_NS, name); 383 } else { 384 XhtmlComposer comp = new XhtmlComposer(true, htmlPretty); 385 // name is also found in the html and should the same 386 // ? check that 387 boolean oldPretty = xml.isPretty(); 388 if (html.getNodeType() != NodeType.Text) 389 xml.namespace(XhtmlComposer.XHTML_NS, null); 390 comp.compose(xml, html); 391 xml.setPretty(oldPretty); 392 } 393 } 394 395 396 abstract protected void composeString(String name, StringType value) throws IOException ; 397 398 protected void composeString(String name, IIdType value) throws IOException { 399 composeString(name, new StringType(value.getValue())); 400 } 401 402 403 protected void composeDomainResource(String name, DomainResource res) throws IOException { 404 xml.enter(FHIR_NS, name); 405 composeResource(res.getResourceType().toString(), res); 406 xml.exit(FHIR_NS, name); 407 } 408 409 protected abstract void composeResource(String name, Resource res) throws IOException ; 410 411}