2007-03-22 10:30:00 -07:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 04:12:37 -07:00
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
#include "nsIOutputStream.h"
|
|
|
|
#include "nsReadableUtils.h"
|
|
|
|
#include "nsCRT.h"
|
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
#include "nsPrintfCString.h"
|
|
|
|
#include "nsIBufferedStreams.h"
|
|
|
|
#include "nsNetCID.h"
|
|
|
|
#include "nsComponentManagerUtils.h"
|
|
|
|
|
|
|
|
#include "rdfISerializer.h"
|
|
|
|
#include "rdfIDataSource.h"
|
|
|
|
#include "rdfITripleVisitor.h"
|
|
|
|
|
|
|
|
#include "nsIRDFResource.h"
|
|
|
|
#include "nsIRDFLiteral.h"
|
2012-06-12 20:42:36 -07:00
|
|
|
#include "mozilla/Attributes.h"
|
2007-03-22 10:30:00 -07:00
|
|
|
|
2012-06-12 20:42:36 -07:00
|
|
|
class TriplesVisitor MOZ_FINAL : public rdfITripleVisitor
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
2014-09-02 15:21:23 -07:00
|
|
|
explicit TriplesVisitor(nsIOutputStream* aOut) : mOut(aOut) {}
|
2007-03-22 10:30:00 -07:00
|
|
|
NS_DECL_RDFITRIPLEVISITOR
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
protected:
|
2014-06-30 15:11:50 -07:00
|
|
|
~TriplesVisitor() {}
|
2007-03-22 10:30:00 -07:00
|
|
|
nsresult writeResource(nsIRDFResource* aResource);
|
|
|
|
nsIOutputStream* mOut;
|
|
|
|
};
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(TriplesVisitor, rdfITripleVisitor)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
TriplesVisitor::writeResource(nsIRDFResource *aResource)
|
|
|
|
{
|
|
|
|
nsCString res;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t writeCount, wroteCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
mOut->Write("<", 1, &wroteCount);
|
|
|
|
NS_ENSURE_TRUE(wroteCount == 1, NS_ERROR_FAILURE);
|
|
|
|
nsresult rv = aResource->GetValueUTF8(res);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
writeCount = res.Length();
|
|
|
|
mOut->Write(res.get(), writeCount, &wroteCount);
|
|
|
|
NS_ENSURE_TRUE(writeCount == wroteCount, NS_ERROR_FAILURE);
|
|
|
|
mOut->Write("> ", 2, &wroteCount);
|
|
|
|
NS_ENSURE_TRUE(wroteCount == 2, NS_ERROR_FAILURE);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
TriplesVisitor::Visit(nsIRDFNode *aSubject, nsIRDFResource *aPredicate,
|
2011-09-28 23:19:26 -07:00
|
|
|
nsIRDFNode *aObject, bool aTruthValue)
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
nsCOMPtr<nsIRDFResource> subjectRes = do_QueryInterface(aSubject);
|
|
|
|
nsresult rv = NS_OK;
|
|
|
|
if (subjectRes) {
|
|
|
|
rv = writeResource(subjectRes);
|
|
|
|
}
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = writeResource(aPredicate);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nsCOMPtr<nsIRDFResource> res = do_QueryInterface(aObject);
|
|
|
|
nsCOMPtr<nsIRDFLiteral> lit;
|
|
|
|
nsCOMPtr<nsIRDFInt> intLit;
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t wroteCount;
|
2007-03-22 10:30:00 -07:00
|
|
|
if (res) {
|
|
|
|
rv = writeResource(res);
|
2012-07-30 07:20:58 -07:00
|
|
|
} else if ((lit = do_QueryInterface(aObject)) != nullptr) {
|
2014-01-04 07:02:17 -08:00
|
|
|
const char16_t *value;
|
2007-03-22 10:30:00 -07:00
|
|
|
lit->GetValueConst(&value);
|
2012-09-01 19:35:17 -07:00
|
|
|
nsAutoCString object;
|
2014-05-26 11:54:58 -07:00
|
|
|
object.Append('"');
|
2007-03-22 10:30:00 -07:00
|
|
|
AppendUTF16toUTF8(value, object);
|
|
|
|
object.AppendLiteral("\" ");
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t writeCount = object.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = mOut->Write(object.get(), writeCount, &wroteCount);
|
|
|
|
NS_ENSURE_TRUE(writeCount == wroteCount, NS_ERROR_FAILURE);
|
2012-07-30 07:20:58 -07:00
|
|
|
} else if ((intLit = do_QueryInterface(aObject)) != nullptr) {
|
2012-08-22 08:56:38 -07:00
|
|
|
int32_t value;
|
2007-03-22 10:30:00 -07:00
|
|
|
intLit->GetValue(&value);
|
|
|
|
nsPrintfCString
|
2012-04-24 11:43:00 -07:00
|
|
|
object("\"%i\"^^<http://www.w3.org/2001/XMLSchema#integer> ",
|
2007-03-22 10:30:00 -07:00
|
|
|
value);
|
2012-08-22 08:56:38 -07:00
|
|
|
uint32_t writeCount = object.Length();
|
2007-03-22 10:30:00 -07:00
|
|
|
rv = mOut->Write(object.get(), writeCount, &wroteCount);
|
|
|
|
NS_ENSURE_TRUE(writeCount == wroteCount, NS_ERROR_FAILURE);
|
|
|
|
}
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
return mOut->Write(".\n", 2, &wroteCount);
|
|
|
|
}
|
|
|
|
|
2012-06-12 20:42:36 -07:00
|
|
|
class rdfTriplesSerializer MOZ_FINAL : public rdfISerializer
|
2007-03-22 10:30:00 -07:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_RDFISERIALIZER
|
|
|
|
|
|
|
|
rdfTriplesSerializer();
|
|
|
|
|
|
|
|
private:
|
|
|
|
~rdfTriplesSerializer();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewTriplesSerializer(rdfISerializer** aResult)
|
|
|
|
{
|
2012-07-30 07:20:58 -07:00
|
|
|
NS_PRECONDITION(aResult != nullptr, "null ptr");
|
2007-03-22 10:30:00 -07:00
|
|
|
if (! aResult)
|
|
|
|
return NS_ERROR_NULL_POINTER;
|
|
|
|
|
|
|
|
*aResult = new rdfTriplesSerializer();
|
|
|
|
if (! *aResult)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
NS_ADDREF(*aResult);
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2014-04-27 00:06:00 -07:00
|
|
|
NS_IMPL_ISUPPORTS(rdfTriplesSerializer, rdfISerializer)
|
2007-03-22 10:30:00 -07:00
|
|
|
|
|
|
|
rdfTriplesSerializer::rdfTriplesSerializer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
rdfTriplesSerializer::~rdfTriplesSerializer()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
rdfTriplesSerializer::Serialize(rdfIDataSource *aDataSource,
|
|
|
|
nsIOutputStream *aOut)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
nsCOMPtr<nsIBufferedOutputStream> bufout =
|
|
|
|
do_CreateInstance(NS_BUFFEREDOUTPUTSTREAM_CONTRACTID, &rv);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
rv = bufout->Init(aOut, 1024);
|
|
|
|
NS_ENSURE_SUCCESS(rv, rv);
|
|
|
|
nsCOMPtr<rdfITripleVisitor> tv = new TriplesVisitor(bufout);
|
|
|
|
NS_ENSURE_TRUE(tv, NS_ERROR_OUT_OF_MEMORY);
|
|
|
|
return aDataSource->VisitAllTriples(tv);
|
|
|
|
}
|