KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > jasper > compiler > SmapGenerator


1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one or more
3  * contributor license agreements. See the NOTICE file distributed with
4  * this work for additional information regarding copyright ownership.
5  * The ASF licenses this file to You under the Apache License, Version 2.0
6  * (the "License"); you may not use this file except in compliance with
7  * the License. You may obtain a copy of the License at
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */

17
18 package org.apache.jasper.compiler;
19
20 import java.util.List JavaDoc;
21 import java.util.ArrayList JavaDoc;
22
23 /**
24  * Represents a source map (SMAP), which serves to associate lines
25  * of the input JSP file(s) to lines in the generated servlet in the
26  * final .class file, according to the JSR-045 spec.
27  *
28  * @author Shawn Bayern
29  */

30 public class SmapGenerator {
31
32     //*********************************************************************
33
// Overview
34

35     /*
36      * The SMAP syntax is reasonably straightforward. The purpose of this
37      * class is currently twofold:
38      * - to provide a simple but low-level Java interface to build
39      * a logical SMAP
40      * - to serialize this logical SMAP for eventual inclusion directly
41      * into a .class file.
42      */

43
44
45     //*********************************************************************
46
// Private state
47

48     private String JavaDoc outputFileName;
49     private String JavaDoc defaultStratum = "Java";
50     private List JavaDoc strata = new ArrayList JavaDoc();
51     private List JavaDoc embedded = new ArrayList JavaDoc();
52     private boolean doEmbedded = true;
53
54     //*********************************************************************
55
// Methods for adding mapping data
56

57     /**
58      * Sets the filename (without path information) for the generated
59      * source file. E.g., "foo$jsp.java".
60      */

61     public synchronized void setOutputFileName(String JavaDoc x) {
62     outputFileName = x;
63     }
64
65     /**
66      * Adds the given SmapStratum object, representing a Stratum with
67      * logically associated FileSection and LineSection blocks, to
68      * the current SmapGenerator. If <tt>default</tt> is true, this
69      * stratum is made the default stratum, overriding any previously
70      * set default.
71      *
72      * @param stratum the SmapStratum object to add
73      * @param defaultStratum if <tt>true</tt>, this SmapStratum is considered
74      * to represent the default SMAP stratum unless
75      * overwritten
76      */

77     public synchronized void addStratum(SmapStratum stratum,
78                     boolean defaultStratum) {
79     strata.add(stratum);
80     if (defaultStratum)
81         this.defaultStratum = stratum.getStratumName();
82     }
83
84     /**
85      * Adds the given string as an embedded SMAP with the given stratum name.
86      *
87      * @param smap the SMAP to embed
88      * @param stratumName the name of the stratum output by the compilation
89      * that produced the <tt>smap</tt> to be embedded
90      */

91     public synchronized void addSmap(String JavaDoc smap, String JavaDoc stratumName) {
92     embedded.add("*O " + stratumName + "\n"
93            + smap
94            + "*C " + stratumName + "\n");
95     }
96
97     /**
98      * Instructs the SmapGenerator whether to actually print any embedded
99      * SMAPs or not. Intended for situations without an SMAP resolver.
100      *
101      * @param status If <tt>false</tt>, ignore any embedded SMAPs.
102      */

103     public void setDoEmbedded(boolean status) {
104     doEmbedded = status;
105     }
106
107     //*********************************************************************
108
// Methods for serializing the logical SMAP
109

110     public synchronized String JavaDoc getString() {
111     // check state and initialize buffer
112
if (outputFileName == null)
113         throw new IllegalStateException JavaDoc();
114         StringBuffer JavaDoc out = new StringBuffer JavaDoc();
115
116     // start the SMAP
117
out.append("SMAP\n");
118     out.append(outputFileName + '\n');
119     out.append(defaultStratum + '\n');
120
121     // include embedded SMAPs
122
if (doEmbedded) {
123         int nEmbedded = embedded.size();
124         for (int i = 0; i < nEmbedded; i++) {
125             out.append(embedded.get(i));
126         }
127     }
128
129     // print our StratumSections, FileSections, and LineSections
130
int nStrata = strata.size();
131     for (int i = 0; i < nStrata; i++) {
132         SmapStratum s = (SmapStratum) strata.get(i);
133         out.append(s.getString());
134     }
135
136     // end the SMAP
137
out.append("*E\n");
138
139     return out.toString();
140     }
141
142     public String JavaDoc toString() { return getString(); }
143
144     //*********************************************************************
145
// For testing (and as an example of use)...
146

147     public static void main(String JavaDoc args[]) {
148     SmapGenerator g = new SmapGenerator();
149     g.setOutputFileName("foo.java");
150     SmapStratum s = new SmapStratum("JSP");
151     s.addFile("foo.jsp");
152     s.addFile("bar.jsp", "/foo/foo/bar.jsp");
153     s.addLineData(1, "foo.jsp", 1, 1, 1);
154     s.addLineData(2, "foo.jsp", 1, 6, 1);
155     s.addLineData(3, "foo.jsp", 2, 10, 5);
156     s.addLineData(20, "bar.jsp", 1, 30, 1);
157     g.addStratum(s, true);
158     System.out.print(g);
159
160     System.out.println("---");
161
162     SmapGenerator embedded = new SmapGenerator();
163     embedded.setOutputFileName("blargh.tier2");
164     s = new SmapStratum("Tier2");
165     s.addFile("1.tier2");
166     s.addLineData(1, "1.tier2", 1, 1, 1);
167     embedded.addStratum(s, true);
168     g.addSmap(embedded.toString(), "JSP");
169     System.out.println(g);
170     }
171 }
172
Popular Tags