/// <summary> /// Parses input markup, and executes configured cleanup and repair operations. /// </summary> /// <param name="logStream">A stream to which errors encountered during the CleanAndRepair operation will be written to.</param> public void CleanAndRepair(Stream logStream) { //Config Error EncodingType tempOutEnc = this.OutputCharacterEncoding; this.OutputCharacterEncoding = EncodingType.Utf8; OutputSink sink = new OutputSink(logStream); PInvoke.tidySetErrorSink(this.handle, ref sink.TidyOutputSink); if (fromString) { EncodingType tempEnc = this.InputCharacterEncoding; this.InputCharacterEncoding = EncodingType.Utf8; PInvoke.tidyParseString(this.handle, this.htmlString); this.InputCharacterEncoding = tempEnc; } else { InputSource input = new InputSource(this.stream); PInvoke.tidyParseSource(this.handle, ref input.TidyInputSource); } PInvoke.tidyCleanAndRepair(this.handle); this.OutputCharacterEncoding = tempOutEnc; cleaned = true; }
/// <summary> /// Saves the processed markup to the supplied stream. /// </summary> /// <param name="stream">A <see cref="System.IO.Stream"/> to write the markup to.</param> public void Save(Stream stream) { if (!cleaned) throw new InvalidOperationException("CleanAndRepair() must be called before Save()."); EncodingType tempEnc = this.OutputCharacterEncoding; if (fromString) this.OutputCharacterEncoding = EncodingType.Utf8; OutputSink sink = new OutputSink(stream); PInvoke.tidySaveSink(this.handle, ref sink.TidyOutputSink); if (fromString) this.OutputCharacterEncoding = tempEnc; }
/// <summary> /// Disposes of all unmanaged resources. /// </summary> /// <param name="disposing">Indicates whether the the document is already being disposed of.</param> protected virtual void Dispose(bool disposing) { if (!this.disposed) { if (disposing) { if (this.stream != null) this.stream.Dispose(); if (this.errorSink != null) this.errorSink.Dispose(); PInvoke.tidyRelease(this.handle); } this.handle = IntPtr.Zero; this.stream = null; this.errorSink = null; this.disposed = true; } }
/// <summary> /// Set error sink to given writable stream. /// </summary> /// <param name="stream"> /// A writable <see cref="Stream"/> /// </param>. /// <returns> /// A <see cref="System.Boolean"/>; true when error sink is successful set. /// </returns> public bool SetErrorSink(Stream stream) { if (this.errorSink != null) this.errorSink.Dispose(); this.errorSink = null; if (stream == null) return false; this.errorSink = new OutputSink(stream); return PInvoke.tidySetErrorSink(this.handle, ref this.errorSink.TidyOutputSink) > 0; }
/// <summary> /// Parses input markup, and executes configured cleanup and repair operations. /// </summary> /// <param name="logStream">A stream to which errors encountered during the CleanAndRepair operation will be written to.</param> public void CleanAndRepair(Stream logStream) { //Config Error EncodingType tempOutEnc = this.OutputCharacterEncoding; this.OutputCharacterEncoding = EncodingType.Utf8; OutputSink sink = new OutputSink(logStream); PInvoke.tidySetErrorSink(this.handle, ref sink.TidyOutputSink); if (fromString) { using (var memStream = new MemoryStream(Encoding.UTF8.GetBytes(htmlString))) { InputSource input = new InputSource(memStream); PInvoke.tidyParseSource(handle, ref input.TidyInputSource); } } else { InputSource input = new InputSource(this.stream); PInvoke.tidyParseSource(this.handle, ref input.TidyInputSource); } PInvoke.tidyCleanAndRepair(this.handle); this.OutputCharacterEncoding = tempOutEnc; cleaned = true; }