Package net.sf.saxon.xpath

Examples of net.sf.saxon.xpath.StaticError


            reqClass = getExternalJavaClass(uri);
            if (reqClass == null) {
                return null;
            }
        } catch (Exception err) {
            throw new StaticError("Cannot load external Java class", err);
        }

        if (debug) {
            diag.println("Looking for method " + local + " in Java class " + reqClass);
            diag.println("Number of actual arguments = " + staticArgs.length);
        }

        int numArgs = staticArgs.length;
        int significantArgs;

        Class theClass = reqClass;

        // if the method name is "new", look for a matching constructor

        if ("new".equals(local)) {

            if (debug) {
                diag.println("Looking for a constructor");
            }

            int mod = theClass.getModifiers();
            if (Modifier.isAbstract(mod)) {
                theException = new StaticError("Class " + theClass + " is abstract");
            } else if (Modifier.isInterface(mod)) {
                theException = new StaticError(theClass + " is an interface");
            } else if (Modifier.isPrivate(mod)) {
                theException = new StaticError("Class " + theClass + " is private");
            } else if (Modifier.isProtected(mod)) {
                theException = new StaticError("Class " + theClass + " is protected");
            }

            if (theException != null) {
                if (debug) {
                    diag.println("Cannot construct an instance: " + theException.getMessage());
                }
                return null;
            }

            Constructor[] constructors = theClass.getConstructors();
            for (int c = 0; c < constructors.length; c++) {
                Constructor theConstructor = constructors[c];
                if (debug) {
                    diag.println("Found a constructor with " + theConstructor.getParameterTypes().length + " arguments");
                }
                if (theConstructor.getParameterTypes().length == numArgs) {
                    candidateMethods.add(theConstructor);
                }
            }
            if (candidateMethods.size() == 0) {
                theException = new StaticError("No constructor with " + numArgs +
                        (numArgs == 1 ? " parameter" : " parameters") +
                        " found in class " + theClass.getName());
                if (debug) {
                    diag.println(theException.getMessage());
                }
                return null;
            }
        } else {

            // convert any hyphens in the name, camelCasing the following character

            String name = toCamelCase(local, debug, diag);

            // look through the methods of this class to find one that matches the local name

            Method[] methods = theClass.getMethods();
            boolean consistentReturnType = true;
            for (int m = 0; m < methods.length; m++) {

                Method theMethod = methods[m];

                if (debug) {
                    if (theMethod.getName().equals(name)) {
                        diag.println("Trying method " + theMethod.getName() + ": name matches");
                        if (!Modifier.isPublic(theMethod.getModifiers())) {
                            diag.println(" -- but the method is not public");
                        }
                    } else {
                        diag.println("Trying method " + theMethod.getName() + ": name does not match");
                    }
                }

                if (theMethod.getName().equals(name) &&
                        Modifier.isPublic(theMethod.getModifiers())) {

                    if (consistentReturnType) {
                        if (resultClass == null) {
                            resultClass = theMethod.getReturnType();
                        } else {
                            consistentReturnType =
                                    (theMethod.getReturnType() == resultClass);
                        }
                    }
                    Class[] theParameterTypes = theMethod.getParameterTypes();
                    boolean isStatic = Modifier.isStatic(theMethod.getModifiers());

                    // if the method is not static, the first supplied argument is the instance, so
                    // discount it

                    if (debug) {
                        diag.println("Method is " + (isStatic ? "" : "not ") + "static");
                    }

                    significantArgs = (isStatic ? numArgs : numArgs - 1);

                    if (significantArgs >= 0) {

                        if (debug) {
                            diag.println("Method has " + theParameterTypes.length + " argument" +
                                    (theParameterTypes.length == 1 ? "" : "s") +
                                    "; expecting " + significantArgs);
                        }

                        if (theParameterTypes.length == significantArgs &&
                                (significantArgs == 0 || theParameterTypes[0] != XPathContext.class)) {
                            if (debug) {
                                diag.println("Found a candidate method:");
                                diag.println("    " + theMethod);
                            }
                            candidateMethods.add(theMethod);
                        }

                        // we allow the method to have an extra parameter if the first parameter is XPathContext

                        if (theParameterTypes.length == significantArgs + 1 &&
                                theParameterTypes[0] == XPathContext.class) {
                            if (debug) {
                                diag.println("Method is a candidate because first argument is XPathContext");
                            }
                            candidateMethods.add(theMethod);
                        }
                    }
                }
            }

            // Code added by GS -- start

            // look through the fields of this class to find those that matches the local name

            Field[] fields = theClass.getFields();
            for (int m = 0; m < fields.length; m++) {

                Field theField = fields[m];

                if (debug) {
                    if (theField.getName().equals(name)) {
                        diag.println("Trying field " + theField.getName() + ": name matches");
                        if (!Modifier.isPublic(theField.getModifiers())) {
                            diag.println(" -- but the field is not public");
                        }
                    } else {
                        diag.println("Trying field " + theField.getName() + ": name does not match");
                    }
                }

                if (theField.getName().equals(name) &&
                        Modifier.isPublic(theField.getModifiers())) {
                    if (consistentReturnType) {
                        if (resultClass == null) {
                            resultClass = theField.getType();
                        } else {
                            consistentReturnType =
                                    (theField.getType() == resultClass);
                        }
                    }
                    boolean isStatic = Modifier.isStatic(theField.getModifiers());

                    // if the field is not static, the first supplied argument is the instance, so
                    // discount it

                    if (debug) {
                        diag.println("Field is " + (isStatic ? "" : "not ") + "static");
                    }

                    significantArgs = (isStatic ? numArgs : numArgs - 1);

                    if (significantArgs == 0) {
                        if (debug) {
                            diag.println("Found a candidate field:");
                            diag.println("    " + theField);
                        }
                        candidateMethods.add(theField);
                    }
                }
            }

            // End of code added by GS

            // No method found?

            if (candidateMethods.size() == 0) {
                theException = new StaticError("No method or field matching " + name +
                        " with " + numArgs +
                        (numArgs == 1 ? " parameter" : " parameters") +
                        " found in class " + theClass.getName());
                if (debug) {
                    diag.println(theException.getMessage());
View Full Code Here


public class FormatDate extends SystemFunction implements XSLTFunction {

    public void checkArguments(StaticContext env) throws XPathException {
        int numArgs = argument.length;
        if (numArgs != 2 && numArgs != 5) {
            throw new StaticError("Function " + getDisplayName(env.getNamePool()) +
                    " must have either two or five arguments",
                    ExpressionTool.getLocator(this));
        }
        super.checkArguments(env);
    }
View Full Code Here

            // We require the output format name to be known at compile time. If we allowed it
            // to be dynamic, we would not only need to save the namespace context, but also to
            // save details of all xsl:output declarations for use at run-time.

            if (!(argument[1] instanceof StringValue)) {
                throw new StaticError("Second argument of saxon:serialize must be known at compile time");
            }
            String format = ((StringValue)argument[1]).getStringValue();
            int fingerprint = -1;
            if (!format.equals("")) {
                fingerprint = ((ExpressionContext)env).getFingerprint(format, false);
                if (fingerprint==-1) {
                    throw new StaticError("Output format '" + format + "' has not been defined");
                }
            } try {
                outputProperties = ((ExpressionContext)env).getXSLStylesheet().gatherOutputProperties(fingerprint);
            } catch (TransformerConfigurationException err) {
                throw new StaticError(err);
            }
        } else {
            // we're not in XSLT: treat the second argument as the method property, default the rest
            outputProperties = new Properties();
            if (!(argument[1] instanceof StringValue)) {
                throw new StaticError("Second argument of saxon:serialize must be known at compile time");
            }
            outputProperties.setProperty(OutputKeys.METHOD, ((StringValue)argument[1]).getStringValue());
        }
    }
View Full Code Here

            throws XPathException {
         if (uri.equals(NamespaceConstant.SCHEMA) || uri.equals(NamespaceConstant.SCHEMA_DATATYPES)
                || uri.equals(NamespaceConstant.XDT)) {
            // it's a constructor function: treat it as shorthand for a cast expression
            if (arguments.length != 1) {
                throw new StaticError("A constructor function must have exactly one argument");
            }
            AtomicType type = (AtomicType)Type.getBuiltInItemType(uri, localName);
            if (type==null) {
                return new ErrorExpression(
                            new DynamicError("Unknown constructor function: " + localName));
View Full Code Here

            String value = ((AtomicValue)select).getStringValue();
            try {

                schemaType.validateContent(value, DummyNamespaceResolver.getInstance());
            } catch (ValidationException err) {
                throw new StaticError("Attribute value " + Err.wrap(value, Err.VALUE) +
                                               " does not the match the required type " +
                                               schemaType.getDescription() + ". " +
                                               err.getMessage());
            }
View Full Code Here

                try {
                    String[] parts = Name.getQNameParts(qname);
                    dfLocalName = parts[1];
                    dfURI = env.getURIForPrefix(parts[0]);
                } catch (QNameException e) {
                    throw new StaticError("Invalid decimal format name. " + e.getMessage());
                }

                DecimalFormatManager dfm = ((ExpressionContext)env).getXSLStylesheet().getDecimalFormatManager();
                requireFixup = true;
                dfm.registerUsage(dfURI, dfLocalName, this);
View Full Code Here

        argument[1] = ExpressionTool.unsorted(argument[1], false);
        if (argument[0] instanceof StringValue) {
            // common case, key name is supplied as a constant
            keyFingerprint = ((ExpressionContext)env).getFingerprint(((StringValue)argument[0]).getStringValue(), false);
            if (keyFingerprint==-1) {
                throw new StaticError("Key " + ((StringValue)argument[0]).getStringValue() + " is not defined");
            }
        } else {
            // we need to save the namespace context
            nsContext = env.getNamespaceResolver();
        }
View Full Code Here

    */

    public String getURIForPrefix(String prefix) throws XPathException {
      String uri = checkURIForPrefix(prefix);
      if (uri==null) {
        throw new StaticError("Prefix " + prefix + " has not been declared");
      }
      return uri;
    }
View Full Code Here

     */

    public void declareDefaultCollation(String name) throws XPathException {
        Comparator c = getCollation(name);
        if (c==null) {
            throw new StaticError("Collation " + name + " is not recognized");
        }
        defaultCollationName = name;
    }
View Full Code Here

    public void declareVariable(VariableDeclaration var) throws StaticError {
        int key = var.getNameCode();
        Integer keyObj = new Integer(key);
        if (variables.get(keyObj) != null) {
            throw new StaticError(
                    "Duplicate definition of global variable " + var.getVariableName());
        }
        variables.put(keyObj, var);
        variableList.add(var);
    }
View Full Code Here

TOP

Related Classes of net.sf.saxon.xpath.StaticError

Copyright © 2018 www.massapicom. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.