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
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/
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
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or 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.
37 * ***** END LICENSE BLOCK ***** */
41 A simple test program that reads in RDF/XML into an in-memory data
42 source, then periodically updates it. It prints the messages
43 indicating assert and unassert activity to the console.
45 The program takes two parameters: the URL from which to read, and
46 the poll-interval at which to re-load the .
54 #include "nsIInputStream.h"
55 #include "nsIIOService.h"
56 #include "nsIRDFCompositeDataSource.h"
57 #include "nsIRDFRemoteDataSource.h"
58 #include "nsIRDFNode.h"
59 #include "nsIRDFService.h"
60 #include "nsIRDFXMLSource.h"
61 #include "nsIServiceManager.h"
62 #include "nsServiceManagerUtils.h"
63 #include "nsIStreamListener.h"
66 #include "nsIComponentManager.h"
67 #include "nsComponentManagerUtils.h"
68 #include "nsThreadUtils.h"
71 #include "nsEmbedString.h"
74 ////////////////////////////////////////////////////////////////////////
78 static NS_DEFINE_CID(kRDFXMLDataSourceCID
, NS_RDFXMLDATASOURCE_CID
);
80 ////////////////////////////////////////////////////////////////////////
84 #include "nsIMemory.h" // for the CID
86 ////////////////////////////////////////////////////////////////////////
88 class Observer
: public nsIRDFObserver
92 virtual ~Observer() {}
94 // nsISupports interface
97 // nsIRDFObserver interface
98 NS_DECL_NSIRDFOBSERVER
105 NS_IMPL_ISUPPORTS1(Observer
, nsIRDFObserver
)
108 rdf_WriteOp(const char* aOp
,
109 nsIRDFResource
* aSource
,
110 nsIRDFResource
* aProperty
,
116 rv
= aSource
->GetValue(getter_Copies(source
));
117 if (NS_FAILED(rv
)) return rv
;
120 rv
= aProperty
->GetValue(getter_Copies(property
));
121 if (NS_FAILED(rv
)) return rv
;
123 nsCOMPtr
<nsIRDFResource
> resource
;
124 nsCOMPtr
<nsIRDFLiteral
> literal
;
125 nsCOMPtr
<nsIRDFDate
> date
;
126 nsCOMPtr
<nsIRDFInt
> number
;
128 printf("%.8s [%s]\n", aOp
, source
.get());
129 printf(" --[%s]--\n", property
.get());
131 if ((resource
= do_QueryInterface(aTarget
)) != nsnull
) {
133 rv
= resource
->GetValue(getter_Copies(target
));
134 if (NS_FAILED(rv
)) return rv
;
136 printf(" ->[%s]\n", target
.get());
138 else if ((literal
= do_QueryInterface(aTarget
)) != nsnull
) {
140 rv
= literal
->GetValue(getter_Copies(target
));
141 if (NS_FAILED(rv
)) return rv
;
143 printf(" ->\"%s\"\n", NS_ConvertUTF16toUTF8(target
).get());
145 else if ((date
= do_QueryInterface(aTarget
)) != nsnull
) {
147 date
->GetValue(&value
);
150 PR_ExplodeTime(value
, PR_GMTParameters
, &t
);
152 printf(" -> %02d/%02d/%04d %02d:%02d:%02d +%06d\n",
161 else if ((number
= do_QueryInterface(aTarget
)) != nsnull
) {
163 number
->GetValue(&value
);
165 printf(" -> %d\n", value
);
168 printf(" -> (unknown node type)\n");
176 Observer::OnAssert(nsIRDFDataSource
* aDataSource
,
177 nsIRDFResource
* aSource
,
178 nsIRDFResource
* aProperty
,
181 return rdf_WriteOp("assert", aSource
, aProperty
, aTarget
);
186 Observer::OnUnassert(nsIRDFDataSource
* aDataSource
,
187 nsIRDFResource
* aSource
,
188 nsIRDFResource
* aProperty
,
191 return rdf_WriteOp("unassert", aSource
, aProperty
, aTarget
);
196 Observer::OnChange(nsIRDFDataSource
* aDataSource
,
197 nsIRDFResource
* aSource
,
198 nsIRDFResource
* aProperty
,
199 nsIRDFNode
* aOldTarget
,
200 nsIRDFNode
* aNewTarget
)
203 rv
= rdf_WriteOp("chg-from", aSource
, aProperty
, aOldTarget
);
204 if (NS_FAILED(rv
)) return rv
;
206 rv
= rdf_WriteOp("chg-to", aSource
, aProperty
, aNewTarget
);
207 if (NS_FAILED(rv
)) return rv
;
213 Observer::OnMove(nsIRDFDataSource
* aDataSource
,
214 nsIRDFResource
* aOldSource
,
215 nsIRDFResource
* aNewSource
,
216 nsIRDFResource
* aProperty
,
220 rv
= rdf_WriteOp("mv-from", aOldSource
, aProperty
, aTarget
);
221 if (NS_FAILED(rv
)) return rv
;
223 rv
= rdf_WriteOp("mv-to", aNewSource
, aProperty
, aTarget
);
224 if (NS_FAILED(rv
)) return rv
;
230 Observer::OnBeginUpdateBatch(nsIRDFDataSource
* aDataSource
)
236 Observer::OnEndUpdateBatch(nsIRDFDataSource
* aDataSource
)
241 ////////////////////////////////////////////////////////////////////////
244 main(int argc
, char** argv
)
249 fprintf(stderr
, "usage: %s <url> [<poll-interval>]\n", argv
[0]);
253 rv
= NS_InitXPCOM2(nsnull
, nsnull
, nsnull
);
255 fprintf(stderr
, "NS_InitXPCOM2 failed\n");
259 // Create a stream data source and initialize it on argv[1], which
260 // is hopefully a "file:" URL. (Actually, we can do _any_ kind of
261 // URL, but only a "file:" URL will be written back to disk.)
262 nsCOMPtr
<nsIRDFDataSource
> ds
= do_CreateInstance(kRDFXMLDataSourceCID
, &rv
);
264 NS_ERROR("unable to create RDF/XML data source");
268 nsCOMPtr
<nsIRDFRemoteDataSource
> remote
= do_QueryInterface(ds
);
270 return NS_ERROR_UNEXPECTED
;
272 rv
= remote
->Init(argv
[1]);
273 NS_ASSERTION(NS_SUCCEEDED(rv
), "unable to initialize data source");
274 if (NS_FAILED(rv
)) return rv
;
276 // The do_QI() on the pointer is a hack to make sure that the new
277 // object gets AddRef()-ed.
278 nsCOMPtr
<nsIRDFObserver
> observer
= do_QueryInterface(new Observer
);
280 return NS_ERROR_OUT_OF_MEMORY
;
282 rv
= ds
->AddObserver(observer
);
283 if (NS_FAILED(rv
)) return rv
;
286 // Okay, this should load the XML file...
287 rv
= remote
->Refresh(PR_TRUE
);
288 NS_ASSERTION(NS_SUCCEEDED(rv
), "unable to open datasource");
289 if (NS_FAILED(rv
)) return rv
;
294 PRInt32 pollinterval
= atol(argv
[2]);
298 PR_Sleep(PR_SecondsToInterval(pollinterval
));