rdf/base/src/rdfTriplesSerializer.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #include "nsIOutputStream.h"
michael@0 7 #include "nsReadableUtils.h"
michael@0 8 #include "nsCRT.h"
michael@0 9 #include "nsCOMPtr.h"
michael@0 10 #include "nsString.h"
michael@0 11 #include "nsPrintfCString.h"
michael@0 12 #include "nsIBufferedStreams.h"
michael@0 13 #include "nsNetCID.h"
michael@0 14 #include "nsComponentManagerUtils.h"
michael@0 15
michael@0 16 #include "rdfISerializer.h"
michael@0 17 #include "rdfIDataSource.h"
michael@0 18 #include "rdfITripleVisitor.h"
michael@0 19
michael@0 20 #include "nsIRDFResource.h"
michael@0 21 #include "nsIRDFLiteral.h"
michael@0 22 #include "mozilla/Attributes.h"
michael@0 23
michael@0 24 class TriplesVisitor MOZ_FINAL : public rdfITripleVisitor
michael@0 25 {
michael@0 26 public:
michael@0 27 TriplesVisitor(nsIOutputStream* aOut) : mOut(aOut) {}
michael@0 28 NS_DECL_RDFITRIPLEVISITOR
michael@0 29 NS_DECL_ISUPPORTS
michael@0 30 protected:
michael@0 31 nsresult writeResource(nsIRDFResource* aResource);
michael@0 32 nsIOutputStream* mOut;
michael@0 33 };
michael@0 34
michael@0 35 NS_IMPL_ISUPPORTS(TriplesVisitor, rdfITripleVisitor)
michael@0 36
michael@0 37 nsresult
michael@0 38 TriplesVisitor::writeResource(nsIRDFResource *aResource)
michael@0 39 {
michael@0 40 nsCString res;
michael@0 41 uint32_t writeCount, wroteCount;
michael@0 42 mOut->Write("<", 1, &wroteCount);
michael@0 43 NS_ENSURE_TRUE(wroteCount == 1, NS_ERROR_FAILURE);
michael@0 44 nsresult rv = aResource->GetValueUTF8(res);
michael@0 45 NS_ENSURE_SUCCESS(rv, rv);
michael@0 46 writeCount = res.Length();
michael@0 47 mOut->Write(res.get(), writeCount, &wroteCount);
michael@0 48 NS_ENSURE_TRUE(writeCount == wroteCount, NS_ERROR_FAILURE);
michael@0 49 mOut->Write("> ", 2, &wroteCount);
michael@0 50 NS_ENSURE_TRUE(wroteCount == 2, NS_ERROR_FAILURE);
michael@0 51 return NS_OK;
michael@0 52 }
michael@0 53
michael@0 54 NS_IMETHODIMP
michael@0 55 TriplesVisitor::Visit(nsIRDFNode *aSubject, nsIRDFResource *aPredicate,
michael@0 56 nsIRDFNode *aObject, bool aTruthValue)
michael@0 57 {
michael@0 58 nsCOMPtr<nsIRDFResource> subjectRes = do_QueryInterface(aSubject);
michael@0 59 nsresult rv = NS_OK;
michael@0 60 if (subjectRes) {
michael@0 61 rv = writeResource(subjectRes);
michael@0 62 }
michael@0 63 if (NS_FAILED(rv)) {
michael@0 64 return rv;
michael@0 65 }
michael@0 66 rv = writeResource(aPredicate);
michael@0 67 if (NS_FAILED(rv)) {
michael@0 68 return rv;
michael@0 69 }
michael@0 70 nsCOMPtr<nsIRDFResource> res = do_QueryInterface(aObject);
michael@0 71 nsCOMPtr<nsIRDFLiteral> lit;
michael@0 72 nsCOMPtr<nsIRDFInt> intLit;
michael@0 73 uint32_t wroteCount;
michael@0 74 if (res) {
michael@0 75 rv = writeResource(res);
michael@0 76 } else if ((lit = do_QueryInterface(aObject)) != nullptr) {
michael@0 77 const char16_t *value;
michael@0 78 lit->GetValueConst(&value);
michael@0 79 nsAutoCString object;
michael@0 80 object.AppendLiteral("\"");
michael@0 81 AppendUTF16toUTF8(value, object);
michael@0 82 object.AppendLiteral("\" ");
michael@0 83 uint32_t writeCount = object.Length();
michael@0 84 rv = mOut->Write(object.get(), writeCount, &wroteCount);
michael@0 85 NS_ENSURE_TRUE(writeCount == wroteCount, NS_ERROR_FAILURE);
michael@0 86 } else if ((intLit = do_QueryInterface(aObject)) != nullptr) {
michael@0 87 int32_t value;
michael@0 88 intLit->GetValue(&value);
michael@0 89 nsPrintfCString
michael@0 90 object("\"%i\"^^<http://www.w3.org/2001/XMLSchema#integer> ",
michael@0 91 value);
michael@0 92 uint32_t writeCount = object.Length();
michael@0 93 rv = mOut->Write(object.get(), writeCount, &wroteCount);
michael@0 94 NS_ENSURE_TRUE(writeCount == wroteCount, NS_ERROR_FAILURE);
michael@0 95 }
michael@0 96 NS_ENSURE_SUCCESS(rv, rv);
michael@0 97 return mOut->Write(".\n", 2, &wroteCount);
michael@0 98 }
michael@0 99
michael@0 100 class rdfTriplesSerializer MOZ_FINAL : public rdfISerializer
michael@0 101 {
michael@0 102 public:
michael@0 103 NS_DECL_ISUPPORTS
michael@0 104 NS_DECL_RDFISERIALIZER
michael@0 105
michael@0 106 rdfTriplesSerializer();
michael@0 107
michael@0 108 private:
michael@0 109 ~rdfTriplesSerializer();
michael@0 110
michael@0 111 };
michael@0 112
michael@0 113 nsresult
michael@0 114 NS_NewTriplesSerializer(rdfISerializer** aResult)
michael@0 115 {
michael@0 116 NS_PRECONDITION(aResult != nullptr, "null ptr");
michael@0 117 if (! aResult)
michael@0 118 return NS_ERROR_NULL_POINTER;
michael@0 119
michael@0 120 *aResult = new rdfTriplesSerializer();
michael@0 121 if (! *aResult)
michael@0 122 return NS_ERROR_OUT_OF_MEMORY;
michael@0 123 NS_ADDREF(*aResult);
michael@0 124 return NS_OK;
michael@0 125 }
michael@0 126
michael@0 127 NS_IMPL_ISUPPORTS(rdfTriplesSerializer, rdfISerializer)
michael@0 128
michael@0 129 rdfTriplesSerializer::rdfTriplesSerializer()
michael@0 130 {
michael@0 131 }
michael@0 132
michael@0 133 rdfTriplesSerializer::~rdfTriplesSerializer()
michael@0 134 {
michael@0 135 }
michael@0 136
michael@0 137 NS_IMETHODIMP
michael@0 138 rdfTriplesSerializer::Serialize(rdfIDataSource *aDataSource,
michael@0 139 nsIOutputStream *aOut)
michael@0 140 {
michael@0 141 nsresult rv;
michael@0 142 nsCOMPtr<nsIBufferedOutputStream> bufout =
michael@0 143 do_CreateInstance(NS_BUFFEREDOUTPUTSTREAM_CONTRACTID, &rv);
michael@0 144 NS_ENSURE_SUCCESS(rv, rv);
michael@0 145 rv = bufout->Init(aOut, 1024);
michael@0 146 NS_ENSURE_SUCCESS(rv, rv);
michael@0 147 nsCOMPtr<rdfITripleVisitor> tv = new TriplesVisitor(bufout);
michael@0 148 NS_ENSURE_TRUE(tv, NS_ERROR_OUT_OF_MEMORY);
michael@0 149 return aDataSource->VisitAllTriples(tv);
michael@0 150 }

mercurial