From dc82ffa6af027e73eee5002ddcbe44e92952c367 Mon Sep 17 00:00:00 2001 From: cxl Date: Sun, 1 Mar 2015 07:36:55 +0000 Subject: [PATCH] ide: Json/Xml features #605 git-svn-id: svn://ultimatepp.org/upp/trunk@8225 f0d560ea-af0d-0410-9eb7-867de7ffcac7 --- uppsrc/ide/Json.cpp | 162 ++++++++++++++++++++++++++++++-------------- uppsrc/ide/Xml.cpp | 156 ++++++++++++++++++++++++------------------ 2 files changed, 202 insertions(+), 116 deletions(-) diff --git a/uppsrc/ide/Json.cpp b/uppsrc/ide/Json.cpp index 2e5f6cde7..65eb31ef7 100644 --- a/uppsrc/ide/Json.cpp +++ b/uppsrc/ide/Json.cpp @@ -1,22 +1,50 @@ #include "ide.h" -struct JsonView : TopWindow { - TreeCtrl tree; +class JsonView : public TopWindow { + typedef JsonView CLASSNAME; + +public: + TreeCtrl tree; + LineEdit view; + FrameTop errorbg; + Label error; + +public: + JsonView(); virtual bool Key(dword key, int count); virtual void Close(); - int AddNode(int parent_id, const Value& id, const String& name, const Value& v); - void Load(const char *json); + void Load(const String& json); void CopyPath(); void Serialize(Stream& s); - typedef JsonView CLASSNAME; +private: + void Reset(); + String Load0(const String& json); - JsonView(); + int AddNode(int parent_id, const Value& id, const String& name, const Value& v); }; +JsonView::JsonView() +{ + Title("JSON view"); + Sizeable().Zoomable(); + Icon(IdeCommonImg::xml()); + + Add(tree.SizePos()); + Add(view.SizePos()); + + error.SetFont(Arial(20)).SetInk(Red); + errorbg.Height(25).Add(error.SizePos()); + view.SetReadOnly(); + view.SetColor(LineEdit::PAPER_READONLY, SColorPaper()); + tree.SetDisplay(QTFDisplay()); + tree.NoRoot(); + tree.WhenLeftDouble = THISBACK(CopyPath); +} + bool JsonView::Key(dword key, int count) { if(key == K_ESCAPE) { @@ -26,10 +54,86 @@ bool JsonView::Key(dword key, int count) return false; } +void JsonView::Load(const String& json) +{ + Reset(); + + String parsingError = Load0(json); + if(parsingError.GetCount() > 0) { + parsingError.Set(0, ToLower(parsingError[0])); + + error = "Json parsing error: \"" + parsingError + "\"."; + AddFrame(errorbg); + view.Show(); + view <<= json; + + return; + } + + tree.Show(); + tree.SetFocus(); +} + +void JsonView::CopyPath() +{ + int id = tree.GetCursor(); + String path; + while(id) { + Value k = tree.Get(id); + if(!IsNull(k)) { + if(IsNumber(k)) + path = "[" + AsString(k) + "]" + path; + if(IsString(k)) + path = "[" + AsCString(String(k)) + "]" + path; + } + id = tree.GetParent(id); + } + WriteClipboardText(path); +} + +void JsonView::Serialize(Stream& s) +{ + int version = 0; + s / version; + SerializePlacement(s); +} + +void JsonView::Close() +{ + StoreToGlobal(*this, "JSONview"); + TopWindow::Close(); +} + +void JsonView::Reset() +{ + RemoveFrame(errorbg); + view.Clear(); + view.Hide(); + tree.Clear(); + tree.Hide(); +} + +String JsonView::Load0(const String& json) +{ + String parsingError; + + try { + tree.Open(AddNode(0, Null, "JSON", ParseJSON(json))); + } + catch(const Exc& e) { + parsingError = e; + } + + return parsingError; +} + int JsonView::AddNode(int parent_id, const Value& id, const String& name, const Value& v) { if(IsError(v)) { - parent_id = tree.Add(parent_id, IdeImg::Error(), "ERROR", "[@R [* " + GetErrorText(v)); + // TODO: Replace with JsonExc or something that is more accurate in this situation. + String errorText = GetErrorText(v); + errorText.Remove(0, errorText.Find(" ") + 1); + throw Exc(errorText); } else if(v.Is()) { @@ -67,50 +171,6 @@ int JsonView::AddNode(int parent_id, const Value& id, const String& name, const return parent_id; } -void JsonView::Load(const char *json) -{ - tree.Clear(); - tree.Open(AddNode(0, Null, "JSON", ParseJSON(json))); -} - -void JsonView::CopyPath() -{ - int id = tree.GetCursor(); - String path; - while(id) { - Value k = tree.Get(id); - if(!IsNull(k)) { - if(IsNumber(k)) - path = "[" + AsString(k) + "]" + path; - if(IsString(k)) - path = "[" + AsCString(String(k)) + "]" + path; - } - id = tree.GetParent(id); - } - WriteClipboardText(path); -} - -void JsonView::Serialize(Stream& s) -{ - int version = 0; - s / version; - SerializePlacement(s); -} - -JsonView::JsonView() -{ - Title("JSON view"); - Add(tree.SizePos()); tree.SetDisplay(QTFDisplay()); tree.NoRoot(); Sizeable().Zoomable(); - Icon(IdeCommonImg::xml()); - tree.WhenLeftDouble = THISBACK(CopyPath); -} - -void JsonView::Close() -{ - StoreToGlobal(*this, "JSONview"); - TopWindow::Close(); -} - void Ide::Json() { static JsonView dlg; diff --git a/uppsrc/ide/Xml.cpp b/uppsrc/ide/Xml.cpp index 34d88c2d9..feafb75d6 100644 --- a/uppsrc/ide/Xml.cpp +++ b/uppsrc/ide/Xml.cpp @@ -1,24 +1,30 @@ #include "ide.h" -struct XmlView : public TopWindow { +class XmlView : public TopWindow { + typedef XmlView CLASSNAME; + +public: TreeCtrl xml; LineEdit view; FrameTop errorbg; Label error; ParentCtrl data; - + + XmlView(); + virtual bool Key(dword key, int count); virtual void Close(); - void Load(int parent, XmlParser& p); void Load(const String& txt); void CopyPath(); - typedef XmlView CLASSNAME; - void Serialize(Stream& s); - XmlView(); +private: + String Load0(XmlParser& p); + void Load0(int parent, XmlParser& p); + + void Reset(); }; bool XmlView::Key(dword key, int count) @@ -30,56 +36,44 @@ bool XmlView::Key(dword key, int count) return false; } -void XmlView::Load(int parent, XmlParser& p) +XmlView::XmlView() { - if(p.IsTag()) { - String tag = p.ReadTag(); - String txt = tag; - for(int i = 0; i < p.GetAttrCount(); i++) - txt << ' ' << p.GetAttr(i) << "=\"" << p[i] << "\""; - parent = xml.Add(parent, IdeImg::XmlTag(), tag, txt); - while(!p.End()) { - if(p.IsEof()) - throw XmlError("Unexpected end of text."); - Load(parent, p); - } - } - else - if(p.IsText()) - xml.Add(parent, IdeImg::XmlText(), Null, NormalizeSpaces(p.ReadText())); - else - if(p.IsPI()) - xml.Add(parent, IdeImg::XmlPI(), Null, NormalizeSpaces(p.ReadPI())); - else - if(p.IsDecl()) - xml.Add(parent, IdeImg::XmlDecl(), Null, NormalizeSpaces(p.ReadDecl())); - else - if(p.IsComment()) - xml.Add(parent, IdeImg::XmlComment(), Null, NormalizeSpaces(p.ReadComment())); - else - throw XmlError("Unexpected input."); + Title("XML view"); + Sizeable().Zoomable(); + Icon(IdeCommonImg::xml()); + + xml.NoRoot(); + + error.SetFont(Arial(20)).SetInk(Red); + errorbg.Height(25).Add(error.SizePos()); + view.SetReadOnly(); + view.SetColor(LineEdit::PAPER_READONLY, SColorPaper()); + + Add(xml.SizePos()); + Add(view.SizePos()); + + xml.WhenLeftDouble = THISBACK(CopyPath); } void XmlView::Load(const String& txt) { - xml.Clear(); + Reset(); + XmlParser p(txt); - p.Relaxed(); - try { - while(!p.IsEof()) - Load(0, p); - } - catch(XmlError e) { - error = "XML parsing error: " + e; + String parsingError = Load0(p); + if(parsingError.GetCount() > 0) { + parsingError.Set(0, ToLower(parsingError[0])); + + error = "XML parsing error: " + parsingError + "."; + AddFrame(errorbg); view.Show(); - xml.Hide(); view <<= txt; view.SetCursor(view.GetPos(p.GetLine() - 1, p.GetColumn() - 1)); view.SetFocus(); return; } + xml.Show(); - view.Hide(); xml.SetFocus(); } @@ -109,29 +103,61 @@ void XmlView::Close() TopWindow::Close(); } -XmlView::XmlView() -{ - Title("XML view"); - - xml.NoRoot(); - - error.SetFont(Arial(20)).SetInk(Red); - errorbg.Height(25).Add(error.SizePos()); - view.AddFrame(errorbg); - view.SetReadOnly(); - view.SetColor(LineEdit::PAPER_READONLY, SColorPaper()); - - xml.Hide(); - view.Hide(); - - Add(xml.SizePos()); - Add(view.SizePos()); - - Sizeable().Zoomable(); - - Icon(IdeCommonImg::xml()); +String XmlView::Load0(XmlParser& p) { + String parsingError; - xml.WhenLeftDouble = THISBACK(CopyPath); + try { + while(!p.IsEof()) + Load0(0, p); + } + catch(const XmlError& e) { + parsingError = e; + } + + if(!parsingError.GetCount() && !xml.GetChildCount(0)) + parsingError = "Not found any XML tags"; + + return parsingError; +} + +void XmlView::Load0(int parent, XmlParser& p) +{ + if(p.IsTag()) { + String tag = p.ReadTag(); + String txt = tag; + for(int i = 0; i < p.GetAttrCount(); i++) + txt << ' ' << p.GetAttr(i) << "=\"" << p[i] << "\""; + parent = xml.Add(parent, IdeImg::XmlTag(), tag, txt); + while(!p.End()) { + if(p.IsEof()) + throw XmlError("Unexpected end of text"); + Load0(parent, p); + } + } + else + if(p.IsText()) + xml.Add(parent, IdeImg::XmlText(), Null, NormalizeSpaces(p.ReadText())); + else + if(p.IsPI()) + xml.Add(parent, IdeImg::XmlPI(), Null, NormalizeSpaces(p.ReadPI())); + else + if(p.IsDecl()) + xml.Add(parent, IdeImg::XmlDecl(), Null, NormalizeSpaces(p.ReadDecl())); + else + if(p.IsComment()) + xml.Add(parent, IdeImg::XmlComment(), Null, NormalizeSpaces(p.ReadComment())); + else + throw XmlError("Unexpected input"); +} + +void XmlView::Reset() +{ + RemoveFrame(errorbg); + + xml.Clear(); + xml.Hide(); + view.Clear(); + view.Hide(); } void Ide::Xml()