/
xc.pl
executable file
·245 lines (172 loc) · 4.54 KB
/
xc.pl
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
#!/usr/bin/env perl
=head1 NAME
xc - cross format converter for any of XML, YAML, JSON and INI formats.
=head1 SYNOPSIS
xc -f FORMAT -t FORMAT [OPTIONS]
xc -r FORMAT -w FORMAT [OPTIONS]
xc --from=FORMAT --to=FORMAT [OPTIONS]
=head1 DESCRIPTION
xc is a naive command line tool to convert data between XML, YAML, JSON and INI formats.
=head1 OPTIONS
=over 4
=item B<-h>, B<--help>
Outputs this help page.
=item B<-r FORMAT>, B<-f FORMAT>, B<--from=FORMAT>
The mandatory option assumes the input file has the specified format.
=item B<-w FORMAT>, B<-t FORMAT>, B<--to=FORMAT>
The mandatory option assumes the output file has the specified format.
=item B<--sort>
Sort output (XML and JSON only).
=item B<--pretty>
Beautify the output.
=item B<--skip-empty>
Skip empty fields when parsing and displaying XML.
=item B<--raw>
Make JSON parsing more permissive (enable an end-comma after a last item; shell-style comments and C-style comments; allow bare keys and single quotes).
=item B<--xml-decl[=DECL]>
Output XML declaration.
=item B<--default=SECTION>
Specifies a section for INI files to be used for default values for parameters outside a section.
=back
=head1 FORMATS
The following formats are supported:
=over 4
=item B<INI>
=item B<JSON>
=item B<XML>
=item B<YAML> (B<YML> is allowed as shortcut)
=back
=head1 SEE ALSO
L<JSON>, L<JSON::XS>, L<JSON::PP>
L<YAML::Tiny>
L<XML::Simple>
L<Config::IniFiles>
=head1 COPYRIGHT
Copyright 2017, 2020, 2021 Ildar Shaimordanov E<lt>F<ildar.shaimordanov@gmail.com>E<gt>
MIT
=cut
# =========================================================================
use strict;
use warnings;
use utf8;
use Pod::Usage;
use Getopt::Long;
use XML::Simple;
$XML::Simple::PREFERRED_PARSER = 'XML::SAX::PurePerl';
#$XML::Simple::PREFERRED_PARSER = 'XML::Parser';
use YAML::Tiny;
use JSON -support_by_pp;
use Config::IniFiles;
use Data::Dumper;
$Data::Dumper::Indent = 1;
binmode(STDOUT, ':utf8');
my $ME = $0;
$ME =~ s/.*[\\\/]//;
# =========================================================================
my %opts;
sub set_format {
my ( $name, $k, $v ) = ( shift, shift, lc shift );
die "$ME: Unknown $name: $v\n"
unless $v =~ m/^( xml | ya?ml | json | ini )$/x;
$opts{$k} = $v;
}
pod2usage unless GetOptions(
'help|h' => sub { pod2usage({ -verbose => 2, -noperldoc => 1 }); },
'from|f|r=s' => sub { set_format 'reader', @_; },
'to|t|w=s' => sub { set_format 'writer', @_; },
'sort' => \$opts{sort},
'pretty' => \$opts{pretty},
'skip-empty' => \$opts{skip_empty},
'raw' => \$opts{raw},
'xml-decl:s' => sub { $opts{xml_decl} = $_[1] || 1; },
'default=s' => \$opts{default},
)
and defined $opts{from}
and defined $opts{to}
;
# =========================================================================
my $text;
my $data;
# =========================================================================
$text = do { undef $/; <> };
# =========================================================================
for ( $opts{from} ) {
/xml/ and do {
$data = XMLin($text,
KeepRoot => 1,
SuppressEmpty => $opts{skip_empty},
);
last;
};
/ya?ml/ and do {
$data = Load($text);
last;
};
/json/ and do {
$data = from_json($text, {
allow_barekey => $opts{raw},
allow_singlequote => $opts{raw},
relaxed => $opts{raw},
});
last;
};
/ini/ and do {
tie my %ini, 'Config::IniFiles', (
-file => \$text,
-default => $opts{default},
-fallback => $opts{default},
-nocase => 0,
-allowcontinue => 0,
-allowempty => 0,
-reloadwarn => 0,
-nomultiline => 1,
-handle_trailing_comment => 0,
);
$data = \%ini;
last;
}
}
# =========================================================================
for ( $opts{to} ) {
/xml/ and do {
$text = XMLout($data,
KeepRoot => 1,
NoSort => ! $opts{sort},
NoIndent => ! $opts{pretty},
AttrIndent => $opts{pretty},
SuppressEmpty => $opts{skip_empty},
XMLDecl => $opts{xml_decl},
);
last;
};
/ya?ml/ and do {
$text = Dump($data);
last;
};
/json/ and do {
$text = to_json($data, {
pretty => $opts{pretty},
canonical => $opts{sort},
});
last;
};
/ini/ and do {
tie my %ini, 'Config::IniFiles', (
-default => $opts{default},
-fallback => $opts{default},
-nocase => 0,
-allowcontinue => 0,
-allowempty => 0,
-reloadwarn => 0,
-nomultiline => 1,
-handle_trailing_comment => 0,
);
%ini = %{ $data };
tied( %ini )->OutputConfig();
last;
}
}
# =========================================================================
print $text unless $opts{to} eq 'ini';
# =========================================================================
# EOF