bool KXMLGUIClient::mergeXML( QDomElement &base, const QDomElement &additive, KActionCollection *actionCollection ) { static const QString &tagAction = KGlobal::staticQString( "Action" ); static const QString &tagMerge = KGlobal::staticQString( "Merge" ); static const QString &tagSeparator = KGlobal::staticQString( "Separator" ); static const QString &attrName = KGlobal::staticQString( "name" ); static const QString &attrAppend = KGlobal::staticQString( "append" ); static const QString &attrWeakSeparator = KGlobal::staticQString( "weakSeparator" ); static const QString &tagMergeLocal = KGlobal::staticQString( "MergeLocal" ); static const QString &tagText = KGlobal::staticQString( "text" ); static const QString &attrAlreadyVisited = KGlobal::staticQString( "alreadyVisited" ); static const QString &attrNoMerge = KGlobal::staticQString( "noMerge" ); static const QString &attrOne = KGlobal::staticQString( "1" ); // there is a possibility that we don't want to merge in the // additive.. rather, we might want to *replace* the base with the // additive. this can be for any container.. either at a file wide // level or a simple container level. we look for the 'noMerge' // tag, in any event and just replace the old with the new if ( additive.attribute(attrNoMerge) == attrOne ) // ### use toInt() instead? (Simon) { base.parentNode().replaceChild(additive, base); return true; } QString tag; // iterate over all elements in the container (of the global DOM tree) QDomNode n = base.firstChild(); while ( !n.isNull() ) { QDomElement e = n.toElement(); n = n.nextSibling(); // Advance now so that we can safely delete e if (e.isNull()) continue; tag = e.tagName(); // if there's an action tag in the global tree and the action is // not implemented, then we remove the element if ( tag == tagAction ) { QCString name = e.attribute( attrName ).utf8(); // WABA if ( !actionCollection->action( name ) || (kapp && !kapp->authorizeKAction(name))) { // remove this child as we aren't using it base.removeChild( e ); continue; } } // if there's a separator defined in the global tree, then add an // attribute, specifying that this is a "weak" separator else if ( tag == tagSeparator ) { e.setAttribute( attrWeakSeparator, (uint)1 ); // okay, hack time. if the last item was a weak separator OR // this is the first item in a container, then we nuke the // current one QDomElement prev = e.previousSibling().toElement(); if ( prev.isNull() || ( prev.tagName() == tagSeparator && !prev.attribute( attrWeakSeparator ).isNull() ) || ( prev.tagName() == tagText ) ) { // the previous element was a weak separator or didn't exist base.removeChild( e ); continue; } } // the MergeLocal tag lets us specify where non-standard elements // of the local tree shall be merged in. After inserting the // elements we delete this element else if ( tag == tagMergeLocal ) { QDomNode it = additive.firstChild(); while ( !it.isNull() ) { QDomElement newChild = it.toElement(); it = it.nextSibling(); if (newChild.isNull() ) continue; if ( newChild.tagName() == tagText ) continue; if ( newChild.attribute( attrAlreadyVisited ) == attrOne ) continue; QString itAppend( newChild.attribute( attrAppend ) ); QString elemName( e.attribute( attrName ) ); if ( ( itAppend.isNull() && elemName.isEmpty() ) || ( itAppend == elemName ) ) { // first, see if this new element matches a standard one in // the global file. if it does, then we skip it as it will // be merged in, later QDomElement matchingElement = findMatchingElement( newChild, base ); if ( matchingElement.isNull() || newChild.tagName() == tagSeparator ) base.insertBefore( newChild, e ); } } base.removeChild( e ); continue; } // in this last case we check for a separator tag and, if not, we // can be sure that its a container --> proceed with child nodes // recursively and delete the just proceeded container item in // case its empty (if the recursive call returns true) else if ( tag != tagMerge ) { // handle the text tag if ( tag == tagText ) continue; QDomElement matchingElement = findMatchingElement( e, additive ); if ( !matchingElement.isNull() ) { matchingElement.setAttribute( attrAlreadyVisited, (uint)1 ); if ( mergeXML( e, matchingElement, actionCollection ) ) { base.removeChild( e ); continue; } // Merge attributes const QDomNamedNodeMap attribs = matchingElement.attributes(); const uint attribcount = attribs.count(); for(uint i = 0; i < attribcount; ++i) { const QDomNode node = attribs.item(i); e.setAttribute(node.nodeName(), node.nodeValue()); } continue; } else { // this is an important case here! We reach this point if the // "local" tree does not contain a container definition for // this container. However we have to call mergeXML recursively // and make it check if there are actions implemented for this // container. *If* none, then we can remove this container now if ( mergeXML( e, QDomElement(), actionCollection ) ) base.removeChild( e ); continue; } } } //here we append all child elements which were not inserted //previously via the LocalMerge tag n = additive.firstChild(); while ( !n.isNull() ) { QDomElement e = n.toElement(); n = n.nextSibling(); // Advance now so that we can safely delete e if (e.isNull()) continue; QDomElement matchingElement = findMatchingElement( e, base ); if ( matchingElement.isNull() ) { base.appendChild( e ); } } // do one quick check to make sure that the last element was not // a weak separator QDomElement last = base.lastChild().toElement(); if ( (last.tagName() == tagSeparator) && (!last.attribute( attrWeakSeparator ).isNull()) ) { base.removeChild( last ); } // now we check if we are empty (in which case we return "true", to // indicate the caller that it can delete "us" (the base element // argument of "this" call) bool deleteMe = true; n = base.firstChild(); while ( !n.isNull() ) { QDomElement e = n.toElement(); n = n.nextSibling(); // Advance now so that we can safely delete e if (e.isNull()) continue; tag = e.tagName(); if ( tag == tagAction ) { // if base contains an implemented action, then we must not get // deleted (note that the actionCollection contains both, // "global" and "local" actions if ( actionCollection->action( e.attribute( attrName ).utf8() ) ) { deleteMe = false; break; } } else if ( tag == tagSeparator ) { // if we have a separator which has *not* the weak attribute // set, then it must be owned by the "local" tree in which case // we must not get deleted either QString weakAttr = e.attribute( attrWeakSeparator ); if ( weakAttr.isEmpty() || weakAttr.toInt() != 1 ) { deleteMe = false; break; } } // in case of a merge tag we have unlimited lives, too ;-) else if ( tag == tagMerge ) { // deleteMe = false; // break; continue; } // a text tag is NOT enough to spare this container else if ( tag == tagText ) { continue; } // what's left are non-empty containers! *don't* delete us in this // case (at this position we can be *sure* that the container is // *not* empty, as the recursive call for it was in the first loop // which deleted the element in case the call returned "true" else { deleteMe = false; break; } } return deleteMe; }
QDomElement OOoReportBuilder::processDetail(const QDomElement &rowDetail) { QDomElement lastElement = rowDetail; if (rowDetail.isNull() || reportBand(rowDetail) != Detail) return QDomElement(); QString textCell = cellText(rowDetail.firstChild().toElement()); textCell.remove(QRegExp("\\{|\\}|detail")); int modelId = textCell.toInt() - 1; if (modelId < m_models.count() - 1) { rowDetail.parentNode().removeChild(rowDetail); return lastElement.previousSibling().toElement(); } QAbstractItemModel *model = m_models.at(modelId); for (int i = 0; i < model->rowCount(); i++) { QDomElement tmpRow = rowDetail.cloneNode(true).toElement(); QDomElement cell = tmpRow.firstChild().toElement(); cell = cell.nextSibling().toElement(); while (!cell.isNull()) { QString str = cellText(cell); if (!str.isEmpty()) { str.remove(QRegExp("\\{|\\}")); if (!QString::compare(str,"rowno",Qt::CaseInsensitive)) { setText(cell,QString::number(i)); } else if (str[0] == 'P') { QVariant var = processParams(str.mid(2)); if (var.type() == QVariant::Double) { setText(cell,var.toDouble()); } else { setText(cell,var.toString()); } } else { QRegExp rx("col\\d{1,2}"); if (rx.indexIn(str) == 0) { int colNo = str.remove(QRegExp("col")).toInt(); QVariant var = model->data(model->index(i,colNo)); if (colNo <= model->columnCount() - 1) { if (var.type() == QVariant::Double) { setText(cell, var.toDouble()); } else setText(cell, var.toString()); } else setText(cell,"Err"); } else setText(cell,str); } } if (cell.attributes().contains("table:formula")) { QString formula = processFormula(cell.attribute("table:formula"), i); cell.setAttribute("table:formula",formula); } cell = cell.nextSibling().toElement(); } lastElement = rowDetail.parentNode().insertBefore(tmpRow,rowDetail).toElement(); } rowDetail.parentNode().removeChild(rowDetail); return lastElement; }