KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > tc > config > schema > beanfactory > TerracottaDomainConfigurationDocumentBeanFactory


1 /*
2  * All content copyright (c) 2003-2006 Terracotta, Inc., except as may otherwise be noted in a separate copyright notice. All rights reserved.
3  */

4 package com.tc.config.schema.beanfactory;
5
6 import org.apache.xmlbeans.XmlError;
7 import org.apache.xmlbeans.XmlException;
8 import org.apache.xmlbeans.XmlOptions;
9 import org.xml.sax.SAXException JavaDoc;
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 JavaDoc;
16 import java.io.InputStream JavaDoc;
17 import java.util.ArrayList JavaDoc;
18 import java.util.List JavaDoc;
19
20 import javax.xml.parsers.ParserConfigurationException JavaDoc;
21
22 /**
23  * A {@link ConfigBeanFactory} that creates {@link TerracottaDomainConfigurationDocument} beans.
24  */

25 public class TerracottaDomainConfigurationDocumentBeanFactory implements ConfigBeanFactory {
26
27   public TerracottaDomainConfigurationDocumentBeanFactory() {
28     // Nothing here yet.
29
}
30
31   public BeanWithErrors createBean(InputStream JavaDoc in, String JavaDoc sourceDescription) throws IOException JavaDoc, SAXException JavaDoc,
32       ParserConfigurationException JavaDoc, XmlException {
33     Assert.assertNotBlank(sourceDescription);
34
35     List JavaDoc errors = new ArrayList JavaDoc();
36     XmlOptions options = createXmlOptions(errors, sourceDescription);
37     Loader configLoader = new Loader();
38
39     TcConfigDocument document = configLoader.parse(in, options);
40     document.validate(options);
41     return new BeanWithErrors(document, (XmlError[]) errors.toArray(new XmlError[errors.size()]));
42   }
43
44   public static XmlOptions createXmlOptions(List JavaDoc errors, String JavaDoc sourceDescription)
45       throws SAXException JavaDoc, ParserConfigurationException JavaDoc {
46     ClassLoader JavaDoc contextClassLoader = Thread.currentThread().getContextClassLoader();
47     Thread.currentThread().setContextClassLoader(
48                                                  TerracottaDomainConfigurationDocumentBeanFactory.class
49                                                      .getClassLoader());
50     try {
51       XmlOptions options = new XmlOptions();
52       options = options.setLoadLineNumbers();
53       options = options.setDocumentSourceName(sourceDescription);
54       options = options.setErrorListener(errors);
55       return options;
56
57     } finally {
58       Thread.currentThread().setContextClassLoader(contextClassLoader);
59     }
60
61   }
62 }
63
Popular Tags