Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / rdf / base / src / nsRDFXMLParser.cpp
blob8b052e679af872b062ae42e725ca9bc282934ce5
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
3 * ***** BEGIN LICENSE BLOCK *****
4 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
6 * The contents of this file are subject to the Mozilla Public License Version
7 * 1.1 (the "License"); you may not use this file except in compliance with
8 * the License. You may obtain a copy of the License at
9 * http://www.mozilla.org/MPL/
11 * Software distributed under the License is distributed on an "AS IS" basis,
12 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
13 * for the specific language governing rights and limitations under the
14 * License.
16 * The Original Code is mozilla.org Code.
18 * The Initial Developer of the Original Code is
19 * Netscape Communications Corporation.
20 * Portions created by the Initial Developer are Copyright (C) 1999
21 * the Initial Developer. All Rights Reserved.
23 * Contributor(s):
24 * Chris Waterson <waterson@netscape.com>
26 * Alternatively, the contents of this file may be used under the terms of
27 * either of the GNU General Public License Version 2 or later (the "GPL"),
28 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
29 * in which case the provisions of the GPL or the LGPL are applicable instead
30 * of those above. If you wish to allow use of your version of this file only
31 * under the terms of either the GPL or the LGPL, and not to allow others to
32 * use your version of this file under the terms of the MPL, indicate your
33 * decision by deleting the provisions above and replace them with the notice
34 * and other provisions required by the GPL or the LGPL. If you do not delete
35 * the provisions above, a recipient may use your version of this file under
36 * the terms of any one of the MPL, the GPL or the LGPL.
38 * ***** END LICENSE BLOCK ***** */
40 #include "nsRDFXMLParser.h"
42 #include "nsIComponentManager.h"
43 #include "nsIParser.h"
44 #include "nsIRDFContentSink.h"
45 #include "nsParserCIID.h"
46 #include "nsStringStream.h"
47 #include "nsNetUtil.h"
49 static NS_DEFINE_CID(kParserCID, NS_PARSER_CID);
51 NS_IMPL_ISUPPORTS1(nsRDFXMLParser, nsIRDFXMLParser)
53 NS_IMETHODIMP
54 nsRDFXMLParser::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult)
56 if (aOuter)
57 return NS_ERROR_NO_AGGREGATION;
59 nsRDFXMLParser* result = new nsRDFXMLParser();
60 if (! result)
61 return NS_ERROR_OUT_OF_MEMORY;
63 nsresult rv;
64 NS_ADDREF(result);
65 rv = result->QueryInterface(aIID, aResult);
66 NS_RELEASE(result);
67 return rv;
70 nsRDFXMLParser::nsRDFXMLParser()
72 MOZ_COUNT_CTOR(nsRDFXMLParser);
75 nsRDFXMLParser::~nsRDFXMLParser()
77 MOZ_COUNT_DTOR(nsRDFXMLParser);
80 NS_IMETHODIMP
81 nsRDFXMLParser::ParseAsync(nsIRDFDataSource* aSink, nsIURI* aBaseURI, nsIStreamListener** aResult)
83 nsresult rv;
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 kCharsetFromDocTypeDefault);
103 parser->SetContentSink(sink);
105 rv = parser->Parse(aBaseURI);
106 if (NS_FAILED(rv)) return rv;
108 return CallQueryInterface(parser, aResult);
111 NS_IMETHODIMP
112 nsRDFXMLParser::ParseString(nsIRDFDataSource* aSink, nsIURI* aBaseURI, const nsACString& aString)
114 nsresult rv;
116 nsCOMPtr<nsIRDFContentSink> sink =
117 do_CreateInstance("@mozilla.org/rdf/content-sink;1", &rv);
119 if (NS_FAILED(rv)) return rv;
121 rv = sink->Init(aBaseURI);
122 if (NS_FAILED(rv)) return rv;
124 // We set the content sink's data source directly to our in-memory
125 // store. This allows the initial content to be generated "directly".
126 rv = sink->SetDataSource(aSink);
127 if (NS_FAILED(rv)) return rv;
129 nsCOMPtr<nsIParser> parser = do_CreateInstance(kParserCID, &rv);
130 if (NS_FAILED(rv)) return rv;
132 parser->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"),
133 kCharsetFromOtherComponent);
134 parser->SetContentSink(sink);
136 rv = parser->Parse(aBaseURI);
137 if (NS_FAILED(rv)) return rv;
139 nsCOMPtr<nsIStreamListener> listener =
140 do_QueryInterface(parser);
142 if (! listener)
143 return NS_ERROR_FAILURE;
145 nsCOMPtr<nsIInputStream> stream;
146 rv = NS_NewCStringInputStream(getter_AddRefs(stream), aString);
147 if (NS_FAILED(rv)) return rv;
149 nsCOMPtr<nsIChannel> channel;
150 rv = NS_NewInputStreamChannel(getter_AddRefs(channel), aBaseURI, stream,
151 NS_LITERAL_CSTRING("text/xml"));
152 if (NS_FAILED(rv)) return rv;
154 listener->OnStartRequest(channel, nsnull);
155 listener->OnDataAvailable(channel, nsnull, stream, 0, aString.Length());
156 listener->OnStopRequest(channel, nsnull, NS_OK);
158 return NS_OK;