public void TestCall( )
        {
            Guid id = new Guid( GuidString );
            string content =
                @"<?xml version=""1.0"" encoding=""utf-8""?>" +
                @"<rb>" +
                @"  <object type=""Rb.ComponentXmlLoader.Tests.TestLoader+Root"" id=""" + GuidString + @""">" +
                @"      <method objectId=""" + GuidString + @""" call=""Call""/>" +
                @"      <method objectId=""" + GuidString + @""" call=""Call"">" +
                @"			<parameters>" +
                @"				<string value=""hello""/>" +
                @"			</parameters>" +
                @"		</method>" +
                @"  </object>" +
                @"</rb>" +
                "";

            Loader loader = new Loader( );

            byte[] contentBytes = System.Text.Encoding.ASCII.GetBytes( content );

            Definition = 0;

            ComponentLoadParameters parameters = new ComponentLoadParameters( );
            loader.Load( MemorySource( contentBytes ), parameters );

            Assert.IsTrue( parameters.Objects.ContainsKey( id ) );

            object obj = parameters.Objects[ id ];
            Assert.IsTrue( obj is Root );
        }
        /// <summary>
        /// Setup constructor
        /// </summary>
        /// <param name="parameters">Load parameters</param>
        /// <param name="errors">Error collection</param>
        /// <param name="reader">XML reader positioned at the element that created this builder</param>
        /// <param name="parentBuilder">Parent builder</param>
        public TypeBuilder( ComponentLoadParameters parameters, ErrorCollection errors, XmlReader reader, BaseBuilder parentBuilder )
            : base(parameters, errors, reader, parentBuilder)
        {
            //  Retrieve type name and optional assembly name from the element
            string typeName     = reader.GetAttribute( "value" );
            string assemblyName = reader.GetAttribute( "assembly" );

            if ( typeName == null )
            {
                throw new ApplicationException( string.Format( "Element \"{0}\" requires a \"type\" attribute", reader.Name ) );
            }

            Type objectType = null;
            if ( assemblyName == null )
            {
                //  Get the object type from the currently loaded set of assemblies
                objectType = AppDomainUtils.FindType( typeName );
                if ( objectType == null )
                {
                    throw new ApplicationException( string.Format( "Failed to find type \"{0}\" in app domain" , typeName ) );
                }
            }
            else
            {
                //  Get the object type from the specified assembly
                Assembly assembly = AppDomain.CurrentDomain.Load( assemblyName );
                objectType = assembly.GetType( typeName );
                if ( objectType == null )
                {
                    throw new ApplicationException( string.Format( "Failed to find type \"{0}\" in assembly \"{1}\"", typeName, assemblyName ) );
                }
            }

            BuildObject = objectType;
        }
 /// <summary>
 /// Setup constructor
 /// </summary>
 /// <param name="parameters">Load parameters</param>
 /// <param name="errors">Error collection</param>
 /// <param name="reader">XML reader positioned at the element that created this builder</param>
 /// <param name="parentBuilder">Parent builder</param>
 public ListBuilder( ComponentLoadParameters parameters, ErrorCollection errors, XmlReader reader, BaseBuilder parentBuilder )
     : base(parameters, errors, reader, parentBuilder)
 {
     //  NOTE: AP: This Just Works, because the base builder implementation checks if BuildObject is an
     //  IList, and adds child build objects to it if so... this is all we need to do (in fact, <list>
     //  is just a shorthand for <object type="System.Collections.ArrayList">)
     BuildObject = m_BuildObjects;
 }
        /// <summary>
        /// Setup constructor
        /// </summary>
        /// <param name="parameters">Load parameters</param>
        /// <param name="errors">Error collection</param>
        /// <param name="reader">XML reader positioned at the element that created this object</param>
        /// <param name="parentBuilder">Parent builder</param>
        public ObjectBuilder( ComponentLoadParameters parameters, ErrorCollection errors, XmlReader reader, BaseBuilder parentBuilder )
            : base(parameters, errors, reader, parentBuilder)
        {
            //  Retrieve type name and optional assembly name from the element
            string typeName = reader.GetAttribute( "type" );
            string assemblyName = reader.GetAttribute( "assembly" );

            Construct( reader.Name, typeName, assemblyName );
        }
 /// <summary>
 /// Setup constructor
 /// </summary>
 /// <param name="parameters">Load parameters</param>
 /// <param name="errors">Error log</param>
 /// <param name="reader">XML reader positioned at the element that created this builder</param>
 public RootBuilder( ComponentLoadParameters parameters, ErrorCollection errors, XmlReader reader )
     : base(parameters, errors, reader, null)
 {
     if ( reader.Name != "rb" )
     {
         errors.Add( reader, "Expected root node of component XML document to be named <rb>, not <{0}>", reader.Name );
     }
     BuildObject = parameters.Target ?? m_ChildObjects;
 }
        /// <summary>
        /// Setup constructor
        /// </summary>
        /// <param name="parameters">Load parameters</param>
        /// <param name="errors">Error collection</param>
        /// <param name="reader">XML reader positioned at the element that created this builder</param>
        /// <param name="parentBuilder">Parent builder</param>
        public BaseBuilder( ComponentLoadParameters parameters, ErrorCollection errors, XmlReader reader, BaseBuilder parentBuilder )
        {
            IXmlLineInfo lineInfo = reader as IXmlLineInfo;
            if ( lineInfo != null )
            {
                m_Line      = lineInfo.LineNumber;
                m_Column    = lineInfo.LinePosition;
            }

            m_ParentBuilder = parentBuilder;
            m_Errors        = errors;
            m_Parameters    = parameters;
            m_Property      = reader.GetAttribute( "property" );
            m_DynProperty   = reader.GetAttribute( "dynProperty" );
            m_Name          = reader.GetAttribute( "name" );
            m_Id            = reader.GetAttribute( "id" );
        }
        /// <summary>
        /// Setup constructor
        /// </summary>
        /// <param name="parameters">Load parameters</param>
        /// <param name="errors">Error collection</param>
        /// <param name="reader">XML reader positioned at the element that created this builder</param>
        /// <param name="parentBuilder">Parent builder</param>
        public AssetBuilder( ComponentLoadParameters parameters, ErrorCollection errors, XmlReader reader, BaseBuilder parentBuilder )
            : base(parameters, errors, reader, parentBuilder)
        {
            string assetPath = reader.GetAttribute( "path" );

            string instance = reader.GetAttribute( "instance" );
            m_Instance = string.IsNullOrEmpty( instance ) ? false : bool.Parse( instance );
            m_Loader = AssetManager.Instance.CreateLoadState( Locations.NewLocation( assetPath ), null );

            string useCurrentParams = reader.GetAttribute( "useCurrentParameters" );
            if ( useCurrentParams != null )
            {
                if ( bool.Parse( useCurrentParams ) )
                {
                    m_Loader.Parameters = Parameters;
                }
            }
        }
 /// <summary>
 /// Setup constructor
 /// </summary>
 /// <param name="parameters">Load parameters</param>
 /// <param name="errors">Error collection</param>
 /// <param name="reader">XML reader positioned at the element that created this builder</param>
 /// <param name="parentBuilder">Parent builder</param>
 public InstanceBuilder( ComponentLoadParameters parameters, ErrorCollection errors, XmlReader reader, BaseBuilder parentBuilder )
     : base(parameters, errors, reader, parentBuilder)
 {
 }
 /// <summary>
 /// Setup constructor
 /// </summary>
 /// <param name="parameters">Load parameters</param>
 /// <param name="errors">Error collection</param>
 /// <param name="reader">XML reader positioned at the element that created this object</param>
 /// <param name="parentBuilder">Parent builder</param>
 /// <param name="typeName">Name of the type to create</param>
 public ObjectBuilder( ComponentLoadParameters parameters, ErrorCollection errors, XmlReader reader, BaseBuilder parentBuilder, string typeName )
     : base(parameters, errors, reader, parentBuilder)
 {
     string assemblyName = reader.GetAttribute( "assembly" );
     Construct( reader.Name, typeName, assemblyName );
 }
 /// <summary>
 /// Called from the form Load handler. Creates the main viewer, loading it from the source specified by ViewerSetupPath
 /// </summary>
 /// <returns>Return the main viewer object</returns>
 protected virtual Viewer CreateMainViewer( )
 {
     Viewer viewer = null;
     ISource viewerSource = ViewerSetupPath;
     if ( viewerSource != null )
     {
         try
         {
             //	Load viewer
             ComponentLoadParameters loadParams = new ComponentLoadParameters( );
             loadParams.Properties[ "User" ] = m_User;
             viewer = ( Viewer )AssetManager.Instance.Load( viewerSource, loadParams );
         }
         catch ( Exception ex )
         {
             AppLog.Exception( ex, "Failed to load viewer from \"{0}\"", viewerSource );
         }
     }
     if ( viewer == null )
     {
         AppLog.Info( "Creating default viewer" );
         viewer = new Viewer( );
     }
     return viewer;
 }
Esempio n. 11
0
        /// <summary>
        /// Loads component XML from a stream
        /// </summary>
        public object Load( Stream stream, ISource source, LoadParameters parameters )
        {
            if ( !( parameters is ComponentLoadParameters ) )
            {
                ComponentLoadParameters newParameters = new ComponentLoadParameters( parameters.Target );

                foreach ( IDynamicProperty property in parameters.Properties )
                {
                    newParameters.Properties.Add( property );
                }

                parameters = newParameters;
            }

            parameters.CanCache = false;

            ErrorCollection errors = new ErrorCollection( string.Copy( source.Path ) );

            XmlTextReader reader = new XmlTextReader( stream );
            reader.WhitespaceHandling = WhitespaceHandling.Significant;
            try
            {
                if ( reader.MoveToContent( ) == XmlNodeType.None )
                {
                    AssetsLog.Warning( "XML component asset \"{0}\" was empty - returning null", source.Name );
                    return null;
                }
            }
            catch ( XmlException ex )
            {
                AssetsLog.Error( "Moving to XML component asset \"{0}\" content threw an exception", source.Name );

                Entry entry = new Entry( AssetsLog.GetSource( Severity.Error ), ex.Message );
                Source.HandleEntry( entry.Locate( source.Path, ex.LineNumber, ex.LinePosition, "" ) );

                throw new ApplicationException( string.Format( "Failed to load component XML asset \"{0}\" (see log for details)", source.Name ) );
            }

            string cacheable = reader.GetAttribute( "cacheable" );
            parameters.CanCache = ( cacheable != null ) && ( ( cacheable == "yes" ) || ( cacheable == "true" ) );

            RootBuilder builder = ( RootBuilder )BaseBuilder.CreateBuilderFromReader( null, ( ComponentLoadParameters )parameters, errors, reader );

            if ( errors.Count == 0 )
            {
                BaseBuilder.SafePostCreate( builder );
                if ( errors.Count == 0 )
                {
                    BaseBuilder.SafeResolve( builder, true );
                }
            }

            if ( ( builder.BuildObject == null ) && ( errors.Count == 0 ) )
            {
                errors.Add( builder, "Empty components file" );
            }

            if ( errors.Count > 0 )
            {
                foreach ( Entry error in errors )
                {
                    Source.HandleEntry( error );
                }
                throw new ApplicationException( string.Format( "Failed to load component XML asset \"{0}\" (see log for details)", source.Name ) );
            }

            //	TODO: AP: bit dubious... if there's more than one object, return a list
            if ( builder.Children.Count == 0 )
            {
                throw new ApplicationException( string.Format( "Failed to load component XML asset \"{0}\" - did not contain any components", source.Name ) );
            }
            if ( builder.Children.Count == 1 )
            {
                return builder.Children[ 0 ];
            }
            return builder.Children;
        }
 /// <summary>
 /// Setup constructor
 /// </summary>
 /// <param name="parameters">Load parameters</param>
 /// <param name="errors">Error collection</param>
 /// <param name="reader">XML reader positioned at the element that created this builder</param>
 /// <param name="parentBuilder">Parent builder</param>
 public MethodBuilder( ComponentLoadParameters parameters, ErrorCollection errors, XmlReader reader, BaseBuilder parentBuilder )
     : base(parameters, errors, reader, parentBuilder)
 {
     m_MethodName = reader.GetAttribute( "call" );
 }
        /// <summary>
        /// Creates a BaseBuilder-derived object from a name
        /// </summary>
        /// <param name="parentBuilder">Parent builder</param>
        /// <param name="parameters">Load parameters</param>
        /// <param name="errors">Error collection</param>
        /// <param name="reader">XML reader</param>
        /// <returns>Returns the new builder, or null if there as an error</returns>
        public static BaseBuilder CreateBuilderFromReader( BaseBuilder parentBuilder, ComponentLoadParameters parameters, ErrorCollection errors, XmlReader reader )
        {
            BaseBuilder result = null;
            try
            {
                switch ( reader.Name )
                {
                    case "null"		: result = new ValueBuilder( parameters, errors, reader, parentBuilder, null );		break;
                    case "rb"       : result = new RootBuilder( parameters, errors, reader );							break;
                    case "object"   : result = new ObjectBuilder( parameters, errors, reader, parentBuilder );			break;
                    case "ref"      : result = new ReferenceBuilder( parameters, errors, reader, parentBuilder );		break;
                    case "asset"	: result = new AssetBuilder( parameters, errors, reader, parentBuilder );			break;
                    case "instance" : result = new InstanceBuilder( parameters, errors, reader, parentBuilder );		break;
                    case "method"	: result = new MethodBuilder( parameters, errors, reader, parentBuilder );			break;
                    case "list"     : result = new ListBuilder( parameters, errors, reader, parentBuilder );			break;
                    case "table"	: result = new DictionaryBuilder( parameters, errors, reader, parentBuilder );		break;
                    case "type"     : result = new TypeBuilder( parameters, errors, reader, parentBuilder );			break;
                    case "template"	: result = new TemplateBuilder( parameters, errors, reader, parentBuilder );		break;
                    case "location" :
                        string loc = reader.GetAttribute( "value" );
                        result = new ValueBuilder( parameters, errors,reader, parentBuilder, Locations.NewLocation( loc ) );
                        break;
                    case "dictionaryEntry"	:
                        result = new DictionaryEntryBuilder( parameters, errors, reader, parentBuilder );
                        break;
                    case "dynProperty"	:
                        object dynPropertyValue = parameters.Properties[ reader.GetAttribute( "value" ) ];
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, dynPropertyValue );
                        break;
                    case "colour"		:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, MakeColour( reader ) );
                        break;
                    case "string"		:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, reader.GetAttribute( "value" ) );
                        break;
                    case "guid"			:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, new Guid( reader.GetAttribute( "value" ) ) );
                        break;
                    case "bool"			:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, bool.Parse( reader.GetAttribute( "value" ) ) );
                        break;
                    case "char"			:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, char.Parse( reader.GetAttribute( "value" ) ) );
                        break;
                    case "byte"			:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, byte.Parse( reader.GetAttribute( "value" ) ) );
                        break;
                    case "sbyte"		:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, sbyte.Parse( reader.GetAttribute( "value" ) ) );
                        break;
                    case "short"		:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, short.Parse( reader.GetAttribute( "value" ) ) );
                        break;
                    case "ushort"		:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, ushort.Parse( reader.GetAttribute( "value" ) ) );
                        break;
                    case "int"			:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, int.Parse( reader.GetAttribute( "value" ) ) );
                        break;
                    case "uint"			:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, uint.Parse( reader.GetAttribute( "value" ) ) );
                        break;
                    case "long"			:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, long.Parse( reader.GetAttribute( "value" ) ) );
                        break;
                    case "ulong"		:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, ulong.Parse( reader.GetAttribute( "value" ) ) );
                        break;
                    case "float"		:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, float.Parse( reader.GetAttribute( "value" ) ) );
                        break;
                    case "double"		:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, double.Parse( reader.GetAttribute( "value" ) ) );
                        break;
                    case "timeSpan"		:
                        result = new ValueBuilder( parameters, errors, reader, parentBuilder, MakeTimeSpan( reader ) );
                        break;
                    case "point3"		:
                        {
                            float x = float.Parse( reader.GetAttribute( "x" ) );
                            float y = float.Parse( reader.GetAttribute( "y" ) );
                            float z = float.Parse( reader.GetAttribute( "z" ) );
                            result = new ValueBuilder( parameters, errors, reader, parentBuilder, new Point3( x, y, z ) );
                            break;
                        }
                    case "bigPoint3"	:
                        {
                            long x = long.Parse( reader.GetAttribute( "x" ) );
                            long y = long.Parse( reader.GetAttribute( "y" ) );
                            long z = long.Parse( reader.GetAttribute( "z" ) );
                            result = new ValueBuilder( parameters, errors, reader, parentBuilder, new BigPoint3( x, y, z ) );
                            break;
                        }
                    case "vector2"		:
                        {
                            float x = float.Parse( reader.GetAttribute( "x" ) );
                            float y = float.Parse( reader.GetAttribute( "y" ) );
                            result = new ValueBuilder( parameters, errors, reader, parentBuilder, new Vector2( x, y ) );
                            break;
                        }
                    case "vector3"		:
                        {
                            float x = float.Parse( reader.GetAttribute( "x" ) );
                            float y = float.Parse( reader.GetAttribute( "y" ) );
                            float z = float.Parse( reader.GetAttribute( "z" ) );
                            result = new ValueBuilder( parameters, errors, reader, parentBuilder, new Vector3( x, y, z ) );
                            break;
                        }
                    case "point2"   	:
                    case "quat"     	:
                        {
                            errors.Add( reader, "Element is not yet supported", reader.Name );
                            reader.Skip( );
                            break;
                        }
                    default	:
                        {
                            //	Interpret name as object type
                            string typeName = reader.Name;
                            result = new ObjectBuilder( parameters, errors, reader, parentBuilder, typeName );
                            break;
                        }
                }
            }
            catch ( Exception ex )
            {
                errors.Add( reader, ex, "Builder created from element \"{0}\" threw an exception", reader.Name );
                reader.Skip( );
            }

            if ( result != null )
            {
                string name = reader.Name;
                try
                {
                    result.ReadChildBuilders( reader );
                }
                catch ( Exception ex )
                {
                    errors.Add( reader, ex, "Exception thrown while reading children from builder \"{0}\"", name );
                }
            }

            return result;
        }
 /// <summary>
 /// Setup constructor
 /// </summary>
 /// <param name="parameters">Load parameters</param>
 /// <param name="errors">Error collection</param>
 /// <param name="reader">XML reader positioned at the element that created this builder</param>
 /// <param name="parentBuilder">Parent builder object</param>
 public ReferenceBuilder( ComponentLoadParameters parameters, ErrorCollection errors, XmlReader reader, BaseBuilder parentBuilder )
     : base(parameters, errors, reader, parentBuilder)
 {
     m_ObjectId = reader.GetAttribute( "objectId" );
     m_Properties = reader.GetAttribute( "access" );
 }
 /// <summary>
 /// Copies members from this LoadParameters to parameters
 /// </summary>
 protected void DeepCopy( ComponentLoadParameters parameters )
 {
     parameters.m_Builder = m_Builder;
     parameters.m_Objects = m_Objects;
     base.DeepCopy( parameters );
 }
 /// <summary>
 /// Clones this object
 /// </summary>
 /// <returns>Deep copy clone</returns>
 public override object Clone( )
 {
     ComponentLoadParameters clone = new ComponentLoadParameters( );
     DeepCopy( clone );
     return clone;
 }