Back to index

lightning-sunbird  0.9+nobinonly
nsRDFXMLParser.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
00002  *
00003  * ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org Code.
00017  *
00018  * The Initial Developer of the Original Code is
00019  * Netscape Communications Corporation.
00020  * Portions created by the Initial Developer are Copyright (C) 1999
00021  * the Initial Developer. All Rights Reserved.
00022  *
00023  * Contributor(s):
00024  *   Chris Waterson <waterson@netscape.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either of the GNU General Public License Version 2 or later (the "GPL"),
00028  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include "nsRDFXMLParser.h"
00041 
00042 #include "nsIComponentManager.h"
00043 #include "nsIParser.h"
00044 #include "nsIRDFContentSink.h"
00045 #include "nsParserCIID.h"
00046 #include "nsNetUtil.h"
00047 
00048 static NS_DEFINE_CID(kParserCID, NS_PARSER_CID);
00049 
00050 NS_IMPL_ISUPPORTS1(nsRDFXMLParser, nsIRDFXMLParser)
00051 
00052 NS_IMETHODIMP
00053 nsRDFXMLParser::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult)
00054 {
00055     if (aOuter)
00056         return NS_ERROR_NO_AGGREGATION;
00057 
00058     nsRDFXMLParser* result = new nsRDFXMLParser();
00059     if (! result)
00060         return NS_ERROR_OUT_OF_MEMORY;
00061 
00062     nsresult rv;
00063     NS_ADDREF(result);
00064     rv = result->QueryInterface(aIID, aResult);
00065     NS_RELEASE(result);
00066     return rv;
00067 }
00068 
00069 nsRDFXMLParser::nsRDFXMLParser()
00070 {
00071     MOZ_COUNT_CTOR(nsRDFXMLParser);
00072 }
00073 
00074 nsRDFXMLParser::~nsRDFXMLParser()
00075 {
00076     MOZ_COUNT_DTOR(nsRDFXMLParser);
00077 }
00078 
00079 NS_IMETHODIMP
00080 nsRDFXMLParser::ParseAsync(nsIRDFDataSource* aSink, nsIURI* aBaseURI, nsIStreamListener** aResult)
00081 {
00082     nsresult rv;
00083 
00084     nsCOMPtr<nsIRDFContentSink> sink =
00085         do_CreateInstance("@mozilla.org/rdf/content-sink;1", &rv);
00086 
00087     if (NS_FAILED(rv)) return rv;
00088 
00089     rv = sink->Init(aBaseURI);
00090     if (NS_FAILED(rv)) return rv;
00091 
00092     // We set the content sink's data source directly to our in-memory
00093     // store. This allows the initial content to be generated "directly".
00094     rv = sink->SetDataSource(aSink);
00095     if (NS_FAILED(rv)) return rv;
00096 
00097     nsCOMPtr<nsIParser> parser = do_CreateInstance(kParserCID, &rv);
00098     if (NS_FAILED(rv)) return rv;
00099 
00100     parser->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"),
00101                                kCharsetFromDocTypeDefault);
00102     parser->SetContentSink(sink);
00103 
00104     rv = parser->Parse(aBaseURI);
00105     if (NS_FAILED(rv)) return rv;
00106 
00107     return CallQueryInterface(parser, aResult);
00108 }
00109 
00110 NS_IMETHODIMP
00111 nsRDFXMLParser::ParseString(nsIRDFDataSource* aSink, nsIURI* aBaseURI, const nsACString& aString)
00112 {
00113     nsresult rv;
00114 
00115     nsCOMPtr<nsIRDFContentSink> sink =
00116         do_CreateInstance("@mozilla.org/rdf/content-sink;1", &rv);
00117 
00118     if (NS_FAILED(rv)) return rv;
00119 
00120     rv = sink->Init(aBaseURI);
00121     if (NS_FAILED(rv)) return rv;
00122 
00123     // We set the content sink's data source directly to our in-memory
00124     // store. This allows the initial content to be generated "directly".
00125     rv = sink->SetDataSource(aSink);
00126     if (NS_FAILED(rv)) return rv;
00127 
00128     nsCOMPtr<nsIParser> parser = do_CreateInstance(kParserCID, &rv);
00129     if (NS_FAILED(rv)) return rv;
00130 
00131     parser->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"),
00132                                kCharsetFromOtherComponent);
00133     parser->SetContentSink(sink);
00134 
00135     rv = parser->Parse(aBaseURI);
00136     if (NS_FAILED(rv)) return rv;
00137 
00138     nsCOMPtr<nsIStreamListener> listener =
00139         do_QueryInterface(parser);
00140 
00141     if (! listener)
00142         return NS_ERROR_FAILURE;
00143 
00144     nsCOMPtr<nsIInputStream> stream;
00145     rv = NS_NewCStringInputStream(getter_AddRefs(stream), aString);
00146     if (NS_FAILED(rv)) return rv;
00147 
00148     nsCOMPtr<nsIChannel> channel;
00149     rv = NS_NewInputStreamChannel(getter_AddRefs(channel), aBaseURI, stream,
00150                                   NS_LITERAL_CSTRING("text/xml"));
00151     if (NS_FAILED(rv)) return rv;
00152 
00153     listener->OnStartRequest(channel, nsnull);
00154     listener->OnDataAvailable(channel, nsnull, stream, 0, aString.Length());
00155     listener->OnStopRequest(channel, nsnull, NS_OK);
00156 
00157     return NS_OK;
00158 }