1 12 13 package org.xquark.xquery.parser; 14 15 import java.util.*; 16 17 import org.xquark.util.NamespaceContextStack; 18 import org.xquark.xpath.Axis; 19 import org.xquark.xpath.NodeKind; 20 import org.xquark.xquery.parser.util.Constants; 21 22 public class PrintVisitor extends DefaultParserVisitor { 23 24 private StringBuffer buf = new StringBuffer (); 25 26 private PrintVisitor pvisitor = null; 27 28 private int indent = 0; 29 30 private boolean doLineFeed = false; 31 32 private boolean doImports = false; 33 34 private boolean doNodeAccessor = false; 36 37 private boolean doPrefix = false; 38 39 private NamespaceContextStack declarations = null; 40 41 45 55 public void reset(boolean doLineFeed, boolean doImports, boolean doNodeAccessor, boolean doPrefix) { 56 this.doLineFeed = doLineFeed; 57 this.doImports = doImports; 58 this.doNodeAccessor = doNodeAccessor; 59 this.doPrefix = doPrefix; 60 this.declarations = null; 61 buf.setLength(0); 62 } 63 64 public void setNamespaceContextStack(NamespaceContextStack context) { 65 declarations = context; 66 } 67 68 75 public String getString() { 76 return buf.toString(); 77 } 78 79 private void lineFeed() { 80 if (doLineFeed) { 81 buf.append('\n'); 82 for (int i = 0; i < indent; i++) { 83 buf.append('\t'); 84 } 85 } 86 } 87 88 private void doBefore(XQueryExpression arg) { 89 if (arg.getParenthesis()) 90 buf.append("("); 91 } 92 private void doAfter(XQueryExpression arg) { 93 if (arg.getParenthesis()) 94 buf.append(")"); 95 } 96 97 private String foreSee(XQueryExpression arg) throws XQueryException { 98 if (pvisitor == null) 99 pvisitor = new PrintVisitor(); 100 pvisitor.reset(doLineFeed, doImports, doNodeAccessor, doPrefix); 101 pvisitor.setNamespaceContextStack(declarations); 102 arg.accept(pvisitor); 103 return pvisitor.getString(); 104 } 105 106 public void visit(AttributeValuePair arg) throws XQueryException { 108 109 if (arg.isXmlns()) { 110 buf.append("xmlns"); 111 if (((ValueString) arg.getAttributeName()).getStringValue().length() != 0) { 112 buf.append(":"); 113 buf.append(((ValueString) arg.getAttributeName()).getValue()); 114 } 115 buf.append("="); 116 arg.getExpression2().accept(this); 117 } else if (arg.isComputed()) { 118 buf.append("attribute "); 119 if (!(arg.getExpression1() instanceof QName)) 120 buf.append("{ "); 121 arg.getExpression1().accept(this); 122 if (!(arg.getExpression1() instanceof QName)) 123 buf.append(" }"); 124 buf.append(" { "); 125 arg.getExpression2().accept(this); 126 buf.append(" }"); 127 } else { 128 arg.getExpression1().accept(this); 129 String expr2 = foreSee(arg.getExpression2()); 130 if (!(expr2.charAt(0) == '\"')) { 131 buf.append("=\""); 132 if (!(arg.getExpression2() instanceof Value)) 133 buf.append("{"); 134 buf.append(expr2); 135 if (!(arg.getExpression2() instanceof Value)) 136 buf.append("}"); 137 buf.append("\""); 138 } else { 139 buf.append("="); 140 if (!(arg.getExpression2() instanceof Value)) 141 buf.append("{"); 142 buf.append(expr2); 143 if (!(arg.getExpression2() instanceof Value)) 144 buf.append("}"); 145 } 146 } 147 148 } 149 150 public void visit(BinOpANDExpression arg) throws XQueryException { 151 152 doBefore(arg); 153 arg.getExpression1().accept(this); 154 buf.append(Constants.NODELISTOPERATORSSTRINGS[Constants.AND_NODELISTOPERATOR]); 155 arg.getExpression2().accept(this); 156 doAfter(arg); 157 } 158 159 public void visit(BinOpORExpression arg) throws XQueryException { 160 161 doBefore(arg); 162 arg.getExpression1().accept(this); 163 buf.append(Constants.NODELISTOPERATORSSTRINGS[Constants.OR_NODELISTOPERATOR]); 164 arg.getExpression2().accept(this); 165 doAfter(arg); 166 } 167 168 public void visit(CaseClauseExpression arg) throws XQueryException { 169 170 buf.append("case "); 171 if (arg.getSequenceType() != null) 172 arg.getSequenceType().accept(this); 173 if (arg.getVariable() != null) 174 arg.getVariable().accept(this); 175 buf.append(" return "); 176 arg.getReturnExpression().accept(this); 177 178 } 179 180 public void visit(CastTreatExpression arg) throws XQueryException { 181 182 buf.append(Constants.TYPINGSTRINGS[arg.getOperator()]); 183 buf.append(" as "); 184 arg.getSequenceType().accept(this); 185 buf.append("("); 186 arg.getExpression().accept(this); 187 buf.append(")"); 188 189 } 190 191 public void visit(CData arg) throws XQueryException { 192 193 buf.append("<![CDATA[ "); 194 buf.append(arg.getCData()); 195 buf.append(" ]]>"); 196 197 } 198 199 public void visit(ComputedNamespace arg) throws XQueryException { 200 201 buf.append("namespace "); 202 buf.append(arg.getNcname()); 203 buf.append(" { "); 204 arg.getExpression().accept(this); 205 buf.append(" }"); 206 207 } 208 209 public void visit(ComputedText arg) throws XQueryException { 210 211 buf.append("text {"); 212 arg.getExpression().accept(this); 213 buf.append(" }"); 214 215 } 216 217 public void visit(Document arg) throws XQueryException { 218 219 buf.append("document { "); 220 arg.getExpression().accept(this); 221 buf.append(" }"); 222 223 } 224 225 public void visit(Element arg) throws XQueryException { 226 HashMap map = arg.getLocalPrefixes(); 227 if (declarations != null && map != null) { 228 declarations.pushContext(); 229 for (Iterator it = map.keySet().iterator(); it.hasNext();) { 230 String prefix = (String ) it.next(); 231 declarations.declarePrefix(prefix, (String ) map.get(prefix)); 232 } 233 } 234 if (arg.isComputed()) { 235 buf.append("element "); 236 if (!(arg.getStartTag() instanceof QName)) 237 buf.append("{ "); 238 arg.getStartTag().accept(this); 239 if (!(arg.getStartTag() instanceof QName)) 240 buf.append(" }"); 241 buf.append(" {"); 242 ArrayList attributes = arg.getAttributes(); 243 if (attributes != null) 244 for (int i = 0; i < attributes.size(); i++) { 245 buf.append(" "); 246 ((AttributeValuePair) attributes.get(i)).accept(this); 247 if (i < attributes.size() - 1) 248 buf.append(","); 249 } 250 ArrayList subExpressions = arg.getSubExpressions(); 251 if (subExpressions != null) 252 for (int j = 0; j < subExpressions.size(); j++) { 253 if (j == 0 && attributes != null) 254 buf.append(","); 255 buf.append(" "); 256 ((XQueryExpression) subExpressions.get(j)).accept(this); 257 if (j < subExpressions.size() - 1) 258 buf.append(","); 259 } 260 buf.append(" }"); 261 } else { 262 buf.append("<"); 263 arg.getStartTag().accept(this); 264 ArrayList attributes = arg.getAttributes(); 265 if (attributes != null) 266 for (int i = 0; i < attributes.size(); i++) { 267 buf.append(" "); 268 ((AttributeValuePair) attributes.get(i)).accept(this); 269 } 270 buf.append(">"); 271 ArrayList subExpressions = arg.getSubExpressions(); 272 if (subExpressions != null) { 273 indent++; 274 for (int j = 0; j < subExpressions.size(); j++) { 275 lineFeed(); 276 XQueryExpression exprj = (XQueryExpression) subExpressions.get(j); 277 if (!(exprj instanceof ValueText) && !(exprj instanceof Element)) 278 buf.append("{"); 279 ((XQueryExpression) subExpressions.get(j)).accept(this); 280 if (!(exprj instanceof ValueText) && !(exprj instanceof Element)) 281 buf.append("}"); 282 } 283 indent--; 284 lineFeed(); 285 } 286 buf.append("</"); 287 arg.getStartTag().accept(this); 288 buf.append(">"); 289 } 290 if (declarations != null && map != null) 291 declarations.popContext(); 292 } 293 294 public void visit(ElementTest arg) throws XQueryException { 295 296 buf.append("element("); 297 if (arg.getSchemaContextPath() != null) 298 arg.getSchemaContextPath().accept(this); 299 else if (arg.getDeclQName() != null) { 300 arg.getDeclQName().accept(this); 301 if (arg.getTypeQName() != null) { 302 buf.append(","); 303 arg.getTypeQName().accept(this); 304 if (arg.getNillable()) 305 buf.append(" nillable"); 306 } 307 } 308 buf.append(')'); 309 310 } 311 312 314 public void visit(FLWRExpression arg) throws XQueryException { 315 if (arg.getParentExpression() != null && !arg.getParentExpression().isEmpty()) { 316 indent++; 317 lineFeed(); 318 } 319 320 ArrayList variables = arg.getVariables(); 321 if (variables != null) { 322 for (int i = 0; i < variables.size(); i++) { 323 if (i != 0) 324 lineFeed(); 325 Variable vari = (Variable) variables.get(i); 326 int bindingtype = vari.getBindingType(); 327 switch (bindingtype) { 328 case Constants.FOR_BINDINGTYPE: 329 buf.append(" for "); 330 vari.accept(this); 331 buf.append(" in "); 332 break; 333 case Constants.LET_BINDINGTYPE: 334 buf.append(" let "); 335 vari.accept(this); 336 buf.append(" := "); 337 break; 338 case Constants.OUTERFOR_BINDINGTYPE: 339 buf.append(" outerfor "); 340 vari.accept(this); 341 buf.append(" in "); 342 break; 343 case Constants.OUTERLET_BINDINGTYPE: 344 buf.append(" outerlet "); 345 vari.accept(this); 346 buf.append(" := "); 347 } 348 if (vari.getPositionVar() != null) { 349 buf.append(" at "); 350 vari.getPositionVar().accept(this); 351 } 352 if (vari.getExpression() != null) 353 vari.getExpression().accept(this); 354 } 355 } 356 if (arg.getWhereClause() != null) { 357 lineFeed(); 358 buf.append(" where "); 359 arg.getWhereClause().accept(this); 360 } 361 if (arg.getOrderBy() != null) { 362 lineFeed(); 363 buf.append(" order by "); 364 for (int i = 0; i < arg.getOrderBy().size(); i++) { 365 XQueryExpression expri = (XQueryExpression) arg.getOrderBy().get(i); 366 if (i != 0) 367 buf.append(","); 368 expri.accept(this); 369 if (expri.getOrder(0) != Constants.NOTHING) { 370 buf.append(" "); 371 buf.append(Constants.ORDERSSTRINGS[expri.getOrder(0)]); 372 } 373 if (expri.getOrder(1) != Constants.NOTHING) { 374 buf.append(" empty "); 375 buf.append(Constants.ORDERSSTRINGS[expri.getOrder(1)]); 376 } 377 } 378 } 379 if (arg.getReturnClause() != null) { 380 lineFeed(); 381 buf.append(" return "); 382 indent++; 383 lineFeed(); 384 arg.getReturnClause().accept(this); 385 indent--; 386 lineFeed(); 387 } 388 389 if (arg.getParentExpression() != null && !arg.getParentExpression().isEmpty()) { 390 indent--; 391 } 392 } 393 394 public void visit(FunctionCall arg) throws XQueryException { 395 396 arg.getFuncName().accept(this); 397 buf.append("("); 398 ArrayList arguments = arg.getArguments(); 399 if (arguments != null) { 400 for (int i = 0; i < arguments.size(); i++) { 401 if (i != 0) 402 buf.append(","); 403 ((XQueryExpression) arguments.get(i)).accept(this); 404 } 405 } 406 buf.append(")"); 407 408 } 409 410 public void visit(FunctionDeclaration arg) throws XQueryException { 411 412 buf.append("declare function "); 413 arg.getFuncName().accept(this); 414 buf.append(" ("); 415 if (arg.getParameters() != null) { 416 for (int i = 0; i < arg.getParameters().size(); i++) { 417 if (i != 0) 418 buf.append(","); 419 Variable vari = (Variable) arg.getParameters().get(i); 420 vari.accept(this); 421 if (vari.getTypeDeclaration() != null) { 422 buf.append(" as "); 423 vari.getTypeDeclaration().accept(this); 424 } 425 } 426 } 427 buf.append(")"); 428 if (arg.getReturnType() != null) { 429 buf.append(" as "); 430 arg.getReturnType().accept(this); 431 } 432 if (arg.getExpressions() != null) { 433 buf.append(" { "); 434 for (int i = 0; i < arg.getExpressions().size(); i++) { 435 if (i != 0) 436 buf.append(" , "); 437 ((XQueryExpression) arg.getExpressions().get(i)).accept(this); 438 } 439 buf.append(" } "); 440 } else { 441 buf.append(" external"); 442 } 443 444 } 445 446 public void visit(ImportModule arg) throws XQueryException { 447 448 buf.append("import module "); 449 if (arg.getPrefix() != null) { 450 buf.append("namespace "); 451 buf.append(arg.getPrefix()); 452 buf.append(" = "); 453 } 454 buf.append("\""); 455 buf.append(arg.getName()); 456 buf.append("\""); 457 if (arg.getLocation() != null) { 458 buf.append(" at \""); 459 buf.append(arg.getLocation()); 460 buf.append("\""); 461 } 462 buf.append(";"); 463 464 } 465 466 public void visit(ImportSchema arg) throws XQueryException { 467 468 buf.append("import schema "); 469 if (arg.getHasPrefix()) { 470 if (arg.getPrefix() == null) 471 buf.append("default element namespace "); 472 else { 473 buf.append("namespace "); 474 buf.append(arg.getPrefix()); 475 buf.append(" = "); 476 } 477 } 478 buf.append("\""); 479 buf.append(arg.getName()); 480 buf.append("\""); 481 if (arg.getLocation() != null) { 482 buf.append(" at \""); 483 buf.append(arg.getLocation()); 484 buf.append("\""); 485 } 486 buf.append(";"); 487 488 } 489 490 public void visit(InstanceOfExpression arg) throws XQueryException { 492 493 arg.getExpression().accept(this); 494 buf.append(" instanceof "); 495 arg.getSequenceType().accept(this); 496 497 } 498 499 public void visit(ITEExpression arg) throws XQueryException { 501 502 buf.append(" if ( "); 503 arg.getIfExpression().accept(this); 504 buf.append(" ) then "); 505 arg.getThenExpression().accept(this); 506 buf.append(" else "); 507 arg.getElseExpression().accept(this); 508 509 } 510 511 public void visit(ItemType arg) throws XQueryException { 512 513 if (arg.getExpression() != null) 514 arg.getExpression().accept(this); 515 else 516 buf.append("item()"); 517 518 } 519 520 public void visit(ListOpAFTERExpression arg) throws XQueryException { 522 523 arg.getExpression1().accept(this); 524 buf.append(" after "); 525 arg.getExpression2().accept(this); 526 527 } 528 529 public void visit(ListOpArithExpression arg) throws XQueryException { 530 531 arg.getExpression1().accept(this); 532 buf.append(" "); 533 buf.append(Constants.ARITHMETICSSTRINGS[arg.getOperator()]); 534 buf.append(" "); 535 arg.getExpression2().accept(this); 536 537 } 538 539 public void visit(ListOpBEFOREExpression arg) throws XQueryException { 540 541 arg.getExpression1().accept(this); 542 buf.append(" before "); 543 arg.getExpression2().accept(this); 544 545 } 546 547 public void visit(ListOpCompExpression arg) throws XQueryException { 548 549 arg.getExpression1().accept(this); 550 buf.append(" "); 551 buf.append(Constants.COMPOPSSTRINGS[arg.getOperator()]); 552 buf.append(" "); 553 arg.getExpression2().accept(this); 554 555 } 556 557 public void visit(ListOpEXCEPTExpression arg) throws XQueryException { 558 559 arg.getExpression1().accept(this); 560 buf.append(" except "); 561 arg.getExpression2().accept(this); 562 563 } 564 565 public void visit(ListOpINTERSECTExpression arg) throws XQueryException { 566 567 arg.getExpression1().accept(this); 568 buf.append(" intersect "); 569 arg.getExpression2().accept(this); 570 571 } 572 573 public void visit(ListOpUNIONExpression arg) throws XQueryException { 574 575 arg.getExpression1().accept(this); 576 buf.append(" union "); 577 arg.getExpression2().accept(this); 578 579 } 580 581 public void visit(LocatedExpression arg) throws XQueryException { 582 for (int i = 0; i < arg.getSteps().size(); i++) { 583 ((Step) arg.getSteps().get(i)).accept(this); 584 } 585 if (doNodeAccessor) { 586 int nodeAccessor = arg.getNodeAccessor(); 587 switch (nodeAccessor) { 588 case Step.CHILD_TEXT: 589 buf.append("/child::text()"); 590 break; 591 case Step.CHILD_NODE: 592 buf.append("/child::node()"); 593 break; 594 case Step.SELF_TEXT: 595 buf.append("/self::text()"); 596 break; 597 case Step.SELF_NODE: 598 buf.append("/self::node()"); 599 break; 600 } 601 } 602 } 603 604 public void visit(NodeTest arg) throws XQueryException { 605 606 buf.append(NodeKind.NODEKINDSTRINGS[arg.getKind()]); 607 buf.append('('); 608 if (arg.getArg() != null) 609 arg.getArg().accept(this); 610 buf.append(')'); 611 612 } 613 614 public void visit(QName arg) throws XQueryException { 616 String prefix = null; 617 if (arg.getPrefixName() != null && arg.getPrefixName().equals("*")) 618 prefix = arg.getPrefixName(); 619 if (doPrefix) { 620 if (prefix == null && arg.getNameSpace() != null) { 621 if (declarations != null) 622 prefix = declarations.getPrefix(arg.getNameSpace()); 623 else if (arg.getNamespaceContext() != null) 624 prefix = arg.getNamespaceContext().getPrefix(arg.getNameSpace()); 625 if (prefix != null) 626 arg.setPrefixName(prefix); 627 } 628 } 629 if (prefix != null) { 630 if (prefix.length() != 0) { 631 buf.append(prefix); 632 buf.append(":"); 633 } 634 } else { 635 if (arg.getNameSpace() != null) { 636 buf.append("{"); 637 buf.append(arg.getNameSpace()); 638 buf.append("}"); 639 } 640 } 641 buf.append(arg.getLocalName()); 642 643 650 } 669 670 public void visit(QuantifiedExpression arg) throws XQueryException { 671 672 buf.append(Constants.QUANTIFIERSSTRINGS[arg.getKind()]); 673 buf.append(" "); 674 for (int i = 0; i < arg.getVariables().size(); i++) { 675 if (i != 0) 676 buf.append(" , "); 677 Variable vari = (Variable) arg.getVariables().get(i); 678 vari.accept(this); 679 buf.append(" in "); 680 buf.append(vari.getExpression()); 681 } 682 buf.append(" satisfies "); 683 arg.getExpression().accept(this); 684 685 } 686 687 public void visit(RangeExpression arg) throws XQueryException { 688 689 arg.getExpression1().accept(this); 690 buf.append(" to "); 691 arg.getExpression2().accept(this); 692 693 } 694 695 public void visit(SchemaContextPath arg) throws XQueryException { 696 697 for (int i = 0; i < arg.getSteps().size(); i++) 698 ((SchemaContextStep) arg.getSteps().get(i)).accept(this); 699 700 } 701 702 public void visit(SchemaContextStep arg) throws XQueryException { 703 704 if (arg.getAxis() == Constants.WITHAXIS) 705 buf.append('/'); 706 else if (arg.getAxis() == Constants.WITHTYPE) 707 buf.append("type("); 708 arg.getExpression().accept(this); 709 if (arg.getAxis() == Constants.WITHTYPE) 710 buf.append(')'); 711 712 } 713 714 public void visit(SequenceType arg) throws XQueryException { 715 716 if (arg.getItemType() != null) { 717 arg.getItemType().accept(this); 718 if (arg.getOccurrence() != -1) 719 buf.append(Constants.OCCURRENCE_INDICATORS[arg.getOccurrence()]); 720 } else 721 buf.append("empty()"); 722 723 } 724 725 public void visit(SortedExpression arg) throws XQueryException { 726 727 arg.getExpression().accept(this); 728 if (arg.getStable()) 729 buf.append(" stable"); 730 buf.append(" sortby ( "); 731 for (int j = 0; j < arg.getSortClauses().size(); j++) { 732 XQueryExpression expri = (XQueryExpression) arg.getSortClauses().get(j); 733 if (j != 0) 734 buf.append(","); 735 expri.accept(this); 736 if (expri.getOrder(0) != Constants.NOTHING) { 737 buf.append(" "); 738 buf.append(Constants.ORDERSSTRINGS[expri.getOrder(0)]); 739 } 740 if (expri.getOrder(1) != Constants.NOTHING) { 741 buf.append(" empty "); 742 buf.append(Constants.ORDERSSTRINGS[expri.getOrder(1)]); 743 } 744 } 745 buf.append(" )"); 746 747 } 748 749 public void visit(Step arg) throws XQueryException { 750 751 if (arg.hasSeparator()) 752 buf.append('/'); 753 buf.append(Axis.AXISSTRINGS[arg.getAxis()]); 754 arg.getExpression().accept(this); 755 if (arg.getPredicates() != null) 756 for (int i = 0; i < arg.getPredicates().size(); i++) { 757 buf.append("["); 758 ((XQueryExpression) arg.getPredicates().get(i)).accept(this); 759 buf.append("]"); 760 } 761 762 } 763 764 public void visit(TypeSwitchExpression arg) throws XQueryException { 765 766 buf.append("typeswitch ("); 767 arg.getExpression1().accept(this); 768 buf.append(") "); 769 for (Iterator it = arg.getCases().values().iterator(); it.hasNext();) { 770 ((CaseClauseExpression) it.next()).accept(this); 771 } 772 buf.append(" default "); 773 if (arg.getDefaultVariable() != null) 774 arg.getDefaultVariable().accept(this); 775 arg.getExpression2().accept(this); 776 777 } 778 779 public void visit(UnOpMinusExpression arg) throws XQueryException { 780 781 if (arg.getMinus()) 782 buf.append("- "); 783 else 784 buf.append("+ "); 785 arg.getExpression().accept(this); 786 787 } 788 789 public void visit(ValidateExpression arg) throws XQueryException { 790 791 buf.append("validate"); 792 if (arg.getValidationType() != Constants.NOTHING) { 793 buf.append(" "); 794 buf.append(Constants.VALIDATIONSTRINGS[arg.getValidationType()]); 795 } 796 if (arg.getScopeType() != Constants.NOTHING) { 797 buf.append(" "); 798 buf.append(Constants.SCOPE_STRINGS[arg.getScopeType()]); 799 } 800 if (arg.getSchemaContext() != null) { 801 buf.append(" "); 802 arg.getSchemaContext().accept(this); 803 } 804 buf.append(" { "); 805 arg.getExpression().accept(this); 806 buf.append(" }"); 807 808 } 809 810 public void visit(Value arg) throws XQueryException { 811 812 buf.append(arg.getValue()); 813 814 } 815 816 public void visit(ValueString arg) throws XQueryException { 822 823 buf.append("\""); 824 buf.append(arg.getValue()); 825 buf.append("\""); 826 827 } 828 829 public void visit(Variable arg) throws XQueryException { 832 833 buf.append("$"); 834 arg.getVarName().accept(this); 835 } 842 843 public void visit(XMLComment arg) throws XQueryException { 844 845 if (arg.isComputed()) { 846 buf.append("comment { "); 847 arg.getExpression().accept(this); 848 buf.append(" }"); 849 } else { 850 buf.append("<!-- "); 851 arg.getExpression().accept(this); 852 buf.append(" -->"); 853 } 854 855 } 856 857 public void visit(XMLProcessingInstruction arg) throws XQueryException { 858 859 if (arg.isComputed()) { 860 buf.append("processing-instruction "); 861 arg.getExpression1().accept(this); 862 buf.append(" { "); 863 arg.getExpression2().accept(this); 864 buf.append(" }"); 865 } else { 866 StringBuffer buf = new StringBuffer (); 867 buf.append("<? "); 868 arg.getExpression1().accept(this); 869 buf.append(" "); 870 arg.getExpression2().accept(this); 871 buf.append(" ?>"); 872 } 873 874 } 875 876 public void visit(XQueryExpressionSequence arg) throws XQueryException { 880 doBefore(arg); 881 for (int j = 0; j < arg.getSubExpressions().size(); j++) { 882 if (j != 0) 883 buf.append(" , "); 884 ((XQueryExpression) arg.getSubExpressions().get(j)).accept(this); 885 } 886 doAfter(arg); 887 } 888 889 public void visit(XQueryModule arg) throws XQueryException { 893 if (arg.getVersion() != null) { 894 lineFeed(); 895 buf.append("xquery version \""); 896 buf.append(arg.getVersion()); 897 buf.append("\";"); 898 } 899 if (arg.getPrefix() != null && arg.getNamespace() != null) { 900 lineFeed(); 901 buf.append("module namespace "); 902 buf.append(arg.getPrefix()); 903 buf.append(" = \""); 904 buf.append(arg.getNamespace()); 905 buf.append("\""); 906 } 907 if (arg.getDeclarations() != null) { 908 lineFeed(); 909 Collection declarationsPrefixes = arg.getDeclarations().getPrefixes(); 910 Iterator prefixes = declarationsPrefixes.iterator(); 911 while (prefixes.hasNext()) { 912 String prefix = (String ) prefixes.next(); 913 if ((prefix == null) || (prefix.trim().length() == 0)) { 914 buf.append("declare default element namespace "); 915 buf.append("=\""); 916 buf.append((String ) arg.getDeclarations().getNamespaceURI(prefix)); 917 buf.append("\";"); 918 } else if (prefix != "xml") { 919 buf.append("declare namespace "); 920 buf.append(prefix); 921 buf.append("=\""); 922 buf.append((String ) arg.getDeclarations().getNamespaceURI(prefix)); 923 buf.append("\";"); 924 } 925 } 926 } 927 if (arg.getXmlSpace() != Constants.NOTHING) { 928 lineFeed(); 929 buf.append("declare xmlspace "); 930 buf.append(Constants.XMLSPACESTRINGS[arg.getXmlSpace()]); 931 } 932 if (arg.getDefaultFunctionNamespace() != null) { 933 lineFeed(); 934 buf.append("declare default function namespace = \""); 935 buf.append(arg.getDefaultFunctionNamespace()); 936 buf.append("\";"); 937 } 938 if (arg.getDefaultCollation() != null) { 939 lineFeed(); 940 buf.append("declare default collation = \""); 941 buf.append(arg.getDefaultCollation()); 942 buf.append("\";"); 943 } 944 if (arg.getBaseURI() != null) { 945 lineFeed(); 946 buf.append("declare base-uri = \""); 947 buf.append(arg.getBaseURI()); 948 buf.append("\";"); 949 } 950 if (arg.getSchemaList() != null) { 951 for (int i = 0; i < arg.getSchemaList().size(); i++) { 952 lineFeed(); 953 ((ImportSchema) arg.getImportSchemas().get(arg.getSchemaList().get(i))).accept(this); 954 } 955 } 956 if (arg.getValidation() != Constants.NOTHING && arg.getValidation() != Constants.VALIDATION_PRESERVE) { 957 lineFeed(); 958 buf.append("declare validation "); 959 buf.append(Constants.VALIDATIONSTRINGS[arg.getValidation()]); 960 buf.append(";"); 961 } 962 if (doImports) { 963 if (arg.getModuleList() != null) { 964 for (int i = 0; i < arg.getModuleList().size(); i++) { 965 lineFeed(); 966 ((ImportModule) arg.getImportModules().get(arg.getModuleList().get(i))).accept(this); 967 } 968 } 969 } 970 if (arg.getDeclList() != null) { 971 for (int i = 0; i < arg.getDeclList().size(); i++) { 972 lineFeed(); 973 QName name = (QName) arg.getDeclList().get(i); 974 HashMap map = null; 975 if (arg.getVariables() != null) { 976 map = (HashMap) arg.getVariables().get(name.getNameSpace()); 977 if (map != null) { 978 Variable vari = (Variable) map.get(name.getLocalName()); 979 if (vari != null) { 980 if (vari.getExpression() != null || doImports) { 981 buf.append("declare variable "); 982 vari.accept(this); 983 if (vari.getTypeDeclaration() != null) { 984 buf.append(" as "); 985 buf.append(vari.getTypeDeclaration()); 986 buf.append(""); 987 } 988 if (vari.getExpression() != null) { 989 buf.append(" {"); 990 buf.append(vari.getExpression()); 991 buf.append("};"); 992 } else 993 buf.append(" external;"); 994 } 995 } 996 } 1003 } 1004 } 1005 } 1006 if (arg.getExpressions() != null) { 1008 for (int i = 0; i < arg.getExpressions().size(); i++) { 1009 if (i != 0) { 1010 buf.append(" ,"); 1011 lineFeed(); 1012 } 1013 ((XQueryExpression) arg.getExpressions().get(i)).accept(this); 1014 } 1015 } 1016 } 1018 } | Popular Tags |