Skip to content

Commit

Permalink
Refactor: move XML namespaces to Utils
Browse files Browse the repository at this point in the history
  • Loading branch information
mausch committed Aug 7, 2015
1 parent 4d4dbe8 commit 051bee4
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 23 deletions.
5 changes: 2 additions & 3 deletions GDataDB/DatabaseClient.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@

namespace GDataDB {
public class DatabaseClient : IDatabaseClient {
public static readonly XNamespace AtomNs = "http://www.w3.org/2005/Atom";

public readonly GDataDBRequestFactory RequestFactory;

Expand Down Expand Up @@ -81,12 +80,12 @@ public IDatabase GetDatabase(string name) {
}

public static Uri ExtractEntryContent(XDocument xdoc) {
return ExtractEntryContent(xdoc.Root.Elements(AtomNs + "entry"));
return ExtractEntryContent(xdoc.Root.Elements(Utils.AtomNs + "entry"));
}

public static Uri ExtractEntryContent(IEnumerable<XElement> entries) {
var selectMany = entries
.SelectMany(e => e.Elements(AtomNs + "content"));
.SelectMany(e => e.Elements(Utils.AtomNs + "content"));
var xAttributes = selectMany
.SelectMany(e => e.Attributes("src"));
var enumerable = xAttributes
Expand Down
20 changes: 11 additions & 9 deletions GDataDB/Impl/Database.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,25 +13,27 @@ public Database(DatabaseClient client, string id, Uri worksheetFeed) {
this.id = id;
this.worksheetFeed = worksheetFeed;
}

private static readonly XNamespace spreadsheetsNs = "http://schemas.google.com/spreadsheets/2006";

public ITable<T> CreateTable<T>(string name) where T: new() {
var length = typeof(T).GetProperties().Length;
var http = client.RequestFactory.CreateRequest();

var request = new XDocument(
new XElement(DatabaseClient.AtomNs + "entry",
new XElement(DatabaseClient.AtomNs + "title", name),
new XElement(spreadsheetsNs + "rowCount", 1),
new XElement(spreadsheetsNs + "colCount", length)
new XElement(Utils.AtomNs + "entry",
new XElement(Utils.AtomNs + "title", name),
new XElement(Utils.SpreadsheetsNs + "rowCount", 1),
new XElement(Utils.SpreadsheetsNs + "colCount", length)
)
);
var response = http.UploadString(worksheetFeed.AbsoluteUri, request.ToString());
var xmlResponse = XDocument.Parse(response);

// i.e. https://spreadsheets.google.com/feeds/list/key/worksheetId/private/full
var listFeedUri = DatabaseClient.ExtractEntryContent(new[] {xmlResponse.Root});

// i.e. https://spreadsheets.google.com/feeds/worksheets/key/private/full/worksheetId/version
var editUri = xmlResponse.Root
.Elements(DatabaseClient.AtomNs + "link")
.Elements(Utils.AtomNs + "link")
.Where(e => e.Attribute("rel").Value == "edit")
.Select(e => new Uri(e.Attribute("href").Value))
.FirstOrDefault();
Expand All @@ -55,8 +57,8 @@ public Database(DatabaseClient client, string id, Uri worksheetFeed) {
return null;

var editUri = xmlResponse.Root
.Elements(DatabaseClient.AtomNs + "entry")
.SelectMany(e => e.Elements(DatabaseClient.AtomNs + "link"))
.Elements(Utils.AtomNs + "entry")
.SelectMany(e => e.Elements(Utils.AtomNs + "link"))
.Where(e => e.Attribute("rel").Value == "edit")
.Select(e => new Uri(e.Attribute("href").Value))
.FirstOrDefault();
Expand Down
20 changes: 11 additions & 9 deletions GDataDB/Impl/Serializer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,23 +17,25 @@ namespace GDataDB.Impl {
public static readonly XNamespace GdNs = "http://schemas.google.com/g/2005";

public XElement SerializeNewRow(T e) {
return new XElement(DatabaseClient.AtomNs + "entry",
return new XElement(Utils.AtomNs + "entry",
new XAttribute(XNamespace.Xmlns + "gsx", GsxNs),
//new XElement(DatabaseClient.AtomNs + "id", "123"),
SerializeFields(e).ToArray());
}

public IEnumerable<PropertyInfo> GetFields() {
return typeof(T).GetProperties().Where(p => p.CanRead);
}

public IEnumerable<XElement> SerializeFields(T e) {
return
from p in typeof(T).GetProperties()
where p.CanRead
select new XElement(GsxNs + p.Name.ToLowerInvariant(), ToNullOrString(p.GetValue(e, null)));
return GetFields()
.Select(p => new XElement(GsxNs + p.Name.ToLowerInvariant(), ToNullOrString(p.GetValue(e, null))));
}

public XElement Serialize(Row<T> row) {
var e = new XElement(DatabaseClient.AtomNs + "entry",
var e = new XElement(Utils.AtomNs + "entry",
new XAttribute(GdNs + "etag", row.Etag));
e.Add(new XElement(DatabaseClient.AtomNs + "id", row.Id.AbsoluteUri));
e.Add(new XElement(Utils.AtomNs + "id", row.Id.AbsoluteUri));
e.Add(SerializeFields(row.Element));
return e;
}
Expand Down Expand Up @@ -74,9 +76,9 @@ public T DeserializeElement(XElement entry) {

public IRow<T> DeserializeRow(XElement entry, DatabaseClient client) {
var etag = entry.Attribute(GdNs + "etag").Value;
var id = new Uri(entry.Element(DatabaseClient.AtomNs + "id").Value);
var id = new Uri(entry.Element(Utils.AtomNs + "id").Value);
var edit = entry
.Elements(DatabaseClient.AtomNs + "link")
.Elements(Utils.AtomNs + "link")
.Where(e => e.Attribute("rel").Value == "edit")
.Select(e => new Uri(e.Attribute("href").Value))
.FirstOrDefault();
Expand Down
4 changes: 2 additions & 2 deletions GDataDB/Impl/Table.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public void Rename(string newName) {
var http = client.RequestFactory.CreateRequest();
var response = http.DownloadString(worksheetUri);
var xmlResponse = XDocument.Parse(response);
var title = xmlResponse.Root.Element(DatabaseClient.AtomNs + "title");
var title = xmlResponse.Root.Element(Utils.AtomNs + "title");
if (title == null)
throw new Exception("Title was null in worksheet feed entry");
title.Value = newName;
Expand Down Expand Up @@ -128,7 +128,7 @@ public IList<IRow<T>> Find(Query q) {
};
var rawResponse = http.DownloadString(uriBuilder.Uri);
var xmlResponse = XDocument.Parse(rawResponse);
return xmlResponse.Root.Elements(DatabaseClient.AtomNs + "entry")
return xmlResponse.Root.Elements(Utils.AtomNs + "entry")
.Select(e => serializer.DeserializeRow(e, client))
.ToList();
}
Expand Down
6 changes: 6 additions & 0 deletions GDataDB/Impl/Utils.cs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
using System;
using System.Xml.Linq;

namespace GDataDB.Impl {
public static class Utils {
public static string UrlEncode(string s) {
return Uri.EscapeDataString(s).Replace("%20", "+");
}

public static readonly XNamespace SpreadsheetsNs = "http://schemas.google.com/spreadsheets/2006";
public static readonly XNamespace BatchNs = "http://schemas.google.com/gdata/batch";
public static readonly XNamespace AtomNs = "http://www.w3.org/2005/Atom";

}
}

0 comments on commit 051bee4

Please sign in to comment.