| 1 |
# WSDL4R - XSD importer library. |
|---|
| 2 |
# Copyright (C) 2000-2007 NAKAMURA, Hiroshi <nahi@ruby-lang.org>. |
|---|
| 3 |
|
|---|
| 4 |
# This program is copyrighted free software by NAKAMURA, Hiroshi. You can |
|---|
| 5 |
# redistribute it and/or modify it under the same terms of Ruby's license; |
|---|
| 6 |
# either the dual license version in 2003, or any later version. |
|---|
| 7 |
|
|---|
| 8 |
|
|---|
| 9 |
require 'soap/soap' |
|---|
| 10 |
require 'soap/httpconfigloader' |
|---|
| 11 |
require 'wsdl/xmlSchema/parser' |
|---|
| 12 |
|
|---|
| 13 |
|
|---|
| 14 |
module WSDL |
|---|
| 15 |
module XMLSchema |
|---|
| 16 |
|
|---|
| 17 |
|
|---|
| 18 |
class Importer |
|---|
| 19 |
DO_NOT_IMPORT = [::SOAP::EncodingNamespace] |
|---|
| 20 |
|
|---|
| 21 |
def self.import(location, originalroot = nil) |
|---|
| 22 |
new.import(location, originalroot) |
|---|
| 23 |
end |
|---|
| 24 |
|
|---|
| 25 |
def initialize |
|---|
| 26 |
@web_client = nil |
|---|
| 27 |
end |
|---|
| 28 |
|
|---|
| 29 |
def import(location, originalroot = nil) |
|---|
| 30 |
if DO_NOT_IMPORT.include?(location.to_s) |
|---|
| 31 |
return nil |
|---|
| 32 |
end |
|---|
| 33 |
unless location.is_a?(URI) |
|---|
| 34 |
location = URI.parse(location) |
|---|
| 35 |
end |
|---|
| 36 |
source, normalizedlocation = fetch(location) |
|---|
| 37 |
content = parse(source, normalizedlocation, originalroot) |
|---|
| 38 |
content.location = normalizedlocation |
|---|
| 39 |
content |
|---|
| 40 |
end |
|---|
| 41 |
|
|---|
| 42 |
private |
|---|
| 43 |
|
|---|
| 44 |
def parse(content, location, originalroot) |
|---|
| 45 |
opt = { |
|---|
| 46 |
:location => location, |
|---|
| 47 |
:originalroot => originalroot |
|---|
| 48 |
} |
|---|
| 49 |
WSDL::XMLSchema::Parser.new(opt).parse(content) |
|---|
| 50 |
end |
|---|
| 51 |
|
|---|
| 52 |
def fetch(location) |
|---|
| 53 |
warn("importing: #{location}") if $DEBUG |
|---|
| 54 |
content = nil |
|---|
| 55 |
normalizedlocation = location |
|---|
| 56 |
if location.scheme == 'file' or |
|---|
| 57 |
(location.relative? and FileTest.exist?(location.path)) |
|---|
| 58 |
content = File.open(location.path).read |
|---|
| 59 |
normalizedlocation = URI.parse('file://' + File.expand_path(location.path)) |
|---|
| 60 |
elsif location.scheme and location.scheme.size == 1 and |
|---|
| 61 |
FileTest.exist?(location.to_s) |
|---|
| 62 |
# ToDo: remove this ugly workaround for a path with drive letter |
|---|
| 63 |
# (D://foo/bar) |
|---|
| 64 |
content = File.open(location.to_s).read |
|---|
| 65 |
else |
|---|
| 66 |
client = web_client.new(nil, "WSDL4R") |
|---|
| 67 |
client.proxy = ::SOAP::Env::HTTP_PROXY |
|---|
| 68 |
client.no_proxy = ::SOAP::Env::NO_PROXY |
|---|
| 69 |
if opt = ::SOAP::Property.loadproperty(::SOAP::PropertyName) |
|---|
| 70 |
http_opt = opt["client.protocol.http"] |
|---|
| 71 |
::SOAP::HTTPConfigLoader.set_options(client, http_opt) if http_opt |
|---|
| 72 |
end |
|---|
| 73 |
content = client.get_content(location) |
|---|
| 74 |
end |
|---|
| 75 |
return content, normalizedlocation |
|---|
| 76 |
end |
|---|
| 77 |
|
|---|
| 78 |
def web_client |
|---|
| 79 |
return @web_client if @web_client |
|---|
| 80 |
begin |
|---|
| 81 |
require 'httpclient' |
|---|
| 82 |
@web_client = HTTPClient |
|---|
| 83 |
rescue LoadError |
|---|
| 84 |
begin |
|---|
| 85 |
require 'http-access2' |
|---|
| 86 |
if HTTPAccess2::VERSION < "2.0" |
|---|
| 87 |
raise LoadError.new("http-access/2.0 or later is required.") |
|---|
| 88 |
end |
|---|
| 89 |
@web_client = HTTPAccess2::Client |
|---|
| 90 |
rescue LoadError |
|---|
| 91 |
warn("Loading http-access2 failed. Net/http is used.") if $DEBUG |
|---|
| 92 |
require 'soap/netHttpClient' |
|---|
| 93 |
@web_client = ::SOAP::NetHttpClient |
|---|
| 94 |
end |
|---|
| 95 |
end |
|---|
| 96 |
@web_client |
|---|
| 97 |
end |
|---|
| 98 |
end |
|---|
| 99 |
|
|---|
| 100 |
|
|---|
| 101 |
end |
|---|
| 102 |
end |
|---|