2015-08-26 07:17:56 -04:00
// XslCompiledTransform_Mobile.cs
2014-08-13 10:39:27 +01:00
//
// Author:
2015-08-26 07:17:56 -04:00
// Atsushi Enomoto <atsushi@xamarin.com>
2014-08-13 10:39:27 +01:00
//
2015-08-26 07:17:56 -04:00
// Copyright (C) 2015 Xamarin Inc. http://www.xamarin.com
2014-08-13 10:39:27 +01:00
//
//
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
using System ;
using System.CodeDom.Compiler ;
using System.Collections ;
using System.IO ;
using System.Text ;
2016-11-10 13:04:39 +00:00
using System.Reflection ;
2014-08-13 10:39:27 +01:00
using System.Runtime.InteropServices ;
using System.Security ;
using System.Security.Policy ;
using System.Xml.XPath ;
2015-08-26 07:17:56 -04:00
using System.Xml.XmlConfiguration ;
2014-08-13 10:39:27 +01:00
namespace System.Xml.Xsl
{
public sealed class XslCompiledTransform
{
2015-08-26 07:17:56 -04:00
readonly bool enable_debug ;
2014-08-13 10:39:27 +01:00
object debugger ;
XmlWriterSettings output_settings = new XmlWriterSettings ( ) ;
2015-08-26 07:17:56 -04:00
XslTransform impl = new XslTransform ( ) ;
2014-08-13 10:39:27 +01:00
public XslCompiledTransform ( )
: this ( false )
{
}
public XslCompiledTransform ( bool enableDebug )
{
enable_debug = enableDebug ;
if ( enable_debug )
debugger = new NoOperationDebugger ( ) ;
output_settings . ConformanceLevel = ConformanceLevel . Fragment ;
}
[MonoTODO]
public XmlWriterSettings OutputSettings {
get { return output_settings ; }
}
#region Transform
public void Transform ( string inputUri , string resultsFile )
{
using ( Stream outStream = File . Create ( resultsFile ) ) {
Transform ( new XPathDocument ( inputUri , XmlSpace . Preserve ) , null , outStream ) ;
}
}
public void Transform ( string inputUri , XmlWriter results )
{
Transform ( inputUri , null , results ) ;
}
public void Transform ( string inputUri , XsltArgumentList arguments , Stream results )
{
Transform ( new XPathDocument ( inputUri , XmlSpace . Preserve ) , arguments , results ) ;
}
public void Transform ( string inputUri , XsltArgumentList arguments , TextWriter results )
{
Transform ( new XPathDocument ( inputUri , XmlSpace . Preserve ) , arguments , results ) ;
}
public void Transform ( string inputUri , XsltArgumentList arguments , XmlWriter results )
{
Transform ( new XPathDocument ( inputUri , XmlSpace . Preserve ) , arguments , results ) ;
}
public void Transform ( XmlReader input , XmlWriter results )
{
Transform ( input , null , results ) ;
}
public void Transform ( XmlReader input , XsltArgumentList arguments , Stream results )
{
Transform ( new XPathDocument ( input , XmlSpace . Preserve ) , arguments , results ) ;
}
public void Transform ( XmlReader input , XsltArgumentList arguments , TextWriter results )
{
Transform ( new XPathDocument ( input , XmlSpace . Preserve ) , arguments , results ) ;
}
public void Transform ( XmlReader input , XsltArgumentList arguments , XmlWriter results )
{
Transform ( input , arguments , results , null ) ;
}
public void Transform ( IXPathNavigable input , XsltArgumentList arguments , TextWriter results )
{
Transform ( input . CreateNavigator ( ) , arguments , results ) ;
}
public void Transform ( IXPathNavigable input , XsltArgumentList arguments , Stream results )
{
2016-01-18 21:29:19 -05:00
using ( var sw = new StreamWriter ( results ) ) {
Transform ( input . CreateNavigator ( ) , arguments , sw ) ;
}
2014-08-13 10:39:27 +01:00
}
public void Transform ( IXPathNavigable input , XmlWriter results )
{
Transform ( input , null , results ) ;
}
public void Transform ( IXPathNavigable input , XsltArgumentList arguments , XmlWriter results )
{
Transform ( input . CreateNavigator ( ) , arguments , results , null ) ;
}
2016-11-10 13:04:39 +00:00
public void Transform ( IXPathNavigable input , XsltArgumentList arguments , XmlWriter results , XmlResolver documentResolver )
{
Transform ( input . CreateNavigator ( ) , arguments , results , documentResolver ) ;
}
2014-08-13 10:39:27 +01:00
public void Transform ( XmlReader input , XsltArgumentList arguments , XmlWriter results , XmlResolver documentResolver )
{
Transform ( new XPathDocument ( input , XmlSpace . Preserve ) . CreateNavigator ( ) , arguments , results , documentResolver ) ;
}
void Transform ( XPathNavigator input , XsltArgumentList args , XmlWriter output , XmlResolver resolver )
{
2015-08-26 07:17:56 -04:00
impl . Transform ( input , args , output , resolver ) ;
2014-08-13 10:39:27 +01:00
}
void Transform ( XPathNavigator input , XsltArgumentList args , TextWriter output )
{
2015-08-26 07:17:56 -04:00
impl . Transform ( input , args , output ) ;
2014-08-13 10:39:27 +01:00
}
#endregion
#region Load
private XmlReader GetXmlReader ( string url )
{
XmlResolver res = new XmlUrlResolver ( ) ;
Uri uri = res . ResolveUri ( null , url ) ;
Stream s = res . GetEntity ( uri , null , typeof ( Stream ) ) as Stream ;
XmlTextReader xtr = new XmlTextReader ( uri . ToString ( ) , s ) ;
xtr . XmlResolver = res ;
XmlValidatingReader xvr = new XmlValidatingReader ( xtr ) ;
xvr . XmlResolver = res ;
xvr . ValidationType = ValidationType . None ;
return xvr ;
}
public void Load ( string stylesheetUri )
{
2015-08-26 07:17:56 -04:00
using ( XmlReader r = GetXmlReader ( stylesheetUri ) )
2014-08-13 10:39:27 +01:00
Load ( r ) ;
}
public void Load ( XmlReader stylesheet )
{
2015-08-26 07:17:56 -04:00
Load ( stylesheet , XsltSettings . Default , XsltConfigSection . CreateDefaultResolver ( ) ) ;
2014-08-13 10:39:27 +01:00
}
public void Load ( IXPathNavigable stylesheet )
{
2015-08-26 07:17:56 -04:00
Load ( stylesheet . CreateNavigator ( ) , XsltSettings . Default , XsltConfigSection . CreateDefaultResolver ( ) ) ;
2014-08-13 10:39:27 +01:00
}
public void Load ( IXPathNavigable stylesheet , XsltSettings settings , XmlResolver stylesheetResolver )
{
2015-08-26 07:17:56 -04:00
// if (!settings.EnableDocumentFunction)
// throw new NotSupportedException ("'document' function cannot be disabled on this framework because it just runs XslTransform which does not support XsltSettings");
if ( settings . EnableScript )
throw new NotSupportedException ( "'msxsl:script' element is not supported on this framework because it does not support run-time code generation" ) ;
impl . Load ( stylesheet , stylesheetResolver ) ;
2014-08-13 10:39:27 +01:00
}
public void Load ( XmlReader stylesheet , XsltSettings settings , XmlResolver stylesheetResolver )
{
2015-08-26 07:17:56 -04:00
Load ( new XPathDocument ( stylesheet , XmlSpace . Preserve ) , settings , stylesheetResolver ) ;
2014-08-13 10:39:27 +01:00
}
public void Load ( string stylesheetUri , XsltSettings settings , XmlResolver stylesheetResolver )
{
Load ( new XPathDocument ( stylesheetUri , XmlSpace . Preserve ) . CreateNavigator ( ) , settings , stylesheetResolver ) ;
}
2016-11-10 13:04:39 +00:00
public void Load ( MethodInfo executeMethod , byte [ ] queryData , Type [ ] earlyBoundTypes )
{
throw new NotImplementedException ( ) ;
}
public void Load ( Type compiledStylesheet )
{
throw new NotImplementedException ( ) ;
}
2014-08-13 10:39:27 +01:00
#endregion
}
class NoOperationDebugger
{
protected void OnCompile ( XPathNavigator input )
{
}
protected void OnExecute ( XPathNodeIterator currentNodeset , XPathNavigator style , XsltContext context )
{
//ShowLocationInTrace (style);
}
/ *
string ShowLocationInTrace ( XPathNavigator style )
{
IXmlLineInfo li = style as IXmlLineInfo ;
return li ! = null ? String . Format ( "at {0} ({1},{2})" , style . BaseURI , li . LineNumber , li . LinePosition ) : "(no debug info available)" ;
}
* /
}
}