2005-06-24 07:49:44 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
2012-05-21 11:12:37 +00: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/. */
|
2005-06-24 07:49:44 +00: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-13 03:42:36 +00:00
|
|
|
#include "mozilla/Attributes.h"
|
2005-06-24 07:49:44 +00:00
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class TriplesVisitor final : public rdfITripleVisitor
|
2005-06-24 07:49:44 +00:00
|
|
|
{
|
|
|
|
public:
|
2014-09-02 22:21:23 +00:00
|
|
|
explicit TriplesVisitor(nsIOutputStream* aOut) : mOut(aOut) {}
|
2005-06-24 07:49:44 +00:00
|
|
|
NS_DECL_RDFITRIPLEVISITOR
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
protected:
|
2014-06-30 22:11:50 +00:00
|
|
|
~TriplesVisitor() {}
|
2005-06-24 07:49:44 +00:00
|
|
|
nsresult writeResource(nsIRDFResource* aResource);
|
|
|
|
nsIOutputStream* mOut;
|
|
|
|
};
|
|
|
|
|
2014-04-27 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(TriplesVisitor, rdfITripleVisitor)
|
2005-06-24 07:49:44 +00:00
|
|
|
|
|
|
|
nsresult
|
|
|
|
TriplesVisitor::writeResource(nsIRDFResource *aResource)
|
|
|
|
{
|
|
|
|
nsCString res;
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t writeCount, wroteCount;
|
2005-06-24 07:49:44 +00: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-29 06:19:26 +00:00
|
|
|
nsIRDFNode *aObject, bool aTruthValue)
|
2005-06-24 07:49:44 +00: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 15:56:38 +00:00
|
|
|
uint32_t wroteCount;
|
2005-06-24 07:49:44 +00:00
|
|
|
if (res) {
|
|
|
|
rv = writeResource(res);
|
2012-07-30 14:20:58 +00:00
|
|
|
} else if ((lit = do_QueryInterface(aObject)) != nullptr) {
|
2014-01-04 15:02:17 +00:00
|
|
|
const char16_t *value;
|
2005-06-24 07:49:44 +00:00
|
|
|
lit->GetValueConst(&value);
|
2012-09-02 02:35:17 +00:00
|
|
|
nsAutoCString object;
|
2014-05-26 18:54:58 +00:00
|
|
|
object.Append('"');
|
2005-06-24 07:49:44 +00:00
|
|
|
AppendUTF16toUTF8(value, object);
|
|
|
|
object.AppendLiteral("\" ");
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t writeCount = object.Length();
|
2005-06-24 07:49:44 +00:00
|
|
|
rv = mOut->Write(object.get(), writeCount, &wroteCount);
|
|
|
|
NS_ENSURE_TRUE(writeCount == wroteCount, NS_ERROR_FAILURE);
|
2012-07-30 14:20:58 +00:00
|
|
|
} else if ((intLit = do_QueryInterface(aObject)) != nullptr) {
|
2012-08-22 15:56:38 +00:00
|
|
|
int32_t value;
|
2005-06-24 07:49:44 +00:00
|
|
|
intLit->GetValue(&value);
|
|
|
|
nsPrintfCString
|
2012-04-24 18:43:00 +00:00
|
|
|
object("\"%i\"^^<http://www.w3.org/2001/XMLSchema#integer> ",
|
2005-06-24 07:49:44 +00:00
|
|
|
value);
|
2012-08-22 15:56:38 +00:00
|
|
|
uint32_t writeCount = object.Length();
|
2005-06-24 07:49:44 +00: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);
|
|
|
|
}
|
|
|
|
|
2015-03-21 16:28:04 +00:00
|
|
|
class rdfTriplesSerializer final : public rdfISerializer
|
2005-06-24 07:49:44 +00:00
|
|
|
{
|
|
|
|
public:
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
NS_DECL_RDFISERIALIZER
|
|
|
|
|
|
|
|
rdfTriplesSerializer();
|
|
|
|
|
|
|
|
private:
|
|
|
|
~rdfTriplesSerializer();
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
nsresult
|
|
|
|
NS_NewTriplesSerializer(rdfISerializer** aResult)
|
|
|
|
{
|
2012-07-30 14:20:58 +00:00
|
|
|
NS_PRECONDITION(aResult != nullptr, "null ptr");
|
2005-06-24 07:49:44 +00: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 07:06:00 +00:00
|
|
|
NS_IMPL_ISUPPORTS(rdfTriplesSerializer, rdfISerializer)
|
2005-06-24 07:49:44 +00: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);
|
|
|
|
}
|