PageRenderTime 43ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/source/library/Interlace.Pinch/Interlace.Pinch/Languages/Java/JavaLanguage.cs

https://bitbucket.org/VahidN/interlace
C# | 142 lines | 95 code | 22 blank | 25 comment | 5 complexity | e61862a609604deafee02c0a03984a42 MD5 | raw file
  1. #region Using Directives and Copyright Notice
  2. // Copyright (c) 2007-2010, Computer Consultancy Pty Ltd
  3. // All rights reserved.
  4. //
  5. // Redistribution and use in source and binary forms, with or without
  6. // modification, are permitted provided that the following conditions are met:
  7. // * Redistributions of source code must retain the above copyright
  8. // notice, this list of conditions and the following disclaimer.
  9. // * Redistributions in binary form must reproduce the above copyright
  10. // notice, this list of conditions and the following disclaimer in the
  11. // documentation and/or other materials provided with the distribution.
  12. // * Neither the name of the Computer Consultancy Pty Ltd nor the
  13. // names of its contributors may be used to endorse or promote products
  14. // derived from this software without specific prior written permission.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL COMPUTER CONSULTANCY PTY LTD BE LIABLE
  20. // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  21. // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  22. // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  23. // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  24. // LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  25. // OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
  26. // DAMAGE.
  27. using System;
  28. using System.Collections.Generic;
  29. using System.IO;
  30. using System.Text;
  31. using Interlace.Pinch.Dom;
  32. using Interlace.Pinch.Generation;
  33. using Interlace.PropertyLists;
  34. using Interlace.Pinch.Languages.Java;
  35. #endregion
  36. namespace Interlace.Pinch.Languages
  37. {
  38. public class JavaLanguage : Language
  39. {
  40. static Dictionary<IntrinsicType, JavaType> _intrinsics;
  41. static JavaLanguage()
  42. {
  43. _intrinsics = new Dictionary<IntrinsicType, JavaType>();
  44. _intrinsics.Add(IntrinsicType.Float32, new JavaType("float", "Float"));
  45. _intrinsics.Add(IntrinsicType.Float64, new JavaType("double", "Double"));
  46. _intrinsics.Add(IntrinsicType.Int8, new JavaType("byte", "Byte"));
  47. _intrinsics.Add(IntrinsicType.Int16, new JavaType("short", "Short"));
  48. _intrinsics.Add(IntrinsicType.Int32, new JavaType("int", "Integer"));
  49. _intrinsics.Add(IntrinsicType.Int64, new JavaType("long", "Long"));
  50. _intrinsics.Add(IntrinsicType.Decimal, new JavaType("java.math.BigDecimal", null));
  51. _intrinsics.Add(IntrinsicType.Bool, new JavaType("boolean", "Boolean"));
  52. _intrinsics.Add(IntrinsicType.String, new JavaType("String", null));
  53. _intrinsics.Add(IntrinsicType.Bytes, new JavaType("byte[]", null));
  54. }
  55. public JavaLanguage()
  56. : base("java", "Java")
  57. {
  58. }
  59. public override object CreateStructureImplementationHelper(Structure structure, PropertyDictionary options)
  60. {
  61. return new JavaStructure(structure, options);
  62. }
  63. public override object CreateStructureMemberImplementationHelper(StructureMember member)
  64. {
  65. if (member.FieldTypeReference is IntrinsicTypeReference)
  66. {
  67. IntrinsicTypeReference reference = member.FieldTypeReference as IntrinsicTypeReference;
  68. return new JavaStructureMember(member, _intrinsics[reference.Type], null);
  69. }
  70. else
  71. {
  72. DeclarationTypeReference reference = member.FieldTypeReference as DeclarationTypeReference;
  73. NamespaceName name = reference.Declaration.QualifiedName;
  74. string codecName = reference.Declaration.Identifier;
  75. bool isReferenceType = !(reference.Declaration is Enumeration);
  76. if (reference.Declaration.Implementation is JavaStructure)
  77. {
  78. JavaStructure structure = (JavaStructure)reference.Declaration.Implementation;
  79. name = structure.ReferenceTypeName;
  80. codecName = structure.Identifier;
  81. isReferenceType = structure.IsReferenceType;
  82. }
  83. bool isInProtocolNamespace =
  84. name.ContainingName.Equals(member.Parent.Parent.Name);
  85. JavaType type;
  86. if (isInProtocolNamespace)
  87. {
  88. type = new JavaType(name.UnqualifiedName, null);
  89. }
  90. else
  91. {
  92. type = new JavaType(name.ToString(), null);
  93. }
  94. return new JavaStructureMember(member, type, reference.Declaration.Implementation as JavaStructure);
  95. }
  96. }
  97. public override void GenerateFiles(Generator generator, Document document)
  98. {
  99. foreach (Protocol protocol in document.Protocols)
  100. {
  101. foreach (Declaration declaration in protocol.Declarations)
  102. {
  103. JavaStructure implementation = declaration.Implementation as JavaStructure;
  104. string fileSuffix = implementation != null && implementation.IsSurrogate ? "Surrogate.java" : ".java";
  105. generator.GenerateFile(
  106. Path.Combine(generator.DestinationPath, declaration.Identifier + fileSuffix),
  107. Templates.JavaTemplate, "file", "Declaration", declaration);
  108. }
  109. }
  110. }
  111. public override object CreateEnumerationImplementationHelper(Enumeration enumeration, PropertyDictionary options)
  112. {
  113. return new JavaEnumeration(enumeration, options);
  114. }
  115. public override object CreateProtocolImplementationHelper(Protocol protocol, PropertyDictionary options)
  116. {
  117. return new JavaProtocol(protocol, options);
  118. }
  119. }
  120. }