source: main/trunk/greenstone3/web/xslt-util.js@ 35896

Last change on this file since 35896 was 35896, checked in by cstephen, 2 years ago

Upgrade web root jQuery and fix clientside XSLT processing.

  • Property svn:executable set to *
File size: 5.0 KB
Line 
1"use strict";
2
3
4
5var foo_count = 0;
6
7function gitDebug(key,val,args) {
8 console.log(key+"="+val+" ("+args+")");
9 foo_count++;
10 return val;
11}
12
13
14// Port of methods from GSXLUtil.java, XSLTUtil.java and Dictionary.java to support client side XSLT
15
16// Function for inserting args into retrieved interface string
17function getInterfaceTextSubstituteArgs(initial,argsStr)
18{
19 var args = argsStr.split(";");
20
21 var complete = ""; //new StringBuffer();
22
23 // While we still have initial string left.
24 while ((initial.length > 0) && (initial.indexOf('{') != -1) && (initial.indexOf('}') != -1))
25 {
26 // Remove preamble
27 var opening = initial.indexOf('{');
28 var closing = initial.indexOf('}');
29 var comment_mark = initial.indexOf('-', opening); // May not exist
30 if (comment_mark > closing)
31 { // May also be detecting a later comment
32 comment_mark = -1;
33 }
34 complete += initial.substring(0, opening);
35
36 // Parse arg_num
37 var arg_str = null;
38 if (comment_mark != -1)
39 {
40 arg_str = initial.substring(opening + 1, comment_mark);
41 }
42 else
43 {
44 arg_str = initial.substring(opening + 1, closing);
45 }
46 if (closing + 1 < initial.length)
47 {
48 initial = initial.substring(closing + 1);
49 }
50 else
51 {
52 initial = "";
53 }
54
55 var arg_num = Number(arg_str);
56 // Insert argument
57 if ((args != null) && (0 <= arg_num) && (arg_num < args.length))
58 {
59 complete += args[arg_num];
60 }
61 }
62
63 complete += initial;
64
65 return complete;
66}
67
68var xsltUtil_stringVariables = {};
69
70//public static void storeString(String name, String value)
71function storeString(name,value)
72{
73 xsltUtil_stringVariables[name] = value;
74}
75
76//public static String getString(String name)
77function getString(name)
78{
79 return xsltUtil_stringVariables[name];
80}
81
82
83//public static String escapeNewLines(String str)
84function escapeNewLines(str)
85{
86 if (str == null || str.length < 1)
87 {
88 return null;
89 }
90 return str.replace("\n", "\\\n");
91}
92
93//public static String escapeQuotes(String str)
94function escapeQuotes(str)
95{
96 if (str == null || str.length < 1)
97 {
98 return null;
99 }
100 return str.replace("\"", "\\\"");
101}
102
103//public static String escapeNewLinesAndQuotes(String str)
104function escapeNewLinesAndQuotes(str)
105{
106 if (str == null || str.length < 1)
107 {
108 return null;
109 }
110 return escapeNewLines(escapeQuotes(str));
111}
112
113// public static String escapeNewLinesQuotesAngleBracketsForJSString(String str)
114function escapeNewLinesQuotesAngleBracketsForJSString(str)
115{
116 // The \n and " becomes \\\n and \\\"
117 // but the <> are escaped/encoded for html, i.e. &gt; and &lt;
118 if (str == null || str.length < 1)
119 {
120 return null;
121 }
122
123 return escapeAngleBrackets(escapeNewLines(escapeQuotes(str)));
124}
125
126// public static String escapeAngleBrackets(String str)
127function escapeAngleBrackets(str)
128{
129 if (str == null || str.length < 1)
130 {
131 return null;
132 }
133
134 return str.replace("<", "&lt;").replace(">", "&gt;");
135}
136
137function getNumberedItem(list, number)
138{
139 var items = list.split(",");
140
141 if (items.length > number)
142 {
143 return items[number];
144 }
145
146 return ""; // index out of bounds
147}
148
149
150
151
152//public static boolean oidIsMatchOrParent(String first, String second)
153function oidIsMatchOrParent(first, second)
154{
155 if (first == second)
156 {
157 return true;
158 }
159
160 var firstParts = first.split(".");
161 var secondParts = second.split(".");
162
163 if (firstParts.length >= secondParts.length)
164 {
165 return false;
166 }
167
168 for (var i = 0; i < firstParts.length; i++)
169 {
170 if (!firstParts[i].equals(secondParts[i]))
171 {
172 return false;
173 }
174 }
175
176 return true;
177}
178
179//public static String oidDocumentRoot(String oid)
180function oidDocumentRoot(oid)
181{
182 var oidParts = oid.split("\\.");
183
184 return oidParts[0];
185}
186
187
188
189//public static String hashToSectionId(String hashString)
190function hashToSectionId(hashString)
191{
192 if (hashString == null || hashString.length == 0)
193 {
194 return "";
195 }
196
197 var firstDotIndex = hashString.indexOf(".");
198 if (firstDotIndex == -1)
199 {
200 return "";
201 }
202
203 var sectionString = hashString.substring(firstDotIndex + 1);
204
205 return sectionString;
206}
207
208//public static String hashToDepthClass(hashString)
209function hashToDepthClass(hashString)
210{
211 if (hashString == null || hashString.length == 0)
212 {
213 return "";
214 }
215
216 var sectionString = hashToSectionId(hashString);
217
218 var count = sectionString.split("\\.").length;
219
220 if (sectionString == "")
221 {
222 return "sectionHeaderDepthTitle";
223 }
224 else
225 {
226 return "sectionHeaderDepth" + count;
227 }
228}
229
230//alert("hashToDepthClass(\"HASH134B.1\")=" + hashToDepthClass("HASH134B.1"));
231//alert("hashToSectionId(\"HASH134B.1\")=" + hashToSectionId("HASH134B.1"));
232
233//alert(getInterfaceTextSubstituteArgs("test {0} test {1} test" ,"1;2"));
234//alert(getNumberedItem("item0,item1,item2" ,1));
Note: See TracBrowser for help on using the repository browser.