rdf/tests/rdfpoll/rdfpoll.cpp

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 /*
michael@0 7
michael@0 8 A simple test program that reads in RDF/XML into an in-memory data
michael@0 9 source, then periodically updates it. It prints the messages
michael@0 10 indicating assert and unassert activity to the console.
michael@0 11
michael@0 12 The program takes two parameters: the URL from which to read, and
michael@0 13 the poll-interval at which to re-load the .
michael@0 14
michael@0 15 */
michael@0 16
michael@0 17 #include <stdlib.h>
michael@0 18 #include <stdio.h>
michael@0 19 #include "nsXPCOM.h"
michael@0 20 #include "nsCOMPtr.h"
michael@0 21 #include "nsIInputStream.h"
michael@0 22 #include "nsIIOService.h"
michael@0 23 #include "nsIRDFCompositeDataSource.h"
michael@0 24 #include "nsIRDFRemoteDataSource.h"
michael@0 25 #include "nsIRDFNode.h"
michael@0 26 #include "nsIRDFService.h"
michael@0 27 #include "nsIRDFXMLSource.h"
michael@0 28 #include "nsIServiceManager.h"
michael@0 29 #include "nsServiceManagerUtils.h"
michael@0 30 #include "nsIStreamListener.h"
michael@0 31 #include "nsIURL.h"
michael@0 32 #include "nsRDFCID.h"
michael@0 33 #include "nsIComponentManager.h"
michael@0 34 #include "nsComponentManagerUtils.h"
michael@0 35 #include "nsThreadUtils.h"
michael@0 36 #include "prthread.h"
michael@0 37 #include "plstr.h"
michael@0 38 #include "nsEmbedString.h"
michael@0 39 #include "nsNetCID.h"
michael@0 40 #include "prtime.h"
michael@0 41
michael@0 42 ////////////////////////////////////////////////////////////////////////
michael@0 43 // CIDs
michael@0 44
michael@0 45 // rdf
michael@0 46 static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
michael@0 47
michael@0 48 ////////////////////////////////////////////////////////////////////////
michael@0 49 // IIDs
michael@0 50
michael@0 51
michael@0 52 #include "nsIMemory.h" // for the CID
michael@0 53
michael@0 54 ////////////////////////////////////////////////////////////////////////
michael@0 55
michael@0 56 class Observer : public nsIRDFObserver
michael@0 57 {
michael@0 58 public:
michael@0 59 Observer();
michael@0 60 virtual ~Observer() {}
michael@0 61
michael@0 62 // nsISupports interface
michael@0 63 NS_DECL_ISUPPORTS
michael@0 64
michael@0 65 // nsIRDFObserver interface
michael@0 66 NS_DECL_NSIRDFOBSERVER
michael@0 67 };
michael@0 68
michael@0 69 Observer::Observer()
michael@0 70 {
michael@0 71 }
michael@0 72
michael@0 73 NS_IMPL_ISUPPORTS(Observer, nsIRDFObserver)
michael@0 74
michael@0 75 static nsresult
michael@0 76 rdf_WriteOp(const char* aOp,
michael@0 77 nsIRDFResource* aSource,
michael@0 78 nsIRDFResource* aProperty,
michael@0 79 nsIRDFNode* aTarget)
michael@0 80 {
michael@0 81 nsresult rv;
michael@0 82
michael@0 83 nsCString source;
michael@0 84 rv = aSource->GetValue(getter_Copies(source));
michael@0 85 if (NS_FAILED(rv)) return rv;
michael@0 86
michael@0 87 nsCString property;
michael@0 88 rv = aProperty->GetValue(getter_Copies(property));
michael@0 89 if (NS_FAILED(rv)) return rv;
michael@0 90
michael@0 91 nsCOMPtr<nsIRDFResource> resource;
michael@0 92 nsCOMPtr<nsIRDFLiteral> literal;
michael@0 93 nsCOMPtr<nsIRDFDate> date;
michael@0 94 nsCOMPtr<nsIRDFInt> number;
michael@0 95
michael@0 96 printf("%.8s [%s]\n", aOp, source.get());
michael@0 97 printf(" --[%s]--\n", property.get());
michael@0 98
michael@0 99 if ((resource = do_QueryInterface(aTarget)) != nullptr) {
michael@0 100 nsCString target;
michael@0 101 rv = resource->GetValue(getter_Copies(target));
michael@0 102 if (NS_FAILED(rv)) return rv;
michael@0 103
michael@0 104 printf(" ->[%s]\n", target.get());
michael@0 105 }
michael@0 106 else if ((literal = do_QueryInterface(aTarget)) != nullptr) {
michael@0 107 nsString target;
michael@0 108 rv = literal->GetValue(getter_Copies(target));
michael@0 109 if (NS_FAILED(rv)) return rv;
michael@0 110
michael@0 111 printf(" ->\"%s\"\n", NS_ConvertUTF16toUTF8(target).get());
michael@0 112 }
michael@0 113 else if ((date = do_QueryInterface(aTarget)) != nullptr) {
michael@0 114 PRTime value;
michael@0 115 date->GetValue(&value);
michael@0 116
michael@0 117 PRExplodedTime t;
michael@0 118 PR_ExplodeTime(value, PR_GMTParameters, &t);
michael@0 119
michael@0 120 printf(" -> %02d/%02d/%04d %02d:%02d:%02d +%06d\n",
michael@0 121 t.tm_month + 1,
michael@0 122 t.tm_mday,
michael@0 123 t.tm_year,
michael@0 124 t.tm_hour,
michael@0 125 t.tm_min,
michael@0 126 t.tm_sec,
michael@0 127 t.tm_usec);
michael@0 128 }
michael@0 129 else if ((number = do_QueryInterface(aTarget)) != nullptr) {
michael@0 130 int32_t value;
michael@0 131 number->GetValue(&value);
michael@0 132
michael@0 133 printf(" -> %d\n", value);
michael@0 134 }
michael@0 135 else {
michael@0 136 printf(" -> (unknown node type)\n");
michael@0 137 }
michael@0 138
michael@0 139 printf("\n");
michael@0 140 return NS_OK;
michael@0 141 }
michael@0 142
michael@0 143 NS_IMETHODIMP
michael@0 144 Observer::OnAssert(nsIRDFDataSource* aDataSource,
michael@0 145 nsIRDFResource* aSource,
michael@0 146 nsIRDFResource* aProperty,
michael@0 147 nsIRDFNode* aTarget)
michael@0 148 {
michael@0 149 return rdf_WriteOp("assert", aSource, aProperty, aTarget);
michael@0 150 }
michael@0 151
michael@0 152
michael@0 153 NS_IMETHODIMP
michael@0 154 Observer::OnUnassert(nsIRDFDataSource* aDataSource,
michael@0 155 nsIRDFResource* aSource,
michael@0 156 nsIRDFResource* aProperty,
michael@0 157 nsIRDFNode* aTarget)
michael@0 158 {
michael@0 159 return rdf_WriteOp("unassert", aSource, aProperty, aTarget);
michael@0 160 }
michael@0 161
michael@0 162
michael@0 163 NS_IMETHODIMP
michael@0 164 Observer::OnChange(nsIRDFDataSource* aDataSource,
michael@0 165 nsIRDFResource* aSource,
michael@0 166 nsIRDFResource* aProperty,
michael@0 167 nsIRDFNode* aOldTarget,
michael@0 168 nsIRDFNode* aNewTarget)
michael@0 169 {
michael@0 170 nsresult rv;
michael@0 171 rv = rdf_WriteOp("chg-from", aSource, aProperty, aOldTarget);
michael@0 172 if (NS_FAILED(rv)) return rv;
michael@0 173
michael@0 174 rv = rdf_WriteOp("chg-to", aSource, aProperty, aNewTarget);
michael@0 175 if (NS_FAILED(rv)) return rv;
michael@0 176
michael@0 177 return NS_OK;
michael@0 178 }
michael@0 179
michael@0 180 NS_IMETHODIMP
michael@0 181 Observer::OnMove(nsIRDFDataSource* aDataSource,
michael@0 182 nsIRDFResource* aOldSource,
michael@0 183 nsIRDFResource* aNewSource,
michael@0 184 nsIRDFResource* aProperty,
michael@0 185 nsIRDFNode* aTarget)
michael@0 186 {
michael@0 187 nsresult rv;
michael@0 188 rv = rdf_WriteOp("mv-from", aOldSource, aProperty, aTarget);
michael@0 189 if (NS_FAILED(rv)) return rv;
michael@0 190
michael@0 191 rv = rdf_WriteOp("mv-to", aNewSource, aProperty, aTarget);
michael@0 192 if (NS_FAILED(rv)) return rv;
michael@0 193
michael@0 194 return NS_OK;
michael@0 195 }
michael@0 196
michael@0 197 NS_IMETHODIMP
michael@0 198 Observer::OnBeginUpdateBatch(nsIRDFDataSource* aDataSource)
michael@0 199 {
michael@0 200 return NS_OK;
michael@0 201 }
michael@0 202
michael@0 203 NS_IMETHODIMP
michael@0 204 Observer::OnEndUpdateBatch(nsIRDFDataSource* aDataSource)
michael@0 205 {
michael@0 206 return NS_OK;
michael@0 207 }
michael@0 208
michael@0 209 ////////////////////////////////////////////////////////////////////////
michael@0 210
michael@0 211 int
michael@0 212 main(int argc, char** argv)
michael@0 213 {
michael@0 214 nsresult rv;
michael@0 215
michael@0 216 if (argc < 2) {
michael@0 217 fprintf(stderr, "usage: %s <url> [<poll-interval>]\n", argv[0]);
michael@0 218 return 1;
michael@0 219 }
michael@0 220
michael@0 221 rv = NS_InitXPCOM2(nullptr, nullptr, nullptr);
michael@0 222 if (NS_FAILED(rv)) {
michael@0 223 fprintf(stderr, "NS_InitXPCOM2 failed\n");
michael@0 224 return 1;
michael@0 225 }
michael@0 226
michael@0 227 // Create a stream data source and initialize it on argv[1], which
michael@0 228 // is hopefully a "file:" URL. (Actually, we can do _any_ kind of
michael@0 229 // URL, but only a "file:" URL will be written back to disk.)
michael@0 230 nsCOMPtr<nsIRDFDataSource> ds = do_CreateInstance(kRDFXMLDataSourceCID, &rv);
michael@0 231 if (NS_FAILED(rv)) {
michael@0 232 NS_ERROR("unable to create RDF/XML data source");
michael@0 233 return 1;
michael@0 234 }
michael@0 235
michael@0 236 nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(ds);
michael@0 237 if (! remote)
michael@0 238 return 1;
michael@0 239
michael@0 240 rv = remote->Init(argv[1]);
michael@0 241 NS_ASSERTION(NS_SUCCEEDED(rv), "unable to initialize data source");
michael@0 242 if (NS_FAILED(rv)) return 1;
michael@0 243
michael@0 244 // The do_QI() on the pointer is a hack to make sure that the new
michael@0 245 // object gets AddRef()-ed.
michael@0 246 nsCOMPtr<nsIRDFObserver> observer = do_QueryInterface(new Observer);
michael@0 247 if (! observer)
michael@0 248 return 1;
michael@0 249
michael@0 250 rv = ds->AddObserver(observer);
michael@0 251 if (NS_FAILED(rv)) return 1;
michael@0 252
michael@0 253 while (1) {
michael@0 254 // Okay, this should load the XML file...
michael@0 255 rv = remote->Refresh(true);
michael@0 256 NS_ASSERTION(NS_SUCCEEDED(rv), "unable to open datasource");
michael@0 257 if (NS_FAILED(rv)) return 1;
michael@0 258
michael@0 259 if (argc <= 2)
michael@0 260 break;
michael@0 261
michael@0 262 int32_t pollinterval = atol(argv[2]);
michael@0 263 if (! pollinterval)
michael@0 264 break;
michael@0 265
michael@0 266 PR_Sleep(PR_SecondsToInterval(pollinterval));
michael@0 267 }
michael@0 268
michael@0 269 return 0;
michael@0 270 }

mercurial