for (PRInt32 i = 0; i < count; i++) { nsCOMPtr<nsIContent> child; rv = mRoot->ChildAt(i, *getter_AddRefs(child)); if (NS_FAILED(rv)) return rv;
PRInt32 nrules = 0;
nsXULTemplateBuilder::CompileRules(){    NS_PRECONDITION(mRoot != nsnull, "not initialized");    if (! mRoot)        return NS_ERROR_NOT_INITIALIZED;    nsresult rv;    GetFlags();    mRulesCompiled = PR_FALSE; // in case an error occurs    InnerNode* childnode;    rv = InitializeRuleNetwork(&childnode);    if (NS_FAILED(rv)) return rv;    // First, check and see if the root has a template attribute. This    // allows a template to be specified "out of line"; e.g.,    //    //   <window>    //     <foo template="MyTemplate">...</foo>    //     <template id="MyTemplate">...</template>    //   </window>    //    nsAutoString templateID;    rv = mRoot->GetAttribute(kNameSpaceID_None, nsXULAtoms::Template, templateID);    if (NS_FAILED(rv)) return rv;    nsCOMPtr<nsIContent> tmpl;    if (rv == NS_CONTENT_ATTR_HAS_VALUE) {        nsCOMPtr<nsIDOMXULDocument>    xulDoc;        xulDoc = do_QueryInterface(mDocument);        if (! xulDoc)            return NS_ERROR_UNEXPECTED;        nsCOMPtr<nsIDOMElement>    domElement;        rv = xulDoc->GetElementById(templateID, getter_AddRefs(domElement));        if (NS_FAILED(rv)) return rv;        tmpl = do_QueryInterface(domElement);    }    // If root node has no template attribute, then look for a child    // node which is a template tag    if (! tmpl) {        PRInt32    count;        rv = mRoot->ChildCount(count);        if (NS_FAILED(rv)) return rv;        for (PRInt32 i = 0; i < count; i++) {            nsCOMPtr<nsIContent> child;            rv = mRoot->ChildAt(i, *getter_AddRefs(child));            if (NS_FAILED(rv)) return rv;            PRInt32 nameSpaceID;            rv = child->GetNameSpaceID(nameSpaceID);            if (NS_FAILED(rv)) return rv;            if (nameSpaceID != kNameSpaceID_XUL)                continue;            nsCOMPtr<nsIAtom> tag;            rv = child->GetTag(*getter_AddRefs(tag));            if (NS_FAILED(rv)) return rv;            if (tag.get() != nsXULAtoms::Template)                continue;            tmpl = child;            break;        }    }    if (tmpl) {        // Found a template                // Set the "container" and "member" variables, if the user has        // specified them.        mContainerSymbol.Truncate();        tmpl->GetAttribute(kNameSpaceID_None, nsXULAtoms::container, mContainerSymbol);        mMemberSymbol.Truncate();        tmpl->GetAttribute(kNameSpaceID_None, nsXULAtoms::member, mMemberSymbol);        // Compile the rules beneath it (if there are any)...        PRInt32    count;        rv = tmpl->ChildCount(count);        if (NS_FAILED(rv)) return rv;        PRInt32 nrules = 0;        for (PRInt32 i = 0; i < count; i++) {            nsCOMPtr<nsIContent> rule;            rv = tmpl->ChildAt(i, *getter_AddRefs(rule));            if (NS_FAILED(rv)) return rv;            PRInt32    nameSpaceID;            rv = rule->GetNameSpaceID(nameSpaceID);            if (NS_FAILED(rv)) return rv;            if (nameSpaceID != kNameSpaceID_XUL)                continue;            nsCOMPtr<nsIAtom> tag;            rv = rule->GetTag(*getter_AddRefs(tag));            if (NS_FAILED(rv)) return rv;            if (tag.get() == nsXULAtoms::rule) {                ++nrules;                // If the <rule> has a <conditions> element, then                // compile it using the extended syntax.                nsCOMPtr<nsIContent> conditions;                rv = gXULUtils->FindChildByTag(rule, kNameSpaceID_XUL, nsXULAtoms::conditions,                                               getter_AddRefs(conditions));                if (NS_FAILED(rv)) return rv;                if (conditions) {                    rv = CompileExtendedRule(rule, nrules, mRules.GetRoot());                    if (NS_FAILED(rv)) return rv;                }                else {                    rv = CompileSimpleRule(rule, nrules, childnode);                    if (NS_FAILED(rv)) return rv;                }            }        }        if (nrules == 0) {            // if no rules are specified in the template, then the            // contents of the <template> tag are the one-and-only            // template.            rv = CompileSimpleRule(tmpl, 1, childnode);            if (NS_FAILED(rv)) return rv;        }    }    // XXXwaterson post-process the rule network to optimize    mRulesCompiled = PR_TRUE;    return NS_OK;}