Пример #1
0
 public void SetParentTemplate(TALProgram parentProgram)
 {
     this.m_parentTemplate = parentProgram;
     this.m_source = parentProgram.Source;
     this.m_sourcePath = parentProgram.SourcePath;
     this.m_commandList = parentProgram.CommandList;
     this.m_symbolTable = parentProgram.SymbolTable;
 }
Пример #2
0
        public TALProgram Compile(string source, string sourcePath)
        {
            // Initialise a template compiler.
            this.commandList = new List<TALCommand>();
            this.tagStack = new List<TagInfo>();
            this.symbolLocationTable = new Dictionary<int, int>();
            this.macroMap = new Dictionary<string, TALSubProgram>();
            this.imports = new HashSet<string>();
            this.endTagSymbol = 1;
            this.currentStartTag = null;

            XmlReader reader = null;
            StringReader inputReader = null;
            try
            {
                inputReader = new StringReader(source);
                XmlTextReader xmlTextReader = new XmlTextReader(inputReader);
                xmlTextReader.XmlResolver = null;
                xmlTextReader.Namespaces = false;

                XmlReaderSettings readerSettings = new XmlReaderSettings();
                readerSettings.ProhibitDtd = false;

                reader = XmlReader.Create(xmlTextReader, readerSettings);
                while (reader.Read())
                {
                    if (reader.NodeType == XmlNodeType.Element)
                    {
                        Tag tag = new Tag();
                        tag.SourcePath = sourcePath;
                        IXmlLineInfo li = reader as IXmlLineInfo;
                        if (li != null && li.HasLineInfo())
                        {
                            tag.LineNumber = li.LineNumber;
                            tag.LinePosition = li.LinePosition;
                        }
                        tag.Name = reader.Name;
                        tag.Attributes = new Dictionary<string, string>();
                        string atttribs = "";
                        for (int i = 0; i < reader.AttributeCount; i++)
                        {
                            reader.MoveToAttribute(i);
                            atttribs += string.Format(" {0}=\"{1}\"", reader.Name, reader.Value);
                            tag.Attributes.Add(reader.Name, reader.Value);
                        }
                        reader.MoveToElement();
                        if (reader.IsEmptyElement)
                        {
                            handle_startendtag(tag);
                        }
                        else
                        {
                            handle_starttag(tag);
                        }
                    }
                    else if (reader.NodeType == XmlNodeType.EndElement)
                    {
                        Tag tag = new Tag();
                        tag.SourcePath = sourcePath;
                        IXmlLineInfo li = reader as IXmlLineInfo;
                        if (li != null && li.HasLineInfo())
                        {
                            tag.LineNumber = li.LineNumber;
                            tag.LinePosition = li.LinePosition;
                        }
                        tag.Name = reader.Name;
                        handle_endtag(tag);
                    }
                    else if (reader.NodeType == XmlNodeType.DocumentType)
                    {
                        handle_doctype(reader);
                    }
                    else if (reader.NodeType == XmlNodeType.Comment)
                    {
                        handle_comment(reader.Value);
                    }
                    else if (reader.NodeType == XmlNodeType.CDATA)
                    {
                        handle_cdata(reader.Value);
                    }
                    else if (reader.NodeType == XmlNodeType.XmlDeclaration)
                    {
                        handle_xmldecl(reader.Name, reader.Value);
                    }
                    else if (reader.NodeType == XmlNodeType.Whitespace ||
                        reader.NodeType == XmlNodeType.SignificantWhitespace ||
                        reader.NodeType == XmlNodeType.Text)
                    {
                        handle_data(reader.Value);
                    }
                    else if (reader.NodeType == XmlNodeType.ProcessingInstruction)
                    {
                        handle_pi(reader.Name, reader.Value);
                    }
                    else if (reader.NodeType == XmlNodeType.EntityReference)
                    {
                        handle_entityref(reader.Name);
                    }
                }
            }
            finally
            {
                if (reader != null)
                    reader.Close();
                if (inputReader != null)
                    inputReader.Close();
            }

            TALProgram template = new TALProgram(source, sourcePath, this.commandList, this.macroMap, this.imports, this.symbolLocationTable);
            return template;
        }
Пример #3
0
        static void CompileImports(TALCompiler compiler, TALProgram program, TemplateInfo ti)
        {
            if (program.Imports != null && program.Imports.Count > 0)
            {
                foreach (string key in program.Imports)
                {
                    // Split the Import key
                    string destNs = key.Split(new char[] { ':' }, 2)[0];
                    string sourcePath = key.Split(new char[] { ':' }, 2)[1];

                    // Imported macros without namespace go into main template namespace.
                    if (string.IsNullOrEmpty(destNs))
                    {
                        destNs = "template";
                    }

                    // Check if the template on path was not compiled
                    TALProgram importedProg = null;
                    if (!ti.ImportedPrograms.ContainsKey(sourcePath))
                    {
                        // Compile Imported template
                        string source = File.ReadAllText(sourcePath);
                        importedProg = compiler.Compile(source, sourcePath);
                        ti.ImportedPrograms.Add(sourcePath, importedProg);

                        // Compile Imports of Imported template
                        CompileImports(compiler, importedProg, ti);
                    }
                    else
                    {
                        importedProg = ti.ImportedPrograms[sourcePath];
                    }

                    // Save info about Imported program by namespace and path
                    if (!ti.ImportedNamespaces.ContainsKey(destNs))
                    {
                        ti.ImportedNamespaces.Add(destNs, new HashSet<string>() { sourcePath });
                    }
                    else
                    {
                        if (!ti.ImportedNamespaces[destNs].Contains(sourcePath))
                        {
                            ti.ImportedNamespaces[destNs].Add(sourcePath);
                        }
                    }
                }
            }
        }