KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > JICEngine


1 package org.jicengine;
2 import org.jicengine.io.Resource;
3 import java.util.Map JavaDoc;
4 import org.xml.sax.SAXException JavaDoc;
5 import java.io.IOException JavaDoc;
6
7 /**
8  * <p>
9  * The main class. Provides:
10  * </p>
11  * <ul>
12  * <li>Provides methods for processing JIC files.</li>
13  * <li>Contains the <code>main</code> method for using JIC Engine from the command-line.</li>
14  * <li>Acts as a factory for obtaining <code>Builder</code> instances.</li>
15  * </ul>
16  *
17  * <p>
18  * Copyright (C) 2004 Timo Laitinen
19  * </p>
20  *
21  * @author Timo Laitinen
22  * @created 2004-09-20
23  * @since JICE-0.10
24  *
25  */

26
27 public class JICEngine {
28
29     private static Builder INSTANCE = new org.jicengine.builder.BuilderImpl();
30
31     /**
32      * Returns a Builder instance that is used for processing JIC files.
33      */

34     public static Builder getBuilder()
35     {
36         return INSTANCE;
37     }
38
39     private JICEngine()
40     {
41     }
42
43     /**
44      * <p>
45      * Processes a JIC file and returns the result. Use this for processing JIC
46      * files.
47      * </p>
48      *
49      * <p>
50      * An alternative way is to first obtain a <code>Builder</code> with
51      * <code>getBuilder</code> method and then use it to process a file.
52      * </p>
53      *
54      * <p>
55      * For details, see Builder class.
56      * </p>
57      *
58      * @see org.jicengine.Builder#build
59      */

60     public static Object JavaDoc build(Instructions instructions) throws IOException JavaDoc, SAXException JavaDoc, JICException
61     {
62         return getBuilder().build(instructions);
63     }
64
65     /**
66      * @param buildParameters may be null if there are no parameters.
67      */

68     public static Object JavaDoc build(Resource jicFile, Map JavaDoc buildParameters) throws IOException JavaDoc, SAXException JavaDoc, JICException
69     {
70         return build(new Instructions(jicFile, buildParameters));
71     }
72
73     public static Object JavaDoc build(Resource jicFile) throws IOException JavaDoc, SAXException JavaDoc, JICException
74     {
75         return build(jicFile, null);
76     }
77
78     /**
79      * <p>
80      * command-line interface for processing a jic-file.
81      * </p>
82      * <p>format:
83      * <code>java org.jicengine.JICE -jic [jic-file-path] [-param name1 value1] [-param name2 value2] ..</code>
84      * </p>
85      */

86     public static void main(String JavaDoc[] args) throws Exception JavaDoc
87     {
88         if( args.length == 0 ){
89             throw new IllegalArgumentException JavaDoc("Usage: java org.jicengine.JICE -jic jic-file [-param name1 value1] [-param name2 value2] ..");
90         }
91
92         String JavaDoc jicFile = null;
93         Map JavaDoc parameters = new java.util.HashMap JavaDoc();
94
95
96         for (int i = 0; i < args.length; i++) {
97             String JavaDoc arg = args[i];
98
99             if( arg.startsWith("-") ){
100                 // ok, this is what was expected
101
String JavaDoc name = arg.substring(1);
102
103                 if( name.equals("jic") ){
104                     i++;
105                     jicFile = getArgument(args, i, "expected '-jic' to be followed by a path.");
106                 }
107                 else if( name.equals("param") ){
108                     i++;
109                     String JavaDoc paramName = getArgument(args, i, "expected '-param' to be followed by the param name.");
110                     i++;
111                     String JavaDoc paramValue = getArgument(args, i, "expected '-param " + paramName + " to be followed by the param value.");
112                     parameters.put(paramName, paramValue);
113                 }
114                 else {
115                     throw new IllegalArgumentException JavaDoc("unknown argument: " + name);
116                 }
117             }
118             else {
119                 throw new IllegalArgumentException JavaDoc("expected an argument-name starting with '-', got '" + arg + "'");
120             }
121         }
122
123         if( jicFile == null ){
124             throw new IllegalArgumentException JavaDoc("argument -jic [jic-file] not specified.");
125         }
126
127         long now = System.currentTimeMillis();
128         Object JavaDoc result = build(new org.jicengine.io.FileResource(new java.io.File JavaDoc(jicFile)), parameters);
129         long elapsed = System.currentTimeMillis() - now;
130         System.out.println("[JICE]: '" + jicFile + "' processed in " + elapsed + " ms. result:");
131         System.out.println(" " + result);
132         if( result != null ){
133             System.out.println(" [" + result.getClass().getName() + "]");
134         }
135     }
136
137     private static String JavaDoc getArgument(String JavaDoc[] args, int index, String JavaDoc errorMessage)
138     {
139         try {
140             return args[index];
141         } catch (ArrayIndexOutOfBoundsException JavaDoc e){
142             throw new IllegalArgumentException JavaDoc(errorMessage);
143         }
144     }
145 }
146
Popular Tags