Exemple #1
0
        private void ReadXmlDocument(XmlDocument xmlDoc)
        {
            XmlNode rootXmlNode = xmlDoc.DocumentElement;

            //make sure it is actually an xml node
            if (rootXmlNode.NodeType == XmlNodeType.Element)
            {
                //eat up the name of that xml node
                string strElementName = rootXmlNode.Name;
                if ("bulletml" != strElementName)
                {
                    //The first node HAS to be bulletml
                    throw new Exception("Error reading \"" + Filename + "\": XML root node needs to be \"bulletml\", found \"" + strElementName + "\" instead");
                }

                //Create the root node of the bulletml tree
                RootNode = new BulletMLNode(ENodeName.bulletml);

                //Read in the whole bulletml tree
                RootNode.Parse(rootXmlNode, null);

                //Find what kind of pattern this is: horizontal or vertical
                XmlNamedNodeMap mapAttributes = rootXmlNode.Attributes;
                for (int i = 0; i < mapAttributes.Count; i++)
                {
                    //will only have the name attribute
                    string strName  = mapAttributes.Item(i).Name;
                    string strValue = mapAttributes.Item(i).Value;
                    if ("type" == strName)
                    {
                        //if  this is a top level node, "type" will be veritcal or horizontal
                        Orientation = StringToPatternType(strValue);
                    }
                }
            }
        }
Exemple #2
0
        /// <summary>
        /// Parse the specified bulletNodeElement.
        /// Read all the data from the xml node into this dude.
        /// </summary>
        /// <param name="bulletNodeElement">Bullet node element.</param>
        public void Parse(XmlNode bulletNodeElement, BulletMLNode parentNode)
        {
            // Handle null argument.
            if (null == bulletNodeElement)
            {
                throw new ArgumentNullException("bulletNodeElement");
            }

            //grab the parent node
            Parent = parentNode;

            //Parse all our attributes
            XmlNamedNodeMap mapAttributes = bulletNodeElement.Attributes;

            for (int i = 0; i < mapAttributes.Count; i++)
            {
                string strName  = mapAttributes.Item(i).Name;
                string strValue = mapAttributes.Item(i).Value;

                if ("type" == strName)
                {
                    //skip the type attribute in top level nodes
                    if (ENodeName.bulletml == Name)
                    {
                        continue;
                    }

                    //get the bullet node type
                    NodeType = BulletMLNode.StringToType(strValue);
                }
                else if ("label" == strName)
                {
                    //label is just a text value
                    Label = strValue;
                }
            }

            //parse all the child nodes
            if (bulletNodeElement.HasChildNodes)
            {
                for (XmlNode childNode = bulletNodeElement.FirstChild;
                     null != childNode;
                     childNode = childNode.NextSibling)
                {
                    //if the child node is a text node, parse it into this dude
                    if (XmlNodeType.Text == childNode.NodeType)
                    {
                        //Get the text of the child xml node, but store it in THIS bullet node
                        NodeEquation.Parse(childNode.Value);
                        continue;
                    }
                    else if (XmlNodeType.Comment == childNode.NodeType)
                    {
                        //skip any comments in the bulletml script
                        continue;
                    }

                    //create a new node
                    BulletMLNode childBulletNode = NodeFactory.CreateNode(BulletMLNode.StringToName(childNode.Name));

                    //read in the node and store it
                    childBulletNode.Parse(childNode, this);
                    ChildNodes.Add(childBulletNode);
                }
            }
        }
        /// <summary>
        /// Parses a bulletml document into this bullet pattern
        /// </summary>
        /// <param name="xmlFileName">Xml file name.</param>
        public void ParseXML(string xmlFileName, XmlReader reader)
        {
            try
              {
            //Open the file.
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(reader);
            XmlNode rootXmlNode = xmlDoc.DocumentElement;

            //make sure it is actually an xml node
            if (rootXmlNode.NodeType == XmlNodeType.Element)
            {
              //eat up the name of that xml node
              string strElementName = rootXmlNode.Name;
              if ("bulletml" != strElementName)
              {
            //The first node HAS to be bulletml
            throw new Exception("Error reading \"" + xmlFileName + "\": XML root node needs to be \"bulletml\", found \"" + strElementName + "\" instead");
              }

              //Create the root node of the bulletml tree
              RootNode = new BulletMLNode(ENodeName.bulletml);

              //Read in the whole bulletml tree
              RootNode.Parse(rootXmlNode, null);

              //Find what kind of pattern this is: horizontal or vertical
              XmlNamedNodeMap mapAttributes = rootXmlNode.Attributes;
              for (int i = 0; i < mapAttributes.Count; i++)
              {
            //will only have the name attribute
            string strName = mapAttributes.Item(i).Name;
            string strValue = mapAttributes.Item(i).Value;
            if ("type" == strName)
            {
              //if  this is a top level node, "type" will be veritcal or horizontal
              Orientation = StringToPatternType(strValue);
            }
              }
            }
              }
              catch (Exception ex)
              {
            //an error ocurred reading in the tree
            throw new Exception("Error reading \"" + xmlFileName + "\"", ex);
              }

              //grab that filename
              Filename = xmlFileName;

              //validate that the bullet nodes are all valid
              try
              {
            RootNode.ValidateNode();
              }
              catch (Exception ex)
              {
            //an error ocurred reading in the tree
            throw new Exception("Error reading \"" + xmlFileName + "\"", ex);
              }
        }
Exemple #4
0
        internal void ParseXmlStream(Stream xmlStream)
        {
            // initialise schema for validating the document
            if (schema == null)
            {
                using (Stream stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("BulletMLLib.Content.bulletml.xsd"))
                {
                    if (stream == null)
                    {
                        throw new InvalidBulletPatternException("Could not find XML schema.");
                    }
                    schema = XmlSchema.Read(stream, null);
                }
            }

            XmlReaderSettings settings = new XmlReaderSettings();

            settings.DtdProcessing  = DtdProcessing.Ignore;
            settings.ValidationType = ValidationType.Schema;
            settings.Schemas.Add(schema);

            using (XmlReader reader = XmlReader.Create(xmlStream, settings))
            {
                try
                {
                    //Open the file.
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(reader);
                    XmlNode rootXmlNode = xmlDoc.DocumentElement;

                    //make sure it is actually an xml node
                    if (rootXmlNode.NodeType == XmlNodeType.Element)
                    {
                        //eat up the name of that xml node
                        string strElementName = rootXmlNode.Name;
                        if ("bulletml" != strElementName)
                        {
                            //The first node HAS to be bulletml
                            throw new InvalidBulletPatternException("Root XML element should be '<bulletml>'.");
                        }

                        //Create the root node of the bulletml tree
                        RootNode = new BulletMLNode(ENodeName.bulletml);

                        //Read in the whole bulletml tree
                        RootNode.Parse(rootXmlNode, null);

                        //Find what kind of pattern this is: horizontal or vertical
                        XmlNamedNodeMap mapAttributes = rootXmlNode.Attributes;
                        for (int i = 0; i < mapAttributes.Count; i++)
                        {
                            //will only have the name attribute
                            string strName  = mapAttributes.Item(i).Name;
                            string strValue = mapAttributes.Item(i).Value;
                            if ("type" == strName)
                            {
                                //if  this is a top level node, "type" will be veritcal or horizontal
                                Orientation = StringToPatternType(strValue);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    //an error ocurred reading in the tree
                    throw new InvalidBulletPatternException("Could not read XML.", ex);
                }
            }

            //validate that the bullet nodes are all valid
            try
            {
                RootNode.ValidateNode();
            }
            catch (Exception ex)
            {
                //an error ocurred reading in the tree
                throw new InvalidBulletPatternException("XML did not validate.", ex);
            }
        }
        /// <summary>
        /// Parses a bulletml document into this bullet pattern
        /// </summary>
        /// <param name="xmlFileName">Xml file name.</param>
        public void ParseXML(string xmlFileName)
        {
#if NETFX_CORE
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.DtdProcessing = DtdProcessing.Ignore;
#else
            XmlReaderSettings settings = new XmlReaderSettings();
            settings.ValidationType          = ValidationType.None;
            settings.DtdProcessing           = DtdProcessing.Parse;
            settings.ValidationEventHandler += new ValidationEventHandler(MyValidationEventHandler);
#endif
            try
            {
                using (XmlReader reader = XmlReader.Create(xmlFileName, settings))
                {
                    //Open the file.
                    XmlDocument xmlDoc = new XmlDocument();
                    xmlDoc.Load(reader);
                    XmlNode rootXmlNode = xmlDoc.DocumentElement;

                    //make sure it is actually an xml node
                    if (rootXmlNode.NodeType == XmlNodeType.Element)
                    {
                        //eat up the name of that xml node
                        string strElementName = rootXmlNode.Name;
                        if ("bulletml" != strElementName)
                        {
                            //The first node HAS to be bulletml
                            throw new Exception("Error reading \"" + xmlFileName + "\": XML root node needs to be \"bulletml\", found \"" + strElementName + "\" instead");
                        }

                        //Create the root node of the bulletml tree
                        RootNode = new BulletMLNode(ENodeName.bulletml);

                        //Read in the whole bulletml tree
                        RootNode.Parse(rootXmlNode, null);

                        //Find what kind of pattern this is: horizontal or vertical
                        XmlNamedNodeMap mapAttributes = rootXmlNode.Attributes;
                        for (int i = 0; i < mapAttributes.Count; i++)
                        {
                            //will only have the name attribute
                            string strName  = mapAttributes.Item(i).Name;
                            string strValue = mapAttributes.Item(i).Value;
                            if ("type" == strName)
                            {
                                //if  this is a top level node, "type" will be veritcal or horizontal
                                Orientation = StringToPatternType(strValue);
                            }
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                //an error ocurred reading in the tree
                throw new Exception("Error reading \"" + xmlFileName + "\"", ex);
            }

            //grab that filename
            Filename = xmlFileName;

            //validate that the bullet nodes are all valid
            try
            {
                RootNode.ValidateNode();
            }
            catch (Exception ex)
            {
                //an error ocurred reading in the tree
                throw new Exception("Error reading \"" + xmlFileName + "\"", ex);
            }
        }