KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > xml > Attribute


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2004 Jennifer Lhotak
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Lesser General Public
6  * License as published by the Free Software Foundation; either
7  * version 2.1 of the License, or (at your option) any later version.
8  *
9  * This library is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12  * Lesser General Public License for more details.
13  *
14  * You should have received a copy of the GNU Lesser General Public
15  * License along with this library; if not, write to the
16  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17  * Boston, MA 02111-1307, USA.
18  */

19
20 package soot.xml;
21
22 import java.util.*;
23 import java.io.*;
24 import soot.*;
25 import soot.tagkit.*;
26 import soot.util.*;
27
28 public class Attribute {
29     
30     //private ColorAttribute color;
31
private ArrayList colors;
32     private int jimpleStartPos;
33     private int jimpleEndPos;
34     private int javaStartPos;
35     private int javaEndPos;
36     private int javaStartLn;
37     private int javaEndLn;
38     private int jimpleStartLn;
39     private int jimpleEndLn;
40
41     public ArrayList colors(){
42         return colors;
43     }
44
45     public void addColor(ColorAttribute ca){
46         if (colors == null){
47             colors = new ArrayList();
48         }
49         colors.add(ca);
50     }
51
52     /*public ColorAttribute color(){
53         return color;
54     }
55
56     public void color(ColorAttribute c){
57         color = c;
58     }*/

59
60     public int jimpleStartPos(){
61         return jimpleStartPos;
62     }
63
64     public void jimpleStartPos(int x){
65         jimpleStartPos = x;
66     }
67
68     public int jimpleEndPos(){
69         return jimpleEndPos;
70     }
71
72     public void jimpleEndPos(int x){
73         jimpleEndPos = x;
74     }
75     
76     public int javaStartPos(){
77         return javaStartPos;
78     }
79
80     public void javaStartPos(int x){
81         javaStartPos = x;
82     }
83
84     public int javaEndPos(){
85         return javaEndPos;
86     }
87
88     public void javaEndPos(int x){
89         javaEndPos = x;
90     }
91     
92     public int jimpleStartLn(){
93         return jimpleStartLn;
94     }
95
96     public void jimpleStartLn(int x){
97         jimpleStartLn = x;
98     }
99
100     public int jimpleEndLn(){
101         return jimpleEndLn;
102     }
103
104     public void jimpleEndLn(int x){
105         jimpleEndLn = x;
106     }
107     
108     public int javaStartLn(){
109         return javaStartLn;
110     }
111
112     public void javaStartLn(int x){
113         javaStartLn = x;
114     }
115
116     public int javaEndLn(){
117         return javaEndLn;
118     }
119
120     public void javaEndLn(int x){
121         javaEndLn = x;
122     }
123
124     public boolean hasColor(){
125         if (colors != null) return true;
126         else return false;
127     }
128
129     ArrayList texts;
130     ArrayList links;
131
132     public void addText(StringAttribute s){
133         if (texts == null) {
134             texts = new ArrayList();
135         }
136         texts.add(s);
137     }
138
139     public void addLink(LinkAttribute la){
140         if (links == null) {
141             links = new ArrayList();
142         }
143         links.add(la);
144     }
145
146     public void addTag(Tag t){
147         if (t instanceof LineNumberTag) {
148             int lnNum = (new Integer JavaDoc(((LineNumberTag)t).toString())).intValue();
149             javaStartLn(lnNum);
150             javaEndLn(lnNum);
151         }
152         else if (t instanceof JimpleLineNumberTag) {
153             JimpleLineNumberTag jlnTag = (JimpleLineNumberTag)t;
154             jimpleStartLn(jlnTag.getStartLineNumber());
155             jimpleEndLn(jlnTag.getEndLineNumber());
156         }
157         else if (t instanceof SourceLnPosTag) {
158             SourceLnPosTag jlnTag = (SourceLnPosTag)t;
159             javaStartLn(jlnTag.startLn());
160             javaEndLn(jlnTag.endLn());
161             javaStartPos(jlnTag.startPos());
162             javaEndPos(jlnTag.endPos());
163         }
164         else if (t instanceof LinkTag) {
165             LinkTag lt = (LinkTag)t;
166             Host h = lt.getLink();
167             LinkAttribute link = new LinkAttribute(lt.getInfo(), getJimpleLnOfHost(h), getJavaLnOfHost(h), lt.getClassName(), lt.getAnalysisType());
168             addLink(link);
169
170         }
171         else if (t instanceof StringTag) {
172             StringTag st = (StringTag)t;
173             StringAttribute string = new StringAttribute(formatForXML(st.getInfo()), st.getAnalysisType());
174             addText(string);
175         }
176         else if (t instanceof PositionTag){
177             PositionTag pt = (PositionTag)t;
178             jimpleStartPos(pt.getStartOffset());
179             jimpleEndPos(pt.getEndOffset());
180         }
181         else if (t instanceof ColorTag){
182             ColorTag ct = (ColorTag)t;
183             ColorAttribute ca = new ColorAttribute(ct.getRed(), ct.getGreen(), ct.getBlue(), ct.isForeground(), ct.getAnalysisType());
184             //color(ca);
185
addColor(ca);
186         }
187         /*else if (t instanceof SourcePositionTag){
188         }
189         else if (t instanceof SourceLineNumberTag){
190         }*/

191         else {
192             //System.out.println("t is: "+t.getClass());
193
StringAttribute sa = new StringAttribute(t.toString(), t.getName());
194             addText(sa);
195         }
196         
197     }
198     
199     private String JavaDoc formatForXML(String JavaDoc in) {
200         in = StringTools.replaceAll(in, "<", "&lt;");
201         in = StringTools.replaceAll(in, ">", "&gt;");
202         in = StringTools.replaceAll(in, "&", "&amp;");
203         in = StringTools.replaceAll(in, "\"", "&quot;");
204         return in;
205     }
206     
207     private int getJavaLnOfHost(Host h){
208         Iterator it = h.getTags().iterator();
209         while (it.hasNext()){
210             Tag t = (Tag)it.next();
211             if (t instanceof SourceLnPosTag) {
212                 return ((SourceLnPosTag)t).startLn();
213             }
214             else if (t instanceof LineNumberTag){
215                 return (new Integer JavaDoc(((LineNumberTag)t).toString())).intValue();
216             }
217         }
218         return 0;
219     }
220     
221     private int getJimpleLnOfHost(Host h){
222         Iterator it = h.getTags().iterator();
223         while (it.hasNext()){
224             Tag t = (Tag)it.next();
225             if (t instanceof JimpleLineNumberTag) {
226                 return ((JimpleLineNumberTag)t).getStartLineNumber();
227             }
228         }
229         return 0;
230     }
231
232     public String JavaDoc toString(){
233         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
234         sb.append("<srcPos sline=\""+javaStartLn()+"\" eline=\""+javaEndLn()+"\" spos=\""+javaStartPos()+"\" epos=\""+javaEndPos()+"\"/>");
235         sb.append("<jmpPos sline=\""+jimpleStartLn()+"\" eline=\""+jimpleEndLn()+"\" spos=\""+jimpleStartPos()+"\" epos=\""+jimpleEndPos()+"\"/>");
236         return sb.toString();
237     }
238
239     public void print(PrintWriter writerOut){
240         if (colors == null && texts == null && links == null) {
241             //System.out.println("no data found for: ");
242
//System.out.println("<srcPos sline=\""+javaStartLn()+"\" eline=\""+javaEndLn()+"\" spos=\""+javaStartPos()+"\" epos=\""+javaEndPos()+"\"/>");
243
//System.out.println("<jmpPos sline=\""+jimpleStartLn()+"\" eline=\""+jimpleEndLn()+"\" spos=\""+jimpleStartPos()+"\" epos=\""+jimpleEndPos()+"\"/>");
244
return;
245         }
246         writerOut.println("<attribute>");
247         writerOut.println("<srcPos sline=\""+javaStartLn()+"\" eline=\""+javaEndLn()+"\" spos=\""+javaStartPos()+"\" epos=\""+javaEndPos()+"\"/>");
248         writerOut.println("<jmpPos sline=\""+jimpleStartLn()+"\" eline=\""+jimpleEndLn()+"\" spos=\""+jimpleStartPos()+"\" epos=\""+jimpleEndPos()+"\"/>");
249         if (colors != null){
250             Iterator cIt = colors.iterator();
251             while (cIt.hasNext()){
252                 ColorAttribute ca = (ColorAttribute)cIt.next();
253                 writerOut.println("<color r=\""+ca.red()+"\" g=\""+ca.green()+"\" b=\""+ca.blue()+"\" fg=\""+ca.fg()+"\" aType=\""+ca.analysisType()+"\"/>");
254             }
255         }
256         if (texts != null){
257             Iterator textsIt = texts.iterator();
258             while (textsIt.hasNext()){
259                 StringAttribute sa = (StringAttribute)textsIt.next();
260                 writerOut.println("<text info=\""+sa.info()+"\" aType=\""+sa.analysisType()+"\"/>");
261             }
262         }
263         if (links != null){
264             Iterator linksIt = links.iterator();
265             while (linksIt.hasNext()){
266                 LinkAttribute la = (LinkAttribute)linksIt.next();
267                 writerOut.println("<link label=\""+formatForXML(la.info())+"\" jmpLink=\""+la.jimpleLink()+"\" srcLink=\""+la.javaLink()+"\" clssNm=\""+la.className()+"\" aType=\""+la.analysisType()+"\"/>");
268             }
269         }
270         writerOut.println("</attribute>");
271     }
272 }
273
Popular Tags