Back to index

lightning-sunbird  0.9+nobinonly
ParseCSS.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 // vim:cindent:ts=8:et:sw=4:
00003 /* ***** BEGIN LICENSE BLOCK *****
00004  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00005  *
00006  * The contents of this file are subject to the Mozilla Public License Version
00007  * 1.1 (the "License"); you may not use this file except in compliance with
00008  * the License. You may obtain a copy of the License at
00009  * http://www.mozilla.org/MPL/
00010  *
00011  * Software distributed under the License is distributed on an "AS IS" basis,
00012  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00013  * for the specific language governing rights and limitations under the
00014  * License.
00015  *
00016  * The Original Code is mozilla.org code.
00017  *
00018  * The Initial Developer of the Original Code is L. David Baron.
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   L. David Baron <dbaron@dbaron.org> (original author)
00024  *
00025  * Alternatively, the contents of this file may be used under the terms of
00026  * either the GNU General Public License Version 2 or later (the "GPL"), or
00027  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00028  * in which case the provisions of the GPL or the LGPL are applicable instead
00029  * of those above. If you wish to allow use of your version of this file only
00030  * under the terms of either the GPL or the LGPL, and not to allow others to
00031  * use your version of this file under the terms of the MPL, indicate your
00032  * decision by deleting the provisions above and replace them with the notice
00033  * and other provisions required by the GPL or the LGPL. If you do not delete
00034  * the provisions above, a recipient may use your version of this file under
00035  * the terms of any one of the MPL, the GPL or the LGPL.
00036  *
00037  * ***** END LICENSE BLOCK ***** */
00038 
00039 /*
00040  * This file is meant to be used with |#define CSS_REPORT_PARSE_ERRORS|
00041  * in mozilla/content/html/style/src/nsCSSScanner.h uncommented, and the
00042  * |#ifdef DEBUG| block in nsCSSScanner::OutputError (in
00043  * nsCSSScanner.cpp in the same directory) used (even if not a debug
00044  * build).
00045  */
00046 
00047 #include "nsXPCOM.h"
00048 #include "nsCOMPtr.h"
00049 
00050 #include "nsILocalFile.h"
00051 #include "nsNetUtil.h"
00052 
00053 #include "nsContentCID.h"
00054 #include "nsICSSLoader.h"
00055 #include "nsICSSStyleSheet.h"
00056 
00057 static NS_DEFINE_CID(kCSSLoaderCID, NS_CSS_LOADER_CID);
00058 
00059 static already_AddRefed<nsIURI>
00060 FileToURI(const char *aFilename, nsresult *aRv = 0)
00061 {
00062     nsCOMPtr<nsILocalFile> lf(do_CreateInstance(NS_LOCAL_FILE_CONTRACTID, aRv));
00063     NS_ENSURE_TRUE(lf, nsnull);
00064     // XXX Handle relative paths somehow.
00065     lf->InitWithNativePath(nsDependentCString(aFilename));
00066 
00067     nsIURI *uri = nsnull;
00068     nsresult rv = NS_NewFileURI(&uri, lf);
00069     if (aRv)
00070         *aRv = rv;
00071     return uri;
00072 }
00073 
00074 static int
00075 ParseCSSFile(nsIURI *aSheetURI)
00076 {
00077     nsCOMPtr<nsICSSLoader> loader(do_CreateInstance(kCSSLoaderCID));
00078     nsCOMPtr<nsICSSStyleSheet> sheet;
00079     loader->LoadAgentSheet(aSheetURI, getter_AddRefs(sheet));
00080     NS_ASSERTION(sheet, "sheet load failed");
00081     /* This can happen if the file can't be found (e.g. you
00082      * ask for a relative path and xpcom/io rejects it)
00083      */
00084     if (!sheet)
00085         return -1;
00086     PRBool complete;
00087     sheet->GetComplete(complete);
00088     NS_ASSERTION(complete, "synchronous load did not complete");
00089     if (!complete)
00090         return -2;
00091     return 0;
00092 }
00093 
00094 int main(int argc, char** argv)
00095 {
00096     if (argc < 2) {
00097         fprintf(stderr, "%s [FILE]...\n", argv[0]);
00098     }
00099     nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00100     if (NS_FAILED(rv))
00101         return (int)rv;
00102 
00103     int res = 0;
00104     for (int i = 1; i < argc; ++i) {
00105         const char *filename = argv[i];
00106 
00107         printf("\nParsing %s.\n", filename);
00108 
00109         nsCOMPtr<nsIURI> uri = FileToURI(filename, &rv);
00110         if (rv == NS_ERROR_OUT_OF_MEMORY) {
00111             fprintf(stderr, "Out of memory.\n");
00112             return 1;
00113         }
00114         if (uri)
00115             res = ParseCSSFile(uri);
00116     }
00117 
00118     NS_ShutdownXPCOM(nsnull);
00119 
00120     return res;
00121 }