xref: /AOO41X/main/oovbaapi/genconstidl/api-to-idl.pl (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
1*cdf0e10cSrcweir:
2*cdf0e10cSrcweir    eval 'exec perl -S $0 ${1+"$@"}'
3*cdf0e10cSrcweir        if 0;
4*cdf0e10cSrcweir#*************************************************************************
5*cdf0e10cSrcweir#
6*cdf0e10cSrcweir# DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
7*cdf0e10cSrcweir#
8*cdf0e10cSrcweir# Copyright 2000, 2010 Oracle and/or its affiliates.
9*cdf0e10cSrcweir#
10*cdf0e10cSrcweir# OpenOffice.org - a multi-platform office productivity suite
11*cdf0e10cSrcweir#
12*cdf0e10cSrcweir# This file is part of OpenOffice.org.
13*cdf0e10cSrcweir#
14*cdf0e10cSrcweir# OpenOffice.org is free software: you can redistribute it and/or modify
15*cdf0e10cSrcweir# it under the terms of the GNU Lesser General Public License version 3
16*cdf0e10cSrcweir# only, as published by the Free Software Foundation.
17*cdf0e10cSrcweir#
18*cdf0e10cSrcweir# OpenOffice.org is distributed in the hope that it will be useful,
19*cdf0e10cSrcweir# but WITHOUT ANY WARRANTY; without even the implied warranty of
20*cdf0e10cSrcweir# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
21*cdf0e10cSrcweir# GNU Lesser General Public License version 3 for more details
22*cdf0e10cSrcweir# (a copy is included in the LICENSE file that accompanied this code).
23*cdf0e10cSrcweir#
24*cdf0e10cSrcweir# You should have received a copy of the GNU Lesser General Public License
25*cdf0e10cSrcweir# version 3 along with OpenOffice.org.  If not, see
26*cdf0e10cSrcweir# <http://www.openoffice.org/license.html>
27*cdf0e10cSrcweir# for a copy of the LGPLv3 License.
28*cdf0e10cSrcweir#
29*cdf0e10cSrcweir#*************************************************************************
30*cdf0e10cSrcweir
31*cdf0e10cSrcweirsub usage() {
32*cdf0e10cSrcweir    print "Usage: api-to-idl.pl source.api destination_path\n";
33*cdf0e10cSrcweir    print;
34*cdf0e10cSrcweir    print "This tool converts oovbaapi *.api files into *.idl's.\n";
35*cdf0e10cSrcweir    exit 1;
36*cdf0e10cSrcweir}
37*cdf0e10cSrcweir
38*cdf0e10cSrcweirmy $src = shift;
39*cdf0e10cSrcweirmy $dest = shift;
40*cdf0e10cSrcweir
41*cdf0e10cSrcweirif ( !defined( $src ) || !defined( $dest ) || $src eq "-h" || $src eq "--help" ) {
42*cdf0e10cSrcweir    usage();
43*cdf0e10cSrcweir}
44*cdf0e10cSrcweir
45*cdf0e10cSrcweir# Parsing functions
46*cdf0e10cSrcweirmy $state = "";
47*cdf0e10cSrcweirmy $source = "";
48*cdf0e10cSrcweirmy $name = "";
49*cdf0e10cSrcweirmy $value = "";
50*cdf0e10cSrcweir
51*cdf0e10cSrcweirmy %result;
52*cdf0e10cSrcweir
53*cdf0e10cSrcweir# Process element start event
54*cdf0e10cSrcweirsub start_element($) {
55*cdf0e10cSrcweir    my ($el) = @_;
56*cdf0e10cSrcweir
57*cdf0e10cSrcweir    @element_attr = split( /\s+/, $el );
58*cdf0e10cSrcweir    my $element = $element_attr[0];
59*cdf0e10cSrcweir
60*cdf0e10cSrcweir    if ( $element eq "element" ) {
61*cdf0e10cSrcweir        if ( $element_attr[1] =~ /type="?([^"]*)"?/ && $1 eq "constant" ) {
62*cdf0e10cSrcweir            $state = "constant";
63*cdf0e10cSrcweir            $source = "";
64*cdf0e10cSrcweir            $name = "";
65*cdf0e10cSrcweir            $value = "";
66*cdf0e10cSrcweir        }
67*cdf0e10cSrcweir    }
68*cdf0e10cSrcweir    elsif ( $state eq "constant" && $element eq "source" ) {
69*cdf0e10cSrcweir        $state = "source";
70*cdf0e10cSrcweir        if ( $element_attr[1] =~ /id="?([^"]*)"?/ ) {
71*cdf0e10cSrcweir            chomp( $source = $1 );
72*cdf0e10cSrcweir        }
73*cdf0e10cSrcweir    }
74*cdf0e10cSrcweir    elsif ( $state eq "source" && $element eq "name" ) {
75*cdf0e10cSrcweir        $state = "name";
76*cdf0e10cSrcweir    }
77*cdf0e10cSrcweir    elsif ( $state eq "source" && $element eq "value" ) {
78*cdf0e10cSrcweir        $state = "value";
79*cdf0e10cSrcweir    }
80*cdf0e10cSrcweir}
81*cdf0e10cSrcweir
82*cdf0e10cSrcweir# Process element end event
83*cdf0e10cSrcweirsub end_element($) {
84*cdf0e10cSrcweir    my ($element) = @_;
85*cdf0e10cSrcweir
86*cdf0e10cSrcweir    if ( $state eq "name" && $element eq "name" ) {
87*cdf0e10cSrcweir        $state = "source";
88*cdf0e10cSrcweir    }
89*cdf0e10cSrcweir    elsif ( $state eq "value" && $element eq "value" ) {
90*cdf0e10cSrcweir        $state = "source";
91*cdf0e10cSrcweir    }
92*cdf0e10cSrcweir    elsif ( $state ne "" && $element eq "element" ) {
93*cdf0e10cSrcweir        $state = "";
94*cdf0e10cSrcweir
95*cdf0e10cSrcweir        my @destination = split( /\./, $source );
96*cdf0e10cSrcweir        my $module = shift( @destination );
97*cdf0e10cSrcweir        my $type = shift( @destination );
98*cdf0e10cSrcweir
99*cdf0e10cSrcweir        $module =~ tr/[A-Z]/[a-z]/;
100*cdf0e10cSrcweir
101*cdf0e10cSrcweir        $result{$module} = {} unless exists $result{$module};
102*cdf0e10cSrcweir        $result{$module}{$type} = [] unless exists $result{$module}{$type};
103*cdf0e10cSrcweir
104*cdf0e10cSrcweir        push( @{$result{$module}{$type}},
105*cdf0e10cSrcweir              { "name" => $name, "value" => $value } );
106*cdf0e10cSrcweir    }
107*cdf0e10cSrcweir}
108*cdf0e10cSrcweir
109*cdf0e10cSrcweir# Process characters
110*cdf0e10cSrcweirsub characters($) {
111*cdf0e10cSrcweir    my ($data) = @_;
112*cdf0e10cSrcweir
113*cdf0e10cSrcweir    if ( $state eq "name" ) {
114*cdf0e10cSrcweir        chomp( $name = $data );
115*cdf0e10cSrcweir    }
116*cdf0e10cSrcweir    elsif ( $state eq "value" ) {
117*cdf0e10cSrcweir        chomp( $value = $data );
118*cdf0e10cSrcweir    }
119*cdf0e10cSrcweir}
120*cdf0e10cSrcweir
121*cdf0e10cSrcweir# Create idls from the parsed data
122*cdf0e10cSrcweirsub generate_idls($) {
123*cdf0e10cSrcweir    my ($path) = @_;
124*cdf0e10cSrcweir
125*cdf0e10cSrcweir    foreach $module ( keys %result ) {
126*cdf0e10cSrcweir        foreach $type ( keys %{$result{$module}} ) {
127*cdf0e10cSrcweir            my $fname = $path . "/" . $type . ".idl";
128*cdf0e10cSrcweir            open( IDL, ">$fname" ) || die "Cannot write $fname.";
129*cdf0e10cSrcweir
130*cdf0e10cSrcweir            if( $module eq "vba" ) {
131*cdf0e10cSrcweir		print IDL "module ooo { module $module {\n";
132*cdf0e10cSrcweir	    }
133*cdf0e10cSrcweir	    else {
134*cdf0e10cSrcweir            	print IDL "module ooo { module vba { module $module {\n";
135*cdf0e10cSrcweir            }
136*cdf0e10cSrcweir
137*cdf0e10cSrcweir            print IDL "    constants $type {\n";
138*cdf0e10cSrcweir            foreach $constant ( @{$result{$module}{$type}} ) {
139*cdf0e10cSrcweir                print IDL "        const long $constant->{'name'} = $constant->{'value'};\n";
140*cdf0e10cSrcweir            }
141*cdf0e10cSrcweir            if( $module eq "vba" ) {
142*cdf0e10cSrcweir		print IDL "    };\n}; };\n";
143*cdf0e10cSrcweir	    }
144*cdf0e10cSrcweir	    else {
145*cdf0e10cSrcweir            	print IDL "    };\n}; }; };\n";
146*cdf0e10cSrcweir            }
147*cdf0e10cSrcweir
148*cdf0e10cSrcweir            close( IDL );
149*cdf0e10cSrcweir        }
150*cdf0e10cSrcweir    }
151*cdf0e10cSrcweir}
152*cdf0e10cSrcweir
153*cdf0e10cSrcweir# Parse the input
154*cdf0e10cSrcweiropen( IN, "<$src" ) || die "Cannot open $src.";
155*cdf0e10cSrcweir
156*cdf0e10cSrcweirmy $in_comment = 0;
157*cdf0e10cSrcweirmy $line = "";
158*cdf0e10cSrcweirwhile (<IN>) {
159*cdf0e10cSrcweir    # ignore comments
160*cdf0e10cSrcweir    s/<!--[^>]*-->//g;
161*cdf0e10cSrcweir    if ( /<!--/ ) {
162*cdf0e10cSrcweir        $in_comment = 1;
163*cdf0e10cSrcweir        s/<!--.*//;
164*cdf0e10cSrcweir    }
165*cdf0e10cSrcweir    elsif ( /-->/ && $in_comment ) {
166*cdf0e10cSrcweir        $in_comment = 0;
167*cdf0e10cSrcweir        s/.*-->//;
168*cdf0e10cSrcweir    }
169*cdf0e10cSrcweir    elsif ( $in_comment ) {
170*cdf0e10cSrcweir        next;
171*cdf0e10cSrcweir    }
172*cdf0e10cSrcweir    # ignore empty lines
173*cdf0e10cSrcweir    chomp;
174*cdf0e10cSrcweir    s/^\s*//;
175*cdf0e10cSrcweir    s/\s*$//;
176*cdf0e10cSrcweir    next if ( $_ eq "" );
177*cdf0e10cSrcweir
178*cdf0e10cSrcweir    # take care of lines where element continues
179*cdf0e10cSrcweir    if ( $line ne "" ) {
180*cdf0e10cSrcweir	$line .= " " . $_;
181*cdf0e10cSrcweir    }
182*cdf0e10cSrcweir    else {
183*cdf0e10cSrcweir	$line = $_;
184*cdf0e10cSrcweir    }
185*cdf0e10cSrcweir    next if ( !/>$/ );
186*cdf0e10cSrcweir
187*cdf0e10cSrcweir    # the actual parsing
188*cdf0e10cSrcweir    my @starts = split( /</, $line );
189*cdf0e10cSrcweir    $line = "";
190*cdf0e10cSrcweir    foreach $start ( @starts ) {
191*cdf0e10cSrcweir        next if ( $start eq "" );
192*cdf0e10cSrcweir
193*cdf0e10cSrcweir        @ends = split( />/, $start );
194*cdf0e10cSrcweir        my $element = $ends[0];
195*cdf0e10cSrcweir        my $data = $ends[1];
196*cdf0e10cSrcweir
197*cdf0e10cSrcweir        # start or end element
198*cdf0e10cSrcweir        if ( $element =~ /^\/(.*)/ ) {
199*cdf0e10cSrcweir            end_element( $1 );
200*cdf0e10cSrcweir        }
201*cdf0e10cSrcweir        else {
202*cdf0e10cSrcweir            start_element( $element );
203*cdf0e10cSrcweir        }
204*cdf0e10cSrcweir
205*cdf0e10cSrcweir        # the data
206*cdf0e10cSrcweir        characters( $data );
207*cdf0e10cSrcweir    }
208*cdf0e10cSrcweir}
209*cdf0e10cSrcweirclose( IN );
210*cdf0e10cSrcweir
211*cdf0e10cSrcweir# Generate the output
212*cdf0e10cSrcweirgenerate_idls($dest);
213