1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/rdf/tests/rdfpoll/rdfpoll.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,270 @@ 1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +/* 1.10 + 1.11 + A simple test program that reads in RDF/XML into an in-memory data 1.12 + source, then periodically updates it. It prints the messages 1.13 + indicating assert and unassert activity to the console. 1.14 + 1.15 + The program takes two parameters: the URL from which to read, and 1.16 + the poll-interval at which to re-load the . 1.17 + 1.18 + */ 1.19 + 1.20 +#include <stdlib.h> 1.21 +#include <stdio.h> 1.22 +#include "nsXPCOM.h" 1.23 +#include "nsCOMPtr.h" 1.24 +#include "nsIInputStream.h" 1.25 +#include "nsIIOService.h" 1.26 +#include "nsIRDFCompositeDataSource.h" 1.27 +#include "nsIRDFRemoteDataSource.h" 1.28 +#include "nsIRDFNode.h" 1.29 +#include "nsIRDFService.h" 1.30 +#include "nsIRDFXMLSource.h" 1.31 +#include "nsIServiceManager.h" 1.32 +#include "nsServiceManagerUtils.h" 1.33 +#include "nsIStreamListener.h" 1.34 +#include "nsIURL.h" 1.35 +#include "nsRDFCID.h" 1.36 +#include "nsIComponentManager.h" 1.37 +#include "nsComponentManagerUtils.h" 1.38 +#include "nsThreadUtils.h" 1.39 +#include "prthread.h" 1.40 +#include "plstr.h" 1.41 +#include "nsEmbedString.h" 1.42 +#include "nsNetCID.h" 1.43 +#include "prtime.h" 1.44 + 1.45 +//////////////////////////////////////////////////////////////////////// 1.46 +// CIDs 1.47 + 1.48 +// rdf 1.49 +static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID); 1.50 + 1.51 +//////////////////////////////////////////////////////////////////////// 1.52 +// IIDs 1.53 + 1.54 + 1.55 +#include "nsIMemory.h" // for the CID 1.56 + 1.57 +//////////////////////////////////////////////////////////////////////// 1.58 + 1.59 +class Observer : public nsIRDFObserver 1.60 +{ 1.61 +public: 1.62 + Observer(); 1.63 + virtual ~Observer() {} 1.64 + 1.65 + // nsISupports interface 1.66 + NS_DECL_ISUPPORTS 1.67 + 1.68 + // nsIRDFObserver interface 1.69 + NS_DECL_NSIRDFOBSERVER 1.70 +}; 1.71 + 1.72 +Observer::Observer() 1.73 +{ 1.74 +} 1.75 + 1.76 +NS_IMPL_ISUPPORTS(Observer, nsIRDFObserver) 1.77 + 1.78 +static nsresult 1.79 +rdf_WriteOp(const char* aOp, 1.80 + nsIRDFResource* aSource, 1.81 + nsIRDFResource* aProperty, 1.82 + nsIRDFNode* aTarget) 1.83 +{ 1.84 + nsresult rv; 1.85 + 1.86 + nsCString source; 1.87 + rv = aSource->GetValue(getter_Copies(source)); 1.88 + if (NS_FAILED(rv)) return rv; 1.89 + 1.90 + nsCString property; 1.91 + rv = aProperty->GetValue(getter_Copies(property)); 1.92 + if (NS_FAILED(rv)) return rv; 1.93 + 1.94 + nsCOMPtr<nsIRDFResource> resource; 1.95 + nsCOMPtr<nsIRDFLiteral> literal; 1.96 + nsCOMPtr<nsIRDFDate> date; 1.97 + nsCOMPtr<nsIRDFInt> number; 1.98 + 1.99 + printf("%.8s [%s]\n", aOp, source.get()); 1.100 + printf(" --[%s]--\n", property.get()); 1.101 + 1.102 + if ((resource = do_QueryInterface(aTarget)) != nullptr) { 1.103 + nsCString target; 1.104 + rv = resource->GetValue(getter_Copies(target)); 1.105 + if (NS_FAILED(rv)) return rv; 1.106 + 1.107 + printf(" ->[%s]\n", target.get()); 1.108 + } 1.109 + else if ((literal = do_QueryInterface(aTarget)) != nullptr) { 1.110 + nsString target; 1.111 + rv = literal->GetValue(getter_Copies(target)); 1.112 + if (NS_FAILED(rv)) return rv; 1.113 + 1.114 + printf(" ->\"%s\"\n", NS_ConvertUTF16toUTF8(target).get()); 1.115 + } 1.116 + else if ((date = do_QueryInterface(aTarget)) != nullptr) { 1.117 + PRTime value; 1.118 + date->GetValue(&value); 1.119 + 1.120 + PRExplodedTime t; 1.121 + PR_ExplodeTime(value, PR_GMTParameters, &t); 1.122 + 1.123 + printf(" -> %02d/%02d/%04d %02d:%02d:%02d +%06d\n", 1.124 + t.tm_month + 1, 1.125 + t.tm_mday, 1.126 + t.tm_year, 1.127 + t.tm_hour, 1.128 + t.tm_min, 1.129 + t.tm_sec, 1.130 + t.tm_usec); 1.131 + } 1.132 + else if ((number = do_QueryInterface(aTarget)) != nullptr) { 1.133 + int32_t value; 1.134 + number->GetValue(&value); 1.135 + 1.136 + printf(" -> %d\n", value); 1.137 + } 1.138 + else { 1.139 + printf(" -> (unknown node type)\n"); 1.140 + } 1.141 + 1.142 + printf("\n"); 1.143 + return NS_OK; 1.144 +} 1.145 + 1.146 +NS_IMETHODIMP 1.147 +Observer::OnAssert(nsIRDFDataSource* aDataSource, 1.148 + nsIRDFResource* aSource, 1.149 + nsIRDFResource* aProperty, 1.150 + nsIRDFNode* aTarget) 1.151 +{ 1.152 + return rdf_WriteOp("assert", aSource, aProperty, aTarget); 1.153 +} 1.154 + 1.155 + 1.156 +NS_IMETHODIMP 1.157 +Observer::OnUnassert(nsIRDFDataSource* aDataSource, 1.158 + nsIRDFResource* aSource, 1.159 + nsIRDFResource* aProperty, 1.160 + nsIRDFNode* aTarget) 1.161 +{ 1.162 + return rdf_WriteOp("unassert", aSource, aProperty, aTarget); 1.163 +} 1.164 + 1.165 + 1.166 +NS_IMETHODIMP 1.167 +Observer::OnChange(nsIRDFDataSource* aDataSource, 1.168 + nsIRDFResource* aSource, 1.169 + nsIRDFResource* aProperty, 1.170 + nsIRDFNode* aOldTarget, 1.171 + nsIRDFNode* aNewTarget) 1.172 +{ 1.173 + nsresult rv; 1.174 + rv = rdf_WriteOp("chg-from", aSource, aProperty, aOldTarget); 1.175 + if (NS_FAILED(rv)) return rv; 1.176 + 1.177 + rv = rdf_WriteOp("chg-to", aSource, aProperty, aNewTarget); 1.178 + if (NS_FAILED(rv)) return rv; 1.179 + 1.180 + return NS_OK; 1.181 +} 1.182 + 1.183 +NS_IMETHODIMP 1.184 +Observer::OnMove(nsIRDFDataSource* aDataSource, 1.185 + nsIRDFResource* aOldSource, 1.186 + nsIRDFResource* aNewSource, 1.187 + nsIRDFResource* aProperty, 1.188 + nsIRDFNode* aTarget) 1.189 +{ 1.190 + nsresult rv; 1.191 + rv = rdf_WriteOp("mv-from", aOldSource, aProperty, aTarget); 1.192 + if (NS_FAILED(rv)) return rv; 1.193 + 1.194 + rv = rdf_WriteOp("mv-to", aNewSource, aProperty, aTarget); 1.195 + if (NS_FAILED(rv)) return rv; 1.196 + 1.197 + return NS_OK; 1.198 +} 1.199 + 1.200 +NS_IMETHODIMP 1.201 +Observer::OnBeginUpdateBatch(nsIRDFDataSource* aDataSource) 1.202 +{ 1.203 + return NS_OK; 1.204 +} 1.205 + 1.206 +NS_IMETHODIMP 1.207 +Observer::OnEndUpdateBatch(nsIRDFDataSource* aDataSource) 1.208 +{ 1.209 + return NS_OK; 1.210 +} 1.211 + 1.212 +//////////////////////////////////////////////////////////////////////// 1.213 + 1.214 +int 1.215 +main(int argc, char** argv) 1.216 +{ 1.217 + nsresult rv; 1.218 + 1.219 + if (argc < 2) { 1.220 + fprintf(stderr, "usage: %s <url> [<poll-interval>]\n", argv[0]); 1.221 + return 1; 1.222 + } 1.223 + 1.224 + rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); 1.225 + if (NS_FAILED(rv)) { 1.226 + fprintf(stderr, "NS_InitXPCOM2 failed\n"); 1.227 + return 1; 1.228 + } 1.229 + 1.230 + // Create a stream data source and initialize it on argv[1], which 1.231 + // is hopefully a "file:" URL. (Actually, we can do _any_ kind of 1.232 + // URL, but only a "file:" URL will be written back to disk.) 1.233 + nsCOMPtr<nsIRDFDataSource> ds = do_CreateInstance(kRDFXMLDataSourceCID, &rv); 1.234 + if (NS_FAILED(rv)) { 1.235 + NS_ERROR("unable to create RDF/XML data source"); 1.236 + return 1; 1.237 + } 1.238 + 1.239 + nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(ds); 1.240 + if (! remote) 1.241 + return 1; 1.242 + 1.243 + rv = remote->Init(argv[1]); 1.244 + NS_ASSERTION(NS_SUCCEEDED(rv), "unable to initialize data source"); 1.245 + if (NS_FAILED(rv)) return 1; 1.246 + 1.247 + // The do_QI() on the pointer is a hack to make sure that the new 1.248 + // object gets AddRef()-ed. 1.249 + nsCOMPtr<nsIRDFObserver> observer = do_QueryInterface(new Observer); 1.250 + if (! observer) 1.251 + return 1; 1.252 + 1.253 + rv = ds->AddObserver(observer); 1.254 + if (NS_FAILED(rv)) return 1; 1.255 + 1.256 + while (1) { 1.257 + // Okay, this should load the XML file... 1.258 + rv = remote->Refresh(true); 1.259 + NS_ASSERTION(NS_SUCCEEDED(rv), "unable to open datasource"); 1.260 + if (NS_FAILED(rv)) return 1; 1.261 + 1.262 + if (argc <= 2) 1.263 + break; 1.264 + 1.265 + int32_t pollinterval = atol(argv[2]); 1.266 + if (! pollinterval) 1.267 + break; 1.268 + 1.269 + PR_Sleep(PR_SecondsToInterval(pollinterval)); 1.270 + } 1.271 + 1.272 + return 0; 1.273 +}