1 16 19 20 package org.apache.xalan.xsltc.compiler; 21 22 import org.apache.bcel.generic.ConstantPoolGen; 23 import org.apache.bcel.generic.INVOKESPECIAL; 24 import org.apache.bcel.generic.INVOKEVIRTUAL; 25 import org.apache.bcel.generic.InstructionList; 26 import org.apache.bcel.generic.NEW; 27 import org.apache.bcel.generic.PUSH; 28 import org.apache.xalan.xsltc.compiler.util.ClassGenerator; 29 import org.apache.xalan.xsltc.compiler.util.MethodGenerator; 30 import org.apache.xalan.xsltc.compiler.util.Type; 31 import org.apache.xalan.xsltc.compiler.util.TypeCheckError; 32 33 38 final class Message extends Instruction { 39 private boolean _terminate = false; 40 41 public void parseContents(Parser parser) { 42 String termstr = getAttribute("terminate"); 43 if (termstr != null) { 44 _terminate = termstr.equals("yes"); 45 } 46 parseChildren(parser); 47 } 48 49 public Type typeCheck(SymbolTable stable) throws TypeCheckError { 50 typeCheckContents(stable); 51 return Type.Void; 52 } 53 54 public void translate(ClassGenerator classGen, MethodGenerator methodGen) { 55 final ConstantPoolGen cpg = classGen.getConstantPool(); 56 final InstructionList il = methodGen.getInstructionList(); 57 58 il.append(classGen.loadTranslet()); 60 61 switch (elementCount()) { 62 case 0: 63 il.append(new PUSH(cpg, "")); 64 break; 65 case 1: 66 SyntaxTreeNode child = (SyntaxTreeNode) elementAt(0); 67 if (child instanceof Text) { 68 il.append(new PUSH(cpg, ((Text) child).getText())); 69 break; 70 } 71 default: 73 il.append(methodGen.loadHandler()); 75 76 il.append(new NEW(cpg.addClass(STREAM_XML_OUTPUT))); 78 il.append(methodGen.storeHandler()); 79 80 il.append(new NEW(cpg.addClass(STRING_WRITER))); 82 il.append(DUP); 83 il.append(DUP); 84 il.append(new INVOKESPECIAL( 85 cpg.addMethodref(STRING_WRITER, "<init>", "()V"))); 86 87 il.append(methodGen.loadHandler()); 89 il.append(new INVOKESPECIAL( 90 cpg.addMethodref(STREAM_XML_OUTPUT, "<init>", 91 "()V"))); 92 93 il.append(methodGen.loadHandler()); 95 il.append(SWAP); 96 il.append(new INVOKEVIRTUAL( 97 cpg.addMethodref(OUTPUT_BASE, "setWriter", 98 "("+WRITER_SIG+")V"))); 99 100 il.append(methodGen.loadHandler()); 102 il.append(new PUSH(cpg, "UTF-8")); il.append(new INVOKEVIRTUAL( 104 cpg.addMethodref(OUTPUT_BASE, "setEncoding", 105 "("+STRING_SIG+")V"))); 106 107 il.append(methodGen.loadHandler()); 109 il.append(ICONST_1); 110 il.append(new INVOKEVIRTUAL( 111 cpg.addMethodref(OUTPUT_BASE, "setOmitXMLDeclaration", 112 "(Z)V"))); 113 114 il.append(methodGen.loadHandler()); 115 il.append(new INVOKEVIRTUAL( 116 cpg.addMethodref(OUTPUT_BASE, "startDocument", 117 "()V"))); 118 119 translateContents(classGen, methodGen); 121 122 il.append(methodGen.loadHandler()); 123 il.append(new INVOKEVIRTUAL( 124 cpg.addMethodref(OUTPUT_BASE, "endDocument", 125 "()V"))); 126 127 il.append(new INVOKEVIRTUAL( 129 cpg.addMethodref(STRING_WRITER, "toString", 130 "()" + STRING_SIG))); 131 132 il.append(SWAP); 134 il.append(methodGen.storeHandler()); 135 break; 136 } 137 138 il.append(new INVOKEVIRTUAL(cpg.addMethodref(TRANSLET_CLASS, 140 "displayMessage", 141 "("+STRING_SIG+")V"))); 142 143 if (_terminate == true) { 146 final int einit = cpg.addMethodref("java.lang.RuntimeException", 148 "<init>", 149 "(Ljava/lang/String;)V"); 150 il.append(new NEW(cpg.addClass("java.lang.RuntimeException"))); 151 il.append(DUP); 152 il.append(new PUSH(cpg,"Termination forced by an " + 153 "xsl:message instruction")); 154 il.append(new INVOKESPECIAL(einit)); 155 il.append(ATHROW); 156 } 157 } 158 159 } 160 | Popular Tags |