1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "nsRDFXMLParser.h"
9 #include "nsIComponentManager.h"
10 #include "nsIParser.h"
11 #include "nsCharsetSource.h"
12 #include "nsIRDFContentSink.h"
13 #include "nsParserCIID.h"
14 #include "nsStringStream.h"
15 #include "nsNetUtil.h"
16 #include "nsNullPrincipal.h"
18 static NS_DEFINE_CID(kParserCID
, NS_PARSER_CID
);
20 NS_IMPL_ISUPPORTS(nsRDFXMLParser
, nsIRDFXMLParser
)
23 nsRDFXMLParser::Create(nsISupports
* aOuter
, REFNSIID aIID
, void** aResult
)
26 return NS_ERROR_NO_AGGREGATION
;
28 nsRDFXMLParser
* result
= new nsRDFXMLParser();
30 return NS_ERROR_OUT_OF_MEMORY
;
34 rv
= result
->QueryInterface(aIID
, aResult
);
39 nsRDFXMLParser::nsRDFXMLParser()
41 MOZ_COUNT_CTOR(nsRDFXMLParser
);
44 nsRDFXMLParser::~nsRDFXMLParser()
46 MOZ_COUNT_DTOR(nsRDFXMLParser
);
50 nsRDFXMLParser::ParseAsync(nsIRDFDataSource
* aSink
, nsIURI
* aBaseURI
, nsIStreamListener
** aResult
)
54 nsCOMPtr
<nsIRDFContentSink
> sink
=
55 do_CreateInstance("@mozilla.org/rdf/content-sink;1", &rv
);
57 if (NS_FAILED(rv
)) return rv
;
59 rv
= sink
->Init(aBaseURI
);
60 if (NS_FAILED(rv
)) return rv
;
62 // We set the content sink's data source directly to our in-memory
63 // store. This allows the initial content to be generated "directly".
64 rv
= sink
->SetDataSource(aSink
);
65 if (NS_FAILED(rv
)) return rv
;
67 nsCOMPtr
<nsIParser
> parser
= do_CreateInstance(kParserCID
, &rv
);
68 if (NS_FAILED(rv
)) return rv
;
70 parser
->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"),
71 kCharsetFromDocTypeDefault
);
72 parser
->SetContentSink(sink
);
74 rv
= parser
->Parse(aBaseURI
);
75 if (NS_FAILED(rv
)) return rv
;
77 return CallQueryInterface(parser
, aResult
);
81 nsRDFXMLParser::ParseString(nsIRDFDataSource
* aSink
, nsIURI
* aBaseURI
, const nsACString
& aString
)
85 nsCOMPtr
<nsIRDFContentSink
> sink
=
86 do_CreateInstance("@mozilla.org/rdf/content-sink;1", &rv
);
88 if (NS_FAILED(rv
)) return rv
;
90 rv
= sink
->Init(aBaseURI
);
91 if (NS_FAILED(rv
)) return rv
;
93 // We set the content sink's data source directly to our in-memory
94 // store. This allows the initial content to be generated "directly".
95 rv
= sink
->SetDataSource(aSink
);
96 if (NS_FAILED(rv
)) return rv
;
98 nsCOMPtr
<nsIParser
> parser
= do_CreateInstance(kParserCID
, &rv
);
99 if (NS_FAILED(rv
)) return rv
;
101 parser
->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"),
102 kCharsetFromOtherComponent
);
103 parser
->SetContentSink(sink
);
105 rv
= parser
->Parse(aBaseURI
);
106 if (NS_FAILED(rv
)) return rv
;
108 nsCOMPtr
<nsIStreamListener
> listener
=
109 do_QueryInterface(parser
);
112 return NS_ERROR_FAILURE
;
114 nsCOMPtr
<nsIInputStream
> stream
;
115 rv
= NS_NewCStringInputStream(getter_AddRefs(stream
), aString
);
116 if (NS_FAILED(rv
)) return rv
;
118 nsCOMPtr
<nsIPrincipal
> nullPrincipal
=
119 do_CreateInstance("@mozilla.org/nullprincipal;1", &rv
);
120 NS_ENSURE_SUCCESS(rv
, rv
);
122 nsCOMPtr
<nsIChannel
> channel
;
123 rv
= NS_NewInputStreamChannel(getter_AddRefs(channel
),
127 nsILoadInfo::SEC_NORMAL
,
128 nsIContentPolicy::TYPE_OTHER
,
129 NS_LITERAL_CSTRING("text/xml"));
130 if (NS_FAILED(rv
)) return rv
;
132 listener
->OnStartRequest(channel
, nullptr);
133 listener
->OnDataAvailable(channel
, nullptr, stream
, 0, aString
.Length());
134 listener
->OnStopRequest(channel
, nullptr, NS_OK
);