Exemple #1
0
        /// <summary>
        /// Converts a W3C PHP object to a corresponding string.
        /// </summary>
        public static string /*!*/ PhpToString(object arg)
        {
            // Node* (XPath)
            IXmlDomNode node = arg as IXmlDomNode;

            if (node != null)
            {
                return(node.UnderlyingObject.Value);
            }

            // Node Set (XPath), Result Tree Fragment (XSLT)
            DOMNodeList list = arg as DOMNodeList;

            if (list != null)
            {
                if (list.length == 0)
                {
                    return(String.Empty);
                }
                return(list.item(0).UnderlyingObject.Value);
            }

            // any other object
            return(PHP.Core.Convert.ObjectToString(arg));
        }
Exemple #2
0
        public object transformToDoc(IXmlDomNode node)
        {
            XmlDocument doc = new XmlDocument();

            doc.PreserveWhitespace = true;

            using (MemoryStream stream = new MemoryStream())
            {
                XmlWriterSettings settings = GetOutputSettings();
                if (settings.Encoding is UTF8Encoding)
                {
                    // no BOM in UTF-8 please!
                    settings          = settings.Clone();
                    settings.Encoding = new UTF8Encoding(false);
                }

                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    // transform the document
                    try
                    {
                        TransformToWriter(node.UnderlyingObject, xsltArgumentList, writer);
                    }
                    catch (XsltException e)
                    {
                        if (e.InnerException != null)
                        {
                            // ScriptDiedException etc.
                            throw e.InnerException;
                        }

                        PhpException.Throw(PhpError.Warning, e.Message);
                        return(false);
                    }
                    catch (InvalidOperationException e)
                    {
                        PhpException.Throw(PhpError.Warning, e.Message);
                        return(false);
                    }
                }

                stream.Seek(0, SeekOrigin.Begin);

                // build the resulting XML document
                try
                {
                    doc.Load(stream);
                }
                catch (XmlException e)
                {
                    PhpException.Throw(PhpError.Warning, e.Message);
                    return(false);
                }
            }

            return(new DOMDocument(doc));
        }
Exemple #3
0
        /// <summary>
        /// Converts a W3C .NET object to the corresponding W3C PHP object.
        /// </summary>
        public static object DotNetToPhp(object arg)
        {
            // Result Tree Fragment (XSLT) / Node (XPath)
            XPathNavigator nav = arg as XPathNavigator;

            if (nav != null)
            {
                return(DOMNode.Create(nav.UnderlyingObject as XmlNode));
            }

            // Node Set (XPath) - XPathNavigator[]
            XPathNavigator[] navs = arg as XPathNavigator[];
            if (navs != null)
            {
                PhpArray array = new PhpArray(navs.Length, 0);

                for (int i = 0; i < navs.Length; i++)
                {
                    IXmlDomNode node = DOMNode.Create(navs[i].UnderlyingObject as XmlNode);
                    if (node != null)
                    {
                        array.Add(node);
                    }
                }

                return(array);
            }

            // Node Set (XPath) - XPathNodeIterator
            XPathNodeIterator iter = arg as XPathNodeIterator;

            if (iter != null)
            {
                PhpArray array = new PhpArray();

                foreach (XPathNavigator navigator in iter)
                {
                    IXmlDomNode node = DOMNode.Create(navigator.UnderlyingObject as XmlNode);
                    if (node != null)
                    {
                        array.Add(node);
                    }
                }

                return(array);
            }

            // Number (XPath), Boolean (XPath), String (XPath)
            return(arg);
        }
Exemple #4
0
        internal void AddNode(IXmlDomNode /*!*/ node)
        {
            Debug.Assert(node != null);

            DOMNode domNode = node as DOMNode;

            if (domNode != null)
            {
                map.Add(new MapKey(domNode.namespaceURI, domNode.localName), domNode);
            }
            else
            {
                map.Add(new MapKey(node.UnderlyingObject.NamespaceURI, node.UnderlyingObject.LocalName), node);
            }
        }
Exemple #5
0
        public object getElementsByTagNameNS(string namespaceUri, string localName)
        {
            DOMNodeList list = new DOMNodeList();

            foreach (XmlNode node in XmlDocument.GetElementsByTagName(localName, namespaceUri))
            {
                IXmlDomNode dom_node = DOMNode.Create(node);
                if (dom_node != null)
                {
                    list.AppendNode(dom_node);
                }
            }

            return(list);
        }
Exemple #6
0
        private DOMNodeList IteratorToList(XPathNodeIterator iterator)
        {
            DOMNodeList list = new DOMNodeList();

            while (iterator.MoveNext())
            {
                IHasXmlNode has_node = iterator.Current as IHasXmlNode;
                if (has_node != null)
                {
                    IXmlDomNode node = DOMNode.Create(has_node.GetNode());
                    if (node != null)
                    {
                        list.AppendNode(node);
                    }
                }
            }

            return(list);
        }
Exemple #7
0
        /// <summary>
        /// Converts a W3C PHP object to the corresponding W3C .NET object.
        /// </summary>
        public static object /*!*/ PhpToDotNet(object arg)
        {
            if (arg == null)
            {
                return(String.Empty);
            }

            // Node* (XPath)
            IXmlDomNode node = arg as IXmlDomNode;

            if (node != null)
            {
                return(node.UnderlyingObject.CreateNavigator());
            }

            // Node Set (XPath), Result Tree Fragment (XSLT)
            DOMNodeList list = arg as DOMNodeList;

            if (list != null)
            {
                XPathNavigator[] navs = new XPathNavigator[list.length];

                int i = 0;
                foreach (var pair in (IEnumerable <KeyValuePair <object, object> >)list)
                {
                    navs[i++] = ((IXmlDomNode)pair.Value).UnderlyingObject.CreateNavigator();
                }

                return(navs);
            }

            // any other object
            IPhpVariable var = arg as IPhpVariable;

            if (var != null)
            {
                return(var.ToString());
            }

            // String (XPath), Boolean (XPath), Number (XPath)
            return(arg);
        }
Exemple #8
0
        public object getElementsByTagName(string name)
        {
            DOMNodeList list = new DOMNodeList();

            if (IsAssociated)
            {
                // enumerate elements in the default namespace
                foreach (XmlNode node in XmlElement.GetElementsByTagName(name))
                {
                    IXmlDomNode dom_node = DOMNode.Create(node);
                    if (dom_node != null)
                    {
                        list.AppendNode(dom_node);
                    }
                }

                // enumerate all namespaces
                XPathNavigator    navigator = XmlElement.CreateNavigator();
                XPathNodeIterator iterator  = navigator.Select("//namespace::*[not(. = ../../namespace::*)]");

                while (iterator.MoveNext())
                {
                    string prefix = iterator.Current.Name;
                    if (!String.IsNullOrEmpty(prefix) && prefix != "xml")
                    {
                        // enumerate elements in this namespace
                        foreach (XmlNode node in XmlElement.GetElementsByTagName(name, iterator.Current.Value))
                        {
                            IXmlDomNode dom_node = DOMNode.Create(node);
                            if (dom_node != null)
                            {
                                list.AppendNode(dom_node);
                            }
                        }
                    }
                }
            }

            return(list);
        }
Exemple #9
0
		public object transformToDoc(IXmlDomNode node)
		{
			XmlDocument doc = new XmlDocument();
			doc.PreserveWhitespace = true;

			using (MemoryStream stream = new MemoryStream())
			{
				XmlWriterSettings settings = GetOutputSettings();
				if (settings.Encoding is UTF8Encoding)
				{
					// no BOM in UTF-8 please!
					settings = settings.Clone();
					settings.Encoding = new UTF8Encoding(false);
				}

				using (XmlWriter writer = XmlWriter.Create(stream, settings))
				{
					// transform the document
					try
					{
						TransformToWriter(node.UnderlyingObject, xsltArgumentList, writer);
					}
					catch (XsltException e)
					{
						if (e.InnerException != null)
						{
							// ScriptDiedException etc.
							throw e.InnerException;
						}

						PhpException.Throw(PhpError.Warning, e.Message);
						return false;
					}
					catch (InvalidOperationException e)
					{
						PhpException.Throw(PhpError.Warning, e.Message);
						return false;
					}
				}

				stream.Seek(0, SeekOrigin.Begin);

				// build the resulting XML document
				try
				{
					doc.Load(stream);
				}
				catch (XmlException e)
				{
					PhpException.Throw(PhpError.Warning, e.Message);
					return false;
				}
			}

			return new DOMDocument(doc);
		}
Exemple #10
0
 internal void AppendNode(IXmlDomNode /*!*/ node)
 {
     Debug.Assert(node != null);
     list.Add(node);
 }