JMap.java 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.jute.compiler;
  19. /**
  20. *
  21. * @author Milind Bhandarkar
  22. */
  23. public class JMap extends JCompType {
  24. static private int level = 0;
  25. static private String getLevel() { return Integer.toString(level); }
  26. static private void incrLevel() { level++; }
  27. static private void decrLevel() { level--; }
  28. static private String getId(String id) { return id+getLevel(); }
  29. private JType mKey;
  30. private JType mValue;
  31. /** Creates a new instance of JMap */
  32. public JMap(JType t1, JType t2) {
  33. super("#error", " ::std::map<"+t1.getCppType()+","+t2.getCppType()+">",
  34. "java.util.TreeMap", "Map", "java.util.TreeMap");
  35. mKey = t1;
  36. mValue = t2;
  37. }
  38. public String getSignature() {
  39. return "{" + mKey.getSignature() + mValue.getSignature() +"}";
  40. }
  41. public String genJavaCompareTo(String fname) {
  42. return "";
  43. }
  44. public String genJavaReadWrapper(String fname, String tag, boolean decl) {
  45. StringBuffer ret = new StringBuffer("");
  46. if (decl) {
  47. ret.append(" java.util.TreeMap "+fname+";\n");
  48. }
  49. ret.append(" {\n");
  50. incrLevel();
  51. ret.append(" org.apache.jute.Index "+getId("midx")+" = a_.startMap(\""+tag+"\");\n");
  52. ret.append(" "+fname+"=new java.util.TreeMap();\n");
  53. ret.append(" for (; !"+getId("midx")+".done(); "+getId("midx")+".incr()) {\n");
  54. ret.append(mKey.genJavaReadWrapper(getId("k"),getId("k"),true));
  55. ret.append(mValue.genJavaReadWrapper(getId("v"),getId("v"),true));
  56. ret.append(" "+fname+".put("+getId("k")+","+getId("v")+");\n");
  57. ret.append(" }\n");
  58. ret.append(" a_.endMap(\""+tag+"\");\n");
  59. decrLevel();
  60. ret.append(" }\n");
  61. return ret.toString();
  62. }
  63. public String genJavaReadMethod(String fname, String tag) {
  64. return genJavaReadWrapper(fname, tag, false);
  65. }
  66. public String genJavaWriteWrapper(String fname, String tag) {
  67. StringBuffer ret = new StringBuffer(" {\n");
  68. incrLevel();
  69. ret.append(" a_.startMap("+fname+",\""+tag+"\");\n");
  70. ret.append(" java.util.Set "+getId("es")+" = "+fname+".entrySet();\n");
  71. ret.append(" for(java.util.Iterator "+getId("midx")+" = "+getId("es")+".iterator(); "+getId("midx")+".hasNext(); ) {\n");
  72. ret.append(" java.util.Map.Entry "+getId("me")+" = (java.util.Map.Entry) "+getId("midx")+".next();\n");
  73. ret.append(" "+mKey.getJavaWrapperType()+" "+getId("k")+" = ("+mKey.getJavaWrapperType()+") "+getId("me")+".getKey();\n");
  74. ret.append(" "+mValue.getJavaWrapperType()+" "+getId("v")+" = ("+mValue.getJavaWrapperType()+") "+getId("me")+".getValue();\n");
  75. ret.append(mKey.genJavaWriteWrapper(getId("k"),getId("k")));
  76. ret.append(mValue.genJavaWriteWrapper(getId("v"),getId("v")));
  77. ret.append(" }\n");
  78. ret.append(" a_.endMap("+fname+",\""+tag+"\");\n");
  79. ret.append(" }\n");
  80. decrLevel();
  81. return ret.toString();
  82. }
  83. public String genJavaWriteMethod(String fname, String tag) {
  84. return genJavaWriteWrapper(fname, tag);
  85. }
  86. }