Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        /// <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;
                }
            }
        }
Exemplo n.º 4
0
        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);
        }
Exemplo n.º 5
0
        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);
            }
        }
Exemplo n.º 6
0
        /// <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);
            }
        }