/// <summary> /// /// </summary> /// <param name="command"></param> /// <param name="currentNode">A C element under either New or Update element.</param> protected override void AddContentToCommand(SyncMLUpdateBase command, XElement currentNode) { if (command == null) { Trace.TraceWarning("Hey, you bad. Null command."); return; } if (currentNode == null) { Trace.TraceWarning("Hey, r u crazy? giving me null currentNode."); return; } if (useBase64) { SyncMLItem item = SyncMLItem.Create(); item.Source.LocURI.Content = currentNode.Attribute("ID").Value; item.Meta.Xml.Add(FormatOfBase64); item.Meta.Xml.Add(TypeOfText); UTF8Encoding byteConverter = new UTF8Encoding(); byte[] buffer = byteConverter.GetBytes(VCardWriter.WriteToString(VCardSIFC.ConvertSifCToVCard(currentNode.Element("contact")))); item.Data.Content = Convert.ToBase64String(buffer); command.ItemCollection.Add(item); } else { SyncMLItem item = SyncMLItem.Create(); item.Source.LocURI.Content = currentNode.Attribute("ID").Value; item.Meta.Xml.Add(xVcardType); item.Data.Content = VCardWriter.WriteToString(VCardSIFC.ConvertSifCToVCard(currentNode.Element("contact"))); command.ItemCollection.Add(item); } }
/// <summary> /// Get contact data as vCard. /// </summary> /// <param name="contactItem"></param> /// <returns></returns> protected override string GetItemData(ContactItem item) { return(VCardWriter.WriteToString( VCardSIFC.ConvertSifCToVCard(XElement.Parse( SifAgent.ReadItemToText(item))))); }
} // 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."); } }