org.apache.derby.iapi.types
Class XML

java.lang.Object
  extended byorg.apache.derby.iapi.types.DataType
      extended byorg.apache.derby.iapi.types.XML
All Implemented Interfaces:
CloneableObject, DataValueDescriptor, java.io.Externalizable, Formatable, Orderable, java.io.Serializable, Storable, StreamStorable, TypedFormat, XMLDataValue

public class XML
extends DataType
implements XMLDataValue, StreamStorable

This type implements the XMLDataValue interface and thus is the type on which all XML related operations are executed. The first and simplest XML store implementation is a UTF-8 based one--all XML data is stored on disk as a UTF-8 string, just like the other Derby string types. In order to make it possible for smarter XML implementations to exist in the future, this class always writes an "XML implementation id" to disk before writing the rest of its data. When reading the data, the impl id is read first and serves as an indicator of how the rest of the data should be read. So long as there's only one implementation (UTF-8) the impl id can be ignored; but when smarter implementations are written, the impl id will be the key to figuring out how an XML value should be read, written, and processed.

See Also:
Serialized Form

Nested Class Summary
private  class XML.ExistsHandler
           
private  class XML.XMLErrorHandler
           
 
Field Summary
private static int BASE_MEMORY_USAGE
           
private static java.lang.String QUERY_MATCH_STRING
           
protected  org.xml.sax.XMLReader saxReader
           
private  org.apache.xalan.processor.TransformerFactoryImpl saxTFactory
           
protected static short UTF8_IMPL_ID
           
protected static java.lang.String XML_PARSER_CLASS
           
private  SQLChar xmlStringValue
           
private static java.lang.String XPATH_PLACEHOLDER
           
private  org.xml.sax.XMLReader xsltReader
           
private static java.lang.String xsltStylesheet
           
 
Fields inherited from interface org.apache.derby.iapi.types.Orderable
ORDER_OP_EQUALS, ORDER_OP_GREATEROREQUALS, ORDER_OP_GREATERTHAN, ORDER_OP_LESSOREQUALS, ORDER_OP_LESSTHAN
 
Constructor Summary
  XML()
          Default constructor.
private XML(SQLChar val)
          Private constructor used for the getClone() method.
 
Method Summary
 int compare(DataValueDescriptor other)
          Compare two XML DataValueDescriptors.
 int estimateMemoryUsage()
          Estimate the memory usage in bytes of the data value and the overhead of the class.
 boolean exists(java.lang.String xExpr)
          Helper method for XMLExists.
 DataValueDescriptor getClone()
          Clone this DataValueDescriptor.
 int getLength()
          Gets the length of the data value.
 DataValueDescriptor getNewNull()
          Get a new null value of the same type as this data value.
 java.lang.String getString()
          Gets the value in the data value descriptor as a String.
 int getTypeFormatId()
          Get a universally unique identifier for the type of this object.
 java.lang.String getTypeName()
          Get the SQL name of the datatype
 boolean isNull()
          Return whether the value is null or not.
protected  void loadSAXReader()
          Load an XMLReader for SAX events that can be used for parsing XML data.
 void loadStream()
          sets the stream state for the object.
private  void loadXSLTObjects()
          Prepare for an XSLT query by loading the objects required for such a query.
 void parseAndLoadXML(java.lang.String text, boolean preserveWS)
          Method to parse an XML string and, if it's valid, store the _parsed_ version for subsequent use.
 void readExternal(java.io.ObjectInput in)
          Read an XML value from an input stream.
 void readExternalFromArray(ArrayInputStream in)
          Read the DataValueDescriptor from the stream.
private  java.lang.String replaceDoubleQuotes(java.lang.String queryText)
          Takes a string (which is an XPath query specified by the user) and replaces any double quotes with single quotes.
 void restoreToNull()
          Restore this object to its (SQL)null value.
 java.io.InputStream returnStream()
          Return the stream state of the object.
protected  void setFrom(DataValueDescriptor theValue)
           
 void setStream(java.io.InputStream newStream)
          sets the stream state for the object.
 void setValueFromResultSet(java.sql.ResultSet resultSet, int colNumber, boolean isNullable)
          Set the value based on the value for the specified DataValueDescriptor from the specified ResultSet.
 int typePrecedence()
          Each built-in type in JSQL has a precedence.
 void writeExternal(java.io.ObjectOutput out)
          Write an XML value.
 BooleanDataValue XMLExists(StringDataValue xExpr, XMLDataValue xml)
          The SQL/XML XMLExists operator.
 StringDataValue XMLSerialize(StringDataValue result, int targetType, int targetWidth)
          The SQL/XML XMLSerialize operator.
 
Methods inherited from class org.apache.derby.iapi.types.DataType
checkHostVariable, cloneObject, coalesce, compare, dataTypeConversion, equals, equals, flip, genericSetObject, getBoolean, getByte, getBytes, getDate, getDouble, getFloat, getInt, getLong, getNationalString, getObject, getShort, getStream, getTime, getTimestamp, greaterOrEquals, greaterThan, in, invalidFormat, isNotNull, isNullOp, lessOrEquals, lessThan, normalize, notEquals, outOfRange, setBigDecimal, setInto, setInto, setObjectForCast, setToNull, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, throwLangSetMismatch, throwLangSetMismatch, typeToBigDecimal
 
Methods inherited from class java.lang.Object
clone, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
 
Methods inherited from interface org.apache.derby.iapi.types.DataValueDescriptor
checkHostVariable, coalesce, compare, equals, getBoolean, getByte, getBytes, getDate, getDouble, getFloat, getInt, getLong, getObject, getShort, getStream, getTime, getTimestamp, greaterOrEquals, greaterThan, in, isNotNull, isNullOp, lessOrEquals, lessThan, normalize, notEquals, setBigDecimal, setInto, setInto, setObjectForCast, setToNull, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, setValue, typeToBigDecimal
 

Field Detail

UTF8_IMPL_ID

protected static final short UTF8_IMPL_ID
See Also:
Constant Field Values

XML_PARSER_CLASS

protected static final java.lang.String XML_PARSER_CLASS
See Also:
Constant Field Values

BASE_MEMORY_USAGE

private static final int BASE_MEMORY_USAGE

xmlStringValue

private SQLChar xmlStringValue

saxReader

protected org.xml.sax.XMLReader saxReader

XPATH_PLACEHOLDER

private static final java.lang.String XPATH_PLACEHOLDER
See Also:
Constant Field Values

QUERY_MATCH_STRING

private static final java.lang.String QUERY_MATCH_STRING
See Also:
Constant Field Values

xsltStylesheet

private static java.lang.String xsltStylesheet

xsltReader

private org.xml.sax.XMLReader xsltReader

saxTFactory

private org.apache.xalan.processor.TransformerFactoryImpl saxTFactory
Constructor Detail

XML

public XML()
Default constructor.


XML

private XML(SQLChar val)
Private constructor used for the getClone() method. Takes a SQLChar and clones it.

Parameters:
val - A SQLChar instance to clone and use for this XML value.
Method Detail

getClone

public DataValueDescriptor getClone()
Description copied from interface: DataValueDescriptor
Clone this DataValueDescriptor. Results in a new object that has the same value as this but can be modified independently.

Specified by:
getClone in interface DataValueDescriptor
Returns:
A clone of the DataValueDescriptor with the same initial value as this.
See Also:
DataValueDescriptor.getClone()

getNewNull

public DataValueDescriptor getNewNull()
Description copied from interface: DataValueDescriptor
Get a new null value of the same type as this data value.

Specified by:
getNewNull in interface DataValueDescriptor
See Also:
DataValueDescriptor.getNewNull()

getTypeName

public java.lang.String getTypeName()
Description copied from interface: DataValueDescriptor
Get the SQL name of the datatype

Specified by:
getTypeName in interface DataValueDescriptor
Returns:
The SQL name of the datatype
See Also:
DataValueDescriptor.getTypeName()

typePrecedence

public int typePrecedence()
Description copied from interface: DataValueDescriptor
Each built-in type in JSQL has a precedence. This precedence determines how to do type promotion when using binary operators. For example, float has a higher precedence than int, so when adding an int to a float, the result type is float. The precedence for some types is arbitrary. For example, it doesn't matter what the precedence of the boolean type is, since it can't be mixed with other types. But the precedence for the number types is critical. The SQL standard requires that exact numeric types be promoted to approximate numeric when one operator uses both. Also, the precedence is arranged so that one will not lose precision when promoting a type.

Specified by:
typePrecedence in interface DataValueDescriptor
Overrides:
typePrecedence in class DataType
Returns:
The precedence of this type.
See Also:
DataValueDescriptor.typePrecedence()

getString

public java.lang.String getString()
                           throws StandardException
Description copied from interface: DataValueDescriptor
Gets the value in the data value descriptor as a String. Throws an exception if the data value is not a string.

Specified by:
getString in interface DataValueDescriptor
Returns:
The data value as a String.
Throws:
StandardException - Thrown on error
See Also:
DataValueDescriptor.getString()

getLength

public int getLength()
              throws StandardException
Description copied from interface: DataValueDescriptor
Gets the length of the data value. The meaning of this is implementation-dependent. For string types, it is the number of characters in the string. For numeric types, it is the number of bytes used to store the number. This is the actual length of this value, not the length of the type it was defined as. For example, a VARCHAR value may be shorter than the declared VARCHAR (maximum) length.

Specified by:
getLength in interface DataValueDescriptor
Returns:
The length of the data value
Throws:
StandardException - On error
See Also:
DataValueDescriptor.getLength()

estimateMemoryUsage

public int estimateMemoryUsage()
Description copied from interface: DataValueDescriptor
Estimate the memory usage in bytes of the data value and the overhead of the class.

Specified by:
estimateMemoryUsage in interface DataValueDescriptor
Returns:
the estimated memory usage
See Also:
DataValueDescriptor.estimateMemoryUsage()

readExternalFromArray

public void readExternalFromArray(ArrayInputStream in)
                           throws java.io.IOException
Description copied from interface: DataValueDescriptor
Read the DataValueDescriptor from the stream.

Initialize the data value by reading it's values from the ArrayInputStream. This interface is provided as a way to achieve possible performance enhancement when reading an array can be optimized over reading from a generic stream from readExternal().

Specified by:
readExternalFromArray in interface DataValueDescriptor
Parameters:
in - The array stream positioned at the beginning of the byte stream to read from.
Throws:
java.io.IOException - Usual error is if you try to read past limit on the stream.
See Also:
DataValueDescriptor.readExternalFromArray(org.apache.derby.iapi.services.io.ArrayInputStream)

setFrom

protected void setFrom(DataValueDescriptor theValue)
                throws StandardException
Overrides:
setFrom in class DataType
Throws:
StandardException
See Also:
DataValueDescriptor#setFrom

setValueFromResultSet

public final void setValueFromResultSet(java.sql.ResultSet resultSet,
                                        int colNumber,
                                        boolean isNullable)
                                 throws java.sql.SQLException
Description copied from interface: DataValueDescriptor
Set the value based on the value for the specified DataValueDescriptor from the specified ResultSet.

Specified by:
setValueFromResultSet in interface DataValueDescriptor
Parameters:
resultSet - The specified ResultSet.
colNumber - The 1-based column # into the resultSet.
isNullable - Whether or not the column is nullable (No need to call wasNull() if not)
Returns:
Nothing.
Throws:
java.sql.SQLException - Error accessing the result set
See Also:
DataValueDescriptor.setValueFromResultSet(java.sql.ResultSet, int, boolean)

compare

public int compare(DataValueDescriptor other)
            throws StandardException
Compare two XML DataValueDescriptors. NOTE: This method should only be used by the database store for the purpose of index positioning--comparisons of XML type are not allowed from the language side of things. That said, all store wants to do is order the NULLs, so we don't actually have to do a full comparison. Just return an order value based on whether or not this XML value and the other XML value are null. As mentioned in the "compare" method of DataValueDescriptor, nulls are considered equal to other nulls and less than all other values. An example of when this method might be used is if the user executed a query like: select i from x_table where x_col is not null

Specified by:
compare in interface DataValueDescriptor
Parameters:
other - The Orderable to compare this one to.
Returns:
<0 - this Orderable is less than other. 0 - this Orderable equals other. >0 - this Orderable is greater than other. The code should not explicitly look for -1, or 1.
Throws:
StandardException - Thrown on error
See Also:
DataValueDescriptor.compare(org.apache.derby.iapi.types.DataValueDescriptor)

getTypeFormatId

public int getTypeFormatId()
Description copied from interface: TypedFormat
Get a universally unique identifier for the type of this object.

Specified by:
getTypeFormatId in interface TypedFormat
Returns:
The identifier. (A UUID stuffed in an array of 16 bytes).
See Also:
From the engine's perspective, all XML implementations share the same format id.

isNull

public boolean isNull()
Description copied from interface: Storable
Return whether the value is null or not.

Specified by:
isNull in interface Storable
Returns:
true if the value is null and false otherwise.
See Also:
Storable#isNull

restoreToNull

public void restoreToNull()
Description copied from interface: Storable
Restore this object to its (SQL)null value.

Specified by:
restoreToNull in interface Storable
See Also:
Storable#restoreToNull

readExternal

public void readExternal(java.io.ObjectInput in)
                  throws java.io.IOException
Read an XML value from an input stream.

Specified by:
readExternal in interface java.io.Externalizable
Parameters:
in - The stream from which we're reading.
Throws:
java.io.IOException

writeExternal

public void writeExternal(java.io.ObjectOutput out)
                   throws java.io.IOException
Write an XML value.

Specified by:
writeExternal in interface java.io.Externalizable
Parameters:
out - The stream to which we're writing.
Throws:
java.io.IOException

returnStream

public java.io.InputStream returnStream()
Description copied from interface: StreamStorable
Return the stream state of the object.

Specified by:
returnStream in interface StreamStorable
See Also:
StreamStorable.returnStream()

setStream

public void setStream(java.io.InputStream newStream)
Description copied from interface: StreamStorable
sets the stream state for the object.

Specified by:
setStream in interface StreamStorable
See Also:
StreamStorable.setStream(java.io.InputStream)

loadStream

public void loadStream()
                throws StandardException
Description copied from interface: StreamStorable
sets the stream state for the object.

Specified by:
loadStream in interface StreamStorable
Throws:
StandardException - on error
See Also:
StreamStorable.loadStream()

parseAndLoadXML

public void parseAndLoadXML(java.lang.String text,
                            boolean preserveWS)
                     throws StandardException
Method to parse an XML string and, if it's valid, store the _parsed_ version for subsequent use.

Specified by:
parseAndLoadXML in interface XMLDataValue
Parameters:
text - The string value to check.
preserveWS - Whether or not to preserve ignorable whitespace.
Returns:
If 'text' constitutes a valid XML document, it has been stored in this XML value and nothing is returned; otherwise, an exception is thrown.
Throws:
StandardException - Thrown on parse error.

XMLSerialize

public StringDataValue XMLSerialize(StringDataValue result,
                                    int targetType,
                                    int targetWidth)
                             throws StandardException
The SQL/XML XMLSerialize operator. Converts this XML value into a string with a user-specified type, and returns that string via the received StringDataValue (if the received StringDataValue is non-null; else a new StringDataValue is returned).

Specified by:
XMLSerialize in interface XMLDataValue
Parameters:
result - The result of a previous call to this method, null if not called yet.
targetType - The string type to which we want to serialize.
targetWidth - The width of the target type.
Returns:
A serialized (to string) version of this XML object, in the form of a StringDataValue object.
Throws:
StandardException - Thrown on error

XMLExists

public BooleanDataValue XMLExists(StringDataValue xExpr,
                                  XMLDataValue xml)
                           throws StandardException
The SQL/XML XMLExists operator. Takes an XML query expression (as a string) and an XML value and checks if at least one node in the XML value matches the query expression. NOTE: For now, the query expression must be XPath only (XQuery not supported).

Specified by:
XMLExists in interface XMLDataValue
Parameters:
xExpr - The query expression, as a string.
xml - The XML value being queried.
Returns:
True if the received query expression matches at least one node in the received XML value; unknown if either the query expression or the xml value is null; false otherwise.
Throws:
StandardException - Thrown on error

exists

public boolean exists(java.lang.String xExpr)
               throws StandardException
Helper method for XMLExists. See if the received XPath expression returns at least one node when evaluated against _this_ XML value.

Specified by:
exists in interface XMLDataValue
Parameters:
xExpr - The XPath expression.
Returns:
True if at least one node in this XML value matches the received xExpr; false otherwise.
Throws:
StandardException

loadSAXReader

protected void loadSAXReader()
                      throws java.lang.Exception
Load an XMLReader for SAX events that can be used for parsing XML data. This method is currently only used for XMLPARSE, and the SQL/XML[2003] spec says that XMLPARSE should NOT perform validation -- Seciont 6.11: "Perform a non-validating parse of a character string to produce an XML value." Thus, we make sure to disable validation on the XMLReader loaded here. At some point in the future we will probably want to add support for the XMLVALIDATE function--but until then, user is unable to validate the XML values s/he inserts. Note that, even with validation turned off, XMLPARSE _will_ still check the well-formedness of the values, and it _will_ still process DTDs to get default values, etc--but that's it; no validation errors will be thrown. For future reference: the features needed to perform validation (with Xerces) are: http://apache.org/xml/features/validation/schema http://apache.org/xml/features/validation/dynamic

Throws:
java.lang.Exception

loadXSLTObjects

private void loadXSLTObjects()
                      throws org.xml.sax.SAXException
Prepare for an XSLT query by loading the objects required for such a query. We should only have to do this once per XML object.

Throws:
org.xml.sax.SAXException

replaceDoubleQuotes

private java.lang.String replaceDoubleQuotes(java.lang.String queryText)
Takes a string (which is an XPath query specified by the user) and replaces any double quotes with single quotes. We have to do this because a double quote in the XSLT stylesheet (which is where the user's query ends up) will be parsed as a query terminator thus will cause XSLT execution errors.

Parameters:
queryText - Text in which we want to replace double quotes.
Returns:
queryText with all double quotes replaced by single quotes.

Built on Tue 2006-10-10 19:23:47+0200, from revision exported

Apache Derby V10.1 Engine Documentation - Copyright © 1997,2005 The Apache Software Foundation or its licensors, as applicable.