forked from hooklift/gowsdl
-
Notifications
You must be signed in to change notification settings - Fork 2
/
location.go
81 lines (68 loc) · 1.8 KB
/
location.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at http://mozilla.org/MPL/2.0/.
package gowsdl
import (
"net/url"
"path/filepath"
)
// A Location encapsulate information about the loc of WSDL/XSD.
//
// It could be either URL or an absolute file path.
type Location struct {
u *url.URL
f string
}
// ParseLocation parses a rawloc into a Location structure.
//
// If rawloc is URL then it should be absolute.
// Relative file path will be converted into absolute path.
func ParseLocation(rawloc string) (*Location, error) {
u, _ := url.Parse(rawloc)
if u.Scheme != "" {
return &Location{u: u}, nil
}
absURI, err := filepath.Abs(rawloc)
if err != nil {
return nil, err
}
return &Location{f: absURI}, nil
}
// Parse parses path in the context of the receiver. The provided path may be relative or absolute.
// Parse returns nil, err on parse failure.
func (r *Location) Parse(ref string) (*Location, error) {
if r.u != nil {
u, err := r.u.Parse(ref)
if err != nil {
return nil, err
}
return &Location{u: u}, nil
}
if filepath.IsAbs(ref) {
return &Location{f: ref}, nil
}
if u, err := url.Parse(ref); err == nil {
if u.Scheme != "" {
return &Location{u: u}, nil
}
}
return &Location{f: filepath.Join(filepath.Dir(r.f), ref)}, nil
}
// IsFile determines whether the Location contains a file path.
func (r *Location) isFile() bool {
return r.f != ""
}
// IsFile determines whether the Location contains URL.
func (r *Location) isURL() bool {
return r.u != nil
}
// String reassembles the Location either into a valid URL string or a file path.
func (r *Location) String() string {
if r.isFile() {
return r.f
}
if r.isURL() {
return r.u.String()
}
return ""
}