Exemple #1
0
func (me *RestrictionSimpleEnumeration) makePkg(bag *PkgBag) {
	me.elemBase.beforeMakePkg(bag)
	safeName := bag.safeName(ustr.PrependIf(bag.Stacks.CurSimpleType().Name.String(), "T"))
	var doc = sfmt("Returns true if the value of this enumerated %v is %#v.", safeName, me.Value)
	bag.ctd.addMethod(me, safeName, "Is"+bag.safeName(me.Value), "bool", sfmt("return me.String() == %#v", me.Value), doc)
	me.elemBase.afterMakePkg(bag)
}
Exemple #2
0
func (me *SimpleType) makePkg(bag *PkgBag) {
	var typeName = me.Name
	var baseType, safeName = "", ""
	var resolve = true
	var isPt bool
	if len(typeName) == 0 {
		typeName = bag.AnonName(me.longSafeName(bag))
		me.Name = typeName
	} else {
		me.Name = typeName
	}
	typeName = xsdt.NCName(ustr.PrependIf(typeName.String(), "T"))
	me.elemBase.beforeMakePkg(bag)
	bag.Stacks.SimpleType.Push(me)
	safeName = bag.safeName(typeName.String())
	if me.RestrictionSimpleType != nil {
		if baseType = me.RestrictionSimpleType.Base.String(); (len(baseType) == 0) && (len(me.RestrictionSimpleType.SimpleTypes) > 0) {
			resolve, baseType = false, me.RestrictionSimpleType.SimpleTypes[0].Name.String()
		}
	}
	if len(baseType) == 0 {
		baseType = bag.xsdStringTypeRef()
	}
	if resolve {
		baseType = bag.resolveQnameRef(baseType, "T", nil)
	}
	bag.simpleBaseTypes[safeName] = baseType
	if isPt = bag.isParseType(baseType); isPt {
		bag.parseTypes[safeName] = true
	}
	var td = bag.addType(me, safeName, baseType, me.Annotation)
	var doc string
	if isPt {
		doc = sfmt("Since %v is a non-string scalar type (either boolean or numeric), sets the current value obtained from parsing the specified string.", safeName)
	} else {
		doc = sfmt("Since %v is just a simple String type, this merely sets the current value from the specified string.", safeName)
	}
	td.addMethod(nil, "*"+safeName, "Set (s string)", "", sfmt("(*%v)(me).Set(s)", baseType), doc)
	if isPt {
		doc = sfmt("Returns a string representation of this %v's current non-string scalar value.", safeName)
	} else {
		doc = sfmt("Since %v is just a simple String type, this merely returns the current string value.", safeName)
	}
	td.addMethod(nil, safeName, "String", "string", sfmt("return %v(me).String()", baseType), doc)
	doc = sfmt("This convenience method just performs a simple type conversion to %v's alias type %v.", safeName, baseType)
	td.addMethod(nil, safeName, "To"+bag.safeName(baseType), baseType, sfmt("return %v(me)", baseType), doc)
	me.hasElemRestrictionSimpleType.makePkg(bag)
	me.hasElemList.makePkg(bag)
	me.hasElemUnion.makePkg(bag)
	bag.Stacks.SimpleType.Pop()
	me.elemBase.afterMakePkg(bag)
}
Exemple #3
0
func (me *Union) makePkg(bag *PkgBag) {
	var memberTypes []string
	var rtn, rtnSafeName, safeName string
	me.elemBase.beforeMakePkg(bag)
	me.hasElemsSimpleType.makePkg(bag)
	memberTypes = ustr.Split(me.MemberTypes, " ")
	for _, st := range me.SimpleTypes {
		memberTypes = append(memberTypes, st.Name.String())
	}
	for _, mt := range memberTypes {
		rtn = bag.resolveQnameRef(mt, "T", nil)
		safeName, rtnSafeName = bag.safeName(ustr.PrependIf(bag.Stacks.CurSimpleType().Name.String(), "T")), bag.safeName(rtn)
		bag.ctd.addMethod(me, safeName, "To"+rtnSafeName, rtn, sfmt(ustr.Ifs(bag.isParseType(rtn), "var x = new(%v); x.Set(me.String()); return *x", "return %v(me)"), rtn), sfmt("%v is an XSD union-type of several types. This is a simple type conversion to %v, but keep in mind the actual value may or may not be a valid %v value.", safeName, rtnSafeName, rtnSafeName), me.Annotation)
	}
	me.elemBase.afterMakePkg(bag)
}
Exemple #4
0
func (me *List) makePkg(bag *PkgBag) {
	var safeName string
	me.elemBase.beforeMakePkg(bag)
	me.hasElemsSimpleType.makePkg(bag)
	rtr := bag.resolveQnameRef(me.ItemType.String(), "T", nil)
	if len(rtr) == 0 {
		rtr = me.SimpleTypes[0].Name.String()
	}
	st := bag.Stacks.CurSimpleType()
	safeName = bag.safeName(ustr.PrependIf(st.Name.String(), "T"))
	body, doc := "", sfmt("%v declares a String containing a whitespace-separated list of %v values. This Values() method creates and returns a slice of all elements in that list", safeName, rtr)
	body = sfmt("svals := %v.ListValues(string(me)); list = make([]%v, len(svals)); for i, s := range svals { list[i].Set(s) }; return", bag.impName, rtr)
	bag.ctd.addMethod(me, safeName, "Values", sfmt("(list []%v)", rtr), body, doc+".", me.Annotation)
	for baseType := bag.simpleBaseTypes[rtr]; len(baseType) > 0; baseType = bag.simpleBaseTypes[baseType] {
		body = sfmt("svals := %v.ListValues(string(me)); list = make([]%v, len(svals)); for i, s := range svals { list[i].Set(s) }; return", bag.impName, baseType)
		bag.ctd.addMethod(me, safeName, "Values"+bag.safeName(baseType), sfmt("(list []%v)", baseType), body, sfmt("%s, typed as %s.", doc, baseType), me.Annotation)
	}
	me.elemBase.afterMakePkg(bag)
}
Exemple #5
0
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)))
}
Exemple #6
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)
}