00001 /* 00002 * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM) 00003 * Copyright (C) 2004-2006 Philippe Delrieu 00004 * All rights reserved. 00005 * Contact: pdelrieu@openmobileis.org 00006 * 00007 * This library is free software; you can redistribute it and/or 00008 * modify it under the terms of the GNU Lesser General Public 00009 * License as published by the Free Software Foundation; either 00010 * version 2.1 of the License, or any later version. 00011 * 00012 * This library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 00015 * Lesser General Public License for more details. 00016 * 00017 * You should have received a copy of the GNU Lesser General Public 00018 * License along with this library; if not, write to the Free Software 00019 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 00020 * USA 00021 * 00022 * Author : Philippe Delrieu 00023 * 00024 */ 00025 00026 package org.openmobileis.synchro.openmsp.protocol; 00027 00028 import org.openmobileis.common.util.collection.Array; 00029 import org.openmobileis.synchro.openmsp.OpenMSPException; 00030 00038 public class ContainerMessage implements java.io.Serializable { 00043 final static long serialVersionUID = 1L; 00044 00045 00046 // array of ContainerMessage 00047 protected Array children; 00048 00049 protected int cursor = -1; 00050 protected Element content; 00051 00052 protected ContainerMessage() { 00053 children = new Array(5); 00054 } 00055 00056 public ContainerMessage(Element element) { 00057 content = element; 00058 children = new Array(5); 00059 } 00060 00061 public void add (Element element) throws OpenMSPException { 00062 children.add(new AtomicMessage(element)); 00063 } 00064 00065 public void add (ContainerMessage message) throws OpenMSPException { 00066 children.add(message); 00067 } 00068 00069 public Array getAllMessages() { 00070 return children; 00071 } 00072 00073 /* 00074 * Get the next element pointed by the cursor 00075 * @return: CmlContent if it exists or null if is the end of the children list 00076 */ 00077 public ContainerMessage nextMessage() { 00078 cursor++; 00079 if (!(cursor < children.size())) 00080 return null; 00081 else 00082 return (ContainerMessage)children.get(cursor); 00083 } 00084 00085 /* 00086 * Return true if the cursor is not at the end of the children list 00087 * @return : boolean 00088 */ 00089 public boolean hasMoreMessage() { 00090 return cursor + 1 < children.size(); 00091 } 00092 00093 public void resetCursor() { 00094 cursor = -1; 00095 for (int i=0; i<children.size(); i++) { 00096 Object obj = children.get(i); 00097 if (obj != null && obj instanceof ContainerMessage) { 00098 ContainerMessage cont = (ContainerMessage)obj; 00099 cont.resetCursor(); 00100 } 00101 } 00102 } 00103 00104 public int getMessagesSize() { 00105 return children.size(); 00106 } 00107 00108 public Element getElement() { 00109 return content; 00110 } 00111 protected void encode (StringBuffer buffer) { 00112 content.writeBeginTag(buffer); 00113 buffer.append("\n"); 00114 content.writeContent(buffer); 00115 for (int i=0; i < children.size(); i++) { 00116 ((ContainerMessage)children.get(i)).encode(buffer); 00117 } 00118 content.writeEndTag(buffer); 00119 } 00120 00121 }