Xml.cpp

Zbigniew Rebacz, 03/01/2015 12:09 AM

Download (3.15 KB)

 
1
#include "ide.h"
2

    
3
class XmlView : public TopWindow {
4
        typedef XmlView CLASSNAME;
5

    
6
public:
7
        TreeCtrl              xml;
8
        LineEdit              view;
9
        FrameTop<StaticRect>  errorbg;
10
        Label                 error;
11
        ParentCtrl            data;
12
        
13
        XmlView();
14
        
15
        virtual bool Key(dword key, int count);
16
        virtual void Close();
17

    
18
        void Load(const String& txt);
19
        void CopyPath();
20

    
21
        void Serialize(Stream& s);
22

    
23
private:
24
        String Load0(XmlParser& p);
25
        void   Load0(int parent, XmlParser& p);
26
        
27
        void   Reset();
28
};
29

    
30
bool XmlView::Key(dword key, int count)
31
{
32
        if(key == K_ESCAPE) {
33
                Close();
34
                return true;
35
        }
36
        return false;
37
}
38

    
39
XmlView::XmlView()
40
{
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);
56
}
57

    
58
void XmlView::Load(const String& txt)
59
{
60
        Reset();
61
        
62
        XmlParser p(txt);
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);
69
                view.Show();
70
                view <<= txt;
71
                view.SetCursor(view.GetPos(p.GetLine() - 1, p.GetColumn() - 1));
72
                view.SetFocus();
73
                return;
74
        }
75
        
76
        xml.Show();
77
        xml.SetFocus();
78
}
79

    
80
void XmlView::Serialize(Stream& s)
81
{
82
        int version = 0;
83
        s / version;
84
        SerializePlacement(s);
85
}
86

    
87
void XmlView::CopyPath()
88
{
89
        String path;
90
        int id = xml.GetCursor();
91
        while(id >= 0) {
92
                String tag = xml.Get(id);
93
                if(tag.GetCount())
94
                        path = "[" + AsCString(tag) + "]" + path;
95
                id = xml.GetParent(id);
96
        }
97
        WriteClipboardText(path);
98
}
99

    
100
void XmlView::Close()
101
{
102
        StoreToGlobal(*this, "XML view");
103
        TopWindow::Close();
104
}
105

    
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)
124
{
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
}
152

    
153
void XmlView::Reset()
154
{
155
        RemoveFrame(errorbg);
156
        
157
        xml.Clear();
158
        xml.Hide();
159
        view.Clear();
160
        view.Hide();
161
}
162

    
163
void Ide::Xml()
164
{
165
        static XmlView dlg;
166
        dlg.Load(editor.IsSelection() ? editor.GetSelection() : editor.Get());
167
        if(!dlg.IsOpen()) {
168
                LoadFromGlobal(dlg, "XMLview");
169
                dlg.OpenMain();
170
        }
171
}