~ubuntu-branches/ubuntu/lucid/groovy/lucid

« back to all changes in this revision

Viewing changes to src/main/groovy/xml/FactorySupport.java

  • Committer: Bazaar Package Importer
  • Author(s): Torsten Werner
  • Date: 2009-05-19 21:49:37 UTC
  • mfrom: (3.2.4 squeeze)
  • Revision ID: james.westby@ubuntu.com-20090519214937-56mctwb05t3xd63r
Tags: 1.6.3-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
/*
2
 
 * Copyright 2003-2007 the original author or authors.
3
 
 *
4
 
 * Licensed under the Apache License, Version 2.0 (the "License");
5
 
 * you may not use this file except in compliance with the License.
6
 
 * You may obtain a copy of the License at
7
 
 *
8
 
 *     http://www.apache.org/licenses/LICENSE-2.0
9
 
 *
10
 
 * Unless required by applicable law or agreed to in writing, software
11
 
 * distributed under the License is distributed on an "AS IS" BASIS,
12
 
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
 
 * See the License for the specific language governing permissions and
14
 
 * limitations under the License.
15
 
 */
16
 
 
17
 
package groovy.xml;
18
 
 
19
 
import javax.xml.parsers.ParserConfigurationException;
20
 
import javax.xml.parsers.DocumentBuilderFactory;
21
 
import javax.xml.parsers.SAXParserFactory;
22
 
import java.security.PrivilegedExceptionAction;
23
 
import java.security.AccessController;
24
 
import java.security.PrivilegedActionException;
25
 
 
26
 
/**
27
 
 * Support class for creating XML Factories
28
 
 */
29
 
public class FactorySupport {
30
 
    static Object createFactory(PrivilegedExceptionAction action) throws ParserConfigurationException {
31
 
        Object factory;
32
 
        try {
33
 
            factory = AccessController.doPrivileged(action);
34
 
        } catch (PrivilegedActionException pae) {
35
 
            Exception e = pae.getException();
36
 
            if (e instanceof ParserConfigurationException) {
37
 
                throw(ParserConfigurationException) e;
38
 
            } else {
39
 
                throw new RuntimeException(e);
40
 
            }
41
 
        }
42
 
        return factory;
43
 
    }
44
 
 
45
 
    public static DocumentBuilderFactory createDocumentBuilderFactory() throws ParserConfigurationException {
46
 
        return (DocumentBuilderFactory) createFactory(new PrivilegedExceptionAction() {
47
 
            public Object run() throws ParserConfigurationException {
48
 
                return DocumentBuilderFactory.newInstance();
49
 
            }
50
 
        });
51
 
    }
52
 
 
53
 
    public static SAXParserFactory createSaxParserFactory() throws ParserConfigurationException {
54
 
        return (SAXParserFactory) createFactory(new PrivilegedExceptionAction() {
55
 
                public Object run() throws ParserConfigurationException {
56
 
                    return SAXParserFactory.newInstance();
57
 
                }
58
 
            });
59
 
    }
60
 
}
 
1
/*
 
2
 * Copyright 2003-2007 the original author or authors.
 
3
 *
 
4
 * Licensed under the Apache License, Version 2.0 (the "License");
 
5
 * you may not use this file except in compliance with the License.
 
6
 * You may obtain a copy of the License at
 
7
 *
 
8
 *     http://www.apache.org/licenses/LICENSE-2.0
 
9
 *
 
10
 * Unless required by applicable law or agreed to in writing, software
 
11
 * distributed under the License is distributed on an "AS IS" BASIS,
 
12
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 
13
 * See the License for the specific language governing permissions and
 
14
 * limitations under the License.
 
15
 */
 
16
 
 
17
package groovy.xml;
 
18
 
 
19
import javax.xml.parsers.ParserConfigurationException;
 
20
import javax.xml.parsers.DocumentBuilderFactory;
 
21
import javax.xml.parsers.SAXParserFactory;
 
22
import java.security.PrivilegedExceptionAction;
 
23
import java.security.AccessController;
 
24
import java.security.PrivilegedActionException;
 
25
 
 
26
/**
 
27
 * Support class for creating XML Factories
 
28
 */
 
29
public class FactorySupport {
 
30
    static Object createFactory(PrivilegedExceptionAction action) throws ParserConfigurationException {
 
31
        Object factory;
 
32
        try {
 
33
            factory = AccessController.doPrivileged(action);
 
34
        } catch (PrivilegedActionException pae) {
 
35
            Exception e = pae.getException();
 
36
            if (e instanceof ParserConfigurationException) {
 
37
                throw(ParserConfigurationException) e;
 
38
            } else {
 
39
                throw new RuntimeException(e);
 
40
            }
 
41
        }
 
42
        return factory;
 
43
    }
 
44
 
 
45
    public static DocumentBuilderFactory createDocumentBuilderFactory() throws ParserConfigurationException {
 
46
        return (DocumentBuilderFactory) createFactory(new PrivilegedExceptionAction() {
 
47
            public Object run() throws ParserConfigurationException {
 
48
                return DocumentBuilderFactory.newInstance();
 
49
            }
 
50
        });
 
51
    }
 
52
 
 
53
    public static SAXParserFactory createSaxParserFactory() throws ParserConfigurationException {
 
54
        return (SAXParserFactory) createFactory(new PrivilegedExceptionAction() {
 
55
                public Object run() throws ParserConfigurationException {
 
56
                    return SAXParserFactory.newInstance();
 
57
                }
 
58
            });
 
59
    }
 
60
}