XmlJson.diff
ide/Json.cpp (kopia robocza) | ||
---|---|---|
1 | 1 |
#include "ide.h" |
2 | 2 | |
3 |
struct JsonView : TopWindow { |
|
4 |
TreeCtrl tree; |
|
3 |
class JsonView : public TopWindow { |
|
4 |
typedef JsonView CLASSNAME; |
|
5 | ||
6 |
public: |
|
7 |
TreeCtrl tree; |
|
8 |
LineEdit view; |
|
9 |
FrameTop<StaticRect> errorbg; |
|
10 |
Label error; |
|
5 | 11 |
|
12 |
public: |
|
13 |
JsonView(); |
|
14 |
|
|
6 | 15 |
virtual bool Key(dword key, int count); |
7 | 16 |
virtual void Close(); |
8 | 17 | |
9 |
int AddNode(int parent_id, const Value& id, const String& name, const Value& v); |
|
10 |
void Load(const char *json); |
|
18 |
void Load(const String& json); |
|
11 | 19 |
void CopyPath(); |
12 | 20 | |
13 | 21 |
void Serialize(Stream& s); |
14 | 22 | |
15 |
typedef JsonView CLASSNAME; |
|
23 |
private: |
|
24 |
void Reset(); |
|
25 |
String Load0(const String& json); |
|
16 | 26 |
|
17 |
JsonView();
|
|
27 |
int AddNode(int parent_id, const Value& id, const String& name, const Value& v);
|
|
18 | 28 |
}; |
19 | 29 | |
30 |
JsonView::JsonView() |
|
31 |
{ |
|
32 |
Title("JSON view"); |
|
33 |
Sizeable().Zoomable(); |
|
34 |
Icon(IdeCommonImg::xml()); |
|
35 |
|
|
36 |
Add(tree.SizePos()); |
|
37 |
Add(view.SizePos()); |
|
38 |
|
|
39 |
error.SetFont(Arial(20)).SetInk(Red); |
|
40 |
errorbg.Height(25).Add(error.SizePos()); |
|
41 |
view.SetReadOnly(); |
|
42 |
view.SetColor(LineEdit::PAPER_READONLY, SColorPaper()); |
|
43 |
tree.SetDisplay(QTFDisplay()); |
|
44 |
tree.NoRoot(); |
|
45 |
tree.WhenLeftDouble = THISBACK(CopyPath); |
|
46 |
} |
|
47 | ||
20 | 48 |
bool JsonView::Key(dword key, int count) |
21 | 49 |
{ |
22 | 50 |
if(key == K_ESCAPE) { |
... | ... | |
26 | 54 |
return false; |
27 | 55 |
} |
28 | 56 | |
57 |
void JsonView::Load(const String& json) |
|
58 |
{ |
|
59 |
Reset(); |
|
60 |
|
|
61 |
String parsingError = Load0(json); |
|
62 |
if(parsingError.GetCount() > 0) { |
|
63 |
parsingError.Set(0, ToLower(parsingError[0])); |
|
64 |
|
|
65 |
error = "Json parsing error: \"" + parsingError + "\"."; |
|
66 |
AddFrame(errorbg); |
|
67 |
view.Show(); |
|
68 |
view <<= json; |
|
69 |
|
|
70 |
return; |
|
71 |
} |
|
72 |
|
|
73 |
tree.Show(); |
|
74 |
tree.SetFocus(); |
|
75 |
} |
|
76 | ||
77 |
void JsonView::CopyPath() |
|
78 |
{ |
|
79 |
int id = tree.GetCursor(); |
|
80 |
String path; |
|
81 |
while(id) { |
|
82 |
Value k = tree.Get(id); |
|
83 |
if(!IsNull(k)) { |
|
84 |
if(IsNumber(k)) |
|
85 |
path = "[" + AsString(k) + "]" + path; |
|
86 |
if(IsString(k)) |
|
87 |
path = "[" + AsCString(String(k)) + "]" + path; |
|
88 |
} |
|
89 |
id = tree.GetParent(id); |
|
90 |
} |
|
91 |
WriteClipboardText(path); |
|
92 |
} |
|
93 | ||
94 |
void JsonView::Serialize(Stream& s) |
|
95 |
{ |
|
96 |
int version = 0; |
|
97 |
s / version; |
|
98 |
SerializePlacement(s); |
|
99 |
} |
|
100 | ||
101 |
void JsonView::Close() |
|
102 |
{ |
|
103 |
StoreToGlobal(*this, "JSONview"); |
|
104 |
TopWindow::Close(); |
|
105 |
} |
|
106 | ||
107 |
void JsonView::Reset() |
|
108 |
{ |
|
109 |
RemoveFrame(errorbg); |
|
110 |
view.Clear(); |
|
111 |
view.Hide(); |
|
112 |
tree.Clear(); |
|
113 |
tree.Hide(); |
|
114 |
} |
|
115 | ||
116 |
String JsonView::Load0(const String& json) |
|
117 |
{ |
|
118 |
String parsingError; |
|
119 |
|
|
120 |
try { |
|
121 |
tree.Open(AddNode(0, Null, "JSON", ParseJSON(json))); |
|
122 |
} |
|
123 |
catch(const Exc& e) { |
|
124 |
parsingError = e; |
|
125 |
} |
|
126 |
|
|
127 |
return parsingError; |
|
128 |
} |
|
129 | ||
29 | 130 |
int JsonView::AddNode(int parent_id, const Value& id, const String& name, const Value& v) |
30 | 131 |
{ |
31 | 132 |
if(IsError(v)) { |
32 |
parent_id = tree.Add(parent_id, IdeImg::Error(), "ERROR", "[@R [* " + GetErrorText(v)); |
|
133 |
// TODO: Replace with JsonExc or something that is more accurate in this situation. |
|
134 |
String errorText = GetErrorText(v); |
|
135 |
errorText.Remove(0, errorText.Find(" ") + 1); |
|
136 |
throw Exc(errorText); |
|
33 | 137 |
} |
34 | 138 |
else |
35 | 139 |
if(v.Is<ValueMap>()) { |
... | ... | |
67 | 171 |
return parent_id; |
68 | 172 |
} |
69 | 173 | |
70 |
void JsonView::Load(const char *json) |
|
71 |
{ |
|
72 |
tree.Clear(); |
|
73 |
tree.Open(AddNode(0, Null, "JSON", ParseJSON(json))); |
|
74 |
} |
|
75 | ||
76 |
void JsonView::CopyPath() |
|
77 |
{ |
|
78 |
int id = tree.GetCursor(); |
|
79 |
String path; |
|
80 |
while(id) { |
|
81 |
Value k = tree.Get(id); |
|
82 |
if(!IsNull(k)) { |
|
83 |
if(IsNumber(k)) |
|
84 |
path = "[" + AsString(k) + "]" + path; |
|
85 |
if(IsString(k)) |
|
86 |
path = "[" + AsCString(String(k)) + "]" + path; |
|
87 |
} |
|
88 |
id = tree.GetParent(id); |
|
89 |
} |
|
90 |
WriteClipboardText(path); |
|
91 |
} |
|
92 | ||
93 |
void JsonView::Serialize(Stream& s) |
|
94 |
{ |
|
95 |
int version = 0; |
|
96 |
s / version; |
|
97 |
SerializePlacement(s); |
|
98 |
} |
|
99 | ||
100 |
JsonView::JsonView() |
|
101 |
{ |
|
102 |
Title("JSON view"); |
|
103 |
Add(tree.SizePos()); tree.SetDisplay(QTFDisplay()); tree.NoRoot(); Sizeable().Zoomable(); |
|
104 |
Icon(IdeCommonImg::xml()); |
|
105 |
tree.WhenLeftDouble = THISBACK(CopyPath); |
|
106 |
} |
|
107 | ||
108 |
void JsonView::Close() |
|
109 |
{ |
|
110 |
StoreToGlobal(*this, "JSONview"); |
|
111 |
TopWindow::Close(); |
|
112 |
} |
|
113 | ||
114 | 174 |
void Ide::Json() |
115 | 175 |
{ |
116 | 176 |
static JsonView dlg; |
ide/Xml.cpp (kopia robocza) | ||
---|---|---|
1 | 1 |
#include "ide.h" |
2 | 2 | |
3 |
struct XmlView : public TopWindow { |
|
3 |
class XmlView : public TopWindow { |
|
4 |
typedef XmlView CLASSNAME; |
|
5 | ||
6 |
public: |
|
4 | 7 |
TreeCtrl xml; |
5 | 8 |
LineEdit view; |
6 | 9 |
FrameTop<StaticRect> errorbg; |
7 | 10 |
Label error; |
8 | 11 |
ParentCtrl data; |
9 | ||
12 |
|
|
13 |
XmlView(); |
|
14 |
|
|
10 | 15 |
virtual bool Key(dword key, int count); |
11 | 16 |
virtual void Close(); |
12 | 17 | |
13 |
void Load(int parent, XmlParser& p); |
|
14 | 18 |
void Load(const String& txt); |
15 | 19 |
void CopyPath(); |
16 | 20 | |
17 |
typedef XmlView CLASSNAME; |
|
18 | ||
19 | 21 |
void Serialize(Stream& s); |
20 | 22 | |
21 |
XmlView(); |
|
23 |
private: |
|
24 |
String Load0(XmlParser& p); |
|
25 |
void Load0(int parent, XmlParser& p); |
|
26 |
|
|
27 |
void Reset(); |
|
22 | 28 |
}; |
23 | 29 | |
24 | 30 |
bool XmlView::Key(dword key, int count) |
... | ... | |
30 | 36 |
return false; |
31 | 37 |
} |
32 | 38 | |
33 |
void XmlView::Load(int parent, XmlParser& p)
|
|
39 |
XmlView::XmlView()
|
|
34 | 40 |
{ |
35 |
if(p.IsTag()) { |
|
36 |
String tag = p.ReadTag(); |
|
37 |
String txt = tag; |
|
38 |
for(int i = 0; i < p.GetAttrCount(); i++) |
|
39 |
txt << ' ' << p.GetAttr(i) << "=\"" << p[i] << "\""; |
|
40 |
parent = xml.Add(parent, IdeImg::XmlTag(), tag, txt); |
|
41 |
while(!p.End()) { |
|
42 |
if(p.IsEof()) |
|
43 |
throw XmlError("Unexpected end of text."); |
|
44 |
Load(parent, p); |
|
45 |
} |
|
46 |
} |
|
47 |
else |
|
48 |
if(p.IsText()) |
|
49 |
xml.Add(parent, IdeImg::XmlText(), Null, NormalizeSpaces(p.ReadText())); |
|
50 |
else |
|
51 |
if(p.IsPI()) |
|
52 |
xml.Add(parent, IdeImg::XmlPI(), Null, NormalizeSpaces(p.ReadPI())); |
|
53 |
else |
|
54 |
if(p.IsDecl()) |
|
55 |
xml.Add(parent, IdeImg::XmlDecl(), Null, NormalizeSpaces(p.ReadDecl())); |
|
56 |
else |
|
57 |
if(p.IsComment()) |
|
58 |
xml.Add(parent, IdeImg::XmlComment(), Null, NormalizeSpaces(p.ReadComment())); |
|
59 |
else |
|
60 |
throw XmlError("Unexpected input."); |
|
41 |
Title("XML view"); |
|
42 |
Sizeable().Zoomable(); |
|
43 |
Icon(IdeCommonImg::xml()); |
|
44 | ||
45 |
xml.NoRoot(); |
|
46 | ||
47 |
error.SetFont(Arial(20)).SetInk(Red); |
|
48 |
errorbg.Height(25).Add(error.SizePos()); |
|
49 |
view.SetReadOnly(); |
|
50 |
view.SetColor(LineEdit::PAPER_READONLY, SColorPaper()); |
|
51 | ||
52 |
Add(xml.SizePos()); |
|
53 |
Add(view.SizePos()); |
|
54 | ||
55 |
xml.WhenLeftDouble = THISBACK(CopyPath); |
|
61 | 56 |
} |
62 | 57 | |
63 | 58 |
void XmlView::Load(const String& txt) |
64 | 59 |
{ |
60 |
Reset(); |
|
61 |
|
|
65 | 62 |
XmlParser p(txt); |
66 |
xml.Clear(); |
|
67 |
try { |
|
68 |
while(!p.IsEof()) |
|
69 |
Load(0, p); |
|
70 |
} |
|
71 |
catch(XmlError e) { |
|
72 |
error = "XML parsing error: " + e; |
|
63 |
String parsingError = Load0(p); |
|
64 |
if(parsingError.GetCount() > 0) { |
|
65 |
parsingError.Set(0, ToLower(parsingError[0])); |
|
66 |
|
|
67 |
error = "XML parsing error: " + parsingError + "."; |
|
68 |
AddFrame(errorbg); |
|
73 | 69 |
view.Show(); |
74 | 70 |
view <<= txt; |
75 | 71 |
view.SetCursor(view.GetPos(p.GetLine() - 1, p.GetColumn() - 1)); |
76 | 72 |
view.SetFocus(); |
77 | 73 |
return; |
78 | 74 |
} |
75 |
|
|
79 | 76 |
xml.Show(); |
80 | 77 |
xml.SetFocus(); |
81 | 78 |
} |
... | ... | |
106 | 103 |
TopWindow::Close(); |
107 | 104 |
} |
108 | 105 | |
109 |
XmlView::XmlView() |
|
106 |
String XmlView::Load0(XmlParser& p) { |
|
107 |
String parsingError; |
|
108 |
|
|
109 |
try { |
|
110 |
while(!p.IsEof()) |
|
111 |
Load0(0, p); |
|
112 |
} |
|
113 |
catch(const XmlError& e) { |
|
114 |
parsingError = e; |
|
115 |
} |
|
116 |
|
|
117 |
if(!parsingError.GetCount() && !xml.GetChildCount(0)) |
|
118 |
parsingError = "Not found any XML tags"; |
|
119 |
|
|
120 |
return parsingError; |
|
121 |
} |
|
122 | ||
123 |
void XmlView::Load0(int parent, XmlParser& p) |
|
110 | 124 |
{ |
111 |
Title("XML view"); |
|
125 |
if(p.IsTag()) { |
|
126 |
String tag = p.ReadTag(); |
|
127 |
String txt = tag; |
|
128 |
for(int i = 0; i < p.GetAttrCount(); i++) |
|
129 |
txt << ' ' << p.GetAttr(i) << "=\"" << p[i] << "\""; |
|
130 |
parent = xml.Add(parent, IdeImg::XmlTag(), tag, txt); |
|
131 |
while(!p.End()) { |
|
132 |
if(p.IsEof()) |
|
133 |
throw XmlError("Unexpected end of text"); |
|
134 |
Load0(parent, p); |
|
135 |
} |
|
136 |
} |
|
137 |
else |
|
138 |
if(p.IsText()) |
|
139 |
xml.Add(parent, IdeImg::XmlText(), Null, NormalizeSpaces(p.ReadText())); |
|
140 |
else |
|
141 |
if(p.IsPI()) |
|
142 |
xml.Add(parent, IdeImg::XmlPI(), Null, NormalizeSpaces(p.ReadPI())); |
|
143 |
else |
|
144 |
if(p.IsDecl()) |
|
145 |
xml.Add(parent, IdeImg::XmlDecl(), Null, NormalizeSpaces(p.ReadDecl())); |
|
146 |
else |
|
147 |
if(p.IsComment()) |
|
148 |
xml.Add(parent, IdeImg::XmlComment(), Null, NormalizeSpaces(p.ReadComment())); |
|
149 |
else |
|
150 |
throw XmlError("Unexpected input"); |
|
151 |
} |
|
112 | 152 | |
113 |
xml.NoRoot(); |
|
114 | ||
115 |
error.SetFont(Arial(20)).SetInk(Red); |
|
116 |
errorbg.Height(25).Add(error.SizePos()); |
|
117 |
view.AddFrame(errorbg); |
|
118 |
view.SetReadOnly(); |
|
119 |
view.SetColor(LineEdit::PAPER_READONLY, SColorPaper()); |
|
120 | ||
153 |
void XmlView::Reset() |
|
154 |
{ |
|
155 |
RemoveFrame(errorbg); |
|
156 |
|
|
157 |
xml.Clear(); |
|
121 | 158 |
xml.Hide(); |
159 |
view.Clear(); |
|
122 | 160 |
view.Hide(); |
123 | ||
124 |
Add(xml.SizePos()); |
|
125 |
Add(view.SizePos()); |
|
126 | ||
127 |
Sizeable().Zoomable(); |
|
128 | ||
129 |
Icon(IdeCommonImg::xml()); |
|
130 |
|
|
131 |
xml.WhenLeftDouble = THISBACK(CopyPath); |
|
132 | 161 |
} |
133 | 162 | |
134 | 163 |
void Ide::Xml() |