@since 22.08.2006
Inheritance: PushbackReader
        /// <summary>
        /// Parses XML from a byte buffer,
        /// fixing the encoding (Latin-1 to UTF-8) and illegal control character optionally.
        /// </summary>
        /// <param name="buffer"> a byte buffer containing the XMP packet </param>
        /// <param name="options"> the parsing options </param>
        /// <returns> Returns an XML DOM-Document. </returns>
        /// <exception cref="XmpException"> Thrown when the parsing fails. </exception>
        private static XmlDocument ParseXmlFromBytebuffer(ByteBuffer buffer, ParseOptions options)
        {
            try {
                XmlDocument doc = new XmlDocument();
                doc.Load(buffer.ByteStream);
                return(doc);
            }
            catch (XmpException e) {
                XmlDocument doc = new XmlDocument();
                if (e.ErrorCode == XmpError.BADXML || e.ErrorCode == XmpError.BADSTREAM)
                {
                    if (options.AcceptLatin1)
                    {
                        buffer = Latin1Converter.Convert(buffer);
                    }

                    if (options.FixControlChars)
                    {
                        try {
                            string       encoding  = buffer.Encoding;
                            StreamReader fixReader = new FixAsciiControlsReader(buffer.ByteStream, encoding);
                            doc.Load(fixReader);
                            return(doc);
                        }
                        catch (Exception) {
                            // can normally not happen as the encoding is provided by a util function
                            throw new XmpException("Unsupported Encoding", XmpError.INTERNALFAILURE, e);
                        }
                    }
                    doc.Load(buffer.ByteStream);
                    return(doc);
                }
                throw e;
            }
        }
Exemple #2
0
        /// <summary>
        /// Parses XML from a byte buffer,
        /// fixing the encoding (Latin-1 to UTF-8) and illegal control character optionally.
        /// </summary>
        /// <param name="buffer"> a byte buffer containing the XMP packet </param>
        /// <param name="options"> the parsing options </param>
        /// <returns> Returns an XML DOM-Document. </returns>
        /// <exception cref="XmpException"> Thrown when the parsing fails. </exception>
        private static XmlDocument ParseXmlFromBytebuffer(ByteBuffer buffer, ParseOptions options)
        {
            try {
                XmlDocument doc = new XmlDocument();
                doc.Load(GetSecureXmlReader(buffer.ByteStream));
                return(doc);
            } catch (XmlException e) {
                XmlDocument doc = new XmlDocument();
                if (options.AcceptLatin1)
                {
                    buffer = Latin1Converter.Convert(buffer);
                }

                if (options.FixControlChars)
                {
                    try {
                        StreamReader           streamReader = new StreamReader(buffer.ByteStream, Encoding.GetEncoding(buffer.Encoding));
                        FixAsciiControlsReader fixReader    = new FixAsciiControlsReader(streamReader);
                        doc.Load(GetSecureXmlReader(fixReader));
                        return(doc);
                    } catch (Exception) {
                        // can normally not happen as the encoding is provided by a util function
                        throw new XmpException("Unsupported Encoding", XmpError.INTERNALFAILURE, e);
                    }
                }
                doc.Load(buffer.ByteStream);
                return(doc);
            }
        }
        /// <summary>
        /// Parses XML from a byte buffer, 
        /// fixing the encoding (Latin-1 to UTF-8) and illegal control character optionally.
        /// </summary>
        /// <param name="buffer"> a byte buffer containing the XMP packet </param>
        /// <param name="options"> the parsing options </param>
        /// <returns> Returns an XML DOM-Document. </returns>
        /// <exception cref="XmpException"> Thrown when the parsing fails. </exception>
        private static XmlDocument ParseXmlFromBytebuffer(ByteBuffer buffer, ParseOptions options) {
            try {
                XmlDocument doc = new XmlDocument();
                doc.Load(buffer.ByteStream);
                return doc;
            } catch (XmlException e) {
                XmlDocument doc = new XmlDocument();
                if (options.AcceptLatin1) {
                    buffer = Latin1Converter.Convert(buffer);
                }

                if (options.FixControlChars) {
                    try {
                        StreamReader streamReader = new StreamReader(buffer.ByteStream, Encoding.GetEncoding(buffer.Encoding));
                        FixAsciiControlsReader fixReader = new FixAsciiControlsReader(streamReader);
                        doc.Load(fixReader);
                        return doc;
                    } catch (Exception) {
                        // can normally not happen as the encoding is provided by a util function
                        throw new XmpException("Unsupported Encoding", XmpError.INTERNALFAILURE, e);
                    }
                }
                doc.Load(buffer.ByteStream);
                return doc;
            }
        }