Esempio n. 1
0
        private string convertToClientType(ITypeModel type, string source, string target, int level = 0)
        {
            if (type == null)
            {
                return(target + " = " + source + ";");
            }

            IndentedStringBuilder builder = new IndentedStringBuilder();

            SequenceTypeModel   sequenceType   = type as SequenceTypeModel;
            DictionaryTypeModel dictionaryType = type as DictionaryTypeModel;

            if (sequenceType != null)
            {
                var elementType = sequenceType.ElementTypeModel;
                var itemName    = string.Format(CultureInfo.InvariantCulture, "item{0}", level == 0 ? "" : level.ToString(CultureInfo.InvariantCulture));
                var itemTarget  = string.Format(CultureInfo.InvariantCulture, "value{0}", level == 0 ? "" : level.ToString(CultureInfo.InvariantCulture));
                builder.AppendLine("{0} = new ArrayList<{1}>();", target, elementType.ResponseVariant.Name)
                .AppendLine("for ({0} {1} : {2}) {{", elementType.Name, itemName, source)
                .Indent().AppendLine("{0} {1};", elementType.ResponseVariant.Name, itemTarget)
                .AppendLine(convertToClientType(elementType, itemName, itemTarget, level + 1))
                .AppendLine("{0}.add({1});", target, itemTarget)
                .Outdent().Append("}");
                _implImports.Add("java.util.ArrayList");
                return(builder.ToString());
            }
            else if (dictionaryType != null)
            {
                var valueType  = dictionaryType.ValueTypeModel;
                var itemName   = string.Format(CultureInfo.InvariantCulture, "entry{0}", level == 0 ? "" : level.ToString(CultureInfo.InvariantCulture));
                var itemTarget = string.Format(CultureInfo.InvariantCulture, "value{0}", level == 0 ? "" : level.ToString(CultureInfo.InvariantCulture));
                builder.AppendLine("{0} = new HashMap<String, {1}>();", target, valueType.ResponseVariant.Name)
                .AppendLine("for (Map.Entry<String, {0}> {1} : {2}.entrySet()) {{", valueType.Name, itemName, source)
                .Indent().AppendLine("{0} {1};", valueType.ResponseVariant.Name, itemTarget)
                .AppendLine(convertToClientType(valueType, itemName + ".getValue()", itemTarget, level + 1))
                .AppendLine("{0}.put({1}.getKey(), {2});", target, itemName, itemTarget)
                .Outdent().Append("}");
                _implImports.Add("java.util.HashMap");
                return(builder.ToString());
            }
            else if (type.IsPrimaryType(KnownPrimaryType.DateTimeRfc1123))
            {
                return(target + " = " + source + ".getDateTime();");
            }
            else if (type.IsPrimaryType(KnownPrimaryType.UnixTime))
            {
                return(target + " = new DateTime(" + source + " * 1000L, DateTimeZone.UTC);");
            }
            else if (type.IsPrimaryType(KnownPrimaryType.Base64Url))
            {
                return(target + " = " + source + ".getDecodedBytes();");
            }
            else
            {
                return(target + " = " + source + ";");
            }
        }
Esempio n. 2
0
        public override IType NormalizeTypeDeclaration(IType type)
        {
            if (type == null)
            {
                return null;
            }

            if (type is ITypeModel)
            {
                return type;
            }
            if (_visited.ContainsKey(type))
            {
                return _visited[type];
            }

            if (type is PrimaryType)
            {
                _visited[type] = new PrimaryTypeModel(type as PrimaryType);
                return _visited[type];
            }
            if (type is SequenceType)
            {
                SequenceTypeModel model = new SequenceTypeModel(type as SequenceType);
                _visited[type] = model;
                return NormalizeSequenceType(model);
            }
            if (type is DictionaryType)
            {
                DictionaryTypeModel model = new DictionaryTypeModel(type as DictionaryType);
                _visited[type] = model;
                return NormalizeDictionaryType(model);
            }
            if (type is CompositeType)
            {
                CompositeTypeModel model = NewCompositeTypeModel(type as CompositeType);
                _visited[type] = model;
                return NormalizeCompositeType(model);
            }
            if (type is EnumType)
            {
                EnumTypeModel model = NewEnumTypeModel(type as EnumType);
                _visited[type] = model;
                return NormalizeEnumType(model);
            }

            throw new NotSupportedException(string.Format(CultureInfo.InvariantCulture,
                "Type {0} is not supported.", type.GetType()));
        }