public void TestConvertXmlDocument()
        {
            StringBuilder xml = new StringBuilder();

            xml.Append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
            xml.Append("<!--document comment-->");
            xml.Append("<root>");
            xml.Append("<child1 a1=\"val1\" a2=\"val2\">");
            xml.Append("<!--first comment-->");
            xml.Append("child1 value");
            xml.Append("<!--second comment-->");
            xml.Append("</child1>");
            xml.Append("<child2>");
            xml.Append("<![CDATA[cdata value ]]>");
            xml.Append("child2 value");
            xml.Append("</child2>");
            xml.Append("</root>");
            XmlDocument xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(xml.ToString());

            Assert.IsNotNull(xmlDoc);
            IXmlDocument convXmlDoc = XmlHelper.ConvertDocument(xmlDoc);

            Assert.IsNotNull(convXmlDoc);

            Assert.AreEqual(convXmlDoc.Encoding, "utf-8");
            Assert.IsNull(convXmlDoc.DtdName);
            Assert.IsNull(convXmlDoc.DtdUri);
            Assert.AreEqual(convXmlDoc.DocumentComment, "document comment");
            Assert.AreEqual(convXmlDoc.Name, "root");
            Assert.AreEqual(convXmlDoc.ElementList.Count, 2);

            IXmlElement child1 = convXmlDoc.GetElement("child1");

            Assert.IsNotNull(child1);
            Assert.AreEqual(child1.Attributes.Count, 2);
            Assert.IsTrue(child1.Attributes.Contains("a1"));
            Assert.AreEqual(child1.GetAttribute("a1").GetString(), "val1");
            Assert.IsTrue(child1.Attributes.Contains("a2"));
            Assert.AreEqual(child1.GetAttribute("a2").GetString(), "val2");
            Assert.AreEqual(child1.GetString(), "child1 value");
            Assert.AreEqual(child1.Comment, "first comment\nsecond comment");

            IXmlElement child2 = convXmlDoc.GetElement("child2");

            Assert.IsNotNull(child2);
            Assert.AreEqual(child2.Attributes.Count, 0);
            Assert.AreEqual(child2.GetString(), "cdata value child2 value");
            Assert.IsTrue(child2.Comment.Length == 0);
        }
Esempio n. 2
0
        public override IDesignModel GenerateDesignModel(IXmlElement xmlElement, DesignModelParseContext context)
        {
            var namespaceName = xmlElement.GetAttribute("name")?.Value;

            if (string.IsNullOrWhiteSpace(namespaceName))
            {
                throw new DesignModelException(context.DesignModel, "Namespace name not set");
            }

            var ns = context.Collection.DefineNamespace(namespaceName);

            context.Namespace = ns;

            return(null);
        }
Esempio n. 3
0
 /// <summary>
 /// Returns a string attribute value.
 /// </summary>
 /// <param name="element"></param>
 /// <param name="name"></param>
 /// <param name="defaultValue"></param>
 /// <returns></returns>
 public static string GetStringAttributeValue(this IXmlElement element, string name, string defaultValue = null)
 {
     return(element.GetAttribute(name)?.Value ?? defaultValue);
 }
Esempio n. 4
0
        /// <summary>
        /// Preprocess the Coherence properties specified either in the
        /// application configuration or environment variables.
        /// When both are specified, environment varialbe takes the precedence.
        /// </summary>
        /// <param name="xmlElement">The XML element to process.</param>
        /// <since>coherence 12.2.1.0.1</since>
        public static void PreprocessProp(IXmlElement xmlElement)
        {
            IXmlValue attr = xmlElement.GetAttribute("system-property");

            if (attr != null)
            {
                string property = attr.GetString();
                string val      = ConfigurationUtils.GetProperty(property, null);
                if (val != null)
                {
                    if (xmlElement.Value is Int32)
                    {
                        xmlElement.SetInt(Int32.Parse(val));
                    }
                    else
                    {
                        xmlElement.SetString(val);
                    }
                }
                xmlElement.Attributes.Remove("system-property");
            }

            string value          = xmlElement.Value.ToString();
            string newValue       = null;
            int    next           = value.IndexOf("${");
            int    i              = next + 2;
            bool   processedParam = false;

            while (next >= 0)
            {
                processedParam = true;
                string   curParam = value.Substring(i, value.IndexOf('}', i) - i);
                string[] entry    = curParam.Split(' ');
                string   property = entry[0];

                string val = ConfigurationUtils.GetProperty(property, null);
                if (val == null)
                {
                    newValue += entry[1];
                }
                else
                {
                    newValue += val;
                }

                next = value.IndexOf("${", i);
                int start = value.IndexOf('}', i) + 1;
                if (next > 0)
                {
                    newValue += value.Substring(start, next - start);
                    i         = next + 2;
                }
                else
                {
                    i = start;
                }
            }
            if (processedParam)
            {
                if (i < value.Length)
                {
                    newValue += value.Substring(i);
                }
                xmlElement.SetString(newValue);
            }
        }