001: package fr.aliacom.form.swt;
002:
003: import java.io.IOException;
004: import java.io.InputStream;
005: import java.util.Iterator;
006:
007: import javax.xml.parsers.DocumentBuilder;
008: import javax.xml.parsers.DocumentBuilderFactory;
009: import javax.xml.parsers.ParserConfigurationException;
010:
011: import org.apache.log4j.Logger;
012: import org.w3c.dom.Document;
013: import org.xml.sax.SAXException;
014:
015: import fr.aliacom.form.common.FormContext;
016: import fr.aliacom.form.common.FormLoader;
017: import fr.aliacom.form.common.FormVariable;
018: import fr.aliacom.form.common.IForm;
019: import fr.aliacom.form.common.IFormComponent;
020: import fr.aliacom.form.common.IFormParser;
021: import fr.aliacom.form.common.ToolkitManager;
022:
023: /**
024: * @author tom
025: *
026: * (C) 2001, 2002 Thomas Cataldo
027: */
028: public final class SWTFormParser implements IFormParser {
029:
030: private BuilderFactory factory;
031: private static final Logger log = Logger
032: .getLogger(SWTFormParser.class);
033:
034: public SWTFormParser() {
035: factory = new BuilderFactory();
036: }
037:
038: private IForm parse(IForm form, InputStream is, FormContext ctx) {
039: IForm ret = null;
040: try {
041: Document doc;
042: doc = newParser().parse(is);
043: is.close();
044: SWTBuilder formBuilder = factory.getBuilder("form");
045: ret = (IForm) formBuilder.build(doc.getDocumentElement(),
046: form, new FormLoader(ctx), factory);
047: } catch (SAXException e) {
048: e.printStackTrace();
049: } catch (IOException e) {
050: e.printStackTrace();
051: } catch (ParserConfigurationException e) {
052: e.printStackTrace();
053: }
054: return ret;
055: }
056:
057: /**
058: * @see fr.aliacom.form.common.IFormParser#parse(IForm, String, FormContext)
059: */
060: public IForm parse(IForm form, String formName, FormContext ctx) {
061: InputStream is = ToolkitManager.getToolkit().loadForm(formName);
062: log.info("Parsing '" + formName + "', is=" + is);
063: IForm f = parse(form, is, ctx);
064: f.setContext(ctx);
065: return f;
066: }
067:
068: /**
069: * @see fr.aliacom.form.common.IFormParser#reset()
070: */
071: public void reset() {
072: }
073:
074: private DocumentBuilder newParser()
075: throws ParserConfigurationException {
076: return DocumentBuilderFactory.newInstance()
077: .newDocumentBuilder();
078: }
079:
080: /**
081: * @see fr.aliacom.form.common.IFormParser#parse(IForm, fr.aliacom.form.common.IFormComponent, java.lang.String, FormContext)
082: */
083: public IFormComponent parse(IForm form, IFormComponent parent,
084: String string, FormContext newVars) {
085: IFormComponent ret = null;
086: try {
087: FormContext ctx = (FormContext) form.getFormContext()
088: .clone();
089: Iterator it = newVars.getVariables();
090: while (it.hasNext()) {
091: ctx.addVariable((FormVariable) it.next());
092: }
093: log.debug("FormContext cloned.");
094:
095: /* Use a new form loader the value of the subform can be drop
096: * when the tab is removed (for example)
097: */
098: FormLoader loader = new FormLoader(ctx);
099: loader.setForm(form);
100: Document doc = newParser().parse(
101: ToolkitManager.getToolkit().loadForm(string));
102: SWTBuilder anyBuilder = factory.getBuilder(doc
103: .getDocumentElement().getNodeName());
104: ret = anyBuilder.build(doc.getDocumentElement(), parent,
105: loader, factory);
106: loader.load();
107: } catch (Exception e) {
108: e.printStackTrace();
109: }
110:
111: return ret;
112: }
113:
114: }
|