|
1 # Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
2 # All rights reserved. |
|
3 # This component and the accompanying materials are made available |
|
4 # under the terms of the License "Eclipse Public License v1.0" |
|
5 # which accompanies this distribution, and is available |
|
6 # at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
7 # |
|
8 # Initial Contributors: |
|
9 # Nokia Corporation - initial contribution. |
|
10 # |
|
11 # Contributors: |
|
12 # |
|
13 # Description: |
|
14 # |
|
15 # |
|
16 # Description: |
|
17 # Symbian::CBR::MRP::Reader |
|
18 # |
|
19 |
|
20 package Symbian::CBR::MRP::Reader; |
|
21 |
|
22 use strict; |
|
23 use Carp; |
|
24 use Symbian::CBR::MRP; |
|
25 |
|
26 use base qw(Class::Singleton); |
|
27 |
|
28 sub _new_instance { |
|
29 my $pkg = shift; |
|
30 my $self = {}; |
|
31 |
|
32 # caller(0))[3] gives the package and the method called, e.g. Symbian::CBR::MRP::Reader::_new_instance |
|
33 croak "Invalid number of arguments passed to " . (caller(0))[3] . "\n" if (scalar(@_)); |
|
34 |
|
35 bless $self, $pkg; |
|
36 } |
|
37 |
|
38 sub ReadFile { |
|
39 my $self = shift; |
|
40 my $file = shift; |
|
41 my $type = shift; |
|
42 |
|
43 if (!$file || !$type || shift) { |
|
44 croak "Invalid number of arguments passed to " . (caller(0))[3] . "\n"; |
|
45 } |
|
46 |
|
47 my $mrpObject; |
|
48 |
|
49 # First we create the type of object required... |
|
50 if ($type eq 'MRP') { |
|
51 $mrpObject = Symbian::CBR::MRP->new($file); |
|
52 } |
|
53 elsif ($type eq 'MRPDATA') { |
|
54 if (!eval "require MrpData") { |
|
55 croak "Error: MrpData module is not available\n"; |
|
56 } |
|
57 |
|
58 # PDDEF128617 fix |
|
59 # The envDb uses the path to the mrp file as a key with SRCROOT removed |
|
60 # An earlier MrpData->New was provided data from the envDb to create an MrpData Object |
|
61 # (so the key was mrp location with SRCROOT removed) |
|
62 # The below MrpData->New is given the full filename (because this function has to read the file) |
|
63 # This is wrong, the key for MrpData->New is not the full path |
|
64 # So the below line removes the SRCROOT from the key before providing it to MrpData->New |
|
65 my $localMrpName = $file; |
|
66 if (Utils::WithinSourceRoot($localMrpName)){ |
|
67 $localMrpName = Utils::RemoveSourceRoot($localMrpName); |
|
68 } |
|
69 #The 1 is to tell MrpData not to read the file |
|
70 $mrpObject = MrpData->New($localMrpName, undef, undef, undef, undef, undef ,1); |
|
71 } |
|
72 else { |
|
73 croak "Error: Invalid MRP object type $type\n"; |
|
74 } |
|
75 |
|
76 if ($mrpObject->Populated()) { |
|
77 # MrpData is a multiton, it's possible a populated object has been returned |
|
78 return $mrpObject; |
|
79 } |
|
80 |
|
81 if (!$mrpObject) { |
|
82 croak "Unable to create an $type object for '$file'\n"; |
|
83 } |
|
84 |
|
85 if (!-f $file) { |
|
86 croak "Error: \"$file\" does not exist\n"; |
|
87 } |
|
88 |
|
89 if ($self->{verbose}) { |
|
90 print "Reading $file...\n"; |
|
91 } |
|
92 |
|
93 # Then parse the file and populate the object |
|
94 open MRP, $file or die "Unable to open \"$file\" for reading: $!\n"; |
|
95 |
|
96 while (my $line = <MRP>) { |
|
97 chomp $line; |
|
98 |
|
99 $line =~ s/(?<!\\)#.*$//; # remove comments |
|
100 $line =~ s/^\s+//; |
|
101 next if (!$line); # blank lines |
|
102 |
|
103 my @parts; |
|
104 |
|
105 my $string = $line; |
|
106 while ($string) { |
|
107 if ($string =~ s/^\"(.*?)\"// # Match and remove next quoted string |
|
108 or $string =~ s/^(.*?)\s+// # or, match and remove next (but not last) unquoted string |
|
109 or $string =~ s/^(.*)\s*$//) { # or, match and remove last unquoted string. |
|
110 push (@parts, $1); |
|
111 $string =~ s/^\s+//; # Remove delimiter if present. |
|
112 } |
|
113 } |
|
114 |
|
115 my $keyword = shift @parts; |
|
116 |
|
117 my $remove = ($keyword =~ s/^-//); |
|
118 |
|
119 if (!scalar(@parts) or ($remove && $keyword !~ /binary|testbinary|export_file/)) { |
|
120 croak "Error: Invalid line in \"$file\" \(Line $.\): \"$line\"\n"; |
|
121 } |
|
122 |
|
123 if ($keyword eq 'component') { |
|
124 if (scalar @parts > 1) { |
|
125 croak "Error: Invalid number of arguments to $keyword keyword in \"$file\"\n"; |
|
126 } |
|
127 if (!$mrpObject->SetComponent($parts[0])) { |
|
128 croak "Error: 'component' keyword used more than once in \"$file\"\n"; |
|
129 } |
|
130 } |
|
131 elsif ($keyword eq 'notes_source') { |
|
132 if (scalar @parts > 1) { |
|
133 croak "Error: Invalid number of arguments to $keyword keyword in \"$file\"\n"; |
|
134 } |
|
135 if (!$mrpObject->SetNotesSource($parts[0])) { |
|
136 croak "Error: 'notes_source' keyword used more than once in \"$file\"\n"; |
|
137 } |
|
138 } |
|
139 elsif ($keyword eq 'source') { |
|
140 my $source = join ' ', @parts; |
|
141 if (!$mrpObject->SetSource($source)) { # some source statements contain spaces in the name |
|
142 croak "Error: 'source' entry for \"$source\" defined more than once in \"$file\"\n"; |
|
143 } |
|
144 } |
|
145 elsif ($keyword =~ /^(test)?binary$/) { |
|
146 if (scalar @parts > 4) { |
|
147 croak "Error: Invalid number of arguments to $keyword keyword in \"$file\"\n"; |
|
148 } |
|
149 |
|
150 # SetBinary (operand, test, remove) |
|
151 $mrpObject->SetBinary(\@parts, $1, $remove); |
|
152 } |
|
153 elsif ($keyword =~ /^(test)?exports$/) { |
|
154 if (scalar @parts > 2) { |
|
155 croak "Error: Invalid number of arguments to $keyword keyword in \"$file\"\n"; |
|
156 } |
|
157 |
|
158 # SetExports (operand, test, dependantComponet) |
|
159 $mrpObject->SetExports($parts[0], $1, $parts[1]); |
|
160 } |
|
161 elsif ($keyword eq 'export_file') { |
|
162 if (scalar @parts > 3) { |
|
163 croak "Error: Invalid number of arguments to $keyword keyword in \"$file\"\n"; |
|
164 } |
|
165 |
|
166 # SetExportFile (source, destination, remove, dependantComponet) |
|
167 $mrpObject->SetExportFile($parts[0], $parts[1], $remove, $parts[2]); |
|
168 } |
|
169 elsif ($keyword eq 'ipr') { |
|
170 if (scalar @parts > 3) { |
|
171 croak "Error: Invalid number of arguments to $keyword keyword in \"$file\"\n"; |
|
172 } |
|
173 |
|
174 # SetIPR (category, path, exportRestricted) |
|
175 if ($parts[0] eq 'export-restricted') { |
|
176 if (!$mrpObject->SetIPR($parts[1], $parts[2], 1)) { |
|
177 croak "Error: IPR information for \"$parts[2]\" specified more than once in \"$file\"\n"; |
|
178 } |
|
179 } |
|
180 else { |
|
181 if (!$mrpObject->SetIPR($parts[0], $parts[1], 0)) { |
|
182 croak "Error: IPR information for \"$parts[1]\" specified more than once in \"$file\"\n"; |
|
183 } |
|
184 } |
|
185 } |
|
186 else { |
|
187 croak "Error: Invalid line in \"$file\" \(Line $.\): \"$line\"\n"; |
|
188 } |
|
189 } |
|
190 close MRP; |
|
191 |
|
192 $mrpObject->ValidateParsing(); |
|
193 |
|
194 return $mrpObject; |
|
195 } |
|
196 |
|
197 sub SetVerbose { |
|
198 my $self = shift; |
|
199 |
|
200 $self->{verbose} = 1; |
|
201 } |
|
202 |
|
203 1; |
|
204 |
|
205 __END__ |
|
206 |
|
207 =pod |
|
208 |
|
209 =head1 NAME |
|
210 |
|
211 Symbian::CBR::MRP::Reader - Parses MRP files and returns a populated MRP object |
|
212 |
|
213 =head1 SYNOPSIS |
|
214 |
|
215 use Symbian::CBR::MRP::Reader; |
|
216 |
|
217 # Instantiate an instance of the Symbian::CBR::MRP::Reader object |
|
218 my $mrpReader = Symbian::CBR::MRP::Reader->instance(); |
|
219 |
|
220 my $mrpFile = '\someFolder\anMrpFile.mrp'; |
|
221 |
|
222 # Enable verbose output |
|
223 $mrpReader->SetVerbose(); |
|
224 |
|
225 # Call ReadFile on the mrp reader, specifying the MRP file to parse and the type |
|
226 # of MRP object you want to be populated and returned |
|
227 my $mrpObject = $mrpReader->ReadFile($mrpFile, 'MRP'); |
|
228 |
|
229 ... |
|
230 |
|
231 # Call methods on the returned MRP object |
|
232 $mrpObject->GetIPRInformation(); |
|
233 |
|
234 =head1 DESCRIPTION |
|
235 |
|
236 This module is used to parse MRP files and populate MRP objects. The user can |
|
237 specify the type of MRP object to be populated and returned. This module includes |
|
238 basic MRP syntax checking but stronger syntax checking should be implemented |
|
239 in the MRP object to be populated. |
|
240 |
|
241 =head1 METHODS |
|
242 |
|
243 =head2 instance() |
|
244 |
|
245 Instantiates and returns Symbian::CBR::MRP::Reader object. This object is a |
|
246 singleton. |
|
247 |
|
248 =head2 ReadFile (mrpfile, type) |
|
249 |
|
250 Reads the specified MRP file, instantiates and populates an MRP object of the |
|
251 type specified and then returns the populated MRP object to the caller. |
|
252 |
|
253 Valid MRP types are MRP and MRPDATA. |
|
254 |
|
255 MRP: This is a Symbian::CBR::MRP object. It is a lightweight MRP object and |
|
256 contains only basic MRP functionality. This option should be used when MRP |
|
257 objects are required for tools which are not part of the CBR Tools. See the |
|
258 Symbian::CBR::MRP documentation for more details. |
|
259 |
|
260 MRPDATA: This is an MrpData object, as used by the CBR Tools. This option |
|
261 should only be used for the CBR Tools. See the MrpData documentation |
|
262 for more details. |
|
263 |
|
264 =head2 SetVerbose () |
|
265 |
|
266 Used to set enable verbose output. Once set it is not possible to unset the |
|
267 verbose output. This is because this package is a singleton, and disabling the |
|
268 verbose output could disrupt other code using this same instance. This means |
|
269 that it is not possible to disable the verbosity once it has been enabled. |
|
270 |
|
271 =head1 COPYRIGHT |
|
272 |
|
273 Copyright (c) 2007-2009 Nokia Corporation and/or its subsidiary(-ies). |
|
274 All rights reserved. |
|
275 This component and the accompanying materials are made available |
|
276 under the terms of the License "Eclipse Public License v1.0" |
|
277 which accompanies this distribution, and is available |
|
278 at the URL "http://www.eclipse.org/legal/epl-v10.html". |
|
279 |
|
280 Initial Contributors: |
|
281 Nokia Corporation - initial contribution. |
|
282 |
|
283 Contributors: |
|
284 |
|
285 Description: |
|
286 |
|
287 |
|
288 =cut |