From 238fbf6c39e8689bffb12b4f742c39652136921a Mon Sep 17 00:00:00 2001 From: cxl Date: Wed, 15 May 2013 13:16:57 +0000 Subject: [PATCH] Core: XML RegisterEntity git-svn-id: svn://ultimatepp.org/upp/trunk@6072 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/Core/XML.cpp | 48 +++++++++++-------------- uppsrc/Core/XML.h | 4 +++ uppsrc/Core/src.tpp/XmlParser$en-us.tpp | 8 ++++- uppsrc/RichText/EncodeQtf.cpp | 6 ++-- 4 files changed, 33 insertions(+), 33 deletions(-) diff --git a/uppsrc/Core/XML.cpp b/uppsrc/Core/XML.cpp index 8f6c5a24e..a1adc4f84 100644 --- a/uppsrc/Core/XML.cpp +++ b/uppsrc/Core/XML.cpp @@ -177,36 +177,18 @@ void XmlParser::Ent(StringBuffer& out) term = t; return; } - if(t[0] == 'l' && t[1] == 't' && t[2] == ';') { - t += 3; - out.Cat('<'); - } - else - if(t[0] == 'g' && t[1] == 't' && t[2] == ';') { - t += 3; - out.Cat('>'); - } - else - if(t[0] == 'a') - if(t[1] == 'm' && t[2] == 'p' && t[3] == ';') { - t += 4; - out.Cat('&'); + const char *b = t; + while(*t && *t != ';') + t++; + if(*t == ';') { + int q = entity.Find(String(b, t)); + if(q >= 0) { + out.Cat(entity[q]); + term = t + 1; + return; } - else - if(t[1] == 'p' && t[2] == 'o' && t[3] == 's' && t[4] == ';') { - t += 5; - out.Cat('\''); - } - else - out.Cat('&'); - else - if(t[0] == 'q' && t[1] == 'u' && t[2] == 'o' && t[3] == 't' && t[4] == ';') { - t += 5; - out.Cat('\"'); } - else - out.Cat('&'); - term = t; + out.Cat('&'); } inline static bool IsXmlNameChar(int c) @@ -425,6 +407,11 @@ void XmlParser::Next() } } +void XmlParser::RegisterEntity(const String& id, const String& text) +{ + entity.Add(id, text); +} + bool XmlParser::IsEof() { return type == XML_EOF; @@ -677,6 +664,11 @@ int XmlParser::GetColumn() const XmlParser::XmlParser(const char *s) { + RegisterEntity("lt", "<"); + RegisterEntity("gt", ">"); + RegisterEntity("amp", "&"); + RegisterEntity("apos", "\'"); + RegisterEntity("quot", "\""); relaxed = false; empty_tag = false; npreserve = false; diff --git a/uppsrc/Core/XML.h b/uppsrc/Core/XML.h index 95872ff14..d00b7cfb5 100644 --- a/uppsrc/Core/XML.h +++ b/uppsrc/Core/XML.h @@ -46,6 +46,8 @@ class XmlParser { bool preserve_blanks; }; + VectorMap entity; + const char *begin; const char *term; String attr1, attrval1; @@ -73,6 +75,8 @@ class XmlParser { public: void SkipWhites(); + + void RegisterEntity(const String& id, const String& text); bool IsEof(); const char *GetPtr() const { return term; } diff --git a/uppsrc/Core/src.tpp/XmlParser$en-us.tpp b/uppsrc/Core/src.tpp/XmlParser$en-us.tpp index 8b61bc7f4..9ffa5f106 100644 --- a/uppsrc/Core/src.tpp/XmlParser$en-us.tpp +++ b/uppsrc/Core/src.tpp/XmlParser$en-us.tpp @@ -23,6 +23,12 @@ for implementation of descent parsers of XML.&] [s2;%% Skips any whitespaces in the input XML.&] [s3; &] [s4; &] +[s5;:XmlParser`:`:RegisterEntity`(const String`&`,const String`&`): [@(0.0.255) void]_[* R +egisterEntity]([@(0.0.255) const]_[_^String^ String][@(0.0.255) `&]_[*@3 id], +[@(0.0.255) const]_[_^String^ String][@(0.0.255) `&]_[*@3 text])&] +[s2;%% Registers a new XML entity [%-*@3 id] with value [%-*@3 text].&] +[s3;%% &] +[s4; &] [s5;:XmlParser`:`:IsEof`(`): [@(0.0.255) bool]_[* IsEof]()&] [s2;%% Returns true if parser reached the end of text.&] [s3; &] @@ -207,4 +213,4 @@ har]_`*[*@3 s])&] text must be valid through the whole parsing process (XmlParser does not make copy of the text).&] [s3; &] -[s0; ] \ No newline at end of file +[s0; ]] \ No newline at end of file diff --git a/uppsrc/RichText/EncodeQtf.cpp b/uppsrc/RichText/EncodeQtf.cpp index 63b6f2c3b..c24e8afda 100644 --- a/uppsrc/RichText/EncodeQtf.cpp +++ b/uppsrc/RichText/EncodeQtf.cpp @@ -530,8 +530,7 @@ String AsQTF(const RichText& text, byte charset, dword options) } String DeQtf(const char *s) { - String r; - r.Reserve(256); + StringBuffer r; for(; *s; s++) { if(*s == '\n') r.Cat('&'); @@ -545,8 +544,7 @@ String DeQtf(const char *s) { } String DeQtfLf(const char *s) { - String r; - r.Reserve(256); + StringBuffer r; while(*s) { if((byte)*s > ' ' && !IsDigit(*s) && !IsAlpha(*s) && (byte)*s < 128) r.Cat('`');