1 /* 2 * Copyright (C) 2007 Esmertec AG. 3 * Copyright (C) 2007 The Android Open Source Project 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 */ 17 18 package com.android.im.imps; 19 20 import java.io.IOException; 21 import java.io.OutputStream; 22 import java.util.Map; 23 24 import com.android.im.imps.ImpsConstants.ImpsVersion; 25 26 public class WbxmlPrimitiveSerializer implements PrimitiveSerializer { 27 28 private String mVersionNs; 29 private String mTransacNs; 30 31 public WbxmlPrimitiveSerializer(ImpsVersion impsVersion, String versionNs, 32 String transacNs) { 33 mVersionNs = versionNs; 34 mTransacNs = transacNs; 35 mWbxmlSerializer = new WbxmlSerializer(impsVersion); 36 } 37 38 private WbxmlSerializer mWbxmlSerializer; 39 40 public void serialize(Primitive primitive, OutputStream out) throws IOException, 41 SerializerException { 42 mWbxmlSerializer.reset(); 43 mWbxmlSerializer.setOutput(out); 44 PrimitiveElement elem = primitive.createMessage(mVersionNs, mTransacNs); 45 writeElement(elem); 46 } 47 48 private void writeElement(PrimitiveElement element) throws IOException, 49 SerializerException { 50 String name = element.getTagName(); 51 String[] atts = null; 52 Map<String, String> attrMap = element.getAttributes(); 53 if(attrMap != null && attrMap.size() > 0) { 54 atts = new String[attrMap.size() * 2]; 55 int index = 0; 56 for (Map.Entry<String, String> entry : attrMap.entrySet()) { 57 atts[index++] = entry.getKey(); 58 atts[index++] = entry.getValue(); 59 } 60 } 61 62 mWbxmlSerializer.startElement(name, atts); 63 64 String contents = element.getContents(); 65 if(contents != null) { 66 mWbxmlSerializer.characters(contents); 67 } 68 if(element.getChildCount() > 0) { 69 for(PrimitiveElement child : element.getChildren()) { 70 writeElement(child); 71 } 72 } 73 mWbxmlSerializer.endElement(); 74 } 75 } 76