KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tonbeller > jpivot > core > ModelFactory


1 /*
2  * ====================================================================
3  * This software is subject to the terms of the Common Public License
4  * Agreement, available at the following URL:
5  * http://www.opensource.org/licenses/cpl.html .
6  * Copyright (C) 2003-2004 TONBELLER AG.
7  * All Rights Reserved.
8  * You must accept the terms of that agreement to use this software.
9  * ====================================================================
10  *
11  *
12  */

13 package com.tonbeller.jpivot.core;
14
15 import java.io.IOException JavaDoc;
16 import java.net.URL JavaDoc;
17
18 import org.apache.commons.digester.Digester;
19 import org.xml.sax.InputSource JavaDoc;
20 import org.xml.sax.SAXException JavaDoc;
21
22
23
24 /**
25  * creates a model instance from an xml description. Example
26  * <pre>
27  * &lt;?xml version="1.0" encoding="utf-8"?&gt;
28  * &lt;model id="m1" class="com.tonbeller.jpivot.core.ModelSupport"&gt;
29  * &lt;extension id="f1" class="com.tonbeller.jpivot.core.ExtensionSupport"/&gt;
30  * &lt;extension id="f2" class="com.tonbeller.jpivot.core.ExtensionSupport"/&gt;
31  * &lt;/model&gt;
32  * </pre>
33  * @author av
34  */

35
36 public class ModelFactory {
37
38   /**
39    * singleton
40    */

41   private ModelFactory() {
42   }
43   
44   /**
45    * not for external use. Has to be public for the commons digester
46    * to access it.
47    */

48   public static class ModelHolder {
49     private Model model;
50     public void setModel(Model model) {
51       this.model = model;
52     }
53     public Model getModel() {
54       return model;
55     }
56   }
57     
58   /**
59    * creates a model from an xml configuration file
60    * @param url url of model configuration file
61    * @return Model
62    * @throws SAXException
63    * @throws IOException
64    */

65   public static Model instance(URL JavaDoc url) throws SAXException JavaDoc, IOException JavaDoc {
66     Digester digester = new Digester();
67     digester.setValidating(false);
68
69     ModelHolder root = new ModelHolder();
70     digester.push(root);
71
72     digester.addObjectCreate("model", "missing \"class\" attribute", "class");
73     digester.addSetProperties("model");
74     digester.addSetNext("model", "setModel");
75
76     digester.addObjectCreate("model/extension", "missing \"class\" attribute", "class");
77     digester.addSetProperties("model/extension");
78     digester.addSetNext("model/extension", "addExtension");
79  
80     InputSource JavaDoc is = new InputSource JavaDoc(url.toExternalForm());
81     digester.parse(is);
82     return root.getModel();
83   }
84   
85
86 }
87
Popular Tags