src/java.xml/share/classes/com/sun/org/apache/xalan/internal/xsltc/compiler/Choose.java
changeset 47359 e1a6c0168741
parent 47216 71c04702a3d5
child 48409 5ab69533994b
equal deleted inserted replaced
47358:d07d5f7cab35 47359:e1a6c0168741
     1 /*
     1 /*
     2  * Copyright (c) 2015, Oracle and/or its affiliates. All rights reserved.
     2  * Copyright (c) 2015, 2017, Oracle and/or its affiliates. All rights reserved.
       
     3  * @LastModified: Oct 2017
     3  */
     4  */
     4 /*
     5 /*
     5  * Licensed to the Apache Software Foundation (ASF) under one or more
     6  * Licensed to the Apache Software Foundation (ASF) under one or more
     6  * contributor license agreements.  See the NOTICE file distributed with
     7  * contributor license agreements.  See the NOTICE file distributed with
     7  * this work for additional information regarding copyright ownership.
     8  * this work for additional information regarding copyright ownership.
    21  * $Id: Choose.java,v 1.2.4.1 2005/09/01 12:00:14 pvedula Exp $
    22  * $Id: Choose.java,v 1.2.4.1 2005/09/01 12:00:14 pvedula Exp $
    22  */
    23  */
    23 
    24 
    24 package com.sun.org.apache.xalan.internal.xsltc.compiler;
    25 package com.sun.org.apache.xalan.internal.xsltc.compiler;
    25 
    26 
    26 import java.util.Enumeration;
       
    27 import java.util.Vector;
       
    28 
       
    29 import com.sun.org.apache.bcel.internal.generic.BranchHandle;
    27 import com.sun.org.apache.bcel.internal.generic.BranchHandle;
    30 import com.sun.org.apache.bcel.internal.generic.GOTO;
    28 import com.sun.org.apache.bcel.internal.generic.GOTO;
    31 import com.sun.org.apache.bcel.internal.generic.IFEQ;
    29 import com.sun.org.apache.bcel.internal.generic.IFEQ;
    32 import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
    30 import com.sun.org.apache.bcel.internal.generic.InstructionHandle;
    33 import com.sun.org.apache.bcel.internal.generic.InstructionList;
    31 import com.sun.org.apache.bcel.internal.generic.InstructionList;
    35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
    33 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.ErrorMsg;
    36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
    34 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.MethodGenerator;
    37 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
    35 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Type;
    38 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
    36 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.TypeCheckError;
    39 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
    37 import com.sun.org.apache.xalan.internal.xsltc.compiler.util.Util;
       
    38 import java.util.ArrayList;
       
    39 import java.util.Collections;
       
    40 import java.util.Enumeration;
    40 import java.util.Iterator;
    41 import java.util.Iterator;
       
    42 import java.util.List;
    41 
    43 
    42 /**
    44 /**
    43  * @author Jacek Ambroziak
    45  * @author Jacek Ambroziak
    44  * @author Santiago Pericas-Geertsen
    46  * @author Santiago Pericas-Geertsen
    45  * @author Morten Jorgensen
    47  * @author Morten Jorgensen
    59     /**
    61     /**
    60      * Translate this Choose element. Generate a test-chain for the various
    62      * Translate this Choose element. Generate a test-chain for the various
    61      * <xsl:when> elements and default to the <xsl:otherwise> if present.
    63      * <xsl:when> elements and default to the <xsl:otherwise> if present.
    62      */
    64      */
    63     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    65     public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    64         final Vector whenElements = new Vector();
    66         final List<SyntaxTreeNode> whenElements = new ArrayList<>();
    65         Otherwise otherwise = null;
    67         Otherwise otherwise = null;
    66         Iterator<SyntaxTreeNode> elements = elements();
    68         Iterator<SyntaxTreeNode> elements = elements();
    67 
    69 
    68         // These two are for reporting errors only
    70         // These two are for reporting errors only
    69         ErrorMsg error = null;
    71         ErrorMsg error = null;
    72         // Traverse all child nodes - must be either When or Otherwise
    74         // Traverse all child nodes - must be either When or Otherwise
    73         while (elements.hasNext()) {
    75         while (elements.hasNext()) {
    74             SyntaxTreeNode element = elements.next();
    76             SyntaxTreeNode element = elements.next();
    75             // Add a When child element
    77             // Add a When child element
    76             if (element instanceof When) {
    78             if (element instanceof When) {
    77                 whenElements.addElement(element);
    79                 whenElements.add(element);
    78             }
    80             }
    79             // Add an Otherwise child element
    81             // Add an Otherwise child element
    80             else if (element instanceof Otherwise) {
    82             else if (element instanceof Otherwise) {
    81                 if (otherwise == null) {
    83                 if (otherwise == null) {
    82                     otherwise = (Otherwise)element;
    84                     otherwise = (Otherwise)element;
   106         InstructionList il = methodGen.getInstructionList();
   108         InstructionList il = methodGen.getInstructionList();
   107 
   109 
   108         // next element will hold a handle to the beginning of next
   110         // next element will hold a handle to the beginning of next
   109         // When/Otherwise if test on current When fails
   111         // When/Otherwise if test on current When fails
   110         BranchHandle nextElement = null;
   112         BranchHandle nextElement = null;
   111         Vector exitHandles = new Vector();
   113         List<InstructionHandle> exitHandles = new ArrayList<>();
   112         InstructionHandle exit = null;
   114         InstructionHandle exit = null;
   113 
   115 
   114         Enumeration whens = whenElements.elements();
   116         Enumeration<SyntaxTreeNode> whens = Collections.enumeration(whenElements);
   115         while (whens.hasMoreElements()) {
   117         while (whens.hasMoreElements()) {
   116             final When when = (When)whens.nextElement();
   118             final When when = (When)whens.nextElement();
   117             final Expression test = when.getTest();
   119             final Expression test = when.getTest();
   118 
   120 
   119             InstructionHandle truec = il.getEnd();
   121             InstructionHandle truec = il.getEnd();
   140             // The When object should be ignored completely in case it tests
   142             // The When object should be ignored completely in case it tests
   141             // for the support of a non-available element
   143             // for the support of a non-available element
   142             if (!when.ignore()) when.translateContents(classGen, methodGen);
   144             if (!when.ignore()) when.translateContents(classGen, methodGen);
   143 
   145 
   144             // goto exit after executing the body of when
   146             // goto exit after executing the body of when
   145             exitHandles.addElement(il.append(new GOTO(null)));
   147             exitHandles.add(il.append(new GOTO(null)));
   146             if (whens.hasMoreElements() || otherwise != null) {
   148             if (whens.hasMoreElements() || otherwise != null) {
   147                 nextElement = il.append(new GOTO(null));
   149                 nextElement = il.append(new GOTO(null));
   148                 test.backPatchFalseList(nextElement);
   150                 test.backPatchFalseList(nextElement);
   149             }
   151             }
   150             else
   152             else
   158             otherwise.translateContents(classGen, methodGen);
   160             otherwise.translateContents(classGen, methodGen);
   159             exit = il.append(NOP);
   161             exit = il.append(NOP);
   160         }
   162         }
   161 
   163 
   162         // now that end is known set targets of exit gotos
   164         // now that end is known set targets of exit gotos
   163         Enumeration exitGotos = exitHandles.elements();
   165         Enumeration<InstructionHandle> exitGotos = Collections.enumeration(exitHandles);
   164         while (exitGotos.hasMoreElements()) {
   166         while (exitGotos.hasMoreElements()) {
   165             BranchHandle gotoExit = (BranchHandle)exitGotos.nextElement();
   167             BranchHandle gotoExit = (BranchHandle)exitGotos.nextElement();
   166             gotoExit.setTarget(exit);
   168             gotoExit.setTarget(exit);
   167         }
   169         }
   168     }
   170     }