Menu

[r7]: / Plugin / lexer_configuration.cpp  Maximize  Restore  History

Download this file

213 lines (185 with data), 8.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
//
// copyright : (C) 2008 by Eran Ifrah
// file name : lexer_configuration.cpp
//
// -------------------------------------------------------------------------
// A
// _____ _ _ _ _
// / __ \ | | | | (_) |
// | / \/ ___ __| | ___| | _| |_ ___
// | | / _ \ / _ |/ _ \ | | | __/ _ )
// | \__/\ (_) | (_| | __/ |___| | || __/
// \____/\___/ \__,_|\___\_____/_|\__\___|
//
// F i l e
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
//////////////////////////////////////////////////////////////////////////////
//////////////////////////////////////////////////////////////////////////////
#include <wx/utils.h>
#include "lexer_configuration.h"
#include "xmlutils.h"
#include "macros.h"
static bool StringTolBool(const wxString &s) {
bool res = s.CmpNoCase(wxT("Yes")) == 0 ? true : false;
return res;
}
LexerConf::LexerConf(const wxString &fileName)
: m_fileName(fileName)
{
m_fileName.MakeAbsolute();
m_doc.Load(m_fileName.GetFullPath());
if(m_doc.GetRoot()){
Parse(m_doc.GetRoot());
}
}
void LexerConf::Save()
{
//replace the root node with the new xml representation for this object
m_doc.SetRoot(ToXml());
if(m_doc.IsOk()){
//we never save to the default file, but rather we create our own copy of it
wxString userExt( wxGetUserName() + wxT("_xml"));
if(m_fileName.GetExt() != userExt) {
m_fileName.SetExt( userExt );
}
m_doc.Save(m_fileName.GetFullPath());
}
}
void LexerConf::Parse(wxXmlNode *element)
{
if( element ){
m_lexerId = XmlUtils::ReadLong(element, wxT("Id"), 0);
// read the lexer name
m_name = element->GetPropVal(wxT("Name"), wxEmptyString);
// load key words
wxXmlNode *node = NULL;
node = XmlUtils::FindFirstByTagName(element, wxT("KeyWords0"));
if( node ){
m_keyWords[0] = node->GetNodeContent();
m_keyWords[0].Replace(wxT("\n"), wxT(" "));
m_keyWords[0].Replace(wxT("\r"), wxT(" "));
}
node = XmlUtils::FindFirstByTagName(element, wxT("KeyWords1"));
if( node ){
m_keyWords[1] = node->GetNodeContent();
m_keyWords[1].Replace(wxT("\n"), wxT(" "));
m_keyWords[1].Replace(wxT("\r"), wxT(" "));
}
node = XmlUtils::FindFirstByTagName(element, wxT("KeyWords2"));
if( node ){
m_keyWords[2] = node->GetNodeContent();
m_keyWords[2].Replace(wxT("\n"), wxT(" "));
m_keyWords[2].Replace(wxT("\r"), wxT(" "));
}
node = XmlUtils::FindFirstByTagName(element, wxT("KeyWords3"));
if( node ){
m_keyWords[3] = node->GetNodeContent();
m_keyWords[3].Replace(wxT("\n"), wxT(" "));
m_keyWords[3].Replace(wxT("\r"), wxT(" "));
}
node = XmlUtils::FindFirstByTagName(element, wxT("KeyWords4"));
if( node ){
m_keyWords[4] = node->GetNodeContent();
m_keyWords[4].Replace(wxT("\n"), wxT(" "));
m_keyWords[4].Replace(wxT("\r"), wxT(" "));
}
// load extensions
node = XmlUtils::FindFirstByTagName(element, wxT("Extensions"));
if( node ){
m_extension = node->GetNodeContent();
}
// load properties
// Search for <properties>
node = XmlUtils::FindFirstByTagName(element, wxT("Properties"));
if( node )
{
// We found the element, read the attributes
wxXmlNode* prop = node->GetChildren();
while( prop )
{
if(prop->GetName() == wxT("Property")){
// Read the font attributes
wxString Name = XmlUtils::ReadString(prop, wxT("Name"), wxT("DEFAULT"));
wxString bold = XmlUtils::ReadString(prop, wxT("Bold"), wxT("no"));
wxString italic = XmlUtils::ReadString(prop, wxT("Italic"), wxT("no"));
wxString underline = XmlUtils::ReadString(prop, wxT("Underline"), wxT("no"));
wxString strikeout = XmlUtils::ReadString(prop, wxT("Strikeout"), wxT("no"));
wxString face = XmlUtils::ReadString(prop, wxT("Face"), wxT("Courier"));
wxString colour = XmlUtils::ReadString(prop, wxT("Colour"), wxT("black"));
wxString bgcolour = XmlUtils::ReadString(prop, wxT("BgColour"), wxT("white"));
long fontSize = XmlUtils::ReadLong(prop, wxT("Size"), 10);
long propId = XmlUtils::ReadLong(prop, wxT("Id"), 0);
StyleProperty property = StyleProperty(propId, colour, bgcolour, fontSize, Name, face,
StringTolBool(bold),
StringTolBool(italic),
StringTolBool(underline));
m_properties.push_back( property );
}
prop = prop->GetNext();
}
}
}
}
LexerConf::~LexerConf()
{
}
wxXmlNode *LexerConf::ToXml() const
{
//convert the lexer back xml node
wxXmlNode *node = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Lexer"));
//set the lexer name
node->AddProperty(wxT("Name"), GetName());
wxString strId;
strId << GetLexerId();
node->AddProperty(wxT("Id"), strId);
//set the keywords node
wxXmlNode *keyWords0 = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("KeyWords0"));
XmlUtils::SetNodeContent(keyWords0, GetKeyWords(0));
node->AddChild(keyWords0);
wxXmlNode *keyWords1 = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("KeyWords1"));
XmlUtils::SetNodeContent(keyWords1, GetKeyWords(1));
node->AddChild(keyWords1);
wxXmlNode *keyWords2 = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("KeyWords2"));
XmlUtils::SetNodeContent(keyWords2, GetKeyWords(2));
node->AddChild(keyWords2);
wxXmlNode *keyWords3 = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("KeyWords3"));
XmlUtils::SetNodeContent(keyWords3, GetKeyWords(3));
node->AddChild(keyWords3);
wxXmlNode *keyWords4 = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("KeyWords4"));
XmlUtils::SetNodeContent(keyWords4, GetKeyWords(4));
node->AddChild(keyWords4);
//set the extensions node
wxXmlNode *extesions = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Extensions"));
XmlUtils::SetNodeContent(extesions, GetFileSpec());
node->AddChild(extesions);
//set the properties
wxXmlNode *properties = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Properties"));
std::list<StyleProperty>::const_iterator iter = m_properties.begin();
for(; iter != m_properties.end(); iter ++){
StyleProperty p = (*iter);
wxXmlNode *property = new wxXmlNode(NULL, wxXML_ELEMENT_NODE, wxT("Property"));
wxString strId;
strId << p.GetId();
property->AddProperty(wxT("Id"), strId);
property->AddProperty(wxT("Name"), p.GetName());
property->AddProperty(wxT("Bold"), BoolToString(p.IsBold()));
property->AddProperty(wxT("Face"), p.GetFaceName());
property->AddProperty(wxT("Colour"), p.GetFgColour());
property->AddProperty(wxT("BgColour"), p.GetBgColour());
property->AddProperty(wxT("Italic"), BoolToString(p.GetItalic()));
property->AddProperty(wxT("Underline"), BoolToString(p.GetUnderlined()));
wxString strSize;
strSize << p.GetFontSize();
property->AddProperty(wxT("Size"), strSize);
properties->AddChild(property);
}
node->AddChild( properties );
return node;
}
Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.