/// <summary> /// /// </summary> /// <param name="command"></param> /// <returns>localId=serverId pair</returns> private string ApplyAddOrReplaceCommand(SyncMLUpdateBase command) { if (command == null) { return(null); } bool isBase64 = false; bool isAddCommand = false; SyncMLMeta commandMeta = command.Meta; SyncMLItem commandItem = command.ItemCollection[0]; //assuming there is always one item. if (String.IsNullOrEmpty(commandMeta.Content) && (!commandMeta.Xml.HasElements)) { commandMeta = commandItem.Meta; } MetaParser metaParser = new MetaParser(commandMeta.Xml); MetaFormat metaFormat = metaParser.GetMetaFormat(); if (metaFormat != null) { isBase64 = metaFormat.Content == "b64"; } MetaType metaType = metaParser.GetMetaType(); if (metaType == null) { return(null); //the meta element may be empty, so no need to proceed. } // if (contentType == ContactExchangeType.Unknown) //todo: add some exception throw new LocalDataSourceException("expected data is Base64 encoded SIF-C or vCard"); isAddCommand = command is SyncMLAdd; //Assuming there will be only one item. string serverId; string localId = null; serverId = commandItem.Source.LocURI.Content; //id of remote one if (!isAddCommand) { localId = commandItem.Target.LocURI.Content; // entryId of existing contact } string text = GetTextFromContent(isBase64, metaType.Content, commandItem.Data.Content); if (text != null) { localId = isAddCommand ? sifAgent.AddItem(text) : sifAgent.ReplaceItem(text, localId); return(isAddCommand ? localId + "=" + serverId : null); } else { return(null); } }
} // using xmlWriter /// <summary> /// /// </summary> /// <param name="topElement">the 'Changes' element, to be manipulated</param> /// <param name="command"></param> private static void WriteAddOrReplaceCommandToXml(XElement topElement, SyncMLUpdateBase command) { if (command == null) { return; } bool isBase64 = false; ContactExchangeType contentType = ContactExchangeType.Unknown; bool isAddCommand = false; string commandTypeStr; SyncMLMeta commandMeta = command.Meta; SyncMLItem commandItem = command.ItemCollection[0]; //assuming there is always one item. if (String.IsNullOrEmpty(commandMeta.Content) && (!commandMeta.Xml.HasElements)) { commandMeta = commandItem.Meta; } MetaParser metaParser = new MetaParser(commandMeta.Xml); MetaFormat metaFormat = metaParser.GetMetaFormat(); if (metaFormat != null) { isBase64 = metaFormat.Content == "b64"; } MetaType metaType = metaParser.GetMetaType(); if (metaType == null) { return; //the meta element may be empty, so no need to proceed. } if (metaType.Content == "text/x-s4j-sifc") { contentType = ContactExchangeType.Sifc; } else if ((metaType.Content == "text/x-vcard") || (metaType.Content == "text/vcard")) { contentType = ContactExchangeType.Vcard21; } if (contentType == ContactExchangeType.Unknown) { throw new LocalDataSourceException("expected data is Base64 encoded SIF-C or vCard"); } isAddCommand = command is SyncMLAdd; commandTypeStr = isAddCommand ? "New" : "Update"; //Assuming there will be only one item. string id; if (isAddCommand) { id = commandItem.Source.LocURI.Content; } else { id = commandItem.Target.LocURI.Content; } XElement cItem = new XElement("C", new XAttribute("ID", id)); XElement changItem = new XElement(commandTypeStr, cItem); topElement.Add(changItem); switch (contentType) { case ContactExchangeType.Sifc: // do not need to check isBase64, because it must be cItem.Add(XElement.Parse(Utility.ConvertFromBase64(commandItem.Data.Content))); break; case ContactExchangeType.Vcard21: XElement sifcXml; if (isBase64) { sifcXml = VCardSIFC.ConvertVCardToSifCXml(VCardReader.ParseText( Utility.ConvertFromBase64(commandItem.Data.Content))); } else { sifcXml = VCardSIFC.ConvertVCardToSifCXml(VCardReader.ParseText(commandItem.Data.Content)); } cItem.Add(sifcXml); break; default: throw new LocalDataSourceException("Can not create stream from command Data."); } }