Monday, August 28, 2006

Impersonation in .Net

There are scenarios when you may want to impersonate in .Net. What this means is you want to execute a part of your code with different authentication information. There are numerous scenarios when you may want to do it. An example: suppose you want to write a file to shared location where the user authentication running the application doesn't have write permissions. So what do you do? Impersonate your piece of code as the user who has rights to write file.

Here's the Microsoft kb article to how to impersonate your piece of code:
http://support.microsoft.com/kb/306158/

Sunday, August 20, 2006

CTRL + C, CTRL + V

Have you ever been in a situation where a dialog box pops up with some message which you need to copy? Many times. Right? Well, at least I have in the course of my development career.

Till date I was not aware of an easy way to copy this text and was manually typing the content. But recently one of my friend showed me a very simple way to do this, CTRL+C, CTRL+V. Yes, it works for dialog boxes. Whenever a dialog box pops up, just do a copy paste and you have the contents.

That's the power of copy-paste ;)

Monday, August 14, 2006

JavaScript | Redirect Parent Window

Have you ever faced a situation where you wanted to close a child window and redirect parent window to a new URL?

Well the solution to this problem is a simple javaScript. It goes something like this (I have executed the script on page load. It can be used with any event)

<body onload="opener.location='http://google.com';self.close();">

Sunday, August 13, 2006

Interesting XSLT Problem

This weekend I worked on a very interesting XSLT problem. I had to convert an XML like:



<?xml version="1.0" encoding="UTF-8"?>
<category>
    <type>1</type>
    <value>20</value>
</category>
<category>
    <type>2</type>
    <value>10</value>
</category>
<category>
    <type>1</type>
    <value>25</value>
</category>
<category>
    <type>2</type>
    <value>40</value>
</category>
<category>
    <type>2</type>
    <value>41</value>
</category>
</categories>


to something like:


<?xml version="1.0" encoding="UTF-8"?>
<output>
    <grid>
        <type val="1">
            <value>20</value>
            <value>-NA-</value>
            <value>25</value>
            <value>-NA-</value>
            <value>-NA-</value>
    </type>
        <type val="2">
            <value>-NA-</value>
            <value>10</value>
            <value>-NA-</value>
            <value>40</value>
            <value>41</value>
        </type>
    </grid>
</output>

I spent couple of hours figuring out how to solve it. Then I came across an interesting XPATH statement at http://www.topxml.com/code/default.asp?p=3&id=v20040207215613&ms=100&l=&sw=All to retrieve distinc values. I worked upon it and wrote the following XSLT which strangely worked :)



<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
    <xsl:output method="xml" version="1.0" encoding="UTF-8" indent="yes"/>
    <xsl:template match="/">
        <Output>
            <Grid>
                <xsl:for-each select="//category[not(type=preceding::category/type)]/type">
                    <xsl:element name="type">
                        <xsl:attribute name="val"><xsl:value-of select="."/></xsl:attribute>
                        <xsl:call-template name="map">
                            <xsl:with-param name="type">
                                <xsl:value-of select="."/>
                            </xsl:with-param>
                        </xsl:call-template>
                    </xsl:element>
                </xsl:for-each>
            </Grid>
        </Output>
    </xsl:template>
    <!--Called Once for each type -->
    <xsl:template name="map">
        <xsl:param name="type"/>
        <xsl:for-each select="//category[not(value=preceding::category/value)]/value">
            <xsl:call-template name="checkAvailability">
                <xsl:with-param name="type">
                    <xsl:value-of select="$type"/>
                </xsl:with-param>
                <xsl:with-param name="value">
                    <xsl:value-of select="."/>
                </xsl:with-param>
            </xsl:call-template>
        </xsl:for-each>
    </xsl:template>
    <!-- Called once for each value -->
    <xsl:template name="checkAvailability">
        <xsl:param name="type"/>
        <xsl:param name="value"/>
        <value>
            <xsl:choose>
                <xsl:when test="//category[type=$type and value=$value]">
                    <xsl:value-of select="$value"/>
                </xsl:when>
                <xsl:otherwise>
                    <xsl:text>-NA-</xsl:text>
                </xsl:otherwise>
            </xsl:choose>
        </value>
    </xsl:template>
</xsl:stylesheet>