Back to index

lightning-sunbird  0.9+nobinonly
rdfTriplesSerializer.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is Mozilla.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Axel Hecht.
00019  * Portions created by the Initial Developer are Copyright (C) 2005
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *  Axel Hecht <axel@pike.org>
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 #include "nsIOutputStream.h"
00040 #include "nsReadableUtils.h"
00041 #include "nsCRT.h"
00042 #include "nsCOMPtr.h"
00043 #include "nsString.h"
00044 #include "nsPrintfCString.h"
00045 #include "nsIBufferedStreams.h"
00046 #include "nsNetCID.h"
00047 #include "nsComponentManagerUtils.h"
00048 
00049 #include "rdfISerializer.h"
00050 #include "rdfIDataSource.h"
00051 #include "rdfITripleVisitor.h"
00052 
00053 #include "nsIRDFResource.h"
00054 #include "nsIRDFLiteral.h"
00055 
00056 class TriplesVisitor : public rdfITripleVisitor
00057 {
00058 public:
00059     TriplesVisitor(nsIOutputStream* aOut) : mOut(aOut) {};
00060     NS_DECL_RDFITRIPLEVISITOR
00061     NS_DECL_ISUPPORTS
00062 protected:
00063     nsresult writeResource(nsIRDFResource* aResource);
00064     nsIOutputStream* mOut;
00065 };
00066 
00067 NS_IMPL_ISUPPORTS1(TriplesVisitor, rdfITripleVisitor)
00068 
00069 nsresult
00070 TriplesVisitor::writeResource(nsIRDFResource *aResource)
00071 {
00072     nsCString res;
00073     PRUint32 writeCount, wroteCount;
00074     mOut->Write("<", 1, &wroteCount);
00075     NS_ENSURE_TRUE(wroteCount == 1, NS_ERROR_FAILURE);
00076     nsresult rv = aResource->GetValueUTF8(res);
00077     NS_ENSURE_SUCCESS(rv, rv);
00078     writeCount = res.Length();
00079     mOut->Write(res.get(), writeCount, &wroteCount);
00080     NS_ENSURE_TRUE(writeCount == wroteCount, NS_ERROR_FAILURE);
00081     mOut->Write("> ", 2, &wroteCount);
00082     NS_ENSURE_TRUE(wroteCount == 2, NS_ERROR_FAILURE);
00083     return NS_OK;
00084 }
00085 
00086 NS_IMETHODIMP
00087 TriplesVisitor::Visit(nsIRDFNode *aSubject, nsIRDFResource *aPredicate,
00088                       nsIRDFNode *aObject, PRBool aTruthValue)
00089 {
00090     nsCOMPtr<nsIRDFResource> subjectRes = do_QueryInterface(aSubject);
00091     nsresult rv = NS_OK;
00092     if (subjectRes) {
00093         rv = writeResource(subjectRes);
00094     }
00095     if (NS_FAILED(rv)) {
00096         return rv;
00097     }
00098     rv = writeResource(aPredicate);
00099     if (NS_FAILED(rv)) {
00100         return rv;
00101     }
00102     nsCOMPtr<nsIRDFResource> res = do_QueryInterface(aObject);
00103     nsCOMPtr<nsIRDFLiteral> lit;
00104     nsCOMPtr<nsIRDFInt> intLit;
00105     PRUint32 wroteCount;
00106     if (res) {
00107         rv = writeResource(res);
00108     } else if ((lit = do_QueryInterface(aObject)) != nsnull) {
00109         const PRUnichar *value;
00110         lit->GetValueConst(&value);
00111         nsCAutoString object;
00112         object.AppendLiteral("\"");
00113         AppendUTF16toUTF8(value, object);
00114         object.AppendLiteral("\" ");
00115         PRUint32 writeCount = object.Length();
00116         rv = mOut->Write(object.get(), writeCount, &wroteCount);
00117         NS_ENSURE_TRUE(writeCount == wroteCount, NS_ERROR_FAILURE);
00118     } else if ((intLit = do_QueryInterface(aObject)) != nsnull) {
00119         PRInt32 value;
00120         intLit->GetValue(&value);
00121         nsPrintfCString
00122             object(128,
00123                    "\"%i\"^^<http://www.w3.org/2001/XMLSchema#integer> ",
00124                    value);
00125         PRUint32 writeCount = object.Length();
00126         rv = mOut->Write(object.get(), writeCount, &wroteCount);
00127         NS_ENSURE_TRUE(writeCount == wroteCount, NS_ERROR_FAILURE);
00128     }
00129     NS_ENSURE_SUCCESS(rv, rv);
00130     return mOut->Write(".\n", 2, &wroteCount);
00131 }
00132 
00133 class rdfTriplesSerializer : public rdfISerializer
00134 {
00135 public:
00136   NS_DECL_ISUPPORTS
00137   NS_DECL_RDFISERIALIZER
00138 
00139   rdfTriplesSerializer();
00140 
00141 private:
00142   ~rdfTriplesSerializer();
00143 
00144 };
00145 
00146 nsresult
00147 NS_NewTriplesSerializer(rdfISerializer** aResult)
00148 {
00149     NS_PRECONDITION(aResult != nsnull, "null ptr");
00150     if (! aResult)
00151         return NS_ERROR_NULL_POINTER;
00152 
00153     *aResult = new rdfTriplesSerializer();
00154     if (! *aResult)
00155         return NS_ERROR_OUT_OF_MEMORY;
00156     NS_ADDREF(*aResult);
00157     return NS_OK;
00158 }
00159 
00160 NS_IMPL_ISUPPORTS1(rdfTriplesSerializer, rdfISerializer)
00161 
00162 rdfTriplesSerializer::rdfTriplesSerializer()
00163 {
00164 }
00165 
00166 rdfTriplesSerializer::~rdfTriplesSerializer()
00167 {
00168 }
00169 
00170 NS_IMETHODIMP
00171 rdfTriplesSerializer::Serialize(rdfIDataSource *aDataSource,
00172                                 nsIOutputStream *aOut)
00173 {
00174     nsresult rv;
00175     nsCOMPtr<nsIBufferedOutputStream> bufout = 
00176         do_CreateInstance(NS_BUFFEREDOUTPUTSTREAM_CONTRACTID, &rv);
00177     NS_ENSURE_SUCCESS(rv, rv);
00178     rv = bufout->Init(aOut, 1024);
00179     NS_ENSURE_SUCCESS(rv, rv);
00180     nsCOMPtr<rdfITripleVisitor> tv = new TriplesVisitor(bufout);
00181     NS_ENSURE_TRUE(tv, NS_ERROR_OUT_OF_MEMORY);
00182     return aDataSource->VisitAllTriples(tv);
00183 }