View Javadoc

    1   package groovy.xml;
    2
    3   import javax.xml.parsers.ParserConfigurationException;
    4   import javax.xml.parsers.DocumentBuilderFactory;
    5   import javax.xml.parsers.SAXParserFactory;
    6   import java.security.PrivilegedExceptionAction;
    7   import java.security.AccessController;
    8   import java.security.PrivilegedActionException;
    9
    10  /***
    11   * Support class for creating XML Factories
    12   */
    13  public class FactorySupport {
    14      static Object createFactory(PrivilegedExceptionAction action) throws ParserConfigurationException {
    15          Object factory;
    16          try {
    17              factory = AccessController.doPrivileged(action);
    18          } catch (PrivilegedActionException pae) {
    19              Exception e = pae.getException();
    20              if (e instanceof ParserConfigurationException) {
    21                  throw(ParserConfigurationException) e;
    22              } else {
    23                  throw new RuntimeException(e);
    24              }
    25          }
    26          return factory;
    27      }
    28
    29      public static DocumentBuilderFactory createDocumentBuilderFactory() throws ParserConfigurationException {
    30          return (DocumentBuilderFactory) createFactory(new PrivilegedExceptionAction() {
    31              public Object run() throws ParserConfigurationException {
    32                  return DocumentBuilderFactory.newInstance();
    33              }
    34          });
    35      }
    36
    37      public static SAXParserFactory createSaxParserFactory() throws ParserConfigurationException {
    38          return (SAXParserFactory) createFactory(new PrivilegedExceptionAction() {
    39                  public Object run() throws ParserConfigurationException {
    40                      return SAXParserFactory.newInstance();
    41                  }
    42              });
    43      }
    44  }