Ejemplo n.º 1
0
func (cfg *Config) resolveDependencies(data ...[]byte) ([][]byte, error) {
	var imports []xsd.Ref
	have := make(map[string]bool)

	for _, b := range data {
		refs, err := xsd.Imports(b)
		if err != nil {
			return nil, err
		}
		imports = append(imports, refs...)
		for _, tns := range lookupTargetNS(b) {
			have[tns] = true
		}
	}
	for _, r := range imports {
		if have[r.Namespace] {
			continue
		}
		d, err := cfg.resolveDependencies1(r, have, 1)
		if err != nil {
			return nil, err
		}
		data = append(data, d...)
	}
	return data, nil
}
Ejemplo n.º 2
0
func (cfg *Config) resolveDependencies1(ref xsd.Ref, have xsdSet, depth int) ([][]byte, error) {
	var result [][]byte
	const maxDepth = 10
	if have[ref.Namespace] {
		return nil, nil
	}

	if depth >= maxDepth {
		return nil, fmt.Errorf("maximum depth of %d reached", maxDepth)
	}

	if ref.Location == "" {
		return nil, fmt.Errorf("do not know where to find schema for %s", ref.Namespace)
	}
	rsp, err := http.Get(ref.Location)
	if err != nil {
		return nil, err
	}
	body, err := ioutil.ReadAll(rsp.Body)
	if err != nil {
		return nil, err
	}

	refs, err := xsd.Imports(body)
	if err != nil {
		return nil, err
	}

	for _, ns := range lookupTargetNS(body) {
		have[ns] = true
	}

	for _, r := range refs {
		if have[ref.Namespace] {
			continue
		}
		d, err := cfg.resolveDependencies1(r, have, depth+1)
		if err != nil {
			return nil, err
		}
		result = append(result, d...)
	}
	return result, nil
}