This repository has been archived by the owner on May 12, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
prefixchecker.cfc
169 lines (137 loc) · 5.39 KB
/
prefixchecker.cfc
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
component
{
/*public prefixchecker function init2(required string filePath) {
variables.myfile = fileOpen(arguments.filePath, "read");
variables.lines = [];
while(not fileisEOF(variables.myfile)) {
arrayAppend(variables.lines, fileReadLine(variables.myfile));
}
fileClose(myfile);
return this;
}*/
public prefixchecker function init() {
return this;
}
public array function arrayMerge(required array array1, required array array2) {
for(var i = 1; i lte arrayLen(array2); i = i + 1) {
arrayAppend(array1, array2[i]);
}
return array1;
}
private array function comparePrefixArrays(required array prefixArray1, required array prefixArray2, required string message) {
var prefixedViolations = {};
var prefixManifest = {};
var violations = "";
for (var i = 1; i lte arrayLen(arguments.prefixArray1); i = i + 1) {
if (not structKeyExists(prefixedViolations, arguments.prefixArray1[i].prefix)) {
structInsert(prefixedViolations, arguments.prefixArray1[i].prefix, []);
}
for (var j = 1; j lte arraylen(arguments.prefixArray2); j = j + 1) {
// Key not found
var formattedMessage = replaceList(arguments.message, '{1},{2}', "#arguments.prefixArray1[i].lineNumber#,#arguments.prefixArray1[i].prefix#");
if (arrayLen(structFindValue(arguments.prefixArray2[j], arguments.prefixArray1[i].prefix, "all")) eq 0) {
// Don't override a true value with false - true is sticky
if (not structKeyExists(prefixManifest, arguments.prefixArray1[i].prefix) or not prefixManifest[arguments.prefixArray1[i].prefix]) {
prefixManifest[arguments.prefixArray1[i].prefix] = false;
}
}
else {
prefixManifest[arguments.prefixArray1[i].prefix] = true;
}
//arrayAppend(prefixedViolations[arguments.prefixArray1[i].prefix], arguments.prefixArray1[i].prefix & ' found: ' & arrayLen(structFindValue(arguments.prefixArray2[j], arguments.prefixArray1[i].prefix, "all")));
if (not arrayFind(prefixedViolations[arguments.prefixArray1[i].prefix], formattedMessage)) {
arrayAppend(prefixedViolations[arguments.prefixArray1[i].prefix], formattedMessage);
}
}
}
// Delete the array of messages for prefix keys that *were* found
for (prefix in prefixManifest) {
if (prefixManifest[prefix]) {
structDelete(prefixedViolations, prefix);
}
else {
violations = listAppend(violations, arrayToList(prefixedViolations[prefix]));
}
}
return listToArray(violations);
}
/**
* checkFile checks an individual file for cfimport prefix problems
*/
private array function checkFile(required string filePath) {
var violations = [];
var prefixes = [];
var prefixUsages = [];
var myfile = fileOpen(arguments.filePath, "read");
var lineNumber = 1;
while(not fileisEOF(myfile)) {
var line = fileReadLine(myfile); // read line
// Look for prefixes in cfimport tags
var importSearch = refindNoCase("prefix=""([A-Za-z0-9]+)""", line, 1, true);
if (arraylen(importSearch.pos) gt 1) {
var importPrefix = mid(line, importSearch.pos[2], importSearch.len[2]);
arrayAppend(prefixes, { prefix = importPrefix, lineNumber = lineNumber });
}
// Look for prefix usages
var startPos = 1;
while (startPos lte len(line)) {
var namespaceSearch = refindNoCase("<([A-Za-z0-9]+):", line, startPos, true);
if (arraylen(namespaceSearch.pos) gt 1) {
var namespacePrefix = mid(line, namespaceSearch.pos[2], namespaceSearch.len[2]);
arrayAppend(prefixUsages, { prefix = namespacePrefix, lineNumber = lineNumber });
startPos = namespaceSearch.pos[2] + namespaceSearch.len[2];
}
else {
startPos = len(line) + 1;
}
}
lineNumber = lineNumber + 1;
}
fileClose(myfile);
var unusedPrefixViolations = comparePrefixArrays(prefixes, prefixUsages, 'line {1}: declared namespace prefix "{2}" not used');
var unimportedPrefixViolations = comparePrefixArrays(prefixUsages, prefixes, 'line {1}: used namespace prefix "{2}" not cfimported');
violations = arrayMerge(unimportedPrefixViolations, unusedPrefixViolations);
return violations;
}
/**
* check Checks a file or directory for cfimport prefix problems
* @param {String} filePath
* @author Tim Beadle
*/
public array function check(required string filePath, string format='dump') {
var fileList = [];
var violations = [];
if (fileExists(arguments.filePath) or fileExists(expandPath(arguments.filePath))) {
if (fileExists(arguments.filePath)) {
fileList = [arguments.filePath];
} else {
fileList = [expandPath(arguments.filePath)];
}
}
else if (directoryExists(arguments.filePath) or directoryExists(expandPath(arguments.filePath))) {
try {
if (directoryExists(arguments.filePath)) {
//violations = [arguments.filePath];
fileList = directoryList(arguments.filePath, true, 'path', '*.cfm');
} else {
fileList = directoryList(expandPath(arguments.filePath), true, 'path', '*.cfm');
}
} catch( Any e ) {
violations = ['Foo'];
}
}
else {
//throw("File not found");
}
if (not arrayIsEmpty(fileList)) {
// Loop over our file list, checking each file
for (var i = 1; i lte arraylen(fileList); i = i + 1) {
var fileViolations = checkFile(fileList[i]);
if (not arrayIsEmpty(fileViolations)) {
arrayAppend(violations, {filePath = fileList[i], violations = fileViolations });
}
}
}
return violations;
}
}