01: package fr.aliacom.form.swt.maker;
02:
03: import java.io.IOException;
04: import java.io.InputStream;
05:
06: import javax.xml.parsers.DocumentBuilder;
07: import javax.xml.parsers.DocumentBuilderFactory;
08: import javax.xml.parsers.FactoryConfigurationError;
09: import javax.xml.parsers.ParserConfigurationException;
10:
11: import org.w3c.dom.Document;
12: import org.w3c.dom.Element;
13: import org.xml.sax.SAXException;
14:
15: import fr.aliacom.form.common.IFormComponent;
16: import fr.aliacom.form.common.FormLoader;
17: import fr.aliacom.form.common.ToolkitManager;
18: import fr.aliacom.form.swt.BuilderFactory;
19: import fr.aliacom.form.swt.SWTBuilder;
20:
21: /**
22: * @author tom
23: *
24: * (C) 2001, 2002 Thomas Cataldo
25: */
26: public final class IncludeBuilder extends SWTBuilder {
27:
28: /**
29: * @see fr.aliacom.form.swt.SWTBuilder#build(Element, IFormComponent, FormLoader, BuilderFactory)
30: */
31: public IFormComponent build(Element elem, IFormComponent parent,
32: FormLoader loader, BuilderFactory factory) {
33: String name = elem.getAttribute("name");
34: log.debug("Including '" + name + "'");
35: InputStream is = ToolkitManager.getToolkit().loadForm(name);
36: Element root = null;
37: try {
38: DocumentBuilder builder = newParser();
39: Document doc = builder.parse(is);
40: root = doc.getDocumentElement();
41: } catch (SAXException e) {
42: e.printStackTrace();
43: } catch (IOException e) {
44: e.printStackTrace();
45: } finally {
46: try {
47: is.close();
48: } catch (IOException e1) {
49: e1.printStackTrace();
50: }
51: }
52: SWTBuilder rootBuilder = factory.getBuilder(root.getNodeName());
53: IFormComponent ret = rootBuilder.build(root, parent, loader,
54: factory);
55: if (root.hasAttribute("id")) {
56: loader.getCtx().addVariable(root.getAttribute("id"), ret);
57: }
58: return ret;
59: }
60:
61: private DocumentBuilder newParser() {
62: DocumentBuilder ret = null;
63: try {
64: ret = DocumentBuilderFactory.newInstance()
65: .newDocumentBuilder();
66: } catch (ParserConfigurationException e) {
67: e.printStackTrace();
68: } catch (FactoryConfigurationError e) {
69: e.printStackTrace();
70: }
71: return ret;
72: }
73:
74: }
|