source: DCWoRMS/trunk/src/simulator/utils/XsltTransformations.java @ 490

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