Example #1
0
 /**
  * Registers an element and gives it a type so that it can be mapped to DOM element types at
  * run-time.
  */
 public int registerElement(QName name) {
   // Register element (full QName)
   Integer code = (Integer) _elements.get(name.toString());
   if (code == null) {
     _elements.put(name.toString(), code = new Integer(_nextGType++));
     _namesIndex.addElement(name.toString());
   }
   if (name.getLocalPart().equals("*")) {
     registerNamespace(name.getNamespace());
   }
   return code.intValue();
 }
Example #2
0
 /**
  * Registers an attribute and gives it a type so that it can be mapped to DOM attribute types at
  * run-time.
  */
 public int registerAttribute(QName name) {
   Integer code = (Integer) _attributes.get(name.toString());
   if (code == null) {
     code = new Integer(_nextGType++);
     _attributes.put(name.toString(), code);
     final String uri = name.getNamespace();
     final String local = "@" + name.getLocalPart();
     if ((uri != null) && (!uri.equals(""))) _namesIndex.addElement(uri + ":" + local);
     else _namesIndex.addElement(local);
     if (name.getLocalPart().equals("*")) {
       registerNamespace(name.getNamespace());
     }
   }
   return code.intValue();
 }
 /*    */ public void translate(ClassGenerator classGen, MethodGenerator methodGen) /*    */ {
   /* 88 */ ConstantPoolGen cpg = classGen.getConstantPool();
   /* 89 */ InstructionList il = methodGen.getInstructionList();
   /* 90 */ SymbolTable symbolTable = getParser().getSymbolTable();
   /*    */
   /* 93 */ for (int i = 0; i < this._sets.size(); i++)
   /*    */ {
     /* 95 */ QName name = (QName) this._sets.elementAt(i);
     /*    */
     /* 97 */ AttributeSet attrs = symbolTable.lookupAttributeSet(name);
     /*    */
     /* 99 */ if (attrs != null) {
       /* 100 */ String methodName = attrs.getMethodName();
       /* 101 */ il.append(classGen.loadTranslet());
       /* 102 */ il.append(methodGen.loadDOM());
       /* 103 */ il.append(methodGen.loadIterator());
       /* 104 */ il.append(methodGen.loadHandler());
       /* 105 */ il.append(methodGen.loadCurrentNode());
       /* 106 */ int method =
           cpg.addMethodref(
               classGen.getClassName(),
               methodName,
               "(Lcom/sun/org/apache/xalan/internal/xsltc/DOM;Lcom/sun/org/apache/xml/internal/dtm/DTMAxisIterator;Lcom/sun/org/apache/xml/internal/serializer/SerializationHandler;I)V");
       /*    */
       /* 108 */ il.append(new INVOKESPECIAL(method));
       /*    */ }
     /*    */ else
     /*    */ {
       /* 112 */ Parser parser = getParser();
       /* 113 */ String atrs = name.toString();
       /* 114 */ reportError(this, parser, "ATTRIBSET_UNDEF_ERR", atrs);
       /*    */ }
     /*    */ }
   /*    */ }
Example #4
0
  /**
   * Registers a namespace prefix and gives it a type so that it can be mapped to DOM namespace
   * types at run-time.
   */
  public int registerNamespacePrefix(QName name) {

    Integer code = (Integer) _namespacePrefixes.get(name.toString());
    if (code == null) {
      code = new Integer(_nextGType++);
      _namespacePrefixes.put(name.toString(), code);
      final String uri = name.getNamespace();
      if ((uri != null) && (!uri.equals(""))) {
        // namespace::ext2:ped2 will be made empty in TypedNamespaceIterator
        _namesIndex.addElement("?");
      } else {
        _namesIndex.addElement("?" + name.getLocalPart());
      }
    }
    return code.intValue();
  }
 /** Translate code to call the BasisLibrary.unallowed_extensionF(String) method. */
 private void translateUnallowedExtension(ConstantPoolGen cpg, InstructionList il) {
   int index =
       cpg.addMethodref(
           BASIS_LIBRARY_CLASS, "unallowed_extension_functionF", "(Ljava/lang/String;)V");
   il.append(new PUSH(cpg, _fname.toString()));
   il.append(new INVOKESTATIC(index));
 }
Example #6
0
  /** Parse the name of the <xsl:decimal-formatting/> element */
  public void parseContents(Parser parser) {
    // Get the name of these decimal formatting symbols
    final String name = getAttribute("name");
    if (name.length() > 0) {
      if (!XML11Char.isXML11ValidQName(name)) {
        ErrorMsg err = new ErrorMsg(ErrorMsg.INVALID_QNAME_ERR, name, this);
        parser.reportError(Constants.ERROR, err);
      }
    }
    _name = parser.getQNameIgnoreDefaultNs(name);
    if (_name == null) {
      _name = parser.getQNameIgnoreDefaultNs(EMPTYSTRING);
    }

    // Check if a set of symbols has already been registered under this name
    SymbolTable stable = parser.getSymbolTable();
    if (stable.getDecimalFormatting(_name) != null) {
      reportWarning(this, parser, ErrorMsg.SYMBOLS_REDEF_ERR, _name.toString());
    } else {
      stable.addDecimalFormatting(_name, this);
    }
  }
Example #7
0
  /**
   * This method is called when the constructor is compiled in Stylesheet.compileConstructor() and
   * not as the syntax tree is traversed.
   */
  public void translate(ClassGenerator classGen, MethodGenerator methodGen) {

    ConstantPoolGen cpg = classGen.getConstantPool();
    InstructionList il = methodGen.getInstructionList();

    // DecimalFormatSymbols.<init>(Locale);
    // xsl:decimal-format - except for the NaN and infinity attributes.
    final int init = cpg.addMethodref(DFS_CLASS, "<init>", "(" + LOCALE_SIG + ")V");

    // Push the format name on the stack for call to addDecimalFormat()
    il.append(classGen.loadTranslet());
    il.append(new PUSH(cpg, _name.toString()));

    // Manufacture a DecimalFormatSymbols on the stack
    // for call to addDecimalFormat()
    // Use the US Locale as the default, as most of its settings
    // are equivalent to the default settings required of
    il.append(new NEW(cpg.addClass(DFS_CLASS)));
    il.append(DUP);
    il.append(new GETSTATIC(cpg.addFieldref(LOCALE_CLASS, "US", LOCALE_SIG)));
    il.append(new INVOKESPECIAL(init));

    String tmp = getAttribute("NaN");
    if ((tmp == null) || (tmp.equals(EMPTYSTRING))) {
      int nan = cpg.addMethodref(DFS_CLASS, "setNaN", "(Ljava/lang/String;)V");
      il.append(DUP);
      il.append(new PUSH(cpg, "NaN"));
      il.append(new INVOKEVIRTUAL(nan));
    }

    tmp = getAttribute("infinity");
    if ((tmp == null) || (tmp.equals(EMPTYSTRING))) {
      int inf = cpg.addMethodref(DFS_CLASS, "setInfinity", "(Ljava/lang/String;)V");
      il.append(DUP);
      il.append(new PUSH(cpg, "Infinity"));
      il.append(new INVOKEVIRTUAL(inf));
    }

    final int nAttributes = _attributes.getLength();
    for (int i = 0; i < nAttributes; i++) {
      final String name = _attributes.getQName(i);
      final String value = _attributes.getValue(i);

      boolean valid = true;
      int method = 0;

      if (name.equals("decimal-separator")) {
        // DecimalFormatSymbols.setDecimalSeparator();
        method = cpg.addMethodref(DFS_CLASS, "setDecimalSeparator", "(C)V");
      } else if (name.equals("grouping-separator")) {
        method = cpg.addMethodref(DFS_CLASS, "setGroupingSeparator", "(C)V");
      } else if (name.equals("minus-sign")) {
        method = cpg.addMethodref(DFS_CLASS, "setMinusSign", "(C)V");
      } else if (name.equals("percent")) {
        method = cpg.addMethodref(DFS_CLASS, "setPercent", "(C)V");
      } else if (name.equals("per-mille")) {
        method = cpg.addMethodref(DFS_CLASS, "setPerMill", "(C)V");
      } else if (name.equals("zero-digit")) {
        method = cpg.addMethodref(DFS_CLASS, "setZeroDigit", "(C)V");
      } else if (name.equals("digit")) {
        method = cpg.addMethodref(DFS_CLASS, "setDigit", "(C)V");
      } else if (name.equals("pattern-separator")) {
        method = cpg.addMethodref(DFS_CLASS, "setPatternSeparator", "(C)V");
      } else if (name.equals("NaN")) {
        method = cpg.addMethodref(DFS_CLASS, "setNaN", "(Ljava/lang/String;)V");
        il.append(DUP);
        il.append(new PUSH(cpg, value));
        il.append(new INVOKEVIRTUAL(method));
        valid = false;
      } else if (name.equals("infinity")) {
        method = cpg.addMethodref(DFS_CLASS, "setInfinity", "(Ljava/lang/String;)V");
        il.append(DUP);
        il.append(new PUSH(cpg, value));
        il.append(new INVOKEVIRTUAL(method));
        valid = false;
      } else {
        valid = false;
      }

      if (valid) {
        il.append(DUP);
        il.append(new PUSH(cpg, value.charAt(0)));
        il.append(new INVOKEVIRTUAL(method));
      }
    }

    final int put =
        cpg.addMethodref(TRANSLET_CLASS, "addDecimalFormat", "(" + STRING_SIG + DFS_SIG + ")V");
    il.append(new INVOKEVIRTUAL(put));
  }
 @Override
 public String getMessage() {
   String id = fieldName != null ? fieldName.toString() : fieldId;
   return "Field <" + id + "> could not be found.";
 }
  /**
   * Translate a function call. The compiled code will leave the function's return value on the
   * JVM's stack.
   */
  public void translate(ClassGenerator classGen, MethodGenerator methodGen) {
    final int n = argumentCount();
    final ConstantPoolGen cpg = classGen.getConstantPool();
    final InstructionList il = methodGen.getInstructionList();
    final boolean isSecureProcessing = classGen.getParser().getXSLTC().isSecureProcessing();
    int index;

    // Translate calls to methods in the BasisLibrary
    if (isStandard() || isExtension()) {
      for (int i = 0; i < n; i++) {
        final Expression exp = argument(i);
        exp.translate(classGen, methodGen);
        exp.startIterator(classGen, methodGen);
      }

      // append "F" to the function's name
      final String name = _fname.toString().replace('-', '_') + "F";
      String args = Constants.EMPTYSTRING;

      // Special precautions for some method calls
      if (name.equals("sumF")) {
        args = DOM_INTF_SIG;
        il.append(methodGen.loadDOM());
      } else if (name.equals("normalize_spaceF")) {
        if (_chosenMethodType.toSignature(args).equals("()Ljava/lang/String;")) {
          args = "I" + DOM_INTF_SIG;
          il.append(methodGen.loadContextNode());
          il.append(methodGen.loadDOM());
        }
      }

      // Invoke the method in the basis library
      index = cpg.addMethodref(BASIS_LIBRARY_CLASS, name, _chosenMethodType.toSignature(args));
      il.append(new INVOKESTATIC(index));
    }
    // Add call to BasisLibrary.unresolved_externalF() to generate
    // run-time error message for unsupported external functions
    else if (unresolvedExternal) {
      index =
          cpg.addMethodref(BASIS_LIBRARY_CLASS, "unresolved_externalF", "(Ljava/lang/String;)V");
      il.append(new PUSH(cpg, _fname.toString()));
      il.append(new INVOKESTATIC(index));
    } else if (_isExtConstructor) {
      if (isSecureProcessing) translateUnallowedExtension(cpg, il);

      final String clazz = _chosenConstructor.getDeclaringClass().getName();
      Class[] paramTypes = _chosenConstructor.getParameterTypes();
      LocalVariableGen[] paramTemp = new LocalVariableGen[n];

      // Backwards branches are prohibited if an uninitialized object is
      // on the stack by section 4.9.4 of the JVM Specification, 2nd Ed.
      // We don't know whether this code might contain backwards branches
      // so we mustn't create the new object until after we've created
      // the suspect arguments to its constructor.  Instead we calculate
      // the values of the arguments to the constructor first, store them
      // in temporary variables, create the object and reload the
      // arguments from the temporaries to avoid the problem.

      for (int i = 0; i < n; i++) {
        final Expression exp = argument(i);
        Type expType = exp.getType();
        exp.translate(classGen, methodGen);
        // Convert the argument to its Java type
        exp.startIterator(classGen, methodGen);
        expType.translateTo(classGen, methodGen, paramTypes[i]);
        paramTemp[i] =
            methodGen.addLocalVariable(
                "function_call_tmp" + i, expType.toJCType(), il.getEnd(), null);
        il.append(expType.STORE(paramTemp[i].getIndex()));
      }

      il.append(new NEW(cpg.addClass(_className)));
      il.append(InstructionConstants.DUP);

      for (int i = 0; i < n; i++) {
        final Expression arg = argument(i);
        il.append(arg.getType().LOAD(paramTemp[i].getIndex()));
      }

      final StringBuffer buffer = new StringBuffer();
      buffer.append('(');
      for (int i = 0; i < paramTypes.length; i++) {
        buffer.append(getSignature(paramTypes[i]));
      }
      buffer.append(')');
      buffer.append("V");

      index = cpg.addMethodref(clazz, "<init>", buffer.toString());
      il.append(new INVOKESPECIAL(index));

      // Convert the return type back to our internal type
      (Type.Object).translateFrom(classGen, methodGen, _chosenConstructor.getDeclaringClass());

    }
    // Invoke function calls that are handled in separate classes
    else {
      if (isSecureProcessing) translateUnallowedExtension(cpg, il);

      final String clazz = _chosenMethod.getDeclaringClass().getName();
      Class[] paramTypes = _chosenMethod.getParameterTypes();

      // Push "this" if it is an instance method
      if (_thisArgument != null) {
        _thisArgument.translate(classGen, methodGen);
      }

      for (int i = 0; i < n; i++) {
        final Expression exp = argument(i);
        exp.translate(classGen, methodGen);
        // Convert the argument to its Java type
        exp.startIterator(classGen, methodGen);
        exp.getType().translateTo(classGen, methodGen, paramTypes[i]);
      }

      final StringBuffer buffer = new StringBuffer();
      buffer.append('(');
      for (int i = 0; i < paramTypes.length; i++) {
        buffer.append(getSignature(paramTypes[i]));
      }
      buffer.append(')');
      buffer.append(getSignature(_chosenMethod.getReturnType()));

      if (_thisArgument != null && _clazz.isInterface()) {
        index = cpg.addInterfaceMethodref(clazz, _fname.getLocalPart(), buffer.toString());
        il.append(new INVOKEINTERFACE(index, n + 1));
      } else {
        index = cpg.addMethodref(clazz, _fname.getLocalPart(), buffer.toString());
        il.append(
            _thisArgument != null
                ? (InvokeInstruction) new INVOKEVIRTUAL(index)
                : (InvokeInstruction) new INVOKESTATIC(index));
      }

      // Convert the return type back to our internal type
      _type.translateFrom(classGen, methodGen, _chosenMethod.getReturnType());
    }
  }
  /**
   * Type check a call to an external (Java) method. The method must be static an public, and a
   * legal type conversion must exist for all its arguments and its return type. Every method of
   * name <code>_fname</code> is inspected as a possible candidate.
   */
  public Type typeCheckExternal(SymbolTable stable) throws TypeCheckError {
    int nArgs = _arguments.size();
    final String name = _fname.getLocalPart();

    // check if function is a contructor 'new'
    if (_fname.getLocalPart().equals("new")) {
      return typeCheckConstructor(stable);
    }
    // check if we are calling an instance method
    else {
      boolean hasThisArgument = false;

      if (nArgs == 0) _isStatic = true;

      if (!_isStatic) {
        if (_namespace_format == NAMESPACE_FORMAT_JAVA
            || _namespace_format == NAMESPACE_FORMAT_PACKAGE) hasThisArgument = true;

        Expression firstArg = (Expression) _arguments.elementAt(0);
        Type firstArgType = (Type) firstArg.typeCheck(stable);

        if (_namespace_format == NAMESPACE_FORMAT_CLASS
            && firstArgType instanceof ObjectType
            && _clazz != null
            && _clazz.isAssignableFrom(((ObjectType) firstArgType).getJavaClass()))
          hasThisArgument = true;

        if (hasThisArgument) {
          _thisArgument = (Expression) _arguments.elementAt(0);
          _arguments.remove(0);
          nArgs--;
          if (firstArgType instanceof ObjectType) {
            _className = ((ObjectType) firstArgType).getJavaClassName();
          } else throw new TypeCheckError(ErrorMsg.NO_JAVA_FUNCT_THIS_REF, name);
        }
      } else if (_className.length() == 0) {
        /*
         * Warn user if external function could not be resolved.
         * Warning will _NOT_ be issued is the call is properly
         * wrapped in an <xsl:if> or <xsl:when> element. For details
         * see If.parserContents() and When.parserContents()
         */
        final Parser parser = getParser();
        if (parser != null) {
          reportWarning(this, parser, ErrorMsg.FUNCTION_RESOLVE_ERR, _fname.toString());
        }
        unresolvedExternal = true;
        return _type = Type.Int; // use "Int" as "unknown"
      }
    }

    final Vector methods = findMethods();

    if (methods == null) {
      // Method not found in this class
      throw new TypeCheckError(ErrorMsg.METHOD_NOT_FOUND_ERR, _className + "." + name);
    }

    Class extType = null;
    final int nMethods = methods.size();
    final Vector argsType = typeCheckArgs(stable);

    // Try all methods to identify the best fit
    int bestMethodDistance = Integer.MAX_VALUE;
    _type = null; // reset internal type
    for (int j, i = 0; i < nMethods; i++) {
      // Check if all paramteters to this method can be converted
      final Method method = (Method) methods.elementAt(i);
      final Class[] paramTypes = method.getParameterTypes();

      int currMethodDistance = 0;
      for (j = 0; j < nArgs; j++) {
        // Convert from internal (translet) type to external (Java) type
        extType = paramTypes[j];
        final Type intType = (Type) argsType.elementAt(j);
        Object match = _internal2Java.maps(intType, extType);
        if (match != null) {
          currMethodDistance += ((JavaType) match).distance;
        } else {
          // no mapping available
          //
          // Allow a Reference type to match any external (Java) type at
          // the moment. The real type checking is performed at runtime.
          if (intType instanceof ReferenceType) {
            currMethodDistance += 1;
          } else if (intType instanceof ObjectType) {
            ObjectType object = (ObjectType) intType;
            if (extType.getName().equals(object.getJavaClassName())) currMethodDistance += 0;
            else if (extType.isAssignableFrom(object.getJavaClass())) currMethodDistance += 1;
            else {
              currMethodDistance = Integer.MAX_VALUE;
              break;
            }
          } else {
            currMethodDistance = Integer.MAX_VALUE;
            break;
          }
        }
      }

      if (j == nArgs) {
        // Check if the return type can be converted
        extType = method.getReturnType();

        _type = (Type) _java2Internal.get(extType);
        if (_type == null) {
          _type = Type.newObjectType(extType);
        }

        // Use this method if all parameters & return type match
        if (_type != null && currMethodDistance < bestMethodDistance) {
          _chosenMethod = method;
          bestMethodDistance = currMethodDistance;
        }
      }
    }

    // It is an error if the chosen method is an instance menthod but we don't
    // have a this argument.
    if (_chosenMethod != null
        && _thisArgument == null
        && !Modifier.isStatic(_chosenMethod.getModifiers())) {
      throw new TypeCheckError(ErrorMsg.NO_JAVA_FUNCT_THIS_REF, getMethodSignature(argsType));
    }

    if (_type != null) {
      if (_type == Type.NodeSet) {
        getXSLTC().setMultiDocument(true);
      }
      return _type;
    }

    throw new TypeCheckError(ErrorMsg.ARGUMENT_CONVERSION_ERR, getMethodSignature(argsType));
  }
 public String getName() {
   return (_fname.toString());
 }