Exemplo n.º 1
0
  /**
   * Evaluates the expression as an string.
   *
   * @param node the current node
   * @param env the variable environment.
   * @return the string representation of the expression.
   */
  public String evalString(Node node, ExprEnvironment env) throws XPathException {
    String rel = _relExpr.evalString(node, env);

    if (rel.startsWith("/") || rel.indexOf(':') > 0 && rel.indexOf(':') < '/') return rel;

    String base;

    if (_baseExpr != null) base = _baseExpr.evalString(node, env);
    else base = QAbstractNode.baseURI(node);

    int last = base.lastIndexOf('/');

    if (last < 0) return rel;
    else return base.substring(0, last + 1) + rel;
  }
  /**
   * 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) {
        }
      }
    }
  }