Ejemplo n.º 1
0
        // Module defining this command


        // Optional custom code for this activity


        /// <summary>
        /// Returns a configured instance of System.Management.Automation.PowerShell, pre-populated with the command to run.
        /// </summary>
        /// <param name="context">The NativeActivityContext for the currently running activity.</param>
        /// <returns>A populated instance of System.Management.Automation.PowerShell</returns>
        /// <remarks>The infrastructure takes responsibility for closing and disposing the PowerShell instance returned.</remarks>
        protected override ActivityImplementationContext GetPowerShell(NativeActivityContext context)
        {
            System.Management.Automation.PowerShell invoker       = global::System.Management.Automation.PowerShell.Create();
            System.Management.Automation.PowerShell targetCommand = invoker.AddCommand(PSCommandName);

            // Initialize the arguments

            if (Name.Expression != null)
            {
                targetCommand.AddParameter("Name", Name.Get(context));
            }

            if (LiteralName.Expression != null)
            {
                targetCommand.AddParameter("LiteralName", LiteralName.Get(context));
            }

            if (Scope.Expression != null)
            {
                targetCommand.AddParameter("Scope", Scope.Get(context));
            }

            if (PSProvider.Expression != null)
            {
                targetCommand.AddParameter("PSProvider", PSProvider.Get(context));
            }


            return(new ActivityImplementationContext()
            {
                PowerShellInstance = invoker
            });
        }
Ejemplo n.º 2
0
        private Object ParseNextToken(PushbackStream pis)
        {
            Object retval   = null;
            int    nextByte = pis.ReadByte();

            //skip whitespace
            while (nextByte == 0x09 || nextByte == 0x20 || nextByte == 0x0D || nextByte == 0x0A)
            {
                nextByte = pis.ReadByte();
            }
            switch (nextByte)
            {
            case '%':
            {
                //header operations, for now return the entire line
                //may need to smarter in the future
                StringBuilder buffer = new StringBuilder();
                buffer.Append((char)nextByte);
                ReadUntilEndOfLine(pis, buffer);
                retval = buffer.ToString();
                break;
            }

            case '(':
            {
                StringBuilder buffer     = new StringBuilder();
                int           stringByte = pis.ReadByte();

                while (stringByte != -1 && stringByte != ')')
                {
                    buffer.Append((char)stringByte);
                    stringByte = pis.ReadByte();
                }
                retval = buffer.ToString();
                break;
            }

            case '>':
            {
                int secondCloseBrace = pis.ReadByte();
                if (secondCloseBrace == '>')
                {
                    retval = MARK_END_OF_DICTIONARY;
                }
                else
                {
                    throw new IOException(MessageLocalization.GetComposedMessage("error.expected.the.end.of.a.dictionary"));
                }
                break;
            }

            case ']':
            {
                retval = MARK_END_OF_ARRAY;
                break;
            }

            case '[':
            {
                IList <byte[]> list = new List <byte[]>();

                Object nextToken = ParseNextToken(pis);
                while (!MARK_END_OF_ARRAY.Equals(nextToken))
                {
                    list.Add((byte[])nextToken);
                    nextToken = ParseNextToken(pis);
                }
                retval = list;
                break;
            }

            case '<':
            {
                int theNextByte = pis.ReadByte();
                if (theNextByte == '<')
                {
                    IDictionary <String, Object> result = new Dictionary <String, Object>();
                    //we are reading a dictionary
                    Object key = ParseNextToken(pis);
                    while (key is LiteralName && !MARK_END_OF_DICTIONARY.Equals(key))
                    {
                        Object value = ParseNextToken(pis);
                        result[((LiteralName)key).name] = value;
                        key = ParseNextToken(pis);
                    }
                    retval = result;
                }
                else
                {
                    //won't read more than 512 bytes

                    int multiplyer  = 16;
                    int bufferIndex = -1;
                    while (theNextByte != -1 && theNextByte != '>')
                    {
                        int intValue = 0;
                        if (theNextByte >= '0' && theNextByte <= '9')
                        {
                            intValue = theNextByte - '0';
                        }
                        else if (theNextByte >= 'A' && theNextByte <= 'F')
                        {
                            intValue = 10 + theNextByte - 'A';
                        }
                        else if (theNextByte >= 'a' && theNextByte <= 'f')
                        {
                            intValue = 10 + theNextByte - 'a';
                        }
                        else if (theNextByte == 0x20 || theNextByte == 0x09)
                        {
                            // skipping whitespaces - from pdf's generated by Mac osx
                            theNextByte = pis.ReadByte();
                            continue;
                        }
                        else
                        {
                            throw new IOException(MessageLocalization.GetComposedMessage("error.expected.hex.character.and.not.char.thenextbyte.1", theNextByte));
                        }
                        intValue *= multiplyer;
                        if (multiplyer == 16)
                        {
                            bufferIndex++;
                            tokenParserByteBuffer[bufferIndex] = 0;
                            multiplyer = 1;
                        }
                        else
                        {
                            multiplyer = 16;
                        }
                        tokenParserByteBuffer[bufferIndex] += (byte)intValue;
                        theNextByte = pis.ReadByte();
                    }
                    byte[] finalResult = new byte[bufferIndex + 1];
                    System.Array.Copy(tokenParserByteBuffer, 0, finalResult, 0, bufferIndex + 1);
                    retval = finalResult;
                }
                break;
            }

            case '/':
            {
                StringBuilder buffer     = new StringBuilder();
                int           stringByte = pis.ReadByte();

                while (!IsWhitespaceOrEOF(stringByte))
                {
                    buffer.Append((char)stringByte);
                    stringByte = pis.ReadByte();
                }
                retval = new LiteralName(buffer.ToString());
                break;
            }

            case -1:
            {
                //EOF return null;
                break;
            }

            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
            {
                StringBuilder buffer = new StringBuilder();
                buffer.Append((char)nextByte);
                nextByte = pis.ReadByte();

                while (!IsWhitespaceOrEOF(nextByte) &&
                       (Char.IsDigit((char)nextByte) ||
                        nextByte == '.'))
                {
                    buffer.Append((char)nextByte);
                    nextByte = pis.ReadByte();
                }
                pis.Unread(nextByte);
                String value = buffer.ToString();
                if (value.IndexOf('.') >= 0)
                {
                    retval = double.Parse(value, CultureInfo.InvariantCulture);
                }
                else
                {
                    retval = int.Parse(value, CultureInfo.InvariantCulture);
                }
                break;
            }

            default:
            {
                StringBuilder buffer = new StringBuilder();
                buffer.Append((char)nextByte);
                nextByte = pis.ReadByte();

                while (!IsWhitespaceOrEOF(nextByte))
                {
                    buffer.Append((char)nextByte);
                    nextByte = pis.ReadByte();
                }
                retval = new Operator(buffer.ToString());

                break;
            }
            }
            return(retval);
        }
Ejemplo n.º 3
0
        private Object ParseNextToken( PushbackStream pis ) 
        {
            Object retval = null;
            int nextByte = pis.ReadByte();
            //skip whitespace
            while ( nextByte == 0x09 || nextByte == 0x20 || nextByte == 0x0D || nextByte == 0x0A )
            {
                nextByte = pis.ReadByte();
            }
            switch ( nextByte )
            {
                case '%':
                {
                    //header operations, for now return the entire line
                    //may need to smarter in the future
                    StringBuilder buffer = new StringBuilder();
                    buffer.Append( (char)nextByte );
                    ReadUntilEndOfLine( pis, buffer );
                    retval = buffer.ToString();
                    break;
                }
                case '(':
                {
                    StringBuilder buffer = new StringBuilder();
                    int stringByte = pis.ReadByte();

                    while ( stringByte != -1 && stringByte != ')' )
                    {
                        buffer.Append( (char)stringByte );
                        stringByte = pis.ReadByte();
                    }
                    retval = buffer.ToString();
                    break;
                }
                case '>':
                {
                    int secondCloseBrace = pis.ReadByte();
                    if ( secondCloseBrace == '>' )
                    {
                        retval = MARK_END_OF_DICTIONARY;
                    }
                    else
                    {
                        throw new IOException(MessageLocalization.GetComposedMessage("error.expected.the.end.of.a.dictionary"));
                    }
                    break;
                }
                case ']':
                {
                    retval = MARK_END_OF_ARRAY;
                    break;
                }
                case '[':
                {
                    IList<Object> list = new List<Object>();

                    Object nextToken = ParseNextToken( pis );
                    while (!MARK_END_OF_ARRAY.Equals(nextToken) )
                    {
                        list.Add( nextToken );
                        nextToken = ParseNextToken( pis );
                    }
                    retval = list;
                    break;
                }
                case '<':
                {
                    int theNextByte = pis.ReadByte();
                    if ( theNextByte == '<' )
                    {
                        IDictionary<String, Object> result = new Dictionary<String, Object>();
                        //we are reading a dictionary
                        Object key = ParseNextToken( pis );
                        while ( key is LiteralName && !MARK_END_OF_DICTIONARY.Equals(key) )
                        {
                            Object value = ParseNextToken( pis );
                            result[((LiteralName)key).name] = value;
                            key = ParseNextToken( pis );
                        }
                        retval = result;
                    }
                    else
                    {
                        //won't read more than 512 bytes

                        int multiplyer = 16;
                        int bufferIndex = -1;
                        while ( theNextByte != -1 && theNextByte != '>' )
                        {
                            int intValue = 0;
                            if ( theNextByte >= '0' && theNextByte <= '9' )
                            {
                                intValue = theNextByte - '0';
                            }
                            else if ( theNextByte >= 'A' && theNextByte <= 'F' )
                            {
                                intValue = 10 + theNextByte - 'A';
                            }
                            else if ( theNextByte >= 'a' && theNextByte <= 'f' )
                            {
                                intValue = 10 + theNextByte - 'a';
                            }
                            else
                            {
                                throw new IOException(MessageLocalization.GetComposedMessage("error.expected.hex.character.and.not.char.thenextbyte.1", theNextByte));
                            }
                            intValue *= multiplyer;
                            if ( multiplyer == 16 )
                            {
                                bufferIndex++;
                                tokenParserByteBuffer[bufferIndex] = 0;
                                multiplyer = 1;
                            }
                            else
                            {
                                multiplyer = 16;
                            }
                            tokenParserByteBuffer[bufferIndex]+= (byte)intValue;
                            theNextByte = pis.ReadByte();
                        }
                        byte[] finalResult = new byte[bufferIndex+1];
                        System.Array.Copy(tokenParserByteBuffer,0,finalResult, 0, bufferIndex+1);
                        retval = finalResult;
                    }
                    break;
                }
                case '/':
                {
                    StringBuilder buffer = new StringBuilder();
                    int stringByte = pis.ReadByte();

                    while ( !IsWhitespaceOrEOF( stringByte ) )
                    {
                        buffer.Append( (char)stringByte );
                        stringByte = pis.ReadByte();
                    }
                    retval = new LiteralName( buffer.ToString() );
                    break;
                }
                case -1:
                {
                    //EOF return null;
                    break;
                }
                case '0':
                case '1':
                case '2':
                case '3':
                case '4':
                case '5':
                case '6':
                case '7':
                case '8':
                case '9':
                {
                    StringBuilder buffer = new StringBuilder();
                    buffer.Append( (char)nextByte );
                    nextByte = pis.ReadByte();

                    while ( !IsWhitespaceOrEOF( nextByte ) &&
                            (Char.IsDigit( (char)nextByte )||
                             nextByte == '.' ) )
                    {
                        buffer.Append( (char)nextByte );
                        nextByte = pis.ReadByte();
                    }
                    pis.Unread( nextByte );
                    String value = buffer.ToString();
                    if ( value.IndexOf( '.' ) >=0 )
                    {
                        retval = double.Parse(value, CultureInfo.InvariantCulture);
                    }
                    else
                    {
                        retval = int.Parse(value, CultureInfo.InvariantCulture);
                    }
                    break;
                }
                default:
                {
                    StringBuilder buffer = new StringBuilder();
                    buffer.Append( (char)nextByte );
                    nextByte = pis.ReadByte();

                    while ( !IsWhitespaceOrEOF( nextByte ) )
                    {
                        buffer.Append( (char)nextByte );
                        nextByte = pis.ReadByte();
                    }
                    retval = new Operator( buffer.ToString() );

                    break;
                }
            }
            return retval;
        }