1998-04-13 20:24:54 +00:00
|
|
|
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*-
|
|
|
|
*
|
|
|
|
* The contents of this file are subject to the Netscape Public License
|
|
|
|
* Version 1.0 (the "NPL"); you may not use this file except in
|
|
|
|
* compliance with the NPL. You may obtain a copy of the NPL at
|
|
|
|
* http://www.mozilla.org/NPL/
|
|
|
|
*
|
|
|
|
* Software distributed under the NPL is distributed on an "AS IS" basis,
|
|
|
|
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
|
|
|
|
* for the specific language governing rights and limitations under the
|
|
|
|
* NPL.
|
|
|
|
*
|
|
|
|
* The Initial Developer of this code under the NPL is Netscape
|
|
|
|
* Communications Corporation. Portions created by Netscape are
|
|
|
|
* Copyright (C) 1998 Netscape Communications Corporation. All Rights
|
|
|
|
* Reserved.
|
|
|
|
*/
|
|
|
|
#include <stdio.h>
|
|
|
|
#include "nsICSSParser.h"
|
1998-11-26 01:34:53 +00:00
|
|
|
#include "nsICSSStyleSheet.h"
|
1998-05-28 18:40:07 +00:00
|
|
|
#include "nsIStyleRule.h"
|
1998-04-13 20:24:54 +00:00
|
|
|
#include "nsIURL.h"
|
|
|
|
#include "nsIInputStream.h"
|
|
|
|
#include "nsIUnicharInputStream.h"
|
|
|
|
#include "nsString.h"
|
|
|
|
|
1998-09-25 18:18:09 +00:00
|
|
|
// XXX begin bad code
|
1998-11-24 01:47:44 +00:00
|
|
|
#include "plevent.h"
|
1998-09-25 18:18:09 +00:00
|
|
|
#include "nsINetService.h"
|
|
|
|
#include "nsRepository.h"
|
1999-01-07 07:47:25 +00:00
|
|
|
#include "nsIServiceManager.h"
|
|
|
|
#include "nsIEventQueueService.h"
|
|
|
|
#include "nsXPComCIID.h"
|
|
|
|
|
1998-09-25 18:18:09 +00:00
|
|
|
#ifdef XP_PC
|
|
|
|
#define NETLIB_DLL "netlib.dll"
|
1999-01-07 07:47:25 +00:00
|
|
|
#define XPCOM_DLL "xpcom32.dll"
|
1998-09-25 18:18:09 +00:00
|
|
|
#else
|
|
|
|
#ifdef XP_MAC
|
|
|
|
#include "nsMacRepository.h"
|
|
|
|
#else
|
|
|
|
#define NETLIB_DLL "libnetlib.so"
|
1999-01-07 07:47:25 +00:00
|
|
|
#define XPCOM_DLL "libxpcom.so"
|
1998-09-25 18:18:09 +00:00
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
static NS_DEFINE_IID(kNetServiceCID, NS_NETSERVICE_CID);
|
1999-01-07 07:47:25 +00:00
|
|
|
static NS_DEFINE_IID(kEventQueueServiceCID, NS_EVENTQUEUESERVICE_CID);
|
|
|
|
|
|
|
|
static NS_DEFINE_IID(kIEventQueueServiceIID, NS_IEVENTQUEUESERVICE_IID);
|
1998-09-25 18:18:09 +00:00
|
|
|
// XXX end bad code
|
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
static void Usage(void)
|
|
|
|
{
|
1998-05-28 18:40:07 +00:00
|
|
|
printf("usage: TestCSSParser [-v] [-s string | url1 ...]\n");
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int main(int argc, char** argv)
|
|
|
|
{
|
1999-01-07 07:47:25 +00:00
|
|
|
nsRepository::RegisterFactory(kEventQueueServiceCID, XPCOM_DLL, PR_FALSE, PR_FALSE);
|
1998-09-25 18:18:09 +00:00
|
|
|
nsRepository::RegisterFactory(kNetServiceCID, NETLIB_DLL, PR_FALSE, PR_FALSE);
|
|
|
|
|
1999-01-07 07:47:25 +00:00
|
|
|
nsresult rv;
|
1998-04-13 20:24:54 +00:00
|
|
|
PRBool verbose = PR_FALSE;
|
1998-05-28 18:40:07 +00:00
|
|
|
nsString* string = nsnull;
|
|
|
|
int i;
|
|
|
|
for (i = 1; i < argc; i++) {
|
|
|
|
if (argv[i][0] == '-') {
|
|
|
|
if (strcmp(argv[i], "-v") == 0) {
|
|
|
|
verbose = PR_TRUE;
|
|
|
|
}
|
|
|
|
else if (strcmp(argv[i], "-s") == 0) {
|
|
|
|
if ((nsnull != string) || (i == argc - 1)) {
|
|
|
|
Usage();
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
string = new nsString(argv[++i]);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
Usage();
|
|
|
|
return -1;
|
|
|
|
}
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
1998-05-28 18:40:07 +00:00
|
|
|
else
|
|
|
|
break;
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
1999-01-07 07:47:25 +00:00
|
|
|
// Create the Event Queue for this thread...
|
|
|
|
nsIEventQueueService* pEventQService = nsnull;
|
|
|
|
rv = nsServiceManager::GetService(kEventQueueServiceCID,
|
|
|
|
kIEventQueueServiceIID,
|
|
|
|
(nsISupports **)&pEventQService);
|
|
|
|
if (NS_SUCCEEDED(rv)) {
|
|
|
|
// XXX: What if this fails?
|
|
|
|
rv = pEventQService->CreateThreadEventQueue();
|
|
|
|
}
|
|
|
|
|
1998-05-28 18:40:07 +00:00
|
|
|
// Create parser
|
|
|
|
nsICSSParser* css;
|
1999-01-07 07:47:25 +00:00
|
|
|
rv = NS_NewCSSParser(&css);
|
1998-04-13 20:24:54 +00:00
|
|
|
if (NS_OK != rv) {
|
1998-05-28 18:40:07 +00:00
|
|
|
printf("can't create css parser: %d\n", rv);
|
1998-04-13 20:24:54 +00:00
|
|
|
return -1;
|
|
|
|
}
|
1998-05-28 18:40:07 +00:00
|
|
|
PRUint32 infoMask;
|
|
|
|
css->GetInfoMask(infoMask);
|
|
|
|
printf("CSS parser supports %x\n", infoMask);
|
1998-04-13 20:24:54 +00:00
|
|
|
|
1998-05-28 18:40:07 +00:00
|
|
|
if (nsnull != string) {
|
|
|
|
nsIStyleRule* rule;
|
|
|
|
rv = css->ParseDeclarations(*string, nsnull, rule);
|
|
|
|
if (NS_OK == rv) {
|
|
|
|
if (verbose && (nsnull != rule)) {
|
|
|
|
rule->List();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("ParseDeclarations failed: rv=%d\n", rv);
|
|
|
|
}
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
1998-05-28 18:40:07 +00:00
|
|
|
else {
|
|
|
|
for (; i < argc; i++) {
|
|
|
|
char* urlName = argv[i];
|
|
|
|
// Create url object
|
|
|
|
nsIURL* url;
|
1998-12-16 05:40:20 +00:00
|
|
|
rv = NS_NewURL(&url, urlName);
|
1998-05-28 18:40:07 +00:00
|
|
|
if (NS_OK != rv) {
|
|
|
|
printf("invalid URL: '%s'\n", urlName);
|
|
|
|
return -1;
|
|
|
|
}
|
1998-04-13 20:24:54 +00:00
|
|
|
|
1998-05-28 18:40:07 +00:00
|
|
|
// Get an input stream from the url
|
1998-12-16 05:40:20 +00:00
|
|
|
nsIInputStream* in;
|
|
|
|
rv = NS_OpenURL(url, &in);
|
|
|
|
if (rv != NS_OK) {
|
|
|
|
printf("open of url('%s') failed: error=%x\n", urlName, rv);
|
1998-05-28 18:40:07 +00:00
|
|
|
continue;
|
|
|
|
}
|
1998-04-13 20:24:54 +00:00
|
|
|
|
1998-05-28 18:40:07 +00:00
|
|
|
// Translate the input using the argument character set id into unicode
|
|
|
|
nsIUnicharInputStream* uin;
|
|
|
|
rv = NS_NewConverterStream(&uin, nsnull, in);
|
|
|
|
if (NS_OK != rv) {
|
|
|
|
printf("can't create converter input stream: %d\n", rv);
|
|
|
|
return -1;
|
|
|
|
}
|
1998-04-13 20:24:54 +00:00
|
|
|
|
1998-05-28 18:40:07 +00:00
|
|
|
// Parse the input and produce a style set
|
1998-11-26 01:34:53 +00:00
|
|
|
nsICSSStyleSheet* sheet;
|
1998-05-28 18:40:07 +00:00
|
|
|
rv = css->Parse(uin, url, sheet);
|
|
|
|
if (NS_OK == rv) {
|
|
|
|
if (verbose) {
|
|
|
|
sheet->List();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
printf("parse failed: %d\n", rv);
|
|
|
|
}
|
|
|
|
|
|
|
|
url->Release();
|
|
|
|
in->Release();
|
|
|
|
uin->Release();
|
|
|
|
sheet->Release();
|
|
|
|
}
|
1998-04-13 20:24:54 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
css->Release();
|
|
|
|
|
1999-01-07 07:47:25 +00:00
|
|
|
/* Release the event queue for the thread */
|
|
|
|
if (nsnull != pEventQService) {
|
|
|
|
pEventQService->DestroyThreadEventQueue();
|
|
|
|
nsServiceManager::ReleaseService(kEventQueueServiceCID, pEventQService);
|
|
|
|
}
|
|
|
|
|
1998-04-13 20:24:54 +00:00
|
|
|
return 0;
|
|
|
|
}
|