01: /*
02: * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
03: */
04: package com.tc.config.schema.beanfactory;
05:
06: import org.apache.xmlbeans.XmlError;
07: import org.apache.xmlbeans.XmlException;
08: import org.apache.xmlbeans.XmlOptions;
09: import org.xml.sax.SAXException;
10:
11: import com.tc.config.Loader;
12: import com.tc.util.Assert;
13: import com.terracottatech.config.TcConfigDocument;
14:
15: import java.io.IOException;
16: import java.io.InputStream;
17: import java.util.ArrayList;
18: import java.util.List;
19:
20: import javax.xml.parsers.ParserConfigurationException;
21:
22: /**
23: * A {@link ConfigBeanFactory} that creates {@link TerracottaDomainConfigurationDocument} beans.
24: */
25: public class TerracottaDomainConfigurationDocumentBeanFactory implements
26: ConfigBeanFactory {
27:
28: public TerracottaDomainConfigurationDocumentBeanFactory() {
29: // Nothing here yet.
30: }
31:
32: public BeanWithErrors createBean(InputStream in,
33: String sourceDescription) throws IOException, SAXException,
34: ParserConfigurationException, XmlException {
35: Assert.assertNotBlank(sourceDescription);
36:
37: List errors = new ArrayList();
38: XmlOptions options = createXmlOptions(errors, sourceDescription);
39: Loader configLoader = new Loader();
40:
41: TcConfigDocument document = configLoader.parse(in, options);
42: document.validate(options);
43: return new BeanWithErrors(document, (XmlError[]) errors
44: .toArray(new XmlError[errors.size()]));
45: }
46:
47: public static XmlOptions createXmlOptions(List errors,
48: String sourceDescription) throws SAXException,
49: ParserConfigurationException {
50: ClassLoader contextClassLoader = Thread.currentThread()
51: .getContextClassLoader();
52: Thread.currentThread().setContextClassLoader(
53: TerracottaDomainConfigurationDocumentBeanFactory.class
54: .getClassLoader());
55: try {
56: XmlOptions options = new XmlOptions();
57: options = options.setLoadLineNumbers();
58: options = options.setDocumentSourceName(sourceDescription);
59: options = options.setErrorListener(errors);
60: return options;
61:
62: } finally {
63: Thread.currentThread().setContextClassLoader(
64: contextClassLoader);
65: }
66:
67: }
68: }
|