01: package groovy.xml;
02:
03: import javax.xml.parsers.ParserConfigurationException;
04: import javax.xml.parsers.DocumentBuilderFactory;
05: import javax.xml.parsers.SAXParserFactory;
06: import java.security.PrivilegedExceptionAction;
07: import java.security.AccessController;
08: import java.security.PrivilegedActionException;
09:
10: /**
11: * Support class for creating XML Factories
12: */
13: public class FactorySupport {
14: static Object createFactory(PrivilegedExceptionAction action)
15: throws ParserConfigurationException {
16: Object factory;
17: try {
18: factory = AccessController.doPrivileged(action);
19: } catch (PrivilegedActionException pae) {
20: Exception e = pae.getException();
21: if (e instanceof ParserConfigurationException) {
22: throw (ParserConfigurationException) e;
23: } else {
24: throw new RuntimeException(e);
25: }
26: }
27: return factory;
28: }
29:
30: public static DocumentBuilderFactory createDocumentBuilderFactory()
31: throws ParserConfigurationException {
32: return (DocumentBuilderFactory) createFactory(new PrivilegedExceptionAction() {
33: public Object run() throws ParserConfigurationException {
34: return DocumentBuilderFactory.newInstance();
35: }
36: });
37: }
38:
39: public static SAXParserFactory createSaxParserFactory()
40: throws ParserConfigurationException {
41: return (SAXParserFactory) createFactory(new PrivilegedExceptionAction() {
42: public Object run() throws ParserConfigurationException {
43: return SAXParserFactory.newInstance();
44: }
45: });
46: }
47: }
|