CH-J SM MobileSerververwaltung über SSH
Menü
Veröffentlichte Quellen

CH-J SM Mobile

Durchsuchen Sie Verzeichnisse und Dateien einer bestimmten Anwendungsausgabe.

Quellen als ZIP herunterladen
CH-J / SM Mobile 175 Dateien
sm-mobile-20261016T185235Z-8e93ecc1b59a Hinzugefügt: · Erstellt:
validation.dart Hinzugefügt: Datum nicht erfasst 2.8 KB · 101 Zeilen
Code Datei herunterladen
1import 'dart:convert';
2import 'package:xml/xml.dart';
3import 'package:yaml/yaml.dart';
5/// A syntax problem found by a validator: 1-based line and column.
6class Problem {
7 final String message;
8 final int? line, column;
9 const Problem(this.message, {this.line, this.column});
12/// Which validator applies to a syntax id, if any.
13String? validatorFor(String syntax) => switch (syntax) {
14 'json' || 'jsonl' => syntax,
15 'yaml' => 'yaml',
16 'xml' || 'plist' || 'svg' => 'xml',
17 _ => null,
18};
20(int, int) _position(String text, int offset) {
21 var line = 1, column = 1;
22 for (var i = 0; i < offset && i < text.length; i++) {
23 if (text.codeUnitAt(i) == 0x0a) {
24 line++;
25 column = 1;
26 } else {
27 column++;
28 }
29 }
30 return (line, column);
33/// Checks [text] without changing it. Returns null when it is valid.
34Problem? validate(String syntax, String text) {
35 switch (validatorFor(syntax)) {
36 case 'json':
37 try {
38 jsonDecode(text);
39 return null;
40 } on FormatException catch (e) {
41 final offset = e.offset;
42 if (offset == null) return Problem(e.message);
43 final (line, column) = _position(text, offset);
44 return Problem(e.message, line: line, column: column);
45 }
46 case 'jsonl':
47 final lines = text.split('\n');
48 for (var i = 0; i < lines.length; i++) {
49 final l = lines[i].trim();
50 if (l.isEmpty) continue;
51 try {
52 jsonDecode(l);
53 } on FormatException catch (e) {
54 return Problem(e.message, line: i + 1, column: (e.offset ?? 0) + 1);
55 }
56 }
57 return null;
58 case 'yaml':
59 try {
60 loadYamlStream(text);
61 return null;
62 } on YamlException catch (e) {
63 final span = e.span;
64 return Problem(
65 e.message,
66 line: span == null ? null : span.start.line + 1,
67 column: span == null ? null : span.start.column + 1,
68 );
69 }
70 case 'xml':
71 try {
72 XmlDocument.parse(text);
73 return null;
74 } on XmlParserException catch (e) {
75 return Problem(e.message, line: e.line, column: e.column);
76 } on XmlTagException catch (e) {
77 return Problem(e.message, line: e.line, column: e.column);
78 } on XmlException catch (e) {
79 return Problem(e.message);
80 }
81 }
82 return null;
85/// Pretty-printed JSON or XML, produced only when the user asks for it.
86/// Returns null when the syntax has no formatter or the text is invalid.
87String? formatText(String syntax, String text, {int indent = 2}) {
88 try {
89 switch (validatorFor(syntax)) {
90 case 'json':
91 return JsonEncoder.withIndent(' ' * indent).convert(jsonDecode(text));
92 case 'xml':
93 return XmlDocument.parse(
94 text,
95 ).toXmlString(pretty: true, indent: ' ' * indent);
96 }
97 } catch (_) {
98 return null;
99 }
100 return null;
SHA-2568b945d931ceeadaa9d9b3b89fc7766c415abf34a3a07b58f407bacc4740addef

CH-J Proprietary Software License 1.14 · Die Quellen werden unter der CH-J Proprietary Software License 1.14 bereitgestellt. Ihre Verfügbarkeit ändert die Lizenzbedingungen nicht und gewährt keine zusätzlichen Rechte.

SHA-256 des Archivs46e427a43e87af091004f36145590bda8fe90086dfb0f631afeb5029fb0b5655
Dateien