public bool ExpandEntity(string entityName) { string entity = (string)EntityExpansion[entityName]; if (entity != null) { Expand(entity.ToCharArray()); return(true); } entityName = entityName.Substring(1, entityName.Length - 1); //System.writer.WriteLine("Trying to expand: "+entityName); DTDEntity realEntity = Expander.ExpandEntity(entityName); if (realEntity != null) { //System.writer.WriteLine("Expanded: "+entityName); StreamReader entityIn = realEntity.GetReader(); if (entityIn != null) { if (InputStreams == null) { InputStreams = new Stack(); } InputStreams.Push(StreamInfo); StreamInfo = new StreamInfo(realEntity.GetExternalId(), entityIn); return(true); } } return(false); }
protected void ParseEntity() { bool isParsed = false; Token name = Scanner.Get(); if (name.Type == Scanner.PERCENT) { isParsed = true; name = Expect(Scanner.IDENTIFIER); } else if (name.Type != Scanner.IDENTIFIER) { throw new DTDParseException(Scanner.GetUriId(), "Invalid entity declaration", Scanner.GetLineNumber(), Scanner.GetColumn()); } DTDEntity entity = (DTDEntity)Dtd.Entities[name.Value]; bool skip = false; if (entity == null) { entity = new DTDEntity(name.Value, DefaultLocation); Dtd.Entities[entity.Name] = entity; } else { // 070501 MAW: If the entity already exists, create a dummy entity - this way // you keep the original definition. Thanks to Jags Krishnamurthy of object // Edge for pointing out this problem and for pointing out the solution entity = new DTDEntity(name.Value, DefaultLocation); skip = true; } Dtd.Items.Add(entity); entity.IsParsed = isParsed; ParseEntityDef(entity); if (entity.IsParsed && (entity.Value != null) && !skip) { Scanner.AddEntity(entity.Name, entity.Value); } }
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 }
protected void ParseEntityDef(DTDEntity entity) { Token token = Scanner.Get(); if (token.Type == Scanner.STRING) { // Only set the entity value if it hasn't been set yet // XML 1.0 spec says that you use the first value of // an entity, not the most recent. if (entity.Value == null) { entity.Value = token.Value; } } else if (token.Type == Scanner.IDENTIFIER) { if (token.Value.Equals("SYSTEM")) { DTDSystem sys = new DTDSystem(); token = Expect(Scanner.STRING); sys.System = token.Value; entity.ExternalId = sys; } else if (token.Value.Equals("PUBLIC")) { DTDPublic pub = new DTDPublic(); token = Expect(Scanner.STRING); pub.Pub = token.Value; token = Expect(Scanner.STRING); pub.System = token.Value; entity.ExternalId = pub; } else { throw new DTDParseException(Scanner.GetUriId(), "Invalid External ID specification", Scanner.GetLineNumber(), Scanner.GetColumn()); } // ISSUE: isParsed is set to TRUE if this is a Parameter Entity // Reference (assuming this is because Parameter Entity // external references are parsed, whereas General Entity // external references are irrelevant for this product). // However, NDATA is only valid if this is // a General Entity Reference. So, "if" conditional should // be (!entity.isParsed) rather than (entity.isParsed). // //Entity Declaration // [70] EntityDecl ::= GEDecl | PEDecl // [71] GEDecl ::= '<!ENTITY' S Name S EntityDef S? '>' // [72] PEDecl ::= '<!ENTITY' S '%' S Name S PEDef S? '>' // [73] EntityDef ::= EntityValue | (ExternalID NDataDecl?) // [74] PEDef ::= EntityValue | ExternalID //External Entity Declaration // [75] ExternalID ::= 'SYSTEM' S SystemLiteral // | 'PUBLIC' S PubidLiteral S SystemLiteral // [76] NDataDecl ::= S 'NDATA' S Name [ VC: Notation Declared ] if (!entity.IsParsed) // CHANGE 1 { token = Scanner.Peek(); if (token.Type == Scanner.IDENTIFIER) { if (!token.Value.Equals("NDATA")) { throw new DTDParseException(Scanner.GetUriId(), "Invalid NData declaration", Scanner.GetLineNumber(), Scanner.GetColumn()); } // CHANGE 2: Add call to scanner.get. // This gets "NDATA" IDENTIFIER. token = Scanner.Get(); // Get the NDATA "Name" IDENTIFIER. token = Expect(Scanner.IDENTIFIER); // Save the ndata value entity.Ndata = token.Value; } } } else { throw new DTDParseException(Scanner.GetUriId(), "Invalid entity definition", Scanner.GetLineNumber(), Scanner.GetColumn()); } Expect(Scanner.GT); }
public override bool Equals(object ob) { if (ob == this) { return(true); } if (!(ob is DTDEntity)) { return(false); } DTDEntity other = (DTDEntity)ob; if (Name == null) { if (other.Name != null) { return(false); } } else { if (!Name.Equals(other.Name)) { return(false); } } if (IsParsed != other.IsParsed) { return(false); } if (Value == null) { if (other.Value != null) { return(false); } } else { if (!Value.Equals(other.Value)) { return(false); } } if (ExternalId == null) { if (other.ExternalId != null) { return(false); } } else { if (!ExternalId.Equals(other.ExternalId)) { return(false); } } if (Ndata == null) { if (other.Ndata != null) { return(false); } } else { if (!Ndata.Equals(other.Ndata)) { return(false); } } return(true); }