KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jical > ShellRun


1 package org.jical;
2 import java.io.ByteArrayInputStream JavaDoc;
3 import java.io.File JavaDoc;
4 import java.io.FileInputStream JavaDoc;
5 import java.io.InputStream JavaDoc;
6
7 import javax.xml.transform.Result JavaDoc;
8 import javax.xml.transform.Source JavaDoc;
9 import javax.xml.transform.Transformer JavaDoc;
10 import javax.xml.transform.TransformerFactory JavaDoc;
11 import javax.xml.transform.stream.StreamResult JavaDoc;
12 import javax.xml.transform.stream.StreamSource JavaDoc;
13 /*
14  * Version 1.2
15  * Provides extra XSLT capabilities.
16  *
17  */

18 public class ShellRun {
19     /** Version number. */
20     public static final String JavaDoc VERSION = "1.4";
21     
22     public static void main(String JavaDoc args[]) {
23         /*
24          * The idea here is to provide user who are stuck on the command line and have no access to jboss with
25          * a way to run the JiCal options from CRON. Not really adviseable as it will incur a penalty of starting
26          * java each time but...
27          *
28          */

29         if (args.length == 0
30         || args[0].equalsIgnoreCase("-help")) {
31             System.out.println(
32             "================================================================================\n"+
33             "JICAL v"+VERSION+" SHELL INTERFACE. GDAY - GOOD START! YOU FOUND ME!\n"+
34             "\n"+
35             "Your Options:\n"+
36             " -ical2fburl <calendarLocation> <fburl> <Organiser> <Email> <DaysForward>\n"+
37             " -ical2jicalxml <calendarLocation> <Organiser> <Email> <DaysForward>\n"+
38             " -ical2jicaldisplayxml <calendarLocation> <Organiser> <Email> <DaysForward> <timerange>\n"+
39             " -xslt <calendarLocation> <Organiser> <Email> <xsl document> <DaysForward>\n"+
40             " -xsltonly <xsl document> <xml document>\n"+
41             "\n"+
42             "Parameters explained:\n"+
43             "<calendarLocation> - your iCal location\n"+
44             " eg /home/sfg/evolution/local/Calendar/calendar.ics\n"+
45             "<fburl> - the internet URL for this FBURL before the username.ifb file\n"+
46             " eg http://www.eurekait.com/\n"+
47             "<Organiser> - this is used to name the free busy file. (sfg.ifb)\n"+
48             " eg sfg\n"+
49             "<Email> - the organisers email address\n"+
50             " eg sfg@eurekait.com\n"+
51             "<DaysForwardOrDateRange> - how far into the future do we project repeat events\n"+
52             " eg1: 180\n"+
53             " eg2: 20021215153500-20021215164500 Date formats are reverse ccyyMMddHHMMss\n"+
54             " in the form fromdate-todate with the dash showing the start of the new date.\n"+
55             "<timerange> - If required, will only show events that fall within the time range\n"+
56             " eg: 0730-1800 for 7:30am to 6:00pm \n"+
57             "<xsl document> - The Stylesheet to translate the XML to something else\n"+
58             " eg HTML\n"+
59             "<xml document> - The xml to translate with the Stylesheet.\n"+
60             "\n"+
61             "Learn by example:\n"+
62             "java -jar jical.jar -ical2fburl /home/sfg/evolution/local/Calendar/calendar.ics http://www.eurekait.com/ sfg sfg@eurekait.com 180 > /www/website.live/sfg.ifb\n"+
63             "java -jar jical.jar -ical2jicalxml /home/sfg/evolution/local/Calendar/calendar.ics sfg sfg@eurekait.com 180 > /www/website.live/sfg.xml\n"+
64             "java -jar jical.jar -xslt /home/sfg/evolution/local/Calendar/calendar.ics sfg sfg@eurekait.com xsl/htmllist.xsl 180 > /www/website.live/sfg-yag.html\n"+
65             "java -jar jical.jar -xsltonly fred.xsl jicaldisplayonyl.xml > /www/website.live/sfg.svg\n"+
66             "java -jar jical.jar -ical2jicaldisplayxml /home/sfg/evolution/local/Calendar/calendar.ics sfg sfg@eurekait.com 180 0630-2230 > /www/website.live/jicaldisplay.xml\n"+
67             "================================================================================");
68         }
69         else if (args[0].equalsIgnoreCase("-ical2fburl")) {
70             /* We will be processing as per jboss run but for a single entry. */
71             if (args.length != 6) {
72                 System.err.println(
73                 "Syntax is: -ical2fburl <calendarLocation> <FBURL> <Organiser> <Email> <DaysForwardOrDateRange>\n"+
74                 "Example:\n"+
75                 "java -jar jical.jar -ical2FBURL /home/sfg/evolution/local/Calendar/calendar.ics http://www.eurekait.com/ sfg sfg@eurekait.com 180 > /www/website.live/sfg.ifb");
76                 return;
77             }
78             ICalendarParser icp = new ICalendarParser();
79             ICalendar ical = icp.parse(args[1]);
80             ical.setFBUrl(args[2]);
81             ical.setOrganizer(args[3]);
82             ical.setOrganizerEmail(args[4]);
83             //int DaysForwardOrDateRange = java.lang.Integer.parseInt(args[5]);
84
//System.out.println(ical.getFBString(DaysForwardOrDateRange));
85
try {
86                 System.out.println(ical.getFBString(args[5]));
87             }
88             catch (Exception JavaDoc e) {
89                 System.err.println(e);
90                 e.printStackTrace();
91             }
92         }
93         else if (args[0].equalsIgnoreCase("-ical2jicalxml")) {
94             /* We will be processing as per jboss run but for a single entry. */
95             if (args.length < 5) {
96                 System.err.println(
97                 "Syntax is: -ical2jicalxml <calendarLocation> <Organiser> <Email> <DaysForwardOrDateRange>\n"+
98                 "Example:\n"+
99                 "java -jar jical.jar -ical2jicalxml /home/sfg/evolution/local/Calendar/calendar.ics sfg sfg@eurekait.com 180 0630-2000 > /www/website.live/sfg.xml");
100                 return;
101             }
102             ICalendarParser icp = new ICalendarParser();
103             ICalendar ical = icp.parse(args[1]);
104             ical.setOrganizer(args[2]);
105             ical.setOrganizerEmail(args[3]);
106             //int DaysForwardOrDateRange = java.lang.Integer.parseInt(args[4]);
107
//System.out.println(ical.getJiCalXML(DaysForwardOrDateRange));
108
try {
109                 if (args.length == 5)
110                     System.out.println(ical.getJiCalXML(args[4], null ));
111                 else
112                     System.out.println(ical.getJiCalXML(args[4], args[5]));
113             }
114             catch (Exception JavaDoc e) {
115                 System.err.println(e);
116                 e.printStackTrace();
117             }
118         }
119         else if (args[0].equalsIgnoreCase("-ical2jicaldisplayxml")) {
120             /* This option should make it easier to generate calendars of peoples dairies for two reasons..
121                Every day is included, so spacing empty days requires no programming in XSLT.
122                Ugly events that span days are shown as seperate events per day. ie 7 day event is 7 single daily events.
123                Repeat events are broken into their individual events. */

124             if (args.length < 5) {
125                 System.err.println(
126                 "Syntax is: -ical2jicaldisplayxml <calendarLocation> <Organiser> <Email> <DaysForwardOrDateRange> <timerange>\n"+
127                 "Example:\n"+
128                 "java -jar jical.jar -ical2jicaldisplayxml /home/sfg/evolution/local/Calendar/calendar.ics sfg sfg@eurekait.com 180 0630-1800 > /www/website.live/sfg-jicaldisplay.xml\n"+
129                 "Note - timerange is optional.");
130                 return;
131             }
132
133             ICalendarParser icp = new ICalendarParser();
134             ICalendar ical = icp.parse(args[1]);
135             ical.setOrganizer(args[2]);
136             ical.setOrganizerEmail(args[3]);
137             //int DaysForwardOrDateRange = java.lang.Integer.parseInt(args[4]);
138

139             try {
140                 if (args.length == 5)
141                     System.out.println(ical.getJiCaldisplayXML(args[4], null ));
142                 else
143                     System.out.println(ical.getJiCaldisplayXML(args[4], args[5]));
144                 
145             }
146             catch (Exception JavaDoc e) {
147                 System.err.println(e);
148                 e.printStackTrace();
149             }
150         }
151         else if (args[0].equalsIgnoreCase("-xslt")) {
152             /* Use XALAN to transform an iCal to XML then via a given Style sheet to a new format. */
153             if (args.length != 6) {
154                 System.err.println(
155                 "Syntax is: -xslt <calendarLocation> <Organiser> <Email> <DaysForwardOrDateRange> <xsl document>\n"+
156                 "Example:\n"+
157                 "java -jar jical.jar -xslt /home/sfg/evolution/local/Calendar/calendar.ics sfg sfg@eurekait.com 180 xsl/yag.xsl > /www/website.live/sfg-yag.html");
158                 return;
159             }
160             ICalendarParser icp = new ICalendarParser();
161             ICalendar ical = icp.parse(args[1]);
162             ical.setOrganizer(args[2]);
163             ical.setOrganizerEmail(args[3]);
164             //int DaysForwardOrDateRange = java.lang.Integer.parseInt(args[5]);
165
//String xmlCalString = ical.getJiCalXML(DaysForwardOrDateRange);
166
String JavaDoc xmlCalString = null;
167             try {
168                 xmlCalString = ical.getJiCalXML(args[5], null);
169             }
170             catch (Exception JavaDoc e) {
171                 System.err.println(e);
172                 e.printStackTrace();
173             }
174             // Now translate..
175
try {
176                 
177                 // 1. Instantiate a TransformerFactory.
178
javax.xml.transform.TransformerFactory JavaDoc tFactory =
179                 javax.xml.transform.TransformerFactory.newInstance();
180                 
181                 // 2. Use the TransformerFactory to process the stylesheet Source and
182
// generate a Transformer.
183

184                 javax.xml.transform.Transformer JavaDoc transformer = tFactory.newTransformer
185                 (new javax.xml.transform.stream.StreamSource JavaDoc(args[4])); //XSL File Name
186

187                 // 3. Use the Transformer to transform an XML Source and send the
188
// output to a Result object.
189
ByteArrayInputStream JavaDoc bais = new ByteArrayInputStream JavaDoc(xmlCalString.getBytes());
190                 transformer.transform
191                 (new javax.xml.transform.stream.StreamSource JavaDoc(bais), // "foo.xml"
192
//new javax.xml.transform.stream.StreamResult ( new java.io.FileOutputStream("foo.out"))
193
new javax.xml.transform.stream.StreamResult JavaDoc(System.out)
194                 );
195             }
196             catch (Exception JavaDoc e) {
197                 System.err.println("JiCal -xsl error in translation: " + e);
198                 e.printStackTrace();
199             }
200         }
201         else if (args[0].equalsIgnoreCase("-xsltonly")) {
202             /* Use XALAN to do a XSLT. This is just so its easy. */
203             if (args.length != 3) {
204                 System.err.println(
205                 "Syntax is: -xsltonly <xsl document> <xml input document>\n"+
206                 "Example:\n"+
207                 "java -jar jical.jar -xslt /www/website.live/jicaldisplay.xml xsl/svg.xsl > /www/website.live/sfg-yag.svg");
208                 return;
209             }
210             File JavaDoc styleFile = new File JavaDoc(args[1]); // stylesheet
211
File JavaDoc dataFile = new File JavaDoc(args[2]); // data
212
try {
213                 InputStream JavaDoc dataStream = new FileInputStream JavaDoc(dataFile);
214                 InputStream JavaDoc styleStream = new FileInputStream JavaDoc(styleFile);
215                 // create XSLT Source and Result objects
216
Source JavaDoc data = new StreamSource JavaDoc(dataStream);
217                 Source JavaDoc style = new StreamSource JavaDoc(styleStream);
218                 Result JavaDoc output = new StreamResult JavaDoc(System.out);
219                 // create Transformer and perform the tranfomation
220
Transformer JavaDoc xslt = TransformerFactory.newInstance().newTransformer(style);
221                 xslt.transform(data, output);
222             }
223             catch (Exception JavaDoc e) {
224                 System.err.println("JiCal -xsltonly error in translation: " + e);
225                 e.printStackTrace();
226             }
227         }
228         else {
229             System.out.println(
230             "================================================================================\n"+
231             "JICAL v"+VERSION+" SHELL INTERFACE. GDAY - GOOD START! YOU FOUND ME!\n"+
232             "Unfortunately, you got the option wrong. try -help for a list\n"+
233             "================================================================================");
234         }
235     }
236 }
237
238
Popular Tags