/// <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 { return(ParseStream(buffer.GetByteStream())); } catch (XmpException e) { if (e.GetErrorCode() == XmpErrorCode.BadXml || e.GetErrorCode() == XmpErrorCode.BadStream) { if (options.AcceptLatin1) { buffer = Latin1Converter.Convert(buffer); } if (options.FixControlChars) { try { return(ParseTextReader(new FixAsciiControlsReader(new StreamReader(buffer.GetByteStream(), buffer.GetEncoding())))); } catch { // can normally not happen as the encoding is provided by a util function throw new XmpException("Unsupported Encoding", XmpErrorCode.InternalFailure, e); } } return(ParseStream(buffer.GetByteStream())); } throw; } }
/// <summary> /// Parses XML from a byte buffer, /// fixing the encoding (Latin-1 to UTF-8) and illegal control character optionally. /// To improve the performance on legal files, it is first tried to parse normally, /// while the character fixing is only done when the first pass fails. /// </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 XDocument ParseXmlFromByteBuffer(ByteBuffer buffer, ParseOptions options) { try { return(ParseStream(buffer.GetByteStream(), options)); } catch (XmpException e) { //if ("DOCTYPE is disallowed".equals(e.getCause().getMessage())) //{ // throw new XMPException(e.getCause().getMessage(), XMPError.BADXML); //} //else if (e.ErrorCode == XmpErrorCode.BadXml || e.ErrorCode == XmpErrorCode.BadStream) { if (options.AcceptLatin1) { buffer = Latin1Converter.Convert(buffer); } if (options.FixControlChars) { try { return(ParseTextReader(new FixAsciiControlsReader(new StreamReader(buffer.GetByteStream(), buffer.GetEncoding())), options)); } catch { // can normally not happen as the encoding is provided by a util function throw new XmpException("Unsupported Encoding", XmpErrorCode.InternalFailure, e); } } return(ParseStream(buffer.GetByteStream(), options)); } throw; } }