• Skip to content
  • Skip to link menu
Trinity API Reference
  • Trinity API Reference
  • kate
 

kate

  • kate
  • part
katesyntaxdocument.cpp
1 /* This file is part of the KDE libraries
2  Copyright (C) 2001 Joseph Wenninger <jowenn@kde.org>
3  Copyright (C) 2000 Scott Manson <sdmanson@alltel.net>
4 
5  This library is free software; you can redistribute it and/or
6  modify it under the terms of the GNU Library General Public
7  License version 2 as published by the Free Software Foundation.
8 
9  This library is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  Library General Public License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to
16  the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  Boston, MA 02110-1301, USA.
18 */
19 
20 #include "katesyntaxdocument.h"
21 
22 #include <sys/types.h>
23 #include <sys/stat.h>
24 #include <unistd.h>
25 
26 #include <kdebug.h>
27 #include <kstandarddirs.h>
28 #include <tdelocale.h>
29 #include <tdemessagebox.h>
30 #include <tdeconfig.h>
31 
32 #include <tqfile.h>
33 
34 KateSyntaxDocument::KateSyntaxDocument(bool force)
35  : TQDomDocument()
36 {
37  // Let's build the Mode List (katesyntaxhighlightingrc)
38  setupModeList(force);
39 }
40 
41 KateSyntaxDocument::~KateSyntaxDocument()
42 {
43  for (uint i=0; i < myModeList.size(); i++)
44  delete myModeList[i];
45 }
46 
51 bool KateSyntaxDocument::setIdentifier(const TQString& identifier)
52 {
53  // if the current file is the same as the new one don't do anything.
54  if(currentFile != identifier)
55  {
56  // let's open the new file
57  TQFile f( identifier );
58 
59  if ( f.open(IO_ReadOnly) )
60  {
61  // Let's parse the contets of the xml file
62  /* The result of this function should be check for robustness,
63  a false returned means a parse error */
64  TQString errorMsg;
65  int line, col;
66  bool success=setContent(&f,&errorMsg,&line,&col);
67 
68  // Ok, now the current file is the pretended one (identifier)
69  currentFile = identifier;
70 
71  // Close the file, is not longer needed
72  f.close();
73 
74  if (!success)
75  {
76  KMessageBox::error(0L,i18n("<qt>The error <b>%4</b><br> has been detected in the file %1 at %2/%3</qt>").arg(identifier)
77  .arg(line).arg(col).arg(i18n("TQXml",errorMsg.utf8())));
78  return false;
79  }
80  }
81  else
82  {
83  // Oh o, we couldn't open the file.
84  KMessageBox::error( 0L, i18n("Unable to open %1").arg(identifier) );
85  return false;
86  }
87  }
88  return true;
89 }
90 
94 bool KateSyntaxDocument::nextGroup( KateSyntaxContextData* data)
95 {
96  if(!data)
97  return false;
98 
99  // No group yet so go to first child
100  if (data->currentGroup.isNull())
101  {
102  // Skip over non-elements. So far non-elements are just comments
103  TQDomNode node = data->parent.firstChild();
104  while (node.isComment())
105  node = node.nextSibling();
106 
107  data->currentGroup = node.toElement();
108  }
109  else
110  {
111  // common case, iterate over siblings, skipping comments as we go
112  TQDomNode node = data->currentGroup.nextSibling();
113  while (node.isComment())
114  node = node.nextSibling();
115 
116  data->currentGroup = node.toElement();
117  }
118 
119  return !data->currentGroup.isNull();
120 }
121 
125 bool KateSyntaxDocument::nextItem( KateSyntaxContextData* data)
126 {
127  if(!data)
128  return false;
129 
130  if (data->item.isNull())
131  {
132  TQDomNode node = data->currentGroup.firstChild();
133  while (node.isComment())
134  node = node.nextSibling();
135 
136  data->item = node.toElement();
137  }
138  else
139  {
140  TQDomNode node = data->item.nextSibling();
141  while (node.isComment())
142  node = node.nextSibling();
143 
144  data->item = node.toElement();
145  }
146 
147  return !data->item.isNull();
148 }
149 
153 TQString KateSyntaxDocument::groupItemData( const KateSyntaxContextData* data, const TQString& name){
154  if(!data)
155  return TQString::null;
156 
157  // If there's no name just return the tag name of data->item
158  if ( (!data->item.isNull()) && (name.isEmpty()))
159  {
160  return data->item.tagName();
161  }
162 
163  // if name is not empty return the value of the attribute name
164  if (!data->item.isNull())
165  {
166  return data->item.attribute(name);
167  }
168 
169  return TQString::null;
170 
171 }
172 
173 TQString KateSyntaxDocument::groupData( const KateSyntaxContextData* data,const TQString& name)
174 {
175  if(!data)
176  return TQString::null;
177 
178  if (!data->currentGroup.isNull())
179  {
180  return data->currentGroup.attribute(name);
181  }
182  else
183  {
184  return TQString::null;
185  }
186 }
187 
188 void KateSyntaxDocument::freeGroupInfo( KateSyntaxContextData* data)
189 {
190  if (data)
191  delete data;
192 }
193 
194 KateSyntaxContextData* KateSyntaxDocument::getSubItems(KateSyntaxContextData* data)
195 {
196  KateSyntaxContextData *retval = new KateSyntaxContextData;
197 
198  if (data != 0)
199  {
200  retval->parent = data->currentGroup;
201  retval->currentGroup = data->item;
202  }
203 
204  return retval;
205 }
206 
207 bool KateSyntaxDocument::getElement (TQDomElement &element, const TQString &mainGroupName, const TQString &config)
208 {
209  kdDebug(13010) << "Looking for \"" << mainGroupName << "\" -> \"" << config << "\"." << endl;
210 
211  TQDomNodeList nodes = documentElement().childNodes();
212 
213  // Loop over all these child nodes looking for mainGroupName
214  for (unsigned int i=0; i<nodes.count(); i++)
215  {
216  TQDomElement elem = nodes.item(i).toElement();
217  if (elem.tagName() == mainGroupName)
218  {
219  // Found mainGroupName ...
220  TQDomNodeList subNodes = elem.childNodes();
221 
222  // ... so now loop looking for config
223  for (unsigned int j=0; j<subNodes.count(); j++)
224  {
225  TQDomElement subElem = subNodes.item(j).toElement();
226  if (subElem.tagName() == config)
227  {
228  // Found it!
229  element = subElem;
230  return true;
231  }
232  }
233 
234  kdDebug(13010) << "WARNING: \""<< config <<"\" wasn't found!" << endl;
235  return false;
236  }
237  }
238 
239  kdDebug(13010) << "WARNING: \""<< mainGroupName <<"\" wasn't found!" << endl;
240  return false;
241 }
242 
247 KateSyntaxContextData* KateSyntaxDocument::getConfig(const TQString& mainGroupName, const TQString &config)
248 {
249  TQDomElement element;
250  if (getElement(element, mainGroupName, config))
251  {
252  KateSyntaxContextData *data = new KateSyntaxContextData;
253  data->item = element;
254  return data;
255  }
256  return 0;
257 }
258 
263 KateSyntaxContextData* KateSyntaxDocument::getGroupInfo(const TQString& mainGroupName, const TQString &group)
264 {
265  TQDomElement element;
266  if (getElement(element, mainGroupName, group+"s"))
267  {
268  KateSyntaxContextData *data = new KateSyntaxContextData;
269  data->parent = element;
270  return data;
271  }
272  return 0;
273 }
274 
278 TQStringList& KateSyntaxDocument::finddata(const TQString& mainGroup, const TQString& type, bool clearList)
279 {
280  kdDebug(13010)<<"Create a list of keywords \""<<type<<"\" from \""<<mainGroup<<"\"."<<endl;
281  if (clearList)
282  m_data.clear();
283 
284  for(TQDomNode node = documentElement().firstChild(); !node.isNull(); node = node.nextSibling())
285  {
286  TQDomElement elem = node.toElement();
287  if (elem.tagName() == mainGroup)
288  {
289  kdDebug(13010)<<"\""<<mainGroup<<"\" found."<<endl;
290  TQDomNodeList nodelist1 = elem.elementsByTagName("list");
291 
292  for (uint l=0; l<nodelist1.count(); l++)
293  {
294  if (nodelist1.item(l).toElement().attribute("name") == type)
295  {
296  kdDebug(13010)<<"List with attribute name=\""<<type<<"\" found."<<endl;
297  TQDomNodeList childlist = nodelist1.item(l).toElement().childNodes();
298 
299  for (uint i=0; i<childlist.count(); i++)
300  {
301  TQString element = childlist.item(i).toElement().text().stripWhiteSpace();
302  if (element.isEmpty())
303  continue;
304 #ifndef NDEBUG
305  if (i<6)
306  {
307  kdDebug(13010)<<"\""<<element<<"\" added to the list \""<<type<<"\""<<endl;
308  }
309  else if(i==6)
310  {
311  kdDebug(13010)<<"... The list continues ..."<<endl;
312  }
313 #endif
314  m_data += element;
315  }
316 
317  break;
318  }
319  }
320  break;
321  }
322  }
323 
324  return m_data;
325 }
326 
327 // Private
331 void KateSyntaxDocument::setupModeList (bool force)
332 {
333  // If there's something in myModeList the Mode List was already built so, don't do it again
334  if (!myModeList.isEmpty())
335  return;
336 
337  // We'll store the ModeList in katesyntaxhighlightingrc
338  TDEConfig config("katesyntaxhighlightingrc", false, false);
339 
340  // figure our if the kate install is too new
341  config.setGroup ("General");
342  if (config.readNumEntry ("Version") > config.readNumEntry ("CachedVersion"))
343  {
344  config.writeEntry ("CachedVersion", config.readNumEntry ("Version"));
345  force = true;
346  }
347 
348  // Let's get a list of all the xml files for hl
349  TQStringList list = TDEGlobal::dirs()->findAllResources("data","katepart/syntax/*.xml",false,true);
350 
351  // Let's iterate through the list and build the Mode List
352  for ( TQStringList::Iterator it = list.begin(); it != list.end(); ++it )
353  {
354  // Each file has a group called:
355  TQString Group="Cache "+ *it;
356 
357  // Let's go to this group
358  config.setGroup(Group);
359 
360  // stat the file
361  struct stat sbuf;
362  memset (&sbuf, 0, sizeof(sbuf));
363  stat(TQFile::encodeName(*it), &sbuf);
364 
365  // If the group exist and we're not forced to read the xml file, let's build myModeList for katesyntax..rc
366  if (!force && config.hasGroup(Group) && (sbuf.st_mtime == config.readNumEntry("lastModified")))
367  {
368  // Let's make a new KateSyntaxModeListItem to instert in myModeList from the information in katesyntax..rc
369  KateSyntaxModeListItem *mli=new KateSyntaxModeListItem;
370  mli->name = config.readEntry("name");
371  mli->nameTranslated = i18n("Language",mli->name.utf8());
372  mli->section = i18n("Language Section",config.readEntry("section").utf8());
373  mli->mimetype = config.readEntry("mimetype");
374  mli->extension = config.readEntry("extension");
375  mli->version = config.readEntry("version");
376  mli->priority = config.readEntry("priority");
377  mli->author = config.readEntry("author");
378  mli->license = config.readEntry("license");
379  mli->hidden = config.readBoolEntry("hidden");
380  mli->identifier = *it;
381 
382  // Apend the item to the list
383  myModeList.append(mli);
384  }
385  else
386  {
387  kdDebug (13010) << "UPDATE hl cache for: " << *it << endl;
388 
389  // We're forced to read the xml files or the mode doesn't exist in the katesyntax...rc
390  TQFile f(*it);
391 
392  if (f.open(IO_ReadOnly))
393  {
394  // Ok we opened the file, let's read the contents and close the file
395  /* the return of setContent should be checked because a false return shows a parsing error */
396  TQString errMsg;
397  int line, col;
398 
399  bool success = setContent(&f,&errMsg,&line,&col);
400 
401  f.close();
402 
403  if (success)
404  {
405  TQDomElement root = documentElement();
406 
407  if (!root.isNull())
408  {
409  // If the 'first' tag is language, go on
410  if (root.tagName()=="language")
411  {
412  // let's make the mode list item.
413  KateSyntaxModeListItem *mli = new KateSyntaxModeListItem;
414 
415  mli->name = root.attribute("name");
416  mli->section = root.attribute("section");
417  mli->mimetype = root.attribute("mimetype");
418  mli->extension = root.attribute("extensions");
419  mli->version = root.attribute("version");
420  mli->priority = root.attribute("priority");
421  mli->author = root.attribute("author");
422  mli->license = root.attribute("license");
423 
424  TQString hidden = root.attribute("hidden");
425  mli->hidden = (hidden == "true" || hidden == "TRUE");
426 
427  mli->identifier = *it;
428 
429  // Now let's write or overwrite (if force==true) the entry in katesyntax...rc
430  config.setGroup(Group);
431  config.writeEntry("name",mli->name);
432  config.writeEntry("section",mli->section);
433  config.writeEntry("mimetype",mli->mimetype);
434  config.writeEntry("extension",mli->extension);
435  config.writeEntry("version",mli->version);
436  config.writeEntry("priority",mli->priority);
437  config.writeEntry("author",mli->author);
438  config.writeEntry("license",mli->license);
439  config.writeEntry("hidden",mli->hidden);
440 
441  // modified time to keep cache in sync
442  config.writeEntry("lastModified", sbuf.st_mtime);
443 
444  // Now that the data is in the config file, translate section
445  mli->section = i18n("Language Section",mli->section.utf8());
446  mli->nameTranslated = i18n("Language",mli->name.utf8());
447 
448  // Append the new item to the list.
449  myModeList.append(mli);
450  }
451  }
452  }
453  else
454  {
455  KateSyntaxModeListItem *emli=new KateSyntaxModeListItem;
456 
457  emli->section=i18n("Errors!");
458  emli->mimetype="invalid_file/invalid_file";
459  emli->extension="invalid_file.invalid_file";
460  emli->version="1.";
461  emli->name=TQString ("Error: %1").arg(*it); // internal
462  emli->nameTranslated=i18n("Error: %1").arg(*it); // translated
463  emli->identifier=(*it);
464 
465  myModeList.append(emli);
466  }
467  }
468  }
469  }
470 
471  // Syncronize with the file katesyntax...rc
472  config.sync();
473 }
KMessageBox::error
static void error(TQWidget *parent, const TQString &text, const TQString &caption=TQString::null, int options=Notify)
KateSyntaxContextData
Class holding the data around the current QDomElement.
Definition: katesyntaxdocument.h:54
KateSyntaxDocument::~KateSyntaxDocument
~KateSyntaxDocument()
Desctructor.
Definition: katesyntaxdocument.cpp:41
KateSyntaxDocument::getGroupInfo
KateSyntaxContextData * getGroupInfo(const TQString &mainGroupName, const TQString &group)
Get the KateSyntaxContextData of the TQDomElement Config inside mainGroupName KateSyntaxContextData::...
Definition: katesyntaxdocument.cpp:263
KateSyntaxDocument::groupItemData
TQString groupItemData(const KateSyntaxContextData *data, const TQString &name)
This function is used to fetch the atributes of the tags.
Definition: katesyntaxdocument.cpp:153
KateSyntaxDocument::KateSyntaxDocument
KateSyntaxDocument(bool force=false)
Constructor Sets the current file to nothing and build the ModeList (katesyntaxhighlightingrc)
Definition: katesyntaxdocument.cpp:34
KateSyntaxDocument::finddata
TQStringList & finddata(const TQString &mainGroup, const TQString &type, bool clearList=true)
Returns a list with all the keywords inside the list type.
Definition: katesyntaxdocument.cpp:278
KateSyntaxDocument::nextItem
bool nextItem(KateSyntaxContextData *data)
Jump to the next item, KateSyntaxContextData::item will point to the next item.
Definition: katesyntaxdocument.cpp:125
KateSyntaxDocument::setIdentifier
bool setIdentifier(const TQString &identifier)
If the open hl file is different from the one needed, it opens the new one and assign some other thin...
Definition: katesyntaxdocument.cpp:51
KateSyntaxDocument::nextGroup
bool nextGroup(KateSyntaxContextData *data)
Jump to the next group, KateSyntaxContextData::currentGroup will point to the next group.
Definition: katesyntaxdocument.cpp:94
KateSyntaxDocument::getConfig
KateSyntaxContextData * getConfig(const TQString &mainGroupName, const TQString &config)
Get the KateSyntaxContextData of the DomElement Config inside mainGroupName It just fills KateSyntaxC...
Definition: katesyntaxdocument.cpp:247
KateSyntaxModeListItem
Information about each syntax hl Mode.
Definition: katesyntaxdocument.h:30
TDEConfig
TDEGlobal::dirs
static TDEStandardDirs * dirs()
TDEStandardDirs::findAllResources
TQStringList findAllResources(const char *type, const TQString &filter=TQString::null, bool recursive=false, bool unique=false) const
endl
kndbgstream & endl(kndbgstream &s)
kdDebug
kdbgstream kdDebug(int area=0)
tdelocale.h

kate

Skip menu "kate"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kate

Skip menu "kate"
  • arts
  • dcop
  • dnssd
  • interfaces
  •   kspeech
  •     interface
  •     library
  •   tdetexteditor
  • kate
  • kded
  • kdoctools
  • kimgio
  • kjs
  • libtdemid
  • libtdescreensaver
  • tdeabc
  • tdecmshell
  • tdecore
  • tdefx
  • tdehtml
  • tdeinit
  • tdeio
  •   bookmarks
  •   httpfilter
  •   kpasswdserver
  •   kssl
  •   tdefile
  •   tdeio
  •   tdeioexec
  • tdeioslave
  •   http
  • tdemdi
  •   tdemdi
  • tdenewstuff
  • tdeparts
  • tdeprint
  • tderandr
  • tderesources
  • tdespell2
  • tdesu
  • tdeui
  • tdeunittest
  • tdeutils
  • tdewallet
Generated for kate by doxygen 1.9.1
This website is maintained by Timothy Pearson.