示例#1
0
 private XmlLightNavigator(XmlNameTable names, XmlLightElement current, List <string> attrNames, int attribute)
 {
     _names     = names;
     _current   = current;
     _attrNames = attrNames;
     _attribute = attribute;
 }
示例#2
0
 public override bool MoveTo(XPathNavigator other)
 {
     _current   = ((XmlLightNavigator)other)._current;
     _attrNames = ((XmlLightNavigator)other)._attrNames;
     _attribute = ((XmlLightNavigator)other)._attribute;
     return(true);
 }
		private XmlLightNavigator(XmlNameTable names, XmlLightElement current, List<string> attrNames, int attribute)
		{
			_names = names;
			_current = current;
			_attrNames = attrNames;
			_attribute = attribute;
		}
		public FauxProjectItem(FauxProject project, XmlLightElement item)
		{
			_project = project;
			_item = item;
			_meta = new Dictionary<string, string>(StringComparer.OrdinalIgnoreCase);
			foreach (XmlLightElement meta in _item.Children)
				_meta[meta.LocalName] = meta.InnerText;
		}
示例#5
0
        /// <summary> Ends the processing of an xml tag </summary>
        public virtual void EndTag(XmlTagInfo tag)
        {
            XmlLightElement e = _parserStack.Pop();

            if (e.TagName != tag.FullName)
            {
                throw new XmlException(String.Format("Incorrect tag closed '</{0}>', expected '</{1}>'", tag.FullName, e.TagName));
            }
            e.ClosingTagWhitespace = tag.EndingWhitespace;
        }
		/// <summary> Begins the processing of an xml tag </summary>
		public virtual void StartTag(XmlTagInfo tag)
		{
			XmlLightElement parent = _parserStack.Peek();
			XmlLightElement e = new XmlLightElement(parent, tag);

			if (Root == null && _parserStack.Count == 1)
				Root = e;
			if (tag.SelfClosed == false)
				_parserStack.Push(e);
		}
示例#7
0
 public override bool MoveToFirstChild()
 {
     if (_current.Children.Count > 0)
     {
         _current   = _current.Children[0];
         _attribute = -1;
         return(true);
     }
     return(false);
 }
示例#8
0
        public override bool MoveToId(string id)
        {
            XmlLightElement item = _current.Document.GetElementById(id);

            if (item != null)
            {
                _current   = item;
                _attribute = -1;
                return(true);
            }
            return(false);
        }
示例#9
0
        public override bool MoveToNext()
        {
            XmlLightElement e = _current.NextSibling;

            if (e != null)
            {
                _current   = e;
                _attribute = -1;
                return(true);
            }
            return(false);
        }
示例#10
0
        /// <summary> Ends the processing of an xml input </summary>
        public virtual void EndDocument()
        {
            XmlLightElement e = _parserStack.Pop();

            if (e.TagName != this.TagName)
            {
                throw new XmlException(String.Format("Tag was not closed, expected '</{0}>'", e.TagName));
            }
            else if (this.Root == null)
            {
                throw new XmlException(String.Format("Root element not found."));
            }
        }
		internal XmlLightElement(XmlLightElement parent, bool closed, string tagName, string closingWs, string tagContent, IEnumerable<XmlLightAttribute> attrs)
		{
            _originalTag = tagContent;
			Parent = parent;
            _tagName = tagName;
			OpeningTagWhitespace = closingWs;
			ClosingTagWhitespace = String.Empty;
			IsEmpty = closed;

			if (parent != null)
				parent.Children.Add(this);

			Attributes = new XmlLightAttributes(attrs ?? new XmlLightAttribute[0]);
        }
示例#12
0
        /// <summary> Begins the processing of an xml tag </summary>
        public virtual void StartTag(XmlTagInfo tag)
        {
            XmlLightElement parent = _parserStack.Peek();
            XmlLightElement e      = new XmlLightElement(parent, tag);

            if (Root == null && _parserStack.Count == 1)
            {
                Root = e;
            }
            if (tag.SelfClosed == false)
            {
                _parserStack.Push(e);
            }
        }
        /// <summary>
        /// Finds the elements matching the provided criteria
        /// </summary>
        public IEnumerable <XmlLightElement> FindElement(Predicate <XmlLightElement> match)
        {
            List <XmlLightElement> todo = new List <XmlLightElement>();

            todo.Add(this);
            for (int ix = 0; ix < todo.Count; ix++)
            {
                XmlLightElement test = todo[ix];
                if (match(test))
                {
                    yield return(test);
                }
                todo.AddRange(test.Children);
            }
        }
        internal XmlLightElement(XmlLightElement parent, bool closed, string tagName, string closingWs, string tagContent, IEnumerable <XmlLightAttribute> attrs)
        {
            _originalTag         = tagContent;
            Parent               = parent;
            _tagName             = tagName;
            OpeningTagWhitespace = closingWs;
            ClosingTagWhitespace = String.Empty;
            IsEmpty              = closed;

            if (parent != null)
            {
                parent.Children.Add(this);
            }

            Attributes = new XmlLightAttributes(attrs ?? new XmlLightAttribute[0]);
        }
示例#15
0
        public override bool MoveToParent()
        {
            if (_attribute != -1)
            {
                _attribute = -1;
                _attrNames = null;
                return(true);
            }
            XmlLightElement e = _current.Parent;

            if (e != null)
            {
                _current   = e;
                _attribute = -1;
                return(true);
            }
            return(false);
        }
        private string FindPrefixUri(string nsPrefix)
        {
            if (String.IsNullOrEmpty(nsPrefix))
            {
                return(null);
            }

            string          attr = String.Format("xmlns:{0}", nsPrefix);
            XmlLightElement e    = this;

            while (e != null)
            {
                string value;
                if (e.Attributes.TryGetValue(attr, out value))
                {
                    return(value);
                }
                e = e.Parent;
            }

            return(null);
        }
        /// <summary> </summary>
        public override void StartTag(XmlTagInfo tag)
        {
            if (_nonClosedTags.Contains(tag.FullName))
            {
                tag.SelfClosed = true;
            }

            XmlLightElement parent = _parserStack.Peek();
            List <string>   allowedParents;

            if (_nonNestingTags.Contains(tag.FullName) && StringComparer.OrdinalIgnoreCase.Equals(parent.TagName, tag.FullName))
            {
                _parserStack.Pop();
            }
            else if (_htmlHeirarchy.TryGetValue(tag.FullName, out allowedParents))
            {
                int depth = 0;
                XmlLightElement[] stack = _parserStack.ToArray();
                while (depth < stack.Length && allowedParents.BinarySearch(stack[depth].TagName, StringComparer.OrdinalIgnoreCase) < 0)
                {
                    depth++;
                }

                if (depth < stack.Length)
                {
                    for (; depth > 0; depth--)
                    {
                        _parserStack.Pop();
                    }
                }
                else
                {
                    StartTag(new XmlTagInfo(allowedParents[0], false));
                }
            }

            base.StartTag(tag);
        }
        /// <summary> </summary>
        public override void EndTag(XmlTagInfo tag)
        {
            if (_nonClosedTags.Contains(tag.FullName))
            {
                return;
            }

            XmlLightElement closed = null;

            try
            {
                XmlLightElement[] stack = _parserStack.ToArray();
                if (stack[0].TagName == tag.FullName)
                {
                    closed = _parserStack.Pop();
                    return;
                }

                //closes any tags left open in these elements
                bool found = false;
                for (int i = 0; !found && i < stack.Length; i++)
                {
                    found = found || StringComparer.OrdinalIgnoreCase.Equals(stack[i].TagName, tag.FullName);
                }

                while (found &&
                       StringComparer.OrdinalIgnoreCase.Equals((closed = _parserStack.Pop()).TagName, tag.FullName) == false)
                {
                }
            }
            finally
            {
                if (closed != null && StringComparer.OrdinalIgnoreCase.Equals(closed.TagName, tag.FullName))
                {
                    closed.ClosingTagWhitespace = tag.EndingWhitespace;
                }
            }
        }
示例#19
0
 public XmlLightNavigator(XmlLightElement current)
     : this(new NameTable(), current, null, -1)
 {
 }
示例#20
0
 private static XmlLightElement MakeElement(XmlLightElement parent, string tag, string cls, string text)
 {
     XmlLightElement e= new XmlLightElement(parent, tag);
     e.Attributes["class"] = cls;
     e.IsEmpty = false;
     if (text != null)
         new XmlLightElement(e, XmlLightElement.TEXT).Value = text;
     return e;
 }
		public override bool MoveToId(string id)
		{
			XmlLightElement item = _current.Document.GetElementById(id);
			if (item != null)
			{
				_current = item;
				_attribute = -1;
				return true;
			}
			return false;
		}
示例#22
0
 private static void WriteXmlNode(TextWriter wtr, XmlLightElement root)
 {
     using (XmlWriter xwtr = XmlWriter.Create(wtr, 
         new XmlWriterSettings()
           {
               CheckCharacters = false,
               CloseOutput = false,
               ConformanceLevel = ConformanceLevel.Fragment,
               OmitXmlDeclaration = true,
               Indent = false
           }))
     {
         root.WriteXml(xwtr);
         xwtr.Flush();
     }
 }
 internal XmlLightElement(XmlLightElement parent, bool closed, string tagName, string tagContent)
     : this(parent, closed, tagName, String.Empty, tagContent, null)
 {
 }
        private static void InsertTag(XmlLightElement start, string xpath, ReplaceOption where, string insertName)
        {
            XmlLightElement tag = new XmlLightElement(null, SearchNamespacePrefix + ":" + insertName);
            tag.Attributes["xmlns:" + SearchNamespacePrefix] = SearchNamespaceUri;
            tag.IsEmpty = false;

            XmlLightElement node = start.SelectRequiredNode(xpath);
            if (where == ReplaceOption.Append)
            {
                node.Children.Add(tag);
                tag.Parent = node;
            }
            else if (where == ReplaceOption.Replace)
            {
                int ordinal = node.Parent.Children.IndexOf(node);
                node.Parent.Children[ordinal] = tag;
                tag.Parent = node.Parent;

                node.Parent = null;
                tag.Children.Add(node);
                node.Parent = tag;
            }
            else
                throw new ArgumentOutOfRangeException();
        }
示例#25
0
        /// <summary> Encountered comment in the document </summary>
        public virtual void AddComment(string comment)
        {
            XmlLightElement parent = _parserStack.Peek();

            new XmlLightElement(parent, true, XmlLightElement.COMMENT, comment);
        }
示例#26
0
            XmlLightElement RewriteElement(XmlLightElement e)
            {
                e = ProcessXPaths(e);
                if (e != null && _bytag.Contains(e.TagName))
                {
                    var replaced = e;
                    foreach (var key in _bytag[e.TagName].Where(t => _processor.IsTagMatch(e, t)))
                    {
                        replaced = GetReplacementNode(replaced, key);
                        if (replaced == null)
                            break;
                    }

                    return replaced;
                }
                return e;
            }
 private FauxProject(XmlLightElement doc, string path)
 {
     _doc = doc;
     _path = path;
 }
		public XmlLightNavigator(XmlLightElement current)
			: this(new NameTable(), current, null, -1) { }
示例#29
0
        /// <summary> Encountered text or whitespace in the document </summary>
        public virtual void AddText(string content)
        {
            XmlLightElement parent = _parserStack.Peek();

            new XmlLightElement(parent, true, XmlLightElement.TEXT, content);
        }
		public override bool MoveToNext()
		{
			XmlLightElement e = _current.NextSibling;
			if (e != null)
			{
				_current = e;
				_attribute = -1;
				return true;
			}
			return false;
		}
		public override bool MoveToParent()
		{
			if (_attribute != -1)
			{
				_attribute = -1;
				_attrNames = null;
				return true;
			}
			XmlLightElement e = _current.Parent;
			if (e != null)
			{
				_current = e;
				_attribute = -1;
				return true;
			}
			return false;
		}
		public override bool MoveToFirstChild()
		{
			if (_current.Children.Count > 0)
			{
				_current = _current.Children[0];
				_attribute = -1;
				return true;
			}
			return false;
		}
示例#33
0
        /// <summary> Encountered cdata section in the document </summary>
        public virtual void AddCData(string cdata)
        {
            XmlLightElement parent = _parserStack.Peek();

            new XmlLightElement(parent, true, XmlLightElement.CDATA, cdata);
        }
示例#34
0
		public FauxProject(string file, string projContent)
		{
			_path = Path.GetFullPath(Check.NotEmpty(file));
			Check.Assert<FileNotFoundException>(File.Exists(_path));
			_doc = new XmlLightDocument(projContent).Root;
		}
        private string CreateTemplate(string html)
        {
            HtmlLightDocument doc = new HtmlLightDocument(html);

            //Add css link:

            XmlLightElement cssLink = new XmlLightElement(doc.SelectRequiredNode("/html/head"), "link");
            cssLink.Attributes["type"] = "text/css";
            cssLink.Attributes["rel"] = "stylesheet";
            cssLink.Attributes["href"] = new Uri(_baseUri, "search.css").AbsoluteUri;

            XmlLightElement startFrom = doc.Root;
            if (_config.Searching.XPathBase != null)
                startFrom = startFrom.SelectRequiredNode(_config.Searching.XPathBase.XPath);

            if(_config.Searching.FormXPath != null)
            {
                XmlLightElement form = startFrom.SelectRequiredNode(_config.Searching.FormXPath.XPath);
                foreach (XmlLightElement textbox in form.Select(".//input[@type='text']"))
                    textbox.Attributes["value"] = String.Empty;
            }
            if(_config.Searching.TermsXPath != null)
            {
                InsertTag(startFrom, _config.Searching.TermsXPath.XPath, _config.Searching.TermsXPath.ReplaceOption, "search-terms");
            }

            if (_config.Searching.ResultXPath != null)
            {
                InsertTag(startFrom, _config.Searching.ResultXPath.XPath, _config.Searching.ResultXPath.ReplaceOption, "search-result");
            }

            using (StringWriter sw = new StringWriter())
            {
                doc.WriteUnformatted(sw);
                return sw.ToString();
            }
        }
示例#36
0
        /// <summary> Encountered control information &lt;! ... &gt; in the document </summary>
        public virtual void AddControl(string ctrl)
        {
            XmlLightElement parent = _parserStack.Peek();

            new XmlLightElement(parent, true, XmlLightElement.CONTROL, ctrl);
        }
		internal XmlLightElement(XmlLightElement parent, bool closed, string tagName, string tagContent)
			: this(parent, closed, tagName, String.Empty, tagContent, null)
		{ }
示例#38
0
		public void TestDocToXml()
		{
			HtmlLightDocument doc = new HtmlLightDocument();
			XmlLightElement body = new XmlLightElement(new XmlLightElement(doc, "html"), "body");
            body.IsEmpty = false;
            body.Attributes.Add("id", "bdy");
			Assert.AreEqual("<html> <body id=\"bdy\"> </body> </html>", Normalize(doc.InnerXml));
		}
    	/// <summary>
		/// Creates a new xml element
		/// </summary>
		public XmlLightElement(XmlLightElement parent, string tagName)
			: this(parent, true, tagName, String.Empty)
		{ }
示例#40
0
        public void TestManuallyCreated()
        {
            XmlLightElement root = new XmlLightElement(null, "root");
            new XmlLightElement(root, "a").Attributes["b"] = "c";
            new XmlLightElement(root, XmlLightElement.TEXT).Value = "Normal & <Encoded> Text";
            new XmlLightElement(root, XmlLightElement.COMMENT).OriginalTag = "<!-- This is just a <simple> comment. -->";
            new XmlLightElement(root, XmlLightElement.CONTROL){
                OriginalTag = "<? Hey, that isn't valid !>"
            }.Remove();

            StringWriter sw = new StringWriter();
            root.WriteUnformatted(sw);
            Assert.AreEqual("<root><a b=\"c\"/>Normal &amp; &lt;Encoded&gt; Text<!-- This is just a <simple> comment. --></root>", sw.ToString());
        }
 /// <summary>
 /// Creates a new xml element
 /// </summary>
 public XmlLightElement(XmlLightElement parent, string tagName)
     : this(parent, true, tagName, String.Empty)
 {
 }
示例#42
0
		public void TestXmlElement()
		{
			XmlLightDocument doc = new HtmlLightDocument(document);
			Assert.IsNull(doc.PrevSibling);
			Assert.IsNull(doc.Children[0].PrevSibling);
			Assert.IsNull(doc.NextSibling);
			Assert.IsNull(doc.Children[doc.Children.Count - 1].NextSibling);

			XmlLightElement e = doc.SelectSingleNode("/html/body//*[@class='2']");
			Assert.IsNotNull(e);
			Assert.AreEqual("p", e.TagName);
			Assert.IsNotNull(e.PrevSibling);
			Assert.AreEqual("p", e.PrevSibling.TagName);

			Assert.AreEqual("", e.Namespace);
			Assert.AreEqual("p", e.LocalName);

			e = new XmlLightElement(null, "a:b");
			Assert.AreEqual("a", e.Namespace);
			Assert.AreEqual("b", e.LocalName);
		}
 internal XmlLightElement(XmlLightElement parent, XmlTagInfo tag)
     : this(parent, tag.SelfClosed, tag.FullName, tag.EndingWhitespace, tag.UnparsedTag, tag.Attributes)
 {
 }
		internal XmlLightElement(XmlLightElement parent, XmlTagInfo tag)
			: this(parent, tag.SelfClosed, tag.FullName, tag.EndingWhitespace, tag.UnparsedTag, tag.Attributes)
		{ }
示例#45
0
            XmlLightElement GetReplacementNode(XmlLightElement e, object key)
            {
                HttpCloneOptimizationReplace replacement = _replaces[key];
                if (String.IsNullOrEmpty(replacement.ReplacementValue))
                    return null;

                string replaceText = replacement.ReplacementValue;
                if (replacement.ExpandValue)
                {
                    replaceText = RegexPatterns.FormatNameSpecifier.Replace(replaceText,
                        m =>
                        {
                            string tmp;
                            if (_namedValues.TryGetValue(m.Groups["field"].Value, out tmp)
                                || e.Attributes.TryGetValue(m.Groups["field"].Value, out tmp))
                                return tmp;
                            return m.Value;
                        });
                }

                XmlLightElement newElem = new XmlLightDocument(replaceText).Root;
                newElem.Parent = null;
                return newElem;
            }
		public override bool MoveTo(XPathNavigator other)
		{
			_current = ((XmlLightNavigator)other)._current;
			_attrNames = ((XmlLightNavigator)other)._attrNames;
			_attribute = ((XmlLightNavigator)other)._attribute;
			return true;
		}
示例#47
0
            private XmlLightElement ProcessXPaths(XmlLightElement e)
            {
                object foundBy;
                if (_elements.TryGetValue(e, out foundBy))
                {
                    return GetReplacementNode(e, foundBy);
                }

                return e;
            }
示例#48
0
        /// <summary> Encountered processing instruction &lt;? ... ?&gt; in the document </summary>
        public virtual void AddInstruction(string instruction)
        {
            XmlLightElement parent = _parserStack.Peek();

            new XmlLightElement(parent, true, XmlLightElement.PROCESSING, instruction);
        }