📄 aecgformat.cs
字号:
/***************************************************************************
Copyright 2008, Thoraxcentrum, Erasmus MC, Rotterdam, The Netherlands
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
Written by Maarten JB van Ettinger.
****************************************************************************/
using System;
using System.Collections;
using System.IO;
using System.Reflection;
using System.Runtime.InteropServices;
using System.Text.RegularExpressions;
using System.Xml;
using System.Xml.XPath;
using System.Xml.Xsl;
using Communication.IO.Tools;
using ECGConversion;
using ECGConversion.ECGDemographics;
using ECGConversion.ECGDiagnostic;
using ECGConversion.ECGGlobalMeasurements;
using ECGConversion.ECGSignals;
namespace ECGConversion.aECG
{
/// <summary>
/// Summary description for aECGFormat.
/// </summary>
public sealed class aECGFormat : IECGFormat, ISignal, IDemographic, IDiagnostic, IGlobalMeasurement
{
public System.Text.Encoding Encoding
{
get
{
try
{
return System.Text.Encoding.GetEncoding(_Config["Encoding"]);
}
catch {}
return null;
}
set
{
_Config["Encoding"] = value.BodyName;
}
}
public const string Name = "AnnotatedECG";
public aECGId Id = new aECGId();
public aECGCode Code = new aECGCode();
public string text = null;
public aECGTime EffectiveTime = new aECGTime();
public aECGCode ConfidentialityCode = new aECGCode("confidentialityCode", true);
public aECGCode ReasonCode = new aECGCode("reasonCode");
public aECGTimepointEvent TimepointEvent = new aECGTimepointEvent();
public aECGSite TestingSite = new aECGSite("testingSite");
public aECGControlVariableHolder[] ControlVariables = new aECGControlVariableHolder[128];
public aECGSubjectFindingComment SubjectFindingComment = new aECGSubjectFindingComment();
public aECGComponent Component = new aECGComponent();
public ArrayList UnknownElements = new ArrayList();
private string _OverreadingPhysician = null;
public aECGFormat()
{
string[] mustValue = {"Encoding"};
_Config = new ECGConfig(mustValue, null, new ECGConversion.ECGConfig.CheckConfigFunction(this._ConfigurationWorks));
_Config["Encoding"] = "UTF-8";
}
private bool _ConfigurationWorks()
{
try
{
System.Text.Encoding enc = System.Text.Encoding.GetEncoding(_Config["Encoding"]);
return enc != null;
}
catch {}
return false;
}
public int Read(XmlTextReader reader)
{
if (!CheckFormat(reader))
return 2;
Encoding = reader.Encoding;
switch (GetFileName(reader.GetAttribute("xsi:schemaLocation")))
{
case "PORI_MT020001.xsd":
try
{
XmlUrlResolver resolver = new XmlUrlResolver();
resolver.Credentials = System.Net.CredentialCache.DefaultCredentials;
MemoryStream ms = new MemoryStream(5 * 1024 * 1024);
XslTransform xslTrans = new XslTransform();
XPathDocument xslt = new XPathDocument(Assembly.GetExecutingAssembly().GetManifestResourceStream("ECGConversion.MegaCare.xslt"));
xslTrans.Load(xslt, resolver, this.GetType().Assembly.Evidence);
xslTrans.Transform(new System.Xml.XPath.XPathDocument(reader), null, ms, resolver);
ms.Seek(0, SeekOrigin.Begin);
XmlTextReader temp = new XmlTextReader(ms);
if (!CheckFormat(temp))
return 2;
reader = temp;
}
catch {}
break;
default:
break;
}
int ret = 0;
while (reader.Read())
{
if ((reader.NodeType == XmlNodeType.Comment)
|| (reader.NodeType == XmlNodeType.Whitespace))
continue;
if (String.Compare(reader.Name, Name) == 0)
{
if (reader.NodeType == XmlNodeType.EndElement)
break;
else
return 3;
}
ret = aECGElement.ReadOne(this, reader);
if (ret != 0)
break;
}
return (ret > 0) ? 3 + ret : ret;
}
public override int Read(Stream input, int offset)
{
XmlTextReader reader = null;
try
{
input.Seek(offset, SeekOrigin.Begin);
reader = new XmlTextReader(input);
return Read(reader);
}
catch {}
return 1;
}
public override int Read(string file, int offset)
{
FileStream stream = null;
try
{
stream = new FileStream(file, FileMode.Open);
return Read(stream, offset);
}
catch {}
finally
{
if (stream != null)
{
stream.Close();
stream = null;
}
}
return 1;
}
public override int Read(byte[] buffer, int offset)
{
System.IO.MemoryStream ms = null;
try
{
ms = new MemoryStream(buffer, offset, buffer.Length-offset, false);
return Read(ms, 0);
}
catch {}
finally
{
if (ms != null)
{
ms.Close();
ms = null;
}
}
return 2;
}
public int Write(XmlWriter writer)
{
if (!Works())
return 1;
writer.WriteStartDocument();
writer.WriteStartElement(Name);
writer.WriteAttributeString("xmlns", "urn:hl7-org:v3");
writer.WriteAttributeString("xmlns:voc", "urn:hl7-org:v3/voc");
writer.WriteAttributeString("xmlns:xsi", "http://www.w3.org/2001/XMLSchema-instance");
writer.WriteAttributeString("xsi:schemaLocation", "urn:hl7-org:v3 ../schema/PORT_MT020001.xsd");
writer.WriteAttributeString("type", "Observation");
int ret = aECGElement.WriteAll(this, writer);
if (ret != 0)
return (ret > 0) ? 1 + ret : ret;
writer.WriteEndElement();
writer.WriteEndDocument();
writer.Flush();
return 0;
}
public override int Write(string file)
{
FileStream output = null;
try
{
output = new FileStream(file, FileMode.Create);
return Write(output);
}
catch {}
finally
{
if (output != null)
{
output.Close();
output = null;
}
}
return 1;
}
public override int Write(Stream output)
{
XmlTextWriter writer = null;
try
{
writer = new XmlTextWriter(output, Encoding);
return Write(writer);
}
catch {}
return 1;
}
public override int Write(byte[] buffer, int offset)
{
System.IO.MemoryStream ms = null;
try
{
ms = new MemoryStream(buffer, offset, buffer.Length-offset, true);
return Write(ms);
}
catch {}
finally
{
if (ms != null)
{
ms.Close();
ms = null;
}
}
return 2;
}
private bool CheckFormat(XmlReader reader)
{
try
{
while (reader.Read())
if ((String.Compare(reader.Name, "AnnotatedECG") == 0)
&& (reader.NodeType == XmlNodeType.Element)
&& (string.Compare(reader.GetAttribute("type"), "Observation", true) == 0))
{
switch (GetFileName(reader.GetAttribute("xsi:schemaLocation")))
{
case "PORT_MT020001.xsd":
case "PORI_MT020001.xsd":
return true;
default:
break;
}
break;
}
}
catch
{
}
return false;
}
public override bool CheckFormat(Stream input, int offset)
{
if (!input.CanRead)
return false;
XmlTextReader reader = null;
try
{
input.Seek(offset, SeekOrigin.Begin);
reader = new XmlTextReader(input);
return CheckFormat(reader);
}
catch {}
return false;
}
public override bool CheckFormat(string file, int offset)
{
FileStream stream = null;
try
{
stream = new FileStream(file, FileMode.Open);
return CheckFormat(stream, offset);
}
catch {}
finally
{
if (stream != null)
{
stream.Close();
stream = null;
}
}
return false;
}
public override bool CheckFormat(byte[] buffer, int offset)
{
System.IO.MemoryStream ms = null;
try
{
ms = new MemoryStream(buffer, offset, buffer.Length-offset, false);
return CheckFormat(ms, 0);
}
catch {}
finally
{
if (ms != null)
{
ms.Close();
ms = null;
}
}
return false;
}
public override IDemographic Demographics
{
get
{
return this;
}
}
public override IDiagnostic Diagnostics
{
get
{
return this;
}
}
public override IGlobalMeasurement GlobalMeasurements
{
get
{
return this;
}
}
public override ISignal Signals
{
get
{
return this;
}
}
public override void Anonymous(byte type)
{
ECGTool.Anonymous(this, (char)type);
}
public override int getFileSize()
{
return -1;
}
public override bool Works()
{
return Id.Works()
&& Code.Works()
&& EffectiveTime.Works()
&& Component.Works();
}
public override void Empty()
{
aECGElement.EmptyAll(this);
}
public bool Add(aECGControlVariableHolder var)
{
for (int i=0;i < ControlVariables.Length;i++)
{
if (ControlVariables[i] == null)
{
ControlVariables[i] = var;
return true;
}
else if (ControlVariables[i].Code.Code == var.Code.Code)
{
ControlVariables[i] = var;
return true;
}
}
return false;
}
public aECGControlVariableHolder this[string varName]
{
get
{
foreach (aECGControlVariableHolder var in ControlVariables)
{
if (var == null)
return null;
else if ((var.Code != null)
&& (var.Code.Code == varName))
return var;
}
return null;
}
}
#region IDisposable Members
public override void Dispose()
{
base.Dispose();
Empty();
}
#endregion
#region ISignal Members
public int getSignals(out Signals signals)
{
signals = new Signals();
int err = getSignalsToObj(signals);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -