📄 generator.java
字号:
// End of lock block
out.popIndent();
out.printil("}");
n.setEndJavaLine(out.getJavaLine());
}
/**
* @return a string for the form 'attr = "value"'
*/
private String makeAttr(String attr, String value) {
if (value == null)
return "";
return " " + attr + "=\"" + value + '\"';
}
public void visit(Node.PlugIn n) throws JasperException {
/**
* A visitor to handle <jsp:param> in a plugin
*/
class ParamVisitor extends Node.Visitor {
private boolean ie;
ParamVisitor(boolean ie) {
this.ie = ie;
}
public void visit(Node.ParamAction n) throws JasperException {
String name = n.getTextAttribute("name");
if (name.equalsIgnoreCase("object"))
name = "java_object";
else if (name.equalsIgnoreCase("type"))
name = "java_type";
n.setBeginJavaLine(out.getJavaLine());
// XXX - Fixed a bug here - value used to be output
// inline, which is only okay if value is not an EL
// expression. Also, key/value pairs for the
// embed tag were not being generated correctly.
// Double check that this is now the correct behavior.
if (ie) {
// We want something of the form
// out.println( "<param name=\"blah\"
// value=\"" + ... + "\">" );
out.printil("out.write( \"<param name=\\\""
+ escape(name)
+ "\\\" value=\\\"\" + "
+ attributeValue(n.getValue(), false,
String.class) + " + \"\\\">\" );");
out.printil("out.write(\"\\n\");");
} else {
// We want something of the form
// out.print( " blah=\"" + ... + "\"" );
out.printil("out.write( \" "
+ escape(name)
+ "=\\\"\" + "
+ attributeValue(n.getValue(), false,
String.class) + " + \"\\\"\" );");
}
n.setEndJavaLine(out.getJavaLine());
}
}
String type = n.getTextAttribute("type");
String code = n.getTextAttribute("code");
String name = n.getTextAttribute("name");
Node.JspAttribute height = n.getHeight();
Node.JspAttribute width = n.getWidth();
String hspace = n.getTextAttribute("hspace");
String vspace = n.getTextAttribute("vspace");
String align = n.getTextAttribute("align");
String iepluginurl = n.getTextAttribute("iepluginurl");
String nspluginurl = n.getTextAttribute("nspluginurl");
String codebase = n.getTextAttribute("codebase");
String archive = n.getTextAttribute("archive");
String jreversion = n.getTextAttribute("jreversion");
String widthStr = null;
if (width != null) {
if (width.isNamedAttribute()) {
widthStr = generateNamedAttributeValue(width
.getNamedAttributeNode());
} else {
widthStr = attributeValue(width, false, String.class);
}
}
String heightStr = null;
if (height != null) {
if (height.isNamedAttribute()) {
heightStr = generateNamedAttributeValue(height
.getNamedAttributeNode());
} else {
heightStr = attributeValue(height, false, String.class);
}
}
if (iepluginurl == null)
iepluginurl = Constants.IE_PLUGIN_URL;
if (nspluginurl == null)
nspluginurl = Constants.NS_PLUGIN_URL;
n.setBeginJavaLine(out.getJavaLine());
// If any of the params have their values specified by
// jsp:attribute, prepare those values first.
// Look for a params node and prepare its param subelements:
Node.JspBody jspBody = findJspBody(n);
if (jspBody != null) {
Node.Nodes subelements = jspBody.getBody();
if (subelements != null) {
for (int i = 0; i < subelements.size(); i++) {
Node m = subelements.getNode(i);
if (m instanceof Node.ParamsAction) {
prepareParams(m);
break;
}
}
}
}
// XXX - Fixed a bug here - width and height can be set
// dynamically. Double-check if this generation is correct.
// IE style plugin
// <object ...>
// First compose the runtime output string
String s0 = "<object"
+ makeAttr("classid", ctxt.getOptions().getIeClassId())
+ makeAttr("name", name);
String s1 = "";
if (width != null) {
s1 = " + \" width=\\\"\" + " + widthStr + " + \"\\\"\"";
}
String s2 = "";
if (height != null) {
s2 = " + \" height=\\\"\" + " + heightStr + " + \"\\\"\"";
}
String s3 = makeAttr("hspace", hspace) + makeAttr("vspace", vspace)
+ makeAttr("align", align)
+ makeAttr("codebase", iepluginurl) + '>';
// Then print the output string to the java file
out.printil("out.write(" + quote(s0) + s1 + s2 + " + " + quote(s3)
+ ");");
out.printil("out.write(\"\\n\");");
// <param > for java_code
s0 = "<param name=\"java_code\"" + makeAttr("value", code) + '>';
out.printil("out.write(" + quote(s0) + ");");
out.printil("out.write(\"\\n\");");
// <param > for java_codebase
if (codebase != null) {
s0 = "<param name=\"java_codebase\""
+ makeAttr("value", codebase) + '>';
out.printil("out.write(" + quote(s0) + ");");
out.printil("out.write(\"\\n\");");
}
// <param > for java_archive
if (archive != null) {
s0 = "<param name=\"java_archive\""
+ makeAttr("value", archive) + '>';
out.printil("out.write(" + quote(s0) + ");");
out.printil("out.write(\"\\n\");");
}
// <param > for type
s0 = "<param name=\"type\""
+ makeAttr("value", "application/x-java-"
+ type
+ ";"
+ ((jreversion == null) ? "" : "version="
+ jreversion)) + '>';
out.printil("out.write(" + quote(s0) + ");");
out.printil("out.write(\"\\n\");");
/*
* generate a <param> for each <jsp:param> in the plugin body
*/
if (n.getBody() != null)
n.getBody().visit(new ParamVisitor(true));
/*
* Netscape style plugin part
*/
out.printil("out.write(" + quote("<comment>") + ");");
out.printil("out.write(\"\\n\");");
s0 = "<EMBED"
+ makeAttr("type", "application/x-java-"
+ type
+ ";"
+ ((jreversion == null) ? "" : "version="
+ jreversion)) + makeAttr("name", name);
// s1 and s2 are the same as before.
s3 = makeAttr("hspace", hspace) + makeAttr("vspace", vspace)
+ makeAttr("align", align)
+ makeAttr("pluginspage", nspluginurl)
+ makeAttr("java_code", code)
+ makeAttr("java_codebase", codebase)
+ makeAttr("java_archive", archive);
out.printil("out.write(" + quote(s0) + s1 + s2 + " + " + quote(s3)
+ ");");
/*
* Generate a 'attr = "value"' for each <jsp:param> in plugin body
*/
if (n.getBody() != null)
n.getBody().visit(new ParamVisitor(false));
out.printil("out.write(" + quote("/>") + ");");
out.printil("out.write(\"\\n\");");
out.printil("out.write(" + quote("<noembed>") + ");");
out.printil("out.write(\"\\n\");");
/*
* Fallback
*/
if (n.getBody() != null) {
visitBody(n);
out.printil("out.write(\"\\n\");");
}
out.printil("out.write(" + quote("</noembed>") + ");");
out.printil("out.write(\"\\n\");");
out.printil("out.write(" + quote("</comment>") + ");");
out.printil("out.write(\"\\n\");");
out.printil("out.write(" + quote("</object>") + ");");
out.printil("out.write(\"\\n\");");
n.setEndJavaLine(out.getJavaLine());
}
public void visit(Node.NamedAttribute n) throws JasperException {
// Don't visit body of this tag - we already did earlier.
}
public void visit(Node.CustomTag n) throws JasperException {
// Use plugin to generate more efficient code if there is one.
if (n.useTagPlugin()) {
generateTagPlugin(n);
return;
}
TagHandlerInfo handlerInfo = getTagHandlerInfo(n);
// Create variable names
String baseVar = createTagVarName(n.getQName(), n.getPrefix(), n
.getLocalName());
String tagEvalVar = "_jspx_eval_" + baseVar;
String tagHandlerVar = "_jspx_th_" + baseVar;
String tagPushBodyCountVar = "_jspx_push_body_count_" + baseVar;
// If the tag contains no scripting element, generate its codes
// to a method.
ServletWriter outSave = null;
Node.ChildInfo ci = n.getChildInfo();
if (ci.isScriptless() && !ci.hasScriptingVars()) {
// The tag handler and its body code can reside in a separate
// method if it is scriptless and does not have any scripting
// variable defined.
String tagMethod = "_jspx_meth_" + baseVar;
// Generate a call to this method
out.printin("if (");
out.print(tagMethod);
out.print("(");
if (parent != null) {
out.print(parent);
out.print(", ");
}
out.print("_jspx_page_context");
if (pushBodyCountVar != null) {
out.print(", ");
out.print(pushBodyCountVar);
}
out.println("))");
out.pushIndent();
out.printil((methodNesting > 0) ? "return true;" : "return;");
out.popIndent();
// Set up new buffer for the method
outSave = out;
/*
* For fragments, their bodies will be generated in fragment
* helper classes, and the Java line adjustments will be done
* there, hence they are set to null here to avoid double
* adjustments.
*/
GenBuffer genBuffer = new GenBuffer(n,
n.implementsSimpleTag() ? null : n.getBody());
methodsBuffered.add(genBuffer);
out = genBuffer.getOut();
methodNesting++;
// Generate code for method declaration
out.println();
out.pushIndent();
out.printin("private boolean ");
out.print(tagMethod);
out.print("(");
if (parent != null) {
out.print("javax.servlet.jsp.tagext.JspTag ");
out.print(parent);
out.print(", ");
}
out.print("PageContext _jspx_page_context");
if (pushBodyCountVar != null) {
out.print(", int[] ");
out.print(pushBodyCountVar);
}
out.println(")");
out.printil(" throws Throwable {");
out.pushIndent();
// Initilaize local variables used i
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -