1. /*
  2. * The Apache Software License, Version 1.1
  3. *
  4. *
  5. * Copyright (c) 1999-2002 The Apache Software Foundation. All rights
  6. * reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. The end-user documentation included with the redistribution,
  21. * if any, must include the following acknowledgment:
  22. * "This product includes software developed by the
  23. * Apache Software Foundation (http://www.apache.org/)."
  24. * Alternately, this acknowledgment may appear in the software itself,
  25. * if and wherever such third-party acknowledgments normally appear.
  26. *
  27. * 4. The names "Xerces" and "Apache Software Foundation" must
  28. * not be used to endorse or promote products derived from this
  29. * software without prior written permission. For written
  30. * permission, please contact apache@apache.org.
  31. *
  32. * 5. Products derived from this software may not be called "Apache",
  33. * nor may "Apache" appear in their name, without prior written
  34. * permission of the Apache Software Foundation.
  35. *
  36. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
  37. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
  38. * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  39. * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR
  40. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  41. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  42. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
  43. * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  44. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  45. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
  46. * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  47. * SUCH DAMAGE.
  48. * ====================================================================
  49. *
  50. * This software consists of voluntary contributions made by many
  51. * individuals on behalf of the Apache Software Foundation and was
  52. * originally based on software copyright (c) 2001, International
  53. * Business Machines, Inc., http://www.apache.org. For more
  54. * information on the Apache Software Foundation, please see
  55. * <http://www.apache.org/>.
  56. */
  57. package com.sun.org.apache.xerces.internal.impl.dv.xs;
  58. import com.sun.org.apache.xerces.internal.impl.dv.InvalidDatatypeValueException;
  59. import com.sun.org.apache.xerces.internal.impl.dv.ValidationContext;
  60. /**
  61. * Validator for <duration> datatype (W3C Schema Datatypes)
  62. *
  63. * @author Elena Litani
  64. * @author Gopal Sharma, SUN Microsystem Inc.
  65. * @version $Id: DurationDV.java,v 1.8 2003/12/11 15:08:25 sandygao Exp $
  66. */
  67. public class DurationDV extends AbstractDateTimeDV {
  68. // order-relation on duration is a partial order. The dates below are used to
  69. // for comparison of 2 durations, based on the fact that
  70. // duration x and y is x<=y iff s+x<=s+y
  71. // see 3.2.6 duration W3C schema datatype specs
  72. //
  73. // the dates are in format: {CCYY,MM,DD, H, S, M, MS, timezone}
  74. private final static int[][] DATETIMES= {
  75. {1696, 9, 1, 0, 0, 0, 0, 'Z'},
  76. {1697, 2, 1, 0, 0, 0, 0, 'Z'},
  77. {1903, 3, 1, 0, 0, 0, 0, 'Z'},
  78. {1903, 7, 1, 0, 0, 0, 0, 'Z'}};
  79. public Object getActualValue(String content, ValidationContext context) throws InvalidDatatypeValueException{
  80. try{
  81. return new DateTimeData(parse(content), this);
  82. } catch (Exception ex) {
  83. throw new InvalidDatatypeValueException("cvc-datatype-valid.1.2.1", new Object[]{content, "duration"});
  84. }
  85. }
  86. /**
  87. * Parses, validates and computes normalized version of duration object
  88. *
  89. * @param str The lexical representation of duration object PnYn MnDTnH nMnS
  90. * @param date uninitialized date object
  91. * @return normalized date representation
  92. * @exception SchemaDateTimeException Invalid lexical representation
  93. */
  94. protected int[] parse(String str) throws SchemaDateTimeException{
  95. int len = str.length();
  96. int[] date=new int[TOTAL_SIZE];
  97. int start = 0;
  98. char c=str.charAt(start++);
  99. if ( c!='P' && c!='-' ) {
  100. throw new SchemaDateTimeException();
  101. }
  102. else {
  103. date[utc]=(c=='-')?'-':0;
  104. if ( c=='-' && str.charAt(start++)!='P' ) {
  105. throw new SchemaDateTimeException();
  106. }
  107. }
  108. int negate = 1;
  109. //negative duration
  110. if ( date[utc]=='-' ) {
  111. negate = -1;
  112. }
  113. //at least one number and designator must be seen after P
  114. boolean designator = false;
  115. int endDate = indexOf (str, start, len, 'T');
  116. if ( endDate == -1 ) {
  117. endDate = len;
  118. }
  119. //find 'Y'
  120. int end = indexOf (str, start, endDate, 'Y');
  121. if ( end!=-1 ) {
  122. //scan year
  123. date[CY]=negate * parseInt(str,start,end);
  124. start = end+1;
  125. designator = true;
  126. }
  127. end = indexOf (str, start, endDate, 'M');
  128. if ( end!=-1 ) {
  129. //scan month
  130. date[M]=negate * parseInt(str,start,end);
  131. start = end+1;
  132. designator = true;
  133. }
  134. end = indexOf (str, start, endDate, 'D');
  135. if ( end!=-1 ) {
  136. //scan day
  137. date[D]=negate * parseInt(str,start,end);
  138. start = end+1;
  139. designator = true;
  140. }
  141. if ( len == endDate && start!=len ) {
  142. throw new SchemaDateTimeException();
  143. }
  144. if ( len !=endDate ) {
  145. //scan hours, minutes, seconds
  146. //REVISIT: can any item include a decimal fraction or only seconds?
  147. //
  148. end = indexOf (str, ++start, len, 'H');
  149. if ( end!=-1 ) {
  150. //scan hours
  151. date[h]=negate * parseInt(str,start,end);
  152. start=end+1;
  153. designator = true;
  154. }
  155. end = indexOf (str, start, len, 'M');
  156. if ( end!=-1 ) {
  157. //scan min
  158. date[m]=negate * parseInt(str,start,end);
  159. start=end+1;
  160. designator = true;
  161. }
  162. end = indexOf (str, start, len, 'S');
  163. if ( end!=-1 ) {
  164. //scan seconds
  165. int mlsec = indexOf (str, start, end, '.');
  166. if ( mlsec >0 ) {
  167. date[s] = negate * parseInt (str, start, mlsec);
  168. date[ms] = negate * parseInt (str, mlsec+1, end);
  169. }
  170. else {
  171. date[s]=negate * parseInt(str, start,end);
  172. }
  173. start=end+1;
  174. designator = true;
  175. }
  176. // no additional data shouls appear after last item
  177. // P1Y1M1DT is illigal value as well
  178. if ( start != len || str.charAt(--start)=='T' ) {
  179. throw new SchemaDateTimeException();
  180. }
  181. }
  182. if ( !designator ) {
  183. throw new SchemaDateTimeException();
  184. }
  185. return date;
  186. }
  187. /**
  188. * Compares 2 given durations. (refer to W3C Schema Datatypes "3.2.6 duration")
  189. *
  190. * @param date1 Unnormalized duration
  191. * @param date2 Unnormalized duration
  192. * @param strict (min/max)Exclusive strict == true ( LESS_THAN ) or ( GREATER_THAN )
  193. * (min/max)Inclusive strict == false (LESS_EQUAL) or (GREATER_EQUAL)
  194. * @return INDETERMINATE if the order relationship between date1 and date2 is indeterminate.
  195. * EQUAL if the order relation between date1 and date2 is EQUAL.
  196. * If the strict parameter is true, return LESS_THAN if date1 is less than date2 and
  197. * return GREATER_THAN if date1 is greater than date2.
  198. * If the strict parameter is false, return LESS_THAN if date1 is less than OR equal to date2 and
  199. * return GREATER_THAN if date1 is greater than OR equal to date2
  200. */
  201. protected short compareDates(int[] date1, int[] date2, boolean strict) {
  202. //REVISIT: this is unoptimazed vs of comparing 2 durations
  203. // Algorithm is described in 3.2.6.2 W3C Schema Datatype specs
  204. //
  205. //add constA to both durations
  206. short resultA, resultB= INDETERMINATE;
  207. //try and see if the objects are equal
  208. resultA = compareOrder (date1, date2);
  209. if ( resultA == 0 ) {
  210. return 0;
  211. }
  212. int[][] result = new int[2][TOTAL_SIZE];
  213. //long comparison algorithm is required
  214. int[] tempA = addDuration (date1, DATETIMES[0], result[0]);
  215. int[] tempB = addDuration (date2, DATETIMES[0], result[1]);
  216. resultA = compareOrder(tempA, tempB);
  217. if ( resultA == INDETERMINATE ) {
  218. return INDETERMINATE;
  219. }
  220. tempA = addDuration(date1, DATETIMES[1], result[0]);
  221. tempB = addDuration(date2, DATETIMES[1], result[1]);
  222. resultB = compareOrder(tempA, tempB);
  223. resultA = compareResults(resultA, resultB, strict);
  224. if (resultA == INDETERMINATE) {
  225. return INDETERMINATE;
  226. }
  227. tempA = addDuration(date1, DATETIMES[2], result[0]);
  228. tempB = addDuration(date2, DATETIMES[2], result[1]);
  229. resultB = compareOrder(tempA, tempB);
  230. resultA = compareResults(resultA, resultB, strict);
  231. if (resultA == INDETERMINATE) {
  232. return INDETERMINATE;
  233. }
  234. tempA = addDuration(date1, DATETIMES[3], result[0]);
  235. tempB = addDuration(date2, DATETIMES[3], result[1]);
  236. resultB = compareOrder(tempA, tempB);
  237. resultA = compareResults(resultA, resultB, strict);
  238. return resultA;
  239. }
  240. private short compareResults(short resultA, short resultB, boolean strict){
  241. if ( resultB == INDETERMINATE ) {
  242. return INDETERMINATE;
  243. }
  244. else if ( resultA!=resultB && strict ) {
  245. return INDETERMINATE;
  246. }
  247. else if ( resultA!=resultB && !strict ) {
  248. if ( resultA!=0 && resultB!=0 ) {
  249. return INDETERMINATE;
  250. }
  251. else {
  252. return (resultA!=0)?resultA:resultB;
  253. }
  254. }
  255. return resultA;
  256. }
  257. private int[] addDuration(int[] date, int[] addto, int[] duration) {
  258. //REVISIT: some code could be shared between normalize() and this method,
  259. // however is it worth moving it? The structures are different...
  260. //
  261. resetDateObj(duration);
  262. //add months (may be modified additionaly below)
  263. int temp = addto[M] + date[M];
  264. duration[M] = modulo (temp, 1, 13);
  265. int carry = fQuotient (temp, 1, 13);
  266. //add years (may be modified additionaly below)
  267. duration[CY]=addto[CY] + date[CY] + carry;
  268. //add seconds
  269. temp = addto[s] + date[s];
  270. carry = fQuotient (temp, 60);
  271. duration[s] = mod(temp, 60, carry);
  272. //add minutes
  273. temp = addto[m] +date[m] + carry;
  274. carry = fQuotient (temp, 60);
  275. duration[m]= mod(temp, 60, carry);
  276. //add hours
  277. temp = addto[h] + date[h] + carry;
  278. carry = fQuotient(temp, 24);
  279. duration[h] = mod(temp, 24, carry);
  280. duration[D]=addto[D] + date[D] + carry;
  281. while ( true ) {
  282. temp=maxDayInMonthFor(duration[CY], duration[M]);
  283. if ( duration[D] < 1 ) { //original duration was negative
  284. duration[D] = duration[D] + maxDayInMonthFor(duration[CY], duration[M]-1);
  285. carry=-1;
  286. }
  287. else if ( duration[D] > temp ) {
  288. duration[D] = duration[D] - temp;
  289. carry=1;
  290. }
  291. else {
  292. break;
  293. }
  294. temp = duration[M]+carry;
  295. duration[M] = modulo(temp, 1, 13);
  296. duration[CY] = duration[CY]+fQuotient(temp, 1, 13);
  297. }
  298. duration[utc]='Z';
  299. return duration;
  300. }
  301. protected String dateToString(int[] date) {
  302. StringBuffer message = new StringBuffer(30);
  303. int negate = 1;
  304. if ( date[CY]<0 ) {
  305. message.append('-');
  306. negate=-1;
  307. }
  308. message.append('P');
  309. message.append(negate * date[CY]);
  310. message.append('Y');
  311. message.append(negate * date[M]);
  312. message.append('M');
  313. message.append(negate * date[D]);
  314. message.append('D');
  315. message.append('T');
  316. message.append(negate * date[h]);
  317. message.append('H');
  318. message.append(negate * date[m]);
  319. message.append('M');
  320. message.append(negate * date[s]);
  321. message.append('.');
  322. message.append(negate * date[ms]);
  323. message.append('S');
  324. return message.toString();
  325. }
  326. }