Back to index

lightning-sunbird  0.9+nobinonly
txTestExpr.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is TransforMiiX XSLT processor code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Axel Hecht.
00019  * Portions created by the Initial Developer are Copyright (C) 2003
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *   Axel Hecht <axel@pike.org>
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 #include "nsXPCOM.h"
00040 #include "txStandaloneXSLTProcessor.h"
00041 #include "nsString.h"
00042 #include "ExprParser.h"
00043 #include "txIXPathContext.h"
00044 
00049 static const char* kTokens[] = {"(", "concat", "(", "foo", ",", "'", "bar",
00050                                 "'",")", "//", ".", "[", "preceding-sibling",
00051                                 "::", "bar", "]", "/", "*", "[", "23", "]",
00052                                 "|", "node", "(", ")", ")", "<", "3"};
00053 static const PRUint8 kCount = sizeof(kTokens)/sizeof(char*);
00054 
00055 class ParseContextImpl : public txIParseContext
00056 {
00057 public:
00058     nsresult
00059     resolveNamespacePrefix(nsIAtom* aPrefix, PRInt32& aID)
00060     {
00061         return NS_ERROR_FAILURE;
00062     }
00063     nsresult
00064     resolveFunctionCall(nsIAtom* aName, PRInt32 aID, FunctionCall*& aFunction)
00065     {
00066         return NS_ERROR_XPATH_UNKNOWN_FUNCTION;
00067     }
00068     PRBool
00069     caseInsensitiveNameTests()
00070     {
00071         return PR_FALSE;
00072     }
00073     void
00074     SetErrorOffset(PRUint32 aOffset)
00075     {
00076         mOff = aOffset;
00077     }
00078     PRUint32 mOff;
00079 };
00080 
00081 static void doTest(const nsASingleFragmentString& aExpr)
00082 {
00083     ParseContextImpl ct;
00084     nsAutoPtr<Expr> expression;
00085     cout << NS_LossyConvertUTF16toASCII(aExpr).get() << endl;
00086     ct.mOff = 0;
00087     nsresult rv = txExprParser::createExpr(aExpr, &ct,
00088                                            getter_Transfers(expression));
00089 
00090     cout << "createExpr returned " << ios::hex << rv  << ios::dec;
00091     cout << " at " << ct.mOff << endl;
00092     if (NS_FAILED(rv)) {
00093         NS_LossyConvertUTF16toASCII cstring(aExpr);
00094         cout << NS_LossyConvertUTF16toASCII(StringHead(aExpr, ct.mOff)).get();
00095         cout << " ^ ";
00096         cout << NS_LossyConvertUTF16toASCII(StringTail(aExpr, aExpr.Length()-ct.mOff)).get();
00097         cout << endl << endl;
00098     }
00099 #ifdef TX_TO_STRING
00100     else {
00101         nsAutoString expr;
00102         expression->toString(expr);
00103         cout << "parsed expression: ";
00104         cout << NS_LossyConvertUTF16toASCII(expr).get() << endl << endl;
00105     }
00106 #endif
00107 }
00108 
00109 int main(int argc, char** argv)
00110 {
00111     using namespace std;
00112     nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
00113     NS_ENSURE_SUCCESS(rv, rv);
00114 
00115     if (!txXSLTProcessor::init())
00116         return 1;
00117 
00118     nsAutoString exprOrig, expr;
00119     nsStringArray exprHead, exprTail;
00120     PRUint8 i, dropStart, dropEnd;
00121     exprHead.AppendString(NS_ConvertASCIItoUTF16(kTokens[0]));
00122     exprTail.AppendString(NS_ConvertASCIItoUTF16(kTokens[kCount - 1]));
00123     for (i = 2; i < kCount; ++i) {
00124         exprHead.AppendString(*exprHead[i - 2] +
00125                               NS_ConvertASCIItoUTF16(kTokens[i - 1]));
00126         exprTail.AppendString(NS_ConvertASCIItoUTF16(kTokens[kCount - i]) +
00127                               *exprTail[i - 2]);
00128     }
00129     exprOrig = NS_ConvertASCIItoUTF16(kTokens[0]) + *exprTail[kCount - 2];
00130     cout << NS_LossyConvertUTF16toASCII(exprOrig).get() << endl << endl;
00131     for (dropStart = 0; dropStart < kCount - 2; ++dropStart) {
00132         doTest(*exprTail[kCount - 2 - dropStart]);
00133         for (dropEnd = kCount - 3 - dropStart; dropEnd > 0; --dropEnd) {
00134             expr = *exprHead[dropStart] + *exprTail[dropEnd];
00135             doTest(expr);
00136         }
00137         doTest(*exprHead[dropStart]);
00138     }
00139     doTest(*exprHead[kCount - 2]);
00140 
00141     txXSLTProcessor::shutdown();
00142     rv = NS_ShutdownXPCOM(nsnull);
00143     NS_ENSURE_SUCCESS(rv, rv);
00144     return 0;
00145 }