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

« back to all changes in this revision

Viewing changes to mozilla/extensions/transformiix/source/xpath/UnionExpr.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
/*
 
2
 * The contents of this file are subject to the Mozilla Public
 
3
 * License Version 1.1 (the "License"); you may not use this file
 
4
 * except in compliance with the License. You may obtain a copy of
 
5
 * the License at http://www.mozilla.org/MPL/
 
6
 * 
 
7
 * Software distributed under the License is distributed on an "AS
 
8
 * IS" basis, WITHOUT WARRANTY OF ANY KIND, either express or
 
9
 * implied. See the License for the specific language governing
 
10
 * rights and limitations under the License.
 
11
 * 
 
12
 * The Original Code is TransforMiiX XSLT processor.
 
13
 * 
 
14
 * The Initial Developer of the Original Code is The MITRE Corporation.
 
15
 * Portions created by MITRE are Copyright (C) 1999 The MITRE Corporation.
 
16
 *
 
17
 * Portions created by Keith Visco as a Non MITRE employee,
 
18
 * (C) 1999 Keith Visco. All Rights Reserved.
 
19
 * 
 
20
 * Contributor(s): 
 
21
 * Keith Visco, kvisco@ziplink.net
 
22
 *   -- original author.
 
23
 * 
 
24
 */
 
25
 
 
26
#include "Expr.h"
 
27
#include "txIXPathContext.h"
 
28
#include "txNodeSet.h"
 
29
 
 
30
  //-------------/
 
31
 //- UnionExpr -/
 
32
//-------------/
 
33
 
 
34
 
 
35
/**
 
36
 * Creates a new UnionExpr
 
37
**/
 
38
UnionExpr::UnionExpr() {
 
39
    //-- do nothing
 
40
}
 
41
 
 
42
/**
 
43
 * Destructor, will delete all Path Expressions
 
44
**/
 
45
UnionExpr::~UnionExpr() {
 
46
    txListIterator iter(&expressions);
 
47
    while (iter.hasNext()) {
 
48
         delete (Expr*)iter.next();
 
49
    }
 
50
} //-- ~UnionExpr
 
51
 
 
52
/**
 
53
 * Adds the Expr to this UnionExpr
 
54
 * @param expr the Expr to add to this UnionExpr
 
55
**/
 
56
nsresult
 
57
UnionExpr::addExpr(Expr* aExpr)
 
58
{
 
59
    nsresult rv = expressions.add(aExpr);
 
60
    if (NS_FAILED(rv)) {
 
61
        delete aExpr;
 
62
    }
 
63
    return rv;
 
64
} //-- addExpr
 
65
 
 
66
    //-----------------------------/
 
67
  //- Virtual methods from Expr -/
 
68
//-----------------------------/
 
69
 
 
70
/**
 
71
 * Evaluates this Expr based on the given context node and processor state
 
72
 * @param context the context node for evaluation of this Expr
 
73
 * @param ps the ContextState containing the stack information needed
 
74
 * for evaluation
 
75
 * @return the result of the evaluation
 
76
**/
 
77
nsresult
 
78
UnionExpr::evaluate(txIEvalContext* aContext, txAExprResult** aResult)
 
79
{
 
80
    *aResult = nsnull;
 
81
    nsRefPtr<txNodeSet> nodes;
 
82
    nsresult rv = aContext->recycler()->getNodeSet(getter_AddRefs(nodes));
 
83
    NS_ENSURE_SUCCESS(rv, rv);
 
84
 
 
85
    txListIterator iter(&expressions);
 
86
    while (iter.hasNext()) {
 
87
        Expr* expr = (Expr*)iter.next();
 
88
        nsRefPtr<txAExprResult> exprResult;
 
89
        rv = expr->evaluate(aContext, getter_AddRefs(exprResult));
 
90
        NS_ENSURE_SUCCESS(rv, rv);
 
91
 
 
92
        if (exprResult->getResultType() != txAExprResult::NODESET) {
 
93
            //XXX ErrorReport: report nonnodeset error
 
94
            return NS_ERROR_XSLT_NODESET_EXPECTED;
 
95
        }
 
96
 
 
97
        nsRefPtr<txNodeSet> resultSet, ownedSet;
 
98
        resultSet = NS_STATIC_CAST(txNodeSet*,
 
99
                                   NS_STATIC_CAST(txAExprResult*, exprResult));
 
100
        exprResult = nsnull;
 
101
        rv = aContext->recycler()->
 
102
            getNonSharedNodeSet(resultSet, getter_AddRefs(ownedSet));
 
103
        NS_ENSURE_SUCCESS(rv, rv);
 
104
 
 
105
        rv = nodes->addAndTransfer(ownedSet);
 
106
        NS_ENSURE_SUCCESS(rv, rv);
 
107
    }
 
108
 
 
109
    *aResult = nodes;
 
110
    NS_ADDREF(*aResult);
 
111
 
 
112
    return NS_OK;
 
113
} //-- evaluate
 
114
 
 
115
/**
 
116
 * Returns the String representation of this Expr.
 
117
 * @param dest the String to use when creating the String
 
118
 * representation. The String representation will be appended to
 
119
 *  any data in the destination String, to allow cascading calls to
 
120
 * other #toString() methods for Expressions.
 
121
 * @return the String representation of this Expr.
 
122
**/
 
123
void UnionExpr::toString(nsAString& dest) {
 
124
    txListIterator iter(&expressions);
 
125
 
 
126
    short count = 0;
 
127
    while (iter.hasNext()) {
 
128
        //-- set operator
 
129
        if (count > 0)
 
130
            dest.Append(NS_LITERAL_STRING(" | "));
 
131
        ((Expr*)iter.next())->toString(dest);
 
132
        ++count;
 
133
    }
 
134
} //-- toString
 
135