2 eval 'exec perl -S $0 ${1+"$@"}'
5 # This file is part of the LibreOffice project.
7 # This Source Code Form is subject to the terms of the Mozilla Public
8 # License, v. 2.0. If a copy of the MPL was not distributed with this
9 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
11 # This file incorporates work covered by the following license notice:
13 # Licensed to the Apache Software Foundation (ASF) under one or more
14 # contributor license agreements. See the NOTICE file distributed
15 # with this work for additional information regarding copyright
16 # ownership. The ASF licenses this file to you under the Apache
17 # License, Version 2.0 (the "License"); you may not use this file
18 # except in compliance with the License. You may obtain a copy of
19 # the License at http://www.apache.org/licenses/LICENSE-2.0 .
23 print "Usage: api-to-idl.pl source.api destination_path\n";
25 print "This tool converts oovbaapi *.api files into *.idl's.\n";
32 if ( !defined( $src ) || !defined( $dest ) || $src eq "-h" || $src eq "--help" ) {
44 # Process element start event
45 sub start_element
($) {
48 @element_attr = split( /\s+/, $el );
49 my $element = $element_attr[0];
51 if ( $element eq "element" ) {
52 if ( $element_attr[1] =~ /type="?([^"]*)"?/ && $1 eq "constant" ) {
59 elsif ( $state eq "constant" && $element eq "source" ) {
61 if ( $element_attr[1] =~ /id="?([^"]*)"?/ ) {
62 chomp( $source = $1 );
65 elsif ( $state eq "source" && $element eq "name" ) {
68 elsif ( $state eq "source" && $element eq "value" ) {
73 # Process element end event
77 if ( $state eq "name" && $element eq "name" ) {
80 elsif ( $state eq "value" && $element eq "value" ) {
83 elsif ( $state ne "" && $element eq "element" ) {
86 my @destination = split( /\./, $source );
87 my $module = shift( @destination );
88 my $type = shift( @destination );
90 $module =~ tr/[A-Z]/[a-z]/;
92 $result{$module} = {} unless exists $result{$module};
93 $result{$module}{$type} = [] unless exists $result{$module}{$type};
95 push( @
{$result{$module}{$type}},
96 { "name" => $name, "value" => $value } );
104 if ( $state eq "name" ) {
105 chomp( $name = $data );
107 elsif ( $state eq "value" ) {
108 chomp( $value = $data );
112 # Create idls from the parsed data
113 sub generate_idls
($) {
116 foreach $module ( keys %result ) {
117 foreach $type ( keys %{$result{$module}} ) {
118 my $fname = $path . "/" . $type . ".idl";
119 open( IDL
, ">$fname" ) || die "Cannot write $fname.";
121 if( $module eq "vba" ) {
122 print IDL
"module ooo { module $module {\n";
125 print IDL
"module ooo { module vba { module $module {\n";
128 print IDL
" constants $type {\n";
129 foreach $constant ( @
{$result{$module}{$type}} ) {
130 print IDL
" const long $constant->{'name'} = $constant->{'value'};\n";
132 if( $module eq "vba" ) {
133 print IDL
" };\n}; };\n";
136 print IDL
" };\n}; }; };\n";
145 open( IN
, "<$src" ) || die "Cannot open $src.";
156 elsif ( /-->/ && $in_comment ) {
160 elsif ( $in_comment ) {
167 next if ( $_ eq "" );
169 # take care of lines where element continues
179 my @starts = split( /</, $line );
181 foreach $start ( @starts ) {
182 next if ( $start eq "" );
184 @ends = split( />/, $start );
185 my $element = $ends[0];
188 # start or end element
189 if ( $element =~ /^\/(.*)/ ) {
193 start_element
( $element );
202 # Generate the output
203 generate_idls
($dest);