KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > objectweb > celtix > configuration > impl > ConfigurationMetadataBuilder


1 package org.objectweb.celtix.configuration.impl;
2
3 import java.io.IOException JavaDoc;
4 import java.io.InputStream JavaDoc;
5 import java.util.logging.Level JavaDoc;
6 import java.util.logging.Logger JavaDoc;
7
8 import javax.xml.XMLConstants JavaDoc;
9 import javax.xml.namespace.QName JavaDoc;
10 import javax.xml.parsers.DocumentBuilder JavaDoc;
11 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
12 import javax.xml.parsers.ParserConfigurationException JavaDoc;
13 import javax.xml.transform.Source JavaDoc;
14 import javax.xml.transform.dom.DOMSource JavaDoc;
15 import javax.xml.transform.stream.StreamSource JavaDoc;
16 import javax.xml.validation.Schema JavaDoc;
17 import javax.xml.validation.SchemaFactory JavaDoc;
18 import javax.xml.validation.Validator JavaDoc;
19
20 import org.w3c.dom.Document JavaDoc;
21 import org.w3c.dom.Element JavaDoc;
22 import org.w3c.dom.Node JavaDoc;
23
24 import org.xml.sax.ErrorHandler JavaDoc;
25 import org.xml.sax.InputSource JavaDoc;
26 import org.xml.sax.SAXException JavaDoc;
27 import org.xml.sax.SAXParseException JavaDoc;
28
29 import org.objectweb.celtix.common.i18n.Message;
30 import org.objectweb.celtix.common.logging.LogUtils;
31 import org.objectweb.celtix.configuration.ConfigurationException;
32 import org.objectweb.celtix.configuration.ConfigurationItemMetadata.LifecyclePolicy;
33 import org.objectweb.celtix.configuration.ConfigurationMetadata;
34 import org.objectweb.celtix.resource.DefaultResourceManager;
35
36
37 public class ConfigurationMetadataBuilder {
38
39     final class ValidatorErrorHandler implements ErrorHandler JavaDoc {
40
41         public void error(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
42             throw exception;
43         }
44
45         public void fatalError(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
46             throw exception;
47         }
48
49         public void warning(SAXParseException JavaDoc exception) throws SAXException JavaDoc {
50             throw exception;
51         }
52     }
53     private static final Logger JavaDoc LOG = LogUtils.getL7dLogger(ConfigurationMetadataBuilder.class);
54     private static final String JavaDoc MEATADATA_NAMESPACE_URI =
55         "http://celtix.objectweb.org/configuration/metadata";
56     private static Schema JavaDoc metadataSchema;
57     private static Validator JavaDoc metadataValidator;
58
59     private static ErrorHandler JavaDoc validatorErrorHandler;
60
61     private final ConfigurationMetadataImpl model;
62     private final boolean forceDefaults;
63     private boolean doValidate;
64
65     public ConfigurationMetadataBuilder(boolean fd) {
66         model = new ConfigurationMetadataImpl();
67         forceDefaults = fd;
68     }
69
70     public void setValidation(boolean onOff) {
71         doValidate = onOff;
72     }
73
74     public ConfigurationMetadata build(InputSource JavaDoc is) throws IOException JavaDoc {
75         parseXML(is);
76         return model;
77     }
78
79     public ConfigurationMetadata build(InputStream JavaDoc is) throws IOException JavaDoc {
80         return build(new InputSource JavaDoc(is));
81     }
82
83     private void deserializeConfig(Document JavaDoc document) {
84         Element JavaDoc root = document.getDocumentElement();
85         model.setNamespaceURI(root.getAttribute("namespace"));
86         model.setParentNamespaceURI(root.getAttribute("parentNamespace"));
87     }
88
89     private void deserializeConfigItem(Document JavaDoc document, Element JavaDoc configItemElement) {
90
91         ConfigurationItemMetadataImpl item = new ConfigurationItemMetadataImpl();
92
93         for (Node JavaDoc nd = configItemElement.getFirstChild(); nd != null; nd = nd.getNextSibling()) {
94             if (Node.ELEMENT_NODE != nd.getNodeType()) {
95                 continue;
96             } else if ("name".equals(nd.getLocalName())) {
97                 item.setName(ConfigurationMetadataUtils.getElementValue(nd));
98             } else if ("type".equals(nd.getLocalName())) {
99                 QName JavaDoc type = ConfigurationMetadataUtils.elementValueToQName(document,
100                                                                            (Element JavaDoc)nd);
101                 item.setType(type);
102                 if (doValidate) {
103                     if (XMLConstants.W3C_XML_SCHEMA_NS_URI.equals(type.getNamespaceURI())) {
104                         continue;
105                     }
106                     TypeSchema ts = new TypeSchemaHelper(forceDefaults).get(type.getNamespaceURI());
107                     if (ts == null) {
108                         throw new ConfigurationException JavaDoc(new Message("NO_TYPESCHEMA_FOR_NAMESPACE_EXC", LOG,
109                                                                      type.getNamespaceURI()));
110                     }
111                     if (!ts.hasType(type.getLocalPart())) {
112                         throw new ConfigurationException JavaDoc(new Message("TYPE_NOT_DEFINED_IN_NAMESPACE_EXC",
113                                                                      LOG, type.getLocalPart(), type
114                                                                          .getNamespaceURI()));
115                     }
116                 }
117             } else if ("description".equals(nd.getLocalName())) {
118                 // item.setDescription(getElementValue(nd));
119
} else if ("lifecyclePolicy".equals(nd.getLocalName())) {
120                 String JavaDoc value = ConfigurationMetadataUtils.getElementValue(nd);
121                 if (null != value) {
122                     if ("static".equals(value)) {
123                         item.setLifecyclePolicy(LifecyclePolicy.STATIC);
124                     } else if ("process".equals(value)) {
125                         item.setLifecyclePolicy(LifecyclePolicy.PROCESS);
126                     } else if ("bus".equals(value)) {
127                         item.setLifecyclePolicy(LifecyclePolicy.BUS);
128                     } else {
129                         item.setLifecyclePolicy(LifecyclePolicy.DYNAMIC);
130                     }
131                 }
132             } else {
133                 // this must be the extension element holding the default value
134
deserializeDefaultValue(item, (Element JavaDoc)nd);
135             }
136         }
137
138         model.addItem(item);
139     }
140
141     private void deserializeConfigItems(Document JavaDoc document) {
142         for (Node JavaDoc nd = document.getDocumentElement().getFirstChild(); nd != null; nd = nd.getNextSibling()) {
143             if (Node.ELEMENT_NODE == nd.getNodeType()
144                 && "configItem".equals(nd.getLocalName())
145                 && MEATADATA_NAMESPACE_URI.equals(nd.getNamespaceURI())) {
146                 Element JavaDoc configItemElement = (Element JavaDoc)nd;
147                 deserializeConfigItem(document, configItemElement);
148             }
149         }
150     }
151
152     private void deserializeDefaultValue(ConfigurationItemMetadataImpl item, Element JavaDoc data) {
153         /*
154         String namespaceURI = data.getNamespaceURI();
155         System.out.println("deserializeDefaultValue: \n"
156                            + " data namespaceURI: " + namespaceURI + "\n"
157                            + " data localName: " + data.getLocalName() + "\n"
158                            + " item type: " + item.getType());
159
160         if (!namespaceURI.equals(item.getType().getNamespaceURI())) {
161             Message msg = new Message("INVALID_ELEMENT_FOR_DEFAULT_VALUE_EXC",
162                                       LOG, item.getName(), item.getType());
163             throw new ConfigurationException(msg);
164         }
165         TypeSchema ts = new TypeSchemaHelper().get(namespaceURI);
166         assert ts != null;
167         String name = data.getLocalName();
168         QName type = ts.getDeclaredType(name);
169         if (null == type || !type.equals(item.getType().getLocalPart())) {
170             Message msg = new Message("INVALID_ELEMENT_FOR_DEFAULT_VALUE_EXC",
171                                       LOG, item.getName(), item.getType());
172             throw new ConfigurationException(msg);
173         }
174         unmarshalDefaultValue(item, data);
175         */

176         String JavaDoc elementName = data.getLocalName();
177         String JavaDoc namespaceURI = data.getNamespaceURI();
178         TypeSchema ts = new TypeSchemaHelper(forceDefaults).get(namespaceURI);
179         QName JavaDoc type = null;
180         if (null != ts) {
181             type = ts.getDeclaredType(elementName);
182         }
183         if (null == ts || null == type) {
184             System.err.println(elementName);
185             System.err.println(namespaceURI);
186             System.err.println(ts);
187             System.err.println(type);
188             throw new ConfigurationException JavaDoc(new Message("INVALID_ELEMENT_FOR_DEFAULT_VALUE_EXC", LOG,
189                                                          item.getName(), item.getType()));
190         }
191         if (!type.equals(item.getType())) {
192             throw new ConfigurationException JavaDoc(new Message("INVALID_TYPE_FOR_DEFAULT_VALUE_EXC", LOG,
193                                                        item.getName(), item.getType()));
194         }
195         unmarshalDefaultValue(item, data);
196     }
197
198     private void deserializeImports(Document JavaDoc document) {
199         TypeSchemaHelper tsh = new TypeSchemaHelper(forceDefaults);
200         for (Node JavaDoc nd = document.getDocumentElement().getFirstChild(); nd != null; nd = nd.getNextSibling()) {
201             if (Node.ELEMENT_NODE == nd.getNodeType()
202                 && "configImport".equals(nd.getLocalName())
203                 && MEATADATA_NAMESPACE_URI.equals(nd.getNamespaceURI())) {
204                 Element JavaDoc importElement = (Element JavaDoc)nd;
205                 String JavaDoc location = importElement.getAttribute("location");
206                 String JavaDoc namespaceURI = importElement.getAttribute("namespace");
207                 if (null == tsh.get(namespaceURI)) {
208                     tsh.get(namespaceURI, document.getDocumentURI(), location);
209                 }
210             }
211         }
212     }
213
214     /**
215      * The configuration metadata schema is obtained system resource
216      * "schemas/configuration/metadata.xsd".
217      * It requires that either the resources directory is on the classpath or that
218      * the resources is listed in the classpath specified in the manifest of celtix.jar.
219      *
220      * @return the metadata schema
221      */

222
223     private Schema JavaDoc getMetadataSchema() {
224         if (null == metadataSchema) {
225             InputStream JavaDoc is =
226                 DefaultResourceManager.instance()
227                     .getResourceAsStream("schemas/configuration/metadata.xsd");
228
229             if (null == is) {
230                 throw new ConfigurationException JavaDoc(new Message("CANNOT_FIND_CONFIG_METADATA_SCHEMA_MSG", LOG));
231             }
232
233             try {
234                 metadataSchema = getSchema(is);
235             } catch (ConfigurationException JavaDoc ex) {
236                 // should never happen as metadata schema is immutable
237
LOG.log(Level.SEVERE, "CANNOT_CREATE_CONFIG_METADATA_SCHEMA_MSG", ex);
238             }
239         }
240         return metadataSchema;
241     }
242
243     private Validator JavaDoc getMetadataValidator() {
244         if (null == metadataValidator) {
245             Schema JavaDoc schema = getMetadataSchema();
246             // assert null != schema;
247
metadataValidator = schema.newValidator();
248             if (null == validatorErrorHandler) {
249                 validatorErrorHandler = new ValidatorErrorHandler();
250             }
251             metadataValidator.setErrorHandler(validatorErrorHandler);
252             // assert null != metadataValidator;
253
}
254         return metadataValidator;
255     }
256
257     private Schema JavaDoc getSchema(InputStream JavaDoc is) {
258         Source JavaDoc schemaFile = new StreamSource JavaDoc(is);
259
260         SchemaFactory JavaDoc factory = SchemaFactory.newInstance(XMLConstants.W3C_XML_SCHEMA_NS_URI);
261         Schema JavaDoc schema = null;
262         try {
263             schema = factory.newSchema(schemaFile);
264         } catch (SAXException JavaDoc ex) {
265             throw new ConfigurationException JavaDoc(new Message("SCHEMA_CREATION_ERROR_EXC", LOG), ex);
266         }
267         return schema;
268     }
269
270     private void parseXML(InputSource JavaDoc is) throws IOException JavaDoc {
271
272         // parse
273
Document JavaDoc document = null;
274         try {
275             DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
276             factory.setNamespaceAware(true);
277             DocumentBuilder JavaDoc parser = factory.newDocumentBuilder();
278             document = parser.parse(is);
279         } catch (ParserConfigurationException JavaDoc ex) {
280             throw new ConfigurationException JavaDoc(new Message("PARSER_CONFIGURATION_ERROR_EXC", LOG), ex);
281         } catch (SAXException JavaDoc ex) {
282             throw new ConfigurationException JavaDoc(new Message("PARSE_ERROR_EXC", LOG), ex);
283         }
284
285         if (doValidate) {
286             try {
287                 Validator JavaDoc v = getMetadataValidator();
288                 v.validate(new DOMSource JavaDoc(document));
289             } catch (SAXException JavaDoc ex) {
290                 Message msg = new Message("METADATA_VALIDATION_ERROR_EXC", LOG);
291                 throw new ConfigurationException JavaDoc(msg, ex);
292             }
293         }
294
295         deserializeImports(document);
296         deserializeConfig(document);
297         deserializeConfigItems(document);
298     }
299
300     private void unmarshalDefaultValue(ConfigurationItemMetadataImpl item, Element JavaDoc data) {
301         TypeSchema ts = new TypeSchemaHelper(forceDefaults).get(data.getNamespaceURI());
302         Object JavaDoc obj = ts.unmarshalDefaultValue(item, data, doValidate);
303         if (null != obj) {
304             item.setDefaultValue(obj);
305         }
306     }
307 }
308
Popular Tags