public object schemaValidateSource(string schemaString) { XmlSchema schema; try { schema = XmlSchema.Read(new System.IO.StringReader(schemaString), null); } catch (XmlException e) { PhpLibXml.IssueXmlError(new PhpLibXml.XmlError(PhpLibXml.LIBXML_ERR_WARNING, 0, 0, 0, e.Message, null)); return(false); } XmlDocument.Schemas.Add(schema); try { XmlDocument.Validate(null); } catch (XmlException) { return(false); } finally { XmlDocument.Schemas.Remove(schema); } return(true); }
public static object load([This] DOMDocument instance, string fileName, [Optional] int options) { // this method can be called both statically and via an instance bool static_call; if (instance == null) { static_call = true; instance = new DOMDocument(); } else { static_call = false; } instance._isHtmlDocument = false; using (PhpStream stream = PhpStream.Open(fileName, "rt")) { if (stream == null) { return(false); } try { if (instance._validateOnParse) { // create a validating XML reader XmlReaderSettings settings = new XmlReaderSettings(); #pragma warning disable 618 settings.ValidationType = ValidationType.Auto; #pragma warning restore 618 instance.XmlDocument.Load(XmlReader.Create(stream.RawStream, settings)); } else { instance.XmlDocument.Load(stream.RawStream); } } catch (XmlException e) { PhpLibXml.IssueXmlError(new PhpLibXml.XmlError(PhpLibXml.LIBXML_ERR_ERROR, 0, 0, 0, e.Message, fileName)); return(false); } catch (IOException e) { PhpLibXml.IssueXmlError(new PhpLibXml.XmlError(PhpLibXml.LIBXML_ERR_ERROR, 0, 0, 0, e.Message, fileName)); return(false); } } return(static_call ? instance : (object)true); }
/// <summary> /// Processes HTML errors, if any. /// </summary> /// <param name="htmlDoc"><see cref="HtmlAgilityPack.HtmlDocument"/> instance to process errors from.</param> /// <param name="filename">HTML file name or <c>null</c> if HTML has been loaded from a string.</param> private void CheckHtmlErrors(HtmlAgilityPack.HtmlDocument /*!*/ htmlDoc, string filename) { Debug.Assert(htmlDoc != null); foreach (var error in htmlDoc.ParseErrors) { switch (error.Code) { case HtmlAgilityPack.HtmlParseErrorCode.EndTagNotRequired: case HtmlAgilityPack.HtmlParseErrorCode.TagNotOpened: break; default: PhpLibXml.IssueXmlError(new PhpLibXml.XmlError(PhpLibXml.LIBXML_ERR_ERROR, 0, error.Line, error.LinePosition, "(" + error.Code.ToString() + ")" + error.Reason, filename)); break; } } }
public object schemaValidate(string schemaFile) { XmlSchema schema; using (PhpStream stream = PhpStream.Open(schemaFile, "rt")) { if (stream == null) { return(false); } try { schema = XmlSchema.Read(stream.RawStream, null); } catch (XmlException e) { PhpLibXml.IssueXmlError(new PhpLibXml.XmlError(PhpLibXml.LIBXML_ERR_WARNING, 0, 0, 0, e.Message, schemaFile)); return(false); } catch (IOException e) { PhpLibXml.IssueXmlError(new PhpLibXml.XmlError(PhpLibXml.LIBXML_ERR_ERROR, 0, 0, 0, e.Message, schemaFile)); return(false); } } XmlDocument.Schemas.Add(schema); try { XmlDocument.Validate(null); } catch (XmlException) { return(false); } finally { XmlDocument.Schemas.Remove(schema); } return(true); }
public object save(string fileName, [Optional] int options) { using (PhpStream stream = PhpStream.Open(fileName, "wt")) { if (stream == null) { return(false); } try { // direct stream write indents if (_formatOutput) { XmlDocument.Save(stream.RawStream); } else { Encoding encoding = XmlDom.GetNodeEncoding(XmlNode); using (XmlTextWriter writer = new XmlTextWriter(stream.RawStream, encoding)) { XmlDocument.Save(writer); } } } catch (XmlException e) { PhpLibXml.IssueXmlError(new PhpLibXml.XmlError(PhpLibXml.LIBXML_ERR_ERROR, 0, 0, 0, e.Message, fileName)); return(null); } catch (IOException e) { PhpLibXml.IssueXmlError(new PhpLibXml.XmlError(PhpLibXml.LIBXML_ERR_ERROR, 0, 0, 0, e.Message, fileName)); return(false); } // TODO: return(stream.RawStream.CanSeek ? stream.RawStream.Position : 1); } }
/// <summary> /// Loads provided XML string into this <see cref="DOMDocument"/>. /// </summary> /// <param name="xmlString">String representing XML document.</param> /// <param name="options">PHP options.</param> /// <param name="isHtml">Whether the <paramref name="xmlString"/> represents XML generated from HTML document (then it may contain some invalid XML characters).</param> /// <returns></returns> private bool loadXMLInternal(string xmlString, int options, bool isHtml) { this._isHtmlDocument = isHtml; var stream = new StringReader(xmlString); try { XmlReaderSettings settings = new XmlReaderSettings(); // validating XML reader if (this._validateOnParse) #pragma warning disable 618 { settings.ValidationType = ValidationType.Auto; } #pragma warning restore 618 // do not check invalid characters in HTML (XML) if (isHtml) { settings.CheckCharacters = false; } // load the document this.XmlDocument.Load(XmlReader.Create(stream, settings)); // done return(true); } catch (XmlException e) { PhpLibXml.IssueXmlError(new PhpLibXml.XmlError(PhpLibXml.LIBXML_ERR_ERROR, 0, 0, 0, e.Message, null)); return(false); } catch (IOException e) { PhpLibXml.IssueXmlError(new PhpLibXml.XmlError(PhpLibXml.LIBXML_ERR_ERROR, 0, 0, 0, e.Message, null)); return(false); } }