📄 objostrxml.cpp
字号:
void CObjectOStreamXml::WriteNull(void){ OpenTagEndBack(); SelfCloseTagEnd();}void CObjectOStreamXml::WriteAnyContentObject(const CAnyContentObject& obj){ if (obj.GetName().empty()) { return; } string ns_name(obj.GetNamespaceName()); bool needNs = x_BeginNamespace(ns_name,obj.GetNamespacePrefix()); OpenTag(obj.GetName()); if (m_UseSchemaRef) { OpenTagEndBack(); if (needNs) { m_Output.PutEol(); m_Output.PutString(" xmlns"); if (!m_CurrNsPrefix.empty()) { m_Output.PutChar(':'); m_Output.PutString(m_CurrNsPrefix); } m_Output.PutString("=\""); m_Output.PutString(ns_name); m_Output.PutChar('\"'); } const vector<CSerialAttribInfoItem>& attlist = obj.GetAttributes(); if (!attlist.empty()) { m_Attlist = true; vector<CSerialAttribInfoItem>::const_iterator it; for ( it = attlist.begin(); it != attlist.end(); ++it) { string ns(it->GetNamespaceName()); string ns_prefix; if (x_BeginNamespace(ns,kEmptyStr)) { m_Output.PutEol(); m_Output.PutString(" xmlns"); ns_prefix = m_NsNameToPrefix[ns]; if (!ns_prefix.empty()) { m_Output.PutChar(':'); m_Output.PutString(ns_prefix); } m_Output.PutString("=\""); m_Output.PutString(ns); m_Output.PutChar('\"'); } ns_prefix = m_NsNameToPrefix[ns]; m_Output.PutEol(); m_Output.PutString(" "); if (!ns_prefix.empty()) { m_Output.PutString(ns_prefix); m_Output.PutChar(':'); } m_Output.PutString(it->GetName()); m_Output.PutString("=\""); m_Output.PutString(it->GetValue()); m_Output.PutChar('\"'); x_EndNamespace(ns); } m_Attlist = false; } OpenTagEnd(); }// value// no verification on write! const string& value = obj.GetValue(); bool was_open = true; for (string::const_iterator is=value.begin(); is != value.end(); ++is) { if (*is == '/' && *(is+1) == '>') { m_Output.DecIndentLevel(); was_open = false; } if (*is == '<') { if (*(is+1) == '/') { m_Output.DecIndentLevel(); if (!was_open) { m_Output.PutEol(); } was_open = false; } else { m_Output.PutEol(); m_Output.IncIndentLevel(); was_open = true; } } m_Output.PutChar(*is); if (*is == '<') { if (*(is+1) == '/') { m_Output.PutChar(*(++is)); } if (m_UseSchemaRef && !m_CurrNsPrefix.empty()) { m_Output.PutString(m_CurrNsPrefix); m_Output.PutChar(':'); } } }// close tag if (!was_open) { m_Output.PutEol(false); m_EndTag = true; } CloseTag(obj.GetName()); x_EndNamespace(ns_name);}void CObjectOStreamXml::CopyAnyContentObject(CObjectIStream& in){ NCBI_THROW(CSerialException,eNotImplemented,"not yet");}void CObjectOStreamXml::WriteCString(const char* str){ if ( str == 0 ) { OpenTagEndBack(); SelfCloseTagEnd(); } else { while ( *str ) { WriteEscapedChar(*str++); } }}void CObjectOStreamXml::WriteString(const string& str, EStringType type){ EEncoding enc = m_Encoding; if (type == eStringTypeUTF8) { if (m_Encoding == eEncoding_UTF8 || m_Encoding == eEncoding_Unknown) { m_Encoding = eEncoding_ISO8859_1; } else { string tmp = (static_cast<const CStringUTF8&>(str)).AsAscii(); WriteString( tmp, eStringTypeVisible); return; } } for ( string::const_iterator i = str.begin(); i != str.end(); ++i ) { WriteEscapedChar(*i); } m_Encoding = enc;}void CObjectOStreamXml::WriteStringStore(const string& str){ for ( string::const_iterator i = str.begin(); i != str.end(); ++i ) { WriteEscapedChar(*i); }}void CObjectOStreamXml::CopyString(CObjectIStream& in){ string str; in.ReadStd(str); for ( string::const_iterator i = str.begin(); i != str.end(); ++i ) { WriteEscapedChar(*i); }}void CObjectOStreamXml::CopyStringStore(CObjectIStream& in){ string str; in.ReadStringStore(str); for ( string::const_iterator i = str.begin(); i != str.end(); ++i ) { WriteEscapedChar(*i); }}void CObjectOStreamXml::WriteNullPointer(void){ OpenTagEndBack(); SelfCloseTagEnd();}void CObjectOStreamXml::WriteObjectReference(TObjectIndex index){ m_Output.PutString("<object index="); if ( sizeof(TObjectIndex) == sizeof(Int4) ) m_Output.PutInt4(Int4(index)); else if ( sizeof(TObjectIndex) == sizeof(Int8) ) m_Output.PutInt8(index); else ThrowError(fIllegalCall, "invalid size of TObjectIndex" "must be either sizeof(Int4) or sizeof(Int8)"); m_Output.PutString("/>"); m_EndTag = true;}void CObjectOStreamXml::WriteTag(const string& name){ if (!m_Attlist && !m_CurrNsPrefix.empty()) { m_Output.PutString(m_CurrNsPrefix); m_Output.PutChar(':'); } m_Output.PutString(name);}void CObjectOStreamXml::OpenTagStart(void){ if (m_Attlist) { if ( m_LastTagAction == eTagOpen ) { m_Output.PutChar(' '); m_LastTagAction = eAttlistTag; } } else { if (!m_EndTag) m_Output.PutEol(false); m_Output.PutIndent(); m_Output.PutChar('<'); m_LastTagAction = eTagOpen; } m_EndTag = false;}void CObjectOStreamXml::OpenTagEnd(void){ if (m_Attlist) { if ( m_LastTagAction == eAttlistTag ) { m_Output.PutString("=\""); } } else { if ( m_LastTagAction == eTagOpen ) { m_Output.PutChar('>'); m_Output.IncIndentLevel(); m_LastTagAction = eTagClose; } }}void CObjectOStreamXml::OpenTagEndBack(void){ _ASSERT(m_LastTagAction == eTagClose); m_LastTagAction = eTagOpen; m_Output.BackChar('>'); m_Output.DecIndentLevel();}void CObjectOStreamXml::SelfCloseTagEnd(void){ _ASSERT(m_LastTagAction == eTagOpen); m_Output.PutString("/>"); m_Output.PutEol(false); m_LastTagAction = eTagSelfClosed; m_EndTag = true;}void CObjectOStreamXml::EolIfEmptyTag(void){ _ASSERT(m_LastTagAction != eTagSelfClosed); if ( m_LastTagAction == eTagOpen ) { m_Output.PutEol(false); m_LastTagAction = eTagClose; }}void CObjectOStreamXml::CloseTagStart(void){ _ASSERT(m_LastTagAction != eTagSelfClosed); m_Output.DecIndentLevel(); if (m_EndTag) m_Output.PutIndent(); m_Output.PutString("</"); m_LastTagAction = eTagOpen;}void CObjectOStreamXml::CloseTagEnd(void){ m_Output.PutChar('>'); m_Output.PutEol(false); m_LastTagAction = eTagClose; m_EndTag = true;}void CObjectOStreamXml::PrintTagName(size_t level){ const TFrame& frame = FetchFrameFromTop(level); switch ( frame.GetFrameType() ) { case TFrame::eFrameNamed: case TFrame::eFrameArray: case TFrame::eFrameClass: case TFrame::eFrameChoice: { _ASSERT(frame.GetTypeInfo()); const string& name = frame.GetTypeInfo()->GetName(); if ( !name.empty() ) { WriteTag(name);#if defined(NCBI_SERIAL_IO_TRACE) TraceTag(name);#endif } else { PrintTagName(level + 1); } return; } case TFrame::eFrameClassMember: case TFrame::eFrameChoiceVariant: { bool attlist = m_Attlist; if (!x_IsStdXml()) { PrintTagName(level + 1); m_Output.PutChar('_'); m_Attlist = true; } WriteTag(frame.GetMemberId().GetName()); m_Attlist = attlist;#if defined(NCBI_SERIAL_IO_TRACE) TraceTag(frame.GetMemberId().GetName());#endif return; } case TFrame::eFrameArrayElement: { PrintTagName(level + 1); if (!x_IsStdXml()) { m_Output.PutString("_E"); } return; } default: break; } ThrowError(fIllegalCall, "illegal frame type");}void CObjectOStreamXml::WriteOtherBegin(TTypeInfo typeInfo){ OpenTag(typeInfo);}void CObjectOStreamXml::WriteOtherEnd(TTypeInfo typeInfo){ CloseTag(typeInfo);}void CObjectOStreamXml::WriteOther(TConstObjectPtr object, TTypeInfo typeInfo){ OpenTag(typeInfo); WriteObject(object, typeInfo); CloseTag(typeInfo);}void CObjectOStreamXml::BeginContainer(const CContainerTypeInfo* containerType){ if (!x_IsStdXml()) { OpenTagIfNamed(containerType); }}void CObjectOStreamXml::EndContainer(void){ if (!x_IsStdXml()) { CloseTagIfNamed(TopFrame().GetTypeInfo()); }}bool CObjectOStreamXml::WillHaveName(TTypeInfo elementType){ while ( elementType->GetName().empty() ) { if ( elementType->GetTypeFamily() != eTypeFamilyPointer ) return false; elementType = CTypeConverter<CPointerTypeInfo>::SafeCast(elementType)->GetPointedType(); } // found named type return true;}void CObjectOStreamXml::BeginContainerElement(TTypeInfo elementType){ if ( !WillHaveName(elementType) ) { BeginArrayElement(elementType); }}void CObjectOStreamXml::EndContainerElement(void){ if ( !WillHaveName(TopFrame().GetTypeInfo()) ) { EndArrayElement(); }}#ifdef VIRTUAL_MID_LEVEL_IOvoid CObjectOStreamXml::WriteContainer(const CContainerTypeInfo* cType, TConstObjectPtr containerPtr){ if ( !cType->GetName().empty() ) { BEGIN_OBJECT_FRAME2(eFrameArray, cType); OpenTag(cType); WriteContainerContents(cType, containerPtr); EolIfEmptyTag(); CloseTag(cType); END_OBJECT_FRAME(); } else { WriteContainerContents(cType, containerPtr);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -