summaryrefslogtreecommitdiffstats
path: root/tools/Sandcastle/ProductionTransforms/AddFriendlyFilenames.xsl
blob: b8daed4b5363fc99c4f13b95e2eb940dfc7ff047 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
<?xml version="1.0"?>
<xsl:stylesheet
	xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	xmlns:msxsl="urn:schemas-microsoft-com:xslt"
	xmlns:ddue="urn:ddue-extensions"
	version="1.1">

	<msxsl:script language="C#" implements-prefix="ddue">
		<msxsl:using namespace="System" />
		<![CDATA[
			public static string getFileName(string id) {
        string fileName = id.Replace(':', '_').Replace('<', '_').Replace('>', '_');

			  if (fileName.IndexOf(".#ctor") != -1 && fileName.IndexOf("Overload") == -1)
			  {
				  fileName = "C_" + fileName.Substring(2);
				  fileName = fileName.Replace(".#ctor", ".ctor");
			  }
        else if (fileName.IndexOf(".#ctor") != -1 && fileName.IndexOf("Overload") != -1)
			  {
				  fileName = fileName.Replace("Overload", "O_T");
				  fileName = fileName.Replace(".#ctor", ".ctor");
			  }
			  else if (fileName.IndexOf(".#cctor") != -1 && fileName.IndexOf("Overload") == -1)
			  {
				  fileName = "C_" + fileName.Substring(2);
				  fileName = fileName.Replace(".#cctor", ".cctor");
			  }
        else if (fileName.IndexOf(".#cctor") != -1 && fileName.IndexOf("Overload") != -1)
			  {
				  fileName = fileName.Replace("Overload", "O_T");
				  fileName = fileName.Replace(".#cctor", ".cctor");
			  }
        else if (fileName.IndexOf("Overload") != -1)
        {
          fileName = fileName.Replace("Overload", "O_T");
        }

			  fileName = fileName.Replace('.', '_').Replace('#', '_');
			
			  int paramStart = fileName.IndexOf('(');
			  if(paramStart != -1)
			  {
				  fileName = fileName.Substring(0, paramStart) + GenerateParametersCode(id.Substring(paramStart));
			  }

			  return fileName;
			}
      
      private static string GenerateParametersCode(string parameterSection)
		  {
			  // TODO: figure out a consistent algorithm that works regardless of runtime version
			  int code = parameterSection.GetHashCode();
			
			  int parameterCount = 1;
			
			  for(int count = 0; count < parameterSection.Length; count += 1)
			  {
				  int c = (int) parameterSection[count];

				  if(c == ',')
					  ++parameterCount;
			  }

			  // format as (# of parameters)_(semi-unique hex code)
			  return string.Format("_{1}_{0:x8}", code, parameterCount);
		}
		]]>
	</msxsl:script>

	<xsl:output indent="yes" encoding="UTF-8" />
	
	<xsl:template match="/">
		<reflection>
			<xsl:apply-templates select="/*/assemblies" />
			<xsl:apply-templates select="/*/apis" />
		</reflection>
	</xsl:template>

	<xsl:template match="assemblies">
		<xsl:copy-of select="." />
	</xsl:template>

	<xsl:template match="apis">
		<apis>
			<xsl:apply-templates select="api" />
		</apis>
	</xsl:template>

	<xsl:template match="api">
		<api id="{@id}">
			<xsl:copy-of select="*" />
			<file name="{ddue:getFileName(@id)}" />
		</api>
	</xsl:template>

</xsl:stylesheet>