Thu, 22 Jan 2015 13:21:57 +0100
Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6
michael@0 | 1 | /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- |
michael@0 | 2 | * |
michael@0 | 3 | * This Source Code Form is subject to the terms of the Mozilla Public |
michael@0 | 4 | * License, v. 2.0. If a copy of the MPL was not distributed with this |
michael@0 | 5 | * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ |
michael@0 | 6 | |
michael@0 | 7 | #include "nsRDFXMLParser.h" |
michael@0 | 8 | |
michael@0 | 9 | #include "nsIComponentManager.h" |
michael@0 | 10 | #include "nsIParser.h" |
michael@0 | 11 | #include "nsCharsetSource.h" |
michael@0 | 12 | #include "nsIRDFContentSink.h" |
michael@0 | 13 | #include "nsParserCIID.h" |
michael@0 | 14 | #include "nsStringStream.h" |
michael@0 | 15 | #include "nsNetUtil.h" |
michael@0 | 16 | |
michael@0 | 17 | static NS_DEFINE_CID(kParserCID, NS_PARSER_CID); |
michael@0 | 18 | |
michael@0 | 19 | NS_IMPL_ISUPPORTS(nsRDFXMLParser, nsIRDFXMLParser) |
michael@0 | 20 | |
michael@0 | 21 | nsresult |
michael@0 | 22 | nsRDFXMLParser::Create(nsISupports* aOuter, REFNSIID aIID, void** aResult) |
michael@0 | 23 | { |
michael@0 | 24 | if (aOuter) |
michael@0 | 25 | return NS_ERROR_NO_AGGREGATION; |
michael@0 | 26 | |
michael@0 | 27 | nsRDFXMLParser* result = new nsRDFXMLParser(); |
michael@0 | 28 | if (! result) |
michael@0 | 29 | return NS_ERROR_OUT_OF_MEMORY; |
michael@0 | 30 | |
michael@0 | 31 | nsresult rv; |
michael@0 | 32 | NS_ADDREF(result); |
michael@0 | 33 | rv = result->QueryInterface(aIID, aResult); |
michael@0 | 34 | NS_RELEASE(result); |
michael@0 | 35 | return rv; |
michael@0 | 36 | } |
michael@0 | 37 | |
michael@0 | 38 | nsRDFXMLParser::nsRDFXMLParser() |
michael@0 | 39 | { |
michael@0 | 40 | MOZ_COUNT_CTOR(nsRDFXMLParser); |
michael@0 | 41 | } |
michael@0 | 42 | |
michael@0 | 43 | nsRDFXMLParser::~nsRDFXMLParser() |
michael@0 | 44 | { |
michael@0 | 45 | MOZ_COUNT_DTOR(nsRDFXMLParser); |
michael@0 | 46 | } |
michael@0 | 47 | |
michael@0 | 48 | NS_IMETHODIMP |
michael@0 | 49 | nsRDFXMLParser::ParseAsync(nsIRDFDataSource* aSink, nsIURI* aBaseURI, nsIStreamListener** aResult) |
michael@0 | 50 | { |
michael@0 | 51 | nsresult rv; |
michael@0 | 52 | |
michael@0 | 53 | nsCOMPtr<nsIRDFContentSink> sink = |
michael@0 | 54 | do_CreateInstance("@mozilla.org/rdf/content-sink;1", &rv); |
michael@0 | 55 | |
michael@0 | 56 | if (NS_FAILED(rv)) return rv; |
michael@0 | 57 | |
michael@0 | 58 | rv = sink->Init(aBaseURI); |
michael@0 | 59 | if (NS_FAILED(rv)) return rv; |
michael@0 | 60 | |
michael@0 | 61 | // We set the content sink's data source directly to our in-memory |
michael@0 | 62 | // store. This allows the initial content to be generated "directly". |
michael@0 | 63 | rv = sink->SetDataSource(aSink); |
michael@0 | 64 | if (NS_FAILED(rv)) return rv; |
michael@0 | 65 | |
michael@0 | 66 | nsCOMPtr<nsIParser> parser = do_CreateInstance(kParserCID, &rv); |
michael@0 | 67 | if (NS_FAILED(rv)) return rv; |
michael@0 | 68 | |
michael@0 | 69 | parser->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"), |
michael@0 | 70 | kCharsetFromDocTypeDefault); |
michael@0 | 71 | parser->SetContentSink(sink); |
michael@0 | 72 | |
michael@0 | 73 | rv = parser->Parse(aBaseURI); |
michael@0 | 74 | if (NS_FAILED(rv)) return rv; |
michael@0 | 75 | |
michael@0 | 76 | return CallQueryInterface(parser, aResult); |
michael@0 | 77 | } |
michael@0 | 78 | |
michael@0 | 79 | NS_IMETHODIMP |
michael@0 | 80 | nsRDFXMLParser::ParseString(nsIRDFDataSource* aSink, nsIURI* aBaseURI, const nsACString& aString) |
michael@0 | 81 | { |
michael@0 | 82 | nsresult rv; |
michael@0 | 83 | |
michael@0 | 84 | nsCOMPtr<nsIRDFContentSink> sink = |
michael@0 | 85 | do_CreateInstance("@mozilla.org/rdf/content-sink;1", &rv); |
michael@0 | 86 | |
michael@0 | 87 | if (NS_FAILED(rv)) return rv; |
michael@0 | 88 | |
michael@0 | 89 | rv = sink->Init(aBaseURI); |
michael@0 | 90 | if (NS_FAILED(rv)) return rv; |
michael@0 | 91 | |
michael@0 | 92 | // We set the content sink's data source directly to our in-memory |
michael@0 | 93 | // store. This allows the initial content to be generated "directly". |
michael@0 | 94 | rv = sink->SetDataSource(aSink); |
michael@0 | 95 | if (NS_FAILED(rv)) return rv; |
michael@0 | 96 | |
michael@0 | 97 | nsCOMPtr<nsIParser> parser = do_CreateInstance(kParserCID, &rv); |
michael@0 | 98 | if (NS_FAILED(rv)) return rv; |
michael@0 | 99 | |
michael@0 | 100 | parser->SetDocumentCharset(NS_LITERAL_CSTRING("UTF-8"), |
michael@0 | 101 | kCharsetFromOtherComponent); |
michael@0 | 102 | parser->SetContentSink(sink); |
michael@0 | 103 | |
michael@0 | 104 | rv = parser->Parse(aBaseURI); |
michael@0 | 105 | if (NS_FAILED(rv)) return rv; |
michael@0 | 106 | |
michael@0 | 107 | nsCOMPtr<nsIStreamListener> listener = |
michael@0 | 108 | do_QueryInterface(parser); |
michael@0 | 109 | |
michael@0 | 110 | if (! listener) |
michael@0 | 111 | return NS_ERROR_FAILURE; |
michael@0 | 112 | |
michael@0 | 113 | nsCOMPtr<nsIInputStream> stream; |
michael@0 | 114 | rv = NS_NewCStringInputStream(getter_AddRefs(stream), aString); |
michael@0 | 115 | if (NS_FAILED(rv)) return rv; |
michael@0 | 116 | |
michael@0 | 117 | nsCOMPtr<nsIChannel> channel; |
michael@0 | 118 | rv = NS_NewInputStreamChannel(getter_AddRefs(channel), aBaseURI, stream, |
michael@0 | 119 | NS_LITERAL_CSTRING("text/xml")); |
michael@0 | 120 | if (NS_FAILED(rv)) return rv; |
michael@0 | 121 | |
michael@0 | 122 | listener->OnStartRequest(channel, nullptr); |
michael@0 | 123 | listener->OnDataAvailable(channel, nullptr, stream, 0, aString.Length()); |
michael@0 | 124 | listener->OnStopRequest(channel, nullptr, NS_OK); |
michael@0 | 125 | |
michael@0 | 126 | return NS_OK; |
michael@0 | 127 | } |