-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCodePreviewHandler.cs
More file actions
172 lines (157 loc) · 5.75 KB
/
CodePreviewHandler.cs
File metadata and controls
172 lines (157 loc) · 5.75 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
using System;
using System.IO;
using System.Drawing;
using System.Runtime.InteropServices;
using System.Windows.Forms;
using System.Collections.Generic;
using System.Xml.Linq;
using System.Text;
using SharpShell.Attributes;
using SharpShell.SharpPreviewHandler;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
namespace XML_JSON_Preview
{
[ComVisible(true)]
[Guid("A3D6E521-1234-4B5C-9D8E-F0A1B2C3D4E5")]
[COMServerAssociation(AssociationType.Class, ".xml")]
[COMServerAssociation(AssociationType.Class, ".json")]
[PreviewHandler]
public class CodePreviewHandler : SharpPreviewHandler
{
public CodePreviewHandler() { }
protected override PreviewHandlerControl DoPreview()
{
return new CodePreviewControl(SelectedFilePath);
}
}
public class CodePreviewControl : PreviewHandlerControl
{
private TreeView _treeView;
public CodePreviewControl(string filePath)
{
this.BackColor = Color.White;
_treeView = new TreeView();
_treeView.Dock = DockStyle.Fill;
_treeView.BorderStyle = BorderStyle.None;
_treeView.Font = new Font("Consolas", 10);
this.Controls.Add(_treeView);
SafeLoad(filePath);
}
private void SafeLoad(string filePath)
{
try
{
_treeView.Nodes.Clear();
if (string.IsNullOrEmpty(filePath))
{
_treeView.Nodes.Add("Error: Path is empty.");
return;
}
LoadData(filePath);
}
catch (Exception ex)
{
_treeView.Nodes.Add("Global Exception: " + ex.Message);
}
}
private void LoadData(string filePath)
{
string content = "";
try
{
// Čtení souboru, které neblokuje ostatní procesy a poradí si s kódováním
using (var stream = new FileStream(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
using (var reader = new StreamReader(stream, Encoding.UTF8, true))
{
content = reader.ReadToEnd();
}
}
catch (Exception ex)
{
_treeView.Nodes.Add("File Access Error: " + ex.Message);
return;
}
if (string.IsNullOrWhiteSpace(content))
{
_treeView.Nodes.Add("File is empty.");
return;
}
string trimmed = content.Trim();
bool loaded = false;
// 1. Zkoušíme JSON
if (trimmed.StartsWith("{") || trimmed.StartsWith("["))
{
try
{
JToken token = JToken.Parse(trimmed);
AddJsonNode(token, "JSON Root", _treeView.Nodes);
loaded = true;
}
catch (Exception ex) { _treeView.Nodes.Add("JSON Parse Hint: " + ex.Message); }
}
// 2. Zkoušíme XML
if (!loaded)
{
try
{
int xmlStart = trimmed.IndexOf('<');
if (xmlStart >= 0)
{
string cleanXml = trimmed.Substring(xmlStart);
XDocument doc = XDocument.Parse(cleanXml);
if (doc.Root != null)
{
AddXmlNode(doc.Root, _treeView.Nodes);
loaded = true;
}
}
}
catch (Exception ex) { _treeView.Nodes.Add("XML Parse Hint: " + ex.Message); }
}
// 3. Poslední záchrana: TEXT
if (!loaded)
{
_treeView.Nodes.Add("--- Parsing failed, showing raw text ---");
TreeNode textRoot = _treeView.Nodes.Add(Path.GetFileName(filePath));
string[] lines = content.Split(new[] { "\r\n", "\r", "\n" }, StringSplitOptions.None);
for (int i = 0; i < Math.Min(lines.Length, 1000); i++)
{
textRoot.Nodes.Add(lines[i]);
}
}
_treeView.ExpandAll();
if (_treeView.Nodes.Count > 0) _treeView.Nodes[0].EnsureVisible();
}
private void AddJsonNode(JToken token, string name, TreeNodeCollection nodes)
{
try
{
if (token is JValue) nodes.Add($"{name}: {token}");
else if (token is JObject obj)
{
TreeNode newNode = nodes.Add(name);
foreach (var prop in obj.Properties()) AddJsonNode(prop.Value, prop.Name, newNode.Nodes);
}
else if (token is JArray array)
{
TreeNode newNode = nodes.Add($"{name} [array]");
for (int i = 0; i < array.Count; i++) AddJsonNode(array[i], $"[{i}]", newNode.Nodes);
}
}
catch { }
}
private void AddXmlNode(XElement element, TreeNodeCollection nodes)
{
try
{
string nodeText = element.Name.LocalName;
if (!element.HasElements && !string.IsNullOrEmpty(element.Value)) nodeText += ": " + element.Value.Trim();
TreeNode newNode = nodes.Add(nodeText);
foreach (var attr in element.Attributes()) newNode.Nodes.Add($"@{attr.Name}: {attr.Value}");
foreach (var sub in element.Elements()) AddXmlNode(sub, newNode.Nodes);
}
catch { }
}
}
}