internal ParserExtension(MarkupCompiler compiler, ParserContext parserContext, BamlRecordWriter bamlWriter, Stream xamlStream, bool pass2) : base(parserContext, bamlWriter, xamlStream, false) { _compiler = compiler; _pass2 = pass2; Debug.Assert(bamlWriter != null, "Need a BamlRecordWriter for compiling to Baml"); }
internal LocalizationParserHooks( MarkupCompiler compiler, LocalizationDirectivesToLocFile directivesToFile, bool isSecondPass ) { _compiler = compiler; _directivesToFile = directivesToFile; _isSecondPass = isSecondPass; // The arrray list holds all the comments collected while parsing Xaml. _commentList = new ArrayList(); // It is the comments current being processed. _currentComment = new LocalizationComment(); }
static void Main(string[] args) { MarkupCompiler compiler = new MarkupCompiler(); Stopwatch stopwatch = new Stopwatch(); stopwatch.Start(); var arc = compiler.CompileArchive(@"D:\TL conversion"); stopwatch.Stop(); Console.OutputEncoding = Encoding.UTF8; //foreach (var line in arc.Sections.SelectMany(x => x.Lines)) // Console.WriteLine($"{line.OriginalLine} = {line.TranslatedLine}"); Console.WriteLine($"Compiled {arc.Sections.Select(x => x.Lines.Count).Sum()} lines in {stopwatch.Elapsed.TotalSeconds} seconds"); Console.ReadLine(); }
private static void LoadTranslationsFromFolder(string FolderPath) { Logger.Log(LogLevel.Debug, $"Loading translations from {FolderPath}"); FolderTranslations.Clear(); regexFolderTranslations.Clear(); if (Directory.Exists(FolderPath)) { try { var arc = new MarkupCompiler().CompileArchive(FolderPath); foreach (Section section in arc.Sections) { foreach (var line in section.Lines) { if (line.Flags.IsOriginalRegex) { regexFolderTranslations[new Regex(line.OriginalLine)] = line; } else { FolderTranslations[line.OriginalLine] = line; } } } Logger.Log(LogLevel.Debug, $"Loaded {FolderTranslations.Count} lines from text"); } catch (Exception ex) { Logger.Log(LogLevel.Error | LogLevel.Message, "Unable to load translations from text!"); Logger.Log(LogLevel.Error, ex); } } }
/// <summary> /// override of WriteEndAttributes /// </summary> public override void WriteEndAttributes(XamlEndAttributesNode xamlEndAttributesNode) { if (xamlEndAttributesNode.IsCompact) { return; } if (_isRootTag) { _class = string.Empty; _classModifier = string.Empty; _subClass = string.Empty; XamlTypeMapper.IsProtectedAttributeAllowed = false; } _isRootTag = false; _isSameScope = false; if (!_pass2) { if (_nameField == null) { if (_isFieldModifierSet) { ThrowException(SRID.FieldModifierNotAllowed, MarkupCompiler.DefinitionNSPrefix, xamlEndAttributesNode.LineNumber, xamlEndAttributesNode.LinePosition); } } else if (_fieldModifier != MemberAttributes.Assembly) { if (MemberAttributes.Private != _fieldModifier && MemberAttributes.Assembly != _fieldModifier) { MarkupCompiler.GenerateXmlComments(_nameField, _nameField.Name + " Name Field"); } _nameField.Attributes = _fieldModifier; _fieldModifier = MemberAttributes.Assembly; } _nameField = null; _isFieldModifierSet = false; _compiler.ConnectNameAndEvents(_name, _events, _connectionId); _name = null; if (_events != null) { _events.Clear(); _events = null; } } else { _compiler.CheckForNestedNameScope(); } // Clear the compiler's generic type argument list (see Dev11 923). _compiler.ClearGenericTypeArgs(); base.WriteEndAttributes(xamlEndAttributesNode); }
/// <summary> /// override of GetElementType /// </summary> public override bool GetElementType( XmlReader xmlReader, string localName, string namespaceUri, ref string assemblyName, ref string typeFullName, ref Type baseType, ref Type serializerType) { if (!ProcessedRootElement && namespaceUri.Equals(XamlReaderHelper.DefinitionNamespaceURI) && (localName.Equals(XamlReaderHelper.DefinitionCodeTag) || localName.Equals(XamlReaderHelper.DefinitionXDataTag))) { MarkupCompiler.ThrowCompilerException(SRID.DefinitionTagNotAllowedAtRoot, xmlReader.Prefix, localName); } bool foundElement = base.GetElementType(xmlReader, localName, namespaceUri, ref assemblyName, ref typeFullName, ref baseType, ref serializerType); if (!ProcessedRootElement) { int count = xmlReader.AttributeCount; // save reader's position, to be restored later string attrName = (xmlReader.NodeType == XmlNodeType.Attribute) ? xmlReader.Name : null; _isRootTag = true; _class = string.Empty; _subClass = string.Empty; ProcessedRootElement = true; XamlTypeMapper.IsProtectedAttributeAllowed = false; xmlReader.MoveToFirstAttribute(); while (--count >= 0) { string attribNamespaceURI = xmlReader.LookupNamespace(xmlReader.Prefix); if (attribNamespaceURI != null && attribNamespaceURI.Equals(XamlReaderHelper.DefinitionNamespaceURI)) { MarkupCompiler.DefinitionNSPrefix = xmlReader.Prefix; if (xmlReader.LocalName == CLASS) { _class = xmlReader.Value.Trim(); if (_class == string.Empty) { // flag an error for processing later in WriteDefAttribute _class = MarkupCompiler.DOT; } else { // flag this so that the Type Mapper can allow protected // attributes on the markup sub-classed root element only. XamlTypeMapper.IsProtectedAttributeAllowed = true; } } else if (xmlReader.LocalName == XamlReaderHelper.DefinitionTypeArgs) { string genericName = _compiler.GetGenericTypeName(localName, xmlReader.Value); foundElement = base.GetElementType(xmlReader, genericName, namespaceUri, ref assemblyName, ref typeFullName, ref baseType, ref serializerType); if (!foundElement) { NamespaceMapEntry[] namespaceMaps = XamlTypeMapper.GetNamespaceMapEntries(namespaceUri); bool isLocal = namespaceMaps != null && namespaceMaps.Length == 1 && namespaceMaps[0].LocalAssembly; if (!isLocal) { MarkupCompiler.ThrowCompilerException(SRID.UnknownGenericType, MarkupCompiler.DefinitionNSPrefix, xmlReader.Value, localName); } } } else if (xmlReader.LocalName == SUBCLASS) { _subClass = xmlReader.Value.Trim(); if (_subClass == string.Empty) { // flag an error for processing later in WriteDefAttribute _subClass = MarkupCompiler.DOT; } else { _compiler.ValidateFullSubClassName(ref _subClass); } } else if (xmlReader.LocalName == CLASSMODIFIER) { if (!_pass2) { _classModifier = xmlReader.Value.Trim(); if (_classModifier == string.Empty) { // flag an error for processing later in WriteDefAttribute _classModifier = MarkupCompiler.DOT; } } else { // This direct comparison is ok to do in pass2 as it has already been validated in pass1. // This is to avoid a costly instantiation of the CodeDomProvider in pass2. _isInternalRoot = string.Compare("public", xmlReader.Value.Trim(), StringComparison.OrdinalIgnoreCase) != 0; } } } xmlReader.MoveToNextAttribute(); } if (namespaceUri.Equals(XamlReaderHelper.DefinitionNamespaceURI)) { xmlReader.MoveToElement(); } else { if (attrName == null) { xmlReader.MoveToFirstAttribute(); } else { xmlReader.MoveToAttribute(attrName); } } } else if (!_compiler.IsBamlNeeded && !_compiler.ProcessingRootContext && _compiler.IsCompilingEntryPointClass && xmlReader.Depth > 0) { if ((!localName.Equals(MarkupCompiler.CODETAG) && !localName.Equals(MarkupCompiler.CODETAG + "Extension")) || !namespaceUri.Equals(XamlReaderHelper.DefinitionNamespaceURI)) { _compiler.IsBamlNeeded = true; } } return(foundElement); }
// <summary> // ctor // </summary> internal CompilerWrapper() { _mc = new MarkupCompiler(); _sourceDir = Directory.GetCurrentDirectory() + "\\"; _nErrors = 0; }
// <summary> // ctor // </summary> internal CompilerWrapper() { _mc = new MarkupCompiler(); _sourceDir = Directory.GetCurrentDirectory() + Path.DirectorySeparatorChar; _nErrors = 0; }
/// <summary> /// override of WriteEndAttributes /// </summary> public override void WriteEndAttributes(XamlEndAttributesNode xamlEndAttributesNode) { if (xamlEndAttributesNode.IsCompact) { return; } if (_isRootTag) { _class = string.Empty; _classModifier = string.Empty; _subClass = string.Empty; XamlTypeMapper.IsProtectedAttributeAllowed = false; } _isRootTag = false; _isSameScope = false; if (!_pass2) { if (_nameField == null) { if (_isFieldModifierSet) { ThrowException(SRID.FieldModifierNotAllowed, MarkupCompiler.DefinitionNSPrefix, xamlEndAttributesNode.LineNumber, xamlEndAttributesNode.LinePosition); } } else if (_fieldModifier != MemberAttributes.Assembly) { if (MemberAttributes.Private != _fieldModifier && MemberAttributes.Assembly != _fieldModifier) { MarkupCompiler.GenerateXmlComments(_nameField, _nameField.Name + " Name Field"); } _nameField.Attributes = _fieldModifier; _fieldModifier = MemberAttributes.Assembly; } _nameField = null; _isFieldModifierSet = false; _compiler.ConnectNameAndEvents(_name, _events, _connectionId); _name = null; if (_events != null) { _events.Clear(); _events = null; } } else { _compiler.CheckForNestedNameScope(); } // Clear the compiler's generic type argument list // (Strange xaml compilation error MC6025 in unrelated class) // The bug arises because the markup compiler's _typeArgsList is set for any tag // that has an x:TypeArguments attribute. It should be cleared upon reaching the // end of the tag's attributes, but this only happens in the non-pass2 case. If the // tag needs pass2 processing, the list is set but not cleared, leading to a mysterious // exception in the next tag (<ResourceDictionary>, in the repro). _compiler.ClearGenericTypeArgs(); base.WriteEndAttributes(xamlEndAttributesNode); }
// // Generate the necessary file lists and other information required by MarkupCompiler. // // Output ArrayLists: localApplicationFile, // localXamlPageFileList // referenceList // private void PrepareForMarkupCompilation(out FileUnit localApplicationFile, out FileUnit[] localXamlPageFileList, out ArrayList referenceList) { Log.LogMessageFromResources(MessageImportance.Low, SRID.PreparingCompile); Log.LogMessageFromResources(MessageImportance.Low, SRID.OutputType, OutputType); // Initialize the output parameters localXamlPageFileList = new FileUnit[0]; localApplicationFile = FileUnit.Empty; referenceList = new ArrayList(); if (_localApplicationFile != null) { // We don't want to support multiple application definition file per project. localApplicationFile = new FileUnit(_localApplicationFile.FilePath, _localApplicationFile.LinkAlias, _localApplicationFile.LogicalName); Log.LogMessageFromResources(MessageImportance.Low, SRID.LocalRefAppDefFile, localApplicationFile); } // Generate the Xaml Markup file list if (_localMarkupPages != null && _localMarkupPages.Length > 0) { int localFileNum = _localMarkupPages.Length; localXamlPageFileList = new FileUnit[localFileNum]; for (int i = 0; i < localFileNum; i++) { FileUnit localPageFile = new FileUnit(_localMarkupPages[i].FilePath, _localMarkupPages[i].LinkAlias, _localMarkupPages[i].LogicalName); localXamlPageFileList[i] = localPageFile; Log.LogMessageFromResources(MessageImportance.Low, SRID.LocalRefMarkupPage, localPageFile); } } // // Generate the asmmebly reference list. // The temporay target assembly should have been added into Reference list from target file. // if (References != null && References.Length > 0) { ReferenceAssembly asmReference; string refpath, asmname; for (int i = 0; i < References.Length; i++) { refpath = References[i].ItemSpec; refpath = TaskHelper.CreateFullFilePath(refpath, SourceDir); asmname = Path.GetFileNameWithoutExtension(refpath); asmReference = new ReferenceAssembly(refpath, asmname); referenceList.Add(asmReference); // // If always run the compilation in second appdomain, there is no need to specially // handle the referenced assemblies. // Unload the appdomain can unload all the referenced assemblies. // if (AlwaysCompileMarkupFilesInSeparateDomain == false) { bool bCouldbeChanged = TaskHelper.CouldReferenceAssemblyBeChanged(refpath, KnownReferencePaths, AssembliesGeneratedDuringBuild); if (bCouldbeChanged) { MarkupCompiler.InitializeAssemblyState(asmname); } } } } }