2001-05-15 22:48:17 +04:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
|
|
|
|
*
|
2012-05-21 15:12:37 +04:00
|
|
|
* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2001-05-15 22:48:17 +04:00
|
|
|
|
|
|
|
#include "nsRDFXMLParser.h"
|
|
|
|
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsIParser.h"
|
2012-03-22 18:42:42 +04:00
|
|
|
#include "nsCharsetSource.h"
|
2001-05-15 22:48:17 +04:00
|
|
|
#include "nsIRDFContentSink.h"
|
|
|
|
#include "nsParserCIID.h"
|
2006-01-02 05:30:32 +03:00
|
|
|
#include "nsStringStream.h"
|
2001-12-11 10:01:03 +03:00
|
|
|
#include "nsNetUtil.h"
|
2014-10-16 22:17:14 +04:00
|
|
|
#include "nsNullPrincipal.h"
|
2001-05-15 22:48:17 +04:00
|
|
|
|
|
|
|
static NS_DEFINE_CID(kParserCID, NS_PARSER_CID);
|
|
|
|
|
2014-04-27 11:06:00 +04:00
|
|
|
NS_IMPL_ISUPPORTS(nsRDFXMLParser, nsIRDFXMLParser)
|
2001-05-15 22:48:17 +04:00
|
|
|
|
2010-06-10 22:11:40 +04:00
|
|
|
nsresult
|
2001-05-15 22:48:17 +04:00
|
|
|
nsRDFXMLParser::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult)
|
|
|
|
{
|
|
|
|
if (aOuter)
|
|
|
|
return NS_ERROR_NO_AGGREGATION;
|
|
|
|
|
|
|
|
nsRDFXMLParser* result = new nsRDFXMLParser();
|
|
|
|
if (! result)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
nsresult rv;
|
|
|
|
NS_ADDREF(result);
|
|
|
|
rv = result->QueryInterface(aIID, aResult);
|
|
|
|
NS_RELEASE(result);
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRDFXMLParser::nsRDFXMLParser()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_CTOR(nsRDFXMLParser);
|
|
|
|
}
|
|
|
|
|
|
|
|
nsRDFXMLParser::~nsRDFXMLParser()
|
|
|
|
{
|
|
|
|
MOZ_COUNT_DTOR(nsRDFXMLParser);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
nsRDFXMLParser::ParseAsync(nsIRDFDataSource* aSink, nsIURI* aBaseURI, nsIStreamListener** aResult)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFContentSink> sink =
|
|
|
|
do_CreateInstance("@mozilla.org/rdf/content-sink;1", &rv);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = sink->Init(aBaseURI);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// We set the content sink's data source directly to our in-memory
|
|
|
|
// store. This allows the initial content to be generated "directly".
|
|
|
|
rv = sink->SetDataSource(aSink);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIParser> parser = do_CreateInstance(kParserCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2003-06-17 20:40:34 +04:00
|
|
|
parser->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"),
|
2002-01-28 01:02:00 +03:00
|
|
|
kCharsetFromDocTypeDefault);
|
2001-05-15 22:48:17 +04:00
|
|
|
parser->SetContentSink(sink);
|
|
|
|
|
|
|
|
rv = parser->Parse(aBaseURI);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
return CallQueryInterface(parser, aResult);
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2004-07-07 09:49:29 +04:00
|
|
|
nsRDFXMLParser::ParseString(nsIRDFDataSource* aSink, nsIURI* aBaseURI, const nsACString& aString)
|
2001-05-15 22:48:17 +04:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFContentSink> sink =
|
|
|
|
do_CreateInstance("@mozilla.org/rdf/content-sink;1", &rv);
|
|
|
|
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = sink->Init(aBaseURI);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// We set the content sink's data source directly to our in-memory
|
|
|
|
// store. This allows the initial content to be generated "directly".
|
|
|
|
rv = sink->SetDataSource(aSink);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIParser> parser = do_CreateInstance(kParserCID, &rv);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2003-06-17 20:40:34 +04:00
|
|
|
parser->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"),
|
2006-01-27 04:10:49 +03:00
|
|
|
kCharsetFromOtherComponent);
|
2001-05-15 22:48:17 +04:00
|
|
|
parser->SetContentSink(sink);
|
|
|
|
|
|
|
|
rv = parser->Parse(aBaseURI);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIStreamListener> listener =
|
|
|
|
do_QueryInterface(parser);
|
|
|
|
|
|
|
|
if (! listener)
|
|
|
|
return NS_ERROR_FAILURE;
|
|
|
|
|
2002-01-28 01:02:00 +03:00
|
|
|
nsCOMPtr<nsIInputStream> stream;
|
2004-07-07 09:49:29 +04:00
|
|
|
rv = NS_NewCStringInputStream(getter_AddRefs(stream), aString);
|
2001-12-11 10:01:03 +03:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2015-04-13 21:47:41 +03:00
|
|
|
nsCOMPtr<nsIPrincipal> nullPrincipal = nsNullPrincipal::Create();
|
|
|
|
NS_ENSURE_TRUE(nullPrincipal, NS_ERROR_FAILURE);
|
2014-10-16 22:17:14 +04:00
|
|
|
|
2001-12-11 10:01:03 +03:00
|
|
|
nsCOMPtr<nsIChannel> channel;
|
2014-10-16 22:17:14 +04:00
|
|
|
rv = NS_NewInputStreamChannel(getter_AddRefs(channel),
|
|
|
|
aBaseURI,
|
|
|
|
stream,
|
|
|
|
nullPrincipal,
|
|
|
|
nsILoadInfo::SEC_NORMAL,
|
|
|
|
nsIContentPolicy::TYPE_OTHER,
|
2004-10-26 02:08:09 +04:00
|
|
|
NS_LITERAL_CSTRING("text/xml"));
|
2001-05-15 22:48:17 +04:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2012-07-30 18:20:58 +04:00
|
|
|
listener->OnStartRequest(channel, nullptr);
|
|
|
|
listener->OnDataAvailable(channel, nullptr, stream, 0, aString.Length());
|
|
|
|
listener->OnStopRequest(channel, nullptr, NS_OK);
|
2001-05-15 22:48:17 +04:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|