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