示例#1
0
        public void TestCurrent()
        {
            CMLStack stack = new CMLStack();

            stack.Push("first");
            Assert.AreEqual("first", stack.Peek());
            stack.Push("second");
            Assert.AreEqual("second", stack.Peek());
            stack.Push("third");
            Assert.AreEqual("third", stack.Peek());
            stack.Pop();
            Assert.AreEqual("second", stack.Peek());
            stack.Pop();
            Assert.AreEqual("first", stack.Peek());
        }
示例#2
0
        public override void StartElement(XElement element)
        {
            var uri   = element.Name.NamespaceName;
            var local = element.Name.LocalName;

            xpath.Push(element.Name.LocalName);
            Debug.WriteLine($"<{element.Value}> -> {xpath}");
            // Detect CML modules, like CRML and CCML
            if (local.StartsWith("reaction", StringComparison.Ordinal))
            {
                // e.g. reactionList, reaction -> CRML module
                Trace.TraceInformation("Detected CRML module");
                if (!string.Equals(conventionStack.Peek(), "CMLR", StringComparison.Ordinal))
                {
                    conv = new CMLReactionModule(conv);
                }
                conventionStack.Push("CMLR");
            }
            else if (string.IsNullOrEmpty(uri) || uri.StartsWith("http://www.xml-cml.org/", StringComparison.Ordinal))
            {
                // assume CML Core

                // Detect conventions
                string convName = "";
                if (element.Attribute(Attribute_convention) != null)
                {
                    convName = element.Attribute(Attribute_convention).Value;
                }
                if (convName.Length == 0)
                {
                    // no convention set/reset: take convention of parent
                    conventionStack.Push(conventionStack.Peek());
                }
                else
                if (convName.Length > 0)
                {
                    if (convName.Equals(conventionStack.Peek(), StringComparison.Ordinal))
                    {
                        Debug.WriteLine("Same convention as parent");
                    }
                    else
                    {
                        Trace.TraceInformation($"New Convention: {convName}");
                        switch (convName)
                        {
                        case "CML":
                            // Don't reset the convention handler to CMLCore,
                            // becuase all handlers should extend this handler, and
                            // use it for any content other then specifically put
                            // into the specific convention
                            break;

                        case "PDB":
                            conv = new PDBConvention(conv);
                            break;

                        case "PMP":
                            conv = new PMPConvention(conv);
                            break;

                        case "MDLMol":
                            Debug.WriteLine("MDLMolConvention instantiated...");
                            conv = new MDLMolConvention(conv);
                            break;

                        case "JMOL-ANIMATION":
                            conv = new JMOLANIMATIONConvention(conv);
                            break;

                        default:
                            if (userConventions.ContainsKey(convName))
                            {
                                //unknown convention. userConvention?
                                var newconv = userConventions[convName];
                                newconv.Inherit(conv);
                                conv = newconv;
                            }
                            else
                            {
                                Trace.TraceWarning($"Detected unknown convention: {convName}");
                            }
                            break;
                        }
                    }
                    conventionStack.Push(convName);
                }
                else
                {
                    // no convention set/reset: take convention of parent
                    conventionStack.Push(conventionStack.Peek());
                }
            }
            else
            {
                conv = new OtherNamespace();
                conventionStack.Push("Other");
            }
            moduleStack.Push(conv);
            Debug.WriteLine($"ConventionStack: {conventionStack}");
            conv.StartElement(xpath, element);
        }