~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/extensions/transformiix/source/xpath/BooleanFunctionCall.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*-
 
2
 *
 
3
 * The contents of this file are subject to the Mozilla Public
 
4
 * License Version 1.1 (the "License"); you may not use this file
 
5
 * except in compliance with the License. You may obtain a copy of
 
6
 * the License at http://www.mozilla.org/MPL/
 
7
 *
 
8
 * Software distributed under the License is distributed on an "AS
 
9
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
10
 * implied. See the License for the specific language governing
 
11
 * rights and limitations under the License.
 
12
 *
 
13
 * The Original Code is TransforMiiX XSLT processor.
 
14
 *
 
15
 * The Initial Developer of the Original Code is The MITRE Corporation.
 
16
 * Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
 
17
 *
 
18
 * Portions created by Keith Visco as a Non MITRE employee,
 
19
 * (C) 1999 Keith Visco. All Rights Reserved.
 
20
 *
 
21
 * Contributor(s):
 
22
 * Keith Visco, kvisco@ziplink.net
 
23
 *   -- original author.
 
24
 *
 
25
 * Marina Mechtcheriakova, mmarina@mindspring.com
 
26
 *   -- added lang() implementation
 
27
 *
 
28
 */
 
29
 
 
30
#include "ExprResult.h"
 
31
#include "FunctionLib.h"
 
32
#include "nsReadableUtils.h"
 
33
#include "txAtoms.h"
 
34
#include "txIXPathContext.h"
 
35
#include "txStringUtils.h"
 
36
#include "txXPathTreeWalker.h"
 
37
 
 
38
/**
 
39
 * Creates a default BooleanFunctionCall, which always evaluates to False
 
40
**/
 
41
BooleanFunctionCall::BooleanFunctionCall(BooleanFunctions aType)
 
42
    : mType(aType)
 
43
{
 
44
}
 
45
 
 
46
/**
 
47
 * Evaluates this Expr based on the given context node and processor state
 
48
 * @param context the context node for evaluation of this Expr
 
49
 * @param ps the ContextState containing the stack information needed
 
50
 * for evaluation
 
51
 * @return the result of the evaluation
 
52
**/
 
53
nsresult
 
54
BooleanFunctionCall::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
 
55
{
 
56
    *aResult = nsnull;
 
57
 
 
58
    txListIterator iter(&params);
 
59
    switch (mType) {
 
60
        case TX_BOOLEAN:
 
61
        {
 
62
            if (!requireParams(1, 1, aContext))
 
63
                return NS_ERROR_XPATH_BAD_ARGUMENT_COUNT;
 
64
 
 
65
            aContext->recycler()->getBoolResult(
 
66
                evaluateToBoolean((Expr*)iter.next(), aContext), aResult);
 
67
 
 
68
            return NS_OK;
 
69
        }
 
70
        case TX_LANG:
 
71
        {
 
72
            if (!requireParams(1, 1, aContext))
 
73
                return NS_ERROR_XPATH_BAD_ARGUMENT_COUNT;
 
74
 
 
75
            txXPathTreeWalker walker(aContext->getContextNode());
 
76
 
 
77
            nsAutoString lang;
 
78
            PRBool found;
 
79
            do {
 
80
                found = walker.getAttr(txXMLAtoms::lang, kNameSpaceID_XML,
 
81
                                       lang);
 
82
            } while (!found && walker.moveToParent());
 
83
 
 
84
            if (!found) {
 
85
                aContext->recycler()->getBoolResult(PR_FALSE, aResult);
 
86
 
 
87
                return NS_OK;
 
88
            }
 
89
 
 
90
            nsAutoString arg;
 
91
            evaluateToString((Expr*)iter.next(), aContext, arg);
 
92
            PRBool result = arg.Equals(Substring(lang, 0, arg.Length()),
 
93
                                       txCaseInsensitiveStringComparator()) &&
 
94
                            (lang.Length() == arg.Length() ||
 
95
                             lang.CharAt(arg.Length()) == '-');
 
96
 
 
97
            aContext->recycler()->getBoolResult(result, aResult);
 
98
 
 
99
            return NS_OK;
 
100
        }
 
101
        case TX_NOT:
 
102
        {
 
103
            if (!requireParams(1, 1, aContext))
 
104
                return NS_ERROR_XPATH_BAD_ARGUMENT_COUNT;
 
105
 
 
106
            aContext->recycler()->getBoolResult(
 
107
                  !evaluateToBoolean((Expr*)iter.next(), aContext), aResult);
 
108
 
 
109
            return NS_OK;
 
110
        }
 
111
        case TX_TRUE:
 
112
        {
 
113
            if (!requireParams(0, 0, aContext))
 
114
                return NS_ERROR_XPATH_BAD_ARGUMENT_COUNT;
 
115
 
 
116
            aContext->recycler()->getBoolResult(PR_TRUE, aResult);
 
117
 
 
118
            return NS_OK;
 
119
        }
 
120
        case TX_FALSE:
 
121
        {
 
122
            if (!requireParams(0, 0, aContext))
 
123
                return NS_ERROR_XPATH_BAD_ARGUMENT_COUNT;
 
124
 
 
125
            aContext->recycler()->getBoolResult(PR_FALSE, aResult);
 
126
 
 
127
            return NS_OK;
 
128
        }
 
129
    }
 
130
 
 
131
    aContext->receiveError(NS_LITERAL_STRING("Internal error"),
 
132
                           NS_ERROR_UNEXPECTED);
 
133
    return NS_ERROR_UNEXPECTED;
 
134
}
 
135
 
 
136
nsresult BooleanFunctionCall::getNameAtom(nsIAtom** aAtom)
 
137
{
 
138
    switch (mType) {
 
139
        case TX_BOOLEAN:
 
140
        {
 
141
            *aAtom = txXPathAtoms::boolean;
 
142
            break;
 
143
        }
 
144
        case TX_LANG:
 
145
        {
 
146
            *aAtom = txXPathAtoms::lang;
 
147
            break;
 
148
        }
 
149
        case TX_NOT:
 
150
        {
 
151
            *aAtom = txXPathAtoms::_not;
 
152
            break;
 
153
        }
 
154
        case TX_TRUE:
 
155
        {
 
156
            *aAtom = txXPathAtoms::_true;
 
157
            break;
 
158
        }
 
159
        case TX_FALSE:
 
160
        {
 
161
            *aAtom = txXPathAtoms::_false;
 
162
            break;
 
163
        }
 
164
        default:
 
165
        {
 
166
            *aAtom = 0;
 
167
            return NS_ERROR_FAILURE;
 
168
        }
 
169
    }
 
170
    NS_ADDREF(*aAtom);
 
171
    return NS_OK;
 
172
}