Skip to content

Commit

Permalink
Inline XML helper methods to get rid of System.Web dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
mausch committed Jul 5, 2015
1 parent bf58c90 commit 36fd091
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 15 deletions.
9 changes: 4 additions & 5 deletions QuartzNetWebConsole.Views/QuartzNetWebConsole.Views.vbproj
Expand Up @@ -14,7 +14,7 @@
<AssemblyName>QuartzNetWebConsole.Views</AssemblyName>
<FileAlignment>512</FileAlignment>
<MyType>Empty</MyType>
<TargetFrameworkVersion>v3.5</TargetFrameworkVersion>
<TargetFrameworkVersion>v4.5</TargetFrameworkVersion>
<TargetFrameworkProfile />
<SolutionDir Condition="$(SolutionDir) == '' Or $(SolutionDir) == '*Undefined*'">..\</SolutionDir>
<RestorePackages>true</RestorePackages>
Expand All @@ -29,6 +29,7 @@
<NoWarn>
</NoWarn>
<WarningsAsErrors>41999,42016,42017,42018,42019,42020,42021,42022,42032,42036</WarningsAsErrors>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup Condition=" '$(Configuration)|$(Platform)' == 'Release|AnyCPU' ">
<DebugType>pdbonly</DebugType>
Expand All @@ -40,6 +41,7 @@
<NoWarn>
</NoWarn>
<WarningsAsErrors>41999,42016,42017,42018,42019,42020,42021,42022,42032,42036</WarningsAsErrors>
<Prefer32Bit>false</Prefer32Bit>
</PropertyGroup>
<PropertyGroup>
<OptionExplicit>On</OptionExplicit>
Expand All @@ -55,16 +57,12 @@
<Reference Include="Common.Logging.Core">
<HintPath>..\packages\Common.Logging.Core.3.0.0\lib\net35\Common.Logging.Core.dll</HintPath>
</Reference>
<Reference Include="MiniMVC, Version=1.0.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\MiniMVC.1.0\lib\net35\MiniMVC.dll</HintPath>
</Reference>
<Reference Include="Quartz, Version=2.3.2.0, Culture=neutral, PublicKeyToken=f6b8c98a402cc8a4, processorArchitecture=MSIL">
<SpecificVersion>False</SpecificVersion>
<HintPath>..\packages\Quartz.2.3.2\lib\net35\Quartz.dll</HintPath>
</Reference>
<Reference Include="System" />
<Reference Include="System.Data" />
<Reference Include="System.Web" />
<Reference Include="System.Xml" />
<Reference Include="System.Core" />
<Reference Include="System.Xml.Linq" />
Expand All @@ -89,6 +87,7 @@
<Compile Include="PaginationInfo.vb" />
<Compile Include="TriggersByJobModel.vb" />
<Compile Include="TriggerWithState.vb" />
<Compile Include="X.vb" />
</ItemGroup>
<ItemGroup>
<Folder Include="My Project\" />
Expand Down
12 changes: 6 additions & 6 deletions QuartzNetWebConsole.Views/Views.vb
@@ -1,6 +1,5 @@
Imports MiniMVC
Imports System.Net
Imports Quartz
Imports System.Web
Imports Quartz.Impl.Triggers

Public Module Views
Expand Down Expand Up @@ -234,7 +233,7 @@ Public Module Views
Public Function TriggerGroup(group As String, paused As Boolean?, thisUrl As String, highlight As String, triggers As IEnumerable(Of TriggerWithState)) As XElement
Dim schedulerOp = Function(method As String) "scheduler.ashx?method=" + method +
"&groupName=" + group +
"&next=" + HttpUtility.UrlEncode(thisUrl)
"&next=" + WebUtility.UrlEncode(thisUrl)
Return _
<html>
<head>
Expand All @@ -259,7 +258,7 @@ Public Module Views
Public Function JobGroup(group As String, paused As Boolean?, highlight As String, thisUrl As String, jobs As IEnumerable(Of JobWithContext)) As XElement
Dim schedulerOp = Function(method As String) "scheduler.ashx?method=" + method +
"&groupName=" + group +
"&next=" + HttpUtility.UrlEncode(thisUrl)
"&next=" + WebUtility.UrlEncode(thisUrl)
Return _
<html>
<head>
Expand Down Expand Up @@ -291,7 +290,7 @@ Public Module Views
Let op = Function(method As String) "scheduler.ashx?method=" + method +
"&jobName=" + j.Job.Key.Name +
"&groupName=" + j.Job.Key.Group +
"&next=" + HttpUtility.UrlEncode(thisUrl)
"&next=" + WebUtility.UrlEncode(thisUrl)
Select
<tr id=<%= j.Job.Key.ToString() %>
class=<%= If(highlight = j.Job.Key.ToString(), "highlight", "") %>>
Expand Down Expand Up @@ -366,7 +365,7 @@ Public Module Views
Let op = Function(method As String) "scheduler.ashx?method=" & method &
"&triggerName=" + trigger.Key.Name +
"&groupName=" + trigger.Key.Group +
"&next=" + HttpUtility.UrlEncode(thisUrl)
"&next=" + WebUtility.UrlEncode(thisUrl)
Select
<tr id=<%= trigger.Key.ToString() %>
class=<%= If(highlight = trigger.Key.ToString(), "highlight", "") %>>
Expand Down Expand Up @@ -403,4 +402,5 @@ Public Module Views
</tr> %>
</table>)
End Function

End Module
288 changes: 288 additions & 0 deletions QuartzNetWebConsole.Views/X.vb
@@ -0,0 +1,288 @@
Imports System
Imports System.Collections.Generic
Imports System.IO
Imports System.Linq
Imports System.Text
Imports System.Xml
Imports System.Xml.Linq

Public Module X
Public ReadOnly XHTML1_0_Transitional_Doctype As XDocumentType = New XDocumentType("html", "-//W3C//DTD XHTML 1.0 Transitional//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd", Nothing)

Public ReadOnly XHTML1_0_Strict_Doctype As XDocumentType = New XDocumentType("html", "-//W3C//DTD XHTML 1.0 Strict//EN", "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd", Nothing)

Public ReadOnly HTML5_Doctype As XDocumentType = New XDocumentType("html", Nothing, Nothing, Nothing)

Public ReadOnly nbsp As String = "\u00A0"

Public ReadOnly raquo As String = "»"

Public ReadOnly laquo As String = "«"

Public ReadOnly rsaquo As String = ChrW(8250)

Public ReadOnly lsaquo As String = ChrW(8249)

Public ReadOnly copy As String = "©"

Public ReadOnly amp As String = "&"

Public ReadOnly lt As String = "<"

Public ReadOnly gt As String = ">"

Public ReadOnly XHTML_Namespace As XNamespace = XNamespace.[Get]("http://www.w3.org/1999/xhtml")

Private ReadOnly emptyElems As HashSet(Of String) = New HashSet(Of String)() From {"area", "base", "basefont", "br", "col", "command", "frame", "hr", "img", "input", "isindex", "keygen", "link", "meta", "param", "source", "track", "wbr"}

Public ReadOnly NoElements As IEnumerable(Of XElement) = Enumerable.Empty(Of XElement)()

Public ReadOnly NoNodes As IEnumerable(Of XNode) = Enumerable.Empty(Of XNode)()

Public Function A(name As String, value As String) As XAttribute
Return New XAttribute(XName.[Get](name), value)
End Function

Public Function E(name As String, ParamArray content As Object()) As XElement
Return New XElement(XName.[Get](name), content)
End Function

Public Function T(text As String) As XNode
Return New XText(text)
End Function

Public Function Raw(xml As String) As XNode()
Return XDocument.Parse("<x>" + xml + "</x>", LoadOptions.PreserveWhitespace).Document.Root.Nodes().ToArray
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function Alter(e As XElement, pred As Func(Of Boolean), a As Action(Of XElement)) As XElement
If pred() Then
a(e)
End If
Return e
End Function

Public Function IsEmptyElement(elementName As String) As Boolean
Return emptyElems.Contains(elementName)
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function IsEmptyElement(element As XElement) As Boolean
Return X.IsEmptyElement(element.Name.LocalName)
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function FixEmptyElements(n As XNode) As XNode
Dim e = TryCast(n, XElement)
If e Is Nothing Then
Return n
End If
Dim isEmptyElem = e.IsEmptyElement()
If isEmptyElem AndAlso Not e.IsEmpty Then
Return New XElement(e.Name, e.Attributes())
End If
Dim children = e.Nodes().Select(AddressOf FixEmptyElements)
If Not isEmptyElem AndAlso e.IsEmpty Then
Return New XElement(e.Name, {e.Attributes(), New XText(""), children})
End If
Return New XElement(e.Name, {e.Attributes(), children})
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function ApplyNamespace(n As XNode, ns As XNamespace) As XNode
Dim e = TryCast(n, XElement)
If e IsNot Nothing Then
Dim arg_58_0 = ns + e.Name.LocalName
Dim children = e.Nodes().Select(Function(x As XNode) x.ApplyNamespace(ns))
Return New XElement(arg_58_0, {e.Attributes(), children})
End If
Return n
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function MakeHTMLCompatible(n As XNode) As XNode
Return n.ApplyNamespace(X.XHTML_Namespace).FixEmptyElements()
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function MakeHTML5Doc(root As XElement) As XDocument
Return New XDocument({X.HTML5_Doctype, root.MakeHTMLCompatible()})
End Function

Public Function CreateXmlWriter(output As Stream) As XmlWriter
Dim settings = New XmlWriterSettings() With {.OmitXmlDeclaration = True, .ConformanceLevel = ConformanceLevel.Fragment, .NewLineHandling = NewLineHandling.None, .Encoding = New UTF8Encoding(False)}
Return XmlWriter.Create(output, settings)
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Sub WriteToStream(n As XNode, output As Stream)
If n Is Nothing Then
Return
End If
Using xmlwriter = X.CreateXmlWriter(output)
n.FixEmptyElements().WriteTo(xmlwriter)
End Using
End Sub

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Sub WriteToStream(nodes As IEnumerable(Of XNode), output As Stream)
If nodes Is Nothing Then
Return
End If
Dim root = TryCast(<x><%= nodes %></x>.FixEmptyElements(), XElement)
Using xmlwriter = X.CreateXmlWriter(output)
Using enumerator = root.Nodes().GetEnumerator()
While enumerator.MoveNext()
enumerator.Current.WriteTo(xmlwriter)
End While
End Using
End Using
End Sub

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function WriteToString(nodes As IEnumerable(Of XNode)) As String
If nodes Is Nothing Then
Return ""
End If
Dim [string] As String
Using ms = New MemoryStream()
nodes.WriteToStream(ms)
[string] = Encoding.UTF8.GetString(ms.ToArray())
End Using
Return [string]
End Function

' <System.Runtime.CompilerServices.ExtensionAttribute()>
' Public Sub WriteToResponse(nodes As IEnumerable(Of XNode))
' Dim ctx As HttpContext = HttpContext.Current
' If ctx Is Nothing Then
' Throw New Exception("No current HttpContext")
' End If
' nodes.WriteToStream(ctx.Response.OutputStream)
' End Sub

' <System.Runtime.CompilerServices.ExtensionAttribute()>
' Public Sub WriteToResponse(elements As IEnumerable(Of XElement))
' Dim arg_1C_1 As Func(Of XElement, XNode)
'If(arg_1C_1 = X.CS$<>9__CachedAnonymousMethodDelegate4) Is Nothing Then
' arg_1C_1 = (X.CS$<>9__CachedAnonymousMethodDelegate4 = (Function(x As XElement) x))
' End If
' elements.[Select](arg_1C_1).WriteToResponse()
' End Sub

Public Function IsNullOrWhiteSpace(value As String) As Boolean
If value IsNot Nothing Then
For i As Integer = 0 To value.Length - 1
If Not Char.IsWhiteSpace(value(i)) Then
Return False
End If
Next
End If
Return True
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function IsWhiteSpace(n As XNode) As Boolean
Dim t As XText = TryCast(n, XText)
Return t IsNot Nothing AndAlso X.IsNullOrWhiteSpace(t.Value)
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function Trim(nodes As IEnumerable(Of XNode)) As IEnumerable(Of XNode)
Return nodes.SkipWhile(AddressOf IsWhiteSpace).Reverse().SkipWhile(AddressOf IsWhiteSpace).Reverse()
End Function

Public Function SpacesToNbsp(s As String) As String
If s Is Nothing Then
Return Nothing
End If
Return s.Replace(" ", " ")
End Function

Public Function Javascript(content As String) As XElement
Dim cdata = New XCData("*/" + content + "/*")
Dim begin = New XText("/*")
Dim [end] = New XText("*/")
Return X.E("script", {X.A("type", "text/javascript"), begin, cdata, [end]})
End Function

Public Function Javascript(content As XCData) As XElement
Return X.Javascript(content.Value)
End Function

Public Function SelectOption(options As IEnumerable(Of XElement), value As String) As IEnumerable(Of XElement)
Return options.Select(Function(e As XElement)
Dim valueAtt = e.Attribute("value")
If valueAtt Is Nothing Then
Return e
End If
If valueAtt.Value <> value Then
Return e
End If
Dim expr_31 = New XElement(e)
expr_31.Add(X.A("selected", "selected"))
Return expr_31
End Function)
End Function

Public Function UnselectOption([option] As XElement) As XElement
Return [option].RemoveAttr("selected")
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function RemoveChildNodes(element As XElement) As XElement
Dim expr_06 = New XElement(element)
expr_06.RemoveNodes()
Return expr_06
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function RemoveAttr(element As XElement) As XElement
Dim expr_06 = New XElement(element)
expr_06.RemoveAttributes()
Return expr_06
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function RemoveAttr(element As XElement, attribute As String) As XElement
Dim arg_30_0 = element.RemoveAttr()
Dim attr = element.Attributes().Where(Function(a As XAttribute) a.Name.LocalName <> attribute).ToArray()
arg_30_0.Add(attr)
Return arg_30_0
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function AttributeValue(element As XElement, attr As String) As String
Dim a As XAttribute = element.Attribute(attr)
If a Is Nothing Then
Return Nothing
End If
Return a.Value
End Function

<System.Runtime.CompilerServices.ExtensionAttribute()>
Public Function Match(Of T)(node As XNode, cdata As Func(Of XCData, T), comment As Func(Of XComment, T), text As Func(Of XText, T), instruction As Func(Of XProcessingInstruction, T), element As Func(Of XElement, T)) As T
Dim ncdata = TryCast(node, XCData)
If ncdata IsNot Nothing Then
Return cdata(ncdata)
End If
Dim ncomment = TryCast(node, XComment)
If ncomment IsNot Nothing Then
Return comment(ncomment)
End If
Dim ntext = TryCast(node, XText)
If ntext IsNot Nothing Then
Return text(ntext)
End If
Dim ninstruction = TryCast(node, XProcessingInstruction)
If ninstruction IsNot Nothing Then
Return instruction(ninstruction)
End If
Dim nelement = TryCast(node, XElement)
If nelement IsNot Nothing Then
Return element(nelement)
End If
Throw New Exception("Unknown node type " + node.[GetType]().ToString())
End Function
End Module

0 comments on commit 36fd091

Please sign in to comment.