/// <summary>
        /// Graphs object <paramref name="o"/> onto <paramref name="s"/>
        /// </summary>
        /// <param name="s">The stream to graph to</param>
        /// <param name="o">The object to graph</param>
        /// <returns>A formatter graphing result</returns>
        public IFormatterGraphResult Graph(XmlWriter s, IGraphable o)
        {
            if (o == null)
            {
                return(new DatatypeR2FormatterGraphResult(ResultCode.Accepted, null, this.ValidateConformance));
            }


            try
            {
                IDatatypeFormatter formatter = GetFormatter(o.GetType());

                if (formatter == null)
                {
                    return(new DatatypeR2FormatterGraphResult(ResultCode.NotAvailable, new IResultDetail[] {
                        new NotImplementedResultDetail(ResultDetailType.Error, String.Format("Could not find formatter for '{0}'", o.GetType().FullName), null)
                    }, this.ValidateConformance));
                }

                // Set the host
                formatter.Host = (IXmlStructureFormatter)(this.Host ?? this);

                var result = new DatatypeR2FormatterGraphResult(ResultCode.Accepted, null, this.ValidateConformance);

                formatter.Graph(s, o, result);

                return(result);
            }
            catch (Exception e)
            {
                return(new DatatypeR2FormatterGraphResult(ResultCode.Error, new IResultDetail[] {
                    new ResultDetail(ResultDetailType.Error, e.Message, s.ToString(), e)
                }, this.ValidateConformance));
            }
        }
Example #2
0
 /// <summary>
 /// Default message
 /// </summary>
 /// <returns></returns>
 public MARC.Everest.RMIM.CA.R020402.Interactions.MCCI_IN000002CA Anything(IGraphable request)
 {
     
     return new MARC.Everest.RMIM.CA.R020402.Interactions.MCCI_IN000002CA(
         Guid.NewGuid(),
         DateTime.Now,
         ResponseMode.Immediate,
         MCCI_IN000002CA.GetInteractionId(),
         MCCI_IN000002CA.GetProfileId(),
         ProcessingID.Production,
         AcknowledgementCondition.Never,
         new Receiver(
             new TEL() { NullFlavor = NullFlavor.NoInformation },
             new Device2(
                 new II() { NullFlavor = NullFlavor.NoInformation }
             )
         ),
         new Sender(
             new TEL(OperationContext.Current.Channel.LocalAddress.Uri.ToString()),
             new Device1(
                 new II("1.2.3.4", "1234"),
                 "Sample Service",
                 "A sample service",
                 null,
                 "Mohawk College of Applied Arts and Technology",
                 "Everest"
             )
         ),
         new Acknowledgement(
             AcknowledgementType.ApplicationAcknowledgementAccept,
             new TargetMessage() { NullFlavor = NullFlavor.NoInformation },
             new AcknowledgementDetail(
                 AcknowledgementDetailType.Information,
                 AcknowledgementDetailCode.InternalSystemError,
                 String.Format("You just used the Everest Serializer! Received a '{0}'", request.GetType().Name),
                 null
             )
         )
     );
             
             
 }
Example #3
0
        /// <summary>
        /// Write a value as an element
        /// </summary>
        private IFormatterGraphResult WriteValueAsElement(XmlWriter s, String element, IGraphable value, Type expectedType, params KeyValuePair <String, String>[] attrs)
        {
            s.WriteStartElement(element, "urn:hl7-org:v3");

            // Value value xsi type
            if (expectedType != null && value.GetType() != expectedType)
            {
                s.WriteAttributeString("xsi", "type", DatatypeFormatter.NS_XSI, Util.CreateXSITypeName(value.GetType()));
            }

            if (attrs != null)
            {
                foreach (var attr in attrs)
                {
                    s.WriteAttributeString(attr.Key, attr.Value);
                }
            }

            var hostResult = Host.Graph(s, value);

            s.WriteEndElement();
            return(hostResult);
        }
Example #4
0
        /// <summary>
        /// Graphs object <paramref name="o"/> onto <paramref name="s"/>
        /// </summary>
        /// <param name="s">The stream to graph to</param>
        /// <param name="o">The object to graph</param>
        /// <returns>A formatter graphing result</returns>
        public IFormatterGraphResult Graph(XmlWriter s, IGraphable o)
        {
            if (o == null)
                return new DatatypeFormatterGraphResult(this.CompatibilityMode, ResultCode.Accepted, null, this.ValidateConformance);

            try
            {
                IDatatypeFormatter formatter = GetFormatter(o.GetType());

                if (formatter == null)
                    return new DatatypeFormatterGraphResult(this.CompatibilityMode, ResultCode.NotAvailable, new IResultDetail[] {
                        new NotImplementedResultDetail(ResultDetailType.Error, String.Format("Could not find formatter for '{0}'", o.GetType().FullName), null)
                    }, this.ValidateConformance);

                // Set the host
                formatter.Host = (IXmlStructureFormatter)(this.Host ?? this);

                var result = new DatatypeFormatterGraphResult(this.CompatibilityMode, ResultCode.Accepted, null, this.ValidateConformance);

                formatter.Graph(s, o, result);

                return result;
            }
            catch (Exception e)
            {
                return new DatatypeFormatterGraphResult(this.CompatibilityMode, ResultCode.Error, new IResultDetail[] {
                    new ResultDetail(ResultDetailType.Error, e.Message, s.ToString(), e)
                }, this.ValidateConformance);
            }
        }
Example #5
0
        /// <summary>
        /// Write a value as an element
        /// </summary>
        private IFormatterGraphResult WriteValueAsElement(XmlWriter s, String element, IGraphable value, Type expectedType, params KeyValuePair<String, String>[] attrs)
        {
            s.WriteStartElement(element, "urn:hl7-org:v3");

            // Value value xsi type
            if (expectedType != null && value.GetType() != expectedType)
                s.WriteAttributeString("xsi", "type", DatatypeFormatter.NS_XSI, Util.CreateXSITypeName(value.GetType()));

            if (attrs != null)
                foreach (var attr in attrs)
                    s.WriteAttributeString(attr.Key, attr.Value);

            var hostResult = Host.Graph(s, value);
            s.WriteEndElement();
            return hostResult;
        }
Example #6
0
        /// <summary>
        /// Parse the RTO
        /// </summary>
        public object Parse(System.Xml.XmlReader s, DatatypeR2FormatterParseResult result)
        {
            // Create the types
            Type rtoType = typeof(RTO <,>);

            QTYFormatter baseFormatter = (new QTYFormatter()
            {
                Host = this.Host
            });
            // Quantity value, just read into a REAL
            var qty = baseFormatter.ParseAttributes <REAL>(s, result);

            // Temporary values
            IGraphable denominator = null,
                       numerator   = null;

            // Elements
            // This type is a little different in R2, basically we can't determine the generic
            // parameters by the XSI Type so we have to parse the num/denom manually, and
            // ensure that we have both, then we construct the object and set the properties
            #region Elements
            if (!s.IsEmptyElement)
            {
                int    sDepth = s.Depth;
                string sName  = s.Name;

                s.Read();
                // string Name
                while (!(s.NodeType == System.Xml.XmlNodeType.EndElement && s.Depth == sDepth && s.Name == sName))
                {
                    string oldName = s.Name; // Name
                    try
                    {
                        // Numerator
                        if (s.LocalName == "numerator")
                        {
                            var parseResult = Host.Parse(s, GenericArguments[0]);
                            result.Code = parseResult.Code;
                            result.AddResultDetail(parseResult.Details);
                            numerator = parseResult.Structure;
                        }
                        // Denominator
                        else if (s.LocalName == "denominator")
                        {
                            var parseResult = Host.Parse(s, GenericArguments[1]);
                            result.Code = parseResult.Code;
                            result.AddResultDetail(parseResult.Details);
                            denominator = parseResult.Structure;
                        }
                        else
                        {
                            baseFormatter.ParseElementsInline(s, qty, result);
                        }
                    }
                    catch (MessageValidationException e)
                    {
                        result.AddResultDetail(new MARC.Everest.Connectors.ResultDetail(MARC.Everest.Connectors.ResultDetailType.Error, e.Message, e));
                    }
                    finally
                    {
                        if (s.Name == oldName)
                        {
                            s.Read();
                        }
                    }
                }
            }
            #endregion

            try
            {
                // Construct the generic type (if possible)
                Type rtoGenericType = rtoType.MakeGenericType(new Type[]
                {
                    numerator == null ? typeof(IQuantity) : numerator.GetType(),
                    denominator == null ? typeof(IQuantity) : denominator.GetType()
                });

                // Create an instance of rto from the rtoType
                object instance = rtoGenericType.GetConstructor(Type.EmptyTypes).Invoke(null);

                // Get the values from the QTY and copy
                IQuantity rtoQty = instance as IQuantity;
                DatatypeR2Formatter.CopyBaseAttributes(qty, rtoQty as ANY);
                rtoQty.Expression      = qty.Expression;
                rtoQty.OriginalText    = qty.OriginalText;
                rtoQty.UncertainRange  = qty.UncertainRange;
                rtoQty.Uncertainty     = qty.Uncertainty;
                rtoQty.UncertaintyType = qty.UncertaintyType;

                // rto properties
                PropertyInfo numeratorProperty   = rtoGenericType.GetProperty("Numerator"),
                             denominatorProperty = rtoGenericType.GetProperty("Denominator");

                // Set the previously found properties
                numeratorProperty.SetValue(instance, Util.FromWireFormat(numerator, numeratorProperty.PropertyType), null);
                denominatorProperty.SetValue(instance, Util.FromWireFormat(denominator, denominatorProperty.PropertyType), null);

                // Validate
                ANYFormatter anyFormatter = new ANYFormatter();
                string       pathName     = s is XmlStateReader ? (s as XmlStateReader).CurrentPath : s.Name;
                anyFormatter.Validate(instance as ANY, pathName, result);

                return(instance);
            }
            catch (Exception e)
            {
                result.AddResultDetail(new ResultDetail(ResultDetailType.Error, e.Message, e));
                return(null);
            }
        }
Example #7
0
 /// <summary>
 /// Default message
 /// </summary>
 /// <returns></returns>
 public MARC.Everest.RMIM.CA.R020402.Interactions.MCCI_IN000002CA Anything(IGraphable request)
 {
     return(new MARC.Everest.RMIM.CA.R020402.Interactions.MCCI_IN000002CA(
                Guid.NewGuid(),
                DateTime.Now,
                ResponseMode.Immediate,
                MCCI_IN000002CA.GetInteractionId(),
                MCCI_IN000002CA.GetProfileId(),
                ProcessingID.Production,
                AcknowledgementCondition.Never,
                new Receiver(
                    new TEL()
     {
         NullFlavor = NullFlavor.NoInformation
     },
                    new Device2(
                        new II()
     {
         NullFlavor = NullFlavor.NoInformation
     }
                        )
                    ),
                new Sender(
                    new TEL(OperationContext.Current.Channel.LocalAddress.Uri.ToString()),
                    new Device1(
                        new II("1.2.3.4", "1234"),
                        "Sample Service",
                        "A sample service",
                        null,
                        "Mohawk College of Applied Arts and Technology",
                        "Everest"
                        )
                    ),
                new Acknowledgement(
                    AcknowledgementType.ApplicationAcknowledgementAccept,
                    new TargetMessage()
     {
         NullFlavor = NullFlavor.NoInformation
     },
                    new AcknowledgementDetail(
                        AcknowledgementDetailType.Information,
                        AcknowledgementDetailCode.InternalSystemError,
                        String.Format("You just used the Everest Serializer! Received a '{0}'", request.GetType().Name),
                        null
                        )
                    )
                ));
 }
Example #8
0
        /// <summary>
        /// Graphs object <paramref name="o"/> onto stream <paramref name="s"/>
        /// </summary>
        /// <param name="s">The stream to which <paramref name="o"/> is to be graphed</param>
        /// <param name="o">The object to be graphed</param>
        /// <returns>A <see cref="T:MARC.Everest.Formatters.XML.ITS1.Its1FormatterGraphResult"/> structure contianing the 
        /// results of formatting</returns>
        /// <seealso cref="F:Graph(System.Xml.XmlWriter,MARC.Everest.Interfaces.IGraphable)"/>
        public IFormatterGraphResult Graph(Stream s, IGraphable o)
        {
            ThrowIfDisposed();

            XmlWriter xwtr = XmlWriter.Create(s);
            XmlStateWriter xw = new XmlStateWriter(xwtr);

            if (o == null)
                return new XmlIts1FormatterGraphResult(ResultCode.AcceptedNonConformant, null);


            //TODO: Length will never be less than 0 so the logic should be > 0 instead of != 0.
            bool needsRoot = o.GetType().GetCustomAttributes(typeof(InteractionAttribute), true).Length == 0;
            if (needsRoot)
            {
                object[] sa = o.GetType().GetCustomAttributes(typeof(StructureAttribute), true);
                needsRoot = sa.Length == 0 || !(sa[0] as StructureAttribute).IsEntryPoint;
                if (needsRoot)
                {
                    xw.WriteStartElement(o.GetType().FullName, "urn:hl7-org:v3");
                    xw.WriteAttributeString("xmlns", "xsi", null, XmlIts1Formatter.NS_XSI);

                }

            }

            // Determine if this is not an interaction
            var result = Graph(xw, o);

            if (needsRoot)
                xw.WriteEndElement();

            xw.Flush();
            return result;
        }
Example #9
0
        /// <summary>
        /// Graphs <paramref name="o"/> onto the <see cref="T:System.Xml.XmlWriter"/> <paramref name="s"/>
        /// </summary>
        /// <param name="s">The <see cref="T:System.Xml.XmlWriter"/> that should be used to graph the object</param>
        /// <param name="o">The <see cref="T:MARC.Everest.Interfaces.IGraphable"/> that should be graphed</param>
        /// <returns>A <see cref="T:MARC.Everest.Formatters.XML.ITS1.Its1FormatterGraphResult"/> containing the results of the graph</returns>
        /// <remarks>
        /// <para>
        /// This particular overload of the Graph method can be used to graph objects into a <see cref="T:System.Xml.XmlWriter"/> and can provide
        /// an opportunity to use Everest as an assistant (or XmlSerializerSurrogate) for more complex systems like WCF.
        /// </para>
        /// <example>
        /// <code lang="cs" title="Using Everest as a Serializer Assistant">
        /// <![CDATA[
        /// StringWriter sw = new StringWriter();
        /// XmlWriter writer = XmlWriter.Create(sw);
        /// writer.WriteStartElement("hello", "urn:my-org");
        /// 
        /// Formatter f = new Formatter();
        /// f.GraphAides.Add(typeof(MARC.Everest.Formatters.XML.DataTypes.R1.Formatter));
        /// f.ValidateConformance = false;
        /// f.Graph(writer, new MCCI_IN000002CA());
        /// 
        /// writer.WriteEndElement();
        /// writer.Close();
        /// Console.WriteLine(sw.ToString());
        /// 
        /// ]]>
        /// </code>
        /// outputs
        /// <code lang="xml" title="Output of example">
        /// <![CDATA[
        /// <hello xmlns="urn:my-org">
        ///     <MCCI_IN000002CA xmlns="urn:hl7-org:v3"/>
        /// </hello>
        /// ]]>
        /// </code>
        /// </example>
        /// <para>
        /// When using this method, it is recommended that you pass a <see cref="T:MARC.Everest.Xml.XmlStateWriter"/> as it 
        /// will allow the formatter to give additional location information when reporting issues.
        /// </para>
        /// </remarks>
        public IFormatterGraphResult Graph(XmlWriter s, IGraphable o)
        {
            ThrowIfDisposed();

            var resultContext = new XmlIts1FormatterGraphResult(ResultCode.Accepted, null);

            IGraphable context = null;
            // Clear out result cache
            if (s.WriteState == WriteState.Start || String.IsNullOrEmpty(s.ToString()) || o is IInteraction)
                context = o;

            if (!(s is XmlStateWriter))
                s = new XmlStateWriter(s);

            if (o == null)
                resultContext.Code = ResultCode.AcceptedNonConformant;
            else
            {
                GraphObject(s, o, o.GetType(), context, resultContext);
                resultContext.Code = CalculateResultCode(resultContext.Details);
            }

            return resultContext;
        }
Example #10
0
        public void ValidateHelper(XmlWriter s, IGraphable o, ITypeFormatter formatter, XmlIts1FormatterGraphResult resultContext)
        {
            IResultDetail[] details = null;

            if (ValidateConformance && (!formatter.Validate(o, s.ToString(), out details)))
                resultContext.AddResultDetail(details.Length > 0 ? details : new IResultDetail[] { new DatatypeValidationResultDetail(ValidateConformance ? ResultDetailType.Error : ResultDetailType.Warning, o.GetType().ToString(), s.ToString()) });
        }
Example #11
0
        public virtual void WriteElementUtil(XmlWriter s, string namespaceUri, string elementName, IGraphable g, Type propType, IGraphable context, XmlIts1FormatterGraphResult resultContext)
        {
            ThrowIfDisposed();

            
            // Graph is nothing
            if (g == null)
                return;

            // Normalize
            if (g is INormalizable)
                g = (g as INormalizable).Normalize();

            // Write start of element
            s.WriteStartElement(elementName, namespaceUri);
            
            // JF: Output XSI:Type
            if (!g.GetType().Equals(propType) )
            {
                // TODO: This may cause issue when assigning a QSET to an R1 or
                //       SXPR to R2 instance as the XSI:TYPE will be inappropriately
                //       assigned.

                string xsiType = String.Empty;
                if (typeof(ANY).IsAssignableFrom(g.GetType()))
                {
                    xsiType += s.LookupPrefix("urn:hl7-org:v3");
                    if (!String.IsNullOrEmpty(xsiType))
                        xsiType += ":";
                    xsiType += Util.CreateXSITypeName(g.GetType());
                }
                else if (propType != null && g.GetType().Assembly.FullName != propType.Assembly.FullName)
                {
                    string typeName = this.CreateXSITypeName(g.GetType(), context != null ? context.GetType() : null, s as IXmlNamespaceResolver);

                    // If there is no different then don't output
                    if (typeName != String.Format("{0}.{1}", this.GetModelName(propType), this.GetStructureName(propType)))
                    {

                        lock (this.m_syncRoot)
                            if (!this.s_typeNameMaps.ContainsKey(typeName))
                                this.RegisterXSITypeName(typeName, g.GetType());
                    }
                    xsiType = typeName;
                }
                if(!String.IsNullOrEmpty(xsiType))
                    s.WriteAttributeString("xsi", "type", XmlIts1Formatter.NS_XSI, xsiType);

                //string xsdTypeName = String.Empty;
                //object[] sa = g.GetType().GetCustomAttributes(typeof(StructureAttribute), false);
                //if (sa.Length > 0 && (sa[0] as StructureAttribute).StructureType == StructureAttribute.StructureAttributeType.DataType)
                //    s.WriteAttributeString("xsi", "type", null, (sa[0] as StructureAttribute).Name);
            }

            // Result code of the
            GraphObject(s, g, g.GetType(), context, resultContext);
            s.WriteEndElement();
        }