/** * Factory method to construct an atomic value given its lexical representation and the * required item type * @param value the lexical representation of the required value * @param type the item type of the required value * @return the constructed item * @throws SaxonApiException if the supplied string is not in the lexical space of the target type, or * if the target type is not atomic */ public static XdmItem newAtomicValue(String value, ItemType type) throws SaxonApiException { net.sf.saxon.type.ItemType it = type.getUnderlyingItemType(); if (!it.isAtomicType()) { throw new SaxonApiException("Requested type is not atomic"); } Configuration config = type.getProcessor().getUnderlyingConfiguration(); ConversionResult result = new StringValue(value).convert((AtomicType)it, true, config.getConversionContext()); try { return (XdmItem)wrap(result.asAtomic()); } catch (ValidationException e) { throw new SaxonApiException(e); } }
/** * Construct an atomic value given its lexical representation and the name of the required * built-in atomic type. * <p>This method cannot be used to construct values that are namespace-sensitive (QNames and Notations)</p> * @param lexicalForm the value in the lexical space of the target data type. More strictly, the input * value before the actions of the whitespace facet for the target data type are applied. * @param type the required atomic type. This must either be one of the built-in * atomic types defined in XML Schema, or a user-defined type whose definition appears * in a schema that is known to the Processor. * @throws SaxonApiException if the type is unknown, or is not atomic, or is namespace-sensitive; * or if the value supplied in <tt>lexicalForm</tt> is not in the lexical space of the specified atomic * type. */ public XdmAtomicValue(String lexicalForm, ItemType type) throws SaxonApiException { net.sf.saxon.type.ItemType it = type.getUnderlyingItemType(); if (!it.isAtomicType()) { throw new SaxonApiException("Requested type is not atomic"); } Configuration config = type.getProcessor().getUnderlyingConfiguration(); ConversionResult result = new StringValue(lexicalForm).convert( (AtomicType)it, true, config.getConversionContext()); try { setValue(result.asAtomic()); } catch (ValidationException e) { throw new SaxonApiException(e); } }