Example #1
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="iText.Kernel.XMP.XMPException">Thrown when the parsing fails.
        ///     </exception>
        private static XmlDocument ParseXmlFromBytebuffer(ByteBuffer buffer, ParseOptions options)
        {
            try {
                XmlDocument doc = new XmlDocument();
                doc.Load(GetSecureXmlReader(buffer.GetByteStream()));
                return(doc);
            } catch (XmlException e) {
                XmlDocument doc = new XmlDocument();
                if (options.GetAcceptLatin1())
                {
                    buffer = Latin1Converter.Convert(buffer);
                }

                if (options.GetFixControlChars())
                {
                    try {
                        StreamReader           streamReader = new StreamReader(buffer.GetByteStream(), Encoding.GetEncoding(buffer.GetEncoding()));
                        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.GetByteStream());
                return(doc);
            }
        }
Example #2
0
 /// <summary>
 /// Parses XML from an <seealso cref="Stream"/>,
 /// fixing the encoding (Latin-1 to UTF-8) and illegal control character optionally.
 /// </summary>
 /// <param name="stream"> an <code>InputStream</code> </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 ParseXmlFromInputStream(Stream stream, ParseOptions options)
 {
     if (!options.GetAcceptLatin1() && !options.GetFixControlChars())
     {
         XmlDocument doc = new XmlDocument();
         doc.Load(GetSecureXmlReader(stream));
         return(doc);
     }
     // load stream into bytebuffer
     try {
         ByteBuffer buffer = new ByteBuffer(stream);
         return(ParseXmlFromBytebuffer(buffer, options));
     }
     catch (IOException e) {
         throw new XMPException("Error reading the XML-file", XMPError.BADSTREAM, e);
     }
 }
 /// <summary>
 /// Parses XML from an
 /// <see cref="InputStream"/>
 /// ,
 /// fixing the encoding (Latin-1 to UTF-8) and illegal control character optionally.
 /// </summary>
 /// <param name="stream">an <code>InputStream</code></param>
 /// <param name="options">the parsing options</param>
 /// <returns>Returns an XML DOM-Document.</returns>
 /// <exception cref="Com.Adobe.Xmp.XMPException">Thrown when the parsing fails.</exception>
 private static XmlDocument ParseXmlFromInputStream(InputStream stream, ParseOptions options)
 {
     if (!options.GetAcceptLatin1() && !options.GetFixControlChars())
     {
         return(ParseInputSource(new InputSource(stream)));
     }
     else
     {
         // load stream into bytebuffer
         try
         {
             ByteBuffer buffer = new ByteBuffer(stream);
             return(ParseXmlFromBytebuffer(buffer, options));
         }
         catch (IOException e)
         {
             throw new XMPException("Error reading the XML-file", XMPErrorConstants.Badstream, e);
         }
     }
 }
        /// <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="Com.Adobe.Xmp.XMPException">Thrown when the parsing fails.</exception>
        private static XmlDocument ParseXmlFromBytebuffer(ByteBuffer buffer, ParseOptions options)
        {
            InputSource source = new InputSource(buffer.GetByteStream());

            try
            {
                return(ParseInputSource(source));
            }
            catch (XMPException e)
            {
                if (e.GetErrorCode() == XMPErrorConstants.Badxml || e.GetErrorCode() == XMPErrorConstants.Badstream)
                {
                    if (options.GetAcceptLatin1())
                    {
                        buffer = Latin1Converter.Convert(buffer);
                    }
                    if (options.GetFixControlChars())
                    {
                        try
                        {
                            string encoding = buffer.GetEncoding();
                            System.IO.StreamReader fixReader = new FixASCIIControlsReader(new InputStreamReader(buffer.GetByteStream(), encoding));
                            return(ParseInputSource(new InputSource(fixReader)));
                        }
                        catch (UnsupportedEncodingException)
                        {
                            // can normally not happen as the encoding is provided by a util function
                            throw new XMPException("Unsupported Encoding", XMPErrorConstants.Internalfailure, e);
                        }
                    }
                    source = new InputSource(buffer.GetByteStream());
                    return(ParseInputSource(source));
                }
                else
                {
                    throw;
                }
            }
        }