Package org.pdf4j.saxon.trans

Examples of org.pdf4j.saxon.trans.XPathException


//                        } else
                        if (currentToken == Token.REPLACE_VALUE) {
                            // this one's a quadruplet - "replace value of node"
                            lookAhead();
                            if (nextToken != Token.NAME || !nextTokenValue.equals("of")) {
                                throw new XPathException("After '" + composite + "', expected 'of'");
                            }
                            lookAhead();
                            if (nextToken != Token.NAME || !nextTokenValue.equals("node")) {
                                throw new XPathException("After 'replace value of', expected 'node'");
                            }
                            nextToken = currentToken;   // to reestablish after-operator state
                        }
                        lookAhead();
                        return;
View Full Code Here


                        nextToken = Token.ASSIGN;
                        inputOffset++;
                        return;
                    }
              }
              throw new XPathException("Unexpected colon at start of token");
            case '@':
              nextToken = Token.AT;
              return;
          case '?':
              nextToken = Token.QMARK;
              return;
            case '[':
              nextToken = Token.LSQB;
              return;
            case ']':
              nextToken = Token.RSQB;
              return;
            case '{':
              nextToken = Token.LCURLY;
              return;
            case '}':
              nextToken = Token.RCURLY;
              return;
            case ';':
                nextToken = Token.SEMICOLON;
                state = DEFAULT_STATE;
                return;
            case '(':
                if (inputOffset < inputLength && input.charAt(inputOffset) == '#') {
                  inputOffset++;
                    int pragmaStart = inputOffset;
                    int nestingDepth = 1;
                    while (nestingDepth > 0 && inputOffset < (inputLength-1)) {
                        if (input.charAt(inputOffset) == '\n') {
                            incrementLineNumber();
                        } else if (input.charAt(inputOffset) == '#' &&
                               input.charAt(inputOffset+1) == ')') {
                            nestingDepth--;
                            inputOffset++;
                        } else if (input.charAt(inputOffset) == '(' &&
                               input.charAt(inputOffset+1) == '#') {
                            nestingDepth++;
                            inputOffset++;
                        }
                        inputOffset++;
                    }
                    if (nestingDepth > 0) {
                        throw new XPathException("Unclosed XQuery pragma");
                    }
                  nextToken = Token.PRAGMA;
                    nextTokenValue = input.substring(pragmaStart, inputOffset-2 );
                  return;
              }
              if (inputOffset < inputLength && input.charAt(inputOffset) == ':') {
                    // XPath comment syntax is (: .... :)
                    // Comments may be nested, and may now be empty
                    inputOffset++;
                    int nestingDepth = 1;
                    while (nestingDepth > 0 && inputOffset < (inputLength-1)) {
                        if (input.charAt(inputOffset) == '\n') {
                            incrementLineNumber();
                        } else if (input.charAt(inputOffset) == ':' &&
                                input.charAt(inputOffset+1) == ')') {
                            nestingDepth--;
                            inputOffset++;
                        } else if (input.charAt(inputOffset) == '(' &&
                               input.charAt(inputOffset+1) == ':') {
                            nestingDepth++;
                            inputOffset++;
                        }
                        inputOffset++;
                    }
                    if (nestingDepth > 0) {
                        throw new XPathException("Unclosed XPath comment");
                    }
                    lookAhead();
                } else {
                  nextToken = Token.LPAR;
              }
              return;
            case ')':
              nextToken = Token.RPAR;
              return;
            case '+':
              nextToken = Token.PLUS;
              return;
            case '-':
              nextToken = Token.MINUS;   // not detected if part of a name
              return;
            case '=':
              nextToken = Token.EQUALS;
              return;
            case '!':
              if (inputOffset < inputLength
                      && input.charAt(inputOffset) == '=') {
                  inputOffset++;
                  nextToken = Token.NE;
                  return;
              }
              throw new XPathException("'!' without '='");
            case '*':
                // disambiguation of MULT and STAR is now done later
                if (inputOffset < inputLength
                        && input.charAt(inputOffset) == ':') {
                    inputOffset++;
                    nextToken = Token.SUFFIX;
                    // we leave the parser to get the following name as a separate
                    // token, but first check there's no intervening white space or comments
                    if (inputOffset < inputLength) {
                        char ahead = input.charAt(inputOffset);
                        if (" \r\t\n(".indexOf(ahead) >= 0) {
                            throw new XPathException("Whitespace and comments are not allowed after '*:'");
                        }
                    }
                    return;
                }
                nextToken = Token.STAR;
              return;
            case ',':
              nextToken = Token.COMMA;
              return;
            case '$':
              nextToken = Token.DOLLAR;
              return;
            case '|':
              nextToken = Token.UNION;
              return;
            case '<':
              if (inputOffset < inputLength
                      && input.charAt(inputOffset) == '=') {
                  inputOffset++;
                  nextToken = Token.LE;
                  return;
              }
              if (inputOffset < inputLength
                      && input.charAt(inputOffset) == '<') {
                  inputOffset++;
                  nextToken = Token.PRECEDES;
                  return;
              }
              nextToken = Token.LT;
              return;
            case '>':
              if (inputOffset < inputLength
                      && input.charAt(inputOffset) == '=') {
                  inputOffset++;
                  nextToken = Token.GE;
                  return;
              }
              if (inputOffset < inputLength
                      && input.charAt(inputOffset) == '>') {
                  inputOffset++;
                  nextToken = Token.FOLLOWS;
                  return;
              }
              nextToken = Token.GT;
              return;
            case '.':
              if (inputOffset < inputLength
                      && input.charAt(inputOffset) == '.') {
                  inputOffset++;
                  nextToken = Token.DOTDOT;
                  return;
              }
              if (inputOffset == inputLength
                      || input.charAt(inputOffset) < '0'
                      || input.charAt(inputOffset) > '9') {
                  nextToken = Token.DOT;
                  return;
              }
                // otherwise drop through: we have a number starting with a decimal point
            case '0':
            case '1':
            case '2':
            case '3':
            case '4':
            case '5':
            case '6':
            case '7':
            case '8':
            case '9':
                // The logic here can return some tokens that are not legitimate numbers,
                // for example "23e" or "1.0e+". However, this will only happen if the XPath
                // expression as a whole is syntactically incorrect.
                // These errors will be caught by the numeric constructor.
                boolean allowE = true;
                boolean allowSign = false;
                boolean allowDot = true;
                boolean endOfNum = false;
            numloop:
                while (!endOfNum) {
                  switch (c) {
                        case '0': case '1': case '2': case '3': case '4':
                        case '5': case '6': case '7': case '8': case '9':
                            allowSign = false;
                            break;
                        case '.':
                            if (allowDot) {
                                allowDot = false;
                                allowSign = false;
                            } else {
                                inputOffset--;
                                break numloop;
                            }
                            break;
                        case 'E': case 'e':
                            if (allowE) {
                                allowSign = true;
                                allowE = false;
                            } else {
                                inputOffset--;
                                break numloop;
                            }
                            break;
                        case '+': case '-':
                            if (allowSign) {
                                allowSign = false;
                            } else {
                                inputOffset--;
                                break numloop;
                            }
                            break;
                        default:
                            if (('a' <= c && c <= 'z') || c>127) {
                                // this prevents the famous "10div 3"
                                throw new XPathException("Separator needed after numeric literal");
                            }
                            inputOffset--;
                            break numloop;
                    }
                    if (inputOffset >= inputLength) break;
                    c = input.charAt(inputOffset++);
              }
              nextTokenValue = input.substring(nextTokenStartOffset, inputOffset);
              nextToken = Token.NUMBER;
              return;
            case '"':
            case '\'':
                nextTokenValue = "";
                while (true) {
                  inputOffset = input.indexOf(c, inputOffset);
                  if (inputOffset < 0) {
                      inputOffset = nextTokenStartOffset + 1;
                      throw new XPathException("Unmatched quote in expression");
                  }
                  nextTokenValue += input.substring(nextTokenStartOffset + 1, inputOffset++);
                // look for doubled delimiters
              if (inputOffset < inputLength && input.charAt(inputOffset) == c) {
                      nextTokenValue += c;
                      nextTokenStartOffset = inputOffset;
                      inputOffset++;
                  } else {
                      break;
                  }
              }

                // maintain line number if there are newlines in the string
                if (nextTokenValue.indexOf('\n') >= 0) {
                    for (int i = 0; i<nextTokenValue.length(); i++) {
                        if (nextTokenValue.charAt(i) == '\n') {
                            lineNumber++;
                            if (newlineOffsets==null) {
                                newlineOffsets = new ArrayList(20);
                            }
                            newlineOffsets.add(new Integer(nextTokenStartOffset+i));
                        }
                    }
                }
              nextTokenValue = nextTokenValue.intern();
              nextToken = Token.STRING_LITERAL;
              return;
            case '\n':
                incrementLineNumber();
                // drop through
            case ' ':
            case '\t':
            case '\r':
              nextTokenStartOffset = inputOffset;
              break;
            default:
              if (c < 0x80 && !Character.isLetter(c)) {
                  throw new XPathException("Invalid character '" + c + "' in expression");
                }
                /* fall through */
            case '_':
            loop:
              for (;inputOffset < inputLength; inputOffset++) {
View Full Code Here

                list = new ArrayList(100);
            } else {
                try {
                    list = (Collection)targetClass.newInstance();
                } catch (InstantiationException e) {
                    XPathException de = new XPathException("Cannot instantiate collection class " + targetClass);
                    de.setXPathContext(context);
                    throw de;
                } catch (IllegalAccessException e) {
                    XPathException de = new XPathException("Cannot access collection class " + targetClass);
                    de.setXPathContext(context);
                    throw de;
                }
            }
            Configuration config = context.getConfiguration();
            TypeHierarchy th = config.getTypeHierarchy();
View Full Code Here

                if (targetClass.isAssignableFrom(val.getClass())) {
                    return val;
                } else if (val instanceof EmptySequence) {
                    return null;
                } else {
                    throw new XPathException("Cannot convert value " + val.getClass() + " of type " +
                            Value.asValue(value).getItemType(context.getConfiguration().getTypeHierarchy()) +
                            " to class " + targetClass.getName());
                }
            }
        }
View Full Code Here

        public static UnwrapExternalObject INSTANCE = new UnwrapExternalObject();

        public Object convert(ValueRepresentation value, Class targetClass, XPathContext context) throws XPathException {
            Value val = Value.asValue(value).reduce();
            if (!(val instanceof ObjectValue)) {
                throw new XPathException("Expected external object of class " + targetClass +
                        ", got " + val.getClass());
            }
            Object obj = ((ObjectValue)val).getObject();
            if (!targetClass.isAssignableFrom(obj.getClass())) {
                throw new XPathException("External object has wrong class (is "
                        + obj.getClass() + ", expected " + targetClass);
            }
            return obj;
        }
View Full Code Here

        public Object convert(ValueRepresentation value, Class targetClass, XPathContext context) throws XPathException {
            String str = value.getStringValue();
            if (str.length() == 1) {
                return new Character(str.charAt(0));
            } else {
                XPathException de = new XPathException("Cannot convert xs:string to Java char unless length is 1");
                de.setXPathContext(context);
                de.setErrorCode(SaxonErrorCode.SXJE0005);
                throw de;
            }
        }
View Full Code Here

        public Object convert(ValueRepresentation value, Class targetClass, XPathContext context) throws XPathException {
            try {
                return new URI(value.getStringValue());
            } catch (URISyntaxException err) {
                throw new XPathException("The anyURI value '" + value + "' is not an acceptable Java URI");
            }
        }
View Full Code Here

        public Object convert(ValueRepresentation value, Class targetClass, XPathContext context) throws XPathException {
            try {
                return new URL(value.getStringValue());
            } catch (MalformedURLException err) {
                throw new XPathException("The anyURI value '" + value + "' is not an acceptable Java URL");
            }
        }
View Full Code Here

        AtomicType t0 = operand0.getItemType(th).getAtomizedItemType();
        AtomicType t1 = operand1.getItemType(th).getAtomizedItemType();

        if (t0.isExternalType() || t1.isExternalType()) {
            XPathException err = new XPathException("Cannot perform comparisons involving external objects");
            err.setIsTypeError(true);
            err.setErrorCode("XPTY0004");
            err.setLocator(this);
            throw err;
        }

        BuiltInAtomicType p0 = (BuiltInAtomicType)t0.getPrimitiveItemType();
        if (p0.equals(BuiltInAtomicType.UNTYPED_ATOMIC)) {
            p0 = BuiltInAtomicType.STRING;
        }
        BuiltInAtomicType p1 = (BuiltInAtomicType)t1.getPrimitiveItemType();
        if (p1.equals(BuiltInAtomicType.UNTYPED_ATOMIC)) {
            p1 = BuiltInAtomicType.STRING;
        }

        //operand0MaybeUntyped = th.relationship(p0, BuiltInAtomicType.UNTYPED_ATOMIC) != TypeHierarchy.DISJOINT;
        //operand1MaybeUntyped = th.relationship(p1, BuiltInAtomicType.UNTYPED_ATOMIC) != TypeHierarchy.DISJOINT;

        needsRuntimeComparabilityCheck =
                p0.equals(BuiltInAtomicType.ANY_ATOMIC) || p1.equals(BuiltInAtomicType.ANY_ATOMIC);

        if (!Type.isComparable(p0, p1, Token.isOrderedOperator(operator))) {
            boolean opt0 = Cardinality.allowsZero(operand0.getCardinality());
            boolean opt1 = Cardinality.allowsZero(operand1.getCardinality());
            if (opt0 || opt1) {
                // This is a comparison such as (xs:integer? eq xs:date?). This is almost
                // certainly an error, but we need to let it through because it will work if
                // one of the operands is an empty sequence.

                // ORBEON: 2013-05-03: Don't issue warning if any of the operands is xs:untypedAtomic.
                //
                // - At compile time, we might not know the type. But at runtime, we might thanks to type annotations,
                //   and the comparison indeed can (and does) succeed in such cases. So the warning is not only annoying
                //   but incorrect (because the comparison can succeed even if the operands are not empty)!
                // - Note that the `opt0 || opt1` test above could also fail when the cardinality does not allow an
                //   empty sequence, and then we would incorrectly go to the error case at compile time. We don't seem
                //   to hit this however. When could we have cardinality information at compile time but not the actual
                //   type? Maybe with a function parameter like `element()?`?
                // - I am wondering, in the case where Saxon uses a schema (which we don't have as we are using the
                //   open source version), whether users wouldn't also get the incorrect warning, and even whether they
                //   could not incorrectly hit the error case. This because even with a schema, types can still be
                //   unknown at compile time.
                if (! t0.equals(BuiltInAtomicType.UNTYPED_ATOMIC) && ! t1.equals(BuiltInAtomicType.UNTYPED_ATOMIC)) {
                    String which = null;
                    if (opt0) which = "the first operand is";
                    if (opt1) which = "the second operand is";
                    if (opt0 && opt1) which = "one or both operands are";

                    visitor.getStaticContext().issueWarning("Comparison of " + t0.toString(namePool) +
                            (opt0 ? "?" : "") + " to " + t1.toString(namePool) +
                            (opt1 ? "?" : "") + " will fail unless " + which + " empty", this);
                }
                needsRuntimeComparabilityCheck = true;
            } else {
                XPathException err = new XPathException("Cannot compare " + t0.toString(namePool) +
                        " to " + t1.toString(namePool));
                err.setIsTypeError(true);
                err.setErrorCode("XPTY0004");
                err.setLocator(this);
                throw err;
            }
        }
        if (!(operator == Token.FEQ || operator == Token.FNE)) {
            if (!p0.isOrdered()) {
                XPathException err = new XPathException("Type " + t0.toString(env.getNamePool()) + " is not an ordered type");
                err.setErrorCode("XPTY0004");
                err.setIsTypeError(true);
                err.setLocator(this);
                throw err;
            }
            if (!p1.isOrdered()) {
                XPathException err = new XPathException("Type " + t1.toString(env.getNamePool()) + " is not an ordered type");
                err.setErrorCode("XPTY0004");
                err.setIsTypeError(true);
                err.setLocator(this);
                throw err;
            }
        }

        if (comparer == null) {
View Full Code Here

//            if (operand1MaybeUntyped && v1 instanceof UntypedAtomicValue) {
//                v1 = new StringValue(v1.getStringValueCS());
//            }
            if (needsRuntimeComparabilityCheck &&
                    !Type.isComparable(v0.getPrimitiveType(), v1.getPrimitiveType(), Token.isOrderedOperator(operator))) {
                XPathException e2 = new XPathException("Cannot compare " + Type.displayTypeName(v0) +
                    " to " + Type.displayTypeName(v1));
                e2.setErrorCode("XPTY0004");
                e2.setIsTypeError(true);
                throw e2;
            }
            return compare(v0, operator, v1, comparer.provideContext(context));
        } catch (XPathException e) {
            // re-throw the exception with location information added
            e.maybeSetLocation(this);
            e.maybeSetContext(context);
            throw e;
        } catch (ClassCastException err) {
            throw new XPathException("CCE", this);
        }
    }
View Full Code Here

TOP

Related Classes of org.pdf4j.saxon.trans.XPathException

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.