@@ -25,9 +25,9 @@ public class XmlWriterImpl : AutoContext<XmlWriterImpl>, IDisposable
2525 {
2626 private TextWriterWithSettings _internalTextWriter ;
2727 private XmlTextWriter _writer ;
28- private XmlWriterSettingsImpl _settings = ( XmlWriterSettingsImpl ) XmlWriterSettingsImpl . Constructor ( ) ;
28+ private XmlWriterSettingsImpl _settings = XmlWriterSettingsImpl . Constructor ( ) ;
2929 private int _depth ;
30- private Stack < Dictionary < string , string > > _nsmap = new Stack < Dictionary < string , string > > ( ) ;
30+ private readonly Stack < Dictionary < string , string > > _nsmap = new ( ) ;
3131 private StringBuilder _stringBuffer ;
3232
3333 private const string DEFAULT_INDENT_STRING = " " ;
@@ -51,6 +51,12 @@ private void ExitScope()
5151 -- _depth ;
5252 }
5353
54+ private void CheckIfOpen ( )
55+ {
56+ if ( _writer == null )
57+ throw NotOpenException ( ) ;
58+ }
59+
5460 #region Properties
5561
5662 [ ContextProperty ( "Отступ" , "Indent" ) ]
@@ -77,8 +83,9 @@ public XmlWriterSettingsImpl Settings
7783
7884 [ ContextMethod ( "ЗаписатьАтрибут" , "WriteAttribute" ) ]
7985 public void WriteAttribute ( string localName , string valueOrNamespace , string value = null )
80- {
81- if ( value == null )
86+ {
87+ CheckIfOpen ( ) ;
88+ if ( value == null )
8289 {
8390 _writer . WriteAttributeString ( localName , valueOrNamespace ) ;
8491 }
@@ -91,30 +98,35 @@ public void WriteAttribute(string localName, string valueOrNamespace, string val
9198 [ ContextMethod ( "ЗаписатьБезОбработки" , "WriteRaw" ) ]
9299 public void WriteRaw ( string data )
93100 {
101+ CheckIfOpen ( ) ;
94102 _writer . WriteRaw ( data ) ;
95103 }
96104
97105 [ ContextMethod ( "ЗаписатьИнструкциюОбработки" , "WriteProcessingInstruction" ) ]
98106 public void WriteProcessingInstruction ( string name , string text )
99107 {
108+ CheckIfOpen ( ) ;
100109 _writer . WriteProcessingInstruction ( name , text ) ;
101110 }
102111
103112 [ ContextMethod ( "ЗаписатьКомментарий" , "WriteComment" ) ]
104113 public void WriteComment ( string text )
105114 {
115+ CheckIfOpen ( ) ;
106116 _writer . WriteComment ( text ) ;
107117 }
108118
109119 [ ContextMethod ( "ЗаписатьКонецАтрибута" , "WriteEndAttribute" ) ]
110120 public void WriteEndAttribute ( )
111121 {
122+ CheckIfOpen ( ) ;
112123 _writer . WriteEndAttribute ( ) ;
113124 }
114125
115126 [ ContextMethod ( "ЗаписатьКонецЭлемента" , "WriteEndElement" ) ]
116127 public void WriteEndElement ( )
117128 {
129+ CheckIfOpen ( ) ;
118130 _internalTextWriter . TrimEndSlashes = true ;
119131 _writer . WriteEndElement ( ) ;
120132 _internalTextWriter . TrimEndSlashes = false ;
@@ -124,7 +136,8 @@ public void WriteEndElement()
124136 [ ContextMethod ( "ЗаписатьНачалоАтрибута" , "WriteStartAttribute" ) ]
125137 public void WriteStartAttribute ( string name , string ns = null )
126138 {
127- if ( ns == null )
139+ CheckIfOpen ( ) ;
140+ if ( ns == null )
128141 {
129142 _writer . WriteStartAttribute ( name ) ;
130143 }
@@ -138,6 +151,7 @@ public void WriteStartAttribute(string name, string ns = null)
138151 [ ContextMethod ( "ЗаписатьНачалоЭлемента" , "WriteStartElement" ) ]
139152 public void WriteStartElement ( string name , string ns = null )
140153 {
154+ CheckIfOpen ( ) ;
141155 if ( ns == null )
142156 {
143157 _writer . WriteStartElement ( name ) ;
@@ -152,37 +166,43 @@ public void WriteStartElement(string name, string ns = null)
152166 [ ContextMethod ( "ЗаписатьОбъявлениеXML" , "WriteXMLDeclaration" ) ]
153167 public void WriteXMLDeclaration ( )
154168 {
169+ CheckIfOpen ( ) ;
155170 _writer . WriteStartDocument ( ) ;
156171 }
157172
158173 [ ContextMethod ( "ЗаписатьСекциюCDATA" , "WriteCDATASection" ) ]
159174 public void WriteCDATASection ( string data )
160175 {
176+ CheckIfOpen ( ) ;
161177 _writer . WriteCData ( data ) ;
162178 }
163179
164180 [ ContextMethod ( "ЗаписатьСоответствиеПространстваИмен" , "WriteNamespaceMapping" ) ]
165181 public void WriteNamespaceMapping ( string prefix , string uri )
166182 {
183+ CheckIfOpen ( ) ;
167184 _writer . WriteAttributeString ( "xmlns" , prefix , null , uri ) ;
168185 _nsmap . Peek ( ) [ prefix ] = uri ;
169186 }
170187
171188 [ ContextMethod ( "ЗаписатьСсылкуНаСущность" , "WriteEntityReference" ) ]
172189 public void WriteEntityReference ( string name )
173190 {
191+ CheckIfOpen ( ) ;
174192 _writer . WriteEntityRef ( name ) ;
175193 }
176194
177195 [ ContextMethod ( "ЗаписатьТекст" , "WriteText" ) ]
178196 public void WriteText ( string text )
179197 {
198+ CheckIfOpen ( ) ;
180199 _writer . WriteString ( text ) ;
181200 }
182201
183202 [ ContextMethod ( "ЗаписатьТекущий" , "WriteCurrent" ) ]
184203 public void WriteCurrent ( XmlReaderImpl reader )
185204 {
205+ CheckIfOpen ( ) ;
186206 var nodeType = reader . NodeType . UnderlyingValue ;
187207 switch ( nodeType )
188208 {
@@ -225,7 +245,8 @@ public void WriteCurrent(XmlReaderImpl reader)
225245 case XmlNodeType . Document :
226246 case XmlNodeType . DocumentFragment :
227247 case XmlNodeType . Notation :
228- throw new RuntimeException ( new Localization . BilingualString ( $ "Копирование узла { nodeType } не поддерживается") ) ;
248+ throw CopyingNotSupportedException ( nodeType ) ;
249+
229250 default :
230251 break ;
231252 }
@@ -292,7 +313,8 @@ private static (string prefix, string localName) splitName(string nameOrLocalNam
292313 [ ContextMethod ( "ЗаписатьТипДокумента" , "WriteDocumentType" ) ]
293314 public void WriteDocumentType ( string name , string varArg2 , string varArg3 = null , string varArg4 = null )
294315 {
295- if ( varArg4 != null )
316+ CheckIfOpen ( ) ;
317+ if ( varArg4 != null )
296318 {
297319 _writer . WriteDocType ( name , varArg2 , varArg3 , varArg4 ) ;
298320 }
@@ -309,6 +331,7 @@ public void WriteDocumentType(string name, string varArg2, string varArg3 = null
309331 [ ContextMethod ( "НайтиПрефикс" , "LookupPrefix" ) ]
310332 public IValue LookupPrefix ( string uri )
311333 {
334+ CheckIfOpen ( ) ;
312335 string prefix = _writer . LookupPrefix ( uri ) ;
313336 if ( prefix == null )
314337 return ValueFactory . Create ( ) ;
@@ -318,26 +341,24 @@ public IValue LookupPrefix(string uri)
318341 [ ContextMethod ( "Закрыть" , "Close" ) ]
319342 public IValue Close ( )
320343 {
321- if ( IsOpenForString ( ) )
344+ if ( _writer == null )
345+ return ValueFactory . Create ( String . Empty ) ;
346+
347+ _writer . Flush ( ) ;
348+ _writer . Close ( ) ;
349+ Dispose ( ) ;
350+
351+ if ( IsOpenForString ( ) )
322352 {
323- _writer . Flush ( ) ;
324- _writer . Close ( ) ;
325-
326- Dispose ( ) ;
327-
328353 var result = _stringBuffer . ToString ( ) ;
329354 _stringBuffer = null ;
355+
330356 return ValueFactory . Create ( result ) ;
331357 }
332358 else
333359 {
334- _writer . Flush ( ) ;
335- _writer . Close ( ) ;
336- Dispose ( ) ;
337-
338- return ValueFactory . Create ( ) ;
360+ return ValueFactory . Create ( String . Empty ) ;
339361 }
340-
341362 }
342363
343364 private void ApplySettings ( BslValue encodingOrSettings )
@@ -477,9 +498,7 @@ protected override void Dispose(bool disposing)
477498
478499 public void Dispose ( )
479500 {
480- if ( _writer != null )
481- _writer . Close ( ) ;
482-
501+ _writer ? . Close ( ) ;
483502 _writer = null ;
484503 }
485504
@@ -490,5 +509,19 @@ public static XmlWriterImpl Create()
490509 }
491510
492511 public XmlWriter GetNativeWriter ( ) => _writer ;
512+
513+ public static RuntimeException NotOpenException ( )
514+ {
515+ return new RuntimeException
516+ ( "Приемник данных XML не открыт" ,
517+ "XML data target is not opened" ) ;
518+ }
519+ public static RuntimeException CopyingNotSupportedException ( XmlNodeType nodeType )
520+ {
521+ return new RuntimeException
522+ ( $ "Копирование узла типа { nodeType } не поддерживается",
523+ $ "Copying a node of type { nodeType } is not supported") ;
524+ }
525+
493526 }
494527}
0 commit comments