-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtool-33-XML-Formatter-Validator.html
More file actions
429 lines (363 loc) · 15.8 KB
/
Copy pathtool-33-XML-Formatter-Validator.html
File metadata and controls
429 lines (363 loc) · 15.8 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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Tool 33 - XML Formatter Validator - Developer Toolbox</title>
<link rel="stylesheet" href="assets/core.css">
<style>
.split-view {
display: grid;
grid-template-columns: 1fr;
gap: 1.5rem;
margin-bottom: 2rem;
margin-top: 1rem;
}
@media (min-width: 900px) {
.split-view { grid-template-columns: 1fr 1fr; }
}
textarea {
width: 100%;
height: 400px;
background: var(--bg-main);
color: var(--text-primary);
border: 1px solid var(--border-color);
border-radius: var(--radius-sm);
padding: 1rem;
font-family: var(--font-mono);
font-size: 14px;
resize: vertical;
white-space: pre;
overflow-x: auto;
}
.controls {
display: flex;
gap: 1rem;
align-items: center;
background: rgba(255,255,255,0.05);
padding: 1rem;
border-radius: var(--radius-md);
margin-bottom: 1.5rem;
flex-wrap: wrap;
}
.control-group {
display: flex;
align-items: center;
gap: 0.5rem;
cursor: pointer;
}
.control-group input[type="radio"], .control-group input[type="checkbox"] {
width: 1.2rem;
height: 1.2rem;
cursor: pointer;
}
.toolbar {
display: flex;
justify-content: space-between;
align-items: center;
margin-bottom: 0.5rem;
}
.error-panel {
background: rgba(255, 85, 85, 0.1);
border: 1px solid #ff5555;
color: #ffaa00;
padding: 1rem;
border-radius: var(--radius-sm);
margin-bottom: 1.5rem;
display: none;
font-family: var(--font-mono);
white-space: pre-wrap;
}
.success-panel {
background: rgba(46, 160, 67, 0.1);
color: #7ee787;
padding: 0.5rem 1rem;
border-radius: var(--radius-sm);
font-family: var(--font-mono);
font-weight: bold;
display: none;
}
</style>
</head>
<body class="theme-dark">
<div class="tool-canvas-container" id="canvas-container"></div>
<main class="main-content">
<div class="container">
<div class="tool-header">
<div>
<a href="index.html" class="back-link">← Back to Toolbox</a>
<h1>33. XML Formatter Validator</h1>
</div>
<div>
<select id="theme-select" aria-label="Select Theme">
<option value="dark">Dark Theme</option>
<option value="light">Light Theme</option>
<option value="solarized">Solarized</option>
<option value="neon">Neon Glow</option>
<option value="highcontrast">High Contrast</option>
</select>
</div>
</div>
<div class="glass-panel">
<div class="controls">
<span style="font-weight: bold; color: var(--text-secondary);">Output Format:</span>
<label class="control-group">
<input type="radio" name="format-mode" value="format" checked> Format (Pretty Print)
</label>
<label class="control-group">
<input type="radio" name="format-mode" value="minify"> Minify
</label>
<div style="margin-left: auto; display: flex; gap: 1rem; align-items: center;">
<span id="success-msg" class="success-panel">✓ Valid XML</span>
</div>
</div>
<div id="error-panel" class="error-panel"></div>
<div class="split-view">
<div>
<div class="toolbar">
<label style="font-weight: bold; color: var(--text-secondary);">XML Input</label>
<button class="btn btn-sm" id="clear-btn">Clear</button>
</div>
<textarea id="xml-input" placeholder="Paste your XML here..."><?xml version="1.0" encoding="UTF-8"?>
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>Tool 33 - XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Tool 33 - Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies.</description>
</book>
</catalog></textarea>
</div>
<div>
<div class="toolbar">
<label style="font-weight: bold; color: var(--text-secondary);">Formatted Output</label>
<button class="btn btn-sm btn-primary" id="copy-btn">Copy XML</button>
</div>
<textarea id="xml-output" readonly placeholder="Output will appear here..."></textarea>
</div>
</div>
</div>
</div>
</main>
<footer class="global-footer">
<div class="container">
Made with ❤️ by <a href="https://github.com/Aliriyaj007" target="_blank">Aliriyaj007</a>
</div>
</footer>
<script src="assets/core.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r134/three.min.js"></script>
<script>
const xmlInput = document.getElementById('xml-input');
const xmlOutput = document.getElementById('xml-output');
const clearBtn = document.getElementById('clear-btn');
const copyBtn = document.getElementById('copy-btn');
const formatRadios = document.querySelectorAll('input[name="format-mode"]');
const errorPanel = document.getElementById('error-panel');
const successMsg = document.getElementById('success-msg');
// Helper to format XML
const formatXml = (xml, minify = false) => {
let formatted = '';
let pad = 0;
const PADDING = ' ';
// Remove inter-tag whitespace and comments if minifying
const cleanXml = xml.replace(/>\s+</g, '><').replace(/<!--[\s\S]*?-->/g, '');
if (minify) return cleanXml;
// Split into tags
const tags = cleanXml.split(/(<[^>]+>)/g).filter(s => s.trim() !== '');
for (let i = 0; i < tags.length; i++) {
const node = tags[i];
if (node.match(/^<\?xml/)) {
formatted += node + '\n';
} else if (node.match(/^<!/)) {
formatted += PADDING.repeat(pad) + node + '\n';
} else if (node.match(/^<\//)) {
pad--;
formatted += PADDING.repeat(pad) + node + '\n';
} else if (node.match(/^<[^>]*\/>/)) {
formatted += PADDING.repeat(pad) + node + '\n';
} else if (node.match(/^<[^>]*>$/)) {
// Check if next node is text and then closing tag
if (i + 2 < tags.length && !tags[i + 1].match(/^</) && tags[i + 2].match(/^<\//)) {
formatted += PADDING.repeat(pad) + node + tags[i + 1] + tags[i + 2] + '\n';
i += 2;
} else {
formatted += PADDING.repeat(pad) + node + '\n';
pad++;
}
} else {
formatted += PADDING.repeat(pad) + node + '\n';
}
}
return formatted.trim();
};
const processXml = () => {
const xml = xmlInput.value.trim();
errorPanel.style.display = 'none';
successMsg.style.display = 'none';
if (!xml) {
xmlOutput.value = '';
if(window.update3DTreeLevel) window.update3DTreeLevel(0, false);
return;
}
const isMinify = document.querySelector('input[name="format-mode"]:checked').value === 'minify';
// Validate XML
const parser = new DOMParser();
const dom = parser.parseFromString(xml, 'application/xml');
const parseError = dom.querySelector('parsererror');
if (parseError) {
errorPanel.style.display = 'block';
// Extract error message text
errorPanel.textContent = parseError.textContent;
xmlOutput.value = '';
if(window.update3DTreeLevel) window.update3DTreeLevel(0, false);
return;
}
// Valid!
successMsg.style.display = 'block';
try {
// Find max depth for 3D visualization
let maxDepth = 0;
const traverse = (node, depth) => {
if (depth > maxDepth) maxDepth = depth;
for(let i=0; i<node.childNodes.length; i++) {
if(node.childNodes[i].nodeType === 1) { // Element node
traverse(node.childNodes[i], depth + 1);
}
}
};
traverse(dom.documentElement, 1);
xmlOutput.value = formatXml(xml, isMinify);
if(window.update3DTreeLevel) window.update3DTreeLevel(maxDepth, true);
} catch(e) {
errorPanel.style.display = 'block';
errorPanel.textContent = "Formatting error: " + e.message;
}
};
formatRadios.forEach(r => r.addEventListener('change', processXml));
xmlInput.addEventListener('input', processXml);
clearBtn.addEventListener('click', () => {
xmlInput.value = '';
processXml();
});
copyBtn.addEventListener('click', () => {
if(!xmlOutput.value) return;
navigator.clipboard.writeText(xmlOutput.value);
const og = copyBtn.textContent;
copyBtn.textContent = "Copied!";
setTimeout(() => copyBtn.textContent = og, 1500);
});
document.addEventListener('DOMContentLoaded', processXml);
// 3D Visualization: A tree with expanding/collapsing branches
let scene, camera, renderer, treeGroup;
function init3D() {
const container = document.getElementById('canvas-container');
if(!container || !window.THREE) return;
scene = new THREE.Scene();
camera = new THREE.PerspectiveCamera(45, window.innerWidth / window.innerHeight, 0.1, 100);
camera.position.set(0, 5, 12);
camera.lookAt(0, 3, 0);
renderer = new THREE.WebGLRenderer({ alpha: true, antialias: true });
renderer.setSize(window.innerWidth, window.innerHeight);
container.appendChild(renderer.domElement);
const ambientLight = new THREE.AmbientLight(0xffffff, 0.6);
scene.add(ambientLight);
const dirLight = new THREE.DirectionalLight(0xffffff, 0.8);
dirLight.position.set(5, 10, 5);
scene.add(dirLight);
treeGroup = new THREE.Group();
scene.add(treeGroup);
window.addEventListener('resize', () => {
camera.aspect = window.innerWidth / window.innerHeight;
camera.updateProjectionMatrix();
renderer.setSize(window.innerWidth, window.innerHeight);
});
animate();
}
let currentDepth = 0;
let isTreeValid = false;
const branchMat = new THREE.MeshPhongMaterial({ color: 0x88bbff, shininess: 80 });
const errorMat = new THREE.MeshPhongMaterial({ color: 0xff5555, shininess: 80 });
window.update3DTreeLevel = (depth, isValid) => {
if(!treeGroup) return;
// clear old tree
while(treeGroup.children.length > 0) {
treeGroup.remove(treeGroup.children[0]);
}
if(depth === 0) return;
const material = isValid ? branchMat : errorMat;
const maxDepth = Math.min(depth, 5); // cap at 5 for visual clarity
// Recursive function to draw fractal tree
const drawBranch = (len, w, currentLevel) => {
if(currentLevel > maxDepth) return;
const geo = new THREE.CylinderGeometry(w/1.5, w, len, 8);
geo.translate(0, len/2, 0);
const mesh = new THREE.Mesh(geo, material);
treeGroup.add(mesh);
if(currentLevel < maxDepth) {
// Left branch
const left = new THREE.Group();
left.position.y = len;
left.rotation.z = 0.5;
mesh.add(left);
drawBranchSub(left, len * 0.7, w * 0.7, currentLevel + 1);
// Right branch
const right = new THREE.Group();
right.position.y = len;
right.rotation.z = -0.5;
mesh.add(right);
drawBranchSub(right, len * 0.7, w * 0.7, currentLevel + 1);
// Sometimes add a deep branch
if(Math.random() > 0.5) {
const front = new THREE.Group();
front.position.y = len;
front.rotation.x = 0.5;
mesh.add(front);
drawBranchSub(front, len * 0.7, w * 0.7, currentLevel + 1);
}
}
};
const drawBranchSub = (parentGroup, len, w, currentLevel) => {
if(currentLevel > maxDepth) return;
const geo = new THREE.CylinderGeometry(w/1.5, w, len, 8);
geo.translate(0, len/2, 0);
const mesh = new THREE.Mesh(geo, material);
parentGroup.add(mesh);
if(currentLevel < maxDepth) {
const left = new THREE.Group();
left.position.y = len;
left.rotation.z = 0.5 + Math.random()*0.2;
left.rotation.x = (Math.random()-0.5)*0.5;
mesh.add(left);
drawBranchSub(left, len * 0.7, w * 0.7, currentLevel + 1);
const right = new THREE.Group();
right.position.y = len;
right.rotation.z = -0.5 - Math.random()*0.2;
right.rotation.x = (Math.random()-0.5)*0.5;
mesh.add(right);
drawBranchSub(right, len * 0.7, w * 0.7, currentLevel + 1);
}
};
drawBranch(2, 0.4, 1);
treeGroup.position.set(3, 1, 0);
};
function animate() {
requestAnimationFrame(animate);
if(treeGroup && treeGroup.children.length > 0) {
treeGroup.rotation.y = Math.sin(Date.now() * 0.001) * 0.5;
}
renderer.render(scene, camera);
}
document.addEventListener('DOMContentLoaded', init3D);
</script>
</body>
</html>