Click here to Skip to main content
Licence 
First Posted 28 Apr 2004
Views 20,495
Bookmarked 8 times

List Excess @Register Tags VS.NET Macro

By | 3 May 2004 | Article
When designing an as(p/c)x page you get lots of excess @register tags. This macro identifies those tags

Introduction

Ever designed a .aspx or .ascx file and ended up with lots of extra @Register tags, tags like this:

<%@ Register TagPrefix="uc1" TagName="MyControl" ... %>
<%@ Register TagPrefix="uc2" Namespace="MySite.Controls"  ... %>

This macro generates a list of unused tags in the output window.

Sub CheckRegisterTags()
    Dim str As String = ""
    'register tag regex
    Dim r As New Regex("\<%@\ Register\ TagPrefix\=\""(?<tag>\w*)\""\" & _
      "(TagName\=\""(?<name>\w*)\"")?")
    'get the document text
    Dim selection As TextSelection = DTE.ActiveDocument.Selection()
    selection.SelectAll()
    Dim theText As String = selection.Text
    
    'check the register tags against the rest of the content
    Dim matches As MatchCollection = r.Matches(theText)
    Dim m As Match
    For Each m In matches
        Dim g1 As Group = m.Groups("tag")
        Dim g2 As Group = m.Groups("name")
        Dim prefix As String = g1.Value
        Dim suffix As String = ""
        'is it a namespace or tagname tag?
        If Not (g2 Is Nothing) Then
            suffix = g2.Value
        End If
        'is the tag used?
        Dim r2 As New Regex("\<" & prefix & ":" & suffix, _
          RegexOptions.IgnoreCase)
        If Not r2.IsMatch(theText) Then
            str += m.Value
            str += vbCrLf
        End If
    Next
    selection.GotoLine(1)
    'no unused tags were found
    If str = "" Then
        str = "No excess registertags found"
    End If

    'now show the output window
    Dim win As Window = DTE.Windows.Item(EnvDTE.Constants.vsWindowKindOutput)
    Dim OWp As OutputWindowPane = win.Object.OutputWindowPanes.Item(1)
    OWp.Clear()
    OWp.Activate()
    OWp.OutputString(str)
    OWp.TextDocument.Selection.GotoLine(OWp.TextDocument.EndPoint().Line())
    DTE.ExecuteCommand("View.Output")
End Sub

The New Regex("\<%@\ Register\ TagPrefix\=\""(?<tag>\w*)\""\ (TagName\=\""(?<name>\w*)\"")?") regular expression has two named groups: <tag> and <name>. The tag is always filled with the TagPrefix value, and when available the name is filled with the TagName value.

For every Match in the Matches collection of this Regex I do a new regular expression search for "\<" & prefix & ":" & suffix (where suffix sometimes is an empty string). If I get no match I concatenate the Match.Value to the str string.

When the str string is empty there are no excess register tags.

Finally, we get the output window and write the str string to this window.

Hope this helps,
Rooc

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here

About the Author

Rooc

Web Developer

Netherlands Netherlands

Member

Developer since 1998 and mainly focused on web development. Currently employed as technical architect.

Sign Up to vote   Poor Excellent
Add a reason or comment to your vote: x
Votes of 3 or less require a comment

Comments and Discussions

 
You must Sign In to use this message board. (secure sign-in)
 
Search this forum  
 FAQ
    Noise  Layout  Per page   
  Refresh
GeneralUseful but write a whole article PinsitebuilderPaul Watson1:46 4 May '04  
GeneralRe: Useful but write a whole article PinmemberRooc2:26 4 May '04  

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Mobile
Web02 | 2.5.120517.1 | Last Updated 4 May 2004
Article Copyright 2004 by Rooc
Everything else Copyright © CodeProject, 1999-2012
Terms of Use
Layout: fixed | fluid