1. // $Id: DatatypeFactoryImpl.java,v 1.4 2004/04/30 01:40:53 jsuttor Exp $
  2. /*
  3. * @(#)DatatypeFactoryImpl.java 1.2 04/07/26
  4. *
  5. * Copyright 2004 Sun Microsystems, Inc. All rights reserved.
  6. * SUN PROPRIETARY/CONFIDENTIAL. Use is subject to license terms.
  7. */
  8. package com.sun.org.apache.xerces.internal.jaxp.datatype;
  9. import java.math.BigInteger;
  10. import java.math.BigDecimal;
  11. import java.util.GregorianCalendar;
  12. import javax.xml.datatype.DatatypeConstants;
  13. import javax.xml.datatype.DatatypeFactory;
  14. import javax.xml.datatype.Duration;
  15. import javax.xml.datatype.XMLGregorianCalendar;
  16. /**
  17. * <p>Factory that creates new <code>javax.xml.datatype</code> <code>Object</code>s that map XML to/from Java <code>Object</code>s.</p>
  18. *
  19. * <p id="DatatypeFactory.newInstance">{@link #newInstance()} is used to create a new <code>DatatypeFactory</code>.
  20. * The following implementation resolution mechanisms are used in the following order:</p>
  21. * <ol>
  22. * <li>
  23. * If the system property specified by {@link #DATATYPEFACTORY_PROPERTY}, "<code>javax.xml.datatype.DatatypeFactory</code>",
  24. * exists, a class with the name of the property's value is instantiated.
  25. * Any Exception thrown during the instantiation process is wrapped as a {@link DatatypeConfigurationException}.
  26. * </li>
  27. * <li>
  28. * If the file ${JAVA_HOME}/lib/jaxp.properties exists, it is loaded in a {@link java.util.Properties} <code>Object</code>.
  29. * The <code>Properties</code> <code>Object </code> is then queried for the property as documented in the prior step
  30. * and processed as documented in the prior step.
  31. * </li>
  32. * <li>
  33. * The services resolution mechanism is used, e.g. <code>META-INF/services/java.xml.datatype.DatatypeFactory</code>.
  34. * Any Exception thrown during the instantiation process is wrapped as a {@link DatatypeConfigurationException}.
  35. * </li>
  36. * <li>
  37. * The final mechanism is to attempt to instantiate the <code>Class</code> specified by
  38. * {@link #DATATYPEFACTORY_IMPLEMENTATION_CLASS}, "<code>javax.xml.datatype.DatatypeFactoryImpl</code>".
  39. * Any Exception thrown during the instantiation process is wrapped as a {@link DatatypeConfigurationException}.
  40. * </li>
  41. * </ol>
  42. *
  43. * @author <a href="mailto:Joseph.Fialli@Sun.COM">Joseph Fialli</a>
  44. * @author <a href="mailto:Jeff.Suttor@Sun.com">Jeff Suttor</a>
  45. * @version $Revision: 1.4 $, $Date: 2004/04/30 01:40:53 $
  46. * @since 1.5
  47. */
  48. public class DatatypeFactoryImpl
  49. extends DatatypeFactory {
  50. /**
  51. * <p>Public constructor is empty..</p>
  52. *
  53. * <p>Use {@link DatatypeFactory#newInstance()} to create a <code>DatatypeFactory<code>.</p>
  54. */
  55. public DatatypeFactoryImpl() {
  56. }
  57. /**
  58. * <p>Obtain a new instance of a <code>Duration</code>
  59. * specifying the <code>Duration</code> as its string representation, "PnYnMnDTnHnMnS",
  60. * as defined in XML Schema 1.0 section 3.2.6.1.</p>
  61. *
  62. * <p>XML Schema Part 2: Datatypes, 3.2.6 duration, defines <code>duration</code> as:</p>
  63. * <blockquote>
  64. * duration represents a duration of time.
  65. * The value space of duration is a six-dimensional space where the coordinates designate the
  66. * Gregorian year, month, day, hour, minute, and second components defined in Section 5.5.3.2 of [ISO 8601], respectively.
  67. * These components are ordered in their significance by their order of appearance i.e. as
  68. * year, month, day, hour, minute, and second.
  69. * </blockquote>
  70. * <p>All six values are set and availabe from the created {@link Duration}</p>
  71. *
  72. * <p>The XML Schema specification states that values can be of an arbitrary size.
  73. * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
  74. * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
  75. * if implementation capacities are exceeded.</p>
  76. *
  77. * @param lexicalRepresentation <code>String</code> representation of a <code>Duration</code>.
  78. *
  79. * @return New <code>Duration</code> created from parsing the <code>lexicalRepresentation</code>.
  80. *
  81. * @throws IllegalArgumentException If <code>lexicalRepresentation</code> is not a valid representation of a <code>Duration</code>.
  82. * @throws UnsupportedOperationException If implementation cannot support requested values.
  83. * @throws NullPointerException if <code>lexicalRepresentation</code> is <code>null</code>.
  84. */
  85. public Duration newDuration(final String lexicalRepresentation) {
  86. return new DurationImpl(lexicalRepresentation);
  87. }
  88. /**
  89. * <p>Obtain a new instance of a <code>Duration</code>
  90. * specifying the <code>Duration</code> as milliseconds.</p>
  91. *
  92. * <p>XML Schema Part 2: Datatypes, 3.2.6 duration, defines <code>duration</code> as:</p>
  93. * <blockquote>
  94. * duration represents a duration of time.
  95. * The value space of duration is a six-dimensional space where the coordinates designate the
  96. * Gregorian year, month, day, hour, minute, and second components defined in Section 5.5.3.2 of [ISO 8601], respectively.
  97. * These components are ordered in their significance by their order of appearance i.e. as
  98. * year, month, day, hour, minute, and second.
  99. * </blockquote>
  100. * <p>All six values are set by computing their values from the specified milliseconds
  101. * and are availabe using the <code>get</code> methods of the created {@link Duration}.
  102. * The values conform to and are defined by:</p>
  103. * <ul>
  104. * <li>ISO 8601:2000(E) Section 5.5.3.2 Alternative format</li>
  105. * <li><a href="http://www.w3.org/TR/xmlschema-2/#isoformats">
  106. * W3C XML Schema 1.0 Part 2, Appendix D, ISO 8601 Date and Time Formats</a>
  107. * </li>
  108. * <li>{@link XMLGregorianCalendar} Date/Time Datatype Field Mapping Between XML Schema 1.0 and Java Representation</li>
  109. * </ul>
  110. *
  111. * <p>The default start instance is defined by {@link GregorianCalendar}'s use of the start of the epoch: i.e.,
  112. * {@link java.util.Calendar#YEAR} = 1970,
  113. * {@link java.util.Calendar#MONTH} = {@link java.util.Calendar#JANUARY},
  114. * {@link java.util.Calendar#DATE} = 1, etc.
  115. * This is important as there are variations in the Gregorian Calendar,
  116. * e.g. leap years have different days in the month = {@link java.util.Calendar#FEBRUARY}
  117. * so the result of {@link Duration#getMonths()} and {@link Duration#getDays()} can be influenced.</p>
  118. *
  119. * @param durationInMilliseconds Duration in milliseconds to create.
  120. *
  121. * @return New <code>Duration</code> representing <code>durationInMilliseconds</code>.
  122. */
  123. public Duration newDuration(final long durationInMilliseconds) {
  124. return new DurationImpl(durationInMilliseconds);
  125. }
  126. /**
  127. * <p>Obtain a new instance of a <code>Duration</code>
  128. * specifying the <code>Duration</code> as isPositive, years, months, days, hours, minutes, seconds.</p>
  129. *
  130. * <p>The XML Schema specification states that values can be of an arbitrary size.
  131. * Implementations may chose not to or be incapable of supporting arbitrarily large and/or small values.
  132. * An {@link UnsupportedOperationException} will be thrown with a message indicating implementation limits
  133. * if implementation capacities are exceeded.</p>
  134. *
  135. * @param isPositive Set to <code>false</code> to create a negative duration. When the length
  136. * of the duration is zero, this parameter will be ignored.
  137. * @param years of this <code>Duration</code>
  138. * @param months of this <code>Duration</code>
  139. * @param days of this <code>Duration</code>
  140. * @param hours of this <code>Duration</code>
  141. * @param minutes of this <code>Duration</code>
  142. * @param seconds of this <code>Duration</code>
  143. *
  144. * @return New <code>Duration</code> created from the specified values.
  145. *
  146. * @throws IllegalArgumentException If values are not a valid representation of a <code>Duration</code>.
  147. * @throws UnsupportedOperationException If implementation cannot support requested values.
  148. * @throws NullPointerException If any values are <code>null</code>.
  149. *
  150. * @see #newDuration(boolean isPositive, BigInteger years, BigInteger months, BigInteger days,
  151. * BigInteger hours, BigInteger minutes, BigDecimal seconds)
  152. */
  153. public Duration newDuration(
  154. final boolean isPositive,
  155. final BigInteger years,
  156. final BigInteger months,
  157. final BigInteger days,
  158. final BigInteger hours,
  159. final BigInteger minutes,
  160. final BigDecimal seconds) {
  161. return new DurationImpl(
  162. isPositive,
  163. years,
  164. months,
  165. days,
  166. hours,
  167. minutes,
  168. seconds
  169. );
  170. }
  171. /**
  172. * <p>Create a new instance of an <code>XMLGregorianCalendar</code>.</p>
  173. *
  174. * <p>All date/time datatype fields set to {@link DatatypeConstants#FIELD_UNDEFINED} or null.</p>
  175. *
  176. * @return New <code>XMLGregorianCalendar</code> with all date/time datatype fields set to
  177. * {@link DatatypeConstants#FIELD_UNDEFINED} or null.
  178. */
  179. public XMLGregorianCalendar newXMLGregorianCalendar() {
  180. return new XMLGregorianCalendarImpl();
  181. }
  182. /**
  183. * <p>Create a new XMLGregorianCalendar by parsing the String as a lexical representation.</p>
  184. *
  185. * <p>Parsing the lexical string representation is defined in
  186. * <a href="http://www.w3.org/TR/xmlschema-2/#dateTime-order">XML Schema 1.0 Part 2, Section 3.2.[7-14].1,
  187. * <em>Lexical Representation</em>.</a></p>
  188. *
  189. * <p>The string representation may not have any leading and trailing whitespaces.</p>
  190. *
  191. * <p>The parsing is done field by field so that
  192. * the following holds for any lexically correct String x:</p>
  193. * <pre>
  194. * newXMLGregorianCalendar(x).toXMLFormat().equals(x)
  195. * </pre>
  196. * <p>Except for the noted lexical/canonical representation mismatches
  197. * listed in <a href="http://www.w3.org/2001/05/xmlschema-errata#e2-45">
  198. * XML Schema 1.0 errata, Section 3.2.7.2</a>.</p>
  199. *
  200. * @param lexicalRepresentation Lexical representation of one the eight XML Schema date/time datatypes.
  201. *
  202. * @return <code>XMLGregorianCalendar</code> created from the <code>lexicalRepresentation</code>.
  203. *
  204. * @throws IllegalArgumentException If the <code>lexicalRepresentation</code> is not a valid <code>XMLGregorianCalendar</code>.
  205. * @throws NullPointerException If <code>lexicalRepresentation</code> is <code>null</code>.
  206. */
  207. public XMLGregorianCalendar newXMLGregorianCalendar(final String lexicalRepresentation) {
  208. return new XMLGregorianCalendarImpl(lexicalRepresentation);
  209. }
  210. /**
  211. * <p>Create an <code>XMLGregorianCalendar</code> from a {@link GregorianCalendar}.</p>
  212. *
  213. * <table border="2" rules="all" cellpadding="2">
  214. * <thead>
  215. * <tr>
  216. * <th align="center" colspan="2">
  217. * Field by Field Conversion from
  218. * {@link GregorianCalendar} to an {@link XMLGregorianCalendar}
  219. * </th>
  220. * </tr>
  221. * <tr>
  222. * <th><code>java.util.GregorianCalendar</code> field</th>
  223. * <th><code>javax.xml.datatype.XMLGregorianCalendar</code> field</th>
  224. * </tr>
  225. * </thead>
  226. * <tbody>
  227. * <tr>
  228. * <td><code>ERA == GregorianCalendar.BC ? -YEAR : YEAR</code></td>
  229. * <td>{@link XMLGregorianCalendar#setYear(int year)}</td>
  230. * </tr>
  231. * <tr>
  232. * <td><code>MONTH + 1</code></td>
  233. * <td>{@link XMLGregorianCalendar#setMonth(int month)}</td>
  234. * </tr>
  235. * <tr>
  236. * <td><code>DAY_OF_MONTH</code></td>
  237. * <td>{@link XMLGregorianCalendar#setDay(int day)}</td>
  238. * </tr>
  239. * <tr>
  240. * <td><code>HOUR_OF_DAY, MINUTE, SECOND, MILLISECOND</code></td>
  241. * <td>{@link XMLGregorianCalendar#setTime(int hour, int minute, int second, BigDecimal fractional)}</td>
  242. * </tr>
  243. * <tr>
  244. * <td>
  245. * <code>(ZONE_OFFSET + DST_OFFSET) / (60*1000)</code><br/>
  246. * <em>(in minutes)</em>
  247. * </td>
  248. * <td>{@link XMLGregorianCalendar#setTimezone(int offset)}<sup><em>*</em></sup>
  249. * </td>
  250. * </tr>
  251. * </tbody>
  252. * </table>
  253. * <p><em>*</em>conversion loss of information. It is not possible to represent
  254. * a <code>java.util.GregorianCalendar</code> daylight savings timezone id in the
  255. * XML Schema 1.0 date/time datatype representation.</p>
  256. *
  257. * <p>To compute the return value's <code>TimeZone</code> field,
  258. * <ul>
  259. * <li>when <code>this.getTimezone() != FIELD_UNDEFINED</code>,
  260. * create a <code>java.util.TimeZone</code> with a custom timezone id
  261. * using the <code>this.getTimezone()</code>.</li>
  262. * <li>else use the <code>GregorianCalendar</code> default timezone value
  263. * for the host is defined as specified by
  264. * <code>java.util.TimeZone.getDefault()</code>.</li></p>
  265. *
  266. * @param cal <code>java.util.GregorianCalendar</code> used to create <code>XMLGregorianCalendar</code>
  267. *
  268. * @return <code>XMLGregorianCalendar</code> created from <code>java.util.GregorianCalendar</code>
  269. *
  270. * @throws NullPointerException If <code>cal</code> is <code>null</code>.
  271. */
  272. public XMLGregorianCalendar newXMLGregorianCalendar(final GregorianCalendar cal) {
  273. return new XMLGregorianCalendarImpl(cal);
  274. }
  275. /**
  276. * <p>Constructor allowing for complete value spaces allowed by
  277. * W3C XML Schema 1.0 recommendation for xsd:dateTime and related
  278. * builtin datatypes. Note that <code>year</code> parameter supports
  279. * arbitrarily large numbers and fractionalSecond has infinite
  280. * precision.</p>
  281. *
  282. * @param year of <code>XMLGregorianCalendar</code> to be created.
  283. * @param month of <code>XMLGregorianCalendar</code> to be created.
  284. * @param day of <code>XMLGregorianCalendar</code> to be created.
  285. * @param hour of <code>XMLGregorianCalendar</code> to be created.
  286. * @param minute of <code>XMLGregorianCalendar</code> to be created.
  287. * @param second of <code>XMLGregorianCalendar</code> to be created.
  288. * @param fractionalSecond of <code>XMLGregorianCalendar</code> to be created.
  289. * @param timezone of <code>XMLGregorianCalendar</code> to be created.
  290. *
  291. * @return <code>XMLGregorianCalendar</code> created from specified values.
  292. *
  293. * @throws IllegalArgumentException If any individual parameter's value is outside the maximum value constraint for the field
  294. * as determined by the Date/Time Data Mapping table in {@link XMLGregorianCalendar}
  295. * or if the composite values constitute an invalid <code>XMLGregorianCalendar</code> instance
  296. * as determined by {@link XMLGregorianCalendar#isValid()}.
  297. * @throws NullPointerException If any parameters are <code>null</code>.
  298. *
  299. */
  300. public XMLGregorianCalendar newXMLGregorianCalendar(
  301. final BigInteger year,
  302. final int month,
  303. final int day,
  304. final int hour,
  305. final int minute,
  306. final int second,
  307. final BigDecimal fractionalSecond,
  308. final int timezone) {
  309. return new XMLGregorianCalendarImpl(
  310. year,
  311. month,
  312. day,
  313. hour,
  314. minute,
  315. second,
  316. fractionalSecond,
  317. timezone
  318. );
  319. }
  320. }