private <R> void assertMessageValidates(Message message) throws HL7Exception { if (isValidating()) { Validator<R> validator = getHapiContext().getMessageValidator(); ValidationExceptionHandlerFactory<R> factory = getHapiContext().getValidationExceptionHandlerFactory(); if (factory == null) { throw new NullPointerException("Validation is enabled for this parser, but ValidationExceptionHandlerFactory is null"); } ValidationExceptionHandler<R> handler = factory.getNewInstance(getHapiContext()); R result = validator.validate(message, handler); handleException(handler, result); } }
parser.setValidationContext( validationContext ); Message msg = parser.parse( message ); Structure structure = msg.get( "MSH" ); String messageType = null;
/** * Formats a Message object into an HL7 message string using this parser's default encoding. * * @param source a Message object from which to construct an encoded message string * @return the encoded message * @throws HL7Exception if the data fields in the message do not permit encoding (e.g. required * fields are null) */ public String encode(Message source) throws HL7Exception { assertMessageValidates(source); String result = doEncode(source); assertMessageValidates(result, getDefaultEncoding(), source.getVersion()); return result; }
/** * @return the set of validation rules that is applied to messages parsed or encoded by this * parser. Note that this method may return <code>null</code> */ public ValidationContext getValidationContext() { return isValidating() ? getHapiContext().getValidationContext() : null; }
/** * {@inheritDoc } */ @Override public void parse(Type type, String string, EncodingCharacters encodingCharacters) throws HL7Exception { primaryParser.parse(type, string, encodingCharacters); }
String encoding = getEncoding(message); if (!supportsEncoding(encoding)) { String startOfMessage = null; if (message.startsWith("MSH")) { String version = getVersion(message); if (!getParserConfiguration().isAllowUnknownVersions()) { assertVersionExists(version); assertMessageValidates(message, encoding, version); Message result = doParse(message, version); assertMessageValidates(result); applySuperStructureName(result);
String outgoingMessageCharset = null; try { incomingMessageObject = myParser.parse(incomingMessageString); log.debug("Exception parsing incoming message", e); try { outgoingMessageString = logAndMakeErrorMessage(e, myParser.getCriticalResponseData(incomingMessageString), myParser, myParser.getEncoding(incomingMessageString)); } catch (HL7Exception e2) { log.error("Exception occurred while logging parse failure", e2); outgoingMessageString = myParser.encode(response, myParser.getEncoding(incomingMessageString));
/** * Parse a message using a specific model package instead of the default, using * {@link ModelClassFactory#getMessageClassInASpecificPackage(String, String, boolean, String)} * . * * <b>WARNING: This method is only implemented in some parser implementations</b>. Currently it * will only work with the PipeParser parser implementation. Use with caution. * * @param message message string * @param packageName name of the package of the models * @return parsed message * @throws HL7Exception if an error occurred while parsing */ public Message parseForSpecificPackage(String message, String packageName) throws HL7Exception { String encoding = getEncoding(message); if (!supportsEncoding(encoding)) { throw new EncodingNotSupportedException("Can't parse message beginning " + message.substring(0, Math.min(message.length(), 50))); } String version = getVersion(message); assertVersionExists(version); assertMessageValidates(message, encoding, version); Message result = doParseForSpecificPackage(message, version, packageName); assertMessageValidates(result); result.setParser(this); return result; }
/** * {@inheritDoc } */ public String encode() throws HL7Exception { return getParser().encode(this); }
/** * Returns given segment serialized as a pipe-encoded string. * * @param source segment to be encoded * @param encodingChars encoding characters to be used * @return encoded group */ public static String encode(Segment source, EncodingCharacters encodingChars) { return encode(source, encodingChars, source.getMessage().getParser().getParserConfiguration(), null); }
private static Transportable makeAcceptAck(Transportable theMessage, String theAckCode, ErrorCode theErrorCode, String theDescription) throws HL7Exception { Segment header = ourParser.getCriticalResponseData(theMessage.getMessage()); Message dummy = header.getMessage(); // MSH header refers to dummy, but not the other way round! DeepCopy.copy(header, (Segment)dummy.get("MSH")); try { HL7Exception hl7e = new HL7Exception(theDescription, theErrorCode); AcknowledgmentCode code = theAckCode == null ? AcknowledgmentCode.CR : AcknowledgmentCode.valueOf(theAckCode); Message out = dummy.generateACK(code, hl7e); String originalEncoding = ourParser.getEncoding(theMessage.getMessage()); String ackText = ourParser.encode(out, originalEncoding); return new TransportableImpl(ackText); } catch (IOException e) { throw new HL7Exception(e); } }
/** * @param context the set of validation rules to be applied to messages parsed or encoded by * this parser (defaults to ValidationContextFactory.DefaultValidation) * * @deprecated use a dedicated {@link HapiContext} and set its ValidationContext property */ public void setValidationContext(ValidationContext context) { HapiContext newContext = new DefaultHapiContext(getHapiContext()); newContext.setValidationContext(context); setHapiContext(newContext); }
/** * Creates an instance that uses the specified <code>Parser</code>. * * @param theParser the parser used for converting between Message and * Transportable */ public ApplicationRouterImpl(Parser theParser) { this(theParser.getHapiContext(), theParser); }
/** * Returns the set of validation rules that applied to this message. If the parser * was set to "not-validating", this method returns null * * @return the set of validation rules that applied to this message */ public ValidationContext getValidationContext() { if (getParser() == null || !getParser().getParserConfiguration().isValidating()) return null; return getParser().getHapiContext().getValidationContext(); }
/** * Given a concatenation of message type and event (e.g. ADT_A04), and the version, finds the * corresponding message structure (e.g. ADT_A01). This is needed because some events share * message structures, although it is not needed when the message structure is explicitly valued * in MSH-9-3. If no mapping is found, returns the original name. * * @throws HL7Exception if there is an error retrieving the map, or if the given version is * invalid * * @deprecated use {@link ModelClassFactory#getMessageStructureForEvent(String, Version)} */ public String getMessageStructureForEvent(String name, String version) throws HL7Exception { assertVersionExists(version); return getHapiContext().getModelClassFactory(). getMessageStructureForEvent(name, Version.versionOf(version)); }
@Override public void setValidationContext(ValidationContext context) { super.setValidationContext(context); }
/** * Formats a Message object into an HL7 message string using the given encoding. * * @param source a Message object from which to construct an encoded message string * @param encoding the name of the HL7 encoding to use (eg "XML"; most implementations support * only one encoding) * @return the encoded message * @throws HL7Exception if the data fields in the message do not permit encoding (e.g. required * fields are null) * @throws EncodingNotSupportedException if the requested encoding is not supported by this * parser. */ public String encode(Message source, String encoding) throws HL7Exception { assertMessageValidates(source); String result = doEncode(source, encoding); assertMessageValidates(result, encoding, source.getVersion()); return result; }
/** * {@inheritDoc } */ @Override public void parse(Segment segment, String string, EncodingCharacters encodingCharacters) throws HL7Exception { primaryParser.parse(segment, string, encodingCharacters); }
String encoding = getEncoding(message); if (!supportsEncoding(encoding)) { String startOfMessage = null; if (message.startsWith("MSH")) { String version = getVersion(message); if (!getParserConfiguration().isAllowUnknownVersions()) { assertVersionExists(version); assertMessageValidates(message, encoding, version); Message result = doParse(message, version); assertMessageValidates(result); applySuperStructureName(result);