/*
 * 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.util.ArrayList;
import java.util.List;

/**
 * Represents a source map (SMAP), which serves to associate lines
 * of the input JSP file(s) to lines in the generated servlet in the
 * final .class file, according to the JSR-045 spec.
 *
 * @author Shawn Bayern
 */
public class SmapGenerator {

    //*********************************************************************
    // Overview

    /*
     * The SMAP syntax is reasonably straightforward.  The purpose of this
     * class is currently twofold:
     *  - to provide a simple but low-level Java interface to build
     *    a logical SMAP
     *  - to serialize this logical SMAP for eventual inclusion directly
     *    into a .class file.
     *
     * There are aspects of the SMAP syntax that this class does not support.
     * It provides all the features required for JSP and associated files but no
     * more.
     */


    //*********************************************************************
    // Private state

    private String outputFileName;
    private String defaultStratum = "Java";
    private final List<SmapStratum> strata = new ArrayList<>();

    //*********************************************************************
    // Methods for adding mapping data

    /**
     * Sets the filename (without path information) for the generated
     * source file.  E.g., "foo$jsp.java".
     * @param x The file name
     */
    public synchronized void setOutputFileName(String x) {
        outputFileName = x;
    }


    /**
     * Sets the default and only stratum for the smap.
     *
     * @param stratum the SmapStratum object to add
     */
    public synchronized void setStratum(SmapStratum stratum) {
        addStratum(stratum, true);
    }


    /**
     * Adds the given SmapStratum object, representing a Stratum with
     * logically associated FileSection and LineSection blocks, to
     * the current SmapGenerator.  If <tt>default</tt> is true, this
     * stratum is made the default stratum, overriding any previously
     * set default.
     *
     * @param stratum the SmapStratum object to add
     * @param defaultStratum if <tt>true</tt>, this SmapStratum is considered
     *                to represent the default SMAP stratum unless
     *                overwritten
     *
     * @deprecated Use {@link #setStratum(SmapStratum)}
     */
    @Deprecated
    public synchronized void addStratum(SmapStratum stratum,
                                        boolean defaultStratum) {
        strata.add(stratum);
        if (defaultStratum)
            this.defaultStratum = stratum.getStratumName();
    }


    //*********************************************************************
    // Methods for serializing the logical SMAP

    public synchronized String getString() {
        // check state and initialize buffer
        if (outputFileName == null)
            throw new IllegalStateException();
        StringBuilder out = new StringBuilder();

        // start the SMAP
        out.append("SMAP\n");
        out.append(outputFileName + '\n');
        out.append(defaultStratum + '\n');

        // print our StratumSections, FileSections, and LineSections
        int nStrata = strata.size();
        for (int i = 0; i < nStrata; i++) {
            SmapStratum s = strata.get(i);
            out.append(s.getString());
        }

        // end the SMAP
        out.append("*E\n");

        return out.toString();
    }

    @Override
    public String toString() { return getString(); }
}

@@ -838,60 +838,8 @@
 r;%0A%0A
-import java.util.ArrayList;%0Aimport java.util.List;%0A%0A
 /**%0A
@@ -1833,101 +1833,26 @@
 te S
-tring defaultStratum = %22Java%22;%0A    private final List%3CSmapStratum%3E strata = new ArrayList%3C%3E()
+mapStratum stratum
 ;%0A%0A 
@@ -2425,914 +2425,30 @@
     
-addStratum(stratum, true);%0A    %7D%0A%0A%0A    /**%0A     * Adds the given SmapStratum object, representing a Stratum with%0A     * logically associated FileSection and LineSection blocks, to%0A     * the current SmapGenerator.  If %3Ctt%3Edefault%3C/tt%3E is true, this%0A     * stratum is made the default stratum, overriding any previously%0A     * set default.%0A     *%0A     * @param stratum the SmapStratum object to add%0A     * @param defaultStratum if %3Ctt%3Etrue%3C/tt%3E, this SmapStratum is considered%0A     *                to represent the default SMAP stratum unless%0A     *                overwritten%0A     *%0A     * @deprecated Use %7B@link #setStratum(SmapStratum)%7D%0A     */%0A    @Deprecated%0A    public synchronized void addStratum(SmapStratum stratum,%0A                                        boolean defaultStratum) %7B%0A        strata.add(stratum);%0A        if (defaultStratum)%0A            this.defaultStratum = stratum.getStratumName()
+this.stratum = stratum
 ;%0A  
@@ -2703,16 +2703,18 @@
 == null)
+ %7B
 %0A       
@@ -2749,24 +2749,35 @@
 xception();%0A
+        %7D%0A%0A
         Stri
@@ -2936,29 +2936,15 @@
 end(
-defaultStratum + '%5Cn'
+%22JSP%5Cn%22
 );%0A%0A
@@ -2978,17 +2978,16 @@
 mSection
-s
 , FileSe
@@ -2991,17 +2991,16 @@
 eSection
-s
 , and Li
@@ -3014,136 +3014,8 @@
 ons%0A
-        int nStrata = strata.size();%0A        for (int i = 0; i %3C nStrata; i++) %7B%0A            SmapStratum s = strata.get(i);%0A    
     
@@ -3030,16 +3030,22 @@
 append(s
+tratum
 .getStri
@@ -3050,26 +3050,16 @@
 ring());
-%0A        %7D
 %0A%0A      

Remove unused code