コード例 #1
0
        public static void DumpAttribute(DTDAttribute attr)
        {
            Console.Write(attr.Name + " ");
            if (attr.Type is string)
            {
                Console.Write(attr.Type);
            }
            else if (attr.Type is DTDEnumeration)
            {
                Console.Write("(");

                List <string> items   = ((DTDEnumeration)attr.Type).Items;
                bool          isFirst = true;
                foreach (string dtditem in items)
                {
                    if (!isFirst)
                    {
                        Console.Write(",");
                    }
                    isFirst = false;
                    Console.Write(dtditem);
                }
                Console.Write(")");
            }
            else if (attr.Type is DTDNotationList)
            {
                Console.Write("Notation (");

                List <string> items   = ((DTDNotationList)attr.Type).Items;
                bool          isFirst = true;
                foreach (string dtditem in items)
                {
                    if (!isFirst)
                    {
                        Console.Write(",");
                    }
                    isFirst = false;
                    Console.Write(dtditem);
                }
                Console.Write(")");
            }

            if (attr.Decl != null)
            {
                Console.Write(" " + attr.Decl.Name);
            }

            if (attr.DefaultValue != null)
            {
                Console.Write(" " + attr.DefaultValue);
            }

            Console.WriteLine();
        }
コード例 #2
0
        protected void ParseAttdef(Scanner scanner, DTDElement element, DTDAttlist attlist)
        {
            Token token = Expect(Scanner.IDENTIFIER);

            DTDAttribute attr = new DTDAttribute(token.Value);

            attlist.Attributes.Add(attr);

            element.Attributes[token.Value] = attr;

            token = scanner.Get();

            if (token.Type == Scanner.IDENTIFIER)
            {
                if (token.Value.Equals("NOTATION"))
                {
                    attr.Type = ParseNotationList();
                }
                else
                {
                    attr.Type = token.Value;
                }
            }
            else if (token.Type == Scanner.LPAREN)
            {
                attr.Type = ParseEnumeration();
            }

            token = scanner.Peek();

            if (token.Type == Scanner.IDENTIFIER)
            {
                scanner.Get();
                if (token.Value.Equals("#FIXED"))
                {
                    attr.Decl = DTDDecl.FIXED;

                    token             = scanner.Get();
                    attr.DefaultValue = token.Value;
                }
                else if (token.Value.Equals("#REQUIRED"))
                {
                    attr.Decl = DTDDecl.REQUIRED;
                }
                else if (token.Value.Equals("#IMPLIED"))
                {
                    attr.Decl = DTDDecl.IMPLIED;
                }
                else
                {
                    throw new DTDParseException(scanner.GetUriId(),
                                                "Invalid token in attribute declaration: " +
                                                token.Value, scanner.GetLineNumber(), scanner.GetColumn());
                }
            }
            else if (token.Type == Scanner.STRING)
            {
                scanner.Get();
                attr.Decl         = DTDDecl.VALUE;
                attr.DefaultValue = token.Value;
            }
        }
コード例 #3
0
        public static void TestParseDtd(string file, bool writeToLog)
        {
            try
            {
                DTDParser parser = null;
                // MAW Version 1.17
                // If it looks like the filename may be a URL, use the URL class
                if (file.IndexOf("://") > 0)
                {
                    parser = new DTDParser(new Uri(file), true);
                }
                else
                {
                    parser = new DTDParser(file, true);
                }


                // Parse the DTD and ask the parser to guess the root element
                DTD dtd = parser.Parse(true);

                FileStream   ostrm  = null;
                StreamWriter writer = null;
                TextWriter   oldOut = null;

                if (writeToLog)
                {
                    oldOut = Console.Out;
                    ostrm  = new FileStream("./log.txt", FileMode.OpenOrCreate, FileAccess.Write);
                    writer = new StreamWriter(ostrm);
                    Console.SetOut(writer);
                }
                if (dtd.RootElement != null)
                {
                    Console.WriteLine("Root element is probably: " +
                                      dtd.RootElement.Name);
                }

                foreach (DictionaryEntry de in dtd.Elements)
                {
                    DTDElement elem = (DTDElement)de.Value;

                    Console.WriteLine("Element: " + elem.Name);
                    Console.Write("   Content: ");
                    DumpDtdItem(elem.Content);
                    Console.WriteLine();

                    if (elem.Attributes.Count > 0)
                    {
                        Console.WriteLine("   Attributes: ");
                        foreach (DictionaryEntry attr_de in elem.Attributes)
                        {
                            Console.Write("        ");
                            DTDAttribute attr = (DTDAttribute)attr_de.Value;
                            DumpAttribute(attr);
                        }
                        Console.WriteLine();
                    }
                }


                foreach (DictionaryEntry de in dtd.Entities)
                {
                    DTDEntity entity = (DTDEntity)de.Value;

                    if (entity.IsParsed)
                    {
                        Console.Write("Parsed ");
                    }

                    Console.WriteLine("Entity: " + entity.Name);

                    if (entity.Value != null)
                    {
                        Console.WriteLine("    Value: " + entity.Value);
                    }

                    if (entity.ExternalId != null)
                    {
                        if (entity.ExternalId is DTDSystem)
                        {
                            Console.WriteLine("    System: " + entity.ExternalId.System);
                        }
                        else
                        {
                            DTDPublic pub = (DTDPublic)entity.ExternalId;
                            Console.WriteLine("    Public: " + pub.Pub + " " + pub.System);
                        }
                    }

                    if (entity.Ndata != null)
                    {
                        Console.WriteLine("    NDATA " + entity.Ndata);
                    }
                }
                foreach (DictionaryEntry de in dtd.Notations)
                {
                    DTDNotation notation = (DTDNotation)de.Value;

                    Console.WriteLine("Notation: " + notation.Name);

                    if (notation.ExternalId != null)
                    {
                        if (notation.ExternalId is DTDSystem)
                        {
                            Console.WriteLine("    System: " +
                                              notation.ExternalId.System);
                        }
                        else
                        {
                            DTDPublic pub = (DTDPublic)notation.ExternalId;

                            Console.Write("    Public: " +
                                          pub.Pub + " ");
                            if (pub.System != null)
                            {
                                Console.WriteLine(pub.System);
                            }
                            else
                            {
                                Console.WriteLine();
                            }
                        }
                    }
                }
                if (writeToLog)
                {
                    Console.SetOut(oldOut);
                    writer.Close();
                    ostrm.Close();
                }
            }
            catch (Exception exc)
            {
                Trace.WriteLine(exc.StackTrace);
                Console.WriteLine(exc.Message);
            }


            Console.WriteLine("Done");
            Console.ReadKey(); //keep the console open
        }
コード例 #4
0
        public override bool Equals(object ob)
        {
            if (ob == this)
            {
                return(true);
            }
            if (!(ob is DTDAttribute))
            {
                return(false);
            }

            DTDAttribute other = (DTDAttribute)ob;

            if (Name == null)
            {
                if (other.Name != null)
                {
                    return(false);
                }
            }
            else
            {
                if (!Name.Equals(other.Name))
                {
                    return(false);
                }
            }

            if (Type == null)
            {
                if (other.Type != null)
                {
                    return(false);
                }
            }
            else
            {
                if (!Type.Equals(other.Type))
                {
                    return(false);
                }
            }

            if (Decl == null)
            {
                if (other.Decl != null)
                {
                    return(false);
                }
            }
            else
            {
                if (!Decl.Equals(other.Decl))
                {
                    return(false);
                }
            }

            if (DefaultValue == null)
            {
                if (other.DefaultValue != null)
                {
                    return(false);
                }
            }
            else
            {
                if (!DefaultValue.Equals(other.DefaultValue))
                {
                    return(false);
                }
            }

            return(true);
        }