public void MatchRoot() { XmlDocument xsltDoc = new XmlDocument(); xsltDoc.LoadXml(@" <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:output method='xml' omit-xml-declaration='no' encoding='UTF-8'/> <xsl:template match='/'><root>Matched</root></xsl:template> </xsl:stylesheet> " ); XslTransform xslt = new XslTransform(); xslt.Load(xsltDoc); StringBuilder builder = new StringBuilder(); XmlWriter writer = new XmlTextWriter(new StringWriter(builder)); CollectionOfSimple c = new CollectionOfSimple(true); ObjectXPathContext context = new ObjectXPathContext(); context.DetectLoops = true; XPathNavigator nav = context.CreateNavigator(c); xslt.Transform(nav, null, writer, null); writer.Close(); Assert.AreEqual("<root>Matched</root>", builder.ToString()); }
public void SelectAllNodes() { // Generate simple objects var objects = new List<SimpleObject>(); int totalIterations = 100000; for( int i=0; i<totalIterations; i++ ) { SimpleObject o = new SimpleObject(); o.Attr2 = i.ToString(); o.Elem1 = "Elem1" + i; o.Elem2 = "Elem2" + i; objects.Add( o ); } // Create navigator var context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace( "sdf", Namespaces.SDF ); XPathNavigator nav = context.CreateNavigator( objects ); // Select nodes var nodes = nav.Select( "//* | //@*" ); int j = 0; while( nodes.MoveNext() ) { var val = nodes.Current.Value; j++; } Assert.AreEqual( totalIterations * 5 + 1, j ); }
public void XmlNodeDefaultNamespace() { string xml = @"<Requirement xmlns='http://www.byte-force.com/schemas/SDF' xmlns:a='ns-a' xmlns:b='ns-b'> <Type tc='11'>APS</Type> <ClientUniqueId>2468</ClientUniqueId> <ProcessingUniqueId xmlns:a='ns-a'>923232</ProcessingUniqueId> <State xmlns:b='ns-b'> <Code tc='75'>Accepted</Code> <SubCode tc='3'>Reviewing</SubCode> </State> </Requirement>" ; XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace("sdf", Namespaces.SDF); XPathNavigator nav = context.CreateNavigator(doc); nav.MoveToFirstChild(); Assert.AreEqual("sdf", nav.Prefix); Assert.AreEqual("Requirement", nav.LocalName); Assert.AreEqual("sdf:Requirement", nav.Name); }
public void MatchRoot() { XmlDocument xsltDoc = new XmlDocument(); xsltDoc.LoadXml( @" <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:output method='xml' omit-xml-declaration='no' encoding='UTF-8'/> <xsl:template match='/'><root>Matched</root></xsl:template> </xsl:stylesheet> " ); XslTransform xslt = new XslTransform(); xslt.Load( xsltDoc ); StringBuilder builder = new StringBuilder(); XmlWriter writer = new XmlTextWriter( new StringWriter( builder ) ); CollectionOfSimple c = new CollectionOfSimple( true ); ObjectXPathContext context = new ObjectXPathContext(); context.DetectLoops = true; XPathNavigator nav = context.CreateNavigator( c ); xslt.Transform( nav, null, writer, null ); writer.Close(); Assert.AreEqual( "<root>Matched</root>", builder.ToString() ); }
public void SelectAllNodes() { // Generate simple objects var objects = new List <SimpleObject>(); int totalIterations = 100000; for (int i = 0; i < totalIterations; i++) { SimpleObject o = new SimpleObject(); o.Attr2 = i.ToString(); o.Elem1 = "Elem1" + i; o.Elem2 = "Elem2" + i; objects.Add(o); } // Create navigator var context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace("sdf", Namespaces.SDF); XPathNavigator nav = context.CreateNavigator(objects); // Select nodes var nodes = nav.Select("//* | //@*"); int j = 0; while (nodes.MoveNext()) { var val = nodes.Current.Value; j++; } Assert.AreEqual(totalIterations * 5 + 1, j); }
public static void IsSubset( object o, string xml ) { XmlDocument doc = new XmlDocument(); doc.LoadXml( xml ); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace( "sdf", Namespaces.SDF ); XPathNavigator nav = context.CreateNavigator( o ); nav.MoveToRoot(); NavigatorUtils.IsSubsetOf( doc.CreateNavigator(), nav ); }
public void LoadInDocument() { CollectionOfSimple c = new CollectionOfSimple( true ); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace( "sdf", Namespaces.SDF ); XPathNavigator nav = context.CreateNavigator( c ); for( int i=0; i<100000; i++ ) { nav.MoveToRoot(); XmlDocument original = new XmlDocument(); original.Load( nav.ReadSubtree() ); } }
private static void SerializeAndCompare(object o) { XPathNavigator nav1 = NavigatorUtils.Serialize(o); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace("sdf", Namespaces.SDF); XPathNavigator nav2 = context.CreateNavigator(o); NavigatorUtils.PrintNavigator(nav2); nav2.MoveToRoot(); NavigatorUtils.AreEqual(nav2, nav1); }
public static void IsSubset(object o, string xml) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace("sdf", Namespaces.SDF); XPathNavigator nav = context.CreateNavigator(o); nav.MoveToRoot(); NavigatorUtils.IsSubsetOf(doc.CreateNavigator(), nav); }
internal ObjectXPathNavigator( object obj, ObjectXPathContext context ) { _context = context; _root = new Node( _context, new RootNodePolicy( obj ) ); _node = _root; if( _context.DetectLoops ) { _navigationStack = new Stack(); _navigationStack.Push( _root ); // Push dummy root object } _lang = _context.NameTable.Add( "" ); }
public void LoadInDocument() { CollectionOfSimple c = new CollectionOfSimple(true); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace("sdf", Namespaces.SDF); XPathNavigator nav = context.CreateNavigator(c); for (int i = 0; i < 100000; i++) { nav.MoveToRoot(); XmlDocument original = new XmlDocument(); original.Load(nav.ReadSubtree()); } }
public void SelectObjectTest() { var obj = new ObjChildrenNS(true); var context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace("sdf", Namespaces.SDF); var nav = context.CreateNavigator(obj) as ObjectXPathNavigator; var str1 = nav.SelectObject("/*/child4"); var str2 = nav.SelectObject("/sdf:XmlObjectWithAttribs/sdf:child2", context.NamespaceManager); Assert.AreEqual("7", str1); Assert.AreEqual("5", str2); }
public static void Compare( object o, string xml ) { XmlDocument doc = new XmlDocument(); doc.LoadXml( xml ); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace( "sdf", Namespaces.SDF ); XPathNavigator nav = context.CreateNavigator( o ); nav.MoveToRoot(); /*NavigatorUtils.PrintNavigator( nav ); nav.MoveToRoot();*/ NavigatorUtils.AreEqual( nav, doc.CreateNavigator() ); }
public void IdentityTransform() { XmlDocument xsltDoc = new XmlDocument(); xsltDoc.LoadXml(@" <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:output method='xml' omit-xml-declaration='no' encoding='UTF-8'/> <xsl:template match='@*|node()'> <xsl:copy> <xsl:apply-templates select='@*|node()'/> </xsl:copy> </xsl:template> </xsl:stylesheet> " ); XslTransform xslt = new XslTransform(); xslt.Load(xsltDoc); StringBuilder builder = new StringBuilder(); XmlWriter writer = new XmlTextWriter(new StringWriter(builder)); CollectionOfSimple c = new CollectionOfSimple(true); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace("sdf", "http://www.byte-force.com/schemas/SDF"); XPathNavigator nav = context.CreateNavigator(c); XmlDocument original = new XmlDocument(); original.Load(nav.ReadSubtree()); nav.MoveToRoot(); Console.WriteLine(original.InnerXml); string originalXml = original.InnerXml; xslt.Transform(nav, null, writer, null); writer.Close(); Console.Write(builder.ToString()); Assert.AreEqual(originalXml, builder.ToString()); }
public static void Compare(object o, string xml) { XmlDocument doc = new XmlDocument(); doc.LoadXml(xml); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace("sdf", Namespaces.SDF); XPathNavigator nav = context.CreateNavigator(o); nav.MoveToRoot(); /*NavigatorUtils.PrintNavigator( nav ); * * nav.MoveToRoot();*/ NavigatorUtils.AreEqual(nav, doc.CreateNavigator()); }
private static void AddFlash(IRailsEngineContext context, XsltTransformationArguments arguments) { if (context.Flash.Keys.Count == 0) { return; } ObjectXPathContext ocontext = new ObjectXPathContext(); //For each object in the flash build an ObjectXPathNavigator to //create an XPath-able represenation of that object foreach (String key in context.Flash.Keys) { object value = context.Flash[key]; if (value != null) { arguments.AddParam(key, string.Empty, ocontext.CreateNavigator(value)); } } }
public void Xslt() { XmlDocument xsltDoc = new XmlDocument(); xsltDoc.LoadXml(@" <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:output method='xml' omit-xml-declaration='no' encoding='UTF-8'/> <xsl:template match='@*|node()'> <xsl:copy> <xsl:apply-templates select='@*'/> <xsl:apply-templates select='node()'/> </xsl:copy> </xsl:template> </xsl:stylesheet> " ); XslTransform xslt = new XslTransform(); xslt.Load(xsltDoc); CollectionOfSimple c = new CollectionOfSimple(true); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace("sdf", Namespaces.SDF); XPathNavigator nav = context.CreateNavigator(c); for (int i = 0; i < 100000; i++) { StringBuilder builder = new StringBuilder(); XmlWriter writer = new XmlTextWriter(new StringWriter(builder)); XsltArgumentList xslArg = new XsltArgumentList(); nav.MoveToRoot(); xslt.Transform(nav, xslArg, writer, null); writer.Close(); } }
/// <summary> /// Adds XPathNavigable representations of the items in the propertybag /// of the controller. /// </summary> /// <param name="controller">The controller.</param> /// <param name="arguments">The arguments.</param> private static void AddPropertyBag(IRailsEngineContext railscontext, IController controller, XsltTransformationArguments arguments) { ObjectXPathContext context = new ObjectXPathContext(); //For each object in the property bag build an ObjectXPathNavigator to //create an XPath-able represenation of that object foreach (String key in controller.PropertyBag.Keys) { object value = controller.PropertyBag[key]; if (value != null) { arguments.AddParam(key, string.Empty, context.CreateNavigator(value)); } } arguments.AddParam("context", string.Empty, context.CreateNavigator(railscontext)); //arguments.AddExtensionObject("urn:request", context.Request); //arguments.AddExtensionObject("urn:response", context.Response); //arguments.AddExtensionObject("urn:server", context.Server); //arguments.AddExtensionObject("urn:session", context.Session); }
public void IdentityTransform() { XmlDocument xsltDoc = new XmlDocument(); xsltDoc.LoadXml( @" <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:output method='xml' omit-xml-declaration='no' encoding='UTF-8'/> <xsl:template match='@*|node()'> <xsl:copy> <xsl:apply-templates select='@*|node()'/> </xsl:copy> </xsl:template> </xsl:stylesheet> " ); XslTransform xslt = new XslTransform(); xslt.Load( xsltDoc ); StringBuilder builder = new StringBuilder(); XmlWriter writer = new XmlTextWriter( new StringWriter( builder )); CollectionOfSimple c = new CollectionOfSimple( true ); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace( "sdf", "http://www.byte-force.com/schemas/SDF" ); XPathNavigator nav = context.CreateNavigator( c ); XmlDocument original = new XmlDocument(); original.Load( nav.ReadSubtree() ); nav.MoveToRoot(); Console.WriteLine( original.InnerXml ); string originalXml = original.InnerXml; xslt.Transform( nav, null, writer, null ); writer.Close(); Console.Write( builder.ToString() ); Assert.AreEqual( originalXml, builder.ToString() ); }
private static void DeserializeAndCompare(object o) { ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace("sdf", Namespaces.SDF); XPathNavigator nav = context.CreateNavigator(o); nav.MoveToRoot(); NavigatorUtils.PrintNavigator(nav); nav.MoveToRoot(); // object o2 = Deserialize( o.GetType(), nav ); object o2 = NavigatorUtils.Deserialize(o.GetType(), NavigatorUtils.Serialize(o)); XPathNavigator nav2 = context.CreateNavigator(o2); nav2.MoveToRoot(); nav.MoveToRoot(); NavigatorUtils.AreEqual(nav, nav2); }
public void Xslt() { XmlDocument xsltDoc = new XmlDocument(); xsltDoc.LoadXml( @" <xsl:stylesheet version='1.0' xmlns:xsl='http://www.w3.org/1999/XSL/Transform'> <xsl:output method='xml' omit-xml-declaration='no' encoding='UTF-8'/> <xsl:template match='@*|node()'> <xsl:copy> <xsl:apply-templates select='@*'/> <xsl:apply-templates select='node()'/> </xsl:copy> </xsl:template> </xsl:stylesheet> " ); XslTransform xslt = new XslTransform(); xslt.Load( xsltDoc ); CollectionOfSimple c = new CollectionOfSimple( true ); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace( "sdf", Namespaces.SDF ); XPathNavigator nav = context.CreateNavigator( c ); for( int i=0; i<100000; i++ ) { StringBuilder builder = new StringBuilder(); XmlWriter writer = new XmlTextWriter( new StringWriter( builder )); XsltArgumentList xslArg = new XsltArgumentList(); nav.MoveToRoot(); xslt.Transform( nav, xslArg, writer, null ); writer.Close(); } }
private static void DeserializeAndCompare( object o ) { ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace( "sdf", Namespaces.SDF ); XPathNavigator nav = context.CreateNavigator( o ); nav.MoveToRoot(); NavigatorUtils.PrintNavigator( nav ); nav.MoveToRoot(); // object o2 = Deserialize( o.GetType(), nav ); object o2 = NavigatorUtils.Deserialize( o.GetType(), NavigatorUtils.Serialize( o ) ); XPathNavigator nav2 = context.CreateNavigator( o2 ); nav2.MoveToRoot(); nav.MoveToRoot(); NavigatorUtils.AreEqual( nav, nav2 ); }
/// <summary> /// Adds XPathNavigable representations of the items in the propertybag /// of the controller. /// </summary> /// <param name="controller">The controller.</param> /// <param name="arguments">The arguments.</param> private static void AddPropertyBag(IRailsEngineContext railscontext, IController controller, XsltTransformationArguments arguments) { ObjectXPathContext context = new ObjectXPathContext(); //For each object in the property bag build an ObjectXPathNavigator to //create an XPath-able represenation of that object foreach (String key in controller.PropertyBag.Keys) { object value = controller.PropertyBag[key]; if (value != null) { arguments.AddParam(key, string.Empty, context.CreateNavigator(value)); } } arguments.AddParam("context", string.Empty, context.CreateNavigator(railscontext)); //arguments.AddExtensionObject("urn:request", context.Request); //arguments.AddExtensionObject("urn:response", context.Response); //arguments.AddExtensionObject("urn:server", context.Server); //arguments.AddExtensionObject("urn:session", context.Session); }
/// <summary> /// See <see cref="System.Xml.XPath.XPathNavigator.MoveTo" /> for details. /// </summary> public override bool MoveTo( XPathNavigator other ) { var otherNav = other as ObjectXPathNavigator; if( otherNav == null ) return false; _context = otherNav._context; _root = otherNav._root; _node = otherNav._node; if( otherNav._childNav != null ) { _childNav = otherNav._childNav.Clone(); _childNavDepth = otherNav._childNavDepth; } else _childNav = null; #if DEBUG Trace( () => string.Format( "MoveTo( N#{0} )", other.GetHashCode() ) ); #endif if( _context.DetectLoops ) _navigationStack = (Stack)otherNav._navigationStack.Clone(); return true; }
private static void AddFlash(IRailsEngineContext context, XsltTransformationArguments arguments) { if (context.Flash.Keys.Count == 0) return; ObjectXPathContext ocontext = new ObjectXPathContext(); //For each object in the flash build an ObjectXPathNavigator to //create an XPath-able represenation of that object foreach (String key in context.Flash.Keys) { object value = context.Flash[key]; if (value != null) { arguments.AddParam(key, string.Empty, ocontext.CreateNavigator(value)); } } }
public void XmlNodeDefaultNamespace() { string xml = @"<Requirement xmlns='http://www.byte-force.com/schemas/SDF' xmlns:a='ns-a' xmlns:b='ns-b'> <Type tc='11'>APS</Type> <ClientUniqueId>2468</ClientUniqueId> <ProcessingUniqueId xmlns:a='ns-a'>923232</ProcessingUniqueId> <State xmlns:b='ns-b'> <Code tc='75'>Accepted</Code> <SubCode tc='3'>Reviewing</SubCode> </State> </Requirement>"; XmlDocument doc = new XmlDocument(); doc.LoadXml( xml ); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace( "sdf", Namespaces.SDF ); XPathNavigator nav = context.CreateNavigator( doc ); nav.MoveToFirstChild(); Assert.AreEqual( "sdf", nav.Prefix ); Assert.AreEqual( "Requirement", nav.LocalName ); Assert.AreEqual( "sdf:Requirement", nav.Name ); }
private static void SerializeAndCompare( object o ) { XPathNavigator nav1 = NavigatorUtils.Serialize( o ); ObjectXPathContext context = new ObjectXPathContext(); context.NamespaceManager.AddNamespace( "sdf", Namespaces.SDF ); XPathNavigator nav2 = context.CreateNavigator( o ); NavigatorUtils.PrintNavigator( nav2 ); nav2.MoveToRoot(); NavigatorUtils.AreEqual( nav2, nav1 ); }
private string _value; // Empty if null #endregion Fields #region Constructors /// <summary> /// Creates a new <see cref="Node"/> instance. /// </summary> /// <param name="context">Context this node belongs to.</param> /// <param name="policy">The node policy.</param> public Node( ObjectXPathContext context, INodePolicy policy ) { _context = context; _nodePolicy = policy; _nodeType = XPathNodeType.All; _nodeState = NodeState.ObjectNotKnown; _attributesCount = -1; _elementsCount = -1; }