File indexing completed on 2024-04-28 05:49:18

0001 /***************************************************************************
0002     xml_parser.cpp  -  Produce a rudimentary list of tags/elements
0003     present in XML or HTML files. In the tree view of the
0004     symbolviewer plugin the list is grouped by the element type.
0005                             -------------------
0006     begin                : May 3 2019
0007     author               : 20019 Andreas Hohenegger based on
0008                                 xslt_parser.cpp by jiri Tyr
0009     email                : hohenegger@gmail.com
0010 ***************************************************************************/
0011 /***************************************************************************
0012  *
0013  *   SPDX-License-Identifier: GPL-2.0-or-later
0014  *
0015  ***************************************************************************/
0016 
0017 #include "plugin_katesymbolviewer.h"
0018 
0019 void KatePluginSymbolViewerView::parseXMLSymbols(void)
0020 {
0021     if (!m_mainWindow->activeView()) {
0022         return;
0023     }
0024 
0025     m_struct->setText(i18n("Show Tags"));
0026 
0027     QTreeWidgetItem *node = nullptr;
0028     QTreeWidgetItem *topNode = nullptr;
0029 
0030     KTextEditor::Document *kv = m_mainWindow->activeView()->document();
0031 
0032     m_symbols->setRootIsDecorated(0);
0033 
0034     bool is_comment = false;
0035     for (int i = 0; i < kv->lines(); i++) {
0036         QString cl = kv->line(i);
0037         cl = cl.trimmed();
0038 
0039         if (cl.indexOf(QLatin1String("<!--")) >= 0) {
0040             is_comment = true;
0041         }
0042         if (cl.indexOf(QLatin1String("-->")) >= 0) {
0043             is_comment = false;
0044             continue;
0045         }
0046 
0047         if (is_comment) {
0048             continue;
0049         }
0050 
0051         if (cl.indexOf(QRegularExpression(QLatin1String("^<[a-zA-Z_]+[a-zA-Z0-9_\\.\\-]*"))) == 0 && m_struct->isChecked()) {
0052             /* Get the tag type */
0053             QString type;
0054             QRegularExpressionMatch match;
0055             QRegularExpression re(QLatin1String("^<([a-zA-Z_]+[a-zA-Z0-9_\\.\\-]*)"));
0056             if (cl.contains(re, &match)) {
0057                 type = match.captured(1);
0058             } else {
0059                 continue;
0060             }
0061 
0062             QString stripped = cl.remove(QRegularExpression(QLatin1String("^<[a-zA-Z_]+[a-zA-Z0-9_\\.\\-]* *")));
0063             stripped.remove(QRegularExpression(QLatin1String(" */*>.*")));
0064 
0065             if (m_treeOn->isChecked()) {
0066                 /* See if group already exists */
0067                 QList<QTreeWidgetItem *> reslist = m_symbols->findItems(type, Qt::MatchExactly);
0068                 if (reslist.isEmpty()) {
0069                     topNode = new QTreeWidgetItem(m_symbols, QStringList(type));
0070                     topNode->setIcon(0, m_icon_class);
0071                     if (m_expandOn->isChecked()) {
0072                         m_symbols->expandItem(topNode);
0073                     }
0074                 } else {
0075                     topNode = reslist[0];
0076                 }
0077                 node = new QTreeWidgetItem(topNode);
0078                 topNode->addChild(node);
0079             } else {
0080                 node = new QTreeWidgetItem(m_symbols);
0081             }
0082             node->setIcon(0, m_icon_variable);
0083             node->setText(0, stripped);
0084             node->setText(1, QString::number(i, 10));
0085         }
0086     }
0087 }