All Downloads are FREE. Search and download functionalities are using the official Maven repository.

net.sf.saxon.xqj.SaxonXQDataFactory Maven / Gradle / Ivy

There is a newer version: 10.5
Show newest version
package net.sf.saxon.xqj;

import net.sf.saxon.Configuration;
import net.sf.saxon.dom.DOMObjectModel;
import net.sf.saxon.evpull.EventIterator;
import net.sf.saxon.evpull.EventToStaxBridge;
import net.sf.saxon.evpull.PullEventSource;
import net.sf.saxon.evpull.StaxToEventBridge;
import net.sf.saxon.expr.EarlyEvaluationContext;
import net.sf.saxon.expr.JPConverter;
import net.sf.saxon.expr.StaticProperty;
import net.sf.saxon.expr.parser.Token;
import net.sf.saxon.lib.AugmentedSource;
import net.sf.saxon.lib.NamespaceConstant;
import net.sf.saxon.lib.Validation;
import net.sf.saxon.om.DocumentInfo;
import net.sf.saxon.om.NodeInfo;
import net.sf.saxon.om.StandardNames;
import net.sf.saxon.pattern.*;
import net.sf.saxon.trans.XPathException;
import net.sf.saxon.type.*;
import net.sf.saxon.value.*;
import net.sf.saxon.value.StringValue;
import net.sf.saxon.z.IntToIntHashMap;
import net.sf.saxon.z.IntToIntMap;
import org.w3c.dom.Node;
import org.xml.sax.InputSource;

import javax.xml.datatype.Duration;
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.QName;
import javax.xml.stream.XMLStreamReader;
import javax.xml.transform.Source;
import javax.xml.transform.sax.SAXSource;
import javax.xml.xquery.*;
import java.io.InputStream;
import java.io.Reader;
import java.io.StringReader;
import java.lang.Boolean;
import java.lang.Byte;
import java.lang.Class;
import java.lang.Double;
import java.lang.Exception;
import java.lang.Float;
import java.lang.Integer;
import java.lang.Long;
import java.lang.Object;
import java.lang.Short;
import java.lang.String;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.URI;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * Saxon implementation of the XQJ interface XQDataFactory. This is an abstract superclass for SaxonXQDataSource
 * and SaxonXQConnection, both of which provide the factory methods in this interface.
 * 

* For Javadoc specifications of the public methods in this class, see the XQJ documentation. */ public abstract class SaxonXQDataFactory extends Closable implements XQDataFactory { private ObjectConverter objectConverter; abstract Configuration getConfiguration(); // Two-way mapping between XQJ integer codes for built-in types and the Saxon equivalents /*@NotNull*/ private static IntToIntMap XQJtoSaxonTypeTranslation = new IntToIntHashMap(80); /*@NotNull*/ private static IntToIntMap saxonToXQJTypeTranslation = new IntToIntHashMap(80); private static void map(int x, int y) { XQJtoSaxonTypeTranslation.put(x, y); saxonToXQJTypeTranslation.put(y, x); } static { map(XQItemType.XQBASETYPE_ANYSIMPLETYPE, StandardNames.XS_ANY_SIMPLE_TYPE); map(XQItemType.XQBASETYPE_ANYTYPE, StandardNames.XS_ANY_TYPE); map(XQItemType.XQBASETYPE_ANYURI, StandardNames.XS_ANY_URI); map(XQItemType.XQBASETYPE_BASE64BINARY, StandardNames.XS_BASE64_BINARY); map(XQItemType.XQBASETYPE_BOOLEAN, StandardNames.XS_BOOLEAN); map(XQItemType.XQBASETYPE_BYTE, StandardNames.XS_BYTE); map(XQItemType.XQBASETYPE_DATE, StandardNames.XS_DATE); map(XQItemType.XQBASETYPE_DATETIME, StandardNames.XS_DATE_TIME); map(XQItemType.XQBASETYPE_DECIMAL, StandardNames.XS_DECIMAL); map(XQItemType.XQBASETYPE_DOUBLE, StandardNames.XS_DOUBLE); map(XQItemType.XQBASETYPE_DURATION, StandardNames.XS_DURATION); map(XQItemType.XQBASETYPE_ENTITIES, StandardNames.XS_ENTITIES); map(XQItemType.XQBASETYPE_ENTITY, StandardNames.XS_ENTITY); map(XQItemType.XQBASETYPE_FLOAT, StandardNames.XS_FLOAT); map(XQItemType.XQBASETYPE_GDAY, StandardNames.XS_G_DAY); map(XQItemType.XQBASETYPE_GMONTH, StandardNames.XS_G_MONTH); map(XQItemType.XQBASETYPE_GMONTHDAY, StandardNames.XS_G_MONTH_DAY); map(XQItemType.XQBASETYPE_GYEAR, StandardNames.XS_G_YEAR); map(XQItemType.XQBASETYPE_GYEARMONTH, StandardNames.XS_G_YEAR_MONTH); map(XQItemType.XQBASETYPE_HEXBINARY, StandardNames.XS_HEX_BINARY); map(XQItemType.XQBASETYPE_ID, StandardNames.XS_ID); map(XQItemType.XQBASETYPE_IDREF, StandardNames.XS_IDREF); map(XQItemType.XQBASETYPE_IDREFS, StandardNames.XS_IDREFS); map(XQItemType.XQBASETYPE_INT, StandardNames.XS_INT); map(XQItemType.XQBASETYPE_INTEGER, StandardNames.XS_INTEGER); map(XQItemType.XQBASETYPE_LANGUAGE, StandardNames.XS_LANGUAGE); map(XQItemType.XQBASETYPE_LONG, StandardNames.XS_LONG); map(XQItemType.XQBASETYPE_NAME, StandardNames.XS_NAME); map(XQItemType.XQBASETYPE_NCNAME, StandardNames.XS_NCNAME); map(XQItemType.XQBASETYPE_NEGATIVE_INTEGER, StandardNames.XS_NEGATIVE_INTEGER); map(XQItemType.XQBASETYPE_NMTOKEN, StandardNames.XS_NMTOKEN); map(XQItemType.XQBASETYPE_NMTOKENS, StandardNames.XS_NMTOKENS); map(XQItemType.XQBASETYPE_NONNEGATIVE_INTEGER, StandardNames.XS_NON_NEGATIVE_INTEGER); map(XQItemType.XQBASETYPE_NONPOSITIVE_INTEGER, StandardNames.XS_NON_POSITIVE_INTEGER); map(XQItemType.XQBASETYPE_NORMALIZED_STRING, StandardNames.XS_NORMALIZED_STRING); map(XQItemType.XQBASETYPE_NOTATION, StandardNames.XS_NOTATION); map(XQItemType.XQBASETYPE_POSITIVE_INTEGER, StandardNames.XS_POSITIVE_INTEGER); map(XQItemType.XQBASETYPE_QNAME, StandardNames.XS_QNAME); map(XQItemType.XQBASETYPE_SHORT, StandardNames.XS_SHORT); map(XQItemType.XQBASETYPE_STRING, StandardNames.XS_STRING); map(XQItemType.XQBASETYPE_TIME, StandardNames.XS_TIME); map(XQItemType.XQBASETYPE_TOKEN, StandardNames.XS_TOKEN); map(XQItemType.XQBASETYPE_UNSIGNED_BYTE, StandardNames.XS_UNSIGNED_BYTE); map(XQItemType.XQBASETYPE_UNSIGNED_INT, StandardNames.XS_UNSIGNED_INT); map(XQItemType.XQBASETYPE_UNSIGNED_LONG, StandardNames.XS_UNSIGNED_LONG); map(XQItemType.XQBASETYPE_UNSIGNED_SHORT, StandardNames.XS_UNSIGNED_SHORT); map(XQItemType.XQBASETYPE_ANYATOMICTYPE, StandardNames.XS_ANY_ATOMIC_TYPE); map(XQItemType.XQBASETYPE_DAYTIMEDURATION, StandardNames.XS_DAY_TIME_DURATION); map(XQItemType.XQBASETYPE_UNTYPED, StandardNames.XS_UNTYPED); map(XQItemType.XQBASETYPE_UNTYPEDATOMIC, StandardNames.XS_UNTYPED_ATOMIC); map(XQItemType.XQBASETYPE_YEARMONTHDURATION, StandardNames.XS_YEAR_MONTH_DURATION); } /** * Get the XQJ type code corresponding to a given Saxon type code * @param type the Saxon type code * @return the corresponding XQJ type code */ static int mapSaxonTypeToXQJ(int type) { return saxonToXQJTypeTranslation.get(type); } protected void init() { objectConverter = new StandardObjectConverter(this); } /** * Set the ObjectConverter to be used. This allows user-defined object conversions to override * or supplement the standard conversions * @param converter the user-supplied ObjectConverter */ public void setObjectConverter(ObjectConverter converter) { objectConverter = converter; } /** * Get the ObjectConverter in use. This will either be the default object converter supplied by Saxon, * or a user-supplied ObjectConverter if one has been set. * @return the ObjectConverter in use. */ public ObjectConverter getObjectConverter() { return objectConverter; } /** * Create an atomic item type object representing a particular built-in atomic type * * @param baseType the built-in atomic type, typically a constant such as * XQItemType.XQBASETYPE_BOOLEAN * @return the corresponding XQItemType * @throws XQException if the supplied baseType parameter is not an atomic type */ /*@NotNull*/ public XQItemType createAtomicType(int baseType) throws XQException { checkNotClosed(); int saxonType = XQJtoSaxonTypeTranslation.get(baseType); if (saxonType == XQJtoSaxonTypeTranslation.getDefaultValue()) { throw new XQException("Unknown base type " + baseType); } SchemaType st = BuiltInType.getSchemaType(saxonType); if (st instanceof AtomicType) { return new SaxonXQItemType((AtomicType)st, getConfiguration()); } else { throw new XQException("baseType " + baseType + " is not atomic"); } } /** * See interface definition, and description of Saxon extensions below. * *

In addition to the actions described in the XQJ interface definitions, Saxon allows the * typename to be a name representing a Java external type. In this case the URI part of the QName * must be {@link net.sf.saxon.lib.NamespaceConstant#JAVA_TYPE}, and the local part of the name must be the Java class * name (qualified with its package name) * @param baseType the "baseType" (in XQJ terminology) * @param typename the qualified name of the type * @param schemaURI the location of a schema document in which the type is defined (may be null) * @return the item type definition * @throws XQException */ /*@NotNull*/ public XQItemType createAtomicType(int baseType, /*@Nullable*/ QName typename, URI schemaURI) throws XQException { checkNotClosed(); if (typename == null) { return createAtomicType(baseType); } if (typename.getNamespaceURI().equals(NamespaceConstant.JAVA_TYPE)) { String className = typename.getLocalPart(); Configuration config = getConfiguration(); try { Class javaClass = config.getClass(className, false, null); return new SaxonXQItemType(new ExternalObjectType(javaClass, config), config); } catch (XPathException e) { throw new XQException(e.getMessage()); } } SchemaType st = getConfiguration().getSchemaType(getFingerprint(typename)); if (st == null) { loadSchema(schemaURI); st = getConfiguration().getSchemaType(getFingerprint(typename)); } if (st == null) { throw new XQException("Type " + typename + " not found in schema"); } else if (st instanceof AtomicType) { return new SaxonXQItemType((AtomicType)st, getConfiguration()); } else { throw new XQException("Type " + typename + " is not atomic"); } } /*@NotNull*/ public XQItemType createAttributeType(/*@Nullable*/ QName nodename, int basetype) throws XQException { checkNotClosed(); Configuration config = getConfiguration(); int saxonType = XQJtoSaxonTypeTranslation.get(basetype); if (saxonType == XQJtoSaxonTypeTranslation.getDefaultValue()) { throw new XQException("Unknown base type " + basetype); } SchemaType st = BuiltInType.getSchemaType(saxonType); if (!(st.isSimpleType())) { throw new XQException("baseType " + basetype + " is not a simple type"); } ContentTypeTest contentTest = new ContentTypeTest(Type.ATTRIBUTE, st, config, false); if (nodename == null) { return new SaxonXQItemType(contentTest, config); } else { NameTest nameTest = new NameTest( Type.ATTRIBUTE, nodename.getNamespaceURI(), nodename.getLocalPart(), config.getNamePool()); CombinedNodeTest combined = new CombinedNodeTest(nameTest, Token.INTERSECT, contentTest); return new SaxonXQItemType(combined, config); } } /*@NotNull*/ public XQItemType createAttributeType(/*@Nullable*/ QName nodename, int basetype, /*@Nullable*/ QName typename, URI schemaURI) throws XQException { checkNotClosed(); if (typename == null) { return createAttributeType(nodename, basetype); } Configuration config = getConfiguration(); SchemaType st = BuiltInType.getSchemaType(getFingerprint(typename)); if (st == null) { loadSchema(schemaURI); st = getConfiguration().getSchemaType(getFingerprint(typename)); } if (st == null) { throw new XQException("Type " + typename + " not found in schema"); } else if (!(st.isSimpleType())) { throw new XQException("Type " + typename + " is not a simple type"); } ContentTypeTest contentTest = new ContentTypeTest(Type.ATTRIBUTE, st, config, false); if (nodename == null) { return new SaxonXQItemType(contentTest, config); } else { NameTest nameTest = new NameTest( Type.ATTRIBUTE, nodename.getNamespaceURI(), nodename.getLocalPart(), config.getNamePool()); CombinedNodeTest combined = new CombinedNodeTest(nameTest, Token.INTERSECT, contentTest); return new SaxonXQItemType(combined, config); } } /*@NotNull*/ public XQItemType createCommentType() throws XQException { checkNotClosed(); return new SaxonXQItemType(NodeKindTest.COMMENT, getConfiguration()); } /*@NotNull*/ public XQItemType createDocumentElementType(XQItemType elementType) throws XQException { checkNotClosed(); SaxonXQDataSource.checkNotNull(elementType, "elementType"); ItemType itemType = ((SaxonXQItemType)elementType).getSaxonItemType(); if (itemType instanceof NodeTest && (((NodeTest)itemType).getNodeKindMask() & (1<notation conversion doesn't work throw new XQException("Target type must be string, untypedAtomic, or anyURI"); } if (result instanceof ValidationFailure) { throw new XQException(((ValidationFailure)result).getMessage()); } return new SaxonXQItem((AtomicValue)result, this); } else if (value instanceof BigDecimal) { AtomicType at = testAtomic(type); int prim = at.getPrimitiveType(); if (prim == StandardNames.XS_DECIMAL || prim == StandardNames.XS_INTEGER) { Converter converter = getConfiguration().getConversionRules().getConverter(BuiltInAtomicType.DECIMAL, at); ConversionResult result = converter.convert(new DecimalValue((BigDecimal) value)); if (result instanceof ValidationFailure) { throw new XQException(((ValidationFailure)result).getMessage()); } return new SaxonXQItem((AtomicValue)result, this); } else { throw new XQException("Target type must be xs:decimal or a subtype"); } } else if (value instanceof BigInteger) { AtomicType at = testAtomic(type); int prim = at.getPrimitiveType(); if (prim == StandardNames.XS_DECIMAL || prim == StandardNames.XS_INTEGER) { Converter converter = getConfiguration().getConversionRules().getConverter(BuiltInAtomicType.DECIMAL, at); ConversionResult result = converter.convert(new DecimalValue(new BigDecimal((BigInteger) value))); if (result instanceof ValidationFailure) { throw new XQException(((ValidationFailure)result).getMessage()); } return new SaxonXQItem((AtomicValue)result, this); } else { throw new XQException("Target type must be xs:decimal or a subtype"); } } else if (value instanceof Duration) { AtomicType at = testAtomic(type); int prim = at.getPrimitiveType(); if (prim == StandardNames.XS_DURATION || prim == StandardNames.XS_DAY_TIME_DURATION || prim == StandardNames.XS_YEAR_MONTH_DURATION) { DurationValue dv = (DurationValue) getObjectConverter().convertToItem(value); Converter converter = getConfiguration().getConversionRules().getConverter(dv.getPrimitiveType(), at); ConversionResult result = converter.convert(dv); if (result instanceof ValidationFailure) { throw new XQException(((ValidationFailure)result).getMessage()); } return new SaxonXQItem((AtomicValue)result, this); } else { throw new XQException("Target type must be xs:duration or a subtype"); } } else if (value instanceof XMLGregorianCalendar) { AtomicType at = testAtomic(type); int prim = at.getPrimitiveType(); switch (prim) { case StandardNames.XS_DATE_TIME: case StandardNames.XS_DATE: case StandardNames.XS_TIME: case StandardNames.XS_G_YEAR: case StandardNames.XS_G_YEAR_MONTH: case StandardNames.XS_G_MONTH: case StandardNames.XS_G_MONTH_DAY: case StandardNames.XS_G_DAY: AtomicValue dv = (AtomicValue) getObjectConverter().convertToItem(value); Converter converter = getConfiguration().getConversionRules().getConverter(dv.getPrimitiveType(), at); ConversionResult result = converter.convert(dv); if (result instanceof ValidationFailure) { throw new XQException(((ValidationFailure)result).getMessage()); } return new SaxonXQItem((AtomicValue)result, this); default: throw new XQException("Target type must be a date/time type"); } } else if (value instanceof QName) { AtomicType at = testAtomic(type); int prim = at.getPrimitiveType(); if (prim == StandardNames.XS_QNAME) { QualifiedNameValue dv = (QualifiedNameValue) getObjectConverter().convertToItem(value); Converter converter = getConfiguration().getConversionRules().getConverter(dv.getPrimitiveType(), at); ConversionResult result = converter.convert(dv); if (result instanceof ValidationFailure) { throw new XQException(((ValidationFailure)result).getMessage()); } return new SaxonXQItem((AtomicValue)result, this); } else { throw new XQException("Target type must be xs:QName or a subtype"); } } else if (value instanceof Node) { NodeInfo result = (NodeInfo) getObjectConverter().convertToItem(value); XQItem item = new SaxonXQItem(result, this); if (!item.instanceOf(type)) { throw new XQException("Supplied node does not match the requested XQItemType"); } return item; } else { return new SaxonXQItem(getObjectConverter().convertToItem(value, type), this); } } /*@NotNull*/ public XQItem createItemFromShort(short value, /*@Nullable*/ XQItemType type) throws XQException { checkNotClosed(); if (type == null) { try { return new SaxonXQItem(new Int64Value(value, BuiltInAtomicType.SHORT, false), this); } catch (XPathException de) { throw newXQException(de); } } else { return createItemFromLong(value, type); } } /*@NotNull*/ public XQItemType createNodeType() throws XQException { checkNotClosed(); return new SaxonXQItemType(AnyNodeTest.getInstance(), getConfiguration()); } /*@NotNull*/ public XQItemType createProcessingInstructionType(/*@Nullable*/ String piTarget) throws XQException { checkNotClosed(); if (piTarget == null) { return new SaxonXQItemType(NodeKindTest.PROCESSING_INSTRUCTION, getConfiguration()); } else { return new SaxonXQItemType( new NameTest(Type.PROCESSING_INSTRUCTION, "", piTarget, getConfiguration().getNamePool()), getConfiguration()); } } /*@NotNull*/ public XQItemType createSchemaAttributeType(/*@NotNull*/ QName nodename, int basetype, /*@Nullable*/ URI schemaURI) throws XQException { checkNotClosed(); Configuration config = getConfiguration(); int fp = getFingerprint(nodename); SchemaDeclaration attributeDecl = config.getAttributeDeclaration(fp); if (attributeDecl == null && schemaURI != null) { loadSchema(schemaURI); attributeDecl = config.getAttributeDeclaration(fp); } if (attributeDecl == null) { throw new XQException("Attribute declaration " + nodename + " not found in schema"); } NodeTest nameTest = attributeDecl.makeSchemaNodeTest(); return new SaxonXQItemType(nameTest, config); } /*@NotNull*/ public XQItemType createSchemaElementType(/*@NotNull*/ QName nodename, int basetype, /*@Nullable*/ URI schemaURI) throws XQException { checkNotClosed(); Configuration config = getConfiguration(); int fp = getFingerprint(nodename); SchemaDeclaration elementDecl = config.getElementDeclaration(fp); if (elementDecl == null && schemaURI != null) { loadSchema(schemaURI); elementDecl = config.getElementDeclaration(fp); } if (elementDecl == null) { throw new XQException("Element declaration " + nodename + " not found in schema"); } NodeTest nameTest = elementDecl.makeSchemaNodeTest(); return new SaxonXQItemType(nameTest, config); } /*@NotNull*/ public XQSequence createSequence(/*@Nullable*/ Iterator i) throws XQException { checkNotClosed(); if (i == null) { throw new XQException("createSequence(): argument is null"); } List list = new ArrayList(50); while (i.hasNext()) { Object object = i.next(); XQItem item; if (object instanceof XQItem) { item = (XQItem)object; } else { item = createItemFromObject(object, null); } list.add(((SaxonXQItem)item).getSaxonItem()); } SequenceExtent extent = new SequenceExtent(list); return new SaxonXQSequence(extent, this); } /*@NotNull*/ public XQSequence createSequence(/*@Nullable*/ XQSequence s) throws XQException { checkNotClosed(); if (s == null) { throw new XQException("createSequence(): argument is null"); } ((Closable)s).checkNotClosed(); if (s instanceof SaxonXQSequence) { return new SaxonXQSequence(((SaxonXQSequence) s).getValue(), this); } else if (s instanceof SaxonXQForwardSequence) { try { SequenceExtent extent = new SequenceExtent(((SaxonXQForwardSequence) s).getCleanIterator()); return new SaxonXQSequence(extent, this); } catch (XPathException de) { throw newXQException(de); } } else { throw new XQException("Supplied sequence is not a Saxon implementation"); } } /*@NotNull*/ public XQSequenceType createSequenceType(XQItemType item, int occurrence) throws XQException { checkNotClosed(); if (item instanceof SaxonXQItemType) { ItemType itemType = ((SaxonXQItemType) item).getSaxonItemType(); int cardinality; switch (occurrence) { case XQSequenceType.OCC_EXACTLY_ONE: cardinality = StaticProperty.EXACTLY_ONE; break; case XQSequenceType.OCC_ONE_OR_MORE: cardinality = StaticProperty.ALLOWS_ONE_OR_MORE; break; case XQSequenceType.OCC_ZERO_OR_ONE: cardinality = StaticProperty.ALLOWS_ZERO_OR_ONE; break; case XQSequenceType.OCC_ZERO_OR_MORE: cardinality = StaticProperty.ALLOWS_ZERO_OR_MORE; break; default: throw new XQException("Invalid occurrence value"); } SequenceType st = SequenceType.makeSequenceType(itemType, cardinality); return new SaxonXQSequenceType(st, getConfiguration()); } else { throw new XQException("Supplied XQItemType is not a Saxon-created object"); } } /*@NotNull*/ public XQItemType createTextType() throws XQException { checkNotClosed(); return new SaxonXQItemType(NodeKindTest.TEXT, getConfiguration()); } private AtomicType testAtomic(/*@NotNull*/ XQItemType type) throws XQException { if (type instanceof SaxonXQItemType) { AtomicType at = ((SaxonXQItemType) type).getAtomicType(); if (at == null) { throw new XQException("Requested type is not atomic"); } return at; } else { throw new XQException("Supplied XQItemType is not a Saxon-created object"); } } /*@NotNull*/ private static XQException newXQException(/*@NotNull*/ Exception err) { XQException e = new XQException(err.getMessage()); e.initCause(err); return e; } private int getFingerprint(/*@NotNull*/ QName name) { return getConfiguration().getNamePool().allocate( name.getPrefix(), name.getNamespaceURI(), name.getLocalPart()); } /** * Attempt to load the schema document at a given location into the Configuration * @param schemaURI the absolute URI of the location of the schema document. If null is supplied, * the method is a no-op. * @throws XQException if the URI is not absolute, or if no schema is found at the location, * or if the schema is invalid, or if it is inconsistent with existing schema components present * in the Configuration. */ private void loadSchema(/*@Nullable*/ URI schemaURI) throws XQException { if (schemaURI == null) { return; } if (!schemaURI.isAbsolute()) { throw new XQException("Schema URI must be an absolute URI"); } try { getConfiguration().loadSchema(schemaURI.toString()); } catch (SchemaException err) { throw newXQException(err); } } } // Copyright (c) 2013 Saxonica Limited. All rights reserved.





© 2015 - 2024 Weber Informatics LLC | Privacy Policy