123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202 |
- package spec
- import (
- "net/url"
- "path"
- "strings"
- )
- const fileScheme = "file"
- func normalizeURI(refPath, base string) string {
- refURL, err := parseURL(refPath)
- if err != nil {
- specLogger.Printf("warning: invalid URI in $ref %q: %v", refPath, err)
- refURL, refPath = repairURI(refPath)
- }
- fixWindowsURI(refURL, refPath)
- refURL.Path = path.Clean(refURL.Path)
- if refURL.Path == "." {
- refURL.Path = ""
- }
- r := MustCreateRef(refURL.String())
- if r.IsCanonical() {
- return refURL.String()
- }
- baseURL, _ := parseURL(base)
- if path.IsAbs(refURL.Path) {
- baseURL.Path = refURL.Path
- } else if refURL.Path != "" {
- baseURL.Path = path.Join(path.Dir(baseURL.Path), refURL.Path)
- }
-
- baseURL.Fragment = refURL.Fragment
- return baseURL.String()
- }
- func denormalizeRef(ref *Ref, originalRelativeBase, id string) Ref {
- debugLog("denormalizeRef called:\n$ref: %q\noriginal: %s\nroot ID:%s", ref.String(), originalRelativeBase, id)
- if ref.String() == "" || ref.IsRoot() || ref.HasFragmentOnly {
-
- return *ref
- }
- if id != "" {
- idBaseURL, err := parseURL(id)
- if err == nil {
- if ref, ok := rebase(ref, idBaseURL, true); ok {
-
- return ref
- }
- }
- }
- originalRelativeBaseURL, _ := parseURL(originalRelativeBase)
- r, _ := rebase(ref, originalRelativeBaseURL, false)
- return r
- }
- func rebase(ref *Ref, v *url.URL, notEqual bool) (Ref, bool) {
- var newBase url.URL
- u := ref.GetURL()
- if u.Scheme != v.Scheme || u.Host != v.Host {
- return *ref, false
- }
- docPath := v.Path
- v.Path = path.Dir(v.Path)
- if v.Path == "." {
- v.Path = ""
- } else if !strings.HasSuffix(v.Path, "/") {
- v.Path += "/"
- }
- newBase.Fragment = u.Fragment
- if strings.HasPrefix(u.Path, docPath) {
- newBase.Path = strings.TrimPrefix(u.Path, docPath)
- } else {
- newBase.Path = strings.TrimPrefix(u.Path, v.Path)
- }
- if notEqual && newBase.Path == "" && newBase.Fragment == "" {
-
- return *ref, false
- }
- if path.IsAbs(newBase.Path) {
-
- newBase.Scheme = v.Scheme
- newBase.Host = v.Host
- }
- return MustCreateRef(newBase.String()), true
- }
- func normalizeRef(ref *Ref, relativeBase string) *Ref {
- r := MustCreateRef(normalizeURI(ref.String(), relativeBase))
- return &r
- }
- func normalizeBase(in string) string {
- u, err := parseURL(in)
- if err != nil {
- specLogger.Printf("warning: invalid URI in RelativeBase %q: %v", in, err)
- u, in = repairURI(in)
- }
- u.Fragment = ""
- fixWindowsURI(u, in)
- u.Path = path.Clean(u.Path)
- if u.Path == "." {
- u.Path = ""
- }
- if u.Scheme != "" {
- if path.IsAbs(u.Path) || u.Scheme != fileScheme {
-
- return u.String()
- }
- }
-
-
-
-
-
- u.Scheme = fileScheme
- u.Path = absPath(u.Path)
- u.RawQuery = ""
- return u.String()
- }
|