// Helper to insert line and position numbers into message, if they are present
        void ThrowException(
             string id,
             int  lineNumber,
             int  linePosition,
             Exception innerException)
        {
            string message = SR.Get(id);
            XamlParseException parseException;

            // Throw the appropriate execption.  If we have line numbers, then we are
            // parsing a xaml file, so throw a xaml exception.  Otherwise were are
            // parsing a baml file.
            if (lineNumber > 0)
            {
                message += " ";
                message += SR.Get(SRID.ParserLineAndOffset,
                                  lineNumber.ToString(CultureInfo.CurrentUICulture),
                                  linePosition.ToString(CultureInfo.CurrentUICulture));
                parseException = new XamlParseException(message, lineNumber, linePosition);
            }
            else
            {
                parseException = new XamlParseException(message);
            }

            throw parseException;
        }
Exemplo n.º 2
0
        /// <summary>
        /// WriteDefAttribute when attributes of the form x:Whatever are encountered
        /// </summary>
        public virtual void WriteDefAttribute(XamlDefAttributeNode xamlDefAttributeNode)
        {
            string attributeValue = xamlDefAttributeNode.Value;

            // There are several known def attributes, and these are checked for
            // correctness by running the known type converters.
            switch(xamlDefAttributeNode.Name)
            {
               case XamlReaderHelper.DefinitionSynchronousMode:
                   if (BamlRecordWriter != null)
                   {
                       if (xamlDefAttributeNode.Value == "Async")
                       {
                           ThrowException(SRID.ParserNoBamlAsync, "Async",
                                      xamlDefAttributeNode.LineNumber,
                                      xamlDefAttributeNode.LinePosition);
                       }
                   }
                   break;

               case XamlReaderHelper.DefinitionAsyncRecords:
                    // Update the AsyncRecords and don't store this as a def attribute
                       ThrowException(SRID.ParserNoBamlAsync, xamlDefAttributeNode.Name,
                                      xamlDefAttributeNode.LineNumber,
                                      xamlDefAttributeNode.LinePosition);
                   break;

                case XamlReaderHelper.DefinitionShared:
                    Boolean.Parse(attributeValue);   // For validation only.
                    if (BamlRecordWriter != null)
                    {
                        BamlRecordWriter.WriteDefAttribute(xamlDefAttributeNode);
                    }
                    break;

                case XamlReaderHelper.DefinitionUid:
                case XamlReaderHelper.DefinitionRuntimeName:
                    //Error if x:Uid or x:Name are markup extensions
                    if (MarkupExtensionParser.LooksLikeAMarkupExtension(attributeValue))
                    {
                        string message = SR.Get(SRID.ParserBadUidOrNameME, attributeValue);
                        message += " ";
                        message += SR.Get(SRID.ParserLineAndOffset,
                                    xamlDefAttributeNode.LineNumber.ToString(CultureInfo.CurrentCulture),
                                    xamlDefAttributeNode.LinePosition.ToString(CultureInfo.CurrentCulture));

                        XamlParseException parseException = new XamlParseException(message,
                                xamlDefAttributeNode.LineNumber, xamlDefAttributeNode.LinePosition);

                        throw parseException;
                    }

                    if (BamlRecordWriter != null)
                    {
                        BamlRecordWriter.WriteDefAttribute(xamlDefAttributeNode);
                    }
                    break;

                case XamlReaderHelper.DefinitionName:
                    if (BamlRecordWriter != null)
                    {
                        BamlRecordWriter.WriteDefAttribute(xamlDefAttributeNode);
                    }
                    break;

                default:
                    string errorID;
                        errorID = SRID.ParserUnknownDefAttribute;
                        ThrowException(errorID,
                                   xamlDefAttributeNode.Name,
                                   xamlDefAttributeNode.LineNumber,
                                   xamlDefAttributeNode.LinePosition);
                    break;
            }
        }
Exemplo n.º 3
0
        /// <summary> 
        /// Used when an exception is thrown -- does shutdown on the parser and throws the exception.
        /// </summary>
        /// <param name="e">Exception</param>
        internal override void ParseError(XamlParseException e) 
        {
            CloseWriterStream(); 
#if !PBTCOMPILER 
            // If there is an associated treebuilder, tell it about the error.  There may not
            // be a treebuilder, if this parser was built from a serializer for the purpose of 
            // converting directly to baml, rather than converting to an object tree.
            if (TreeBuilder != null)
            {
                TreeBuilder.XamlTreeBuildError(e); 
            }
#endif 
            throw e; 
        }
Exemplo n.º 4
0
        private static void ThrowExceptionWithLine(string message, int lineNumber, int linePosition)
        {
            message += " ";
            message += SR.Get(SRID.ParserLineAndOffset,
                                    lineNumber.ToString(CultureInfo.CurrentCulture),
                                    linePosition.ToString(CultureInfo.CurrentCulture));

            XamlParseException parseException = new XamlParseException(message,
                lineNumber, linePosition);


            throw parseException;
        }
Exemplo n.º 5
0
        /// <summary>
        /// Main function called by the XamlCompiler on a Compile.
        /// If singleRecordMode is true the Read returns after each item read.
        /// </summary>
        /// <returns>True if more nodes to read</returns>

        // !! Review - now that have separate out into XamlReaderHelper
        // review if still need all the virtuals or the caller of the TokenReader
        // can dispatch as they feel appropriate.
        public bool ReadXaml(bool singleRecordMode)
        {
            XamlNode xamlNode = null;
            bool     cleanup = !singleRecordMode;
            bool     done = false;

            try // What do we do with Exceptions we catch on this thread?.
            {

                while (TokenReader.Read(ref xamlNode))
                {
                    SetParserAction(xamlNode);

                    if (ParserAction == ParserAction.Normal)
                    {
                        // If processing of the xaml node determines that we are done,
                        // then stop now.  This can happen if ProcessXamlNode is
                        // overridden (such as when processing styles) and the parsed
                        // block is finished, but the overall file is not.  In that
                        // case exit this parser, but leave everything intact.
                        ProcessXamlNode(xamlNode, ref cleanup, ref done);

                        if (done)
                        {
                            break;
                        }

                        // return here in single record mode since we don't want
                        // to set the parser loop to Done.
                        if (singleRecordMode)
                        {
                            return true;
                        }

                    }
                }

            }
            catch (Exception e)
            {
                if (CriticalExceptions.IsCriticalException(e))
                {
                   throw;
                }
                else
                {
                    if (e is XamlParseException)
                    {
                        throw;
                    }
                    // If the exception was a XamlParse exception on the other
                    // side of a Reflection Invoke, then just pull up the Parse exception.
                    if (e is TargetInvocationException &&  e.InnerException is XamlParseException)
                    {
                        throw e.InnerException;
                    }

                    int lineNumber = 0;
                    int linePosition = 0;
                    string newMessage = null;

                    if (e is XmlException)
                    {
                        XmlException xmlEx = (XmlException)e;
                        lineNumber = xmlEx.LineNumber;
                        linePosition = xmlEx.LinePosition;
                        newMessage = xmlEx.Message;
                    }
                    else
                    {
                        // generic exception, If have a xamlNode then use the
                        // nodes values for the line Numbers.
                        if (null != xamlNode)
                        {
                            lineNumber = xamlNode.LineNumber;
                            linePosition = xamlNode.LinePosition;
                        }
                        newMessage = e.Message + " " + SR.Get(SRID.ParserLineAndOffset,
                                                  lineNumber.ToString(CultureInfo.CurrentCulture),
                                                  linePosition.ToString(CultureInfo.CurrentCulture));
                    }
                    XamlParseException parseException = new XamlParseException(newMessage, lineNumber, linePosition, e);
                    ParseError(parseException);

                    // Recurse on error
                    cleanup = true;
                    throw parseException;
                }
            }
            finally
            {
                // Perform cleanup only if we encountered a non-recoverable error, or
                // we're finished reading the stream (EndDocument reached in single
                // record mode, or end of stream reached in multi-record mode)
                if (cleanup)
                {
                    // Close the reader, which will close the underlying stream.
                    TokenReader.Close();
                }
            }

            return false;
        }
Exemplo n.º 6
0
 public void Destroy()
 {
     xamlParseException = null;
     tlog.Info(tag, "Destroy() is called!");
 }
Exemplo n.º 7
0
        // virtuals to override the default implementation. used by the compiler
        // for internal virtuals review why not public as the others?

        // Used when an exception is thrown.The default action is to shutdown the parser
        // and throw the exception.
        internal virtual void ParseError(XamlParseException e)
        {
        }
Exemplo n.º 8
0
 public void Init()
 {
     tlog.Info(tag, "Init() is called!");
     xamlParseException = new XamlParseException("Parse Exception!");
 }
        private Type Resolve(string qualifiedTypeName, IServiceProvider serviceProvider, out XamlParseException exception)
        {
            exception = null;
            var split = qualifiedTypeName.Split(':');

            if (split.Length > 2)
            {
                return(null);
            }

            string prefix, name;

            if (split.Length == 2)
            {
                prefix = split[0];
                name   = split[1];
            }
            else
            {
                prefix = "";
                name   = split[0];
            }

            IXmlLineInfo xmlLineInfo = null;

            if (serviceProvider != null)
            {
                var lineInfoProvider = serviceProvider.GetService(typeof(IXmlLineInfoProvider)) as IXmlLineInfoProvider;
                if (lineInfoProvider != null)
                {
                    xmlLineInfo = lineInfoProvider.XmlLineInfo;
                }
            }

            var namespaceuri = namespaceResolver.LookupNamespace(prefix);

            if (namespaceuri == null)
            {
                exception = new XamlParseException(string.Format("No xmlns declaration for prefix \"{0}\"", prefix), xmlLineInfo);
                return(null);
            }

            return(getTypeFromXmlName(new XmlType(namespaceuri, name, null), xmlLineInfo, currentAssembly, out exception));
        }
Exemplo n.º 10
0
 public void Init()
 {
     tlog.Info(tag, "Init() is called!");
     xamlParse = new XamlParseException("myMessage");
 }
Exemplo n.º 11
0
        bool TryCoreCompile(MethodDefinition initComp, ILRootNode rootnode, out Exception exception)
        {
            try
            {
                var body   = new MethodBody(initComp);
                var module = body.Method.Module;
                var type   = initComp.DeclaringType;

                MethodDefinition constructorOfRemoveEventsType;
                TypeDefinition   typeOfRemoveEvents = CreateTypeForRemoveEvents(type, out constructorOfRemoveEventsType);

                var field = type.GetOrCreateField("___Info_Of_RemoveEvent___", FieldAttributes.Private, typeOfRemoveEvents);

                body.InitLocals = true;
                var il = body.GetILProcessor();
                il.Emit(OpCodes.Ldarg_0);
                il.Emit(OpCodes.Newobj, constructorOfRemoveEventsType);
                il.Emit(OpCodes.Stfld, field);

                var resourcePath = GetPathForType(module, type);

                il.Emit(Nop);

                List <Instruction> insOfAddEvent = new List <Instruction>();

                var visitorContext = new ILContext(il, body, insOfAddEvent, module);

                rootnode.Accept(new XamlNodeVisitor((node, parent) => node.Parent = parent), null);
                rootnode.Accept(new ExpandMarkupsVisitor(visitorContext), null);
                rootnode.Accept(new PruneIgnoredNodesVisitor(), null);
                rootnode.Accept(new CreateObjectVisitor(visitorContext), null);

                Set(visitorContext, visitorContext.Variables[rootnode], "IsCreateByXaml", new ValueNode("true", rootnode.NamespaceResolver), null);

                rootnode.Accept(new SetNamescopesAndRegisterNamesVisitor(visitorContext), null);
                rootnode.Accept(new SetFieldVisitor(visitorContext), null);
                rootnode.Accept(new SetResourcesVisitor(visitorContext), null);
                rootnode.Accept(new SetPropertiesVisitor(visitorContext, true), null);

                AddInsOfRemoveEvent(il, visitorContext.InsOfAddEvent, typeOfRemoveEvents);

                il.Emit(Ret);
                initComp.Body = body;
                exception     = null;
                return(true);
            }
            catch (Exception e)
            {
                XamlParseException xamlParseException = e as XamlParseException;
                if (null != xamlParseException)
                {
                    XamlParseException ret = new XamlParseException(xamlParseException.Message + "\n" + ReferencePath, xamlParseException.XmlInfo, xamlParseException.InnerException);
                    exception = ret;
                }
                else
                {
                    exception = e;
                }

                return(false);
            }
        }
Exemplo n.º 12
0
		Type Resolve(string qualifiedTypeName, IServiceProvider serviceProvider, out XamlParseException exception)
		{
			exception = null;
			var split = qualifiedTypeName.Split(':');
			if (split.Length > 2)
				return null;

			string prefix, name;
			if (split.Length == 2)
			{
				prefix = split[0];
				name = split[1];
			}
			else
			{
				prefix = "";
				name = split[0];
			}

			IXmlLineInfo xmlLineInfo = null;
			if (serviceProvider != null)
			{
				var lineInfoProvider = serviceProvider.GetService(typeof (IXmlLineInfoProvider)) as IXmlLineInfoProvider;
				if (lineInfoProvider != null)
					xmlLineInfo = lineInfoProvider.XmlLineInfo;
			}

			var namespaceuri = string.IsNullOrEmpty(prefix) ? "" : namespaceResolver.LookupNamespace(prefix);
			if (namespaceuri == null)
			{
				exception = new XamlParseException(string.Format("No xmlns declaration for prefix \"{0}\"", prefix), xmlLineInfo);
				return null;
			}

			return getTypeFromXmlName(new XmlType(namespaceuri, name, null), xmlLineInfo, currentAssembly, out exception);
		}