2001-09-28 20:14:13 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
|
|
|
|
/* ***** BEGIN LICENSE BLOCK *****
|
|
|
|
* Version: NPL 1.1/GPL 2.0/LGPL 2.1
|
1999-05-05 01:15:01 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.1 (the "License"); you may not use this file except in
|
|
|
|
* compliance with the License. You may obtain a copy of the License at
|
|
|
|
* http://www.mozilla.org/NPL/
|
1999-05-05 01:15:01 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Software distributed under the License is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* License.
|
1999-05-05 01:15:01 +00:00
|
|
|
*
|
1999-11-06 03:43:54 +00:00
|
|
|
* The Original Code is mozilla.org code.
|
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* The Initial Developer of the Original Code is
|
|
|
|
* Netscape Communications Corporation.
|
|
|
|
* Portions created by the Initial Developer are Copyright (C) 1998
|
|
|
|
* the Initial Developer. All Rights Reserved.
|
1999-11-06 03:43:54 +00:00
|
|
|
*
|
2001-09-28 20:14:13 +00:00
|
|
|
* Contributor(s):
|
2000-01-11 20:49:15 +00:00
|
|
|
* Pierre Phaneuf <pp@ludusdesign.com>
|
2001-09-28 20:14:13 +00:00
|
|
|
*
|
|
|
|
* Alternatively, the contents of this file may be used under the terms of
|
|
|
|
* either the GNU General Public License Version 2 or later (the "GPL"), or
|
|
|
|
* the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
|
|
|
|
* in which case the provisions of the GPL or the LGPL are applicable instead
|
|
|
|
* of those above. If you wish to allow use of your version of this file only
|
|
|
|
* under the terms of either the GPL or the LGPL, and not to allow others to
|
|
|
|
* use your version of this file under the terms of the NPL, indicate your
|
|
|
|
* decision by deleting the provisions above and replace them with the notice
|
|
|
|
* and other provisions required by the GPL or the LGPL. If you do not delete
|
|
|
|
* the provisions above, a recipient may use your version of this file under
|
|
|
|
* the terms of any one of the NPL, the GPL or the LGPL.
|
|
|
|
*
|
|
|
|
* ***** END LICENSE BLOCK ***** */
|
1999-05-05 01:15:01 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
|
|
|
|
A simple test program that reads in RDF/XML into an in-memory data
|
|
|
|
source, then periodically updates it. It prints the messages
|
|
|
|
indicating assert and unassert activity to the console.
|
|
|
|
|
|
|
|
The program takes two parameters: the URL from which to read, and
|
|
|
|
the poll-interval at which to re-load the .
|
|
|
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdlib.h>
|
2002-02-16 16:18:13 +00:00
|
|
|
#include "nsXPCOM.h"
|
1999-05-05 01:15:01 +00:00
|
|
|
#include "nsCOMPtr.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsIInputStream.h"
|
1999-06-18 17:34:08 +00:00
|
|
|
#include "nsIIOService.h"
|
1999-05-05 01:15:01 +00:00
|
|
|
#include "nsIRDFCompositeDataSource.h"
|
1999-06-24 00:22:58 +00:00
|
|
|
#include "nsIRDFRemoteDataSource.h"
|
1999-05-05 01:15:01 +00:00
|
|
|
#include "nsIRDFNode.h"
|
|
|
|
#include "nsIRDFService.h"
|
|
|
|
#include "nsIRDFXMLSource.h"
|
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIStreamListener.h"
|
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsRDFCID.h"
|
|
|
|
#include "nsIComponentManager.h"
|
|
|
|
#include "nsXPIDLString.h"
|
|
|
|
#include "prthread.h"
|
|
|
|
#include "plevent.h"
|
|
|
|
#include "plstr.h"
|
2000-01-03 23:40:10 +00:00
|
|
|
#include "nsString.h"
|
2001-09-29 08:28:41 +00:00
|
|
|
#include "nsReadableUtils.h"
|
2001-04-10 06:01:08 +00:00
|
|
|
#include "nsNetCID.h"
|
2002-05-15 18:55:21 +00:00
|
|
|
#include "nsCRT.h"
|
1999-05-05 01:15:01 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// CIDs
|
|
|
|
|
|
|
|
// rdf
|
|
|
|
static NS_DEFINE_CID(kRDFServiceCID, NS_RDFSERVICE_CID);
|
|
|
|
static NS_DEFINE_CID(kRDFXMLDataSourceCID, NS_RDFXMLDATASOURCE_CID);
|
|
|
|
|
|
|
|
// xpcom
|
|
|
|
static NS_DEFINE_CID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
// IIDs
|
|
|
|
|
|
|
|
NS_DEFINE_IID(kIEventQueueServiceIID, NS_IEVENTQUEUESERVICE_IID);
|
|
|
|
|
2000-06-03 09:46:12 +00:00
|
|
|
#include "nsIMemory.h" // for the CID
|
1999-05-05 01:15:01 +00:00
|
|
|
|
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
class Observer : public nsIRDFObserver
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
Observer();
|
|
|
|
virtual ~Observer() {}
|
|
|
|
|
|
|
|
// nsISupports interface
|
|
|
|
NS_DECL_ISUPPORTS
|
|
|
|
|
|
|
|
// nsIRDFObserver interface
|
2000-08-08 00:22:36 +00:00
|
|
|
NS_DECL_NSIRDFOBSERVER
|
1999-05-05 01:15:01 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
Observer::Observer()
|
|
|
|
{
|
|
|
|
NS_INIT_REFCNT();
|
|
|
|
}
|
|
|
|
|
2000-11-17 20:54:21 +00:00
|
|
|
NS_IMPL_ISUPPORTS1(Observer, nsIRDFObserver)
|
1999-05-05 01:15:01 +00:00
|
|
|
|
1999-06-24 00:22:58 +00:00
|
|
|
static nsresult
|
|
|
|
rdf_WriteOp(const char* aOp,
|
|
|
|
nsIRDFResource* aSource,
|
|
|
|
nsIRDFResource* aProperty,
|
|
|
|
nsIRDFNode* aTarget)
|
1999-05-05 01:15:01 +00:00
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
nsXPIDLCString source;
|
|
|
|
rv = aSource->GetValue(getter_Copies(source));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsXPIDLCString property;
|
|
|
|
rv = aProperty->GetValue(getter_Copies(property));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
nsCOMPtr<nsIRDFResource> resource;
|
|
|
|
nsCOMPtr<nsIRDFLiteral> literal;
|
2002-01-15 01:56:15 +00:00
|
|
|
nsCOMPtr<nsIRDFDate> date;
|
|
|
|
nsCOMPtr<nsIRDFInt> number;
|
1999-05-05 01:15:01 +00:00
|
|
|
|
1999-06-24 00:22:58 +00:00
|
|
|
printf("%.8s [%s]\n", aOp, (const char*) source);
|
1999-05-05 01:15:01 +00:00
|
|
|
printf(" --[%s]--\n", (const char*) property);
|
|
|
|
|
|
|
|
if ((resource = do_QueryInterface(aTarget)) != nsnull) {
|
|
|
|
nsXPIDLCString target;
|
|
|
|
rv = resource->GetValue(getter_Copies(target));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
printf(" ->[%s]\n", (const char*) target);
|
|
|
|
}
|
|
|
|
else if ((literal = do_QueryInterface(aTarget)) != nsnull) {
|
|
|
|
nsXPIDLString target;
|
|
|
|
rv = literal->GetValue(getter_Copies(target));
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
2001-09-29 08:28:41 +00:00
|
|
|
char* p = ToNewCString(target);
|
1999-05-05 01:15:01 +00:00
|
|
|
if (! p)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
printf(" ->\"%s\"\n", p);
|
|
|
|
|
1999-09-04 15:40:35 +00:00
|
|
|
nsCRT::free(p);
|
1999-05-05 01:15:01 +00:00
|
|
|
}
|
2002-01-15 01:56:15 +00:00
|
|
|
else if ((date = do_QueryInterface(aTarget)) != nsnull) {
|
|
|
|
PRTime value;
|
|
|
|
date->GetValue(&value);
|
|
|
|
|
|
|
|
PRExplodedTime t;
|
|
|
|
PR_ExplodeTime(value, PR_GMTParameters, &t);
|
|
|
|
|
|
|
|
printf(" -> %02d/%02d/%04d %02d:%02d:%02d +%06d\n",
|
|
|
|
t.tm_month + 1,
|
|
|
|
t.tm_mday,
|
|
|
|
t.tm_year,
|
|
|
|
t.tm_hour,
|
|
|
|
t.tm_min,
|
|
|
|
t.tm_sec,
|
|
|
|
t.tm_usec);
|
|
|
|
}
|
|
|
|
else if ((number = do_QueryInterface(aTarget)) != nsnull) {
|
|
|
|
PRInt32 value;
|
|
|
|
number->GetValue(&value);
|
|
|
|
|
|
|
|
printf(" -> %d\n", value);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf(" -> (unknown node type)\n");
|
|
|
|
}
|
1999-05-05 01:15:01 +00:00
|
|
|
|
|
|
|
printf("\n");
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
1999-06-24 00:22:58 +00:00
|
|
|
NS_IMETHODIMP
|
2000-08-08 00:22:36 +00:00
|
|
|
Observer::OnAssert(nsIRDFDataSource* aDataSource,
|
|
|
|
nsIRDFResource* aSource,
|
1999-06-24 00:22:58 +00:00
|
|
|
nsIRDFResource* aProperty,
|
|
|
|
nsIRDFNode* aTarget)
|
|
|
|
{
|
|
|
|
return rdf_WriteOp("assert", aSource, aProperty, aTarget);
|
|
|
|
}
|
|
|
|
|
1999-05-05 01:15:01 +00:00
|
|
|
|
|
|
|
NS_IMETHODIMP
|
2000-08-08 00:22:36 +00:00
|
|
|
Observer::OnUnassert(nsIRDFDataSource* aDataSource,
|
|
|
|
nsIRDFResource* aSource,
|
1999-05-05 01:15:01 +00:00
|
|
|
nsIRDFResource* aProperty,
|
|
|
|
nsIRDFNode* aTarget)
|
|
|
|
{
|
1999-06-24 00:22:58 +00:00
|
|
|
return rdf_WriteOp("unassert", aSource, aProperty, aTarget);
|
|
|
|
}
|
1999-05-05 01:15:01 +00:00
|
|
|
|
|
|
|
|
1999-06-24 00:22:58 +00:00
|
|
|
NS_IMETHODIMP
|
2000-08-08 00:22:36 +00:00
|
|
|
Observer::OnChange(nsIRDFDataSource* aDataSource,
|
|
|
|
nsIRDFResource* aSource,
|
1999-06-24 00:22:58 +00:00
|
|
|
nsIRDFResource* aProperty,
|
|
|
|
nsIRDFNode* aOldTarget,
|
|
|
|
nsIRDFNode* aNewTarget)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
rv = rdf_WriteOp("chg-from", aSource, aProperty, aOldTarget);
|
1999-05-05 01:15:01 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-06-24 00:22:58 +00:00
|
|
|
rv = rdf_WriteOp("chg-to", aSource, aProperty, aNewTarget);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-05-05 01:15:01 +00:00
|
|
|
|
1999-06-24 00:22:58 +00:00
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-05-05 01:15:01 +00:00
|
|
|
|
1999-06-24 00:22:58 +00:00
|
|
|
NS_IMETHODIMP
|
2000-08-08 00:22:36 +00:00
|
|
|
Observer::OnMove(nsIRDFDataSource* aDataSource,
|
|
|
|
nsIRDFResource* aOldSource,
|
1999-06-24 00:22:58 +00:00
|
|
|
nsIRDFResource* aNewSource,
|
|
|
|
nsIRDFResource* aProperty,
|
|
|
|
nsIRDFNode* aTarget)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
rv = rdf_WriteOp("mv-from", aOldSource, aProperty, aTarget);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-05-05 01:15:01 +00:00
|
|
|
|
1999-06-24 00:22:58 +00:00
|
|
|
rv = rdf_WriteOp("mv-to", aNewSource, aProperty, aTarget);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
1999-05-05 01:15:01 +00:00
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
2000-08-08 00:22:36 +00:00
|
|
|
NS_IMETHODIMP
|
|
|
|
Observer::BeginUpdateBatch(nsIRDFDataSource* aDataSource)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
NS_IMETHODIMP
|
|
|
|
Observer::EndUpdateBatch(nsIRDFDataSource* aDataSource)
|
|
|
|
{
|
|
|
|
return NS_OK;
|
|
|
|
}
|
1999-06-24 00:22:58 +00:00
|
|
|
|
1999-05-05 01:15:01 +00:00
|
|
|
////////////////////////////////////////////////////////////////////////
|
|
|
|
|
|
|
|
int
|
|
|
|
main(int argc, char** argv)
|
|
|
|
{
|
|
|
|
nsresult rv;
|
|
|
|
|
|
|
|
if (argc < 2) {
|
|
|
|
fprintf(stderr, "usage: %s <url> [<poll-interval>]\n", argv[0]);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2002-02-16 16:18:13 +00:00
|
|
|
rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
|
|
|
|
if (NS_FAILED(rv)) {
|
|
|
|
fprintf(stderr, "NS_InitXPCOM2 failed\n");
|
|
|
|
return 1;
|
|
|
|
}
|
1999-05-05 01:15:01 +00:00
|
|
|
|
|
|
|
// Get netlib off the floor...
|
2001-07-25 07:54:28 +00:00
|
|
|
nsCOMPtr<nsIEventQueueService> theEventQueueService =
|
|
|
|
do_GetService(kEventQueueServiceCID, &rv);
|
1999-05-05 01:15:01 +00:00
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
rv = theEventQueueService->CreateThreadEventQueue();
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create thread event queue");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
// Create a stream data source and initialize it on argv[1], which
|
|
|
|
// is hopefully a "file:" URL. (Actually, we can do _any_ kind of
|
|
|
|
// URL, but only a "file:" URL will be written back to disk.)
|
1999-06-24 00:22:58 +00:00
|
|
|
nsCOMPtr<nsIRDFDataSource> ds;
|
1999-05-05 01:15:01 +00:00
|
|
|
rv = nsComponentManager::CreateInstance(kRDFXMLDataSourceCID,
|
|
|
|
nsnull,
|
2000-01-11 20:49:15 +00:00
|
|
|
NS_GET_IID(nsIRDFDataSource),
|
1999-05-05 01:15:01 +00:00
|
|
|
getter_AddRefs(ds));
|
|
|
|
|
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to create RDF/XML data source");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-06-24 00:22:58 +00:00
|
|
|
nsCOMPtr<nsIRDFRemoteDataSource> remote
|
|
|
|
= do_QueryInterface(ds);
|
|
|
|
|
|
|
|
if (! remote)
|
|
|
|
return NS_ERROR_UNEXPECTED;
|
|
|
|
|
|
|
|
rv = remote->Init(argv[1]);
|
1999-05-05 01:15:01 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to initialize data source");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
1999-06-17 19:47:21 +00:00
|
|
|
// The do_QI() on the pointer is a hack to make sure that the new
|
|
|
|
// object gets AddRef()-ed.
|
|
|
|
nsCOMPtr<nsIRDFObserver> observer = do_QueryInterface(new Observer);
|
1999-05-05 01:15:01 +00:00
|
|
|
if (! observer)
|
|
|
|
return NS_ERROR_OUT_OF_MEMORY;
|
|
|
|
|
|
|
|
rv = ds->AddObserver(observer);
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
while (1) {
|
|
|
|
// Okay, this should load the XML file...
|
1999-06-24 00:22:58 +00:00
|
|
|
rv = remote->Refresh(PR_TRUE);
|
1999-05-05 01:15:01 +00:00
|
|
|
NS_ASSERTION(NS_SUCCEEDED(rv), "unable to open datasource");
|
|
|
|
if (NS_FAILED(rv)) return rv;
|
|
|
|
|
|
|
|
if (argc <= 2)
|
|
|
|
break;
|
|
|
|
|
|
|
|
PRInt32 pollinterval = atol(argv[2]);
|
|
|
|
if (! pollinterval)
|
|
|
|
break;
|
|
|
|
|
|
|
|
PR_Sleep(PR_SecondsToInterval(pollinterval));
|
|
|
|
}
|
|
|
|
|
|
|
|
return NS_OK;
|
|
|
|
}
|