Beispiel #1
0
        public virtual DOMNode removeChild(DOMNode oldnode)
        {
            // check for readonly node
            if (XmlNode.IsReadOnly)
            {
                DOMException.Throw(ExceptionCode.DomModificationNotAllowed);
                return(null);
            }

            try
            {
                XmlNode.RemoveChild(oldnode.XmlNode);
            }
            catch (ArgumentException)
            {
                DOMException.Throw(ExceptionCode.NotFound);
                return(null);
            }

            return(oldnode);
        }
Beispiel #2
0
        /// <summary>
        /// Gets all descendant elements with the matching tag name.
        /// </summary>
        /// <param name="name">The tag name. Use <B>*</B> to return all elements within the element tree.</param>
        /// <returns>A <see cref="DOMNodeList"/>.</returns>
        public virtual DOMNodeList getElementsByTagName(string name)
        {
            DOMNodeList list = new DOMNodeList();

            if (IsAssociated)
            {
                // enumerate elements in the default namespace
                foreach (XmlNode node in XmlElement.GetElementsByTagName(name))
                {
                    var 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))
                        {
                            var dom_node = DOMNode.Create(node);
                            if (dom_node != null)
                            {
                                list.AppendNode(dom_node);
                            }
                        }
                    }
                }
            }

            return(list);
        }
Beispiel #3
0
        /// <summary>
        /// Evaluates the given XPath expression and returns a typed result if possible.
        /// </summary>
        /// <param name="expr">The expression to evaluate.</param>
        /// <param name="contextnode">The context node for doing relative XPath queries. By default, the queries are
        /// relative to the root element.</param>
        /// <param name="registerNodeNS">Can be specified to disable automatic registration of the context node namespace.</param>
        /// <returns>A typed result if possible or a <see cref="DOMNodeList"/> containing all nodes matching the
        /// given <paramref name="expr"/>.</returns>
        public PhpValue evaluate(string expr, DOMNode contextnode = null, bool registerNodeNS = true)
        {
            XPathNavigator navigator = GetNavigator(contextnode);

            if (navigator == null)
            {
                return(PhpValue.Create(false));
            }

            var nsManager = registerNodeNS ? NamespaceManagerFull : NamespaceManagerExplicit;

            object result;

            try
            {
                result = navigator.Evaluate(expr, nsManager);
            }
            catch (Exception ex)
            {
                DOMException.Throw(ExceptionCode.SyntaxError, ex.Message);
                return(PhpValue.Create(false));
            }

            // the result can be bool, double, string, or iterator
            XPathNodeIterator iterator = result as XPathNodeIterator;

            if (iterator != null)
            {
                //return PhpValue.FromClass(IteratorToList(iterator));
                var domList = QueryInternal(contextnode?.XmlNode, expr, nsManager);
                return((domList == null) ? PhpValue.Create(false) : PhpValue.FromClass(domList));
            }
            else
            {
                return(PhpValue.FromClr(result));
            }
        }
Beispiel #4
0
        public virtual DOMNode appendChild(DOMNode newNode)
        {
            bool is_fragment;

            if (newNode is DOMDocumentFragment)
            {
                if (!newNode.IsAssociated || !newNode.XmlNode.HasChildNodes)
                {
                    PhpException.Throw(PhpError.Warning, Resources.DocumentFragmentEmpty);
                    return(null);
                }
                is_fragment = true;
            }
            else
            {
                is_fragment = false;
            }

            XmlNode result = CheckedChildOperation(newNode, null, delegate(DOMNode _newNode, DOMNode _)
            {
                return(XmlNode.AppendChild(_newNode.XmlNode));
            });

            if (result == null)
            {
                return(null);
            }
            if (is_fragment)
            {
                return(Create(result));
            }
            else
            {
                return(newNode);
            }
        }
Beispiel #5
0
        /// <summary>
        /// Saves this document to a given stream.
        /// </summary>
        /// <param name="ctx">Current runtime context.</param>
        /// <param name="outStream">The output stream.</param>
        /// <param name="node">The node to dump (the entire document if <B>null</B>).</param>
        /// <param name="omitXmlDeclaration">Whether to skip the opening XML declaration.</param>
        /// <returns>True for success, false for failure.</returns>
        private bool SaveXMLInternal(Context ctx, Stream outStream, DOMNode node = null, bool omitXmlDeclaration = false)
        {
            XmlNode xml_node;

            if (node == null)
            {
                xml_node = XmlDocument;
            }
            else
            {
                xml_node = node.XmlNode;

                if (xml_node.OwnerDocument != XmlDocument && xml_node != XmlNode)
                {
                    DOMException.Throw(ExceptionCode.WrongDocument);
                    return(false);
                }
            }

            var settings = new XmlWriterSettings()
            {
                NewLineHandling    = NewLineHandling.None,
                Encoding           = Utils.GetNodeEncoding(ctx, xml_node),
                Indent             = _formatOutput,
                ConformanceLevel   = node == null ? ConformanceLevel.Document : ConformanceLevel.Fragment,
                OmitXmlDeclaration = omitXmlDeclaration
            };

            // use a XML writer and set its Formatting property to Formatting.Indented
            using (var writer = System.Xml.XmlWriter.Create(outStream, settings))
            {
                xml_node.WriteTo(writer);
            }

            return(true);
        }
Beispiel #6
0
 internal void AppendNode(DOMNode /*!*/ node)
 {
     Debug.Assert(node != null);
     _list.Add(node);
 }
Beispiel #7
0
 /// <summary>
 /// Not implemented in PHP 5.1.6.
 /// </summary>
 public DOMNode setNamedItemNS(DOMNode item)
 {
     throw new NotImplementedException();
 }
Beispiel #8
0
 /// <summary>
 /// Indicates if two nodes are the same node.
 /// </summary>
 /// <param name="anotherNode">The other node.</param>
 /// <returns><B>True</B> or <B>false</B>.</returns>
 public virtual bool isSameNode(DOMNode anotherNode) => (XmlNode == anotherNode.XmlNode);
Beispiel #9
0
 /// <summary>
 /// Not implemented in PHP 7.1.1.
 /// </summary>
 public virtual void renameNode(DOMNode node, string namespaceUri, string qualifiedName)
 {
     throw new NotImplementedException();
 }
Beispiel #10
0
 /// <summary>
 /// Not implemented in PHP 7.1.1.
 /// </summary>
 public virtual DOMNode adoptNode(DOMNode source)
 {
     throw new NotImplementedException();
 }
Beispiel #11
0
 /// <summary>
 /// Currently not supported.
 /// </summary>
 public bool expand(DOMNode basenode = null)
 {
     PhpException.FunctionNotSupported(nameof(expand));
     return(false);
 }
Beispiel #12
0
 public static DOMElement dom_import_simplexml(SimpleXMLElement node) => (DOMElement)DOMNode.Create(node.XmlElement);
Beispiel #13
0
 public virtual string saveHTML(DOMNode node = null)
 {
     throw new NotImplementedException();
 }