1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
2 : *
3 : * ***** BEGIN LICENSE BLOCK *****
4 : * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 : *
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/
10 : *
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.
15 : *
16 : * The Original Code is mozilla.org Code.
17 : *
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.
22 : *
23 : * Contributor(s):
24 : * Chris Waterson <waterson@netscape.com>
25 : *
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.
37 : *
38 : * ***** END LICENSE BLOCK ***** */
39 :
40 : #include "nsRDFXMLParser.h"
41 :
42 : #include "nsIComponentManager.h"
43 : #include "nsIParser.h"
44 : #include "nsIRDFContentSink.h"
45 : #include "nsParserCIID.h"
46 : #include "nsStringStream.h"
47 : #include "nsNetUtil.h"
48 :
49 : static NS_DEFINE_CID(kParserCID, NS_PARSER_CID);
50 :
51 35134 : NS_IMPL_ISUPPORTS1(nsRDFXMLParser, nsIRDFXMLParser)
52 :
53 : nsresult
54 2072 : nsRDFXMLParser::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult)
55 : {
56 2072 : if (aOuter)
57 0 : return NS_ERROR_NO_AGGREGATION;
58 :
59 2072 : nsRDFXMLParser* result = new nsRDFXMLParser();
60 2072 : if (! result)
61 0 : return NS_ERROR_OUT_OF_MEMORY;
62 :
63 : nsresult rv;
64 2072 : NS_ADDREF(result);
65 2072 : rv = result->QueryInterface(aIID, aResult);
66 2072 : NS_RELEASE(result);
67 2072 : return rv;
68 : }
69 :
70 2072 : nsRDFXMLParser::nsRDFXMLParser()
71 : {
72 2072 : MOZ_COUNT_CTOR(nsRDFXMLParser);
73 2072 : }
74 :
75 4144 : nsRDFXMLParser::~nsRDFXMLParser()
76 : {
77 2072 : MOZ_COUNT_DTOR(nsRDFXMLParser);
78 8288 : }
79 :
80 : NS_IMETHODIMP
81 1818 : nsRDFXMLParser::ParseAsync(nsIRDFDataSource* aSink, nsIURI* aBaseURI, nsIStreamListener** aResult)
82 : {
83 : nsresult rv;
84 :
85 : nsCOMPtr<nsIRDFContentSink> sink =
86 3636 : do_CreateInstance("@mozilla.org/rdf/content-sink;1", &rv);
87 :
88 1818 : if (NS_FAILED(rv)) return rv;
89 :
90 1818 : rv = sink->Init(aBaseURI);
91 1818 : if (NS_FAILED(rv)) return rv;
92 :
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 1818 : rv = sink->SetDataSource(aSink);
96 1818 : if (NS_FAILED(rv)) return rv;
97 :
98 3636 : nsCOMPtr<nsIParser> parser = do_CreateInstance(kParserCID, &rv);
99 1818 : if (NS_FAILED(rv)) return rv;
100 :
101 3636 : parser->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"),
102 1818 : kCharsetFromDocTypeDefault);
103 1818 : parser->SetContentSink(sink);
104 :
105 1818 : rv = parser->Parse(aBaseURI);
106 1818 : if (NS_FAILED(rv)) return rv;
107 :
108 1818 : return CallQueryInterface(parser, aResult);
109 : }
110 :
111 : NS_IMETHODIMP
112 254 : nsRDFXMLParser::ParseString(nsIRDFDataSource* aSink, nsIURI* aBaseURI, const nsACString& aString)
113 : {
114 : nsresult rv;
115 :
116 : nsCOMPtr<nsIRDFContentSink> sink =
117 508 : do_CreateInstance("@mozilla.org/rdf/content-sink;1", &rv);
118 :
119 254 : if (NS_FAILED(rv)) return rv;
120 :
121 254 : rv = sink->Init(aBaseURI);
122 254 : if (NS_FAILED(rv)) return rv;
123 :
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 254 : rv = sink->SetDataSource(aSink);
127 254 : if (NS_FAILED(rv)) return rv;
128 :
129 508 : nsCOMPtr<nsIParser> parser = do_CreateInstance(kParserCID, &rv);
130 254 : if (NS_FAILED(rv)) return rv;
131 :
132 508 : parser->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"),
133 254 : kCharsetFromOtherComponent);
134 254 : parser->SetContentSink(sink);
135 :
136 254 : rv = parser->Parse(aBaseURI);
137 254 : if (NS_FAILED(rv)) return rv;
138 :
139 : nsCOMPtr<nsIStreamListener> listener =
140 508 : do_QueryInterface(parser);
141 :
142 254 : if (! listener)
143 0 : return NS_ERROR_FAILURE;
144 :
145 508 : nsCOMPtr<nsIInputStream> stream;
146 254 : rv = NS_NewCStringInputStream(getter_AddRefs(stream), aString);
147 254 : if (NS_FAILED(rv)) return rv;
148 :
149 508 : nsCOMPtr<nsIChannel> channel;
150 254 : rv = NS_NewInputStreamChannel(getter_AddRefs(channel), aBaseURI, stream,
151 508 : NS_LITERAL_CSTRING("text/xml"));
152 254 : if (NS_FAILED(rv)) return rv;
153 :
154 254 : listener->OnStartRequest(channel, nsnull);
155 254 : listener->OnDataAvailable(channel, nsnull, stream, 0, aString.Length());
156 254 : listener->OnStopRequest(channel, nsnull, NS_OK);
157 :
158 254 : return NS_OK;
159 : }
|