public virtual JDFDoc parseInputSource(XmlSourceSupport inSource, string schemaLocation, string documentClassName, XmlSaxErrorHandler errorHandler, bool bEraseEmpty, bool bDoNamespaces) { JDFDoc doc = null; if (errorHandler is XMLErrorHandler) { initParser(schemaLocation, documentClassName); } else { initParser(schemaLocation, documentClassName); } doc = runParser(inSource, bEraseEmpty); if (doc == null) { // this is the error case: if (!bDoNamespaces) { // try again, ignoring name spaces setIgnoreNamespace(false); doc = runParser(inSource, bEraseEmpty); } } return(doc); }
/// /// <summary> * parseStream - parse a stream specified by inStream /// * </summary> /// * <param name="inStream"> stream to parse </param> /// * <returns> JDFDoc or null if parse failed default: parseStream(inStream) </returns> /// public virtual JDFDoc parseStream(Stream inStream) { if (inStream == null) { return(null); } XMLReaderStream bis = new XMLReaderStream(false, inStream); SupportClass.BufferedStreamManager.manager.MarkPosition(0, bis); XmlSourceSupport inSource = new XmlSourceSupport(bis); JDFDoc d = parseInputSource(inSource); if (d == null && m_searchStream) { try { bis.Position = SupportClass.BufferedStreamManager.manager.ResetMark(bis); } catch (IOException) { bis.allowClose = true; bis.close(); return(null); } inSource = new XmlSourceSupport(new XMLReaderStream(true, bis)); d = parseInputSource(inSource); } bis.allowClose = true; bis.close(); return(d); }
/// /// <summary> * Method parse /// * </summary> /// * <seealso cref= javax.xml.parsers.DocumentBuilder#parse(org.xml.sax.InputSource) /// * </seealso> /// * <param name="is"> /// * /// * @return </param> /// public XmlDocument parse(XmlSourceSupport @is) { m_parser.parse(@is); XmlDocument doc = m_parser; // Java to C# Conversion - NOTE: Don't believe this is necessary in .NET //m_parser.reset(); return(doc); }
/// /// <summary> * parse an input source /// * </summary> /// * <param name="inSource"> the InputSource to parse </param> /// * <returns> JDFDoc the newly parsed doc </returns> /// public virtual JDFDoc parseInputSource(XmlSourceSupport inSource) { JDFDoc jdfDoc = null; if (inSource != null) { initParser(m_SchemaLocation, m_DocumentClass); jdfDoc = runParser(inSource, m_eraseEmpty); } return(jdfDoc); }
/// /// * <param name="parser"> </param> /// * <param name="inSource"> </param> /// * <param name="bEraseEmpty"> /// * @return </param> /// private JDFDoc runParser(XmlSourceSupport inSource, bool bEraseEmpty) { JDFDoc doc = new JDFDoc(); try { XmlReaderSettings settings = new XmlReaderSettings(); XmlReader reader = XmlTextReader.Create(inSource.Bytes, settings); base.Load(reader); doc.setMemberDoc((DocumentJDFImpl)this); if (bEraseEmpty) { doc.getRoot().eraseEmptyNodes(true); // cleanup the XML } } catch (StackOverflowException) { m_lastExcept = null; doc = null; } catch (Exception e) { m_lastExcept = e; doc = null; } if (doc != null && m_ErrorHandler != null) { doc.setValidationResult(m_ErrorHandler.getXMLOutput()); m_ErrorHandler.cleanXML(m_SchemaLocation); } if (doc != null) { KElement root = doc.getRoot(); DocumentJDFImpl memberDocument = doc.getMemberDocument(); string namespaceURI = root.getNamespaceURI(); if (namespaceURI == null || !namespaceURI.ToLower().Contains(JDFConstants.CIP4ORG)) { memberDocument.bKElementOnly = true; memberDocument.setIgnoreNSDefault(true); } else { memberDocument.setIgnoreNSDefault(ignoreNSDefault); } } return(doc); }
/// <summary>parse message according to our HL7 XML handler, and dump the data found /// to props. /// </summary> /// <summary>returns true if we parsed ok, which means well-formed XML, and /// that's about it. We just barely check against HL7 structure, and ignore any /// elements / text that is unexpected (that is, impossible in any HL7 message: /// independant of any message / segment definitions). /// "message" should be an XML document with one top-level element -- that being /// the message. (<ACK> or whatever). We're only expecting one message to be in /// "message". /// props can be null if you don't want the data (we still parse). The message /// data found in message (that passes msgMask) will be added to props as key / /// value pairs with the key a toString() of the appropriate DatumPath for the /// location where the data is found (i.e. in the ZYX[a]-b[c]-d-e style), and /// the value the corresponding text. So, after calling parseMessage /// successfully, if you wanted to retrieve the message data from props you /// might call something like /// props.getProperty((new DatumPath()).add("MSH").add(1).toString()) /// and that would return a String with "|", probably. /// Note that this package facilitates the extraction of message data in a way /// independent of message version (i.e. components and whatever getting added): /// With a message of "<FOO><ZYX><ZYX.42>fieldy-field-field</ZYX.42></ZYX></FOO>", /// "ZYX[0]-1[0]-1-1" will be the key that ends up in props (see notes at /// DatumPath.toString()) /// So if you, coding for a future version of the FOO message but /// recieving old-version message data, tried /// props.getProperty((new DatumPath()).add("ZYX").add(0).add(42).add(0).add(1).toString()) /// with the message above (that is, trying to extract a repetition and /// component that aren't there), you would get "ZYX[0]-42[0]-1-1" mapping to /// "fieldy-field-field" in the resulting props. /// If the message was /// "<FOO><ZYX><ZYX.42><ARG.1>component data</ARG.1></ZYX.42></ZYX></FOO>" /// and you, coding for an old version of this FOO message but recieving /// new-version FOO message data, tried /// props.getProperty((new DatumPath()).add("ZYX").add(0).add(42).toString()) /// you would get "ZYX[0]-42[0]-1-1" mapping to "component data" in the resulting /// props. /// msgMask lets you specify which parts of the message you want dumped to props. /// Passing in null gets you everything. Otherwise, msgMask's elements should /// all be DatumPaths (! => ClassCastException), and a particular part of the /// message will be dumped to props only if it's location, as represented by a /// DatumPath, startsWith (as in DatumPath.startsWith()) at least one element of /// msgMask. So if one element of msgMask was a (new DatumPath()).add(new /// String("ZYX")), then everything in all ZYX segment would get dumped to props. /// A (new DatumPath()).add(new String("ZYX")).add(1) would get only the first /// repetitions of same (if there is one) dumped to props. etc. etc. Note that /// a DatumPath of size() == 0 in msgMask will get you everything, no matter what /// the other elements of msgMask are, because all DatumPaths startsWith the /// zero-length DatumPath. /// Segment group elements (eg. ADT_A01.PROCEDURE) are handled fine, but they /// aren't addressed in msgMask or in the output in props -- basically any /// element tags at the level immediately inside the message element, and having /// a name that starts with the message element name + '.', is ignored (meaning /// it's contents are dealt with the same as if the start and end tags' just /// wasn't there.) /// </summary> public static bool parseMessage(System.Collections.Specialized.NameValueCollection props, System.String message, System.Collections.ICollection msgMask) { bool ret = false; try { XmlSAXDocumentManager factory = XmlSAXDocumentManager.NewInstance(); XmlSAXDocumentManager parser = XmlSAXDocumentManager.CloneInstance(factory); XmlSourceSupport inSrc = new XmlSourceSupport(new System.IO.MemoryStream(new System.Text.ASCIIEncoding().GetBytes(message))); HL7MessageHandler handler = new HL7MessageHandler(); handler.m_props = (props != null?props:new System.Collections.Specialized.NameValueCollection()); // it's expecting a props. if (msgMask != null) { handler.m_msgMask = msgMask; } else { handler.m_msgMask = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); SupportClass.ICollectionSupport.Add(handler.m_msgMask, new NuGenDatumPath()); } parser.parse(inSrc, handler); ret = true; } catch (System.IO.IOException e) { throw new NuGenHL7Exception(e); } catch (StopParsingException e) { throw new NuGenHL7Exception(e); } catch (System.Xml.XmlException e) { throw new NuGenHL7Exception(e); } return(ret); }
private System.Xml.XmlDocument ParseXml(System.IO.Stream templateStream) { try { var builder = new System.Xml.XmlDocument(); var inputSource = new XmlSourceSupport(templateStream); System.Xml.XmlDocument document = SupportClass.ParseDocument(builder, inputSource); return(document); } catch (System.IO.IOException e) { initialContext.ErrorHandler.Error(IO_ERROR, "Error occurred while trying to read xml template: " + e.Message, e); } catch (System.Exception e) { initialContext.ErrorHandler.Error(XML_PARSING_ERROR, "Error occurred while parsing xml template: " + e.Message, e); } return(null); }
/*******************************/ /// <summary> /// This method loads a Xml DOM tree in memory taking data from a Xml source. /// </summary> /// <param name="manager">The XmlDOMDocumentManager needed to build the XmlDocument instance.</param> /// <param name="source">The source to be used to build the DOM tree.</param> /// <returns>A XmlDocument class with the contains of the source.</returns> public static System.Xml.XmlDocument ParseDocument(System.Xml.XmlDocument document, XmlSourceSupport source) { if (source.Characters != null) { document.Load(source.Characters.BaseStream); return (System.Xml.XmlDocument)document.Clone(); } else { if (source.Bytes != null) { document.Load(source.Bytes); return (System.Xml.XmlDocument)document.Clone(); } else { if(source.Uri != null) { document.Load(source.Uri); return (System.Xml.XmlDocument)document.Clone(); } else throw new System.Xml.XmlException("The XmlSource class can't be null"); } } }
/// <summary> /// Parses the specified 'XmlSourceSupport' and processes the events over the specified handler, and /// resolves the entities with the specified URI. /// </summary> /// <param name="source">The 'XmlSourceSupport' instance with the XML.</param> public virtual void parse(XmlSourceSupport source) { if (source.Characters != null) parse(source.Characters.BaseStream); else { if (source.Bytes != null) parse(source.Bytes); else { if (source.Uri != null) parse(source.Uri); else throw new System.Xml.XmlException("The XmlSource class can't be null"); } } }
/// <summary> /// Parses the specified 'XmlSourceSupport' instance and process the events over the specified handler, /// and resolves the entities with the specified URI. /// </summary> /// <param name="source">The 'XmlSourceSupport' that contains the XML.</param> /// <param name="handler">The handler that manages the parser events.</param> public virtual void parse(XmlSourceSupport source, XmlSaxContentHandler handler) { if (source.Characters != null) parse(source.Characters.BaseStream, handler); else { if (source.Bytes != null) parse(source.Bytes, handler); else { if(source.Uri != null) parse(source.Uri, handler); else throw new System.Xml.XmlException("The XmlSource class can't be null"); } } }
/// <summary>parse message according to our HL7 XML handler, and dump the data found /// to props. /// </summary> /// <summary>returns true if we parsed ok, which means well-formed XML, and /// that's about it. We just barely check against HL7 structure, and ignore any /// elements / text that is unexpected (that is, impossible in any HL7 message: /// independant of any message / segment definitions). /// "message" should be an XML document with one top-level element -- that being /// the message. (<ACK> or whatever). We're only expecting one message to be in /// "message". /// props can be null if you don't want the data (we still parse). The message /// data found in message (that passes msgMask) will be added to props as key / /// value pairs with the key a toString() of the appropriate DatumPath for the /// location where the data is found (i.e. in the ZYX[a]-b[c]-d-e style), and /// the value the corresponding text. So, after calling parseMessage /// successfully, if you wanted to retrieve the message data from props you /// might call something like /// props.getProperty((new DatumPath()).add("MSH").add(1).toString()) /// and that would return a String with "|", probably. /// Note that this package facilitates the extraction of message data in a way /// independent of message version (i.e. components and whatever getting added): /// With a message of "<FOO><ZYX><ZYX.42>fieldy-field-field</ZYX.42></ZYX></FOO>", /// "ZYX[0]-1[0]-1-1" will be the key that ends up in props (see notes at /// DatumPath.toString()) /// So if you, coding for a future version of the FOO message but /// recieving old-version message data, tried /// props.getProperty((new DatumPath()).add("ZYX").add(0).add(42).add(0).add(1).toString()) /// with the message above (that is, trying to extract a repetition and /// component that aren't there), you would get "ZYX[0]-42[0]-1-1" mapping to /// "fieldy-field-field" in the resulting props. /// If the message was /// "<FOO><ZYX><ZYX.42><ARG.1>component data</ARG.1></ZYX.42></ZYX></FOO>" /// and you, coding for an old version of this FOO message but recieving /// new-version FOO message data, tried /// props.getProperty((new DatumPath()).add("ZYX").add(0).add(42).toString()) /// you would get "ZYX[0]-42[0]-1-1" mapping to "component data" in the resulting /// props. /// msgMask lets you specify which parts of the message you want dumped to props. /// Passing in null gets you everything. Otherwise, msgMask's elements should /// all be DatumPaths (! => ClassCastException), and a particular part of the /// message will be dumped to props only if it's location, as represented by a /// DatumPath, startsWith (as in DatumPath.startsWith()) at least one element of /// msgMask. So if one element of msgMask was a (new DatumPath()).add(new /// String("ZYX")), then everything in all ZYX segment would get dumped to props. /// A (new DatumPath()).add(new String("ZYX")).add(1) would get only the first /// repetitions of same (if there is one) dumped to props. etc. etc. Note that /// a DatumPath of size() == 0 in msgMask will get you everything, no matter what /// the other elements of msgMask are, because all DatumPaths startsWith the /// zero-length DatumPath. /// Segment group elements (eg. ADT_A01.PROCEDURE) are handled fine, but they /// aren't addressed in msgMask or in the output in props -- basically any /// element tags at the level immediately inside the message element, and having /// a name that starts with the message element name + '.', is ignored (meaning /// it's contents are dealt with the same as if the start and end tags' just /// wasn't there.) /// </summary> public static bool parseMessage(System.Collections.Specialized.NameValueCollection props, System.String message, System.Collections.ICollection msgMask) { bool ret = false; try { XmlSAXDocumentManager factory = XmlSAXDocumentManager.NewInstance(); XmlSAXDocumentManager parser = XmlSAXDocumentManager.CloneInstance(factory); XmlSourceSupport inSrc = new XmlSourceSupport(new System.IO.MemoryStream(new System.Text.ASCIIEncoding().GetBytes(message))); HL7MessageHandler handler = new HL7MessageHandler(); handler.m_props = (props != null?props:new System.Collections.Specialized.NameValueCollection()); // it's expecting a props. if (msgMask != null) handler.m_msgMask = msgMask; else { handler.m_msgMask = System.Collections.ArrayList.Synchronized(new System.Collections.ArrayList(10)); SupportClass.ICollectionSupport.Add(handler.m_msgMask, new NuGenDatumPath()); } parser.parse(inSrc, handler); ret = true; } catch (System.IO.IOException e) { throw new NuGenHL7Exception(e); } catch (StopParsingException e) { throw new NuGenHL7Exception(e); } catch (System.Xml.XmlException e) { throw new NuGenHL7Exception(e); } return ret; }
private System.Xml.XmlDocument ParseXml(System.IO.Stream templateStream) { try { var builder = new System.Xml.XmlDocument(); var inputSource = new XmlSourceSupport(templateStream); System.Xml.XmlDocument document = SupportClass.ParseDocument(builder, inputSource); return document; } catch (System.IO.IOException e) { initialContext.ErrorHandler.Error(IO_ERROR, "Error occurred while trying to read xml template: " + e.Message, e); } catch (System.Exception e) { initialContext.ErrorHandler.Error(XML_PARSING_ERROR, "Error occurred while parsing xml template: " + e.Message, e); } return null; }
/// /// <summary> * parse an input source /// * </summary> /// * <param name="inSource"> the InputSource to parse </param> /// public virtual void parse(XmlSourceSupport inSource) { parseInputSource(inSource); }
/*******************************/ public static System.Xml.XmlDocument ParseDocument(System.Xml.XmlDocument document, XmlSourceSupport source) { if (source.Characters != null) { document.Load(source.Characters.BaseStream); return((System.Xml.XmlDocument)document.Clone()); } if (source.Bytes != null) { document.Load(source.Bytes); return((System.Xml.XmlDocument)document.Clone()); } if (source.Uri != null) { document.Load(source.Uri); return((System.Xml.XmlDocument)document.Clone()); } throw new System.Xml.XmlException("The XmlSource class can't be null"); }