Skip to content

Commit e74d6a8

Browse files
committed
update Library.XmlDom.XMLWriter peachpiecompiler#1142
1 parent 0fb482e commit e74d6a8

File tree

1 file changed

+32
-22
lines changed

1 file changed

+32
-22
lines changed

src/Peachpie.Library.XmlDom/XmlWriter.cs

+32-22
Original file line numberDiff line numberDiff line change
@@ -16,15 +16,17 @@ namespace Peachpie.Library.XmlDom
1616
[PhpType(PhpTypeAttribute.InheritName), PhpExtension("xmlwriter")]
1717
public class XMLWriter : IDisposable
1818
{
19+
1920
#region Constants
2021

2122
private protected const string DefaultXmlVersion = "1.0";
2223

2324
#endregion
2425

2526
#region Fields and properties
27+
XmlTextWriter _writer;
2628

27-
System.Xml.XmlWriter _writer;
29+
//wjw - System.Xml.XmlWriter _writer;
2830
MemoryStream _memoryStream;
2931
PhpStream _uriPhpStream;
3032

@@ -212,7 +214,7 @@ public bool endDtd()
212214
_state.Pop();
213215

214216
// Closes dtd section.
215-
string end = _writer.Settings.Indent ? _writer.Settings.NewLineChars : "";
217+
string end = _writer.Formatting == Formatting.Indented ? DefaultSettings.NewLineChars : ""; // string end = _writer.Settings.Indent ? _writer.Settings.NewLineChars : "";
216218
end += _dtdStart ? ">" : "]>";
217219
_dtdStart = false;
218220

@@ -292,7 +294,9 @@ public bool openMemory(Context ctx)
292294
{
293295
Clear();
294296
_memoryStream = new MemoryStream();
295-
_writer = System.Xml.XmlWriter.Create(_memoryStream, DefaultSettings);
297+
_writer = new XmlTextWriter(_memoryStream, DefaultSettings.Encoding); //_writer = System.Xml.XmlWriter.Create(_memoryStream, DefaultSettings);
298+
_writer.Formatting = DefaultSettings.Indent ? Formatting.Indented : Formatting.None;
299+
296300
ctx.RegisterDisposable(this);
297301
return true;
298302
}
@@ -309,7 +313,7 @@ public bool openUri(Context ctx, string uri)
309313

310314
try
311315
{
312-
_writer = System.Xml.XmlWriter.Create(_uriPhpStream.RawStream, DefaultSettings);
316+
_writer = new XmlTextWriter(_uriPhpStream.RawStream, DefaultSettings.Encoding); //_writer = System.Xml.XmlWriter.Create(_uriPhpStream.RawStream, DefaultSettings);
313317
ctx.RegisterDisposable(this);
314318
return true;
315319
}
@@ -340,13 +344,13 @@ public bool setIndentString(string indentString)
340344
return false;
341345

342346
// The settings is read-only, but we can create a new xmlwriter if the current xmlwriter haven't written anything yet.
343-
var settings = _writer.Settings.Clone();
347+
var settings =DefaultSettings.Clone(); // var settings = _writer.Settings.Clone();
344348
settings.IndentChars = indentString;
345349

346350
if (_uriPhpStream == null)
347-
_writer = XmlWriter.Create(_memoryStream, settings);
351+
_writer = new XmlTextWriter(_memoryStream, settings.Encoding); //_writer = XmlWriter.Create(_memoryStream, settings);
348352
else
349-
_writer = XmlWriter.Create(_uriPhpStream.RawStream, settings);
353+
_writer = new XmlTextWriter(_uriPhpStream.RawStream, settings.Encoding); //_writer = XmlWriter.Create(_uriPhpStream.RawStream, settings);
350354

351355
return true;
352356
}
@@ -357,13 +361,13 @@ public bool setIndent(bool indent)
357361
return false;
358362

359363
// The settings is read-only, but we can create a new xmlwriter if the current xmlwriter haven't written anything yet.
360-
var settings = _writer.Settings.Clone();
364+
var settings = DefaultSettings.Clone(); // var settings = _writer.Settings.Clone();
361365
settings.Indent = indent;
362366

363367
if (_uriPhpStream == null)
364-
_writer = XmlWriter.Create(_memoryStream, settings);
368+
_writer = new XmlTextWriter(_memoryStream, settings.Encoding); //_writer = XmlWriter.Create(_memoryStream, settings);
365369
else
366-
_writer = XmlWriter.Create(_uriPhpStream.RawStream, settings);
370+
_writer = new XmlTextWriter(_uriPhpStream.RawStream, settings.Encoding); //_writer = XmlWriter.Create(_uriPhpStream.RawStream, settings);
367371

368372
return true;
369373
}
@@ -422,16 +426,16 @@ public bool startDocument(string version = DefaultXmlVersion, string encoding =
422426
if (string.IsNullOrEmpty(standalone))
423427
{
424428
bool res = CheckedCall(() => _writer.WriteStartDocument());
425-
if (!_writer.Settings.Indent) // Php writes a new line character after prolog.
426-
res &= CheckedCall(() => _writer.WriteRaw(_writer.Settings.NewLineChars));
429+
if (_writer.Formatting==Formatting.None) // if (!_writer.Settings.Indent) // Php writes a new line character after prolog.
430+
res &= CheckedCall(() => _writer.WriteRaw(DefaultSettings.NewLineChars)); //res &= CheckedCall(() => _writer.WriteRaw(_writer.Settings.NewLineChars));
427431

428432
return res;
429433
}
430434
else
431435
{
432436
bool res = CheckedCall(() => _writer.WriteStartDocument(standalone == "yes"));
433-
if (!_writer.Settings.Indent) // Php writes a new line character after prolog.
434-
res &= CheckedCall(() => _writer.WriteRaw(_writer.Settings.NewLineChars));
437+
if (_writer.Formatting == Formatting.None) // if (!_writer.Settings.Indent) // Php writes a new line character after prolog.
438+
res &= CheckedCall(() => _writer.WriteRaw(DefaultSettings.NewLineChars)); // res &= CheckedCall(() => _writer.WriteRaw(_writer.Settings.NewLineChars));
435439

436440
return res;
437441
}
@@ -476,7 +480,8 @@ public bool startDtdEntity(string name, bool isparam)
476480
public bool startDtd(string qualifiedName, string publicId = null, string systemId = null)
477481
{
478482
if (_state.Count != 0 || // DTD can be only placed in default section and prolog.
479-
(_writer.Settings.ConformanceLevel == ConformanceLevel.Document && _writer.WriteState != WriteState.Prolog && _writer.WriteState != WriteState.Start))
483+
//(_writer.Settings.ConformanceLevel == ConformanceLevel.Document && _writer.WriteState != WriteState.Prolog && _writer.WriteState != WriteState.Start))
484+
(DefaultSettings.ConformanceLevel == ConformanceLevel.Document && _writer.WriteState != WriteState.Prolog && _writer.WriteState != WriteState.Start))
480485
{
481486
PhpException.Throw(PhpError.Warning, Resources.XmlWritterDtdInProlog);
482487
return false;
@@ -491,9 +496,11 @@ public bool startDtd(string qualifiedName, string publicId = null, string system
491496
// Makes a doctype
492497
string doctype = $"<!DOCTYPE {qualifiedName}";
493498
if (!String.IsNullOrEmpty(publicId))
494-
doctype += _writer.Settings.Indent ? $"{_writer.Settings.NewLineChars}PUBLIC \"{publicId}\"" : $" PUBLIC \"{publicId}\"";
499+
//doctype += _writer.Settings.Indent ? $"{_writer.Settings.NewLineChars}PUBLIC \"{publicId}\"" : $" PUBLIC \"{publicId}\"";
500+
doctype += _writer.Formatting==Formatting.Indented ? $"{DefaultSettings.NewLineChars}PUBLIC \"{publicId}\"" : $" PUBLIC \"{publicId}\"";
495501
if (!String.IsNullOrEmpty(systemId))
496-
doctype += _writer.Settings.Indent ? $"{_writer.Settings.NewLineChars}SYSTEM \"{systemId}\"" : $" SYSTEM \"{systemId}\"";
502+
//doctype += _writer.Settings.Indent ? $"{_writer.Settings.NewLineChars}SYSTEM \"{systemId}\"" : $" SYSTEM \"{systemId}\"";
503+
doctype += _writer.Formatting == Formatting.Indented ? $"{DefaultSettings.NewLineChars}SYSTEM \"{systemId}\"" : $" SYSTEM \"{systemId}\"";
497504

498505
CheckDtdStartHelper();
499506
_state.Push(State.DTD);
@@ -662,7 +669,8 @@ public bool writeDtdEntity(string name, string content, bool pe = false)
662669
public bool writeDtd(string name, string publicId = null, string systemId = null, string subset = null)
663670
{
664671
if (_state.Count != 0 ||
665-
(_writer.Settings.ConformanceLevel == ConformanceLevel.Document && _writer.WriteState != WriteState.Prolog && _writer.WriteState != WriteState.Start))
672+
//(_writer.Settings.ConformanceLevel == ConformanceLevel.Document && _writer.WriteState != WriteState.Prolog && _writer.WriteState != WriteState.Start))
673+
(DefaultSettings.ConformanceLevel == ConformanceLevel.Document && _writer.WriteState != WriteState.Prolog && _writer.WriteState != WriteState.Start))
666674
{
667675
PhpException.Throw(PhpError.Warning, Resources.XmlWritterDtdInProlog);
668676
return false;
@@ -677,9 +685,11 @@ public bool writeDtd(string name, string publicId = null, string systemId = null
677685
// Makes doctype
678686
string doctype = $"<!DOCTYPE {name}";
679687
if (!String.IsNullOrEmpty(publicId))
680-
doctype += _writer.Settings.Indent ? $"{_writer.Settings.NewLineChars}PUBLIC \"{publicId}\"" : $" PUBLIC \"{publicId}\"";
688+
//doctype += _writer.Settings.Indent ? $"{_writer.Settings.NewLineChars}PUBLIC \"{publicId}\"" : $" PUBLIC \"{publicId}\"";
689+
doctype += _writer.Formatting==Formatting.Indented ? $"{DefaultSettings.NewLineChars}PUBLIC \"{publicId}\"" : $" PUBLIC \"{publicId}\"";
681690
if (!String.IsNullOrEmpty(systemId))
682-
doctype += _writer.Settings.Indent ? $"{_writer.Settings.NewLineChars} \"{systemId}\"" : $" \"{systemId}\"";
691+
//doctype += _writer.Settings.Indent ? $"{_writer.Settings.NewLineChars} \"{systemId}\"" : $" \"{systemId}\"";
692+
doctype += _writer.Formatting == Formatting.Indented ? $"{DefaultSettings.NewLineChars} \"{systemId}\"" : $" \"{systemId}\"";
683693
if (!String.IsNullOrEmpty(subset))
684694
doctype += $" [{subset}]";
685695
doctype += ">";
@@ -740,9 +750,9 @@ private void CheckDtdStartHelper()
740750
_dtdStart = false;
741751
}
742752

743-
if (_writer.Settings.Indent)
753+
if (_writer.Formatting==Formatting.Indented) //if (_writer.Settings.Indent)
744754
{
745-
_writer.WriteRaw(_writer.Settings.NewLineChars);
755+
_writer.WriteRaw(DefaultSettings.NewLineChars); //_writer.WriteRaw(_writer.Settings.NewLineChars);
746756

747757
if (_state.Count != 0 && _state.Peek() == State.DTD)
748758
_writer.WriteRaw(" ");

0 commit comments

Comments
 (0)