FileDocCategorySizeDatePackage
SmapStratum.javaAPI DocGlassfish v2 API13371Fri May 04 22:32:52 BST 2007org.apache.jasper.compiler

SmapStratum

public class SmapStratum extends Object
Represents the line and file mappings associated with a JSR-045 "stratum".
author
Jayson Falkner
author
Shawn Bayern

Fields Summary
private String
stratumName
private List
fileNameList
private List
filePathList
private List
lineData
private int
lastFileID
Constructors Summary
public SmapStratum(String stratumName)
Constructs a new SmapStratum object for the given stratum name (e.g., JSP).

param
stratumName the name of the stratum (e.g., JSP)

        this.stratumName = stratumName;
        fileNameList = new ArrayList();
        filePathList = new ArrayList();
        lineData = new ArrayList();
        lastFileID = 0;
    
Methods Summary
public voidaddFile(java.lang.String filename)
Adds record of a new file, by filename.

param
filename the filename to add, unqualified by path.

        addFile(filename, filename);
    
public voidaddFile(java.lang.String filename, java.lang.String filePath)
Adds record of a new file, by filename and path. The path may be relative to a source compilation path.

param
filename the filename to add, unqualified by path
param
filePath the path for the filename, potentially relative to a source compilation path

        int pathIndex = filePathList.indexOf(filePath);
        if (pathIndex == -1) {
            fileNameList.add(filename);
            filePathList.add(filePath);
        }
    
public voidaddLineData(int inputStartLine, java.lang.String inputFileName, int inputLineCount, int outputStartLine, int outputLineIncrement)
Adds complete information about a simple line mapping. Specify all the fields in this method; the back-end machinery takes care of printing only those that are necessary in the final SMAP. (My view is that fields are optional primarily for spatial efficiency, not for programmer convenience. Could always add utility methods later.)

param
inputStartLine starting line in the source file (SMAP InputStartLine)
param
inputFileName the filepath (or name) from which the input comes (yields SMAP LineFileID) Use unqualified names carefully, and only when they uniquely identify a file.
param
inputLineCount the number of lines in the input to map (SMAP LineFileCount)
param
outputStartLine starting line in the output file (SMAP OutputStartLine)
param
outputLineIncrement number of output lines to map to each input line (SMAP OutputLineIncrement). Given the fact that the name starts with "output", I continuously have the subconscious urge to call this field OutputLineExcrement.

        // check the input - what are you doing here??
        int fileIndex = filePathList.indexOf(inputFileName);
        if (fileIndex == -1) // still
            throw new IllegalArgumentException(
                "inputFileName: " + inputFileName);

        //Jasper incorrectly SMAPs certain Nodes, giving them an 
        //outputStartLine of 0.  This can cause a fatal error in
        //optimizeLineSection, making it impossible for Jasper to
        //compile the JSP.  Until we can fix the underlying
        //SMAPping problem, we simply ignore the flawed SMAP entries.
        if (outputStartLine == 0)
            return;

        // build the LineInfo
        LineInfo li = new LineInfo();
        li.setInputStartLine(inputStartLine);
        li.setInputLineCount(inputLineCount);
        li.setOutputStartLine(outputStartLine);
        li.setOutputLineIncrement(outputLineIncrement);
        if (fileIndex != lastFileID)
            li.setLineFileID(fileIndex);
        lastFileID = fileIndex;

        // save it
        lineData.add(li);
    
public java.lang.StringgetStratumName()
Returns the name of the stratum.

        return stratumName;
    
public java.lang.StringgetString()
Returns the given stratum as a String: a StratumSection, followed by at least one FileSection and at least one LineSection.

        // check state and initialize buffer
        if (fileNameList.size() == 0 || lineData.size() == 0)
            return null;

        StringBuffer out = new StringBuffer();

        // print StratumSection
        out.append("*S " + stratumName + "\n");

        // print FileSection
        out.append("*F\n");
        int bound = fileNameList.size();
        for (int i = 0; i < bound; i++) {
            if (filePathList.get(i) != null) {
                out.append("+ " + i + " " + fileNameList.get(i) + "\n");
                // Source paths must be relative, not absolute, so we
                // remove the leading "/", if one exists.
                String filePath = (String)filePathList.get(i);
                if (filePath.startsWith("/")) {
                    filePath = filePath.substring(1);
                }
                out.append(filePath + "\n");
            } else {
                out.append(i + " " + fileNameList.get(i) + "\n");
            }
        }

        // print LineSection
        out.append("*L\n");
        bound = lineData.size();
        for (int i = 0; i < bound; i++) {
            LineInfo li = (LineInfo)lineData.get(i);
            out.append(li.getString());
        }

        return out.toString();
    
public voidoptimizeLineSection()
Combines consecutive LineInfos wherever possible


/* Some debugging code
        for (int i = 0; i < lineData.size(); i++) {
            LineInfo li = (LineInfo)lineData.get(i);
            System.out.print(li.toString());
        }
*/
        //Incorporate each LineInfo into the previous LineInfo's 
        //outputLineIncrement, if possible
        int i = 0;
        while (i < lineData.size() - 1) {
            LineInfo li = (LineInfo)lineData.get(i);
            LineInfo liNext = (LineInfo)lineData.get(i + 1);
            if (!liNext.lineFileIDSet
                && liNext.inputStartLine == li.inputStartLine
                && liNext.inputLineCount == 1
                && li.inputLineCount == 1
                && liNext.outputStartLine
                    == li.outputStartLine
                        + li.inputLineCount * li.outputLineIncrement) {
                li.setOutputLineIncrement(
                    liNext.outputStartLine
                        - li.outputStartLine
                        + liNext.outputLineIncrement);
                lineData.remove(i + 1);
            } else {
                i++;
            }
        }

        //Incorporate each LineInfo into the previous LineInfo's
        //inputLineCount, if possible
        i = 0;
        while (i < lineData.size() - 1) {
            LineInfo li = (LineInfo)lineData.get(i);
            LineInfo liNext = (LineInfo)lineData.get(i + 1);
            if (!liNext.lineFileIDSet
                && liNext.inputStartLine == li.inputStartLine + li.inputLineCount
                && liNext.outputLineIncrement == li.outputLineIncrement
                && liNext.outputStartLine
                    == li.outputStartLine
                        + li.inputLineCount * li.outputLineIncrement) {
                li.setInputLineCount(li.inputLineCount + liNext.inputLineCount);
                lineData.remove(i + 1);
            } else {
                i++;
            }
        }
    
public java.lang.StringtoString()

        return getString();