/**
   * Returns existing compiled Templates if it exists.
   *
   * @param systemId source path for the stylesheet.
   * @return a compiled stylesheet
   */
  StylesheetImpl loadPrecompiledStylesheet(String systemId, String userId, boolean checkModified) {
    if (!_loadPrecompiledStylesheet) return null;

    try {
      // look for compiled template base on SystemID
      StylesheetImpl stylesheet = loadStylesheet(systemId, getMangledName(userId));

      if (stylesheet == null) return null;

      stylesheet.setURIResolver(_uriResolver);
      // and check if it's modified or not
      if (!checkModified || !stylesheet.isModified()) {
        stylesheet.setURIResolver(_uriResolver);
        return stylesheet;
      }
    } catch (Throwable e) {
      log.log(Level.FINER, e.toString(), e);
    }

    return null;
  }
  /**
   * Loads the compiled stylesheet .class file
   *
   * @param className the mangled classname for the stylesheet
   */
  protected StylesheetImpl loadStylesheet(String systemId, String className) throws Exception {
    LruCache<String, SoftReference<StylesheetImpl>> cache;

    ClassLoader parentLoader = Thread.currentThread().getContextClassLoader();

    cache = _stylesheetCache.getLevel(parentLoader);

    if (cache == null) {
      cache = new LruCache<String, SoftReference<StylesheetImpl>>(256);
      _stylesheetCache.set(cache, parentLoader);
    }

    SoftReference<StylesheetImpl> stylesheetRef;

    stylesheetRef = cache.get(className);

    StylesheetImpl stylesheet = null;

    if (stylesheetRef != null) stylesheet = stylesheetRef.get();

    try {
      if (stylesheet != null && !stylesheet.isModified()) return stylesheet;
    } catch (Throwable e) {
      log.log(Level.FINER, e.toString(), e);
    }

    Path classPath = getWorkPath().lookup(className.replace('.', '/') + ".class");
    if (!classPath.canRead())
      throw new ClassNotFoundException("can't find compiled XSL `" + className + "'");

    DynamicClassLoader loader;
    loader = SimpleLoader.create(parentLoader, getWorkPath(), className);

    Class cl = null;

    // If the loading fails, remove the class because it may be corrupted
    try {
      cl = CauchoSystem.loadClass(className, false, loader);
    } catch (Error e) {
      try {
        classPath.remove();
      } catch (IOException e1) {
        log.log(Level.FINE, e1.toString(), e1);
      }

      throw e;
    }

    stylesheet = (StylesheetImpl) cl.newInstance();
    Path path;

    path = getSearchPath().lookup("").lookup(systemId);
    /*
    try {
    } catch (TransformerException e) {
      log.log(Level.FINE, e.toString(), e);

      path = Vfs.lookup(systemId);
    }
      */

    // stylesheet.init(path);
    stylesheet.init(getStylePath());
    stylesheet.setURIResolver(_uriResolver);

    stylesheetRef = new SoftReference<StylesheetImpl>(stylesheet);
    cache.put(className, stylesheetRef);

    return stylesheet;
  }
  /**
   * Generates a compiled stylesheet from a parsed XSL document.
   *
   * @param xsl the parsed xsl document.
   * @param path the path to the document.
   */
  Templates generate(Node xsl, Path path) throws TransformerConfigurationException {
    log.fine("Generating XSL from " + path);

    // The code generation needs a static lock because the
    // application might have a separate factory object
    // for each thread.  The static lock protects the code generation
    // from overwriting its own code.
    synchronized (AbstractStylesheetFactory.class) {
      Generator gen = null;

      try {
        if (path == null && xsl != null) {
          Document doc = xsl.getOwnerDocument();
          if (doc == null && xsl instanceof Document) doc = (Document) xsl;

          DocumentType dtd = doc.getDoctype();
          String systemId = null;
          if (dtd != null) systemId = dtd.getSystemId();

          if (systemId != null) path = getStylePath().lookup(systemId);
        }

        if (path == null && xsl instanceof CauchoNode) {
          String filename = ((CauchoNode) xsl).getFilename();
          if (filename != null) path = getStylePath().lookup(filename);
        }

        if (path == null) path = getStylePath().lookup("anonymous.xsl");

        Path stylePath = path.getParent();

        Expr expr = XPath.parseExpr("//xtp:directive.page/@language");
        String language = expr.evalString(xsl);

        String userName = path.getUserPath();
        String mangledName = getMangledName(userName);

        String encoding = XPath.evalString("//xsl:output/@encoding", xsl);
        if (encoding != null && encoding.equals("")) encoding = null;

        if (language == null || language.equals("") || language.equals("java")) {
          language = "java";
          gen = new JavaGenerator(this, mangledName, encoding);
        } else throw new XslParseException(L.l("unsupported language `{0}'", language));

        gen.setPath(path);

        Iterator iter = XPath.select("//xtp:directive.page/@*", xsl);
        while (iter.hasNext()) {
          Attr attr = (Attr) iter.next();
          String name = attr.getNodeName();
          String value = attr.getNodeValue();

          if (name.equals("errorPage")) gen.setErrorPage(value);
          else if (name.equals("import")) gen.addImport(value);
          else if (name.equals("contentType")) gen.setContentType(value);
          else if (name.equals("language")) {
            if (!language.equalsIgnoreCase(value))
              throw new XslParseException(L.l("mismatched language `{0}'", value));
          } else if (name.equals("xml:space")) {
          } else throw new XslParseException(L.l("unknown directive `{0}'", name));
        }

        StylesheetImpl stylesheet = gen.generate(xsl);
        gen = null;
        stylesheet.init(path);
        // XXX: why was this here? stylesheet.init(getStylePath());
        stylesheet.setURIResolver(_uriResolver);

        return stylesheet;
      } catch (TransformerConfigurationException e) {
        throw e;
      } catch (Exception e) {
        throw new XslParseException(e);
      } finally {
        try {
          if (gen != null) gen.close();
        } catch (IOException e) {
        }
      }
    }
  }