/[suikacvs]/messaging/manakai/bin/mkdisdump.pl
Suika

Contents of /messaging/manakai/bin/mkdisdump.pl

Parent Directory Parent Directory | Revision Log Revision Log


Revision 1.4 - (hide annotations) (download)
Mon Sep 5 05:21:11 2005 UTC (19 years, 2 months ago) by wakaba
Branch: MAIN
Changes since 1.3: +144 -60 lines
File MIME type: text/plain
New DOMFeature module introduced

1 wakaba 1.1 use lib qw[../..];
2    
3     #!/usr/bin/perl -w
4     use strict;
5    
6     =head1 NAME
7    
8     cdis2pm - Generating Perl Module from a Compiled "dis"
9    
10     =head1 SYNOPSIS
11    
12     perl path/to/cdis2pm.pl input.cdis \
13     {--module-name=ModuleName | --module-uri=module-uri} \
14     [--for=for-uri] [options] > ModuleName.pm
15     perl path/to/cdis2pm.pl --help
16    
17     =head1 DESCRIPTION
18    
19     The C<cdis2pm> script generates a Perl module from a compiled "dis"
20     ("cdis") file. It is intended to be used to generate a manakai
21     DOM Perl module files, although it might be useful for other purpose.
22    
23     This script is part of manakai.
24    
25     =cut
26    
27     use Message::DOM::DOMHTML;
28     use Message::DOM::DOMLS;
29     use Message::Util::DIS::DISDump;
30     use Message::Util::QName::Filter {
31 wakaba 1.3 ddel => q<http://suika.fam.cx/~wakaba/archive/2005/disdoc#>,
32 wakaba 1.1 ddoct => q<http://suika.fam.cx/~wakaba/archive/2005/8/disdump-xslt#>,
33     DIS => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#>,
34     dis => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#dis-->,
35     dis2pm => q<http://suika.fam.cx/~wakaba/archive/2004/11/8/dis2pm#>,
36     DISCore => q<http://suika.fam.cx/~wakaba/archive/2004/dis/Core#>,
37     DISLang => q<http://suika.fam.cx/~wakaba/archive/2004/dis/Lang#>,
38     DISPerl => q<http://suika.fam.cx/~wakaba/archive/2004/dis/Perl#>,
39     disPerl => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#dis--Perl-->,
40     DOMCore => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/dom-core#>,
41     DOMEvents => q<http://suika.fam.cx/~wakaba/archive/2004/dom/events#>,
42     DOMLS => q<http://suika.fam.cx/~wakaba/archive/2004/dom/ls#>,
43     DOMMain => q<http://suika.fam.cx/~wakaba/archive/2004/dom/main#>,
44     DOMXML => q<http://suika.fam.cx/~wakaba/archive/2004/dom/xml#>,
45     dump => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/DIS#DISDump/>,
46     DX => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/Error/DOMException#>,
47     html5 => q<http://www.w3.org/1999/xhtml>,
48 wakaba 1.3 infoset => q<http://www.w3.org/2001/04/infoset#>,
49 wakaba 1.1 lang => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#>,
50     Perl => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/lang#Perl-->,
51     license => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/license#>,
52     ManakaiDOM => q<http://suika.fam.cx/~wakaba/archive/2004/8/18/manakai-dom#>,
53     Markup => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Markup#>,
54     MDOMX => q<http://suika.fam.cx/~wakaba/archive/2004/8/4/manakai-dom-exception#>,
55     owl => q<http://www.w3.org/2002/07/owl#>,
56     pc => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/PerlCode#>,
57     rdf => q<http://www.w3.org/1999/02/22-rdf-syntax-ns#>,
58     rdfs => q<http://www.w3.org/2000/01/rdf-schema#>,
59     swcfg21 => q<http://suika.fam.cx/~wakaba/archive/2005/swcfg21#>,
60     TreeCore => q<>,
61     Util => q<http://suika.fam.cx/~wakaba/archive/2005/manakai/Util/>,
62     xhtml1 => q<http://www.w3.org/1999/xhtml>,
63     xhtml2 => q<http://www.w3.org/2002/06/xhtml2>,
64     xml => q<http://www.w3.org/XML/1998/namespace>,
65 wakaba 1.3 xmlns => q<http://www.w3.org/2000/xmlns/>,
66 wakaba 1.1 };
67    
68     =head1 OPTIONS
69    
70     =over 4
71    
72     =item --enable-assertion / --noenable-assertion (default)
73    
74     Whether assertion codes should be outputed or not.
75    
76     =item --for=I<for-uri> (Optional)
77    
78     Specifies the "For" URI reference for which the outputed module is.
79     If this parameter is ommitted, the default "For" URI reference
80     for the module, if any, or the C<ManakaiDOM:all> is assumed.
81    
82     =item --help
83    
84     Shows the help message.
85    
86     =item --module-name=I<ModuleName>
87    
88     The name of module to output. It is the local name part of
89     the C<Module> C<QName> in the source "dis" file. Either
90     C<--module-name> or C<--module-uri> is required.
91    
92     =item --module-uri=I<module-uri>
93    
94     A URI reference that identifies a module to output. Either
95     C<--module-name> or C<--module-uri> is required.
96    
97     =item --output-file-path=I<perl-module-file-path> (default: C<STDOUT>)
98    
99     A platform-dependent file name path for the output.
100     If it is not specified, then the generated Perl module
101     content is outputed to the standard output.
102    
103     =item --output-module-version (default) / --nooutput-module-version
104    
105     Whether the C<$VERSION> special variable should be generated or not.
106    
107     =item --verbose / --noverbose (default)
108    
109     Whether a verbose message mode should be selected or not.
110    
111     =back
112    
113     =cut
114    
115     use Getopt::Long;
116     use Pod::Usage;
117     use Storable;
118     use Message::Util::Error;
119 wakaba 1.4 my %Opt = (
120     module_uri => {},
121     );
122 wakaba 1.1 GetOptions (
123     'for=s' => \$Opt{For},
124     'help' => \$Opt{help},
125 wakaba 1.4 'module-uri=s' => sub {
126     shift;
127     $Opt{module_uri}->{+shift} = 1;
128     },
129 wakaba 1.1 'output-file-path=s' => \$Opt{output_file_name},
130     ) or pod2usage (2);
131     pod2usage ({-exitval => 0, -verbose => 1}) if $Opt{help};
132     $Opt{file_name} = shift;
133     pod2usage ({-exitval => 2, -verbose => 0}) unless $Opt{file_name};
134 wakaba 1.4 pod2usage (2) unless keys %{$Opt{module_uri}};
135 wakaba 1.1
136     sub status_msg ($) {
137     my $s = shift;
138     $s .= "\n" unless $s =~ /\n$/;
139     print STDERR $s;
140     }
141    
142     sub status_msg_ ($) {
143     my $s = shift;
144     print STDERR $s;
145     }
146    
147     sub verbose_msg ($) {
148     my $s = shift;
149     $s .= "\n" unless $s =~ /\n$/;
150     print STDERR $s;
151     }
152    
153     sub verbose_msg_ ($) {
154     my $s = shift;
155     print STDERR $s;
156     }
157    
158     my $impl = $Message::DOM::DOMImplementationRegistry->get_dom_implementation
159     ({
160     ExpandedURI q<ManakaiDOM:Minimum> => '3.0',
161     # ExpandedURI q<ManakaiDOM:HTML> => '', # 3.0
162     '+' . ExpandedURI q<DOMLS:LS> => '3.0',
163     '+' . ExpandedURI q<DIS:Doc> => '2.0',
164     ExpandedURI q<DIS:Dump> => '1.0',
165     });
166    
167     ## -- Load input dac database file
168     status_msg_ qq<Opening dac file "$Opt{file_name}"...>;
169     my $db = $impl->get_feature (ExpandedURI q<DIS:Core> => '1.0')
170     ->pl_load_dis_database ($Opt{file_name});
171     status_msg qq<done\n>;
172    
173 wakaba 1.2 our %ReferredResource;
174    
175 wakaba 1.1 sub append_module_documentation (%) {
176     my %opt = @_;
177     my $section = $opt{result_parent}->create_module ($opt{source_resource}->uri);
178 wakaba 1.2
179     add_uri ($opt{source_resource} => $section);
180 wakaba 1.1
181     my $pl_full_name = $opt{source_resource}->pl_fully_qualified_name;
182     if (defined $pl_full_name) {
183     $section->perl_package_name ($pl_full_name);
184     my $path = $pl_full_name;
185     $path =~ s#::#/#g;
186     $section->resource_file_path_stem ($path);
187     $section->set_attribute_ns
188     (ExpandedURI q<ddoct:>, 'ddoct:basePath', '../' x ($path =~ tr#/#/#));
189 wakaba 1.2 $pl_full_name =~ s/.*:://g;
190     $section->perl_name ($pl_full_name);
191 wakaba 1.1 }
192    
193     $section->resource_file_name_stem ($opt{source_resource}->pl_file_name_stem);
194    
195     append_description (source_resource => $opt{source_resource},
196     result_parent => $section);
197    
198 wakaba 1.2 if ($opt{is_partial}) {
199     $section->resource_is_partial (1);
200     return;
201     }
202    
203 wakaba 1.1 for my $rres (@{$opt{source_resource}->get_property_resource_list
204     (ExpandedURI q<DIS:resource>)}) {
205 wakaba 1.4 if ($rres->owner_module eq $opt{source_resource} and## Defined in this module
206     not ($ReferredResource{$rres->uri} < 0)) {
207 wakaba 1.1 ## TODO: Modification required to support modplans
208 wakaba 1.2 status_msg_ "*";
209 wakaba 1.1 if ($rres->is_type_uri (ExpandedURI q<ManakaiDOM:Class>)) {
210     append_class_documentation
211     (result_parent => $section,
212     source_resource => $rres);
213     } elsif ($rres->is_type_uri (ExpandedURI q<ManakaiDOM:IF>)) {
214     append_interface_documentation
215     (result_parent => $section,
216     source_resource => $rres);
217 wakaba 1.3 } elsif ($rres->is_type_uri (ExpandedURI q<DISCore:AbstractDataType>)) {
218     append_datatype_documentation
219     (result_parent => $section,
220     source_resource => $rres);
221 wakaba 1.1 }
222     } else { ## Aliases
223     #
224     }
225     }
226 wakaba 1.2 status_msg "";
227 wakaba 1.1 } # append_module_documentation
228    
229 wakaba 1.3 sub append_datatype_documentation (%) {
230     my %opt = @_;
231     my $section = $opt{result_parent}->create_data_type
232     ($opt{source_resource}->uri);
233    
234     add_uri ($opt{source_resource} => $section);
235    
236 wakaba 1.4 my $uri = $opt{source_resource}->name_uri;
237     if ($uri) {
238     my $fu = $opt{source_resource}->for_uri;
239     unless ($fu eq ExpandedURI q<ManakaiDOM:all>) {
240     $fu =~ /([\w.-]+)[^\w.-]*$/;
241     $uri .= '-' . $1;
242     }
243     } else {
244     $opt{source_resource}->uri;
245     }
246     $uri =~ s#\b(\d\d\d\d+)/(\d\d?)/(\d\d?)#sprintf '%04d%02d%02d', $1, $2, $3#ge;
247     my @file = map {s/[^\w-]/_/g; $_} split m{[/:#?]+}, $uri;
248 wakaba 1.3
249     $section->resource_file_name_stem ($file[-1]);
250     $section->resource_file_path_stem (join '/', @file);
251    
252     my $docr = $opt{source_resource}->get_feature (ExpandedURI q<DIS:Doc>, '2.0');
253     my $label = $docr->get_label ($section->owner_document);
254     if ($label) {
255     $section->create_label->append_child (transform_disdoc_tree ($label));
256     }
257    
258     append_description (source_resource => $opt{source_resource},
259     result_parent => $section);
260    
261     if ($opt{is_partial}) {
262     $section->resource_is_partial (1);
263     return;
264     }
265    
266     ## Inheritance
267     append_inheritance (source_resource => $opt{source_resource},
268     result_parent => $section);
269     } # append_datatype_documentation
270    
271 wakaba 1.1 sub append_interface_documentation (%) {
272     my %opt = @_;
273     my $section = $opt{result_parent}->create_interface
274     ($opt{source_resource}->uri);
275 wakaba 1.2
276     add_uri ($opt{source_resource} => $section);
277 wakaba 1.1
278     my $pl_full_name = $opt{source_resource}->pl_fully_qualified_name;
279     if (defined $pl_full_name) {
280     $section->perl_package_name ($pl_full_name);
281     my $path = $pl_full_name;
282     $path =~ s#::#/#g;
283     $section->resource_file_path_stem ($path);
284     $section->set_attribute_ns
285     (ExpandedURI q<ddoct:>, 'ddoct:basePath',
286     join '', '../' x ($path =~ tr#/#/#));
287 wakaba 1.2 $pl_full_name =~ s/.*:://g;
288     $section->perl_name ($pl_full_name);
289 wakaba 1.1 }
290    
291     $section->resource_file_name_stem ($opt{source_resource}->pl_file_name_stem);
292    
293     $section->is_exception_interface (1)
294     if $opt{source_resource}->is_type_uri
295     (ExpandedURI q<ManakaiDOM:ExceptionIF>);
296    
297     append_description (source_resource => $opt{source_resource},
298     result_parent => $section);
299    
300 wakaba 1.2 if ($opt{is_partial}) {
301     $section->resource_is_partial (1);
302     return;
303     }
304    
305 wakaba 1.1 ## Inheritance
306     append_inheritance (source_resource => $opt{source_resource},
307     result_parent => $section);
308    
309     for my $memres (@{$opt{source_resource}->get_property_resource_list
310     (ExpandedURI q<DIS:childResource>)}) {
311     if ($memres->is_type_uri (ExpandedURI q<DISLang:Method>)) {
312     append_method_documentation (source_resource => $memres,
313     result_parent => $section);
314     } elsif ($memres->is_type_uri (ExpandedURI q<DISLang:Attribute>)) {
315     append_attr_documentation (source_resource => $memres,
316     result_parent => $section);
317     } elsif ($memres->is_type_uri (ExpandedURI q<ManakaiDOM:ConstGroup>)) {
318 wakaba 1.3 append_constgroup_documentation (source_resource => $memres,
319     result_parent => $section);
320 wakaba 1.1 }
321     }
322     } # append_interface_documentation
323    
324     sub append_class_documentation (%) {
325     my %opt = @_;
326     my $section = $opt{result_parent}->create_class ($opt{source_resource}->uri);
327 wakaba 1.2
328     add_uri ($opt{source_resource} => $section);
329 wakaba 1.1
330     my $pl_full_name = $opt{source_resource}->pl_fully_qualified_name;
331     if (defined $pl_full_name) {
332     $section->perl_package_name ($pl_full_name);
333     my $path = $pl_full_name;
334     $path =~ s#::#/#g;
335     $section->resource_file_path_stem ($path);
336     $section->set_attribute_ns
337     (ExpandedURI q<ddoct:>, 'ddoct:basePath', '../' x ($path =~ tr#/#/#));
338 wakaba 1.2 $pl_full_name =~ s/.*:://g;
339     $section->perl_name ($pl_full_name);
340 wakaba 1.1 }
341    
342     $section->resource_file_name_stem ($opt{source_resource}->pl_file_name_stem);
343    
344     append_description (source_resource => $opt{source_resource},
345     result_parent => $section);
346    
347 wakaba 1.2 if ($opt{is_partial}) {
348     $section->resource_is_partial (1);
349     return;
350     }
351    
352 wakaba 1.1 ## Inheritance
353     append_inheritance (source_resource => $opt{source_resource},
354     result_parent => $section,
355     append_implements => 1);
356    
357     for my $memres (@{$opt{source_resource}->get_property_resource_list
358     (ExpandedURI q<DIS:childResource>)}) {
359     if ($memres->is_type_uri (ExpandedURI q<DISLang:Method>)) {
360     append_method_documentation (source_resource => $memres,
361     result_parent => $section);
362     } elsif ($memres->is_type_uri (ExpandedURI q<DISLang:Attribute>)) {
363     append_attr_documentation (source_resource => $memres,
364     result_parent => $section);
365     } elsif ($memres->is_type_uri (ExpandedURI q<ManakaiDOM:ConstGroup>)) {
366 wakaba 1.3 append_constgroup_documentation
367     (source_resource => $memres,
368     result_parent => $section);
369 wakaba 1.1 }
370     }
371     } # append_class_documentation
372    
373     sub append_method_documentation (%) {
374     my %opt = @_;
375     my $perl_name = $opt{source_resource}->pl_name;
376     my $m;
377     if (defined $perl_name) {
378     $m = $opt{result_parent}->create_method ($perl_name);
379    
380     } else { ## Anonymous
381     ## TODO
382     return;
383     }
384 wakaba 1.2
385     add_uri ($opt{source_resource} => $m);
386 wakaba 1.1
387     append_description (source_resource => $opt{source_resource},
388 wakaba 1.4 result_parent => $m,
389     method_resource => $opt{source_resource});
390 wakaba 1.1
391     my $ret = $opt{source_resource}->get_child_resource_by_type
392     (ExpandedURI q<DISLang:MethodReturn>);
393     if ($ret) {
394     my $r = $m->dis_return;
395    
396     try {
397 wakaba 1.2 $r->resource_data_type (my $u = $ret->dis_data_type_resource->uri);
398     $ReferredResource{$u} ||= 1;
399     $r->resource_actual_data_type
400     ($u = $ret->dis_actual_data_type_resource->uri);
401     $ReferredResource{$u} ||= 1;
402    
403 wakaba 1.1 ## TODO: Exceptions
404     } catch Message::Util::DIS::ManakaiDISException with {
405    
406     };
407 wakaba 1.4
408     append_description (source_resource => $ret,
409     result_parent => $r,
410     has_case => 1,
411     method_resource => $opt{source_resource});
412 wakaba 1.1 }
413    
414     for my $cr (@{$opt{source_resource}->get_property_resource_list
415     (ExpandedURI q<DIS:childResource>)}) {
416     if ($cr->is_type_uri (ExpandedURI q<DISLang:MethodParameter>)) {
417     append_param_documentation (source_resource => $cr,
418 wakaba 1.4 result_parent => $m,
419     method_resource => $opt{source_resource});
420 wakaba 1.1 }
421     }
422    
423     ## TODO: raises
424    
425     $m->resource_access ('private')
426     if $opt{source_resource}->get_property_boolean
427     (ExpandedURI q<ManakaiDOM:isForInternal>, 0);
428     } # append_method_documentation
429    
430     sub append_attr_documentation (%) {
431     my %opt = @_;
432     my $perl_name = $opt{source_resource}->pl_name;
433     my $m;
434     if (defined $perl_name) {
435     $m = $opt{result_parent}->create_attribute ($perl_name);
436    
437     } else { ## Anonymous
438     ## TODO
439     return;
440     }
441 wakaba 1.2
442     add_uri ($opt{source_resource} => $m);
443 wakaba 1.1
444     append_description (source_resource => $opt{source_resource},
445     result_parent => $m,
446     has_case => 1);
447    
448     my $ret = $opt{source_resource}->get_child_resource_by_type
449     (ExpandedURI q<DISLang:AttributeGet>);
450     if ($ret) {
451     my $r = $m->dis_get;
452    
453 wakaba 1.2 $r->resource_data_type (my $u = $ret->dis_data_type_resource->uri);
454     $ReferredResource{$u} ||= 1;
455     $r->resource_actual_data_type
456     ($u = $ret->dis_actual_data_type_resource->uri);
457     $ReferredResource{$u} ||= 1;
458    
459 wakaba 1.1 append_description (source_resource => $ret,
460     result_parent => $r,
461     has_case => 1);
462    
463     ## TODO: Exceptions
464     }
465    
466     my $set = $opt{source_resource}->get_child_resource_by_type
467     (ExpandedURI q<DISLang:AttributeSet>);
468     if ($set) {
469     my $r = $m->dis_set;
470    
471 wakaba 1.2 $r->resource_data_type (my $u = $set->dis_data_type_resource->uri);
472     $ReferredResource{$u} ||= 1;
473 wakaba 1.1 $r->resource_actual_data_type ($set->dis_actual_data_type_resource->uri);
474 wakaba 1.2 $ReferredResource{$u} ||= 1;
475 wakaba 1.1
476     append_description (source_resource => $set,
477     result_parent => $r,
478     has_case => 1);
479    
480     ## TODO: InCase, Exceptions
481     } else {
482     $m->is_read_only_attribute (1);
483     }
484    
485     $m->resource_access ('private')
486     if $opt{source_resource}->get_property_boolean
487     (ExpandedURI q<ManakaiDOM:isForInternal>, 0);
488     } # append_attr_documentation
489    
490 wakaba 1.3 sub append_constgroup_documentation (%) {
491     my %opt = @_;
492     my $perl_name = $opt{source_resource}->pl_name;
493     my $m = $opt{result_parent}->create_const_group ($perl_name);
494    
495     add_uri ($opt{source_resource} => $m);
496    
497     append_description (source_resource => $opt{source_resource},
498     result_parent => $m);
499    
500     $m->resource_data_type
501     (my $u = $opt{source_resource}->dis_data_type_resource->uri);
502     $ReferredResource{$u} ||= 1;
503     $m->resource_actual_data_type
504     ($u = $opt{source_resource}->dis_actual_data_type_resource->uri);
505     $ReferredResource{$u} ||= 1;
506    
507    
508     for my $cr (@{$opt{source_resource}->get_property_resource_list
509     (ExpandedURI q<DIS:childResource>)}) {
510     if ($cr->is_type_uri (ExpandedURI q<ManakaiDOM:Const>)) {
511     append_const_documentation (source_resource => $cr,
512     result_parent => $m);
513     }
514     }
515     } # append_constgroup_documentation
516    
517     sub append_const_documentation (%) {
518     my %opt = @_;
519     my $perl_name = $opt{source_resource}->pl_name;
520     my $m = $opt{result_parent}->create_const ($perl_name);
521    
522     add_uri ($opt{source_resource} => $m);
523    
524     append_description (source_resource => $opt{source_resource},
525     result_parent => $m);
526    
527     $m->resource_data_type
528     (my $u = $opt{source_resource}->dis_data_type_resource->uri);
529     $ReferredResource{$u} ||= 1;
530     $m->resource_actual_data_type
531     ($u = $opt{source_resource}->dis_actual_data_type_resource->uri);
532     $ReferredResource{$u} ||= 1;
533    
534     my $value = $opt{source_resource}->pl_code_fragment;
535     if ($value) {
536     $m->create_value->text_content ($value->stringify);
537     }
538    
539     for my $cr (@{$opt{source_resource}->get_property_resource_list
540     (ExpandedURI q<DIS:childResource>)}) {
541     if ($cr->is_type_uri (ExpandedURI q<ManakaiDOM:ExceptionOrWarningSubType>)) {
542     append_xsubtype_documentation (source_resource => $cr,
543     result_parent => $m);
544     }
545     }
546     ## TODO: xparam
547     } # append_const_documentation
548    
549     sub append_xsubtype_documentation (%) {
550     my %opt = @_;
551     my $m = $opt{result_parent}->create_exception_sub_code
552     ($opt{source_resource}->uri);
553     add_uri ($opt{source_resource} => $m);
554    
555     append_description (source_resource => $opt{source_resource},
556     result_parent => $m);
557    
558     ## TODO: xparam
559     } # append_xsubtype_documentation
560    
561 wakaba 1.1 sub append_param_documentation (%) {
562     my %opt = @_;
563    
564     my $is_named_param = $opt{source_resource}->get_property_boolean
565     (ExpandedURI q<DISPerl:isNamedParameter>, 0);
566    
567     my $perl_name = $is_named_param
568     ? $opt{source_resource}->pl_name
569     : $opt{source_resource}->pl_variable_name;
570    
571     my $p = $opt{result_parent}->create_parameter ($perl_name, $is_named_param);
572    
573 wakaba 1.2 add_uri ($opt{source_resource} => $p);
574    
575 wakaba 1.1 $p->is_nullable_parameter ($opt{source_resource}->pl_is_nullable);
576 wakaba 1.2 $p->resource_data_type
577     (my $u = $opt{source_resource}->dis_data_type_resource->uri);
578     $ReferredResource{$u} ||= 1;
579 wakaba 1.1 $p->resource_actual_data_type
580 wakaba 1.2 ($u = $opt{source_resource}->dis_actual_data_type_resource->uri);
581     $ReferredResource{$u} ||= 1;
582 wakaba 1.1
583     append_description (source_resource => $opt{source_resource},
584     result_parent => $p,
585 wakaba 1.4 has_case => 1,
586     method_resource => $opt{method_resource});
587 wakaba 1.1 } # append_param_documentation
588    
589     sub append_description (%) {
590     my %opt = @_;
591 wakaba 1.2
592 wakaba 1.1 my $od = $opt{result_parent}->owner_document;
593     my $resd = $opt{source_resource}->get_feature (ExpandedURI q<DIS:Doc>, '2.0');
594 wakaba 1.4 my $doc = transform_disdoc_tree ($resd->get_description ($od),
595     method_resource => $opt{method_resource});
596 wakaba 1.1 $opt{result_parent}->create_description->append_child ($doc);
597     ## TODO: Negotiation
598    
599 wakaba 1.3 my $fn = $resd->get_full_name ($od);
600     if ($fn) {
601     $opt{result_parent}->create_full_name
602 wakaba 1.4 ->append_child (transform_disdoc_tree
603     ($fn,
604     method_resource => $opt{method_resource}));
605 wakaba 1.3 }
606    
607 wakaba 1.1 if ($opt{has_case}) {
608     for my $caser (@{$opt{source_resource}->get_property_resource_list
609     (ExpandedURI q<DIS:childResource>)}) {
610     if ($caser->is_type_uri (ExpandedURI q<ManakaiDOM:InCase>)) {
611     my $case = $opt{result_parent}->append_case;
612     my $cased = $caser->get_feature (ExpandedURI q<DIS:Doc>, '2.0');
613     my $label = $cased->get_label ($od);
614     if ($label) {
615 wakaba 1.4 $case->create_label->append_child
616     (transform_disdoc_tree ($label,
617     method_resource => $opt{method_resource}));
618 wakaba 1.1 }
619     my $value = $caser->pl_code_fragment;
620     if ($value) {
621     $case->create_value->text_content ($value->stringify);
622     }
623 wakaba 1.3 $case->resource_data_type
624     (my $u = $caser->dis_data_type_resource->uri);
625     $ReferredResource{$u} ||= 1;
626     $case->resource_actual_data_type
627     ($u = $caser->dis_actual_data_type_resource->uri);
628     $ReferredResource{$u} ||= 1;
629    
630 wakaba 1.1 append_description (source_resource => $caser,
631 wakaba 1.4 result_parent => $case,
632     method_resource => $opt{method_resource});
633 wakaba 1.1 }
634     }
635     }
636     } # append_description
637    
638 wakaba 1.3 sub transform_disdoc_tree ($;%) {
639     my ($el, %opt) = @_;
640     my @el = ($el);
641     EL: while (defined (my $el = shift @el)) {
642     if ($el->node_type == $el->ELEMENT_NODE and
643     defined $el->namespace_uri) {
644     my $mmParsed = $el->get_attribute_ns (ExpandedURI q<ddel:>, 'mmParsed');
645     if ($mmParsed) {
646     my $lextype = $el->get_attribute_ns (ExpandedURI q<ddel:>, 'lexType');
647     if ($lextype eq ExpandedURI q<dis:TFQNames>) {
648 wakaba 1.4 my $uri = dd_get_tfqnames_uri ($el);
649     if (defined $uri) {
650     $ReferredResource{$uri} ||= 1;
651     next EL;
652     }
653     } elsif ($lextype eq ExpandedURI q<DISPerl:MemRef>) {
654     my @nm = @{$el->get_elements_by_tag_name_ns
655     (ExpandedURI q<ddel:>, 'name')};
656     if (@nm == 1) {
657     my $uri = dd_get_tfqnames_uri ($el);
658     if (defined $uri) {
659     $ReferredResource{$uri} ||= 1;
660     next EL;
661     }
662     } elsif (@nm == 2) {
663     my $uri = dd_get_tfqnames_uri ($nm[0]);
664     if (not defined $uri) {
665     #
666     } elsif ($nm[1]->get_elements_by_tag_name_ns
667     (ExpandedURI q<ddel:>, 'prefix')->[0]) {
668     #my $luri = dd_get_qname_uri ($nm[1]);
669     ## QName: Currently not used
670     } else {
671     my $lnel = $nm[1]->get_elements_by_tag_name_ns
672     (ExpandedURI q<ddel:>, 'localName')->[0];
673     my $lname = $lnel ? $lnel->text_content : '';
674     ## NOTE: $db
675     my $res = $db->get_resource ($uri)
676     ->get_child_resource_by_name_and_type
677     ($lname, ExpandedURI q<DISLang:AnyMethod>);
678     if ($res) {
679     $el->set_attribute_ns
680     (ExpandedURI q<dump:>, 'dump:uri', $res->uri);
681     $ReferredResource{$res->uri} ||= 1;
682     }
683     next EL;
684     }
685     }
686     } # lextype
687     } # mmParsed
688     elsif ($opt{method_resource} and
689     $el->namespace_uri eq ExpandedURI q<ddel:> and
690     $el->local_name eq 'P') {
691     my $res = $opt{method_resource}
692     ->get_child_resource_by_name_and_type
693     ($el->text_content, ExpandedURI q<DISLang:MethodParameter>);
694     if ($res) {
695     $el->set_attribute_ns
696     (ExpandedURI q<dump:>, 'dump:uri', $res->uri);
697     $ReferredResource{$res->uri} ||= 1;
698 wakaba 1.3 }
699 wakaba 1.4 next EL;
700 wakaba 1.3 }
701 wakaba 1.4 push @el, @{$el->child_nodes};
702 wakaba 1.3 } elsif ($el->node_type == $el->DOCUMENT_FRAGMENT_NODE or
703     $el->node_type == $el->DOCUMENT_NODE) {
704 wakaba 1.4 push @el, @{$el->child_nodes};
705 wakaba 1.3 }
706     } # EL
707     $el;
708     } # transform_disdoc_tree
709    
710 wakaba 1.4 sub dd_get_tfqnames_uri ($;%) {
711     my ($el, %opt) = @_;
712     return '' unless $el;
713     my $turi = dd_get_qname_uri ($el->get_elements_by_tag_name_ns
714     (ExpandedURI q<ddel:>, 'nameQName')->[0]);
715     my $furi = dd_get_qname_uri ($el->get_elements_by_tag_name_ns
716     (ExpandedURI q<ddel:>, 'forQName')->[0]);
717     return undef if not defined $turi or not defined $furi;
718     my $uri = tfuris2uri ($turi, $furi);
719     $el->set_attribute_ns (ExpandedURI q<dump:>, 'dump:uri', $uri);
720     $uri;
721     } # dd_get_tfqnames_uri
722 wakaba 1.3
723     sub dd_get_qname_uri ($;%) {
724     my ($el, %opt) = @_;
725 wakaba 1.4 return undef unless $el;
726 wakaba 1.3 my $plel = $el->get_elements_by_tag_name_ns
727     (ExpandedURI q<ddel:>, 'prefix')->[0];
728     my $lnel = $el->get_elements_by_tag_name_ns
729     (ExpandedURI q<ddel:>, 'localName')->[0];
730     my $nsuri = ($plel ? $plel : $el)->lookup_namespace_uri
731     ($plel ? $plel->text_content : undef);
732     $nsuri = '' unless defined $nsuri;
733     if ($plel and $nsuri eq '') {
734     $plel->remove_attribute_ns
735 wakaba 1.4 (ExpandedURI q<xmlns:>, $plel->text_content);
736     $el->set_attribute_ns (ExpandedURI q<dump:>, 'dump:namespaceURI', $nsuri);
737     return undef;
738     } else {
739     $el->set_attribute_ns (ExpandedURI q<dump:>, 'dump:namespaceURI', $nsuri);
740 wakaba 1.3 }
741     if ($lnel) {
742     $nsuri . $lnel->text_content;
743     } else {
744     $el->get_attribute_ns (ExpandedURI q<ddel:>, 'defaultURI');
745     }
746     } # dd_get_qname_uri
747    
748     sub tfuris2uri ($$) {
749     my ($turi, $furi) = @_;
750     my $uri;
751     if ($furi eq <Q::ManakaiDOM:all>) {
752     $uri = $turi;
753     } else {
754     my $__turi = $turi;
755     my $__furi = $furi;
756     for my $__uri ($__turi, $__furi) {
757     $__uri =~ s{([^0-9A-Za-z:;?=_./-])}{sprintf '%%%02X', ord $1}ge;
758     }
759     $uri = qq<data:,200411tf#xmlns(t=data:,200411tf%23)>.
760     qq<t:tf($__turi,$__furi)>;
761     }
762     $uri;
763     } # tfuris2uri
764    
765 wakaba 1.1 sub append_inheritance (%) {
766     my %opt = @_;
767     if (($opt{depth} ||= 0) == 100) {
768     warn "<".$opt{source_resource}->uri.">: Loop in inheritance";
769     return;
770     }
771    
772     for my $isa (@{$opt{source_resource}->get_property_resource_list
773     (ExpandedURI q<dis:ISA>,
774     default_media_type => ExpandedURI q<dis:TFQNames>)}) {
775     append_inheritance
776     (source_resource => $isa,
777     result_parent => $opt{result_parent}->append_new_extends ($isa->uri),
778     depth => $opt{depth} + 1);
779 wakaba 1.2 $ReferredResource{$isa->uri} ||= 1;
780 wakaba 1.1 }
781    
782     if ($opt{append_implements}) {
783     for my $impl (@{$opt{source_resource}->get_property_resource_list
784     (ExpandedURI q<dis:Implement>,
785     default_media_type => ExpandedURI q<dis:TFQNames>,
786 wakaba 1.4 isa_recursive => 1)}) {
787 wakaba 1.1 append_inheritance
788     (source_resource => $impl,
789     result_parent => $opt{result_parent}->append_new_implements
790     ($impl->uri),
791     depth => $opt{depth});
792 wakaba 1.2 $ReferredResource{$impl->uri} ||= 1;
793 wakaba 1.1 }
794     }
795     } # append_inheritance
796    
797 wakaba 1.2 sub add_uri ($$;%) {
798     my ($res, $el, %opt) = @_;
799     my $canon_uri = $res->uri;
800     for my $uri (@{$res->uris}) {
801     $el->add_uri ($uri, $canon_uri eq $uri ? 0 : 1);
802     $ReferredResource{$uri} = -1;
803     }
804 wakaba 1.3
805     my $nsuri = $res->namespace_uri;
806     $el->resource_namespace_uri ($nsuri) if defined $nsuri;
807     my $lname = $res->local_name;
808     $el->resource_local_name ($lname) if defined $lname;
809 wakaba 1.2 } # add_uri
810    
811 wakaba 1.4
812    
813 wakaba 1.1 my $doc = $impl->create_disdump_document;
814    
815     my $body = $doc->document_element;
816    
817    
818 wakaba 1.4 ## -- Outputs requested modules
819    
820     for my $mod_uri (keys %{$Opt{module_uri}}) {
821     my $mod = $db->get_module ($mod_uri, for_arg => $Opt{For});
822     unless ($Opt{For}) {
823     my $el = $mod->source_element;
824     if ($el) {
825     $Opt{For} = $el->default_for_uri;
826     $mod = $db->get_module ($mod_uri, for_arg => $Opt{For});
827     }
828     }
829     unless ($mod->is_defined) {
830     die qq<$0: Module <$mod_uri> for <$Opt{For}> is not defined>;
831     }
832    
833     status_msg qq<Module <$mod_uri> for <$Opt{For}>...>;
834    
835     append_module_documentation
836     (result_parent => $body,
837     source_resource => $mod);
838     } # mod_uri
839    
840     ## -- Outputs referenced resources in external modules
841 wakaba 1.2
842     while (my @ruri = grep {$ReferredResource{$_} > 0} keys %ReferredResource) {
843     U: while (defined (my $uri = shift @ruri)) {
844     next U if $ReferredResource{$uri} < 0; ## Already done
845     my $res = $db->get_resource ($uri);
846     unless ($res->is_defined) {
847     $res = $db->get_module ($uri);
848     unless ($res->is_defined) {
849     $ReferredResource{$uri} = -1;
850     next U;
851     }
852     append_module_documentation
853     (result_parent => $body,
854     source_resource => $res,
855     is_partial => 1);
856     } elsif ($res->is_type_uri (ExpandedURI q<ManakaiDOM:Class>)) {
857     my $mod = $res->owner_module;
858     unless ($ReferredResource{$mod->uri} < 0) {
859     unshift @ruri, $uri;
860     unshift @ruri, $mod->uri;
861     next U;
862     }
863     append_class_documentation
864     (result_parent => $body->create_module ($mod->uri),
865     source_resource => $res,
866     is_partial => 1);
867     } elsif ($res->is_type_uri (ExpandedURI q<ManakaiDOM:IF>)) {
868     my $mod = $res->owner_module;
869     unless ($mod->is_defined) {
870     $ReferredResource{$uri} = -1;
871     next U;
872     } elsif (not ($ReferredResource{$mod->uri} < 0)) {
873     unshift @ruri, $uri;
874     unshift @ruri, $mod->uri;
875     next U;
876     }
877     append_interface_documentation
878     (result_parent => $body->create_module ($mod->uri),
879     source_resource => $res,
880     is_partial => 1);
881 wakaba 1.3 } elsif ($res->is_type_uri (ExpandedURI q<DISCore:AbstractDataType>)) {
882     my $mod = $res->owner_module;
883     unless ($mod->is_defined) {
884     $ReferredResource{$uri} = -1;
885     next U;
886     } elsif (not ($ReferredResource{$mod->uri} < 0)) {
887     unshift @ruri, $uri;
888     unshift @ruri, $mod->uri;
889     next U;
890     }
891     append_datatype_documentation
892     (result_parent => $body->create_module ($mod->uri),
893     source_resource => $res);
894     } elsif ($res->is_type_uri (ExpandedURI q<DISLang:AnyMethod>) or
895     $res->is_type_uri (ExpandedURI q<ManakaiDOM:ConstGroup>)) {
896 wakaba 1.2 my $cls = $res->get_property_resource
897     (ExpandedURI q<dis2pm:parentResource>);
898     if (not ($ReferredResource{$cls->uri} < 0) and
899     ($cls->is_type_uri (ExpandedURI q<ManakaiDOM:Class>) or
900     $cls->is_type_uri (ExpandedURI q<ManakaiDOM:IF>))) {
901     unshift @ruri, $uri;
902     unshift @ruri, $cls->uri;
903     next U;
904     }
905     my $model = $body->create_module ($cls->owner_module->uri);
906     my $clsel = $cls->is_type_uri (ExpandedURI q<ManakaiDOM:Class>)
907     ? $model->create_class ($cls->uri)
908     : $model->create_interface ($cls->uri);
909     if ($res->is_type_uri (ExpandedURI q<DISLang:Method>)) {
910     append_method_documentation
911     (result_parent => $clsel,
912     source_resource => $res);
913 wakaba 1.3 } elsif ($res->is_type_uri (ExpandedURI q<DISLang:Attribute>)) {
914 wakaba 1.2 append_attr_documentation
915 wakaba 1.3 (result_parent => $clsel,
916     source_resource => $res);
917     } elsif ($res->is_type_uri (ExpandedURI q<ManakaiDOM:ConstGroup>)) {
918     append_constgroup_documentation
919     (result_parent => $clsel,
920 wakaba 1.2 source_resource => $res);
921     }
922     } elsif ($res->is_type_uri (ExpandedURI q<DISLang:MethodParameter>)) {
923     my $m = $res->get_property_resource
924     (ExpandedURI q<dis2pm:parentResource>);
925     if (not ($ReferredResource{$m->uri} < 0) and
926     $m->is_type_uri (ExpandedURI q<DISLang:Method>)) {
927     unshift @ruri, $m->uri;
928 wakaba 1.4 $ReferredResource{$res->uri} = -1;
929 wakaba 1.2 next U;
930     }
931 wakaba 1.3 } elsif ($res->is_type_uri (ExpandedURI q<ManakaiDOM:Const>)) {
932     my $m = $res->get_property_resource
933     (ExpandedURI q<dis2pm:parentResource>);
934     if (not ($ReferredResource{$m->uri} < 0) and
935     $m->is_type_uri (ExpandedURI q<ManakaiDOM:ConstGroup>)) {
936     unshift @ruri, $m->uri;
937 wakaba 1.4 $ReferredResource{$res->uri} = -1;
938 wakaba 1.3 next U;
939     }
940     } elsif ($res->is_type_uri
941     (ExpandedURI q<ManakaiDOM:ExceptionOrWarningSubType>)) {
942     my $m = $res->get_property_resource
943     (ExpandedURI q<dis2pm:parentResource>);
944     if (not ($ReferredResource{$m->uri} < 0) and
945     $m->is_type_uri (ExpandedURI q<ManakaiDOM:Const>)) {
946     unshift @ruri, $m->uri;
947 wakaba 1.4 $ReferredResource{$res->uri} = -1;
948 wakaba 1.3 next U;
949     }
950 wakaba 1.2 } else { ## Unsupported type
951     $ReferredResource{$uri} = -1;
952     }
953     } # U
954     }
955    
956 wakaba 1.1 my $lsimpl = $impl->get_feature (ExpandedURI q<DOMLS:LS> => '3.0');
957    
958     status_msg_ qq<Writing file ""...>;
959    
960     use Encode;
961     my $serializer = $lsimpl->create_mls_serializer
962     ({ExpandedURI q<DOMLS:SerializeDocumentInstance> => ''});
963     print Encode::encode ('utf8', $serializer->write_to_string ($doc));
964    
965     status_msg qq<done>;
966    
967     verbose_msg_ qq<Checking undefined resources...>;
968    
969     $db->check_undefined_resource;
970    
971     verbose_msg qq<done>;
972    
973     verbose_msg_ qq<Closing database...>;
974     undef $db;
975     verbose_msg qq<done>;
976    
977     =head1 SEE ALSO
978    
979     L<lib/manakai/dis.pl> and L<bin/cdis2pm.pl> - Old version of
980     this script.
981    
982     L<lib/Message/Util/DIS.dis> - The <QUOTE::dis> object implementation.
983    
984     L<lib/Message/Util/PerlCode.dis> - The Perl code generator.
985    
986     L<lib/manakai/DISCore.dis> - The definition for the "dis" format.
987    
988     L<lib/manakai/DISPerl.dis> - The definition for the "dis" Perl-specific
989     vocabulary.
990    
991     =head1 LICENSE
992    
993     Copyright 2004-2005 Wakaba <w@suika.fam.cx>. All rights reserved.
994    
995     This program is free software; you can redistribute it and/or
996     modify it under the same terms as Perl itself.
997    
998     =cut
999    
1000 wakaba 1.4 1; # $Date: 2005/09/01 17:07:20 $

admin@suikawiki.org
ViewVC Help
Powered by ViewVC 1.1.24