1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191
|
/*
* Copyright (c) 2007, 2018, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*/
package nsk.share.jdi.sde;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Represents a source map (SMAP), which serves to associate lines
* of the input source to lines in the generated output source in the
* final .class file, according to the JSR-045 spec.
*/
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.
*/
//*********************************************************************
// Private state
private String outputFileName;
private String defaultStratum = "Java";
private List<SmapStratum> strata = new ArrayList<SmapStratum>();
private List<String> embedded = new ArrayList<String>();
private boolean doEmbedded = true;
//*********************************************************************
// Methods for adding mapping data
/**
* Sets the filename (without path information) for the generated
* source file. E.g., "foo$jsp.java".
*/
public synchronized void setOutputFileName(String x) {
outputFileName = x;
}
/**
* 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
*/
public synchronized void addStratum(SmapStratum stratum,
boolean defaultStratum) {
strata.add(stratum);
if (defaultStratum)
this.defaultStratum = stratum.getStratumName();
}
/**
* Adds the given string as an embedded SMAP with the given stratum name.
*
* @param smap the SMAP to embed
* @param stratumName the name of the stratum output by the compilation
* that produced the <tt>smap</tt> to be embedded
*/
public synchronized void addSmap(String smap, String stratumName) {
embedded.add("*O " + stratumName + "\n"
+ smap
+ "*C " + stratumName + "\n");
}
/**
* Instructs the SmapGenerator whether to actually print any embedded
* SMAPs or not. Intended for situations without an SMAP resolver.
*
* @param status If <tt>false</tt>, ignore any embedded SMAPs.
*/
public void setDoEmbedded(boolean status) {
doEmbedded = status;
}
public File saveToFile(String fileName)
throws IOException
{
File file = new File(fileName);
file.createNewFile();
FileOutputStream outStream = new FileOutputStream(file);
outStream.write(getString().getBytes());
outStream.close();
return file;
}
//*********************************************************************
// Methods for serializing the logical SMAP
public synchronized String getString() {
// check state and initialize buffer
if (outputFileName == null)
throw new IllegalStateException();
StringBuffer out = new StringBuffer();
// start the SMAP
out.append("SMAP\n");
out.append(outputFileName + '\n');
out.append(defaultStratum + '\n');
// include embedded SMAPs
if (doEmbedded) {
int nEmbedded = embedded.size();
for (int i = 0; i < nEmbedded; i++) {
out.append(embedded.get(i));
}
}
// print our StratumSections, FileSections, and LineSections
int nStrata = strata.size();
for (int i = 0; i < nStrata; i++) {
SmapStratum s = (SmapStratum) strata.get(i);
out.append(s.getString());
}
// end the SMAP
out.append("*E\n");
return out.toString();
}
public String toString() { return getString(); }
//*********************************************************************
// For testing (and as an example of use)...
public static void main(String args[]) {
SmapGenerator g = new SmapGenerator();
g.setOutputFileName("foo.java");
SmapStratum s = new SmapStratum("JSP");
s.addFile("foo.jsp");
s.addFile("bar.jsp", "/foo/foo/bar.jsp");
s.addLineData(1, "foo.jsp", 1, 1, 1);
s.addLineData(2, "foo.jsp", 1, 6, 1);
s.addLineData(3, "foo.jsp", 2, 10, 5);
s.addLineData(20, "bar.jsp", 1, 30, 1);
g.addStratum(s, true);
System.out.print(g);
System.out.println("---");
SmapGenerator embedded = new SmapGenerator();
embedded.setOutputFileName("blargh.tier2");
s = new SmapStratum("Tier2");
s.addFile("1.tier2");
s.addLineData(1, "1.tier2", 1, 1, 1);
embedded.addStratum(s, true);
g.addSmap(embedded.toString(), "JSP");
System.out.println(g);
}
}
|