001: /*******************************************************************************
002: * $URL: https://source.sakaiproject.org/svn/metaobj/tags/sakai_2-4-1/metaobj-util/tool-lib/src/java/org/sakaiproject/metaobj/utils/xml/XsltFunctions.java $
003: * $Id: XsltFunctions.java 22432 2007-03-12 16:32:07Z john.ellis@rsmart.com $
004: * **********************************************************************************
005: *
006: * Copyright (c) 2004, 2005, 2006 The Sakai Foundation.
007: *
008: * Licensed under the Educational Community License, Version 1.0 (the "License");
009: * you may not use this file except in compliance with the License.
010: * You may obtain a copy of the License at
011: *
012: * http://www.opensource.org/licenses/ecl1.php
013: *
014: * Unless required by applicable law or agreed to in writing, software
015: * distributed under the License is distributed on an "AS IS" BASIS,
016: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
017: * See the License for the specific language governing permissions and
018: * limitations under the License.
019: *
020: ******************************************************************************/package org.sakaiproject.metaobj.utils.xml;
021:
022: import org.w3c.dom.Node;
023: import org.w3c.dom.NodeList;
024: import org.w3c.dom.Document;
025: import org.w3c.dom.Element;
026: import org.sakaiproject.component.cover.ServerConfigurationService;
027: import org.sakaiproject.content.cover.ContentHostingService;
028: import org.sakaiproject.content.cover.ContentTypeImageService;
029: import org.sakaiproject.tool.cover.ToolManager;
030: import org.sakaiproject.util.Xml;
031: import org.sakaiproject.util.ResourceLoader;
032: import org.sakaiproject.util.DateWidgetFormat;
033: import org.sakaiproject.entity.api.Reference;
034: import org.sakaiproject.entity.cover.EntityManager;
035:
036: import java.util.*;
037: import java.text.*;
038:
039: /**
040: * Created by IntelliJ IDEA.
041: * User: johnellis
042: * Date: Nov 20, 2006
043: * Time: 8:27:06 AM
044: * To change this template use File | Settings | File Templates.
045: */
046: public class XsltFunctions {
047:
048: private static final String DATE_FORMAT = "yyyy-MM-dd";
049: private static final String TIME_FORMAT = "HH:mm:ss.SSSZ";
050: private static final String DATE_TIME_FORMAT = DATE_FORMAT + "'T'"
051: + TIME_FORMAT;
052:
053: private static final Format dateTimeFormat = new SimpleDateFormat(
054: DATE_TIME_FORMAT);
055: private static final Format dateFormat = new SimpleDateFormat(
056: DATE_FORMAT);
057: private static final Format timeFormat = new SimpleDateFormat(
058: TIME_FORMAT);
059:
060: private static Map<String, ResourceLoader> resourceLoaders = new Hashtable<String, ResourceLoader>();
061:
062: public static String getRichTextScript(String textBoxId,
063: Node schemaElement) {
064:
065: String script = "";
066:
067: String editor = ServerConfigurationService
068: .getString("wysiwyg.editor");
069: String twinpeaks = ServerConfigurationService
070: .getString("wysiwyg.twinpeaks");
071: String collectionId = "/";
072: if (ToolManager.getCurrentPlacement() != null) {
073: collectionId = ContentHostingService
074: .getSiteCollection(ToolManager
075: .getCurrentPlacement().getContext());
076: }
077:
078: if (editor.equalsIgnoreCase("FCKeditor")) {
079: script += "\t<script type=\"text/javascript\" language=\"JavaScript\">\n"
080: + "\n"
081: + "\tfunction chef_setupformattedtextarea(textarea_id)\n"
082: + "\t{\n"
083: + " \tvar oFCKeditor = new FCKeditor(textarea_id);\n"
084: + "\t\toFCKeditor.BasePath = \"/library/editor/FCKeditor/\";\n"
085: + "\n"
086: + " var courseId = \""
087: + collectionId
088: + "\";\n"
089: + "\n"
090: + " oFCKeditor.Config['ImageBrowserURL'] = oFCKeditor.BasePath + \"editor/filemanager/browser/default/browser.html?Connector=/sakai-fck-connector/web/editor/filemanager/browser/default/connectors/jsp/connector&Type=Image&CurrentFolder=\" + courseId;\n"
091: + " oFCKeditor.Config['LinkBrowserURL'] = oFCKeditor.BasePath + \"editor/filemanager/browser/default/browser.html?Connector=/sakai-fck-connector/web/editor/filemanager/browser/default/connectors/jsp/connector&Type=Link&CurrentFolder=\" + courseId;\n"
092: + " oFCKeditor.Config['FlashBrowserURL'] = oFCKeditor.BasePath + \"editor/filemanager/browser/default/browser.html?Connector=/sakai-fck-connector/web/editor/filemanager/browser/default/connectors/jsp/connector&Type=Flash&CurrentFolder=\" + courseId;\n"
093: + " oFCKeditor.Config['ImageUploadURL'] = oFCKeditor.BasePath + \"/sakai-fck-connector/web/editor/filemanager/browser/default/connectors/jsp/connector?Type=Image&Command=QuickUpload&Type=Image&CurrentFolder=\" + courseId;\n"
094: + " oFCKeditor.Config['FlashUploadURL'] = oFCKeditor.BasePath + \"/sakai-fck-connector/web/editor/filemanager/browser/default/connectors/jsp/connector?Type=Flash&Command=QuickUpload&Type=Flash&CurrentFolder=\" + courseId;\n"
095: + " oFCKeditor.Config['LinkUploadURL'] = oFCKeditor.BasePath + \"/sakai-fck-connector/web/editor/filemanager/browser/default/connectors/jsp/connector?Type=File&Command=QuickUpload&Type=Link&CurrentFolder=\" + courseId;\n"
096: + "\t\toFCKeditor.Width = \"600\" ;\n"
097: + "\t\toFCKeditor.Height = \"400\" ;\n"
098: + " oFCKeditor.Config['CustomConfigurationsPath'] = \"/library/editor/FCKeditor/config.js\";\n"
099: + " \t\toFCKeditor.ReplaceTextarea() ;\n"
100: + "\t}\n"
101: + "\t\n"
102: + "</script>\n"
103: + "\n"
104: + "\n"
105: + "\n";
106: } else {
107: script += "\t<script type=\"text/javascript\" language=\"JavaScript\">\n"
108: + "\tvar _editor_url = \"/library/editor/"
109: + editor
110: + "\"\n"
111: + "\tvar _editor_lang = \"en\";\n"
112: + "</script>\n"
113: + "\t<script type=\"text/javascript\" language=\"JavaScript\" src=\"/library/editor/"
114: + editor
115: + "/htmlarea.js\">\n"
116: + "\t</script>\n"
117: + "\t<script type=\"text/javascript\" language=\"JavaScript\" src=\"/library/editor/"
118: + editor + "/sakai_editor";
119: if (twinpeaks != null && twinpeaks.equalsIgnoreCase("true")) {
120: script += "_twinpeaks";
121: }
122:
123: script += ".js\">\n" + "\t</script>\n" + "";
124: }
125:
126: script += "\t<script type=\"text/javascript\" defer=\"1\">chef_setupformattedtextarea('"
127: + textBoxId + "');</script>";
128:
129: return script;
130: }
131:
132: public static String formatDate(String date, String format) {
133: return formatDate(date, new SimpleDateFormat(format));
134: }
135:
136: protected static String formatDate(String date, DateFormat format) {
137: Date dateObject = null;
138: if (date == null || date.equals("")) {
139: return "";
140: } else {
141: try {
142: dateObject = (Date) dateFormat.parseObject(date);
143: return format.format(dateObject);
144: } catch (ParseException e) {
145: throw new RuntimeException(e);
146: }
147: }
148: }
149:
150: public static long dateField(String date, int field, String type) {
151: Format useFormat = dateTimeFormat;
152: if (type.equalsIgnoreCase("date")) {
153: useFormat = dateFormat;
154: } else if (type.equals("time")) {
155: useFormat = timeFormat;
156: }
157:
158: Date dateObject = null;
159: if (date == null || date.equals("")) {
160: return -1;
161: } else {
162: try {
163: dateObject = (Date) useFormat.parseObject(date);
164: } catch (ParseException e) {
165: throw new RuntimeException(e);
166: }
167: }
168: Calendar cal = Calendar.getInstance();
169: cal.setTime(dateObject);
170: return cal.get(field);
171: }
172:
173: public static String getDateWidget(String fieldId, String name) {
174: String calType = "2"; // default to US
175: DateWidgetFormat format = new DateWidgetFormat();
176:
177: if (format.getLocaleDateFormat() == DateWidgetFormat.DD_MM_YYYY) {
178: calType = "1"; // European type
179: }
180:
181: String javascript = "javascript:var cal" + fieldId
182: + " = new calendar" + calType
183: + "(document.getElementById('" + name + "'));cal"
184: + fieldId + ".year_scroll = true;cal" + fieldId
185: + ".time_comp = false;cal" + fieldId
186: + ".popup('','/sakai-jsf-resource/inputDate/')";
187:
188: return javascript;
189: }
190:
191: public static String formatDateWidget(String date) {
192: DateWidgetFormat format = new DateWidgetFormat();
193: return formatDate(date, format.getLocaleDateFormat());
194: }
195:
196: public static NodeList loopCounter(int start, int end) {
197: Document doc = Xml.createDocument();
198:
199: Element parent = doc.createElement("parent");
200:
201: for (int i = start; i <= end; i++) {
202: Element data = doc.createElement("data");
203: data.appendChild(doc.createTextNode(i + ""));
204: parent.appendChild(data);
205: }
206:
207: return parent.getElementsByTagName("data");
208: }
209:
210: public static String currentDate() {
211: return dateFormat.format(new Date());
212: }
213:
214: public static String getMessage(String loaderKey, String key) {
215: ResourceLoader loader = getLoader(loaderKey);
216:
217: return (String) loader.get(key);
218: }
219:
220: public static String formatMessage(String loaderKey, String key,
221: Object arg1) {
222: return formatMessageInternal(loaderKey, key, arg1);
223: }
224:
225: public static String formatMessage(String loaderKey, String key,
226: Object arg1, Object arg2) {
227: return formatMessageInternal(loaderKey, key, arg1, arg2);
228: }
229:
230: public static String formatMessage(String loaderKey, String key,
231: Object arg1, Object arg2, Object arg3) {
232: return formatMessageInternal(loaderKey, key, arg1, arg2, arg3);
233: }
234:
235: public static String formatMessage(String loaderKey, String key,
236: Object arg1, Object arg2, Object arg3, Object arg4) {
237: return formatMessageInternal(loaderKey, key, arg1, arg2, arg3,
238: arg4);
239: }
240:
241: public static String formatMessage(String loaderKey, String key,
242: Object arg1, Object arg2, Object arg3, Object arg4,
243: Object arg5) {
244: return formatMessageInternal(loaderKey, key, arg1, arg2, arg3,
245: arg4, arg5);
246: }
247:
248: public static String formatMessage(String loaderKey, String key,
249: Object arg1, Object arg2, Object arg3, Object arg4,
250: Object arg5, Object arg6) {
251: return formatMessageInternal(loaderKey, key, arg1, arg2, arg3,
252: arg4, arg5, arg6);
253: }
254:
255: public static String formatMessage(String loaderKey, String key,
256: Object arg1, Object arg2, Object arg3, Object arg4,
257: Object arg5, Object arg6, Object arg7) {
258: return formatMessageInternal(loaderKey, key, arg1, arg2, arg3,
259: arg4, arg5, arg6, arg7);
260: }
261:
262: public static String formatMessageInternal(String loaderKey,
263: String key, Object... arguments) {
264: ResourceLoader loader = getLoader(loaderKey);
265:
266: String format = (String) loader.get(key);
267: return MessageFormat.format(format, arguments);
268: }
269:
270: public static ResourceLoader getLoader(String loaderKey) {
271: ResourceLoader loader = resourceLoaders.get(loaderKey);
272:
273: if (loader == null) {
274: loader = new ResourceLoader(loaderKey);
275: registerLoader(loaderKey, loader);
276: }
277: return loader;
278: }
279:
280: public static void registerLoader(String key, ResourceLoader loader) {
281: resourceLoaders.put(key, loader);
282: }
283:
284: public static String getReferenceName(String idString) {
285: String refString = ContentHostingService.getReference(idString);
286: Reference ref = EntityManager.newReference(refString);
287:
288: if (ref == null || ref.getEntity() == null) {
289: return "";
290: }
291:
292: String prop = ref.getEntity().getProperties()
293: .getNamePropDisplayName();
294: return ref.getEntity().getProperties().getProperty(prop);
295: }
296:
297: public static String getReferenceUrl(String idString) {
298: String refString = ContentHostingService.getReference(idString);
299: Reference ref = EntityManager.newReference(refString);
300:
301: if (ref == null || ref.getEntity() == null) {
302: return "";
303: }
304:
305: return ref.getUrl();
306: }
307:
308: public static String getImageUrl(String idString) {
309: String refString = ContentHostingService.getReference(idString);
310: Reference ref = EntityManager.newReference(refString);
311:
312: if (ref == null) {
313: return "/library/image/sakai/unknown.gif";
314: }
315:
316: return "/library/image"
317: + ContentTypeImageService.getContentTypeImage(ref
318: .getType());
319: }
320:
321: }
|