XmlOutputArchive.java 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  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 com.yahoo.jute;
  19. import java.io.IOException;
  20. import java.util.List;
  21. import java.util.TreeMap;
  22. import java.io.PrintStream;
  23. import java.io.OutputStream;
  24. import java.util.Stack;
  25. /**
  26. *
  27. * @author Milind Bhandarkar
  28. */
  29. class XmlOutputArchive implements OutputArchive {
  30. private PrintStream stream;
  31. private int indent = 0;
  32. private Stack compoundStack;
  33. private void putIndent() {
  34. StringBuffer sb = new StringBuffer("");
  35. for (int idx = 0; idx < indent; idx++) {
  36. sb.append(" ");
  37. }
  38. stream.print(sb.toString());
  39. }
  40. private void addIndent() {
  41. indent++;
  42. }
  43. private void closeIndent() {
  44. indent--;
  45. }
  46. private void printBeginEnvelope(String tag) {
  47. if (!compoundStack.empty()) {
  48. String s = (String) compoundStack.peek();
  49. if ("struct".equals(s)) {
  50. putIndent();
  51. stream.print("<member>\n");
  52. addIndent();
  53. putIndent();
  54. stream.print("<name>"+tag+"</name>\n");
  55. putIndent();
  56. stream.print("<value>");
  57. } else if ("vector".equals(s)) {
  58. stream.print("<value>");
  59. } else if ("map".equals(s)) {
  60. stream.print("<value>");
  61. }
  62. } else {
  63. stream.print("<value>");
  64. }
  65. }
  66. private void printEndEnvelope(String tag) {
  67. if (!compoundStack.empty()) {
  68. String s = (String) compoundStack.peek();
  69. if ("struct".equals(s)) {
  70. stream.print("</value>\n");
  71. closeIndent();
  72. putIndent();
  73. stream.print("</member>\n");
  74. } else if ("vector".equals(s)) {
  75. stream.print("</value>\n");
  76. } else if ("map".equals(s)) {
  77. stream.print("</value>\n");
  78. }
  79. } else {
  80. stream.print("</value>\n");
  81. }
  82. }
  83. private void insideVector(String tag) {
  84. printBeginEnvelope(tag);
  85. compoundStack.push("vector");
  86. }
  87. private void outsideVector(String tag) throws IOException {
  88. String s = (String) compoundStack.pop();
  89. if (!"vector".equals(s)) {
  90. throw new IOException("Error serializing vector.");
  91. }
  92. printEndEnvelope(tag);
  93. }
  94. private void insideMap(String tag) {
  95. printBeginEnvelope(tag);
  96. compoundStack.push("map");
  97. }
  98. private void outsideMap(String tag) throws IOException {
  99. String s = (String) compoundStack.pop();
  100. if (!"map".equals(s)) {
  101. throw new IOException("Error serializing map.");
  102. }
  103. printEndEnvelope(tag);
  104. }
  105. private void insideRecord(String tag) {
  106. printBeginEnvelope(tag);
  107. compoundStack.push("struct");
  108. }
  109. private void outsideRecord(String tag) throws IOException {
  110. String s = (String) compoundStack.pop();
  111. if (!"struct".equals(s)) {
  112. throw new IOException("Error serializing record.");
  113. }
  114. printEndEnvelope(tag);
  115. }
  116. static XmlOutputArchive getArchive(OutputStream strm) {
  117. return new XmlOutputArchive(strm);
  118. }
  119. /** Creates a new instance of XmlOutputArchive */
  120. public XmlOutputArchive(OutputStream out) {
  121. stream = new PrintStream(out);
  122. compoundStack = new Stack();
  123. }
  124. public void writeByte(byte b, String tag) throws IOException {
  125. printBeginEnvelope(tag);
  126. stream.print("<ex:i1>");
  127. stream.print(Byte.toString(b));
  128. stream.print("</ex:i1>");
  129. printEndEnvelope(tag);
  130. }
  131. public void writeBool(boolean b, String tag) throws IOException {
  132. printBeginEnvelope(tag);
  133. stream.print("<boolean>");
  134. stream.print(b ? "1" : "0");
  135. stream.print("</boolean>");
  136. printEndEnvelope(tag);
  137. }
  138. public void writeInt(int i, String tag) throws IOException {
  139. printBeginEnvelope(tag);
  140. stream.print("<i4>");
  141. stream.print(Integer.toString(i));
  142. stream.print("</i4>");
  143. printEndEnvelope(tag);
  144. }
  145. public void writeLong(long l, String tag) throws IOException {
  146. printBeginEnvelope(tag);
  147. stream.print("<ex:i8>");
  148. stream.print(Long.toString(l));
  149. stream.print("</ex:i8>");
  150. printEndEnvelope(tag);
  151. }
  152. public void writeFloat(float f, String tag) throws IOException {
  153. printBeginEnvelope(tag);
  154. stream.print("<ex:float>");
  155. stream.print(Float.toString(f));
  156. stream.print("</ex:float>");
  157. printEndEnvelope(tag);
  158. }
  159. public void writeDouble(double d, String tag) throws IOException {
  160. printBeginEnvelope(tag);
  161. stream.print("<double>");
  162. stream.print(Double.toString(d));
  163. stream.print("</double>");
  164. printEndEnvelope(tag);
  165. }
  166. public void writeString(String s, String tag) throws IOException {
  167. printBeginEnvelope(tag);
  168. stream.print("<string>");
  169. stream.print(Utils.toXMLString(s));
  170. stream.print("</string>");
  171. printEndEnvelope(tag);
  172. }
  173. public void writeBuffer(byte buf[], String tag)
  174. throws IOException {
  175. printBeginEnvelope(tag);
  176. stream.print("<string>");
  177. stream.print(Utils.toXMLBuffer(buf));
  178. stream.print("</string>");
  179. printEndEnvelope(tag);
  180. }
  181. public void writeRecord(Record r, String tag) throws IOException {
  182. r.serialize(this, tag);
  183. }
  184. public void startRecord(Record r, String tag) throws IOException {
  185. insideRecord(tag);
  186. stream.print("<struct>\n");
  187. addIndent();
  188. }
  189. public void endRecord(Record r, String tag) throws IOException {
  190. closeIndent();
  191. putIndent();
  192. stream.print("</struct>");
  193. outsideRecord(tag);
  194. }
  195. public void startVector(List v, String tag) throws IOException {
  196. insideVector(tag);
  197. stream.print("<array>\n");
  198. addIndent();
  199. }
  200. public void endVector(List v, String tag) throws IOException {
  201. closeIndent();
  202. putIndent();
  203. stream.print("</array>");
  204. outsideVector(tag);
  205. }
  206. public void startMap(TreeMap v, String tag) throws IOException {
  207. insideMap(tag);
  208. stream.print("<array>\n");
  209. addIndent();
  210. }
  211. public void endMap(TreeMap v, String tag) throws IOException {
  212. closeIndent();
  213. putIndent();
  214. stream.print("</array>");
  215. outsideMap(tag);
  216. }
  217. }