本文整理汇总了C#中XDoc.Clone方法的典型用法代码示例。如果您正苦于以下问题:C# XDoc.Clone方法的具体用法?C# XDoc.Clone怎么用?C# XDoc.Clone使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类XDoc
的用法示例。
在下文中一共展示了XDoc.Clone方法的7个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: DreamMessage
/// <summary>
/// Create a new message.
/// </summary>
/// <param name="status">Http status.</param>
/// <param name="headers">Header collection.</param>
/// <param name="contentType">Content Mime-Type.</param>
/// <param name="doc">Message body.</param>
public DreamMessage(DreamStatus status, DreamHeaders headers, MimeType contentType, XDoc doc) {
if(doc == null) {
throw new ArgumentNullException("doc");
}
this.Status = status;
this.Headers = new DreamHeaders(headers);
// check if document is empty
if(doc.IsEmpty) {
// we store empty XML documents as text content; it causes less confusion for browsers
this.Headers.ContentType = MimeType.TEXT;
this.Headers.ContentLength = 0L;
_doc = doc;
_bytes = new byte[0];
} else {
this.Headers.ContentType = contentType ?? MimeType.XML;
_doc = doc.Clone();
}
}
示例2: AddText
/// <summary>
/// Add text to the document.
/// </summary>
/// <param name="tag">Enclosing tag for the text.</param>
/// <param name="mime">Mime type of the enclosed text.</param>
/// <param name="xml">The body document to add.</param>
/// <returns>Returns the current document instance.</returns>
public XAtomBase AddText(string tag, MimeType mime, XDoc xml)
{
if(mime.Match(MimeType.XHTML)) {
Start(tag).Attr("type", "xhtml");
// add content and normalize the root node
XDoc added = xml.Clone().Rename("div");
if(added["@xmlns"].IsEmpty) {
added.Attr("xmlns", "http://www.w3.org/1999/xhtml");
}
Add(added);
} else if(mime.Match(MimeType.HTML)) {
Start(tag).Attr("type", "html");
// embed HTML as text
Value(xml.ToInnerXHtml());
} else {
Start(tag).Attr("type", mime.FullType);
Add(xml);
}
// close element
End();
return this;
}
示例3: Adding_same_document_after_first_dispatch_fires_after_normal_delay
public void Adding_same_document_after_first_dispatch_fires_after_normal_delay() {
XDoc m1 = new XDoc("deki-event")
.Attr("wikiid", "abc")
.Elem("channel", "event://abc/deki/pages/create")
.Elem("uri", "http://foo/baz")
.Elem("content.uri", "")
.Elem("revision.uri", "")
.Elem("path", "baz")
.Elem("previous-path", "bar");
DateTime queueTime1 = DateTime.Now;
_queue.Enqueue(m1);
Assert.AreEqual(0, _dispatcher.Dispatches.Count);
Assert.IsTrue(_dispatcher.ResetEvent.WaitOne(5000, true), "first callback didn't happen");
_dispatcher.ResetEvent.Reset();
Assert.AreEqual(1, _dispatcher.Dispatches.Count);
_queue.Enqueue(m1.Clone());
Assert.AreEqual(1, _dispatcher.Dispatches.Count);
Assert.IsTrue(_dispatcher.ResetEvent.WaitOne(5000, true), "second callback didn't happen");
Assert.AreEqual(2, _dispatcher.Dispatches.Count);
Assert.AreEqual(0, _peekQueue.Count);
}
示例4: Start
//--- Methods ---
protected override Yield Start(XDoc config, Result result) {
yield return Coroutine.Invoke(base.Start, config, new Result());
// set up plug for phpscript that will handle the notifications
_emailer = Plug.New(config["uri.emailer"].AsUri);
// set up plug deki, so we can validate users
_deki = Plug.New(config["uri.deki"].AsUri);
// get the apikey, which we will need as a subscription auth token for subscriptions not done on behalf of a user
_apikey = config["apikey"].AsText;
_cache = new PageChangeCache(_deki.With("apikey", _apikey), TimeSpan.FromSeconds(config["page-cache-ttl"].AsInt ?? 2));
// resource manager for email template
string resourcePath = Config["resources-path"].AsText;
if(!string.IsNullOrEmpty(resourcePath)) {
_resourceManager = new PlainTextResourceManager(Environment.ExpandEnvironmentVariables(resourcePath));
} else {
// creating a test resource manager
_log.WarnFormat("'resource-path' was not defined in Config, using a test resource manager for email templating");
TestResourceSet testSet = new TestResourceSet();
testSet.Add("Notification.Page.email-subject", "Page Modified");
testSet.Add("Notification.Page.email-header", "The following pages have changed:");
_resourceManager = new PlainTextResourceManager(testSet);
}
// get persisted subscription storage
List<Tuplet<string, List<XDoc>>> allWikiSubs = new List<Tuplet<string, List<XDoc>>>();
Result<DreamMessage> storageCatalog;
yield return storageCatalog = Storage.At("subscriptions").GetAsync();
foreach(XDoc wikiSubs in storageCatalog.Value.ToDocument()["folder/name"]) {
string wikihost = wikiSubs.AsText;
Tuplet<string, List<XDoc>> wikiDoc = new Tuplet<string, List<XDoc>>(wikihost, new List<XDoc>());
allWikiSubs.Add(wikiDoc);
Result<DreamMessage> wikiUsers;
yield return wikiUsers = Storage.At("subscriptions", wikihost).GetAsync();
foreach(XDoc userDocname in wikiUsers.Value.ToDocument()["file/name"]) {
string userFile = userDocname.AsText;
if(!userFile.EndsWith(".xml")) {
_log.WarnFormat("Found stray file '{0}' in wiki '{1}' store, ignoring", userFile, wikihost);
continue;
}
Result<DreamMessage> userDoc;
yield return userDoc = Storage.At("subscriptions", wikihost, userFile).GetAsync();
try {
wikiDoc.Item2.Add(userDoc.Value.ToDocument());
} catch(InvalidDataException e) {
_log.Error(string.Format("Unable to retrieve subscription store for user {0}/{1}", wikihost, userFile), e);
}
}
}
_subscriptions = new SubscriptionManager(Self.Uri.AsServerUri().At("notify"), allWikiSubs);
_subscriptions.RecordsChanged += PersistSubscriptions;
_subscriptions.SubscriptionsChanged += PushSubscriptionSetUpstream;
// set up subscription for pubsub
_baseSubscriptionSet = new XDoc("subscription-set")
.Elem("uri.owner", Self.Uri.AsServerUri().ToString())
.Start("subscription")
.Elem("channel", "event://*/deki/users/*")
.Add(DreamCookie.NewSetCookie("service-key", InternalAccessKey, Self.Uri).AsSetCookieDocument)
.Start("recipient")
.Attr("authtoken", _apikey)
.Elem("uri", Self.Uri.AsServerUri().At("updateuser").ToString())
.End()
.End();
XDoc subSet = _baseSubscriptionSet.Clone();
foreach(XDoc sub in _subscriptions.Subscriptions) {
subSet.Add(sub);
}
Result<DreamMessage> subscribe;
yield return subscribe = PubSub.At("subscribers").PostAsync(subSet);
string accessKey = subscribe.Value.ToDocument()["access-key"].AsText;
XUri location = subscribe.Value.Headers.Location;
Cookies.Update(DreamCookie.NewSetCookie("access-key", accessKey, location), null);
_subscriptionLocation = Plug.New(location.AsLocalUri().WithoutQuery());
_log.DebugFormat("set up initial subscription location at {0}", _subscriptionLocation.Uri);
// set up notification accumulator queue
TimeSpan accumulationMinutes = TimeSpan.FromSeconds(config["accumulation-time"].AsInt ?? 10 * 60);
_log.DebugFormat("Initializing queue with {0:0.00} minute accumulation", accumulationMinutes.TotalMinutes);
_notificationQueue = new NotificationDelayQueue(accumulationMinutes, SendEmail);
result.Return();
}
示例5: CleanseHtmlDocument
public static XDoc CleanseHtmlDocument(XDoc html) {
if(html.HasName("html")) {
html = html.Clone();
// remove <head> and <tail> elements
html["head"].RemoveAll();
html["tail"].RemoveAll();
// make sure there is only one body and validate it
var mainBody = html["body[not(@target)]"];
if(mainBody.IsEmpty) {
html.Elem("body");
mainBody = html["body[not(@target)]"];
}
foreach(XDoc body in html["body[@target]"]) {
body.Remove();
}
ValidateXHtml(mainBody, true, true);
}
return html;
}
示例6: Put
//--- Methods ---
/// <summary>
/// Store a document in the collection.
/// </summary>
/// <param name="doc">Document to store.</param>
/// <param name="force"><see langword="True"/> if the write should proceed even if optimistic locking meta-data indicates the document is older than the document already stored.</param>
/// <returns><see langword="True"/> if the action completed successfully.</returns>
public bool Put(XDoc doc, bool force)
{
Map(doc);
string docid = doc[_idXPath].AsText;
string revisionClause = string.Empty;
XDoc revisionAttr = doc["@docstore:revision"];
if(!revisionAttr.IsEmpty) {
if(!force) {
int? rev = revisionAttr.AsInt;
if(rev.HasValue) {
int pk = doc["@docstore:id"].AsInt ?? 0;
revisionClause = string.Format("AND id = {0} AND revision = {1}", pk, rev.Value);
}
}
// if we have docstore specific tags, we need to remove them before storage, but don't want to alter the doc
// that was given to us
doc = doc.Clone();
Map(doc);
doc["@docstore:revision"].Remove();
doc["@docstore:id"].Remove();
}
if(string.IsNullOrEmpty(docid)) {
throw new ArgumentException(string.Format("Document does not contain a valid value at '{0}'", _idXPath));
}
int rowsAffected = 0;
int id = 0;
int revision = 0;
// try update first, check for rows affected
_catalog.NewQuery(string.Format(@"
UPDATE {0} SET id = (@id := id), doc = ?DOC, revision = (@revision := revision + 1) WHERE doc_id = ?DOCID {1};
SELECT ROW_COUNT(), @id, @revision;", _name, revisionClause))
.With("DOCID", docid)
.With("DOC", doc.ToString())
.Execute(delegate(IDataReader reader) {
while(reader.Read()) {
rowsAffected = reader.GetInt32(0);
if(rowsAffected == 0) {
continue;
}
// Note (arnec): have to fetch as string and convert to int, because @variables in mysql
// are already returned as byte arrays representing strings
id = Convert.ToInt32(reader.GetString(1));
revision = Convert.ToInt32(reader.GetString(2));
}
});
bool wroteData = (rowsAffected > 0);
// if there was a revisionClause it's always an update, so we can skip the next block
if(string.IsNullOrEmpty(revisionClause) && rowsAffected == 0) {
// no row updated, try insert
try {
id = _catalog.NewQuery(string.Format(@"
INSERT INTO {0} (doc_id,doc) VALUES (?DOCID,?VALUE);
SELECT last_insert_id();", _name))
.With("DOCID", docid)
.With("VALUE", doc.ToString()).ReadAsInt() ?? 0;
revision = 1;
} catch(Exception e) {
// Note: need to do this by reflection magic, because Dream doesn't take DB dependencies at the dll level
bool isDuplicate = false;
if(StringUtil.EqualsInvariant(e.GetType().ToString(), "MySql.Data.MySqlClient.MySqlException")) {
try {
int errorNumber = (int)e.GetType().GetProperty("Number").GetValue(e, null);
// trap for duplicate key collisions
if(errorNumber == 1062) {
isDuplicate = true;
}
} catch { }
if(!isDuplicate) {
throw;
}
}
}
if(id == 0) {
// insert failed, try update once more
_catalog.NewQuery(string.Format(@"
UPDATE {0} SET id = (@id := id), doc = ?DOC, revision = (@revision := revision + 1) WHERE doc_id = ?DOCID;
SELECT @id, @revision;", _name))
.With("DOCID", docid)
.With("DOC", doc.ToString())
.Execute(delegate(IDataReader reader) {
while(reader.Read()) {
// Note (arnec): have to fetch as string and convert to int, because @variables in mysql
// are already returned as byte arrays representing strings
id = Convert.ToInt32(reader.GetString(0));
//.........这里部分代码省略.........
示例7: TruncateTocDepth
internal static XDoc TruncateTocDepth(XDoc toc, int? depth) {
// check if we need to limit the depth
if(depth != null) {
toc = toc.Clone();
string xpath = "." + "/ol/li".RepeatPattern(Math.Max(0, depth.Value)) + "/ol";
toc[xpath].RemoveAll();
}
return toc;
}