public static Document parse(InputStream s, boolean validateIfSchema) { DOMImplementationLS impl = getDOMImpl(); LSInput input = impl.createLSInput(); input.setByteStream(s); return parse(input, validateIfSchema); }
/** * Returns a semantic equivalent of a given XML-encoded message in a default format. * Attributes, comments, and processing instructions are not considered to change the * HL7 meaning of the message, and are removed in the standardized representation. */ public static String standardizeXML(String message) throws SAXException { try { Document doc = XMLUtils.parse(message); clean(doc.getDocumentElement()); return XMLUtils.serialize(doc, true); } catch (Exception e) { throw new RuntimeException("Exception while standardizing XML ", e); } }
/** Parses profile string into DOM document */ private Document parseIntoDOM(String profileString) throws ProfileException { try { Document doc = XMLUtils.parse(profileString, true); if (alwaysValidate) XMLUtils.validate(doc, PROFILE_XSD, errorHandler); return doc; } catch (Exception e) { throw new ProfileException("Exception parsing message profile: " + e.getMessage(), e); } }
ClasspathResourceResolver() { impl = getDOMImpl(); }
public static Document parse(String s) { return parse(s, false); }
/** * Formats a Message object into an HL7 message string using this parser's default encoding (XML * encoding). This method calls the abstract method <code>encodeDocument(...)</code> in order to * obtain XML Document object representation of the Message, then serializes it to a String. * * @throws HL7Exception if the data fields in the message do not permit encoding (e.g. required * fields are null) */ protected String doEncode(Message source) throws HL7Exception { if (source instanceof GenericMessage) { throw new HL7Exception( "Can't XML-encode a GenericMessage. Message must have a recognized structure."); } Document doc = encodeDocument(source); // Element documentElement = doc.getDocumentElement(); // if (!documentElement.hasAttribute("xmlns")) // documentElement.setAttribute("xmlns", "urn:hl7-org:v2xml"); try { return XMLUtils.serialize(doc, getParserConfiguration().isPrettyPrintWhenEncodingXml()); } catch (Exception e) { throw new HL7Exception("Exception serializing XML document to string", e); } }
/** * <p>Creates an XML Document that corresponds to the given Message object. </p> * <p>If you are implementing this method, you should create an XML Document, and insert XML Elements * into it that correspond to the groups and segments that belong to the message type that your subclass * of XMLParser supports. Then, for each segment in the message, call the method * <code>encode(Segment segmentObject, Element segmentElement)</code> using the Element for * that segment and the corresponding Segment object from the given Message.</p> */ public Document encodeDocument(Message source) throws HL7Exception { String messageClassName = source.getClass().getName(); String messageName = messageClassName.substring(messageClassName.lastIndexOf('.') + 1); try { Document doc = XMLUtils.emptyDocument(messageName); //Element root = doc.createElement(messageName); //doc.appendChild(root); encode(source, doc.getDocumentElement()); return doc; } catch (Exception e) { throw new HL7Exception( "Can't create XML document - " + e.getClass().getName(), e); } }
ClasspathResourceResolver() { impl = getDOMImpl(); }
public static Document parse(String s) { return parse(s, false); }
/** * Formats a Message object into an HL7 message string using this parser's default encoding (XML * encoding). This method calls the abstract method <code>encodeDocument(...)</code> in order to * obtain XML Document object representation of the Message, then serializes it to a String. * * @throws HL7Exception if the data fields in the message do not permit encoding (e.g. required * fields are null) */ protected String doEncode(Message source) throws HL7Exception { if (source instanceof GenericMessage) { throw new HL7Exception( "Can't XML-encode a GenericMessage. Message must have a recognized structure."); } Document doc = encodeDocument(source); // Element documentElement = doc.getDocumentElement(); // if (!documentElement.hasAttribute("xmlns")) // documentElement.setAttribute("xmlns", "urn:hl7-org:v2xml"); try { return XMLUtils.serialize(doc, getParserConfiguration().isPrettyPrintWhenEncodingXml()); } catch (Exception e) { throw new HL7Exception("Exception serializing XML document to string", e); } }
/** * <p>Creates an XML Document that corresponds to the given Message object. </p> * <p>If you are implementing this method, you should create an XML Document, and insert XML Elements * into it that correspond to the groups and segments that belong to the message type that your subclass * of XMLParser supports. Then, for each segment in the message, call the method * <code>encode(Segment segmentObject, Element segmentElement)</code> using the Element for * that segment and the corresponding Segment object from the given Message.</p> */ public Document encodeDocument(Message source) throws HL7Exception { String messageClassName = source.getClass().getName(); String messageName = messageClassName.substring(messageClassName.lastIndexOf('.') + 1); try { Document doc = XMLUtils.emptyDocument(messageName); //Element root = doc.createElement(messageName); //doc.appendChild(root); encode(source, doc.getDocumentElement()); return doc; } catch (Exception e) { throw new HL7Exception( "Can't create XML document - " + e.getClass().getName(), e); } }
public static Document parse(String s, boolean validateIfSchema) { DOMImplementationLS impl = getDOMImpl(); LSInput input = impl.createLSInput(); input.setStringData(s); return parse(input, validateIfSchema); }
/** Parses profile string into DOM document */ private Document parseIntoDOM(String profileString) throws ProfileException { try { Document doc = XMLUtils.parse(profileString, true); if (alwaysValidate) XMLUtils.validate(doc, PROFILE_XSD, errorHandler); return doc; } catch (Exception e) { throw new ProfileException("Exception parsing message profile: " + e.getMessage(), e); } }
public static Document emptyDocument(String title) { DOMImplementation impl = getDOMImpl(); Document doc = impl.createDocument("urn:hl7-org:v2xml", title, null); return doc; }
/** * Returns a semantic equivalent of a given XML-encoded message in a default format. * Attributes, comments, and processing instructions are not considered to change the * HL7 meaning of the message, and are removed in the standardized representation. */ public static String standardizeXML(String message) throws SAXException { try { Document doc = XMLUtils.parse(message); clean(doc.getDocumentElement()); return XMLUtils.serialize(doc, true); } catch (Exception e) { throw new RuntimeException("Exception while standardizing XML ", e); } }
/** * Parses a string containing an XML document into a Document object. * * Note that this method is synchronized currently, as the XML parser is not thread safe * * @throws HL7Exception */ protected synchronized Document parseStringIntoDocument(String message) throws HL7Exception { try { return XMLUtils.parse(message); } catch (Exception e) { throw new HL7Exception("Exception parsing XML", e); } }
public static Document parse(InputStream s, boolean validateIfSchema) { DOMImplementationLS impl = getDOMImpl(); LSInput input = impl.createLSInput(); input.setByteStream(s); return parse(input, validateIfSchema); }
/** * Test/validate a given xml document against a hl7 v2.xml schema. * <p> * Before the schema is applied, the namespace is verified because otherwise schema validation * fails anyway. * <p> * If a schema file is specified in the xml message and the file can be located on the disk this * one is used. If no schema has been specified, or the file can't be located, the locations * property is used. * * @param msg the xml message (as string) to be validated. * @return ValidationException[] an array of validation exceptions, which is zero-sized when no * validation errors occured. */ public ValidationException[] apply(String msg) { List<ValidationException> validationErrors = new ArrayList<ValidationException>(); try { // parse the incoming string into a dom document - no schema validation yet Document doc = XMLUtils.parse(msg); if (hasCorrectNamespace(doc, validationErrors)) { XMLUtils.validate(doc, getSchemaLocation(doc), new ErrorHandler(validationErrors)); } } catch (Exception e) { log.error("Unable to validate message: {}", e.getMessage(), e); validationErrors.add(new ValidationException("Unable to validate message " + e.getMessage(), e)); } return validationErrors.toArray(new ValidationException[validationErrors.size()]); }
public static Document emptyDocument(String title) { DOMImplementation impl = getDOMImpl(); Document doc = impl.createDocument("urn:hl7-org:v2xml", title, null); return doc; }
/** * Parses a string containing an XML document into a Document object. * * Note that this method is synchronized currently, as the XML parser is not thread safe * * @throws HL7Exception */ protected synchronized Document parseStringIntoDocument(String message) throws HL7Exception { try { return XMLUtils.parse(message); } catch (Exception e) { throw new HL7Exception("Exception parsing XML", e); } }