001: /***************************************************************
002: * This file is part of the [fleXive](R) project.
003: *
004: * Copyright (c) 1999-2007
005: * UCS - unique computing solutions gmbh (http://www.ucs.at)
006: * All rights reserved
007: *
008: * The [fleXive](R) project is free software; you can redistribute
009: * it and/or modify it under the terms of the GNU General Public
010: * License as published by the Free Software Foundation;
011: * either version 2 of the License, or (at your option) any
012: * later version.
013: *
014: * The GNU General Public License can be found at
015: * http://www.gnu.org/copyleft/gpl.html.
016: * A copy is found in the textfile GPL.txt and important notices to the
017: * license from the author are found in LICENSE.txt distributed with
018: * these libraries.
019: *
020: * This library is distributed in the hope that it will be useful,
021: * but WITHOUT ANY WARRANTY; without even the implied warranty of
022: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
023: * GNU General Public License for more details.
024: *
025: * For further information about UCS - unique computing solutions gmbh,
026: * please see the company website: http://www.ucs.at
027: *
028: * For further information about [fleXive](R), please see the
029: * project website: http://www.flexive.org
030: *
031: *
032: * This copyright notice MUST APPEAR in all copies of the file!
033: ***************************************************************/package com.flexive.faces.javascript;
034:
035: import org.apache.commons.lang.StringUtils;
036:
037: import java.io.IOException;
038: import java.io.Writer;
039:
040: /**
041: * Utility functions for handling/creating javascript code.
042: *
043: * @author Daniel Lichtenberger (daniel.lichtenberger@flexive.com), UCS - unique computing solutions gmbh (http://www.ucs.at)
044: * @version $Rev: 1 $
045: */
046: public class FxJavascriptUtils {
047:
048: /**
049: * Private c'tor to avoid instantiation
050: */
051: private FxJavascriptUtils() {
052: }
053:
054: /**
055: * Start a javascript block.
056: *
057: * @param out the output writer
058: * @throws IOException if the code could not be written
059: */
060: public static void beginJavascript(Writer out) throws IOException {
061: out.write("<script type=\"text/javascript\">\n<!--\n");
062: }
063:
064: /**
065: * End a javascript block.
066: *
067: * @param out the output writer
068: * @throws IOException if the code could not be written
069: */
070: public static void endJavascript(Writer out) throws IOException {
071: out.write("\n//-->\n</script>");
072: }
073:
074: /**
075: * Write dojo "require" calls for all requested components.
076: *
077: * @param out the output writer
078: * @param packages array of packages to be written (e.g. "dojo.widget.*")
079: * @throws IOException if the code could not be written
080: */
081: public static void writeDojoRequires(Writer out, String... packages)
082: throws IOException {
083: for (String pkg : packages) {
084: out.write("dojo.require(\"" + pkg + "\");\n");
085: }
086: }
087:
088: /**
089: * Converts the given fully qualified widget name (e.g. dojo.widget.Menu2) to
090: * the widget name used createWidget. The package prefix "flexive.widget" is converted
091: * to the namespace prefix "flexive:".
092: *
093: * @param fqn a fully qualified widget name, e.g. "dojo.widget.Menu2"
094: * @return the widget name for dojo.widget.createWidget
095: */
096: public static String getWidgetName(String fqn) {
097: return StringUtils.replace(StringUtils.replace(fqn,
098: "flexive.widget.", "flexive:"), "dojo.widget.", "");
099: }
100: }
|