001: package org.jicengine;
002:
003: import org.jicengine.io.Resource;
004: import java.util.Map;
005: import org.xml.sax.SAXException;
006: import java.io.IOException;
007:
008: /**
009: * <p>
010: * The main class. Provides:
011: * </p>
012: * <ul>
013: * <li>Provides methods for processing JIC files.</li>
014: * <li>Contains the <code>main</code> method for using JIC Engine from the command-line.</li>
015: * <li>Acts as a factory for obtaining <code>Builder</code> instances.</li>
016: * </ul>
017: *
018: * <p>
019: * Copyright (C) 2004 Timo Laitinen
020: * </p>
021: *
022: * @author Timo Laitinen
023: * @created 2004-09-20
024: * @since JICE-0.10
025: *
026: */
027:
028: public class JICEngine {
029:
030: private static Builder INSTANCE = new org.jicengine.builder.BuilderImpl();
031:
032: /**
033: * Returns a Builder instance that is used for processing JIC files.
034: */
035: public static Builder getBuilder() {
036: return INSTANCE;
037: }
038:
039: private JICEngine() {
040: }
041:
042: /**
043: * <p>
044: * Processes a JIC file and returns the result. Use this for processing JIC
045: * files.
046: * </p>
047: *
048: * <p>
049: * An alternative way is to first obtain a <code>Builder</code> with
050: * <code>getBuilder</code> method and then use it to process a file.
051: * </p>
052: *
053: * <p>
054: * For details, see Builder class.
055: * </p>
056: *
057: * @see org.jicengine.Builder#build
058: */
059: public static Object build(Instructions instructions)
060: throws IOException, SAXException, JICException {
061: return getBuilder().build(instructions);
062: }
063:
064: /**
065: * @param buildParameters may be null if there are no parameters.
066: */
067: public static Object build(Resource jicFile, Map buildParameters)
068: throws IOException, SAXException, JICException {
069: return build(new Instructions(jicFile, buildParameters));
070: }
071:
072: public static Object build(Resource jicFile) throws IOException,
073: SAXException, JICException {
074: return build(jicFile, null);
075: }
076:
077: /**
078: * <p>
079: * command-line interface for processing a jic-file.
080: * </p>
081: * <p>format:
082: * <code>java org.jicengine.JICE -jic [jic-file-path] [-param name1 value1] [-param name2 value2] ..</code>
083: * </p>
084: */
085: public static void main(String[] args) throws Exception {
086: if (args.length == 0) {
087: throw new IllegalArgumentException(
088: "Usage: java org.jicengine.JICE -jic jic-file [-param name1 value1] [-param name2 value2] ..");
089: }
090:
091: String jicFile = null;
092: Map parameters = new java.util.HashMap();
093:
094: for (int i = 0; i < args.length; i++) {
095: String arg = args[i];
096:
097: if (arg.startsWith("-")) {
098: // ok, this is what was expected
099: String name = arg.substring(1);
100:
101: if (name.equals("jic")) {
102: i++;
103: jicFile = getArgument(args, i,
104: "expected '-jic' to be followed by a path.");
105: } else if (name.equals("param")) {
106: i++;
107: String paramName = getArgument(args, i,
108: "expected '-param' to be followed by the param name.");
109: i++;
110: String paramValue = getArgument(
111: args,
112: i,
113: "expected '-param "
114: + paramName
115: + " to be followed by the param value.");
116: parameters.put(paramName, paramValue);
117: } else {
118: throw new IllegalArgumentException(
119: "unknown argument: " + name);
120: }
121: } else {
122: throw new IllegalArgumentException(
123: "expected an argument-name starting with '-', got '"
124: + arg + "'");
125: }
126: }
127:
128: if (jicFile == null) {
129: throw new IllegalArgumentException(
130: "argument -jic [jic-file] not specified.");
131: }
132:
133: long now = System.currentTimeMillis();
134: Object result = build(new org.jicengine.io.FileResource(
135: new java.io.File(jicFile)), parameters);
136: long elapsed = System.currentTimeMillis() - now;
137: System.out.println("[JICE]: '" + jicFile + "' processed in "
138: + elapsed + " ms. result:");
139: System.out.println(" " + result);
140: if (result != null) {
141: System.out.println(" [" + result.getClass().getName()
142: + "]");
143: }
144: }
145:
146: private static String getArgument(String[] args, int index,
147: String errorMessage) {
148: try {
149: return args[index];
150: } catch (ArrayIndexOutOfBoundsException e) {
151: throw new IllegalArgumentException(errorMessage);
152: }
153: }
154: }
|