Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD

## com.trolltech.qt.xmlpatterns
Class QXmlQuery

    java.lang.Object
      com.trolltech.qt.QSignalEmitter
          com.trolltech.qt.QtJambiObject
              com.trolltech.qt.xmlpatterns.QXmlQuery

All Implemented Interfaces:
  QtJambiInterface

    public class QXmlQuery

extends QtJambiObject

The `QXmlQuery` class performs `XQueries` on XML data, or on non-XML data modeled to look like XML. The `QXmlQuery` class compiles and executes queries written in the `XQuery` language. `QXmlQuery` is typically used to query XML data, but it can also query non-XML data that has been modeled to look like XML.

Using `QXmlQuery` to query XML data, as in the snippet below, is simple because it can use the built-in `XML data model` as its delegate to the underlying query engine for traversing the data. The built-in data model is specified in `XQuery` 1.0 and `XPath` 2.0 Data Model.

      QXmlQuery query = new QXmlQuery();
      query.setQuery("doc('index.html')/html/body/p[1]");

      QXmlSerializerPointer serializer(new QXmlSerializer(query, myOutputDevice));
      query.evaluateToReceiver(serializer);

The example uses `QXmlQuery` to match the first paragraph of an XML document and then `output the result` to a device as XML.

Using `QXmlQuery` to query non-XML data requires writing a subclass of `QAbstractXmlNodeModel` to use as a replacement for the built-in XML data model. The custom data model will be able to traverse the non-XML data as required by the `QAbstractXmlNodeModel` interface. An instance of this custom data model then becomes the delegate used by the query engine to traverse the non-XML data. For an example of how to use `QXmlQuery` to query non-XML data, see the documentation for `QAbstractXmlNodeModel`.

## Running Queries

To run a query set up with `QXmlQuery`, call one of the evaluation functions.

- `evaluateTo()` is called with a pointer to an XML `receiver`, which receives the query results as a sequence of callbacks. The receiver callback class is like the callback class used for translating the output of a SAX parser. `QXmlSerializer`, for example, is a receiver callback class for translating the sequence of callbacks for output as XML text.

- `evaluateTo()` is called with a pointer to an iterator for an empty sequence of query `result items`. The Java-like iterator allows the query results to be accessed sequentially.

- `evaluateTo()` is similar to `evaluateTo()`, but the query must evaluate to a sequence of strings.

## Binding A Query To A Starting Node

When a query is run on XML data, as in the snippet above, the doc() function returns the node in the built-in data model where the query evaluation will begin. But when a query is run on a custom node model representing non-XML data, one of the `bindVariable()` functions must be called to bind a variable name to a starting node. $variable name can then be used in the written query to access the starting node in the custom data model. It is not necessary to declare the variable name external in the query. See the example in the documentation for `QAbstractXmlNodeModel`.

## Reentrancy and Thread-Safety

`QXmlQuery` is reentrant but not thread-safe. It is safe to use the `QxmlQuery` copy constructor to create a copy of a query and run the same query multiple times. Behind the scenes, `QXmlQuery` will reuse resources such as opened files and compiled queries to the extent possible. But it is not safe to use the same instance of `QXmlQuery` in multiple threads.

## Error Handling

Errors can occur during query evaluation. Examples include type errors and file loading errors. When an error occurs:

- The error message is sent to the `messageHandler()`.
- `QXmlResultItems::hasError()` will return `true`, or `evaluateTo()` will return `false`;
- The results of the evaluation function called are undefined.

## Resource Management

A query can create `query result items`, open documents, and allocate other resources. All resources created, opened, or allocated by `QtPatterns` are normally managed and deallocated automatically, when they are no longer needed. If it should become necessary to deallocate resources manually, ensure that the relevant instances of `QXmlQuery`, `QAbstractXmlReceiver`, and QAbstractXmlForwardIterator have been destroyed.

An instance of `QCoreApplication` must exist before `QXmlQuery` can be used.

Nested Class Summary

Nested classes/interfaces inherited from class com.trolltech.qt.QSignalEmitter
`QSignalEmitter.Signal0, QSignalEmitter.Signal1, QSignalEmitter.Signal2, QSignalEmitter.Signal3, QSignalEmitter.Signal4, QSignalEmitter.Signal5, QSignalEmitter.Signal6, QSignalEmitter.Signal7, QSignalEmitter.Signal8, QSignalEmitter.Signal9`

Constructor Summary
`QXmlQuery()` Constructs an empty query.
`QXmlQuery(QXmlNamePool np)` Constructs a query that will use `np` as its name pool.
`QXmlQuery(QXmlQuery other)` Constructs a `QXmlQuery` that is a copy of `other`.

Method Summary
`void` | `bindVariable(QXmlName name, QIODevice arg__2)` Binds the variable `name` to the `device` so that $`name` can be used from within the query to refer to the `device`.
`void` | `bindVariable(QXmlName name, QXmlItem value)` Binds the variable `name` to the `value` so that $`name` can be used from within the query to refer to the `value`.
`void` | `bindVariable(java.lang.String localName, QIODevice arg__2)` If `localName` is a valid `NCName`, this function is equivalent to the following snippet.
`void` | `bindVariable(java.lang.String localName, QXmlItem value)` This function constructs a `QXmlName` from `localName` using the query's `namespace`.
`void` | `evaluateTo(QXmlResultItems result)` Starts the evaluation and makes it available in `result`.
`static QXmlQuery` | `fromNativePointer(QNativePointer nativePointer)` This method returns the QXmlQuery instance pointed to by `nativePointer`.
`boolean` | `isValid()` Returns true if this query is valid.
`QAbstractMessageHandler` | `messageHandler()` Returns the message handler that handles compile and runtime messages for this `QXmlQuery`.
`QXmlNamePool` | `namePool()` Returns the name pool used by this `QXmlQuery` for constructing `names`.
`QXmlQuery` | `operator_assign(QXmlQuery other)` Assigns `other` to this `QXmlQuery` instance.
`void` | `setFocus(QXmlItem item)` Sets the focus to `item`.
`void` | `setMessageHandler(QAbstractMessageHandler messageHandler)` Changes the `message handler` for this `QXmlQuery` to `aMessageHandler`.
`void` | `setQuery(QIODevice sourceCode)` Sets this `QXmlQuery` to an `XQuery` read from the `sourceCode` device.
`void` | `setQuery(QIODevice sourceCode, QUrl documentURI)` Sets this `QXmlQuery` to an `XQuery` read from the `sourceCode` device.
`void` | `setQuery(QUrl queryURI)` Sets this `QXmlQuery` to the XQuery read from the `queryURI`.
`void` | `setQuery(QUrl queryURI, QUrl baseURI)` Sets this `QXmlQuery` to the XQuery read from the `queryURI`.
`void` | `setQuery(java.lang.String sourceCode)` The behavior and requirements of this function are the same as for setQuery(`QIODevice`*, const `QUrl`&), after the XQuery has been read from the IO device into a string.
`void` | `setQuery(java.lang.String sourceCode, QUrl documentURI)` The behavior and requirements of this function are the same as for setQuery(`QIODevice`*, const `QUrl`&), after the XQuery has been read from the IO device into a string.
`void` | `setUriResolver(QAbstractUriResolver resolver)` Sets the URI resolver to `resolver`.
`QAbstractUriResolver` | `uriResolver()` Returns the query's URI resolver.

Methods inherited from class com.trolltech.qt.QtJambiObject
`dispose, disposed, equals, finalize, reassignNativeResources, tr, tr, tr`

Methods inherited from class com.trolltech.qt.QSignalEmitter
`blockSignals, disconnect, disconnect, signalsBlocked, signalSender, thread`

Methods inherited from class java.lang.Object
`clone, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait`

Methods inherited from interface com.trolltech.qt.QtJambiInterface
`disableGarbageCollection, nativeId, nativePointer, reenableGarbageCollection, setJavaOwnership`

Constructor Detail

### QXmlQuery

    public QXmlQuery()

  Constructs an empty query. A default constructed query cannot be evaluated until `setQuery()` has been called.

### QXmlQuery

    public QXmlQuery(QXmlNamePool np)

  Constructs a query that will use `np` as its name pool. The query cannot be evaluated until `setQuery()` has been called.

### QXmlQuery

    public QXmlQuery(QXmlQuery other)

  Constructs a `QXmlQuery` that is a copy of `other`.

Method Detail

### bindVariable

    public final void bindVariable(java.lang.String localName,
                                   QIODevice arg__2)

  If `localName` is a valid `NCName`, this function is equivalent to the following snippet.

          query.bindVariable(new QXmlName(query.namePool(), localName), device);

  A `QXmlName` is constructed from `localName`, and is passed to the appropriate overload along with `device`.

See also:
  `QXmlName::isNCName()`.

### bindVariable

    public final void bindVariable(java.lang.String localName,
                                   QXmlItem value)

  This function constructs a `QXmlName` from `localName` using the query's `namespace`. The function then behaves as the overloaded function. It is equivalent to the following snippet.

          query.bindVariable(new QXmlName(query.namePool(), localName), value);

### bindVariable

    public final void bindVariable(QXmlName name,
                                   QIODevice arg__2)

  Binds the variable `name` to the `device` so that $`name` can be used from within the query to refer to the `device`. The `QIODevice` `device` is exposed to the query as a URI of type `xs:anyURI`, which can be passed to the `fn:doc()` function to be read. e.g., this function can be used to pass an XML document in memory to `fn:doc`.

          QByteArray myDocument = new QByteArray();
          QBuffer buffer = new QBuffer(myDocument); // This is a QIODevice.
          buffer.open(QIODevice.OpenModeFlag.ReadOnly);
          QXmlQuery query = new QXmlQuery();
          query.bindVariable("myDocument", buffer);
          query.setQuery("declare variable $myDocument external; doc($myDocument)");

  The caller must ensure that `device` has been opened with at least `QIODevice::ReadOnly` prior to this binding. Otherwise, behavior is undefined.

  If the query will access an XML document contained in a QString, use a `QBuffer` as shown in the following snippet. Suppose myQString contains content
  The following code example is written in c++.

          QBuffer device;
          device.setData(myQString.toUtf8());
          device.open(QIODevice::ReadOnly));

          QXmlQuery query;
          query.setQuery("declare variable $inputDocument external;"
                          "doc($inputDocument)/query[theDocument]");
          query.bindVariable("inputDocument", &device);

  `name` must not be null. `name`.isNull() must return false. If `name` has already been bound, its previous binding will be overriden. The URI that `name` evaluates to is arbitrary and may change.

  `device` must not be deleted while this `QXmlQuery` exists.

### bindVariable

    public final void bindVariable(QXmlName name,
                                   QXmlItem value)

  Binds the variable `name` to the `value` so that $`name` can be used from within the query to refer to the `value`.

  `name` must not be null. `name`.isNull() must return false. If `name` has already been bound by a previous `bindVariable()` call, its previous binding will be overriden.

  If `value` is null so that `value`.isNull() returns true, and `name` already has a binding, the effect is to remove the existing binding for `name`.

  To bind a value of type QString or `QUrl`, wrap the value in a `QVariant` such that `QXmlItem`'s `QVariant` constructor is called.

  All strings processed by the query must be valid `XQuery` strings, which means they must contain only only XML 1.0 characters. However, this requirement is not checked. If the query processes an invalid string, the behavior is undefined.

See also:
  QVariant::isValid(), How QVariant maps to XQuery's Data Model, and `QXmlItem::isNull()`.

### evaluateTo

    public final void evaluateTo(QXmlResultItems result)

  Starts the evaluation and makes it available in `result`. If `result` is null, the behavior is undefined. The evaluation takes place incrementally (lazy evaluation), as the caller uses `QXmlResultItems::next()` to get the enxt result.

See also:
  `QXmlResultItems::next()`.

### isValid

    public final boolean isValid()

  Returns true if this query is valid. Examples of invalid queries are ones that contain syntax errors or that have not had `setQuery()` called for them yet.

### messageHandler

    public final QAbstractMessageHandler messageHandler()

  Returns the message handler that handles compile and runtime messages for this `QXmlQuery`.

See also:
  `setMessageHandler()`.

### namePool

    public final QXmlNamePool namePool()

  Returns the name pool used by this `QXmlQuery` for constructing `names`. There is no setter for the name pool, because mixing name pools causes errors due to name confusion.

### operator_assign

    public final QXmlQuery operator_assign(QXmlQuery other)

  Assigns `other` to this `QXmlQuery` instance.

### setFocus

    public final void setFocus(QXmlItem item)

  Sets the focus to `item`. The focus is the set of items that the context item expression and path expressions navigate from. For example, in the expression p/span, the element that p evaluates to is the focus for the following expression, span.

  The focus can be accessed using the context item expression, dot, ".".

  By default, the focus is not set but is undefined. It will therefore result in a dynamic error, `XPDY0002`, if the query is evaluated without a focus. In fact, the focus must be set before the query is set with `setQuery()`.

### setMessageHandler

    public final void setMessageHandler(QAbstractMessageHandler messageHandler)

  Changes the `message handler` for this `QXmlQuery` to `aMessageHandler`. The query sends all compile and runtime messages to this message handler. `QXmlQuery` does not take ownership of `aMessageHandler`.

  Normally, the default message handler is sufficient. It writes compile and runtime messages to stderr. The default message handler includes color codes if stderr can render colors.

  Note that changing the message handler after the query has been compiled has no effect, ie the query uses the same message handler at runtime that it uses at compile time.

  When `QXmlQuery` calls `QAbstractMessageHandler::message()`, the arguments are as follows:

  `message()` argument | Semantics
  --- | ---
  `QtMsgType` type | Only `QtWarningMsg` and `QtFatalMsg` are used. The former identifies a compile or runtime warning, while the latter identifies a dynamic or static error.
  const QString & description | An XHTML document which is the actual message. It is translated into the current language.
  const `QUrl` &identifier | Identifies the error with a URI, where the fragment is the error code, and the rest of the URI is the error namespace.
  const `QSourceLocation` & `sourceLocation` | Identifies where the error occurred.

See also:
  `messageHandler()`.

### setQuery

    public final void setQuery(QIODevice sourceCode)

  Sets this `QXmlQuery` to an `XQuery` read from the `sourceCode` device. The device must have been opened with at least `QIODevice::ReadOnly` .

  `documentURI` represents the query obtained from the `sourceCode` device. It is the Base URI of the static context, as defined in the `XQuery` language. It is used internally to resolve relative URIs that appear in the query, and for message reporting. `documentURI` can be empty. If it is empty, the `application file path` is used. If it is not empty, it may be either relative or absolute. If it is relative, it is resolved itself against the `application file path` before it is used. If `documentURI` is neither a valid URI nor empty, the result is undefined.

  If the query contains a static error (e.g. syntax error), an error message is sent to the `messageHandler()`, and `isValid()` will return false.

  Variables must be bound before `setQuery()` is called.

  The encoding of the `XQuery` in `sourceCode` is detected internally using the rules for setting and detecting encoding of `XQuery` files, which are explained in the `XQuery` language.

See also:
  `isValid()`.

### setQuery

    public final void setQuery(QIODevice sourceCode,
                               QUrl documentURI)

  Sets this `QXmlQuery` to an `XQuery` read from the `sourceCode` device. The device must have been opened with at least `QIODevice::ReadOnly` .

  `documentURI` represents the query obtained from the `sourceCode` device. It is the Base URI of the static context, as defined in the `XQuery` language. It is used internally to resolve relative URIs that appear in the query, and for message reporting. `documentURI` can be empty. If it is empty, the `application file path` is used. If it is not empty, it may be either relative or absolute. If it is relative, it is resolved itself against the `application file path` before it is used. If `documentURI` is neither a valid URI nor empty, the result is undefined.

  If the query contains a static error (e.g. syntax error), an error message is sent to the `messageHandler()`, and `isValid()` will return false.

  Variables must be bound before `setQuery()` is called.

  The encoding of the `XQuery` in `sourceCode` is detected internally using the rules for setting and detecting encoding of `XQuery` files, which are explained in the `XQuery` language.

See also:
  `isValid()`.

### setQuery

    public final void setQuery(java.lang.String sourceCode)

  The behavior and requirements of this function are the same as for setQuery(`QIODevice`*, const `QUrl`&), after the XQuery has been read from the IO device into a string. Because `sourceCode` is already a Unicode string, detection of its encoding is unnecessary.

### setQuery

    public final void setQuery(java.lang.String sourceCode,
                               QUrl documentURI)

  The behavior and requirements of this function are the same as for setQuery(`QIODevice`*, const `QUrl`&), after the XQuery has been read from the IO device into a string. Because `sourceCode` is already a Unicode string, detection of its encoding is unnecessary.

### setQuery

    public final void setQuery(QUrl queryURI)

  Sets this `QXmlQuery` to the XQuery read from the `queryURI`. Use `isValid()` after calling this function. If an error occurred reading `queryURI`, e.g., the query does not exist, cannot be read, or is invalid, `isValid()` will return false.

  The supported URI schemes are the same as those in the XQuery function `fn:doc`, except that queryURI can be the object of a variable binding.

  `baseURI` is the Base URI of the static context, as defined in the XQuery language. It is used internally to resolve relative URIs that appear in the query, and for message reporting. If `baseURI` is empty, `queryURI` is used. Otherwise, `baseURI` is used, and it is resolved against the `application file path` if it is relative.

  If `queryURI` is empty or invalid, or if `baseURI` is invalid, the behavior of this function is undefined.

### setQuery

    public final void setQuery(QUrl queryURI,
                               QUrl baseURI)

  Sets this `QXmlQuery` to the XQuery read from the `queryURI`. Use `isValid()` after calling this function. If an error occurred reading `queryURI`, e.g., the query does not exist, cannot be read, or is invalid, `isValid()` will return false.

  The supported URI schemes are the same as those in the XQuery function `fn:doc`, except that queryURI can be the object of a variable binding.

  `baseURI` is the Base URI of the static context, as defined in the XQuery language. It is used internally to resolve relative URIs that appear in the query, and for message reporting. If `baseURI` is empty, `queryURI` is used. Otherwise, `baseURI` is used, and it is resolved against the `application file path` if it is relative.

  If `queryURI` is empty or invalid, or if `baseURI` is invalid, the behavior of this function is undefined.

### setUriResolver

    public final void setUriResolver(QAbstractUriResolver resolver)

  Sets the URI resolver to `resolver`. `QXmlQuery` does not take ownership of `resolver`.

See also:
  `uriResolver()`.

### uriResolver

    public final QAbstractUriResolver uriResolver()

  Returns the query's URI resolver. If no URI resolver has been set, QtXmlPatterns will use the URIs in queries as they are.

  The URI resolver provides a level of abstraction, or polymorphic URIs. A resolver can rewrite logical URIs to physical ones, or it can translate obsolete or invalid URIs to valid ones.

  QtXmlPatterns calls the URI resolver for all URIs it encounters, except for namespaces. Specifically, all builtin functions that deal with URIs (`fn:doc()`, and `fn:doc-available()`).

  In the case of `fn:doc()`, the absolute URI is the base URI in the static context (which most likely is the location of the query). Rather than use the URI the user specified, the return value of `QAbstractUriResolver::resolve()` will be used.

  When QtXmlPatterns calls `QAbstractUriResolver::resolve()` the absolute URI is the URI mandated by the `XQuery` language, and the relative URI is the URI specified by the user.

See also:
  `setUriResolver()`.

### fromNativePointer

    public static QXmlQuery fromNativePointer(QNativePointer nativePointer)

  This method returns the QXmlQuery instance pointed to by `nativePointer`.

Overview | Package | Class | Tree | Deprecated | Index | Help
PREV CLASS NEXT CLASS | FRAMES NO FRAMES
SUMMARY: NESTED | FIELD | CONSTR | METHOD | DETAIL: FIELD | CONSTR | METHOD