b=253954, let nsDOMParser detect charset from xml entity decl -- finally remembered to land this; r=bz,sr=shaver

This commit is contained in:
vladimir%pobox.com 2004-09-20 23:15:26 +00:00
Родитель b64d2bbf98
Коммит 3c86288e1c
5 изменённых файлов: 47 добавлений и 33 удалений

Просмотреть файл

@ -501,7 +501,7 @@ nsFeedLoadListener::TryParseAsSimpleRSS ()
if (NS_FAILED(rv)) return rv;
nsCOMPtr<nsIDOMDocument> xmldoc;
rv = parser->ParseFromUTF8String (mBody, NS_LITERAL_CSTRING("text/xml"), getter_AddRefs(xmldoc));
rv = parser->ParseFromBuffer ((const PRUint8*) mBody.get(), mBody.Length(), "text/xml", getter_AddRefs(xmldoc));
if (NS_FAILED(rv)) return rv;
// becomes true if we figure out that this is an atom stream.

Просмотреть файл

@ -291,8 +291,8 @@ NS_IMETHODIMP
if (NS_FAILED(rv))
return rv;
rv = parser->ParseFromString(nsString(*kEmptySOAPDocStr[aVersion]).get(),
NS_LITERAL_CSTRING("text/xml"), getter_AddRefs(mMessage));
rv = parser->ParseFromString(nsPromiseFlatString(*kEmptySOAPDocStr[aVersion]).get(),
"text/xml", getter_AddRefs(mMessage));
if (NS_FAILED(rv))
return rv;

Просмотреть файл

@ -56,22 +56,24 @@ interface nsIDOMParser : nsISupports {
/**
* The string passed in is parsed into a DOM document.
*
* @param str The string to be parsed
* @param str The UTF16 string to be parsed
* @param contentType The content type of the string (see parseFromStream)
* @returns The DOM document created as a result of parsing the
* string
*/
nsIDOMDocument parseFromString(in wstring str, in AUTF8String contentType);
nsIDOMDocument parseFromString(in wstring str, in string contentType);
/**
* The UTF8 string passed in is parsed into a DOM document.
* The buffer is parsed into a DOM document.
* The charset is determined from the xml entity decl.
*
* @param str The UTF8 string to be parsed
* @param contentType The content type of the string (see parseFromStream)
* @param buf The octet array data to be parsed
* @param bufLen Length (in bytes) of the data
* @param contentType The content type of the data (see parseFromStream)
* @returns The DOM document created as a result of parsing the
* string
*/
nsIDOMDocument parseFromUTF8String(in AUTF8String str, in AUTF8String contentType);
nsIDOMDocument parseFromBuffer([const,array,size_is(bufLen)] in octet buf, in PRUint32 bufLen, in string contentType);
/**
* The byte stream passed in is parsed into a DOM document.
@ -81,15 +83,17 @@ interface nsIDOMParser : nsISupports {
* @param stream The byte stream whose contents are parsed
* @param charset The character set that was used to encode the byte
* stream. NULL if not specified.
* @param contentLength The number of bytes in the input stream.
* @param contentType The content type of the string - either text/xml,
* application/xml, or application/xhtml+xml
* application/xml, or application/xhtml+xml.
* Must not be NULL.
* @returns The DOM document created as a result of parsing the
* stream
*/
nsIDOMDocument parseFromStream(in nsIInputStream stream,
in AUTF8String charset,
in string charset,
in long contentLength,
in AUTF8String contentType);
in string contentType);
/**
* Set/Get the baseURI, may be needed when called from native code.

Просмотреть файл

@ -413,7 +413,7 @@ ConvertWStringToStream(const PRUnichar* aStr,
/* nsIDOMDocument parseFromString (in wstring str, in string contentType); */
NS_IMETHODIMP
nsDOMParser::ParseFromString(const PRUnichar *str,
const nsACString& contentType,
const char *contentType,
nsIDOMDocument **_retval)
{
NS_ENSURE_ARG(str);
@ -428,26 +428,32 @@ nsDOMParser::ParseFromString(const PRUnichar *str,
return rv;
}
return ParseFromStream(stream, NS_LITERAL_CSTRING("UTF-8"), contentLength, contentType, _retval);
return ParseFromStream(stream, "UTF-8", contentLength, contentType, _retval);
}
/* nsIDOMDocument parseFromUTF8String (in wstring str, in string contentType); */
/* nsIDOMDocument parseFromBuffer([const,array,size_is(bufLen)] in octet buf, in PRUint32 bufLen, in string contentType); */
NS_IMETHODIMP
nsDOMParser::ParseFromUTF8String(const nsACString& str,
const nsACString& contentType,
nsIDOMDocument **_retval)
nsDOMParser::ParseFromBuffer(const PRUint8 *buf,
PRUint32 bufLen,
const char *contentType,
nsIDOMDocument **_retval)
{
NS_ENSURE_ARG_POINTER(buf);
NS_ENSURE_ARG_POINTER(_retval);
nsCOMPtr<nsIInputStream> stream;
nsCOMPtr<nsIByteArrayInputStream> baiStream;
char *bufStr = ToNewCString(str);
PRUint8 *streamBuf = (PRUint8*)nsMemory::Clone(buf, bufLen);
if (streamBuf == nsnull) {
*_retval = nsnull;
return NS_ERROR_OUT_OF_MEMORY;
}
// The new stream takes ownership of the buffer
nsresult rv = NS_NewByteArrayInputStream(getter_AddRefs(baiStream), bufStr, str.Length());
nsresult rv = NS_NewByteArrayInputStream(getter_AddRefs(baiStream), (char*)streamBuf, bufLen);
if (NS_FAILED(rv)) {
nsMemory::Free(bufStr);
nsMemory::Free(streamBuf);
*_retval = nsnull;
return rv;
}
@ -458,28 +464,28 @@ nsDOMParser::ParseFromUTF8String(const nsACString& str,
return NS_ERROR_FAILURE;
}
return ParseFromStream(stream, NS_LITERAL_CSTRING("UTF-8"), str.Length(), contentType, _retval);
return ParseFromStream(stream, nsnull, bufLen, contentType, _retval);
}
/* nsIDOMDocument parseFromStream (in nsIInputStream stream, in string charset, in string contentType); */
NS_IMETHODIMP
nsDOMParser::ParseFromStream(nsIInputStream *stream,
const nsACString& charset,
const char *charset,
PRInt32 contentLength,
const nsACString& contentType,
const char *contentType,
nsIDOMDocument **_retval)
{
NS_ENSURE_ARG(stream);
NS_ENSURE_ARG(contentType);
NS_ENSURE_ARG_POINTER(_retval);
*_retval = nsnull;
// For now, we can only create XML documents.
if (!(contentType.Equals(NS_LITERAL_CSTRING("text/xml"))) &&
!(contentType.Equals(NS_LITERAL_CSTRING("application/xml"))) &&
!(contentType.Equals(NS_LITERAL_CSTRING("application/xhtml+xml")))) {
if ((nsCRT::strcmp(contentType, "text/xml") != 0) &&
(nsCRT::strcmp(contentType, "application/xml") != 0) &&
(nsCRT::strcmp(contentType, "application/xhtml+xml") != 0))
return NS_ERROR_NOT_IMPLEMENTED;
}
nsresult rv;
nsCOMPtr<nsIPrincipal> principal;
@ -568,7 +574,7 @@ nsDOMParser::ParseFromStream(nsIInputStream *stream,
}
// Create a fake channel
nsDOMParserChannel* parserChannel = new nsDOMParserChannel(baseURI, contentType);
nsDOMParserChannel* parserChannel = new nsDOMParserChannel(baseURI, nsDependentCString(contentType));
if (!parserChannel) return NS_ERROR_OUT_OF_MEMORY;
// Hold a reference to it in this method
@ -576,7 +582,11 @@ nsDOMParser::ParseFromStream(nsIInputStream *stream,
if (principal) {
channel->SetOwner(principal);
}
parserChannel->SetContentCharset(charset);
if (charset) {
parserChannel->SetContentCharset(nsDependentCString(charset));
}
nsCOMPtr<nsIRequest> request = NS_STATIC_CAST(nsIRequest*, parserChannel);
// Tell the document to start loading

Просмотреть файл

@ -153,7 +153,7 @@ int main (int argc, char* argv[])
if (NS_SUCCEEDED( rv )) {
nsString str; str.AssignWithConversion(argv[2]);
rv = pDOMParser->ParseFromString(str.get(), NS_LITERAL_CSTRING("text/xml"),
rv = pDOMParser->ParseFromString(str.get(), "text/xml",
getter_AddRefs( pDOMDocument ) );
if (NS_SUCCEEDED( rv )) {
@ -191,9 +191,9 @@ int main (int argc, char* argv[])
pDOMParser->SetBaseURI(pURI);
rv = pDOMParser->ParseFromStream( pInputStream,
NS_LITERAL_CSTRING("UTF-8"),
"UTF-8",
uiContentLength,
NS_LITERAL_CSTRING("text/xml"),
"text/xml",
getter_AddRefs( pDOMDocument ) );
if (NS_SUCCEEDED( rv )) {
printf( "DOM parse of %s successful\n", argv[2] );