KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > soot > XMLAttributesPrinter


1 /* Soot - a J*va Optimization Framework
2  * Copyright (C) 2003 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;
21
22 import soot.tagkit.*;
23 import soot.util.*;
24 import soot.xml.*;
25 import java.util.*;
26 import java.io.*;
27
28 public class XMLAttributesPrinter {
29
30     
31     private String JavaDoc inFilename;
32     private String JavaDoc useFilename;
33     private SootClass sootClass;
34     private String JavaDoc outputDir;
35     private ArrayList attributes;
36     
37     private void setOutputDir(String JavaDoc dir) {
38         outputDir = dir;
39     }
40
41     private String JavaDoc getOutputDir() {
42         return outputDir;
43     }
44     
45     public XMLAttributesPrinter(String JavaDoc filename, String JavaDoc outputDir) {
46         setInFilename(filename);
47         setOutputDir(outputDir);
48         initAttributesDir();
49         createUseFilename();
50         initFile();
51     }
52
53     private void initFile() {
54         attributes = new ArrayList();
55         try {
56           streamOut = new FileOutputStream(getUseFilename());
57           writerOut = new PrintWriter(new OutputStreamWriter(streamOut));
58           writerOut.println("<?xml version=\"1.0\" encoding=\"ISO-8859-1\"?>");
59           writerOut.println("<attributes>");
60         }
61         catch(IOException e1) {
62           G.v().out.println(e1.getMessage());
63         }
64                                     
65     }
66
67     private void finishFile() {
68           writerOut.println("</attributes>");
69           writerOut.close();
70     }
71     
72     public void printAttrs(SootClass c, soot.xml.TagCollector tc) {
73     
74         tc.collectKeyTags(c);
75         Iterator fIt = c.getFields().iterator();
76         while (fIt.hasNext()){
77             SootField sf = (SootField)fIt.next();
78             tc.collectFieldTags(sf);
79         }
80         Iterator mIt = c.getMethods().iterator();
81         while (mIt.hasNext()){
82             SootMethod sm = (SootMethod)mIt.next();
83             tc.collectMethodTags(sm);
84         }
85         tc.printTags(writerOut);
86         tc.printKeys(writerOut);
87         finishFile();
88     }
89     
90     public void printAttrs(SootClass c) {
91     
92         soot.xml.TagCollector tc = new soot.xml.TagCollector();
93         tc.collectKeyTags(c);
94         tc.collectTags(c);
95         tc.printTags(writerOut);
96         tc.printKeys(writerOut);
97         finishFile();
98     }
99
100      
101     FileOutputStream streamOut = null;
102     PrintWriter writerOut = null;
103     
104     private int getJavaLnOfHost(Host h){
105         Iterator it = h.getTags().iterator();
106         while (it.hasNext()){
107             Tag t = (Tag)it.next();
108             //G.v().out.println(t.getClass().toString());
109
if (t instanceof SourceLnPosTag) {
110                 //G.v().out.println("t is LineNumberTag");
111
return ((SourceLnPosTag)t).startLn();
112             }
113             else if (t instanceof LineNumberTag){
114                 return (new Integer JavaDoc(((LineNumberTag)t).toString())).intValue();
115             }
116         }
117         return 0;
118     }
119     
120     private int getJimpleLnOfHost(Host h){
121         Iterator it = h.getTags().iterator();
122         while (it.hasNext()){
123             Tag t = (Tag)it.next();
124             if (t instanceof JimpleLineNumberTag) {
125                 return ((JimpleLineNumberTag)t).getStartLineNumber();
126             }
127         }
128         return 0;
129     }
130
131
132     private void initAttributesDir() {
133     
134         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
135         String JavaDoc attrDir = "attributes";
136         
137         sb.append(getOutputDir());
138         sb.append(System.getProperty("file.separator"));
139         sb.append(attrDir);
140         
141         File dir = new File(sb.toString());
142
143         if (!dir.exists()) {
144             try {
145                 dir.mkdirs();
146             }
147             catch (SecurityException JavaDoc se) {
148                     G.v().out.println("Unable to create " + attrDir);
149                         //System.exit(0);
150
}
151         }
152                 
153     }
154
155     private String JavaDoc formatForXML(String JavaDoc in) {
156         in = StringTools.replaceAll(in, "<", "&lt;");
157         in = StringTools.replaceAll(in, ">", "&gt;");
158         in = StringTools.replaceAll(in, "&", "&amp;");
159         return in;
160     }
161
162     private void createUseFilename() {
163         String JavaDoc tmp = getInFilename();
164         //G.v().out.println("attribute file name: "+tmp);
165
tmp = tmp.substring(0, tmp.lastIndexOf('.'));
166         int slash = tmp.lastIndexOf(System.getProperty("file.separator"));
167         if (slash != -1) {
168             tmp = tmp.substring((slash+1), tmp.length());
169         }
170     
171         StringBuffer JavaDoc sb = new StringBuffer JavaDoc();
172         String JavaDoc attrDir = "attributes";
173         sb.append(getOutputDir());
174         sb.append(System.getProperty("file.separator"));
175         sb.append(attrDir);
176         sb.append(System.getProperty("file.separator"));
177         sb.append(tmp);
178         sb.append(".xml");
179         //tmp = sb.toString()+tmp+".xml";
180
setUseFilename(sb.toString());
181     }
182
183     private void setInFilename(String JavaDoc file) {
184         useFilename = file;
185     }
186
187     private String JavaDoc getInFilename() {
188         return useFilename;
189     }
190
191     private void setUseFilename(String JavaDoc file) {
192         useFilename = file;
193     }
194
195     private String JavaDoc getUseFilename() {
196         return useFilename;
197     }
198         
199     
200 }
201
Popular Tags