/// <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; } } }
/// <summary> /// Parses XML from a /// <see cref="string"/> /// , /// fixing the illegal control character optionally. /// </summary> /// <param name="input">a <code>String</code> 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 ParseXmlFromString(string input, ParseOptions options) { InputSource source = new InputSource(new Sharpen.StringReader(input)); try { return ParseInputSource(source); } catch (XMPException e) { if (e.GetErrorCode() == XMPErrorConstants.Badxml && options.GetFixControlChars()) { source = new InputSource(new FixASCIIControlsReader(new Sharpen.StringReader(input))); return ParseInputSource(source); } else { throw; } } }
/// <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); } } }