Message.java

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.synchro.openmsp.OpenMSPException;
00029 
00067 public class Message  implements java.io.Serializable  {
00072   final static long serialVersionUID = 1L;
00073 
00074 
00075   protected ContainerMessage root;
00076   protected int cmdIdCounter = 0;
00077   protected boolean isFinal = true;
00078 
00079 
00080   protected Message(Header header, boolean isFinal) {
00081     root = new ContainerMessage(header);
00082     this.isFinal = isFinal;
00083   }
00084 
00085   protected Message (ContainerMessage root, boolean isFinal) {
00086     this.root = root;
00087      this.isFinal = isFinal;
00088   }
00089 
00090   protected void setFinal (boolean isFinalMessage) {
00091     isFinal = isFinalMessage;
00092   }
00093 
00094   public boolean isFinal() {
00095     return isFinal;
00096   }
00097 
00098   /* return true is there is no command */
00099   public boolean isEmpty() {
00100     return root.getMessagesSize() == 0;
00101   }
00102 
00103   public void add (ContainerMessage container) throws OpenMSPException {
00104     this.setCmdIds(container);
00105     root.add(container);
00106   }
00107 
00108   public void add (Element element)  throws OpenMSPException {
00109     if ((element.hasCommmandId()) && (element.getCmdId()==0))
00110       element.setCmdId(this.nextCmdId());
00111     root.add(element);
00112   }
00113 
00114   public ContainerMessage getRoot() {
00115     return root;
00116   }
00117 
00118   public boolean hasMoreMessage() {
00119     return root.hasMoreMessage();
00120   }
00121 
00122   public void resetCursor() {
00123     root.resetCursor();
00124   }
00125 
00126   public ContainerMessage nextMessage() {
00127     return root.nextMessage();
00128   }
00129 
00130   protected int nextCmdId() {
00131     cmdIdCounter++;
00132     return cmdIdCounter;
00133   }
00134 
00135   public Header getHeader () {
00136     return (Header)root.getElement();
00137   }
00138 
00139 
00140   public String encode() {
00141     StringBuffer buffer = new StringBuffer();
00142     Header header = (Header)root.getElement();
00143     MessageFactory.writeBeginTag(buffer, MessageFactory.TAG_ROOT);
00144     buffer.append("\n");
00145     header.writeBeginTag(buffer);
00146     header.writeContent(buffer);
00147     header.writeEndTag(buffer);
00148     MessageFactory.writeBeginTag(buffer, MessageFactory.TAG_BODY);
00149     buffer.append("\n");
00150     for (int i=0; i < root.children.size(); i++) {
00151       ((ContainerMessage)root.children.get(i)).encode(buffer);
00152     }
00153     if (isFinal)
00154         MessageFactory.writeSingleTag(buffer, MessageFactory.TAG_FINAL);
00155     MessageFactory.writeEndTag(buffer, MessageFactory.TAG_BODY);
00156     MessageFactory.writeEndTag(buffer, MessageFactory.TAG_ROOT);
00157     return buffer.toString();
00158   }
00159 
00160  protected void setCmdIds (ContainerMessage container) {
00161     container.resetCursor();
00162     if ((container.getElement().hasCommmandId()) && (container.getElement().getCmdId()==0))
00163       container.getElement().setCmdId(this.nextCmdId());
00164     while (container.hasMoreMessage()) {
00165           setCmdIds(container.nextMessage());
00166     }
00167  }
00168 
00169 
00170 }
00171 

Generated on Mon Dec 4 11:03:28 2006 for OpenMobileIS by  doxygen 1.5.1-p1