public void configure(DBBroker broker, Collection parent, Map parameters)
      throws CollectionConfigurationException {
    super.configure(broker, parent, parameters);
    String stylesheet = (String) parameters.get("src");
    if (stylesheet == null)
      throw new CollectionConfigurationException(
          "STXTransformerTrigger requires an " + "attribute 'src'");
    String origProperty = System.getProperty("javax.xml.transform.TransformerFactory");
    System.setProperty(
        "javax.xml.transform.TransformerFactory", "net.sf.joost.trax.TransformerFactoryImpl");
    factory = (SAXTransformerFactory) TransformerFactory.newInstance();
    // reset property to previous setting
    if (origProperty != null)
      System.setProperty("javax.xml.transform.TransformerFactory", origProperty);

    getLogger().debug("compiling stylesheet " + stylesheet);
    XmldbURI stylesheetUri = null;
    try {
      stylesheetUri = XmldbURI.xmldbUriFor(stylesheet);
    } catch (URISyntaxException e) {
    }
    // TODO: allow full XmldbURIs to be used as well.
    if (stylesheetUri == null || stylesheet.indexOf(':') == Constants.STRING_NOT_FOUND) {
      stylesheetUri = parent.getURI().resolveCollectionPath(stylesheetUri);
      DocumentImpl doc;
      try {
        doc = (DocumentImpl) broker.getXMLResource(stylesheetUri);
        if (doc == null)
          throw new CollectionConfigurationException(
              "stylesheet " + stylesheetUri + " not found in database");
        Serializer serializer = broker.getSerializer();
        TemplatesHandler thandler = factory.newTemplatesHandler();
        serializer.setSAXHandlers(thandler, null);
        serializer.toSAX(doc);
        template = thandler.getTemplates();
        handler = factory.newTransformerHandler(template);
      } catch (TransformerConfigurationException e) {
        throw new CollectionConfigurationException(e.getMessage(), e);
      } catch (PermissionDeniedException e) {
        throw new CollectionConfigurationException(e.getMessage(), e);
      } catch (SAXException e) {
        throw new CollectionConfigurationException(e.getMessage(), e);
      }
    } else
      try {
        template = factory.newTemplates(new StreamSource(stylesheet));
        handler = factory.newTransformerHandler(template);
      } catch (TransformerConfigurationException e) {
        throw new CollectionConfigurationException(e.getMessage(), e);
      }
  }
Ejemplo n.º 2
0
  public void init(ServletConfig config) throws ServletException {
    super.init(config);

    query = config.getInitParameter("xquery");
    if (query == null) {
      throw new ServletException("RedirectorServlet requires a parameter 'xquery'.");
    }
    user = config.getInitParameter("user");
    if (user == null) {
      user = DEFAULT_USER;
    }
    password = config.getInitParameter("password");
    if (password == null) {
      password = DEFAULT_PASS;
    }
    final String confCollectionURI = config.getInitParameter("uri");
    if (confCollectionURI == null) {
      collectionURI = DEFAULT_URI;
    } else {
      try {
        collectionURI = XmldbURI.xmldbUriFor(confCollectionURI);
      } catch (final URISyntaxException e) {
        throw new ServletException("Invalid XmldbURI for parameter 'uri': " + e.getMessage(), e);
      }
    }

    try {
      final Class<?> driver = Class.forName(DRIVER);
      final Database database = (Database) driver.newInstance();
      database.setProperty("create-database", "true");
      DatabaseManager.registerDatabase(database);
    } catch (final Exception e) {
      final String errorMessage = "Failed to initialize database driver";
      LOG.error(errorMessage, e);
      throw new ServletException(errorMessage + ": " + e.getMessage(), e);
    }
  }