| 1 |
using System; |
|---|
| 2 |
using System.Xml; |
|---|
| 3 |
using net.sf.saxon.value; |
|---|
| 4 |
using Saxon.Api; |
|---|
| 5 |
using Sgml; |
|---|
| 6 |
using System.Web; |
|---|
| 7 |
using Nuxleus.Memcached; |
|---|
| 8 |
using Nuxleus.Transform; |
|---|
| 9 |
using System.IO; |
|---|
| 10 |
using System.Text; |
|---|
| 11 |
using System.Collections; |
|---|
| 12 |
using System.Collections.Generic; |
|---|
| 13 |
using Mvp.Xml; |
|---|
| 14 |
using System.Xml.XPath; |
|---|
| 15 |
using Memcached.ClientLibrary; |
|---|
| 16 |
|
|---|
| 17 |
namespace Xameleon.Function |
|---|
| 18 |
{ |
|---|
| 19 |
|
|---|
| 20 |
public class HttpSgmlToXml |
|---|
| 21 |
{ |
|---|
| 22 |
static IDictionary<String, String> _cacheDictionary = new Dictionary<String, String>(); |
|---|
| 23 |
|
|---|
| 24 |
public HttpSgmlToXml () { } |
|---|
| 25 |
|
|---|
| 26 |
public static String GetXmlFromHtmlString (String html) |
|---|
| 27 |
{ |
|---|
| 28 |
using (SgmlReader sr = new SgmlReader()) |
|---|
| 29 |
{ |
|---|
| 30 |
sr.InputStream = new StringReader(html); |
|---|
| 31 |
return sr.ReadOuterXml(); |
|---|
| 32 |
} |
|---|
| 33 |
} |
|---|
| 34 |
|
|---|
| 35 |
public static StringReader GetStreamFromHtmlString (String html) |
|---|
| 36 |
{ |
|---|
| 37 |
return new StringReader(html); |
|---|
| 38 |
} |
|---|
| 39 |
|
|---|
| 40 |
public static String DecodeHtmlString (String html) |
|---|
| 41 |
{ |
|---|
| 42 |
return HttpUtility.HtmlDecode(html); |
|---|
| 43 |
} |
|---|
| 44 |
|
|---|
| 45 |
//public static Value GetDocXml (String uri, HttpContext context) |
|---|
| 46 |
//{ |
|---|
| 47 |
// return getDocXml(uri, "/html", context); |
|---|
| 48 |
//} |
|---|
| 49 |
|
|---|
| 50 |
//public static Value GetDocXml (String uri, String path, HttpContext context) |
|---|
| 51 |
//{ |
|---|
| 52 |
// return getDocXml(uri, path, context); |
|---|
| 53 |
//} |
|---|
| 54 |
|
|---|
| 55 |
public static String GetDocXml (String uri, String path, bool stripNS, HttpContext context) |
|---|
| 56 |
{ |
|---|
| 57 |
try |
|---|
| 58 |
{ |
|---|
| 59 |
return getXmlReader(uri, path, context); |
|---|
| 60 |
} |
|---|
| 61 |
catch (Exception e) |
|---|
| 62 |
{ |
|---|
| 63 |
Console.WriteLine(e.Message); |
|---|
| 64 |
return "<stacktrace>" + e.StackTrace + "</stacktrace>"; |
|---|
| 65 |
} |
|---|
| 66 |
} |
|---|
| 67 |
|
|---|
| 68 |
//private static Value getDocXml (String uri, String path, HttpContext context) |
|---|
| 69 |
//{ |
|---|
| 70 |
// try |
|---|
| 71 |
// { |
|---|
| 72 |
// return Value.asValue(getXdmNode(uri, path, context).Unwrap()); |
|---|
| 73 |
// } |
|---|
| 74 |
// catch (Exception e) |
|---|
| 75 |
// { |
|---|
| 76 |
// throw; |
|---|
| 77 |
// } |
|---|
| 78 |
//} |
|---|
| 79 |
|
|---|
| 80 |
private static String getXmlReader (String uri, String path, HttpContext context) |
|---|
| 81 |
{ |
|---|
| 82 |
string decodedUri = HttpUtility.UrlDecode(uri); |
|---|
| 83 |
string eTag = Context.GenerateETag(decodedUri, Nuxleus.Cryptography.HashAlgorithm.SHA1); |
|---|
| 84 |
String xhtml; |
|---|
| 85 |
|
|---|
| 86 |
try |
|---|
| 87 |
{ |
|---|
| 88 |
|
|---|
| 89 |
if (_cacheDictionary.ContainsKey(eTag)) |
|---|
| 90 |
{ |
|---|
| 91 |
xhtml = _cacheDictionary[eTag]; |
|---|
| 92 |
} |
|---|
| 93 |
else |
|---|
| 94 |
{ |
|---|
| 95 |
using (SgmlReader sr = new SgmlReader()) |
|---|
| 96 |
{ |
|---|
| 97 |
try |
|---|
| 98 |
{ |
|---|
| 99 |
if ((bool)context.Application["usememcached"]) |
|---|
| 100 |
{ |
|---|
| 101 |
MemcachedClient m_client = (MemcachedClient)context.Application["memcached"]; |
|---|
| 102 |
|
|---|
| 103 |
if (m_client.KeyExists(eTag)) |
|---|
| 104 |
{ |
|---|
| 105 |
sr.InputStream = GetStreamFromHtmlString((string)m_client.Get(eTag)); |
|---|
| 106 |
} |
|---|
| 107 |
|
|---|
| 108 |
else |
|---|
| 109 |
{ |
|---|
| 110 |
sr.Href = decodedUri; |
|---|
| 111 |
m_client.Add(eTag, sr.ReadOuterXml(), DateTime.Now.AddMinutes(60)); |
|---|
| 112 |
} |
|---|
| 113 |
|
|---|
| 114 |
} |
|---|
| 115 |
else |
|---|
| 116 |
{ |
|---|
| 117 |
sr.Href = decodedUri; |
|---|
| 118 |
|
|---|
| 119 |
} |
|---|
| 120 |
|
|---|
| 121 |
} |
|---|
| 122 |
catch (Exception e) |
|---|
| 123 |
{ |
|---|
| 124 |
Console.WriteLine(e.Message); |
|---|
| 125 |
Console.WriteLine(e.StackTrace); |
|---|
| 126 |
} |
|---|
| 127 |
|
|---|
| 128 |
xhtml = sr.ReadOuterXml(); |
|---|
| 129 |
_cacheDictionary.Add(eTag, xhtml); |
|---|
| 130 |
|
|---|
| 131 |
} |
|---|
| 132 |
} |
|---|
| 133 |
|
|---|
| 134 |
return xhtml; |
|---|
| 135 |
} |
|---|
| 136 |
|
|---|
| 137 |
catch (Exception e) |
|---|
| 138 |
{ |
|---|
| 139 |
Console.WriteLine(e.Message); |
|---|
| 140 |
Console.WriteLine(e.StackTrace); |
|---|
| 141 |
|
|---|
| 142 |
} |
|---|
| 143 |
|
|---|
| 144 |
return String.Format(@"<html xmlns='http://www.w3.org/1999/xhtml'> |
|---|
| 145 |
<head> |
|---|
| 146 |
<title>No Readable HTML</title> |
|---|
| 147 |
</head> |
|---|
| 148 |
<body> |
|---|
| 149 |
<h1>No readable HTML was located at the specified URL</h1> |
|---|
| 150 |
</body> |
|---|
| 151 |
</html>"); |
|---|
| 152 |
} |
|---|
| 153 |
} |
|---|
| 154 |
} |
|---|