Example #1
0
        /// <summary>
        /// Sets the value of a attribute (creates new one if it does not exist).
        /// </summary>
        /// <param name="namespaceUri">The attribute namespace URI.</param>
        /// <param name="qualifiedName">The attribute qualified name.</param>
        /// <param name="value">The attribute value.</param>
        /// <returns><B>True</B> on success, <B>false</B> on failure.</returns>
        public virtual bool setAttributeNS(string namespaceUri, string qualifiedName, string value)
        {
            if (!IsAssociated)
            {
                DOMException.Throw(ExceptionCode.DomModificationNotAllowed);
                return(false);
            }

            if (string.IsNullOrEmpty(qualifiedName))
            {
                // Warning: DOMElement::setAttributeNS(): Attribute Name is required
                PhpException.InvalidArgument(nameof(qualifiedName));
                return(false);
            }

            // parse the qualified name
            Utils.ParseQualifiedName(qualifiedName, out var prefix, out var local_name);

            var attr = XmlElement.Attributes[local_name, namespaceUri];

            if (attr == null)
            {
                attr = XmlNode.OwnerDocument.CreateAttribute(qualifiedName, namespaceUri);
                XmlElement.Attributes.Append(attr);
            }
            else
            {
                attr.Prefix = prefix;
            }

            attr.Value = value;

            return(true);
        }
Example #2
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)));
            }
            else
            {
                return(PhpValue.FromClr(result));
            }
        }
Example #3
0
        public virtual PhpString saveXML(Context ctx, DOMNode node = null, int options = 0)
        {
            XmlNode xml_node;

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

            var settings = new XmlWriterSettings()
            {
                Encoding = Utils.GetNodeEncoding(ctx, xml_node),
                Indent   = _formatOutput,
            };

            using (MemoryStream stream = new MemoryStream())
            {
                // use a XML writer and set its Formatting proprty to Formatting.Indented
                using (XmlWriter writer = XmlWriter.Create(stream, settings))
                {
                    xml_node.WriteTo(writer);
                }

                return(new PhpString(stream.ToArray()));
            }
        }
Example #4
0
        /// <summary>
        /// Adds new attribute node to the element.
        /// </summary>
        /// <param name="attribute">The attribute node.</param>
        /// <returns>Old node if the attribute has been replaced or <B>null</B>.</returns>
        public virtual DOMAttr setAttributeNodeNS(DOMAttr attribute)
        {
            if (!IsAssociated)
            {
                DOMException.Throw(ExceptionCode.DomModificationNotAllowed);
                return(null);
            }

            attribute.Associate(XmlElement.OwnerDocument);

            if (XmlNode.OwnerDocument != attribute.XmlNode.OwnerDocument)
            {
                DOMException.Throw(ExceptionCode.WrongDocument);
                return(null);
            }

            XmlAttribute attr = XmlElement.Attributes[attribute.localName, attribute.namespaceURI];

            if (attr != null)
            {
                XmlElement.Attributes.Remove(attr);
                XmlElement.Attributes.Append(attribute.XmlAttribute);
                return(new DOMAttr(attr));
            }
            else
            {
                XmlElement.Attributes.Append(attribute.XmlAttribute);
                return(null);
            }
        }
Example #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 = XmlNode;
            }
            else
            {
                xml_node = node.XmlNode;
                if (xml_node.OwnerDocument != XmlDocument && xml_node != XmlNode)
                {
                    DOMException.Throw(ExceptionCode.WrongDocument);
                    return(false);
                }
            }

            var settings = new XmlWriterSettings()
            {
                Encoding           = Utils.GetNodeEncoding(ctx, xml_node),
                Indent             = _formatOutput,
                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);
        }
Example #6
0
        /// <summary>
        /// Sets the value of a attribute (creates new one if it does not exist).
        /// </summary>
        /// <param name="namespaceUri">The attribute namespace URI.</param>
        /// <param name="qualifiedName">The attribute qualified name.</param>
        /// <param name="value">The attribute value.</param>
        /// <returns><B>True</B> on success, <B>false</B> on failure.</returns>
        public virtual bool setAttributeNS(string namespaceUri, string qualifiedName, string value)
        {
            if (!IsAssociated)
            {
                DOMException.Throw(ExceptionCode.DomModificationNotAllowed);
                return(false);
            }

            // parse the qualified name
            string local_name, prefix;

            Utils.ParseQualifiedName(qualifiedName, out prefix, out local_name);

            XmlAttribute attr = XmlElement.Attributes[local_name, namespaceUri];

            if (attr == null)
            {
                attr = XmlNode.OwnerDocument.CreateAttribute(qualifiedName, namespaceUri);
                XmlElement.Attributes.Append(attr);
            }
            else
            {
                attr.Prefix = prefix;
            }

            attr.Value = value;

            return(true);
        }
Example #7
0
        /// <summary>
        /// Inserts the specified string at the specified character offset.
        /// </summary>
        /// <param name="offset">The position within the string to insert the supplied string data.</param>
        /// <param name="arg">The string data that is to be inserted into the existing string.</param>
        public virtual void insertData(int offset, string arg)
        {
            if (offset < 0 || offset > XmlCharacterData.Length)
            {
                DOMException.Throw(ExceptionCode.IndexOutOfBounds);
            }

            XmlCharacterData.InsertData(offset, arg);
        }
Example #8
0
        /// <summary>
        /// Replaces the specified number of characters starting at the specified offset with the specified string.
        /// </summary>
        /// <param name="offset">The position within the string to start replacing.</param>
        /// <param name="count">The number of characters to replace.</param>
        /// <param name="arg">The new data that replaces the old string data.</param>
        /// <returns><B>True</B> or <B>false</B>.</returns>
        public virtual void replaceData(int offset, int count, string arg)
        {
            if (offset < 0 || count < 0 || offset > length)
            {
                DOMException.Throw(ExceptionCode.IndexOutOfBounds);
            }

            XmlCharacterData.ReplaceData(offset, count, arg);
        }
Example #9
0
        /// <summary>
        /// Removes a range of characters from the node.
        /// </summary>
        /// <param name="offset">The position within the string to start deleting.</param>
        /// <param name="count">The number of characters to delete.</param>
        /// <returns><B>True</B> or <B>false</B>.</returns>
        public virtual void deleteData(int offset, int count)
        {
            if (offset < 0 || count < 0 || offset > XmlCharacterData.Length)
            {
                DOMException.Throw(ExceptionCode.IndexOutOfBounds);
            }

            XmlCharacterData.DeleteData(offset, count);
        }
Example #10
0
        public virtual string substringData(int offset, int count)
        {
            if (offset < 0 || count < 0 || offset > XmlCharacterData.Length)
            {
                DOMException.Throw(ExceptionCode.IndexOutOfBounds);
                return(null);
            }

            return(XmlCharacterData.Substring(offset, count));
        }
Example #11
0
        private protected XmlNode CheckedChildOperation(DOMNode /*!*/ newNode, DOMNode auxNode, NodeAction /*!*/ action)
        {
            newNode.Associate(XmlNode.OwnerDocument != null ? XmlNode.OwnerDocument : (XmlDocument)XmlNode);

            // check for readonly node
            if (XmlNode.IsReadOnly || (newNode.XmlNode.ParentNode != null && newNode.XmlNode.ParentNode.IsReadOnly))
            {
                DOMException.Throw(ExceptionCode.DomModificationNotAllowed);
                return(null);
            }

            // check for owner document mismatch
            if (XmlNode.OwnerDocument != null ?
                XmlNode.OwnerDocument != newNode.XmlNode.OwnerDocument :
                XmlNode != newNode.XmlNode.OwnerDocument)
            {
                DOMException.Throw(ExceptionCode.WrongDocument);
                return(null);
            }

            XmlNode result;

            try
            {
                result = action(newNode, auxNode);
            }
            catch (InvalidOperationException)
            {
                // the current node is of a type that does not allow child nodes of the type of the newNode node
                // or the newNode is an ancestor of this node.
                DOMException.Throw(ExceptionCode.BadHierarchy);
                return(null);
            }
            catch (ArgumentException)
            {
                // check for newNode == this which System.Xml reports as ArgumentException
                if (newNode.XmlNode == XmlNode)
                {
                    DOMException.Throw(ExceptionCode.BadHierarchy);
                }
                else
                {
                    // the refNode is not a child of this node
                    DOMException.Throw(ExceptionCode.NotFound);
                }
                return(null);
            }

            return(result);
        }
Example #12
0
        /// <summary>
        /// Removes an attribute.
        /// </summary>
        /// <param name="namespaceUri">The attribute namespace URI.</param>
        /// <param name="localName">The attribute local name.</param>
        /// <returns><B>True</B> on success, <B>false</B> on failure.</returns>
        public virtual bool removeAttributeNS(string namespaceUri, string localName)
        {
            if (!IsAssociated)
            {
                DOMException.Throw(ExceptionCode.DomModificationNotAllowed);
                return(false);
            }

            XmlAttribute attr = XmlElement.Attributes[localName, namespaceUri];

            if (attr != null)
            {
                XmlElement.Attributes.Remove(attr);
            }

            return(true);
        }
Example #13
0
        /// <summary>
        /// Removes an attribute.
        /// </summary>
        /// <param name="name">The attribute name.</param>
        /// <returns><B>True</B> on success, <B>false</B> on failure.</returns>
        public virtual bool removeAttribute(string name)
        {
            if (!IsAssociated)
            {
                DOMException.Throw(ExceptionCode.DomModificationNotAllowed);
                return(false);
            }

            var attr = XmlElement.Attributes[name];

            if (attr != null)
            {
                XmlElement.Attributes.Remove(attr);
            }

            return(true);
        }
Example #14
0
        private XPathNavigator GetNavigator(DOMNode context)
        {
            if (context == null)
            {
                return(XPathNavigator);
            }
            else
            {
                XmlNode node = context.XmlNode;

                if (node.OwnerDocument != (XmlDocument)XPathNavigator.UnderlyingObject)
                {
                    DOMException.Throw(ExceptionCode.WrongDocument);
                    return(null);
                }

                return(node.CreateNavigator());
            }
        }
Example #15
0
        /// <summary>
        /// Sets the value of a attribute (creates new one if it does not exist).
        /// </summary>
        /// <param name="name">The attribute name.</param>
        /// <param name="value">The attribute value.</param>
        /// <returns><B>True</B> on success, <B>false</B> on failure.</returns>
        public virtual bool setAttribute(string name, string value)
        {
            if (!IsAssociated)
            {
                DOMException.Throw(ExceptionCode.DomModificationNotAllowed);
                return(false);
            }

            XmlAttribute attr = XmlElement.Attributes[name];

            if (attr == null)
            {
                attr = XmlNode.OwnerDocument.CreateAttribute(name);
                XmlElement.Attributes.Append(attr);
            }
            attr.Value = value;

            return(true);
        }
Example #16
0
        /// <summary>
        /// Removes an attribute node from the element.
        /// </summary>
        /// <param name="attribute">The attribute node.</param>
        /// <returns>Old node if the attribute has been removed or <B>null</B>.</returns>
        public virtual DOMAttr removeAttributeNode(DOMAttr attribute)
        {
            if (!IsAssociated)
            {
                DOMException.Throw(ExceptionCode.DomModificationNotAllowed);
                return(null);
            }

            XmlAttribute attr = XmlElement.Attributes[attribute.nodeName];

            if (attr == null)
            {
                DOMException.Throw(ExceptionCode.NotFound);
                return(null);
            }

            XmlElement.Attributes.Remove(attr);

            return(attribute);
        }
Example #17
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);
        }
Example #18
0
        /// <summary>
        /// Creates a new <see cref="DOMDocument"/>.
        /// </summary>
        /// <param name="namespaceUri">The namespace URI of the root element to create.</param>
        /// <param name="qualifiedName">The qualified name of the document element.</param>
        /// <param name="docType">The type of document to be created.</param>
        /// <returns>The <see cref="DOMDocument"/>.</returns>
        public DOMDocument createDocument(string namespaceUri = null, string qualifiedName = null, DOMDocumentType docType = null)
        {
            XmlDocument doc = XmlImplementation.CreateDocument();

            if (docType != null)
            {
                if (!docType.IsAssociated)
                {
                    docType.Associate(doc);
                }
                else
                {
                    DOMException.Throw(ExceptionCode.WrongDocument);
                    return(null);
                }
            }

            doc.AppendChild(docType.XmlNode);
            doc.AppendChild(doc.CreateElement(qualifiedName, namespaceUri));

            return(new DOMDocument(doc));
        }
Example #19
0
 private protected virtual DOMNode CloneObjectInternal(bool deepCopyFields)
 {
     DOMException.Throw(ExceptionCode.InvalidState);
     return(null);
 }