content/xul/templates/src/nsXULTemplateResultRDF.cpp

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/content/xul/templates/src/nsXULTemplateResultRDF.cpp	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,208 @@
     1.4 +/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
     1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public
     1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this
     1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
     1.8 +
     1.9 +#include "nsXULTemplateResultRDF.h"
    1.10 +#include "nsXULContentUtils.h"
    1.11 +
    1.12 +// XXXndeakin for some reason, making this class have classinfo breaks trees.
    1.13 +//#include "nsIDOMClassInfo.h"
    1.14 +
    1.15 +NS_IMPL_CYCLE_COLLECTION(nsXULTemplateResultRDF, mQuery)
    1.16 +
    1.17 +NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(nsXULTemplateResultRDF)
    1.18 +  NS_INTERFACE_MAP_ENTRY(nsIXULTemplateResult)
    1.19 +  NS_INTERFACE_MAP_ENTRY(nsISupports)
    1.20 +NS_INTERFACE_MAP_END
    1.21 +
    1.22 +NS_IMPL_CYCLE_COLLECTING_ADDREF(nsXULTemplateResultRDF)
    1.23 +NS_IMPL_CYCLE_COLLECTING_RELEASE(nsXULTemplateResultRDF)
    1.24 +
    1.25 +nsXULTemplateResultRDF::nsXULTemplateResultRDF(nsIRDFResource* aNode)
    1.26 +    : mQuery(nullptr),
    1.27 +      mNode(aNode)
    1.28 +{
    1.29 +}
    1.30 +
    1.31 +nsXULTemplateResultRDF::nsXULTemplateResultRDF(nsRDFQuery* aQuery,
    1.32 +                                               const Instantiation& aInst,
    1.33 +                                               nsIRDFResource *aNode)
    1.34 +    : mQuery(aQuery),
    1.35 +      mNode(aNode),
    1.36 +      mInst(aInst)
    1.37 +{
    1.38 +}
    1.39 +
    1.40 +nsXULTemplateResultRDF::~nsXULTemplateResultRDF()
    1.41 +{
    1.42 +}
    1.43 +
    1.44 +NS_IMETHODIMP
    1.45 +nsXULTemplateResultRDF::GetIsContainer(bool* aIsContainer)
    1.46 +{
    1.47 +    *aIsContainer = false;
    1.48 +
    1.49 +    if (mNode) {
    1.50 +        nsXULTemplateQueryProcessorRDF* processor = GetProcessor();
    1.51 +        if (processor)
    1.52 +            return processor->CheckContainer(mNode, aIsContainer);
    1.53 +    }
    1.54 +
    1.55 +    return NS_OK;
    1.56 +}
    1.57 +
    1.58 +NS_IMETHODIMP
    1.59 +nsXULTemplateResultRDF::GetIsEmpty(bool* aIsEmpty)
    1.60 +{
    1.61 +    *aIsEmpty = true;
    1.62 +
    1.63 +    if (mNode) {
    1.64 +        nsXULTemplateQueryProcessorRDF* processor = GetProcessor();
    1.65 +        if (processor)
    1.66 +            return processor->CheckEmpty(mNode, aIsEmpty);
    1.67 +    }
    1.68 +
    1.69 +    return NS_OK;
    1.70 +}
    1.71 +
    1.72 +NS_IMETHODIMP
    1.73 +nsXULTemplateResultRDF::GetMayProcessChildren(bool* aMayProcessChildren)
    1.74 +{
    1.75 +    // RDF always allows recursion
    1.76 +    *aMayProcessChildren = true;
    1.77 +    return NS_OK;
    1.78 +}
    1.79 +
    1.80 +NS_IMETHODIMP
    1.81 +nsXULTemplateResultRDF::GetId(nsAString& aId)
    1.82 +{
    1.83 +    if (! mNode)
    1.84 +        return NS_ERROR_FAILURE;
    1.85 +
    1.86 +    const char* uri;
    1.87 +    mNode->GetValueConst(&uri);
    1.88 +
    1.89 +    CopyUTF8toUTF16(uri, aId);
    1.90 +
    1.91 +    return NS_OK;
    1.92 +}
    1.93 +
    1.94 +NS_IMETHODIMP
    1.95 +nsXULTemplateResultRDF::GetResource(nsIRDFResource** aResource)
    1.96 +{
    1.97 +    *aResource = mNode;
    1.98 +    NS_IF_ADDREF(*aResource);
    1.99 +    return NS_OK;
   1.100 +}
   1.101 +
   1.102 +NS_IMETHODIMP
   1.103 +nsXULTemplateResultRDF::GetType(nsAString& aType)
   1.104 +{
   1.105 +    aType.Truncate();
   1.106 +
   1.107 +    nsresult rv = NS_OK;
   1.108 +
   1.109 +    nsXULTemplateQueryProcessorRDF* processor = GetProcessor();
   1.110 +    if (processor) {
   1.111 +        bool found;
   1.112 +        rv = processor->CheckIsSeparator(mNode, &found);
   1.113 +        if (NS_SUCCEEDED(rv) && found)
   1.114 +            aType.AssignLiteral("separator");
   1.115 +    }
   1.116 +
   1.117 +    return rv;
   1.118 +}
   1.119 +
   1.120 +NS_IMETHODIMP
   1.121 +nsXULTemplateResultRDF::GetBindingFor(nsIAtom* aVar, nsAString& aValue)
   1.122 +{
   1.123 +    nsCOMPtr<nsIRDFNode> val;
   1.124 +    GetAssignment(aVar, getter_AddRefs(val));
   1.125 +
   1.126 +    return nsXULContentUtils::GetTextForNode(val, aValue);
   1.127 +}
   1.128 +
   1.129 +NS_IMETHODIMP
   1.130 +nsXULTemplateResultRDF::GetBindingObjectFor(nsIAtom* aVar, nsISupports** aValue)
   1.131 +{
   1.132 +    GetAssignment(aVar, (nsIRDFNode **)aValue);
   1.133 +
   1.134 +    return NS_OK;
   1.135 +}
   1.136 +
   1.137 +NS_IMETHODIMP
   1.138 +nsXULTemplateResultRDF::RuleMatched(nsISupports* aQuery, nsIDOMNode* aRuleNode)
   1.139 +{
   1.140 +    // when a rule matches, set the bindings that must be used.
   1.141 +    nsXULTemplateQueryProcessorRDF* processor = GetProcessor();
   1.142 +    if (processor) {
   1.143 +        RDFBindingSet* bindings = processor->GetBindingsForRule(aRuleNode);
   1.144 +        if (bindings) {
   1.145 +            nsresult rv = mBindingValues.SetBindingSet(bindings);
   1.146 +            if (NS_FAILED(rv))
   1.147 +                return rv;
   1.148 +
   1.149 +            bindings->AddDependencies(mNode, this);
   1.150 +        }
   1.151 +    }
   1.152 +
   1.153 +    return NS_OK;
   1.154 +}
   1.155 +
   1.156 +NS_IMETHODIMP
   1.157 +nsXULTemplateResultRDF::HasBeenRemoved()
   1.158 +{
   1.159 +    // when a result is no longer used, clean up the dependencies and
   1.160 +    // memory elements that refer to it
   1.161 +    mBindingValues.RemoveDependencies(mNode, this);
   1.162 +
   1.163 +    nsXULTemplateQueryProcessorRDF* processor = GetProcessor();
   1.164 +    if (processor)
   1.165 +        processor->RemoveMemoryElements(mInst, this);
   1.166 +
   1.167 +    return NS_OK;
   1.168 +}
   1.169 +
   1.170 +
   1.171 +void
   1.172 +nsXULTemplateResultRDF::GetAssignment(nsIAtom* aVar, nsIRDFNode** aValue)
   1.173 +{
   1.174 +    // look up a variable in the assignments map
   1.175 +    *aValue = nullptr;
   1.176 +    mInst.mAssignments.GetAssignmentFor(aVar, aValue);
   1.177 +
   1.178 +    // if not found, look up the variable in the bindings
   1.179 +    if (! *aValue)
   1.180 +        mBindingValues.GetAssignmentFor(this, aVar, aValue);
   1.181 +}
   1.182 +
   1.183 +
   1.184 +bool
   1.185 +nsXULTemplateResultRDF::SyncAssignments(nsIRDFResource* aSubject,
   1.186 +                                        nsIRDFResource* aPredicate,
   1.187 +                                        nsIRDFNode* aTarget)
   1.188 +{
   1.189 +    // synchronize the bindings when an assertion is added or removed
   1.190 +    RDFBindingSet* bindingset = mBindingValues.GetBindingSet();
   1.191 +    if (bindingset) {
   1.192 +        return bindingset->SyncAssignments(aSubject, aPredicate, aTarget,
   1.193 +            (aSubject == mNode) ? mQuery->GetMemberVariable() : nullptr,
   1.194 +            this, mBindingValues);
   1.195 +    }
   1.196 +
   1.197 +    return false;
   1.198 +}
   1.199 +
   1.200 +bool
   1.201 +nsXULTemplateResultRDF::HasMemoryElement(const MemoryElement& aMemoryElement)
   1.202 +{
   1.203 +    MemoryElementSet::ConstIterator last = mInst.mSupport.Last();
   1.204 +    for (MemoryElementSet::ConstIterator element = mInst.mSupport.First();
   1.205 +                                         element != last; ++element) {
   1.206 +        if ((*element).Equals(aMemoryElement))
   1.207 +            return true;
   1.208 +    }
   1.209 +
   1.210 +    return false;
   1.211 +}

mercurial