KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > jicengine > builder > BuilderImpl


1 package org.jicengine.builder;
2 import javax.xml.parsers.*;
3 import org.xml.sax.SAXException JavaDoc;
4 import org.jicengine.*;
5 import java.io.File JavaDoc;
6 import java.io.IOException JavaDoc;
7 import java.util.Map JavaDoc;
8 import java.util.Iterator JavaDoc;
9 import org.jicengine.element.Element;
10 import org.jicengine.element.ElementException;
11 import org.jicengine.element.VariableElement;
12 import org.jicengine.element.ActionElement;
13 import org.jicengine.io.Resource;
14 import org.jicengine.io.UrlReadable;
15 import org.jicengine.operation.Context;
16 import org.jicengine.operation.SimpleContext;
17 /**
18  *
19  *
20  *
21  * <p>
22  * Copyright (C) 2004 Timo Laitinen
23  * </p>
24  *
25  * @author Timo Laitinen
26  * @created 2004-09-20
27  * @since JICE-0.10
28  *
29  */

30
31 public class BuilderImpl implements Builder {
32
33     private static boolean log = false;
34     //Boolean.valueOf(System.getProperty("org.jicengine.Builder.log", "false")).booleanValue();
35

36     public BuilderImpl() {
37     }
38
39     public Object JavaDoc build(Instructions instructions) throws IOException JavaDoc, SAXException JavaDoc, JICException
40     {
41
42         // parse
43
long begin = System.currentTimeMillis();
44         Element rootElement = parse(instructions);
45         long parseTime = System.currentTimeMillis() - begin;
46
47         // execute the elements and return the result.
48
begin = System.currentTimeMillis();
49         Object JavaDoc result = execute(rootElement, instructions);
50         long execTime = System.currentTimeMillis() - begin;
51
52         if( log ){
53             System.out.println("[" + instructions.getFile() + "]:");
54             System.out.println(" parse time : " + parseTime);
55             System.out.println(" exec time : " + execTime);
56             System.out.println(" total time : " + (parseTime+execTime));
57             System.out.println();
58         }
59
60         return result;
61     }
62
63     private SAXParser getSAXParser() throws JICException
64     {
65         try {
66             SAXParserFactory factory = SAXParserFactory.newInstance();
67             factory.setNamespaceAware(true);
68             factory.setFeature("http://xml.org/sax/features/namespaces", true);
69             factory.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
70             SAXParser parser = factory.newSAXParser();
71             return parser;
72         } catch (Exception JavaDoc e){
73             throw new JICException("Failed to obtain a SAX parser", e);
74         }
75     }
76
77     private Element parse(Instructions instructions) throws IOException JavaDoc, SAXException JavaDoc, JICException
78     {
79         SAXParser parser = getSAXParser();
80         Handler handler = new Handler();
81         //XMLReader
82

83         try {
84             parser.parse(instructions.getFile().getInputStream(), handler, getSystemIdentifier(instructions));
85             return handler.getResult();
86         } catch (org.xml.sax.SAXException JavaDoc e){
87             Exception JavaDoc cause = e.getException();
88             if( cause != null ){
89                 if( cause instanceof JICException ){
90                     throw (JICException) cause;
91                 }
92                 else {
93                     throw new JICException(cause);
94                 }
95             }
96             else {
97                 // a SAX exception without a nested exception. throw it as is.
98
throw e;
99             }
100         }
101     }
102
103   private static String JavaDoc getSystemIdentifier(Instructions instructions)
104   {
105     Resource resource = instructions.getFile();
106     String JavaDoc systemIdentifier;
107     if( resource instanceof UrlReadable){
108       try {
109         systemIdentifier = ((UrlReadable)resource).getUrl().toString();
110       } catch (IOException JavaDoc e){
111         System.out.println(new java.util.Date JavaDoc()+ " [JICE]: failed to create resource URL, using identifier '" + resource.getIdentifier() + "' as system identifier");
112         systemIdentifier = resource.getIdentifier();
113       }
114     }
115     else {
116       systemIdentifier = resource.getIdentifier();
117     }
118     
119     return systemIdentifier;
120   }
121   
122     private Object JavaDoc execute(Element rootElement, Instructions buildInstructions) throws ElementException
123     {
124         Context rootContext = new SimpleContext("//");
125         BuildContext jiceContext = new BuildContextImpl(buildInstructions);
126         rootContext.addObject(BuildContext.VARIABLE_NAME, jiceContext);
127
128         // reserve the variable names 'true' and 'false' to boolean values.
129
rootContext.addObject("true", Boolean.TRUE);
130         rootContext.addObject("false", Boolean.FALSE);
131
132         Map JavaDoc buildParameters = buildInstructions.getParameters();
133         for (Iterator JavaDoc it = buildParameters.keySet().iterator() ; it.hasNext();) {
134             String JavaDoc key = it.next().toString();
135             rootContext.addObject(org.jicengine.expression.BuildParameterParser.PREFIX + key, buildParameters.get(key));
136         }
137
138         if( rootElement.isExecuted(rootContext,null) ){
139
140             if (rootElement instanceof VariableElement) {
141                 return ((VariableElement) rootElement).getValue(rootContext, null);
142             } else if (rootElement instanceof ActionElement) {
143                 ((ActionElement) rootElement).execute(rootContext, null);
144                 return null;
145             } else {
146                 throw new IllegalStateException JavaDoc("unknown element type: " + rootElement);
147             }
148         }
149         else {
150             // element not executed. return null.
151
return null;
152         }
153     }
154
155     public static void main(String JavaDoc[] args) throws Exception JavaDoc
156     {
157         if( args.length == 0 ){
158             System.out.println("give the org.jicengine-file as an argument.");
159         }
160         else {
161             File JavaDoc file = new File JavaDoc(args[0]);
162             System.out.println("-------------------");
163             System.out.println("Processing file '" + file + "'");
164             System.out.println("-------------------");
165             java.util.Map JavaDoc parameters = new java.util.HashMap JavaDoc();
166             //parameters.put("language.code", "fi");
167
Object JavaDoc result = new BuilderImpl().build(new Instructions(new org.jicengine.io.FileResource(file),parameters));
168
169             System.out.println("-------------------");
170             System.out.println("Got object:");
171             System.out.println(" " + result);
172             System.out.println(" (" + result.getClass().getName() + ")");
173             System.out.println("-------------------");
174         }
175
176         /*
177         javax.swing.JFrame frame = new javax.swing.JFrame("Anne on taas kiva");
178         frame.setLocation(300,300);
179         frame.setSize(200,200);
180         frame.show();
181         */

182     }
183 }
184
Popular Tags