1# BabelFish.tcl - Copyright (C) 2003 Pat Thoyts <patthoyts@users.sf.net>
2#
3# This file downloads the BabelFish service WSDL description,
4# parses it to generate a Tcl script for the service
5# applies a bug fix and then performs a translation of the arguments.
6#
7# Usage: BabelFish languages text
8#   e.g: BabelFish en_fr "Good morning"
9#
10# You should be able to do this:
11#  set babelfish [SOAP::service \
12#      -wsdl http://www.xmethods.net/sd/2001/BabelFishService.wsdl]
13#  BabelFishService::BabelFish en_fr {Good morning}
14# but not yet.
15#
16#  Translation                 translationmode
17#  -----------                 ----------------
18#  English -> French           "en_fr"
19#  English -> German           "en_de"
20#  English -> Italian          "en_it"
21#  English -> Portugese        "en_pt"
22#  English -> Spanish          "en_es"
23#  French -> English           "fr_en"
24#  German -> English           "de_en"
25#  Italian -> English          "it_en"
26#  Portugese -> English        "pt_en"
27#  Russian -> English          "ru_en"
28#  Spanish -> English          "es_en"
29#
30# -------------------------------------------------------------------------
31# This software is distributed in the hope that it will be useful, but
32# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
33# or FITNESS FOR A PARTICULAR PURPOSE.  See the accompanying file `LICENSE'
34# for more details.
35# -------------------------------------------------------------------------
36#
37# @(#)$Id: BabelFish.tcl,v 1.2 2003/09/06 17:08:46 patthoyts Exp $
38
39package require SOAP
40package require http
41
42if {[catch {package require SOAP::WSDL}]} {
43
44    # User doesn't have the WSDL package,  do it manually
45    # The following code was generated by parsing the WSDL document
46    namespace eval ::BabelFishService {
47        set endpoint http://services.xmethods.net:80/perl/soaplite.cgi
48        SOAP::create BabelFish -proxy $endpoint \
49            -params {translationmode string sourcedata string} \
50            -action urn:xmethodsBabelFish#BabelFish \
51            -encoding http://schemas.xmlsoap.org/soap/encoding/ \
52            -uri urn:xmethodsBabelFish
53    }; # end of BabelFishService
54
55} else {
56
57    # Get the WSDL document (and cache the result for later)
58    set wsdl_name BabelFishService.wsdl
59    set url http://www.xmethods.net/sd/2001/BabelFishService.wsdl
60    if {[file exists [set fname [file join $::env(TEMP) $wsdl_name]]]} {
61        set f [open $fname r]
62        set wsdl [read $f]
63        close $f
64    } else {
65        set tok [http::geturl $url]
66        if {[http::status $tok] eq "ok"} {
67            set wsdl [http::data $tok]
68            set f [open $fname w]
69            puts $f $wsdl
70            close $f
71        }
72        http::cleanup $tok
73    }
74
75    # Process the WSDL and generate Tcl script defining the SOAP accessors.
76    set doc  [dom::DOMImplementation parse $wsdl]
77    set impl [SOAP::WSDL::parse $doc]
78    eval [set $impl]
79
80    # Fixup the parameters (the rpcvar package needs to be enhanced for this)
81    set schema {http://www.w3.org/2001/XMLSchema}
82    foreach cmd [info commands ::BabelFishService::*] {
83        set fixed {}
84        foreach {param type} [SOAP::cget $cmd -params] {
85            set type [regsub "${schema}:" $type {}]
86            lappend fixed $param $type
87        }
88        SOAP::configure $cmd -params $fixed -schemas [list xsd $schema]
89    }
90}
91
92
93# Make available as a command line script.
94if {!$::tcl_interactive} {
95    if {[info command BabelFishService::BabelFish] != {}} {
96        if {[llength $argv] < 2} {
97            puts "usage: [file tail $argv0] lanuage text ?...?"
98            puts " eg: [file tail $argv0] en_fr Good morning"
99            exit 1
100        }
101
102        set r [BabelFishService::BabelFish \
103                   [lindex $argv 0] [lindex $argv 1]]
104        puts $r
105    }
106}
107
108# -------------------------------------------------------------------------
109# Local variables:
110#   mode: tcl
111#   indent-tabs-mode: nil
112# End:
113