Esempio n. 1
0
        /// <summary>
        /// Gets the singleton instance
        /// </summary>
        /// <returns></returns>
        public static PropertyVP Singleton()
        {
            if (singleton == null)
                singleton = new PropertyVP();

            return singleton;
        }
Esempio n. 2
0
        //_________Load_________
        /// <summary>
        /// Parses an XML file to a QuicDocument object.
        /// </summary>
        /// <param name="filePath"></param>
        /// <returns></returns>
        public static QuicDocument Load(string filePath) 
        {
            try
            {
                XDocument xDoc = XDocument.Load(filePath, LoadOptions.SetLineInfo);

                QuicDocument doc = new QuicDocument();
                doc.SourcePath = filePath;
                doc.internalXDoc = xDoc;
                Environment.CurrentDirectory = new FileInfo(doc.SourcePath).Directory.FullName;

                //add default property provider
                PropertyVP pp = new PropertyVP();
                //pp.Key = "$";
                pp.Document = doc;
                doc.resDic.Add("$", pp);

                XElement rootTag = xDoc.Root;
                if(rootTag == null || rootTag.Name.LocalName != "html")
                    throw new QuicException("Document does not contain a root <Quic> tag.", filePath);

                XAttribute attri = rootTag.Attribute("AllowUnknownAttributes");
                if (attri != null)
                {
                    string value = attri.Value;
                    doc.OutputOptions.AllowUnknownAttributes = (bool)BoolVP.Singleton().Evaluate(value);
                }
                attri = rootTag.Attribute("AllowUnknownTags");
                if (attri != null)
                {
                    string value = attri.Value;
                    doc.OutputOptions.AllowUnknownTags = (bool)BoolVP.Singleton().Evaluate(value);
                }
                attri = rootTag.Attribute("IgnoreAttributeCase");
                if (attri != null)
                {
                    string value = attri.Value;
                    doc.OutputOptions.IgnoreAttributeCase = (bool)BoolVP.Singleton().Evaluate(value);
                }
                attri = rootTag.Attribute("IgnoreTagCase");
                if (attri != null)
                {
                    string value = attri.Value;
                    doc.OutputOptions.IgnoreTagCase = (bool)BoolVP.Singleton().Evaluate(value);
                }

                //parse head
                XElement headTag = null;
                var defNamespace = rootTag.GetDefaultNamespace();
                string defNamespaceUri = defNamespace != null ? "{" + defNamespace.NamespaceName + "}" : "";
                List<XElement> allHeadTags = rootTag.Elements(defNamespaceUri + "head").ToList();
                if (allHeadTags.Count > 1)
                {
                    var lineInfo = (IXmlLineInfo)allHeadTags[1]; //point to the second <head> tag
                    throw new QuicException("Document cannot have more than one <head> tag.", filePath, 
                        lineInfo.LineNumber, lineInfo.LinePosition);
                }
                else if (allHeadTags.Count == 1)
                {
                    headTag = allHeadTags[0];
                }
                if (headTag != null)
                {
                    foreach (XElement resTag in headTag.Elements())
                    {
                        var headElement = /*(ResourceElement)*/doc.BuildElement(resTag);
                        if (headElement is IResource)
                            if (!string.IsNullOrWhiteSpace(((IResource)headElement).Key))
                                doc.resDic.Add(((IResource)headElement).Key, (IResource)headElement);
                        doc.headElements.Add(headElement);
                    }
                }

                //parse body
                XElement bodyTag = null;
                List<XElement> allBodyTags = rootTag.Elements(defNamespaceUri + "body").ToList();
                if (allBodyTags.Count > 1)
                {
                    var lineInfo = (IXmlLineInfo)allBodyTags[1]; //point to the second <body> tag
                    throw new QuicException("Document cannot have more than one <body> tag.",
                                filePath, lineInfo.LineNumber, lineInfo.LinePosition);
                }
                else if (allBodyTags.Count == 1)
                {
                    bodyTag = allBodyTags[0];
                }
                if (bodyTag != null)
                {
                    foreach (XNode tag in bodyTag.Nodes())
                    {
                        var bodyElement = /*(UIElement)*/doc.BuildElement(tag);
                        doc.bodyElements.Add(bodyElement);
                    }
                }

                return doc;
            }
            catch (QuicException)
            {
                throw;
            }
            catch (XmlException ex)
            {
                throw new QuicException(ex.Message, filePath, ex.LineNumber, ex.LinePosition, ex);
            }
            catch (Exception ex)
            {
                throw new QuicException(ex.Message, filePath, ex);
            }
        }