Parser parser = new GenericParser(); ValidationContext validationContext = new NoValidation(); parser.setValidationContext( validationContext );
/** * @see org.openmrs.hl7.HL7Service#parseHL7String(String) */ @Override public Message parseHL7String(String hl7Message) throws HL7Exception { // Any pre-parsing for HL7 messages would go here // or a module can use AOP to pre-parse the message // First, try and parse the message Message message; try { message = parser.parse(hl7Message); } catch (EncodingNotSupportedException e) { throw new HL7Exception("HL7 encoding not supported", e); } catch (HL7Exception e) { throw new HL7Exception("Error parsing message", e); } return message; }
/** * Converts XML message to ER7, first checking integrity of parse and throwing * an exception if parse not correct */ static String safeER7Conversion(String xmlMessage) throws HL7Exception { Message m = parser.parse(xmlMessage); String check = parser.encode(m, "XML"); if (!equivalent(xmlMessage, check)) { throw new HL7Exception("Parsed and encoded message not equivalent to original (possibilities: invalid message, bug in parser)"); } return parser.encode(m, "VB"); }
// the ANTLR grammar File f = new File("src/test/ressources/Java.g4"); // plug the ANTLR grammar in GenericParser gp = new GenericParser(f, "Java"); // load the file that we'd like to parse into a String variable String s = FileUtils.loadFileContent("src/test/ressources/HelloWorld.java"); // this listener will create an AST from the java file gp.setListener(new DefaultTreeListener()); // compile Parser/Lexer gp.compile(); ParserRuleContext ctx = ctx = gp.parse(s); // get access to AST Ast ast = dlist.getAst(); // print AST in dot format System.out.println(ast.toDot());
public static void main(String[] args) throws HL7Exception { String msgString = "MSH|^~\\&|RAMSOFT|SENDING FACILITY|RAMSOFT|RECEIVING FACILITY|20101223202939-0400||ADT^A08|101|P|2.3.1||||||||\r" + "EVN|A08|20101223202939-0400||||\r" + "PID||P12345^^^ISSUER|P12345^^^ISSUER||PATIENT^TEST^M^^^^||19741018|M|||10808 FOOTHILL BLVD^^RANCHO CUCAMONGA^CA^91730^US||(909)481-5872^^^sales@ramsoft.com|(909)481-5800x1||M||12345|286-50-9510|||\r" + "PV1||O||||||||||||||||||||||||||||||||||||||||||||||||||\r" + "AL1|1||^PORK^|\r" + "AL1|2||^PENICILLIN^|"; GenericParser parser = new GenericParser(); parser.setValidationContext(ValidationContextFactory.noValidation()); Message msg = parser.parse(msgString); System.out.println(msg.getClass().getName()); }
/** * Parses a message string and returns the corresponding Message object. * * @throws HL7Exception if the message is not correctly formatted. * @throws EncodingNotSupportedException if the message encoded is not supported by this parser. */ protected Message doParse(String message, String version) throws HL7Exception { return getAppropriateParser(message).doParse(message, version); }
/** * Checks the encoding of the message using getEncoding(), and returns the corresponding parser * (either pipeParser or xmlParser). If the encoding is not recognized an HL7Exception is * thrown. */ private Parser getAppropriateParser(String message) throws HL7Exception { String encoding = getEncoding(message); if ("VB".equalsIgnoreCase(encoding)) return pipeParser; if ("XML".equalsIgnoreCase(encoding)) return xmlParser; throw new HL7Exception("Can't find appropriate parser - encoding not recognized"); }
/** * @param context the HapiContext to be used */ public GenericParser(HapiContext context) { super(context); pipeParser = new PipeParser(context); xmlParser = new DefaultXMLParser(context); setPipeParserAsPrimary(); }
/** * Generates an empty response message. This class generates an * ACKnowledgement using the code returned by {@link #getSuccessAcknowledgementCode()}. * * @param request request message, either a {@link String} or a * {@link Message} * @return acknowledgment to the request * @throws HL7Exception */ protected Message generateResponseMessage(Object request) throws HL7Exception { try { Message in; if (request instanceof String) { Segment s = getHapiContext().getGenericParser().getCriticalResponseData( (String)request); in = s.getMessage(); DeepCopy.copy(s, (Segment) in.get("MSH")); } else if (request instanceof Message) { in = (Message) request; } else { throw new HL7Exception("Validated message must be either Message or String"); } return in.generateACK(getSuccessAcknowledgementCode(), null); } catch (IOException e) { throw new HL7Exception(e); } }
public static void main(String[] args) throws HL7Exception { String msgString = "MSH|^~\\&|RAMSOFT|SENDING FACILITY|RAMSOFT|RECEIVING FACILITY|20101223202939-0400||ADT^A08|101|P|2.3.1||||||||\r" + "EVN|A08|20101223202939-0400||||\r" + "PID||P12345^^^ISSUER|P12345^^^ISSUER||PATIENT^TEST^M^^^^||19741018|M|||10808 FOOTHILL BLVD^^RANCHO CUCAMONGA^CA^91730^US||(909)481-5872^^^sales@ramsoft.com|(909)481-5800x1||M||12345|286-50-9510|||\r" + "PV1||O||||||||||||||||||||||||||||||||||||||||||||||||||\r" + "AL1|1||^PORK^|\r" + "AL1|2||^PENICILLIN^|"; GenericParser parser = new GenericParser(); parser.setValidationContext(ValidationContextFactory.noValidation()); Message msg = parser.parse(msgString); System.out.println(msg.getClass().getName()); }
/** * Returns the version ID (MSH-12) from the given message, without fully parsing the message. * The version is needed prior to parsing in order to determine the message class into which the * text of the message should be parsed. * * @throws HL7Exception if the version field can not be found. */ public String getVersion(String message) throws HL7Exception { return getAppropriateParser(message).getVersion(message); }
/** * Checks the encoding of the message using getEncoding(), and returns the corresponding parser * (either pipeParser or xmlParser). If the encoding is not recognized an HL7Exception is * thrown. */ private Parser getAppropriateParser(String message) throws HL7Exception { String encoding = getEncoding(message); if ("VB".equalsIgnoreCase(encoding)) return pipeParser; if ("XML".equalsIgnoreCase(encoding)) return xmlParser; throw new HL7Exception("Can't find appropriate parser - encoding not recognized"); }
/** * Creates a new instance of GenericParser * * @param theFactory custom factory to use for model class lookup */ public GenericParser(ModelClassFactory theFactory) { super(theFactory); pipeParser = new PipeParser(theFactory); xmlParser = new DefaultXMLParser(theFactory); setPipeParserAsPrimary(); }
/** * Generates an empty response message. This class generates an * ACKnowledgement using the code returned by {@link #getSuccessAcknowledgementCode()}. * * @param request request message, either a {@link String} or a * {@link Message} * @return acknowledgment to the request * @throws HL7Exception */ protected Message generateResponseMessage(Object request) throws HL7Exception { try { Message in; if (request instanceof String) { Segment s = getHapiContext().getGenericParser().getCriticalResponseData( (String)request); in = s.getMessage(); DeepCopy.copy(s, (Segment) in.get("MSH")); } else if (request instanceof Message) { in = (Message) request; } else { throw new HL7Exception("Validated message must be either Message or String"); } return in.generateACK(getSuccessAcknowledgementCode(), null); } catch (IOException e) { throw new HL7Exception(e); } }
/** * @see ORUR01Handler#processMessage(Message) */ @Test(expected = ApplicationException.class) public void processMessage_shouldFailOnEmptyConceptAnswers() throws Exception { String hl7string = "MSH|^~\\&|FORMENTRY|AMRS.ELD|HL7LISTENER|AMRS.ELD|20080630094800||ORU^R01|kgWdFt0SVwwClOfJm3pe|P|2.5|1||||||||15^AMRS.ELD.FORMID\r" + "PID|||3^^^^~d3811480^^^^||John3^Doe^||\r" + "PV1||O|1^Unknown||||1^Super User (admin)|||||||||||||||||||||||||||||||||||||20080208|||||||V\r" + "ORC|RE||||||||20080208000000|1^Super User\r" + "OBR|1|||1238^MEDICAL RECORD OBSERVATIONS^99DCT\r" + "OBX|1|CWE|5497^CD4, BY FACS^99DCT||^^99DCT|||||||||20080208"; Message hl7message = parser.parse(hl7string); router.processMessage(hl7message); }
meta.getFields( data.outputRowMeta, getStepname(), null, null, this, repository, metaStore ); data.parser = new GenericParser(); data.parser.setValidationContext( new NoValidation() );
/** * Converts XML message to ER7, first checking integrity of parse and throwing * an exception if parse not correct */ static String safeER7Conversion(String xmlMessage) throws HL7Exception { Message m = parser.parse(xmlMessage); String check = parser.encode(m, "XML"); if (!equivalent(xmlMessage, check)) { throw new HL7Exception("Parsed and encoded message not equivalent to original (possibilities: invalid message, bug in parser)"); } return parser.encode(m, "VB"); }
/** * Parses a message string and returns the corresponding Message object. * * @throws HL7Exception if the message is not correctly formatted. * @throws EncodingNotSupportedException if the message encoded is not supported by this parser. */ protected Message doParse(String message, String version) throws HL7Exception { return getAppropriateParser(message).doParse(message, version); }
/** * Returns a "standardized" equivalent of the given message string. For delimited * messages, the returned value is the shortest string that has an equivalent * meaning in HL7. For XML-encoded messages, the returned value is equivalent XML output * using a standard pretty-print format. An automatic determination is made about whether * the given string is XML or ER7 (i.e. traditionally) encoded. * @param message an XML-encoded or ER7-encoded message string */ public static String standardize(String message) throws SAXException { String result = null; String encoding = parser.getEncoding(message); if (encoding.equals("XML")) { result = standardizeXML(message); } else { result = standardizeER7(message); } return result; }
/** * @param context the HapiContext to be used */ public GenericParser(HapiContext context) { super(context); pipeParser = new PipeParser(context); xmlParser = new DefaultXMLParser(context); setPipeParserAsPrimary(); }