Main Page | Packages | Class Hierarchy | Class List | Directories | File List | Class Members | Related Pages

MessageFactory.java

00001 /*
00002  * OpenMobileIS - a free Java(TM) Framework for mobile applications Java(TM)
00003  * Copyright (C) 2004-2005 Philippe Delrieu
00004  * All rights reserved.
00005  * Contact: openmobileis@e-care.fr
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 
00040 public class MessageFactory extends Handler {
00041 
00042 
00043   public static String TAG_ROOT = "OpenMSP";
00044   public static String TAG_HEADER = "SyncHdr";
00045   public static String TAG_VERSION = "Version";
00046   public static String TAG_SESSION = "SessionID";
00047   public static String TAG_MSGID = "MsgID";
00048   public static String TAG_TARGET = "Target";
00049   public static String TAG_SOURCE = "Source";
00050   public static String TAG_URI = "LocURI";
00051   public static String TAG_BODY = "SyncBody";
00052   public static String TAG_SYNC = "Sync";
00053   public static String TAG_CMDID = "CmdID";
00054   public static String TAG_CRED = "Cred";
00055   public static String TAG_META = "Meta";
00056   public static String TAG_DATA = "Data";
00057   public static String TAG_ADD = "Add";
00058   public static String TAG_REPLACE = "Replace";
00059   public static String TAG_DELETE = "Delete";
00060   public static String TAG_MAP = "Map";
00061   public static String TAG_RESULT = "Result";
00062   public static String TAG_ITEM = "Item";
00063   public static String TAG_GET = "Get";
00064   public static String TAG_CMD_REF = "CmdRef";
00065   public static String TAG_SOURCE_REF = "SourceRef";
00066   public static String TAG_TARGET_REF = "TargetRef";
00067   public static String TAG_SEQUENCE = "Sequence";
00068   public static String TAG_STATUS = "Status";
00069   public static String TAG_MAP_ITEM = "MapItem";
00070   public static String TAG_FINAL = "Final";
00071 
00072   // link between elementType and Tags
00073   protected static String[] tags =
00074     {TAG_HEADER, TAG_SYNC, TAG_GET, TAG_ADD, TAG_REPLACE, TAG_DELETE, TAG_MAP, TAG_RESULT, TAG_STATUS, TAG_ITEM, TAG_MAP_ITEM, TAG_SEQUENCE, TAG_FINAL};
00075 
00076   private static MessageFactory factory;
00077 
00078 
00079   public static MessageFactory getFactory() {
00080      if (factory == null)  {
00081         synchronized(org.openmobileis.synchro.openmsp.protocol.MessageFactory.class) {
00082         if (factory == null)  {
00083           factory = new MessageFactory();
00084         }
00085       }
00086     }
00087     return factory;
00088   }
00089 
00090 
00091   public Message getMessage (String openML) throws OpenMSPException {
00092      DefaultHandler handler = new DefaultHandler();
00093      Parser parser = new Parser(handler);
00094      parser.parse(openML);
00095      return handler.getMessage();
00096  }
00097 
00098   public Message createMessage (Header header, boolean isFinal) {
00099     return new Message(header, isFinal);
00100   }
00101 
00102   public Message createMessage (Header header) {
00103     return new Message(header, true);
00104   }
00105 
00107   protected static void writeElement (StringBuffer buffer, String tag, String value) {
00108     writeBeginTag(buffer, tag);
00109     buffer.append(value);
00110     writeEndTag(buffer, tag);
00111   }
00112 
00113   protected static void writeBeginTagForElement (StringBuffer buffer, int elementType) {
00114     writeBeginTag (buffer, tags[elementType]);
00115   }
00116 
00117   protected static void writeEndTagForElement (StringBuffer buffer, int elementType) {
00118     writeEndTag (buffer, tags[elementType]);
00119   }
00120 
00121 
00122   protected static void writeBeginTag (StringBuffer buffer, String tag) {
00123     buffer.append("<");
00124     buffer.append(tag);
00125     buffer.append(">");
00126     }
00127 
00128   protected static void writeSingleTag (StringBuffer buffer, String tag) {
00129     buffer.append("<");
00130     buffer.append(tag);
00131     buffer.append("/>");
00132   }
00133 
00134   protected static void writeEndTag (StringBuffer buffer, String tag) {
00135     buffer.append("</");
00136     buffer.append(tag);
00137     buffer.append(">");
00138     }
00139 
00140   protected static void writeCredential(StringBuffer buffer, String meta, String data) {
00141       writeBeginTag(buffer, MessageFactory.TAG_CRED);
00142       if (meta != null)
00143           writeElement(buffer, MessageFactory.TAG_META, meta);
00144       writeElement(buffer, MessageFactory.TAG_DATA, data);
00145       writeEndTag(buffer, MessageFactory.TAG_CRED);
00146     }
00147 
00148 
00149   protected static void writeTarget(StringBuffer buffer, String target) {
00150       writeBeginTag(buffer, MessageFactory.TAG_TARGET);
00151       writeElement(buffer, MessageFactory.TAG_URI, target);
00152       writeEndTag(buffer, MessageFactory.TAG_TARGET);
00153     }
00154 
00155   protected static void writeSource(StringBuffer buffer, String source, long sessionID) {
00156       writeBeginTag(buffer, MessageFactory.TAG_SOURCE);
00157       writeElement(buffer, MessageFactory.TAG_URI, source);
00158       if (sessionID != -1)  {
00159         writeElement(buffer, MessageFactory.TAG_SESSION, Long.toString(sessionID));
00160       }
00161       writeEndTag(buffer, MessageFactory.TAG_SOURCE);
00162     }
00163 }

Generated on Wed Dec 14 21:05:34 2005 for OpenMobileIS by  doxygen 1.4.4