public StylesheetImpl newStylesheet(Path path) throws Exception {
    StylesheetImpl stylesheet = loadPrecompiledStylesheet(path.getFullPath(), path.getUserPath());

    if (stylesheet != null) return stylesheet;

    synchronized (AbstractStylesheetFactory.class) {
      stylesheet = loadPrecompiledStylesheet(path.getFullPath(), path.getUserPath());

      if (stylesheet != null) return stylesheet;

      Path oldStylePath = _stylePath;

      if (_stylePath == null) _stylePath = path.getParent();

      InputStream is = null;

      try {
        is = path.openRead();
        return newStylesheet(is);
      } finally {
        _stylePath = oldStylePath;
        if (is != null) is.close();
      }
    }
  }
  /** Compile a schema. */
  public Schema compileSchema(Path path) throws SAXException, IOException {
    String nativePath = path.getNativePath();

    SoftReference<Schema> schemaRef = _schemaMap.get(path);
    Schema schema = null;

    if (schemaRef != null && (schema = schemaRef.get()) != null) {
      // XXX: probably eventually add an isModified
      return schema;
    }

    ReadStream is = path.openRead();

    try {
      InputSource source = new InputSource(is);

      source.setSystemId(path.getUserPath());

      schema = compileSchema(source);

      if (schema != null) _schemaMap.put(path, new SoftReference<Schema>(schema));
    } finally {
      is.close();
    }

    return schema;
  }
Esempio n. 3
0
 public String __toString(Env env) {
   return _path.getUserPath();
 }
  /**
   * 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) {
        }
      }
    }
  }