001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: *
017: * $Header:$
018: */
019: package org.apache.beehive.netui.tags.javascript;
020:
021: import org.apache.beehive.netui.tags.AbstractSimpleTag;
022: import org.apache.beehive.netui.tags.rendering.WriteRenderAppender;
023:
024: import javax.servlet.jsp.JspException;
025: import javax.servlet.jsp.PageContext;
026: import java.io.IOException;
027:
028: /**
029: * This class will output a script block into the generated HTML. The primary reason
030: * for this class is to add the ability to add JavaScript either before or after the
031: * framework provided JavaScript.
032: * @jsptagref.tagdescription <p>Outputs a script block into the generated HTML. The primary reason
033: * for this tag is to add JavaScript either before or after the
034: * framework provided JavaScript.</p>
035: * @netui:tag name="scriptBlock" body-content="scriptless" description="Used to place JavaScript in relationship to the framework genrated JavaScript"
036: */
037: public class ScriptBlock extends AbstractSimpleTag {
038: private ScriptPlacement _placement = ScriptPlacement.PLACE_INLINE;
039:
040: /**
041: * Return the name of the Tag.
042: */
043: public String getTagName() {
044: return "Content";
045: }
046:
047: /**
048: * Place the JavaScript inside in relationship to the frameword generated JavaScript.
049: * @param placement The placement of the JavaScript
050: * @jsptagref.attributedescription String value 'after' or 'before'. Places the JavaScript
051: * before or after the JavaScript provided by the framework.
052: * @jsptagref.databindable false
053: * @jsptagref.attributesyntaxvalue <i>string_or_expression_output</i>
054: * @netui:attribute rtexprvalue="true"
055: * description="The String literal or expression used to output the content."
056: */
057: public void setPlacement(String placement) {
058: if (placement.equals("after"))
059: _placement = ScriptPlacement.PLACE_AFTER;
060: else if (placement.equals("before"))
061: _placement = ScriptPlacement.PLACE_BEFORE;
062: else
063: _placement = ScriptPlacement.PLACE_INLINE;
064: }
065:
066: /**
067: * Render the content.
068: * @throws javax.servlet.jsp.JspException if a JSP exception has occurred
069: */
070: public void doTag() throws JspException, IOException {
071: // report any errors...
072: if (hasErrors()) {
073: reportErrors();
074: return;
075: }
076:
077: PageContext pageContext = getPageContext();
078: WriteRenderAppender writer = new WriteRenderAppender(
079: pageContext);
080: String script = getBufferBody(false);
081: if (script == null)
082: return;
083:
084: // make sure that the last character is a new line
085: if (script.length() > 0
086: && script.charAt(script.length() - 1) != '\n') {
087: script = script + "\n";
088: }
089:
090: IScriptReporter sr = getScriptReporter();
091:
092: // if we are writting the javaScript inline then do it....
093: if (_placement == ScriptPlacement.PLACE_INLINE || sr == null) {
094: ScriptRequestState.writeScriptBlock(pageContext
095: .getRequest(), writer, script);
096: return;
097: }
098:
099: sr.addScriptFunction(_placement, script);
100: }
101: }
|