1 # BEGIN BPS TAGGED BLOCK {{{
5 # This software is Copyright (c) 1996-2005 Best Practical Solutions, LLC
6 # <jesse@bestpractical.com>
8 # (Except where explicitly superseded by other copyright notices)
13 # This work is made available to you under the terms of Version 2 of
14 # the GNU General Public License. A copy of that license should have
15 # been provided with this software, but in any event can be snarfed
18 # This work is distributed in the hope that it will be useful, but
19 # WITHOUT ANY WARRANTY; without even the implied warranty of
20 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 # General Public License for more details.
23 # You should have received a copy of the GNU General Public License
24 # along with this program; if not, write to the Free Software
25 # Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
28 # CONTRIBUTION SUBMISSION POLICY:
30 # (The following paragraph is not intended to limit the rights granted
31 # to you to modify and distribute this software under the terms of
32 # the GNU General Public License and is only of importance to you if
33 # you choose to contribute your changes and enhancements to the
34 # community by submitting them to Best Practical Solutions, LLC.)
36 # By intentionally submitting any modifications, corrections or
37 # derivatives to this work, or any other work intended for use with
38 # Request Tracker, to Best Practical Solutions, LLC, you confirm that
39 # you are the copyright holder for those contributions and you grant
40 # Best Practical Solutions, LLC a nonexclusive, worldwide, irrevocable,
41 # royalty-free, perpetual, license to use, copy, create derivative
42 # works based on those contributions, and sublicense and distribute
43 # those contributions and any derivatives thereof.
45 # END BPS TAGGED BLOCK }}}
46 # lib/RT/Interface/REST.pm
49 package RT::Interface::REST;
55 use vars qw($VERSION @ISA @EXPORT);
57 $VERSION = do { my @r = (q$Revision: 1.1.1.3 $ =~ /\d+/g); sprintf "%d."."%02d"x$#r, @r };
60 @EXPORT = qw(expand_list form_parse form_compose vpush vsplit);
63 my $field = '[a-zA-Z][a-zA-Z0-9_-]*';
67 my ($elt, @elts, %elts);
69 foreach $elt (split /,/, $list) {
70 if ($elt =~ /^(\d+)-(\d+)$/) { push @elts, ($1..$2) }
71 else { push @elts, $elt }
75 return sort {$a<=>$b} keys %elts;
78 # Returns a reference to an array of parsed forms.
82 my @lines = split /\n/, $_[0];
83 my ($c, $o, $k, $e) = ("", [], {}, "");
87 my $line = shift @lines;
89 next LINE if $line eq '';
92 # We reached the end of one form. We'll ignore it if it was
93 # empty, and store it otherwise, errors and all.
94 if ($e || $c || @$o) {
95 push @forms, [ $c, $o, $k, $e ];
96 $c = ""; $o = []; $k = {}; $e = "";
100 elsif ($state != -1) {
101 if ($state == 0 && $line =~ /^#/) {
102 # Read an optional block of comments (only) at the start
106 while (@lines && $lines[0] =~ /^#/) {
107 $c .= "\n".shift @lines;
111 elsif ($state <= 1 && $line =~ /^($field):(?:\s+(.*))?$/) {
112 # Read a field: value specification.
116 # Read continuation lines, if any.
117 while (@lines && ($lines[0] eq '' || $lines[0] =~ /^\s+/)) {
118 push @v, shift @lines;
120 pop @v while (@v && $v[-1] eq '');
122 # Strip longest common leading indent from text.
123 my ($ws, $ls) = ("");
124 foreach $ls (map {/^(\s+)/} @v[1..$#v]) {
125 $ws = $ls if (!$ws || length($ls) < length($ws));
129 push(@$o, $f) unless exists $k->{$f};
130 vpush($k, $f, join("\n", @v));
134 elsif ($line !~ /^#/) {
135 # We've found a syntax error, so we'll reconstruct the
136 # form parsed thus far, and add an error marker. (>>)
138 $e = form_compose([[ "", $o, $k, "" ]]);
139 $e.= $line =~ /^>>/ ? "$line\n" : ">> $line\n";
143 # We saw a syntax error earlier, so we'll accumulate the
144 # contents of this form until the end.
148 push(@forms, [ $c, $o, $k, $e ]) if ($e || $c || @$o);
151 foreach $l (keys %$k) {
152 $k->{$l} = vsplit($k->{$l}) if (ref $k->{$l} eq 'ARRAY');
158 # Returns text representing a set of forms.
163 foreach $form (@$forms) {
164 my ($c, $o, $k, $e) = @$form;
179 my @values = (ref $k->{$key} eq 'ARRAY') ?
183 $sp = " "x(length("$key: "));
184 $sp = " "x4 if length($sp) > 16;
186 foreach $v (@values) {
192 push @lines, "$line\n\n";
195 elsif (@lines && $lines[-1] !~ /\n\n$/) {
198 push @lines, "$key: $v\n\n";
201 length($line)+length($v)-rindex($line, "\n") >= 70)
206 $line = $line ? "$line, $v" : "$key: $v";
210 $line = "$key:" unless @values;
213 if (@lines && $lines[-1] !~ /\n\n$/) {
218 push @lines, "$line\n";
222 $text .= join "", @lines;
230 return join "\n--\n\n", @text;
233 # Add a value to a (possibly multi-valued) hash key.
235 my ($hash, $key, $val) = @_;
236 my @val = ref $val eq 'ARRAY' ? @$val : $val;
238 if (exists $hash->{$key}) {
239 unless (ref $hash->{$key} eq 'ARRAY') {
240 my @v = $hash->{$key} ne '' ? $hash->{$key} : ();
243 push @{ $hash->{$key} }, @val;
246 $hash->{$key} = $val;
250 # "Normalise" a hash key that's known to be multi-valued.
253 my ($line, $word, @words);
255 foreach $line (map {split /\n/} (ref $val eq 'ARRAY') ? @$val : $val)
257 # XXX: This should become a real parser, à la Text::ParseWords.
260 push @words, split /\s*,\s*/, $line;
270 RT::Interface::REST - helper functions for the REST interface.
274 Only the REST should use this module.