LCOV - code coverage report
Current view: directory - rdf/base/src - rdfTriplesSerializer.cpp (source / functions) Found Hit Coverage
Test: app.info Lines: 68 0 0.0 %
Date: 2012-06-02 Functions: 13 0 0.0 %

       1                 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
       2                 : /* ***** BEGIN LICENSE BLOCK *****
       3                 :  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
       4                 :  *
       5                 :  * The contents of this file are subject to the Mozilla Public License Version
       6                 :  * 1.1 (the "License"); you may not use this file except in compliance with
       7                 :  * the License. You may obtain a copy of the License at
       8                 :  * http://www.mozilla.org/MPL/
       9                 :  *
      10                 :  * Software distributed under the License is distributed on an "AS IS" basis,
      11                 :  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
      12                 :  * for the specific language governing rights and limitations under the
      13                 :  * License.
      14                 :  *
      15                 :  * The Original Code is Mozilla.
      16                 :  *
      17                 :  * The Initial Developer of the Original Code is
      18                 :  * Axel Hecht.
      19                 :  * Portions created by the Initial Developer are Copyright (C) 2005
      20                 :  * the Initial Developer. All Rights Reserved.
      21                 :  *
      22                 :  * Contributor(s):
      23                 :  *  Axel Hecht <axel@pike.org>
      24                 :  *
      25                 :  * Alternatively, the contents of this file may be used under the terms of
      26                 :  * either the GNU General Public License Version 2 or later (the "GPL"), or
      27                 :  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
      28                 :  * in which case the provisions of the GPL or the LGPL are applicable instead
      29                 :  * of those above. If you wish to allow use of your version of this file only
      30                 :  * under the terms of either the GPL or the LGPL, and not to allow others to
      31                 :  * use your version of this file under the terms of the MPL, indicate your
      32                 :  * decision by deleting the provisions above and replace them with the notice
      33                 :  * and other provisions required by the GPL or the LGPL. If you do not delete
      34                 :  * the provisions above, a recipient may use your version of this file under
      35                 :  * the terms of any one of the MPL, the GPL or the LGPL.
      36                 :  *
      37                 :  * ***** END LICENSE BLOCK ***** */
      38                 : 
      39                 : #include "nsIOutputStream.h"
      40                 : #include "nsReadableUtils.h"
      41                 : #include "nsCRT.h"
      42                 : #include "nsCOMPtr.h"
      43                 : #include "nsString.h"
      44                 : #include "nsPrintfCString.h"
      45                 : #include "nsIBufferedStreams.h"
      46                 : #include "nsNetCID.h"
      47                 : #include "nsComponentManagerUtils.h"
      48                 : 
      49                 : #include "rdfISerializer.h"
      50                 : #include "rdfIDataSource.h"
      51                 : #include "rdfITripleVisitor.h"
      52                 : 
      53                 : #include "nsIRDFResource.h"
      54                 : #include "nsIRDFLiteral.h"
      55                 : 
      56                 : class TriplesVisitor : public rdfITripleVisitor
      57                 : {
      58                 : public:
      59               0 :     TriplesVisitor(nsIOutputStream* aOut) : mOut(aOut) {}
      60                 :     NS_DECL_RDFITRIPLEVISITOR
      61                 :     NS_DECL_ISUPPORTS
      62                 : protected:
      63                 :     nsresult writeResource(nsIRDFResource* aResource);
      64                 :     nsIOutputStream* mOut;
      65                 : };
      66                 : 
      67               0 : NS_IMPL_ISUPPORTS1(TriplesVisitor, rdfITripleVisitor)
      68                 : 
      69                 : nsresult
      70               0 : TriplesVisitor::writeResource(nsIRDFResource *aResource)
      71                 : {
      72               0 :     nsCString res;
      73                 :     PRUint32 writeCount, wroteCount;
      74               0 :     mOut->Write("<", 1, &wroteCount);
      75               0 :     NS_ENSURE_TRUE(wroteCount == 1, NS_ERROR_FAILURE);
      76               0 :     nsresult rv = aResource->GetValueUTF8(res);
      77               0 :     NS_ENSURE_SUCCESS(rv, rv);
      78               0 :     writeCount = res.Length();
      79               0 :     mOut->Write(res.get(), writeCount, &wroteCount);
      80               0 :     NS_ENSURE_TRUE(writeCount == wroteCount, NS_ERROR_FAILURE);
      81               0 :     mOut->Write("> ", 2, &wroteCount);
      82               0 :     NS_ENSURE_TRUE(wroteCount == 2, NS_ERROR_FAILURE);
      83               0 :     return NS_OK;
      84                 : }
      85                 : 
      86                 : NS_IMETHODIMP
      87               0 : TriplesVisitor::Visit(nsIRDFNode *aSubject, nsIRDFResource *aPredicate,
      88                 :                       nsIRDFNode *aObject, bool aTruthValue)
      89                 : {
      90               0 :     nsCOMPtr<nsIRDFResource> subjectRes = do_QueryInterface(aSubject);
      91               0 :     nsresult rv = NS_OK;
      92               0 :     if (subjectRes) {
      93               0 :         rv = writeResource(subjectRes);
      94                 :     }
      95               0 :     if (NS_FAILED(rv)) {
      96               0 :         return rv;
      97                 :     }
      98               0 :     rv = writeResource(aPredicate);
      99               0 :     if (NS_FAILED(rv)) {
     100               0 :         return rv;
     101                 :     }
     102               0 :     nsCOMPtr<nsIRDFResource> res = do_QueryInterface(aObject);
     103               0 :     nsCOMPtr<nsIRDFLiteral> lit;
     104               0 :     nsCOMPtr<nsIRDFInt> intLit;
     105                 :     PRUint32 wroteCount;
     106               0 :     if (res) {
     107               0 :         rv = writeResource(res);
     108               0 :     } else if ((lit = do_QueryInterface(aObject)) != nsnull) {
     109                 :         const PRUnichar *value;
     110               0 :         lit->GetValueConst(&value);
     111               0 :         nsCAutoString object;
     112               0 :         object.AppendLiteral("\"");
     113               0 :         AppendUTF16toUTF8(value, object);
     114               0 :         object.AppendLiteral("\" ");
     115               0 :         PRUint32 writeCount = object.Length();
     116               0 :         rv = mOut->Write(object.get(), writeCount, &wroteCount);
     117               0 :         NS_ENSURE_TRUE(writeCount == wroteCount, NS_ERROR_FAILURE);
     118               0 :     } else if ((intLit = do_QueryInterface(aObject)) != nsnull) {
     119                 :         PRInt32 value;
     120               0 :         intLit->GetValue(&value);
     121                 :         nsPrintfCString
     122                 :             object(128,
     123                 :                    "\"%i\"^^<http://www.w3.org/2001/XMLSchema#integer> ",
     124               0 :                    value);
     125               0 :         PRUint32 writeCount = object.Length();
     126               0 :         rv = mOut->Write(object.get(), writeCount, &wroteCount);
     127               0 :         NS_ENSURE_TRUE(writeCount == wroteCount, NS_ERROR_FAILURE);
     128                 :     }
     129               0 :     NS_ENSURE_SUCCESS(rv, rv);
     130               0 :     return mOut->Write(".\n", 2, &wroteCount);
     131                 : }
     132                 : 
     133                 : class rdfTriplesSerializer : public rdfISerializer
     134                 : {
     135                 : public:
     136                 :   NS_DECL_ISUPPORTS
     137                 :   NS_DECL_RDFISERIALIZER
     138                 : 
     139                 :   rdfTriplesSerializer();
     140                 : 
     141                 : private:
     142                 :   ~rdfTriplesSerializer();
     143                 : 
     144                 : };
     145                 : 
     146                 : nsresult
     147               0 : NS_NewTriplesSerializer(rdfISerializer** aResult)
     148                 : {
     149               0 :     NS_PRECONDITION(aResult != nsnull, "null ptr");
     150               0 :     if (! aResult)
     151               0 :         return NS_ERROR_NULL_POINTER;
     152                 : 
     153               0 :     *aResult = new rdfTriplesSerializer();
     154               0 :     if (! *aResult)
     155               0 :         return NS_ERROR_OUT_OF_MEMORY;
     156               0 :     NS_ADDREF(*aResult);
     157               0 :     return NS_OK;
     158                 : }
     159                 : 
     160               0 : NS_IMPL_ISUPPORTS1(rdfTriplesSerializer, rdfISerializer)
     161                 : 
     162               0 : rdfTriplesSerializer::rdfTriplesSerializer()
     163                 : {
     164               0 : }
     165                 : 
     166               0 : rdfTriplesSerializer::~rdfTriplesSerializer()
     167                 : {
     168               0 : }
     169                 : 
     170                 : NS_IMETHODIMP
     171               0 : rdfTriplesSerializer::Serialize(rdfIDataSource *aDataSource,
     172                 :                                 nsIOutputStream *aOut)
     173                 : {
     174                 :     nsresult rv;
     175                 :     nsCOMPtr<nsIBufferedOutputStream> bufout = 
     176               0 :         do_CreateInstance(NS_BUFFEREDOUTPUTSTREAM_CONTRACTID, &rv);
     177               0 :     NS_ENSURE_SUCCESS(rv, rv);
     178               0 :     rv = bufout->Init(aOut, 1024);
     179               0 :     NS_ENSURE_SUCCESS(rv, rv);
     180               0 :     nsCOMPtr<rdfITripleVisitor> tv = new TriplesVisitor(bufout);
     181               0 :     NS_ENSURE_TRUE(tv, NS_ERROR_OUT_OF_MEMORY);
     182               0 :     return aDataSource->VisitAllTriples(tv);
     183                 : }

Generated by: LCOV version 1.7