source: xssim/trunk/src/simulator/utils/XsltTransformations.java @ 104

Revision 104, 6.6 KB checked in by wojtekp, 13 years ago (diff)
  • Property svn:mime-type set to text/plain
Line 
1package simulator.utils;
2
3import gssim.schedframe.scheduling.utils.JobDescription;
4import gssim.schedframe.scheduling.utils.TaskDescription;
5
6import java.io.IOException;
7import java.io.Reader;
8import java.io.StringReader;
9import java.io.StringWriter;
10import java.io.Writer;
11import java.util.Properties;
12
13import javax.xml.parsers.ParserConfigurationException;
14import javax.xml.transform.Templates;
15import javax.xml.transform.Transformer;
16import javax.xml.transform.TransformerConfigurationException;
17import javax.xml.transform.TransformerException;
18import javax.xml.transform.TransformerFactory;
19import javax.xml.transform.stream.StreamResult;
20import javax.xml.transform.stream.StreamSource;
21import javax.xml.xpath.XPathExpressionException;
22
23/**
24 *
25 * @author Marcin Krystek
26 *
27 */
28public class XsltTransformations {
29       
30        public static String INSTALLDIR = null;
31       
32        static {
33                String key = "javax.xml.transform.TransformerFactory";
34                String value =
35                        //"org.apache.xalan.xsltc.trax.TransformerFactoryImpl";
36                        "org.apache.xalan.processor.TransformerFactoryImpl";
37                Properties props = System.getProperties();
38                props.put(key, value);
39                System.setProperties(props);
40               
41                INSTALLDIR = System.getProperty("gssim.install.dir");
42                if(INSTALLDIR != null)
43                        INSTALLDIR = INSTALLDIR+"/";
44                else
45                        INSTALLDIR = "";
46        }
47       
48       
49        public XsltTransformations() throws ParserConfigurationException, XPathExpressionException{
50        }
51       
52        protected enum Transformers {
53                EXTEND_JOB_DESC(INSTALLDIR+"simulator/xslt/ext_job_desc.xslt"),
54                TASK2RESOURCE_REQUIREMENTS(INSTALLDIR+"simulator/xslt/task2resreq.xslt"),
55                RESOURCE_REQUIREMENTS2RTG(INSTALLDIR+"simulator/xslt/resreq2rtg.xslt"),
56                RTG2HOST_PARAMETERS(INSTALLDIR+"simulator/xslt/rtg2host_params.xslt"),
57                HOST_PARAMETERS2RTG(INSTALLDIR+"simulator/xslt/host_params2rtg.xslt"),
58                RTG2SCHEDULIING_PLAN(INSTALLDIR+"simulator/xslt/schedulingPlan.xslt"),
59                EXTRACT_JOB_ID(INSTALLDIR+"simulator/xslt/extract_job_id.xslt"),
60                JOB_POSTPROCESSING(INSTALLDIR+"simulator/xslt/job_postprocessing.xslt");
61               
62                private String fileName;
63                private Transformer transformer;
64
65                private Transformers(String arg){
66                        this.fileName = arg;
67                        this.transformer = null;
68                }
69               
70                public Transformer geTransformer() throws TransformerConfigurationException{
71                        if(this.transformer == null){
72                                TransformerFactory tFactory = TransformerFactory.newInstance();
73                                StreamSource ss = new StreamSource(this.fileName);
74                                Templates translet = tFactory.newTemplates(ss);
75                                this.transformer = translet.newTransformer();
76                        }
77                       
78                        return this.transformer;
79                }
80        }
81       
82        public String extendJobDescription(String jobDesc){
83                Transformer t = null;
84                try {
85                        t = Transformers.EXTEND_JOB_DESC.geTransformer();
86                        return transform(t, jobDesc);
87                } catch (TransformerConfigurationException e) {
88                        e.printStackTrace();
89                }
90                return null;
91        }
92       
93        public String taskToResourceRequirements(String task, String jobId, String userDN, org.joda.time.DateTime submitionTime){
94                Transformer t = null;
95                try {
96                        t = Transformers.TASK2RESOURCE_REQUIREMENTS.geTransformer();
97                        t.clearParameters();
98                        t.setParameter("JOB_ID", jobId);
99                        t.setParameter("USER_DN", userDN);
100                       
101                        org.exolab.castor.types.DateTime dt = new org.exolab.castor.types.DateTime(submitionTime.getMillis());
102                        short milis = 0;
103                        dt.setMilliSecond(milis);
104                        t.setParameter("SUBMISSION_TIME", dt);
105                       
106                        return transform(t, task);
107                       
108                } catch (TransformerConfigurationException e) {
109                        e.printStackTrace();
110                }
111                return null;
112        }
113       
114        public String resourceRequirementsToRTG(String resReq){
115                Transformer t = null;
116                try {
117                        t = Transformers.RESOURCE_REQUIREMENTS2RTG.geTransformer();
118                        return transform(t, resReq);
119                } catch (TransformerConfigurationException e) {
120                        e.printStackTrace();
121                }
122                return null;
123        }
124       
125        public String RTGToHostParameters(String rtg){
126                Transformer t = null;
127                try {
128                        t = Transformers.RTG2HOST_PARAMETERS.geTransformer();
129                        return transform(t, rtg);
130                } catch (TransformerConfigurationException e) {
131                        e.printStackTrace();
132                }
133                return null;
134        }
135       
136        public String hostParametersToRTG(String hostParameters){
137                Transformer t = null;
138                try {
139                        t = Transformers.HOST_PARAMETERS2RTG.geTransformer();
140                        return transform(t, hostParameters);
141                } catch (TransformerConfigurationException e) {
142                        e.printStackTrace();
143                }
144                return null;
145        }
146       
147        public String RTGToSchedulingPlan(String rtg){
148                Transformer t = null;
149                try {
150                        t = Transformers.RTG2SCHEDULIING_PLAN.geTransformer();
151                        return transform(t, rtg);
152                } catch (TransformerConfigurationException e) {
153                        e.printStackTrace();
154                }
155                return null;
156        }
157       
158        public JobDescription splitJobToTasks(String xmlJob) throws Exception{
159
160                int begin = 0;
161                int end = 0;
162               
163                begin = xmlJob.indexOf("appId=") + 7;
164                end = xmlJob.indexOf("\"", begin);
165                String jobId = xmlJob.substring(begin, end);
166               
167                JobDescription jobDesc = new JobDescription(xmlJob);
168            jobDesc.setJobId(jobId);
169               
170                begin = 0;
171                end = 0;
172                int tb = 0;
173                int te = 0;
174                String taskId;
175                while((begin = xmlJob.indexOf("<task", end)) != -1){
176                        end = xmlJob.indexOf("</task>", begin);
177                        end += 7;
178                        String task = xmlJob.substring(begin, end);
179                        tb = task.indexOf("taskId=") + 8;
180                        te = task.indexOf("\"", tb);
181                        taskId = task.substring(tb, te);
182                       
183                       
184                        TaskDescription taskDescription = new TaskDescription(task);
185                taskDescription.setTaskId(taskId);
186                jobDesc.add(taskDescription);
187                }
188               
189                return jobDesc;
190        }
191       
192        public String extractJobId(String job){
193                Transformer t = null;
194               
195                try {
196               
197                        t = Transformers.EXTRACT_JOB_ID.geTransformer();
198                        return transform(t, job);
199                       
200                } catch (TransformerConfigurationException e) {
201                        e.printStackTrace();
202                }
203               
204                return null;
205        }
206       
207        public String jobPostProcessing(String job, String taskId, String executionDuration){
208                Transformer t = null;
209               
210                try {
211               
212                        t = Transformers.JOB_POSTPROCESSING.geTransformer();
213                        t.setParameter("TASK_ID", taskId);
214                        t.setParameter("EXEC_DURATION_VALUE", executionDuration);
215                       
216                        return transform(t, job);
217                       
218                } catch (TransformerConfigurationException e) {
219                        e.printStackTrace();
220                }
221               
222                return null;
223        }
224       
225        protected String transform(Transformer t, String input){
226                Writer writer = new StringWriter();
227                Reader reader = new StringReader(input);
228               
229                try {
230                        writer = new StringWriter();
231                        reader = new StringReader(input);
232                       
233                        t.transform(new StreamSource(reader), new StreamResult(writer));
234                       
235
236                        return writer.toString();
237                } catch (TransformerConfigurationException e) {
238                        e.printStackTrace();
239                } catch (TransformerException e) {
240                        e.printStackTrace();
241                } finally {
242                        try {
243                                writer.close();
244                                reader.close();
245                        } catch (IOException e) {
246                                e.printStackTrace();
247                        }
248                       
249                }
250                return null;
251        }
252}
Note: See TracBrowser for help on using the repository browser.