Ejemplo n.º 1
0
        ///////////////////////////////////////////////////////////////////////

        public ReturnCode AddImport(
            INamespace targetNamespace,
            string qualifiedImportName,
            string qualifiedExportName,
            ref Result error
            )
        {
            CheckDisposed();

            if (qualifiedImportName == null)
            {
                error = "invalid import name";
                return(ReturnCode.Error);
            }

            if (qualifiedExportName == null)
            {
                error = "invalid export name";
                return(ReturnCode.Error);
            }

            if (interpreter == null)
            {
                error = "invalid interpreter";
                return(ReturnCode.Error);
            }

            if (imports == null)
            {
                error = String.Format(
                    "imports not available in namespace \"{0}\"",
                    GetDisplayName());

                return(ReturnCode.Error);
            }

            IAlias alias       = null;
            Result localResult = null;

            if (interpreter.AddAlias(
                    qualifiedImportName, CommandFlags.None,
                    AliasFlags.NamespaceImport, _Public.ClientData.Empty,
                    interpreter, null, new ArgumentList(qualifiedExportName),
                    null, 0, ref alias, ref localResult) == ReturnCode.Ok)
            {
                alias.SourceNamespace = this;
                alias.TargetNamespace = targetNamespace;
            }
            else
            {
                error = localResult;
                return(ReturnCode.Error);
            }

            string nameToken = alias.NameToken;

            if (nameToken == null)
            {
                //
                // NOTE: This should not happen as the alias was successfully
                //       added to the interpreter and the name token cannot be
                //       null in that case.
                //
                error = "invalid alias name";
                return(ReturnCode.Error);
            }

            if (imports.ContainsKey(qualifiedImportName))
            {
                Result     localError = null;
                ResultList errors     = new ResultList();

                errors.Add(String.Format(
                               "can't add import \"{0}\" in \"{1}\": already exists",
                               nameToken, GetDisplayName()));

                if (interpreter.RemoveAliasAndCommand(
                        nameToken, null, false,
                        ref localError) != ReturnCode.Ok)
                {
                    errors.Add(localError);
                }

                error = errors;
                return(ReturnCode.Error);
            }

            imports.Add(qualifiedImportName, alias);
            return(ReturnCode.Ok);
        }