public void TestMetaContent() { XElement nav = XElement.Load(CaseFile("MetaContent.xml")); SyncMLMeta meta = SyncMLSimpleElementFactory.Create <SyncMLMeta>(nav); Assert.IsTrue(CompareXml(meta.Xml, nav)); }
/// <summary> /// Called by ProcessResponseForXXX in order to create status for Alert returned by server. /// </summary> /// <param name="alert"></param> protected void PrepareStatusForReturnedAlert(SyncMLAlert alert) { switch (alert.Data.Content) { case "200": case "201": case "202": case "203": case "204": case "205": SyncMLStatus responseStatus = SyncMLStatus.Create(); //status.CmdID is not defined here, but only defined right before sending next message responseStatus.MsgRef.Content = ServerSyncML.Hdr.MsgID.Content; responseStatus.Data.Content = "200"; responseStatus.Cmd.Content = "Alert"; responseStatus.CmdRef.Content = alert.CmdID.Content; responseStatus.TargetRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLTargetRef>(Facade.ContactDataSourceAtServer)); responseStatus.SourceRefCollection.Add(SyncMLSimpleElementFactory.Create <SyncMLSourceRef>(Facade.LocalDataSource.DataSourceName)); //Get alert anchor next SyncMLItem alertItem = alert.ItemCollection[0]; //assuming there's always one. SyncMLMeta alertItemMeta = alertItem.Meta; // assuming there's always one. MetaParser metaParser = new MetaParser(alertItemMeta.Xml); MetaAnchor alertAnchor = metaParser.GetMetaAnchor(); string alertAnchorNext = alertAnchor.Next.Content; SyncMLItem statusItem = SyncMLItem.Create(); MetaAnchor statusAnchor = MetaAnchor.Create(); statusAnchor.Next = SyncMLSimpleElementFactory.Create <MetaNext>(alertAnchorNext); statusItem.Data.Xml.Add(statusAnchor.Xml); responseStatus.ItemCollection.Add(statusItem); Facade.ResponseCommandPool.Add(responseStatus); break; case "100": //Show. The Data element type contains content information that should be processed and displayed through the user agent. string serverStatusMessage = alert.ItemCollection[0].Data.Content; Facade.DisplayOperationMessage(serverStatusMessage); SyncMLStatus statusFor100 = SyncMLStatus.Create(); statusFor100.MsgRef.Content = ServerSyncML.Hdr.MsgID.Content; statusFor100.Data.Content = "200"; statusFor100.Cmd.Content = "Alert"; statusFor100.CmdRef.Content = alert.CmdID.Content; Facade.ResponseCommandPool.Add(statusFor100); break; default: Trace.TraceInformation("Do not know what to do in PrepareStatusForReturnedAlert:"); Trace.TraceInformation(alert.Xml.ToString()); break; } }
/// <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."); } }