Update ooo320-m1
[ooovba.git] / oovbaapi / genconstidl / api-to-idl.pl
blobac7498ca7baa0b7232be38d33feec52fb5728d95
2 eval 'exec perl -S $0 ${1+"$@"}'
3 if 0;
4 #*************************************************************************
6 # DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7 #
8 # Copyright 2008 by Sun Microsystems, Inc.
10 # OpenOffice.org - a multi-platform office productivity suite
12 # $RCSfile: api-to-idl.pl,v $
14 # $Revision: 1.3 $
16 # This file is part of OpenOffice.org.
18 # OpenOffice.org is free software: you can redistribute it and/or modify
19 # it under the terms of the GNU Lesser General Public License version 3
20 # only, as published by the Free Software Foundation.
22 # OpenOffice.org is distributed in the hope that it will be useful,
23 # but WITHOUT ANY WARRANTY; without even the implied warranty of
24 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25 # GNU Lesser General Public License version 3 for more details
26 # (a copy is included in the LICENSE file that accompanied this code).
28 # You should have received a copy of the GNU Lesser General Public License
29 # version 3 along with OpenOffice.org. If not, see
30 # <http://www.openoffice.org/license.html>
31 # for a copy of the LGPLv3 License.
33 #*************************************************************************
35 sub usage() {
36 print "Usage: api-to-idl.pl source.api destination_path\n";
37 print;
38 print "This tool converts oovbaapi *.api files into *.idl's.\n";
39 exit 1;
42 my $src = shift;
43 my $dest = shift;
45 if ( !defined( $src ) || !defined( $dest ) || $src eq "-h" || $src eq "--help" ) {
46 usage();
49 # Parsing functions
50 my $state = "";
51 my $source = "";
52 my $name = "";
53 my $value = "";
55 my %result;
57 # Process element start event
58 sub start_element($) {
59 my ($el) = @_;
61 @element_attr = split( /\s+/, $el );
62 my $element = $element_attr[0];
64 if ( $element eq "element" ) {
65 if ( $element_attr[1] =~ /type="?([^"]*)"?/ && $1 eq "constant" ) {
66 $state = "constant";
67 $source = "";
68 $name = "";
69 $value = "";
72 elsif ( $state eq "constant" && $element eq "source" ) {
73 $state = "source";
74 if ( $element_attr[1] =~ /id="?([^"]*)"?/ ) {
75 chomp( $source = $1 );
78 elsif ( $state eq "source" && $element eq "name" ) {
79 $state = "name";
81 elsif ( $state eq "source" && $element eq "value" ) {
82 $state = "value";
86 # Process element end event
87 sub end_element($) {
88 my ($element) = @_;
90 if ( $state eq "name" && $element eq "name" ) {
91 $state = "source";
93 elsif ( $state eq "value" && $element eq "value" ) {
94 $state = "source";
96 elsif ( $state ne "" && $element eq "element" ) {
97 $state = "";
99 my @destination = split( /\./, $source );
100 my $module = shift( @destination );
101 my $type = shift( @destination );
103 $module =~ tr/[A-Z]/[a-z]/;
105 $result{$module} = {} unless exists $result{$module};
106 $result{$module}{$type} = [] unless exists $result{$module}{$type};
108 push( @{$result{$module}{$type}},
109 { "name" => $name, "value" => $value } );
113 # Process characters
114 sub characters($) {
115 my ($data) = @_;
117 if ( $state eq "name" ) {
118 chomp( $name = $data );
120 elsif ( $state eq "value" ) {
121 chomp( $value = $data );
125 # Create idls from the parsed data
126 sub generate_idls($) {
127 my ($path) = @_;
129 foreach $module ( keys %result ) {
130 foreach $type ( keys %{$result{$module}} ) {
131 my $fname = $path . "/" . $type . ".idl";
132 open( IDL, ">$fname" ) || die "Cannot write $fname.";
134 if( $module eq "vba" ) {
135 print IDL "module ooo { module $module {\n";
137 else {
138 print IDL "module ooo { module vba { module $module {\n";
141 print IDL " constants $type {\n";
142 foreach $constant ( @{$result{$module}{$type}} ) {
143 print IDL " const long $constant->{'name'} = $constant->{'value'};\n";
145 if( $module eq "vba" ) {
146 print IDL " };\n}; };\n";
148 else {
149 print IDL " };\n}; }; };\n";
152 close( IDL );
157 # Parse the input
158 open( IN, "<$src" ) || die "Cannot open $src.";
160 my $in_comment = 0;
161 my $line = "";
162 while (<IN>) {
163 # ignore comments
164 s/<!--[^>]*-->//g;
165 if ( /<!--/ ) {
166 $in_comment = 1;
167 s/<!--.*//;
169 elsif ( /-->/ && $in_comment ) {
170 $in_comment = 0;
171 s/.*-->//;
173 elsif ( $in_comment ) {
174 next;
176 # ignore empty lines
177 chomp;
178 s/^\s*//;
179 s/\s*$//;
180 next if ( $_ eq "" );
182 # take care of lines where element continues
183 if ( $line ne "" ) {
184 $line .= " " . $_;
186 else {
187 $line = $_;
189 next if ( !/>$/ );
191 # the actual parsing
192 my @starts = split( /</, $line );
193 $line = "";
194 foreach $start ( @starts ) {
195 next if ( $start eq "" );
197 @ends = split( />/, $start );
198 my $element = $ends[0];
199 my $data = $ends[1];
201 # start or end element
202 if ( $element =~ /^\/(.*)/ ) {
203 end_element( $1 );
205 else {
206 start_element( $element );
209 # the data
210 characters( $data );
213 close( IN );
215 # Generate the output
216 generate_idls($dest);