1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748
|
/*
* Licensed to the Apache Software Foundation (ASF) under one or more
* contributor license agreements. See the NOTICE file distributed with
* this work for additional information regarding copyright ownership.
* The ASF licenses this file to You under the Apache License, Version 2.0
* (the "License"); you may not use this file except in compliance with
* the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.jasper.compiler;
import java.io.ByteArrayInputStream;
import java.io.CharArrayWriter;
import java.io.InputStream;
import java.nio.charset.Charset;
import java.util.ListIterator;
import javax.servlet.jsp.tagext.PageData;
import org.apache.jasper.JasperException;
import org.xml.sax.Attributes;
import org.xml.sax.helpers.AttributesImpl;
/**
* An implementation of <tt>javax.servlet.jsp.tagext.PageData</tt> which
* builds the XML view of a given page.
*
* The XML view is built in two passes:
*
* During the first pass, the FirstPassVisitor collects the attributes of the
* top-level jsp:root and those of the jsp:root elements of any included
* pages, and adds them to the jsp:root element of the XML view.
* In addition, any taglib directives are converted into xmlns: attributes and
* added to the jsp:root element of the XML view.
* This pass ignores any nodes other than JspRoot and TaglibDirective.
*
* During the second pass, the SecondPassVisitor produces the XML view, using
* the combined jsp:root attributes determined in the first pass and any
* remaining pages nodes (this pass ignores any JspRoot and TaglibDirective
* nodes).
*
* @author Jan Luehe
*/
class PageDataImpl extends PageData implements TagConstants {
private static final String JSP_VERSION = "2.0";
private static final String CDATA_START_SECTION = "<![CDATA[\n";
private static final String CDATA_END_SECTION = "]]>\n";
private static final Charset CHARSET_UTF8 = Charset.forName("UTF-8");
// string buffer used to build XML view
private StringBuilder buf;
/**
* Constructor.
*
* @param page the page nodes from which to generate the XML view
*/
public PageDataImpl(Node.Nodes page, Compiler compiler)
throws JasperException {
// First pass
FirstPassVisitor firstPass = new FirstPassVisitor(page.getRoot(),
compiler.getPageInfo());
page.visit(firstPass);
// Second pass
buf = new StringBuilder();
SecondPassVisitor secondPass
= new SecondPassVisitor(page.getRoot(), buf, compiler,
firstPass.getJspIdPrefix());
page.visit(secondPass);
}
/**
* Returns the input stream of the XML view.
*
* @return the input stream of the XML view
*/
@Override
public InputStream getInputStream() {
return new ByteArrayInputStream(buf.toString().getBytes(CHARSET_UTF8));
}
/*
* First-pass Visitor for JspRoot nodes (representing jsp:root elements)
* and TablibDirective nodes, ignoring any other nodes.
*
* The purpose of this Visitor is to collect the attributes of the
* top-level jsp:root and those of the jsp:root elements of any included
* pages, and add them to the jsp:root element of the XML view.
* In addition, this Visitor converts any taglib directives into xmlns:
* attributes and adds them to the jsp:root element of the XML view.
*/
static class FirstPassVisitor
extends Node.Visitor implements TagConstants {
private Node.Root root;
private AttributesImpl rootAttrs;
private PageInfo pageInfo;
// Prefix for the 'id' attribute
private String jspIdPrefix;
/*
* Constructor
*/
public FirstPassVisitor(Node.Root root, PageInfo pageInfo) {
this.root = root;
this.pageInfo = pageInfo;
this.rootAttrs = new AttributesImpl();
this.rootAttrs.addAttribute("", "", "version", "CDATA",
JSP_VERSION);
this.jspIdPrefix = "jsp";
}
@Override
public void visit(Node.Root n) throws JasperException {
visitBody(n);
if (n == root) {
/*
* Top-level page.
*
* Add
* xmlns:jsp="http://java.sun.com/JSP/Page"
* attribute only if not already present.
*/
if (!JSP_URI.equals(rootAttrs.getValue("xmlns:jsp"))) {
rootAttrs.addAttribute("", "", "xmlns:jsp", "CDATA",
JSP_URI);
}
if (pageInfo.isJspPrefixHijacked()) {
/*
* 'jsp' prefix has been hijacked, that is, bound to a
* namespace other than the JSP namespace. This means that
* when adding an 'id' attribute to each element, we can't
* use the 'jsp' prefix. Therefore, create a new prefix
* (one that is unique across the translation unit) for use
* by the 'id' attribute, and bind it to the JSP namespace
*/
jspIdPrefix += "jsp";
while (pageInfo.containsPrefix(jspIdPrefix)) {
jspIdPrefix += "jsp";
}
rootAttrs.addAttribute("", "", "xmlns:" + jspIdPrefix,
"CDATA", JSP_URI);
}
root.setAttributes(rootAttrs);
}
}
@Override
public void visit(Node.JspRoot n) throws JasperException {
addAttributes(n.getTaglibAttributes());
addAttributes(n.getNonTaglibXmlnsAttributes());
addAttributes(n.getAttributes());
visitBody(n);
}
/*
* Converts taglib directive into "xmlns:..." attribute of jsp:root
* element.
*/
@Override
public void visit(Node.TaglibDirective n) throws JasperException {
Attributes attrs = n.getAttributes();
if (attrs != null) {
String qName = "xmlns:" + attrs.getValue("prefix");
/*
* According to javadocs of org.xml.sax.helpers.AttributesImpl,
* the addAttribute method does not check to see if the
* specified attribute is already contained in the list: This
* is the application's responsibility!
*/
if (rootAttrs.getIndex(qName) == -1) {
String location = attrs.getValue("uri");
if (location != null) {
if (location.startsWith("/")) {
location = URN_JSPTLD + location;
}
rootAttrs.addAttribute("", "", qName, "CDATA",
location);
} else {
location = attrs.getValue("tagdir");
rootAttrs.addAttribute("", "", qName, "CDATA",
URN_JSPTAGDIR + location);
}
}
}
}
public String getJspIdPrefix() {
return jspIdPrefix;
}
private void addAttributes(Attributes attrs) {
if (attrs != null) {
int len = attrs.getLength();
for (int i=0; i<len; i++) {
String qName = attrs.getQName(i);
if ("version".equals(qName)) {
continue;
}
// Bugzilla 35252: http://issues.apache.org/bugzilla/show_bug.cgi?id=35252
if(rootAttrs.getIndex(qName) == -1) {
rootAttrs.addAttribute(attrs.getURI(i),
attrs.getLocalName(i),
qName,
attrs.getType(i),
attrs.getValue(i));
}
}
}
}
}
/*
* Second-pass Visitor responsible for producing XML view and assigning
* each element a unique jsp:id attribute.
*/
static class SecondPassVisitor extends Node.Visitor
implements TagConstants {
private Node.Root root;
private StringBuilder buf;
private Compiler compiler;
private String jspIdPrefix;
private boolean resetDefaultNS = false;
// Current value of jsp:id attribute
private int jspId;
/*
* Constructor
*/
public SecondPassVisitor(Node.Root root, StringBuilder buf,
Compiler compiler, String jspIdPrefix) {
this.root = root;
this.buf = buf;
this.compiler = compiler;
this.jspIdPrefix = jspIdPrefix;
}
/*
* Visits root node.
*/
@Override
public void visit(Node.Root n) throws JasperException {
if (n == this.root) {
// top-level page
appendXmlProlog();
appendTag(n);
} else {
boolean resetDefaultNSSave = resetDefaultNS;
if (n.isXmlSyntax()) {
resetDefaultNS = true;
}
visitBody(n);
resetDefaultNS = resetDefaultNSSave;
}
}
/*
* Visits jsp:root element of JSP page in XML syntax.
*
* Any nested jsp:root elements (from pages included via an
* include directive) are ignored.
*/
@Override
public void visit(Node.JspRoot n) throws JasperException {
visitBody(n);
}
@Override
public void visit(Node.PageDirective n) throws JasperException {
appendPageDirective(n);
}
@Override
public void visit(Node.IncludeDirective n) throws JasperException {
// expand in place
visitBody(n);
}
@Override
public void visit(Node.Comment n) throws JasperException {
// Comments are ignored in XML view
}
@Override
public void visit(Node.Declaration n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.Expression n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.Scriptlet n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.JspElement n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.ELExpression n) throws JasperException {
if (!n.getRoot().isXmlSyntax()) {
buf.append("<").append(JSP_TEXT_ACTION);
buf.append(" ");
buf.append(jspIdPrefix);
buf.append(":id=\"");
buf.append(jspId++).append("\">");
}
buf.append("${");
buf.append(JspUtil.escapeXml(n.getText()));
buf.append("}");
if (!n.getRoot().isXmlSyntax()) {
buf.append(JSP_TEXT_ACTION_END);
}
buf.append("\n");
}
@Override
public void visit(Node.IncludeAction n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.ForwardAction n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.GetProperty n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.SetProperty n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.ParamAction n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.ParamsAction n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.FallBackAction n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.UseBean n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.PlugIn n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.NamedAttribute n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.JspBody n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.CustomTag n) throws JasperException {
boolean resetDefaultNSSave = resetDefaultNS;
appendTag(n, resetDefaultNS);
resetDefaultNS = resetDefaultNSSave;
}
@Override
public void visit(Node.UninterpretedTag n) throws JasperException {
boolean resetDefaultNSSave = resetDefaultNS;
appendTag(n, resetDefaultNS);
resetDefaultNS = resetDefaultNSSave;
}
@Override
public void visit(Node.JspText n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.DoBodyAction n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.InvokeAction n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.TagDirective n) throws JasperException {
appendTagDirective(n);
}
@Override
public void visit(Node.AttributeDirective n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.VariableDirective n) throws JasperException {
appendTag(n);
}
@Override
public void visit(Node.TemplateText n) throws JasperException {
/*
* If the template text came from a JSP page written in JSP syntax,
* create a jsp:text element for it (JSP 5.3.2).
*/
appendText(n.getText(), !n.getRoot().isXmlSyntax());
}
/*
* Appends the given tag, including its body, to the XML view.
*/
private void appendTag(Node n) throws JasperException {
appendTag(n, false);
}
/*
* Appends the given tag, including its body, to the XML view,
* and optionally reset default namespace to "", if none specified.
*/
private void appendTag(Node n, boolean addDefaultNS)
throws JasperException {
Node.Nodes body = n.getBody();
String text = n.getText();
buf.append("<").append(n.getQName());
buf.append("\n");
printAttributes(n, addDefaultNS);
buf.append(" ").append(jspIdPrefix).append(":id").append("=\"");
buf.append(jspId++).append("\"\n");
if (ROOT_ACTION.equals(n.getLocalName()) || body != null
|| text != null) {
buf.append(">\n");
if (ROOT_ACTION.equals(n.getLocalName())) {
if (compiler.getCompilationContext().isTagFile()) {
appendTagDirective();
} else {
appendPageDirective();
}
}
if (body != null) {
body.visit(this);
} else {
appendText(text, false);
}
buf.append("</" + n.getQName() + ">\n");
} else {
buf.append("/>\n");
}
}
/*
* Appends the page directive with the given attributes to the XML
* view.
*
* Since the import attribute of the page directive is the only page
* attribute that is allowed to appear multiple times within the same
* document, and since XML allows only single-value attributes,
* the values of multiple import attributes must be combined into one,
* separated by comma.
*
* If the given page directive contains just 'contentType' and/or
* 'pageEncoding' attributes, we ignore it, as we've already appended
* a page directive containing just these two attributes.
*/
private void appendPageDirective(Node.PageDirective n) {
boolean append = false;
Attributes attrs = n.getAttributes();
int len = (attrs == null) ? 0 : attrs.getLength();
for (int i=0; i<len; i++) {
@SuppressWarnings("null") // If attrs==null, len == 0
String attrName = attrs.getQName(i);
if (!"pageEncoding".equals(attrName)
&& !"contentType".equals(attrName)) {
append = true;
break;
}
}
if (!append) {
return;
}
buf.append("<").append(n.getQName());
buf.append("\n");
// append jsp:id
buf.append(" ").append(jspIdPrefix).append(":id").append("=\"");
buf.append(jspId++).append("\"\n");
// append remaining attributes
for (int i=0; i<len; i++) {
@SuppressWarnings("null") // If attrs==null, len == 0
String attrName = attrs.getQName(i);
if ("import".equals(attrName) || "contentType".equals(attrName)
|| "pageEncoding".equals(attrName)) {
/*
* Page directive's 'import' attribute is considered
* further down, and its 'pageEncoding' and 'contentType'
* attributes are ignored, since we've already appended
* a new page directive containing just these two
* attributes
*/
continue;
}
String value = attrs.getValue(i);
buf.append(" ").append(attrName).append("=\"");
buf.append(JspUtil.getExprInXml(value)).append("\"\n");
}
if (n.getImports().size() > 0) {
// Concatenate names of imported classes/packages
boolean first = true;
ListIterator<String> iter = n.getImports().listIterator();
while (iter.hasNext()) {
if (first) {
first = false;
buf.append(" import=\"");
} else {
buf.append(",");
}
buf.append(JspUtil.getExprInXml(iter.next()));
}
buf.append("\"\n");
}
buf.append("/>\n");
}
/*
* Appends a page directive with 'pageEncoding' and 'contentType'
* attributes.
*
* The value of the 'pageEncoding' attribute is hard-coded
* to UTF-8, whereas the value of the 'contentType' attribute, which
* is identical to what the container will pass to
* ServletResponse.setContentType(), is derived from the pageInfo.
*/
private void appendPageDirective() {
buf.append("<").append(JSP_PAGE_DIRECTIVE_ACTION);
buf.append("\n");
// append jsp:id
buf.append(" ").append(jspIdPrefix).append(":id").append("=\"");
buf.append(jspId++).append("\"\n");
buf.append(" ").append("pageEncoding").append("=\"UTF-8\"\n");
buf.append(" ").append("contentType").append("=\"");
buf.append(compiler.getPageInfo().getContentType()).append("\"\n");
buf.append("/>\n");
}
/*
* Appends the tag directive with the given attributes to the XML
* view.
*
* If the given tag directive contains just a 'pageEncoding'
* attributes, we ignore it, as we've already appended
* a tag directive containing just this attributes.
*/
private void appendTagDirective(Node.TagDirective n)
throws JasperException {
boolean append = false;
Attributes attrs = n.getAttributes();
int len = (attrs == null) ? 0 : attrs.getLength();
for (int i=0; i<len; i++) {
@SuppressWarnings("null") // If attrs==null, len == 0
String attrName = attrs.getQName(i);
if (!"pageEncoding".equals(attrName)) {
append = true;
break;
}
}
if (!append) {
return;
}
appendTag(n);
}
/*
* Appends a tag directive containing a single 'pageEncoding'
* attribute whose value is hard-coded to UTF-8.
*/
private void appendTagDirective() {
buf.append("<").append(JSP_TAG_DIRECTIVE_ACTION);
buf.append("\n");
// append jsp:id
buf.append(" ").append(jspIdPrefix).append(":id").append("=\"");
buf.append(jspId++).append("\"\n");
buf.append(" ").append("pageEncoding").append("=\"UTF-8\"\n");
buf.append("/>\n");
}
private void appendText(String text, boolean createJspTextElement) {
if (createJspTextElement) {
buf.append("<").append(JSP_TEXT_ACTION);
buf.append("\n");
// append jsp:id
buf.append(" ").append(jspIdPrefix).append(":id").append("=\"");
buf.append(jspId++).append("\"\n");
buf.append(">\n");
appendCDATA(text);
buf.append(JSP_TEXT_ACTION_END);
buf.append("\n");
} else {
appendCDATA(text);
}
}
/*
* Appends the given text as a CDATA section to the XML view, unless
* the text has already been marked as CDATA.
*/
private void appendCDATA(String text) {
buf.append(CDATA_START_SECTION);
buf.append(escapeCDATA(text));
buf.append(CDATA_END_SECTION);
}
/*
* Escapes any occurrences of "]]>" (by replacing them with "]]>")
* within the given text, so it can be included in a CDATA section.
*/
private String escapeCDATA(String text) {
if( text==null ) return "";
int len = text.length();
CharArrayWriter result = new CharArrayWriter(len);
for (int i=0; i<len; i++) {
if (((i+2) < len)
&& (text.charAt(i) == ']')
&& (text.charAt(i+1) == ']')
&& (text.charAt(i+2) == '>')) {
// match found
result.write(']');
result.write(']');
result.write('&');
result.write('g');
result.write('t');
result.write(';');
i += 2;
} else {
result.write(text.charAt(i));
}
}
return result.toString();
}
/*
* Appends the attributes of the given Node to the XML view.
*/
private void printAttributes(Node n, boolean addDefaultNS) {
/*
* Append "xmlns" attributes that represent tag libraries
*/
Attributes attrs = n.getTaglibAttributes();
int len = (attrs == null) ? 0 : attrs.getLength();
for (int i=0; i<len; i++) {
@SuppressWarnings("null") // If attrs==null, len == 0
String name = attrs.getQName(i);
String value = attrs.getValue(i);
buf.append(" ").append(name).append("=\"").append(value).append("\"\n");
}
/*
* Append "xmlns" attributes that do not represent tag libraries
*/
attrs = n.getNonTaglibXmlnsAttributes();
len = (attrs == null) ? 0 : attrs.getLength();
boolean defaultNSSeen = false;
for (int i=0; i<len; i++) {
@SuppressWarnings("null") // If attrs==null, len == 0
String name = attrs.getQName(i);
String value = attrs.getValue(i);
buf.append(" ").append(name).append("=\"").append(value).append("\"\n");
defaultNSSeen |= "xmlns".equals(name);
}
if (addDefaultNS && !defaultNSSeen) {
buf.append(" xmlns=\"\"\n");
}
resetDefaultNS = false;
/*
* Append all other attributes
*/
attrs = n.getAttributes();
len = (attrs == null) ? 0 : attrs.getLength();
for (int i=0; i<len; i++) {
@SuppressWarnings("null") // If attrs==null, len == 0
String name = attrs.getQName(i);
String value = attrs.getValue(i);
buf.append(" ").append(name).append("=\"");
buf.append(JspUtil.getExprInXml(value)).append("\"\n");
}
}
/*
* Appends XML prolog with encoding declaration.
*/
private void appendXmlProlog() {
buf.append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
}
}
}
|