示例#1
0
func (me *Attribute) makePkg(bag *PkgBag) {
	var safeName, typeName, tmp, key, defVal, impName string
	var defName = "Default"
	me.elemBase.beforeMakePkg(bag)
	if len(me.Form) == 0 {
		me.Form = bag.Schema.AttributeFormDefault
	}
	me.hasElemsSimpleType.makePkg(bag)
	if len(me.Ref) > 0 {
		key = bag.resolveQnameRef(me.Ref.String(), "", &impName)
		tmp = ustr.PrefixWithSep(impName, ".", idPrefix+"HasAttr_"+bag.safeName(me.Ref.String()[(strings.Index(me.Ref.String(), ":")+1):]))
		if bag.attRefImps[me], bag.attsKeys[me] = impName, key; len(bag.attsCache[key]) == 0 {
			bag.attsCache[key] = tmp
		}
	} else {
		safeName = bag.safeName(me.Name.String())
		if typeName = me.Type.String(); (len(typeName) == 0) && (len(me.SimpleTypes) > 0) {
			typeName = me.SimpleTypes[0].Name.String()
		} else {
			if len(typeName) == 0 {
				typeName = bag.xsdStringTypeRef()
			}
			typeName = bag.resolveQnameRef(typeName, "T", &impName)
		}
		if defVal = me.Default; len(defVal) == 0 {
			defName, defVal = "Fixed", me.Fixed
		}
		if me.Parent() == bag.Schema {
			key = safeName
		} else {
			key = safeName + "_" + bag.safeName(typeName) + "_" + bag.safeName(defVal)
		}
		if len(bag.attsCache[key]) == 0 {
			tmp = idPrefix + "HasAttr_" + key
			bag.attsKeys[me] = key
			bag.attsCache[key] = tmp
			var td = bag.addType(me, tmp, "", me.Annotation)
			td.addField(me, safeName, typeName, ustr.Ifs(len(bag.Schema.TargetNamespace) > 0, bag.Schema.TargetNamespace.String()+" ", "")+me.Name.String()+",attr", me.Annotation)
			if isPt := bag.isParseType(typeName); len(defVal) > 0 {
				doc := sfmt("Returns the %v value for %v -- "+ustr.Ifs(isPt, "%v", "%#v"), strings.ToLower(defName), safeName, defVal)
				if isPt {
					if PkgGen.ForceParseForDefaults {
						td.addMethod(nil, tmp, safeName+defName, typeName, sfmt("var x = new(%v); x.Set(%#v); return *x", typeName, defVal), doc)
					} else {
						td.addMethod(nil, tmp, safeName+defName, typeName, sfmt("return %v(%v)", typeName, defVal), doc)
					}
				} else {
					td.addMethod(nil, tmp, safeName+defName, typeName, sfmt("return %v(%#v)", typeName, defVal), doc)
				}
			}
		} else {
			bag.attsKeys[me] = key
		}
	}
	me.elemBase.afterMakePkg(bag)
}
示例#2
0
func (me *Schema) globalComplexType(bag *PkgBag, name string) (ct *ComplexType) {
	var imp string
	for _, ct = range me.ComplexTypes {
		if bag.resolveQnameRef(ustr.PrefixWithSep(me.XMLNamespacePrefix, ":", ct.Name.String()), "T", &imp) == name {
			return
		}
	}
	for _, ss := range me.XMLIncludedSchemas {
		if ct = ss.globalComplexType(bag, name); ct != nil {
			return
		}
	}
	ct = nil
	return
}
示例#3
0
func (me *Schema) globalElement(bag *PkgBag, name string) (el *Element) {
	var imp string
	if len(name) > 0 {
		var rname = bag.resolveQnameRef(name, "", &imp)
		for _, el = range me.Elements {
			if bag.resolveQnameRef(ustr.PrefixWithSep(me.XMLNamespacePrefix, ":", el.Name.String()), "", &imp) == rname {
				return
			}
		}
		for _, ss := range me.XMLIncludedSchemas {
			if el = ss.globalElement(bag, name); el != nil {
				return
			}
		}
	}
	el = nil
	return
}
示例#4
0
func (me *Schema) globalComplexType(bag *PkgBag, name string, loadedSchemas map[string]bool) (ct *ComplexType) {
	var imp string
	for _, ct = range me.ComplexTypes {
		if bag.resolveQnameRef(ustr.PrefixWithSep(me.XMLNamespacePrefix, ":", ct.Name.String()), "T", &imp) == name {
			return
		}
	}
	loadedSchemas[me.loadUri] = true
	for _, ss := range me.XMLIncludedSchemas {
		if v, ok := loadedSchemas[ss.loadUri]; ok && v {
			//fmt.Printf("Ignoring processed schema: %s\n", ss.loadUri)
			continue
		}
		if ct = ss.globalComplexType(bag, name, loadedSchemas); ct != nil {
			return
		}
	}
	ct = nil
	return
}
示例#5
0
文件: makepkg.go 项目: tmc/xsd
func (me *PkgBag) resolveQnameRef(ref, pref string, noUsageRec *string) string {
	var ns = me.Schema.XMLNamespaces[""]
	var impName = ""
	if len(ref) == 0 {
		return ""
	}
	if pos := strings.Index(ref, ":"); pos > 0 {
		impName, ns = ref[:pos], me.Schema.XMLNamespaces[ref[:pos]]
		ref = ref[(pos + 1):]
	}
	if ns == xsdNamespaceUri {
		impName, pref = me.impName, ""
	}
	if ns == me.Schema.TargetNamespace.String() {
		impName = ""
	}
	if noUsageRec == nil { /*me.impsUsed[impName] = true*/
	} else {
		*noUsageRec = impName
	}
	return ustr.PrefixWithSep(impName, ".", me.safeName(ustr.PrependIf(ref, pref)))
}
示例#6
0
文件: makepkg.go 项目: tmc/xsd
func (me *PkgBag) xsdStringTypeRef() string {
	return ustr.PrefixWithSep(me.Schema.XSDNamespacePrefix, ":", "string")
}
示例#7
0
func (me *Element) makePkg(bag *PkgBag) {
	var (
		safeName, typeName, valueType, tmp, key, defVal, impName string
		subEl                                                    *Element
	)
	asterisk, defName, doc := "", "Default", ""
	me.elemBase.beforeMakePkg(bag)
	if len(me.Form) == 0 {
		me.Form = bag.Schema.ElementFormDefault
	}
	me.hasElemsSimpleType.makePkg(bag)
	me.hasElemComplexType.makePkg(bag)
	if len(me.Ref) > 0 {
		key = bag.resolveQnameRef(me.Ref.String(), "", &impName)
		for pref, cache := range map[string]map[string]string{"HasElem_": bag.elemsCacheOnce, "HasElems_": bag.elemsCacheMult} {
			tmp = ustr.PrefixWithSep(impName, ".", idPrefix+pref+bag.safeName(me.Ref.String()[(strings.Index(me.Ref.String(), ":")+1):]))
			if bag.elemRefImps[me], bag.elemKeys[me] = impName, key; len(cache[key]) == 0 {
				cache[key] = tmp
			}
		}
	} else {
		safeName = bag.safeName(me.Name.String())
		if typeName = me.Type.String(); (len(typeName) == 0) && ((me.ComplexType != nil) || (len(me.SimpleTypes) > 0)) {
			if me.ComplexType != nil {
				asterisk, typeName = "*", me.ComplexType.Name.String()
			} else {
				typeName = me.SimpleTypes[0].Name.String()
			}
		} else {
			if len(typeName) == 0 {
				typeName = bag.xsdStringTypeRef()
			}
			if typeName = bag.resolveQnameRef(typeName, "T", &impName); bag.Schema.RootSchema().globalComplexType(bag, typeName) != nil {
				asterisk = "*"
			}
		}
		if defVal = me.Default; len(defVal) == 0 {
			defName, defVal = "Fixed", me.Fixed
		}
		if me.Parent() == bag.Schema {
			key = safeName
		} else {
			key = bag.Stacks.FullName() + "_" + safeName + "_" + bag.safeName(typeName) + "_" + bag.safeName(defVal)
		}
		if valueType = bag.simpleContentValueTypes[typeName]; len(valueType) == 0 {
			valueType = typeName
		}
		isPt := bag.isParseType(valueType)
		if _, isChoice := me.Parent().(*Choice); isChoice && isPt {
			asterisk = "*"
		}
		for pref, cache := range map[string]map[string]string{"HasElem_": bag.elemsCacheOnce, "HasElems_": bag.elemsCacheMult} {
			if tmp = idPrefix + pref + key; !bag.elemsWritten[tmp] {
				bag.elemsWritten[tmp], bag.elemKeys[me] = true, key
				cache[key] = tmp
				var td = bag.addType(me, tmp, "", me.Annotation)
				td.addField(me, ustr.Ifs(pref == "HasElems_", pluralize(safeName), safeName), ustr.Ifs(pref == "HasElems_", "[]"+asterisk+typeName, asterisk+typeName), ustr.Ifs(len(bag.Schema.TargetNamespace) > 0, bag.Schema.TargetNamespace.String()+" ", "")+me.Name.String(), me.Annotation)
				if me.parent == bag.Schema {
					for _, subEl = range bag.Schema.RootSchema().globalSubstitutionElems(me) {
						td.addEmbed(subEl, idPrefix+pref+bag.safeName(subEl.Name.String()), subEl.Annotation)
					}
				}
				if len(defVal) > 0 {
					doc = sfmt("Returns the %v value for %v -- "+ustr.Ifs(isPt, "%v", "%#v"), strings.ToLower(defName), safeName, defVal)
					if isPt {
						if PkgGen.ForceParseForDefaults {
							td.addMethod(nil, tmp, safeName+defName, valueType, sfmt("var x = new(%v); x.Set(%#v); return *x", valueType, defVal), doc)
						} else {
							td.addMethod(nil, tmp, safeName+defName, valueType, sfmt("return %v(%v)", valueType, defVal), doc)
						}
					} else {
						td.addMethod(nil, tmp, safeName+defName, valueType, sfmt("return %v(%#v)", valueType, defVal), doc)
					}
				}
			}
		}
	}
	me.elemBase.afterMakePkg(bag)
}
示例#8
0
func (me *ComplexType) makePkg(bag *PkgBag) {
	var att *Attribute
	var attGroup *AttributeGroup
	var ctBaseType, ctValueType, typeSafeName string
	var allAtts = map[*Attribute]bool{}
	var allAttGroups = map[*AttributeGroup]bool{}
	var allElems = map[*Element]bool{}
	var allElemGroups = map[*Group]bool{}
	var elsDone, grsDone = map[string]bool{}, map[string]bool{}
	var allChoices, tmpChoices = []*Choice{}, []*Choice{me.Choice}
	var allSeqs, tmpSeqs = []*Sequence{}, []*Sequence{me.Sequence}
	var el *Element
	var elGr *Group
	var mixed = false
	me.elemBase.beforeMakePkg(bag)
	me.hasElemsAttribute.makePkg(bag)
	me.hasElemsAnyAttribute.makePkg(bag)
	me.hasElemsAttributeGroup.makePkg(bag)
	me.hasElemAll.makePkg(bag)
	me.hasElemChoice.makePkg(bag)
	me.hasElemGroup.makePkg(bag)
	me.hasElemSequence.makePkg(bag)
	me.hasElemComplexContent.makePkg(bag)
	me.hasElemSimpleContent.makePkg(bag)
	if len(me.Name) == 0 {
		me.Name = bag.AnonName(me.longSafeName(bag))
	}
	typeSafeName = bag.safeName(ustr.PrependIf(me.Name.String(), "T"))
	var td = bag.addType(me, typeSafeName, "", me.Annotation)
	for _, att = range me.Attributes {
		allAtts[att] = true
	}
	for _, attGroup = range me.AttributeGroups {
		allAttGroups[attGroup] = true
	}
	allChoices, allSeqs = Flattened(tmpChoices, tmpSeqs)
	if me.All != nil {
		for _, el = range me.All.Elements {
			allElems[el] = true
		}
	}
	if me.Group != nil {
		allElemGroups[me.Group] = true
	}
	if mixed = me.Mixed; me.ComplexContent != nil {
		mixed = mixed || me.ComplexContent.Mixed
		td.addAnnotations(me.ComplexContent.Annotation)
		if me.ComplexContent.ExtensionComplexContent != nil {
			td.addAnnotations(me.ComplexContent.ExtensionComplexContent.Annotation)
			if me.ComplexContent.ExtensionComplexContent.All != nil {
				for _, el = range me.ComplexContent.ExtensionComplexContent.All.Elements {
					allElems[el] = true
				}
			}
			for _, elGr = range me.ComplexContent.ExtensionComplexContent.Groups {
				allElemGroups[elGr] = true
			}
			tmpChoices, tmpSeqs = Flattened(me.ComplexContent.ExtensionComplexContent.Choices, me.ComplexContent.ExtensionComplexContent.Sequences)
			allChoices, allSeqs = append(allChoices, tmpChoices...), append(allSeqs, tmpSeqs...)
			for _, att = range me.ComplexContent.ExtensionComplexContent.Attributes {
				allAtts[att] = true
			}
			for _, attGroup = range me.ComplexContent.ExtensionComplexContent.AttributeGroups {
				allAttGroups[attGroup] = true
			}
			if len(me.ComplexContent.ExtensionComplexContent.Base) > 0 {
				ctBaseType = me.ComplexContent.ExtensionComplexContent.Base.String()
			}
		}
		if me.ComplexContent.RestrictionComplexContent != nil {
			td.addAnnotations(me.ComplexContent.RestrictionComplexContent.Annotation)
			if me.ComplexContent.RestrictionComplexContent.All != nil {
				for _, el = range me.ComplexContent.RestrictionComplexContent.All.Elements {
					allElems[el] = true
				}
			}
			tmpChoices, tmpSeqs = Flattened(me.ComplexContent.RestrictionComplexContent.Choices, me.ComplexContent.RestrictionComplexContent.Sequences)
			allChoices, allSeqs = append(allChoices, tmpChoices...), append(allSeqs, tmpSeqs...)
			for _, att = range me.ComplexContent.RestrictionComplexContent.Attributes {
				allAtts[att] = true
			}
			for _, attGroup = range me.ComplexContent.RestrictionComplexContent.AttributeGroups {
				allAttGroups[attGroup] = true
			}
			if len(me.ComplexContent.RestrictionComplexContent.Base) > 0 {
				ctBaseType = me.ComplexContent.RestrictionComplexContent.Base.String()
			}
		}
	}
	if me.SimpleContent != nil {
		td.addAnnotations(me.SimpleContent.Annotation)
		if me.SimpleContent.ExtensionSimpleContent != nil {
			if len(me.SimpleContent.ExtensionSimpleContent.Base) > 0 {
				ctBaseType = me.SimpleContent.ExtensionSimpleContent.Base.String()
			}
			td.addAnnotations(me.SimpleContent.ExtensionSimpleContent.Annotation)
			for _, att = range me.SimpleContent.ExtensionSimpleContent.Attributes {
				allAtts[att] = true
			}
			for _, attGroup = range me.SimpleContent.ExtensionSimpleContent.AttributeGroups {
				allAttGroups[attGroup] = true
			}
			if (len(ctValueType) == 0) && (len(me.SimpleContent.ExtensionSimpleContent.Base) > 0) {
				ctValueType = me.SimpleContent.ExtensionSimpleContent.Base.String()
			}
		}
		if me.SimpleContent.RestrictionSimpleContent != nil {
			if len(me.SimpleContent.RestrictionSimpleContent.Base) > 0 {
				ctBaseType = me.SimpleContent.RestrictionSimpleContent.Base.String()
			}
			td.addAnnotations(me.SimpleContent.RestrictionSimpleContent.Annotation)
			for _, att = range me.SimpleContent.RestrictionSimpleContent.Attributes {
				allAtts[att] = true
			}
			for _, attGroup = range me.SimpleContent.RestrictionSimpleContent.AttributeGroups {
				allAttGroups[attGroup] = true
			}
			if (len(ctValueType) == 0) && (len(me.SimpleContent.RestrictionSimpleContent.Base) > 0) {
				ctValueType = me.SimpleContent.RestrictionSimpleContent.Base.String()
			}
			if (len(ctValueType) == 0) && (len(me.SimpleContent.RestrictionSimpleContent.SimpleTypes) > 0) {
				ctValueType = me.SimpleContent.RestrictionSimpleContent.SimpleTypes[0].Name.String()
			}
			for _, enum := range me.SimpleContent.RestrictionSimpleContent.Enumerations {
				println("ENUMTODO!?! Whoever sees this message, please post an issue at github.com/metaleap/go-xsd with a link to the XSD..." + enum.selfName().String())
			}
		}
	}
	if ctBaseType = bag.resolveQnameRef(ctBaseType, "T", nil); len(ctBaseType) > 0 {
		td.addEmbed(nil, bag.safeName(ctBaseType))
	} else if ctValueType = bag.resolveQnameRef(ctValueType, "T", nil); len(ctValueType) > 0 {
		bag.simpleContentValueTypes[typeSafeName] = ctValueType
		td.addField(nil, idPrefix+"Value", ctValueType, ",chardata")
		chain := sfmt("me.%vValue", idPrefix)
		td.addMethod(nil, "*"+typeSafeName, sfmt("To%v", bag.safeName(ctValueType)), ctValueType, sfmt("return %v", chain), sfmt("Simply returns the value of its %vValue field.", idPrefix))
		ttn := ctValueType
		for ttd := bag.declTypes[ctValueType]; ttd != nil; ttd = bag.declTypes[ttn] {
			if ttd != nil {
				bag.declConvs[ttd.Name] = true
			}
			if ttn = ttd.Type; len(ttn) > 0 {
				chain += sfmt(".To%v()", bag.safeName(ttn))
				td.addMethod(nil, "*"+typeSafeName, sfmt("To%v", bag.safeName(ttn)), ttn, sfmt("return %v", chain), sfmt("Returns the value of its %vValue field as a %v (which %v is just aliasing).", idPrefix, ttn, ctValueType))
			} else {
				break
			}
		}
		if (!strings.HasPrefix(ctValueType, "xsdt.")) && (bag.declTypes[ctValueType] == nil) {
			println("NOTFOUND: " + ctValueType)
		}
	} else if mixed {
		td.addEmbed(nil, idPrefix+"HasCdata")
	}
	for elGr, _ = range allElemGroups {
		subMakeElemGroup(bag, td, elGr, grsDone, anns(nil, me.ComplexContent)...)
	}
	for el, _ = range allElems {
		subMakeElem(bag, td, el, elsDone, 1, anns(me.All, nil)...)
	}
	for _, ch := range allChoices {
		for _, el = range ch.Elements {
			subMakeElem(bag, td, el, elsDone, ch.hasAttrMaxOccurs.Value(), ch.Annotation)
		}
		for _, elGr = range ch.Groups {
			subMakeElemGroup(bag, td, elGr, grsDone, ch.Annotation)
		}
	}
	for _, seq := range allSeqs {
		for _, el = range seq.Elements {
			subMakeElem(bag, td, el, elsDone, seq.hasAttrMaxOccurs.Value(), seq.Annotation)
		}
		for _, elGr = range seq.Groups {
			subMakeElemGroup(bag, td, elGr, grsDone, seq.Annotation)
		}
	}
	for attGroup, _ = range allAttGroups {
		td.addEmbed(attGroup, ustr.PrefixWithSep(bag.attGroupRefImps[attGroup], ".", bag.attGroups[attGroup][(strings.Index(bag.attGroups[attGroup], ".")+1):]), attGroup.Annotation)
	}

	for att, _ = range allAtts {
		if key := bag.attsKeys[att]; len(key) > 0 {
			td.addEmbed(att, ustr.PrefixWithSep(bag.attRefImps[att], ".", bag.attsCache[key][(strings.Index(bag.attsCache[key], ".")+1):]), att.Annotation)
		}
	}
	me.elemBase.afterMakePkg(bag)
}