1 /*
2 * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
3 */
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20 /*
21 * $Id: XslElement.java,v 1.2.4.1 2005/09/12 11:39:55 pvedula Exp $
22 */
23
24 package com.sun.org.apache.xalan.internal.xsltc.compiler;
25
26 import com.sun.org.apache.bcel.internal.generic.ALOAD;
27 import com.sun.org.apache.bcel.internal.generic.ASTORE;
28 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
29 import com.sun.org.apache.bcel.internal.generic.ICONST;
30 import com.sun.org.apache.bcel.internal.generic.INVOKESTATIC;
31 import com.sun.org.apache.bcel.internal.generic.InstructionList;
32 import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
33 import com.sun.org.apache.bcel.internal.generic.PUSH;
34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
37 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
38 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
39 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
40 import com.sun.org.apache.xml.internal.utils.XML11Char;
41
42 /**
43 * @author Jacek Ambroziak
44 * @author Santiago Pericas-Geertsen
45 * @author Morten Jorgensen
46 */
47 final class XslElement extends Instruction {
48
49 private String _prefix;
50 private boolean _ignore = false;
51 private boolean _isLiteralName = true;
52 private AttributeValueTemplate _name;
53 private AttributeValueTemplate _namespace;
54
55 /**
56 * Displays the contents of the element
57 */
58 public void display(int indent) {
59 indent(indent);
60 Util.println("Element " + _name);
61 displayContents(indent + IndentIncrement);
62 }
63
64 /**
65 * This method is now deprecated. The new implemation of this class
66 * never declares the default NS.
67 */
68 public boolean declaresDefaultNS() {
69 return false;
70 }
71
72 public void parseContents(Parser parser) {
73 final SymbolTable stable = parser.getSymbolTable();
74
75 // Handle the 'name' attribute
76 String name = getAttribute("name");
77 if (name == EMPTYSTRING) {
78 ErrorMsg msg = new ErrorMsg(ErrorMsg.ILLEGAL_ELEM_NAME_ERR,
79 name, this);
80 parser.reportError(WARNING, msg);
81 parseChildren(parser);
82 _ignore = true; // Ignore the element if the QName is invalid
83 return;
84 }
85
86 // Get namespace attribute
87 String namespace = getAttribute("namespace");
88
89 // Optimize compilation when name is known at compile time
90 _isLiteralName = Util.isLiteral(name);
91 if (_isLiteralName) {
194 il.append(methodGen.namespace());
195 }
196 }
197
198 translateContents(classGen, methodGen);
199
200 if (!_ignore) {
201 il.append(methodGen.endElement());
202 }
203 }
204
205 /**
206 * At runtime the compilation of xsl:element results in code that: (i)
207 * evaluates the avt for the name, (ii) checks for a prefix in the name
208 * (iii) generates a new prefix and create a new qname when necessary
209 * (iv) calls startElement() on the handler (v) looks up a uri in the XML
210 * when the prefix is not known at compile time (vi) calls namespace()
211 * on the handler (vii) evaluates the contents (viii) calls endElement().
212 */
213 public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
214 LocalVariableGen local = null;
215 final ConstantPoolGen cpg = classGen.getConstantPool();
216 final InstructionList il = methodGen.getInstructionList();
217
218 // Optimize translation if element name is a literal
219 if (_isLiteralName) {
220 translateLiteral(classGen, methodGen);
221 return;
222 }
223
224 if (!_ignore) {
225
226 // if the qname is an AVT, then the qname has to be checked at runtime if it is a valid qname
227 LocalVariableGen nameValue =
228 methodGen.addLocalVariable2("nameValue",
229 Util.getJCRefType(STRING_SIG),
230 null);
231
232 // store the name into a variable first so _name.translate only needs to be called once
233 _name.translate(classGen, methodGen);
234 nameValue.setStart(il.append(new ASTORE(nameValue.getIndex())));
|
1 /*
2 * Copyright (c) 2015, 2016, Oracle and/or its affiliates. All rights reserved.
3 */
4 /*
5 * Licensed to the Apache Software Foundation (ASF) under one or more
6 * contributor license agreements. See the NOTICE file distributed with
7 * this work for additional information regarding copyright ownership.
8 * The ASF licenses this file to You under the Apache License, Version 2.0
9 * (the "License"); you may not use this file except in compliance with
10 * the License. You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 */
20
21 package com.sun.org.apache.xalan.internal.xsltc.compiler;
22
23 import com.sun.org.apache.bcel.internal.generic.ALOAD;
24 import com.sun.org.apache.bcel.internal.generic.ASTORE;
25 import com.sun.org.apache.bcel.internal.generic.ConstantPoolGen;
26 import com.sun.org.apache.bcel.internal.generic.INVOKESTATIC;
27 import com.sun.org.apache.bcel.internal.generic.InstructionList;
28 import com.sun.org.apache.bcel.internal.generic.LocalVariableGen;
29 import com.sun.org.apache.bcel.internal.generic.PUSH;
30 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ClassGenerator;
31 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
32 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
36 import com.sun.org.apache.xml.internal.utils.XML11Char;
37
38 /**
39 * @author Jacek Ambroziak
40 * @author Santiago Pericas-Geertsen
41 * @author Morten Jorgensen
42 */
43 final class XslElement extends Instruction {
44
45 private String _prefix;
46 private boolean _ignore = false;
47 private boolean _isLiteralName = true;
48 private AttributeValueTemplate _name;
49 private AttributeValueTemplate _namespace;
50
51 /**
52 * Displays the contents of the element
53 */
54 public void display(int indent) {
55 indent(indent);
56 Util.println("Element " + _name);
57 displayContents(indent + IndentIncrement);
58 }
59
60 public void parseContents(Parser parser) {
61 final SymbolTable stable = parser.getSymbolTable();
62
63 // Handle the 'name' attribute
64 String name = getAttribute("name");
65 if (name == EMPTYSTRING) {
66 ErrorMsg msg = new ErrorMsg(ErrorMsg.ILLEGAL_ELEM_NAME_ERR,
67 name, this);
68 parser.reportError(WARNING, msg);
69 parseChildren(parser);
70 _ignore = true; // Ignore the element if the QName is invalid
71 return;
72 }
73
74 // Get namespace attribute
75 String namespace = getAttribute("namespace");
76
77 // Optimize compilation when name is known at compile time
78 _isLiteralName = Util.isLiteral(name);
79 if (_isLiteralName) {
182 il.append(methodGen.namespace());
183 }
184 }
185
186 translateContents(classGen, methodGen);
187
188 if (!_ignore) {
189 il.append(methodGen.endElement());
190 }
191 }
192
193 /**
194 * At runtime the compilation of xsl:element results in code that: (i)
195 * evaluates the avt for the name, (ii) checks for a prefix in the name
196 * (iii) generates a new prefix and create a new qname when necessary
197 * (iv) calls startElement() on the handler (v) looks up a uri in the XML
198 * when the prefix is not known at compile time (vi) calls namespace()
199 * on the handler (vii) evaluates the contents (viii) calls endElement().
200 */
201 public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
202 final ConstantPoolGen cpg = classGen.getConstantPool();
203 final InstructionList il = methodGen.getInstructionList();
204
205 // Optimize translation if element name is a literal
206 if (_isLiteralName) {
207 translateLiteral(classGen, methodGen);
208 return;
209 }
210
211 if (!_ignore) {
212
213 // if the qname is an AVT, then the qname has to be checked at runtime if it is a valid qname
214 LocalVariableGen nameValue =
215 methodGen.addLocalVariable2("nameValue",
216 Util.getJCRefType(STRING_SIG),
217 null);
218
219 // store the name into a variable first so _name.translate only needs to be called once
220 _name.translate(classGen, methodGen);
221 nameValue.setStart(il.append(new ASTORE(nameValue.getIndex())));
|