Bug 359675 - provide an option to manually fill forms and log in. p=poshannessy@mozil...
[wine-gecko.git] / rdf / tests / rdfpoll / rdfpoll.cpp
blob2c21d72d7f4eddcd808b3b776f22929177445d55
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
13 * License.
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.
22 * Contributor(s):
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 .
50 #include <stdlib.h>
51 #include <stdio.h>
52 #include "nsXPCOM.h"
53 #include "nsCOMPtr.h"
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"
64 #include "nsIURL.h"
65 #include "nsRDFCID.h"
66 #include "nsIComponentManager.h"
67 #include "nsComponentManagerUtils.h"
68 #include "nsThreadUtils.h"
69 #include "prthread.h"
70 #include "plstr.h"
71 #include "nsEmbedString.h"
72 #include "nsNetCID.h"
74 ////////////////////////////////////////////////////////////////////////
75 // CIDs
77 // rdf
78 static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
80 ////////////////////////////////////////////////////////////////////////
81 // IIDs
84 #include "nsIMemory.h" // for the CID
86 ////////////////////////////////////////////////////////////////////////
88 class Observer : public nsIRDFObserver
90 public:
91 Observer();
92 virtual ~Observer() {}
94 // nsISupports interface
95 NS_DECL_ISUPPORTS
97 // nsIRDFObserver interface
98 NS_DECL_NSIRDFOBSERVER
101 Observer::Observer()
105 NS_IMPL_ISUPPORTS1(Observer, nsIRDFObserver)
107 static nsresult
108 rdf_WriteOp(const char* aOp,
109 nsIRDFResource* aSource,
110 nsIRDFResource* aProperty,
111 nsIRDFNode* aTarget)
113 nsresult rv;
115 nsCString source;
116 rv = aSource->GetValue(getter_Copies(source));
117 if (NS_FAILED(rv)) return rv;
119 nsCString property;
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) {
132 nsCString target;
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) {
139 nsString target;
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) {
146 PRTime value;
147 date->GetValue(&value);
149 PRExplodedTime t;
150 PR_ExplodeTime(value, PR_GMTParameters, &t);
152 printf(" -> %02d/%02d/%04d %02d:%02d:%02d +%06d\n",
153 t.tm_month + 1,
154 t.tm_mday,
155 t.tm_year,
156 t.tm_hour,
157 t.tm_min,
158 t.tm_sec,
159 t.tm_usec);
161 else if ((number = do_QueryInterface(aTarget)) != nsnull) {
162 PRInt32 value;
163 number->GetValue(&value);
165 printf(" -> %d\n", value);
167 else {
168 printf(" -> (unknown node type)\n");
171 printf("\n");
172 return NS_OK;
175 NS_IMETHODIMP
176 Observer::OnAssert(nsIRDFDataSource* aDataSource,
177 nsIRDFResource* aSource,
178 nsIRDFResource* aProperty,
179 nsIRDFNode* aTarget)
181 return rdf_WriteOp("assert", aSource, aProperty, aTarget);
185 NS_IMETHODIMP
186 Observer::OnUnassert(nsIRDFDataSource* aDataSource,
187 nsIRDFResource* aSource,
188 nsIRDFResource* aProperty,
189 nsIRDFNode* aTarget)
191 return rdf_WriteOp("unassert", aSource, aProperty, aTarget);
195 NS_IMETHODIMP
196 Observer::OnChange(nsIRDFDataSource* aDataSource,
197 nsIRDFResource* aSource,
198 nsIRDFResource* aProperty,
199 nsIRDFNode* aOldTarget,
200 nsIRDFNode* aNewTarget)
202 nsresult rv;
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;
209 return NS_OK;
212 NS_IMETHODIMP
213 Observer::OnMove(nsIRDFDataSource* aDataSource,
214 nsIRDFResource* aOldSource,
215 nsIRDFResource* aNewSource,
216 nsIRDFResource* aProperty,
217 nsIRDFNode* aTarget)
219 nsresult rv;
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;
226 return NS_OK;
229 NS_IMETHODIMP
230 Observer::OnBeginUpdateBatch(nsIRDFDataSource* aDataSource)
232 return NS_OK;
235 NS_IMETHODIMP
236 Observer::OnEndUpdateBatch(nsIRDFDataSource* aDataSource)
238 return NS_OK;
241 ////////////////////////////////////////////////////////////////////////
244 main(int argc, char** argv)
246 nsresult rv;
248 if (argc < 2) {
249 fprintf(stderr, "usage: %s <url> [<poll-interval>]\n", argv[0]);
250 return 1;
253 rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
254 if (NS_FAILED(rv)) {
255 fprintf(stderr, "NS_InitXPCOM2 failed\n");
256 return 1;
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);
263 if (NS_FAILED(rv)) {
264 NS_ERROR("unable to create RDF/XML data source");
265 return rv;
268 nsCOMPtr<nsIRDFRemoteDataSource> remote = do_QueryInterface(ds);
269 if (! remote)
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);
279 if (! observer)
280 return NS_ERROR_OUT_OF_MEMORY;
282 rv = ds->AddObserver(observer);
283 if (NS_FAILED(rv)) return rv;
285 while (1) {
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;
291 if (argc <= 2)
292 break;
294 PRInt32 pollinterval = atol(argv[2]);
295 if (! pollinterval)
296 break;
298 PR_Sleep(PR_SecondsToInterval(pollinterval));
301 return NS_OK;