XmlRpcDateTimeFormat.java
Go to the documentation of this file.
00001 /*
00002  * Licensed to the Apache Software Foundation (ASF) under one
00003  * or more contributor license agreements.  See the NOTICE file
00004  * distributed with this work for additional information
00005  * regarding copyright ownership.  The ASF licenses this file
00006  * to you under the Apache License, Version 2.0 (the
00007  * "License"); you may not use this file except in compliance
00008  * with the License.  You may obtain a copy of the License at
00009  *
00010  *   http://www.apache.org/licenses/LICENSE-2.0
00011  *
00012  * Unless required by applicable law or agreed to in writing,
00013  * software distributed under the License is distributed on an
00014  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
00015  * KIND, either express or implied.  See the License for the
00016  * specific language governing permissions and limitations
00017  * under the License.    
00018  */
00019 package org.apache.xmlrpc.util;
00020 
00021 
00022 import java.text.FieldPosition;
00023 import java.text.Format;
00024 import java.text.ParsePosition;
00025 import java.util.Calendar;
00026 import java.util.TimeZone;
00027 
00028 
00038 public abstract class XmlRpcDateTimeFormat extends Format {
00039     private static final long serialVersionUID = -8008230377361175138L;
00040 
00044     protected abstract TimeZone getTimeZone();
00045 
00046     private int parseInt(String pString, int pOffset, StringBuffer pDigits, int pMaxDigits) {
00047         int length = pString.length();
00048         pDigits.setLength(0);
00049         while (pMaxDigits-- > 0  &&  pOffset < length) {
00050             char c = pString.charAt(pOffset);
00051             if (Character.isDigit(c)) {
00052                 pDigits.append(c);
00053                 ++pOffset;
00054             } else {
00055                 break;
00056             }
00057         }
00058         return pOffset;
00059     }
00060 
00061     public Object parseObject(String pString, ParsePosition pParsePosition) {
00062         if (pString == null) {
00063             throw new NullPointerException("The String argument must not be null.");
00064         }
00065         if (pParsePosition == null) {
00066             throw new NullPointerException("The ParsePosition argument must not be null.");
00067         }
00068         int offset = pParsePosition.getIndex();
00069         int length = pString.length();
00070 
00071         StringBuffer digits = new StringBuffer();
00072         int year, month, mday;
00073 
00074         offset = parseInt(pString, offset, digits, 4);
00075         if (digits.length() < 4) {
00076             pParsePosition.setErrorIndex(offset);
00077             return null;
00078         }
00079         year = Integer.parseInt(digits.toString());
00080         
00081         offset = parseInt(pString, offset, digits, 2);
00082         if (digits.length() != 2) {
00083             pParsePosition.setErrorIndex(offset);
00084             return null;
00085         }
00086         month = Integer.parseInt(digits.toString());
00087         
00088         offset = parseInt(pString, offset, digits, 2);
00089         if (digits.length() != 2) {
00090             pParsePosition.setErrorIndex(offset);
00091             return null;
00092         }
00093         mday = Integer.parseInt(digits.toString());
00094 
00095         if (offset < length  &&  pString.charAt(offset) == 'T') {
00096             ++offset;
00097         } else {
00098             pParsePosition.setErrorIndex(offset);
00099             return null;
00100         }
00101 
00102         int hour, minute, second;
00103         offset = parseInt(pString, offset, digits, 2);
00104         if (digits.length() != 2) {
00105             pParsePosition.setErrorIndex(offset);
00106             return null;
00107         }
00108         hour = Integer.parseInt(digits.toString());
00109         
00110         if (offset < length  &&  pString.charAt(offset) == ':') {
00111             ++offset;
00112         } else {
00113             pParsePosition.setErrorIndex(offset);
00114             return null;
00115         }
00116         
00117         offset = parseInt(pString, offset, digits, 2);
00118         if (digits.length() != 2) {
00119             pParsePosition.setErrorIndex(offset);
00120             return null;
00121         }
00122         minute = Integer.parseInt(digits.toString());
00123         
00124         if (offset < length  &&  pString.charAt(offset) == ':') {
00125             ++offset;
00126         } else {
00127             pParsePosition.setErrorIndex(offset);
00128             return null;
00129         }
00130         
00131         offset = parseInt(pString, offset, digits, 2);
00132         if (digits.length() != 2) {
00133             pParsePosition.setErrorIndex(offset);
00134             return null;
00135         }
00136         second = Integer.parseInt(digits.toString());
00137         
00138         Calendar cal = Calendar.getInstance(getTimeZone());
00139         cal.set(year, month-1, mday, hour, minute, second);
00140         cal.set(Calendar.MILLISECOND, 0);
00141         pParsePosition.setIndex(offset);
00142         return cal;
00143     }
00144 
00145     private void append(StringBuffer pBuffer, int pNum, int pMinLen) {
00146         String s = Integer.toString(pNum);
00147         for (int i = s.length();  i < pMinLen;  i++) {
00148             pBuffer.append('0');
00149         }
00150         pBuffer.append(s);
00151     }
00152 
00153     public StringBuffer format(Object pCalendar, StringBuffer pBuffer, FieldPosition pPos) {
00154         if (pCalendar == null) {
00155             throw new NullPointerException("The Calendar argument must not be null.");
00156         }
00157         if (pBuffer == null) {
00158             throw new NullPointerException("The StringBuffer argument must not be null.");
00159         }
00160         if (pPos == null) {
00161             throw new NullPointerException("The FieldPosition argument must not be null.");
00162         }
00163 
00164         Calendar cal = (Calendar) pCalendar;
00165         int year = cal.get(Calendar.YEAR);
00166         append(pBuffer, year, 4);
00167         append(pBuffer, cal.get(Calendar.MONTH)+1, 2);
00168         append(pBuffer, cal.get(Calendar.DAY_OF_MONTH), 2);
00169         pBuffer.append('T');
00170         append(pBuffer, cal.get(Calendar.HOUR_OF_DAY), 2);
00171         pBuffer.append(':');
00172         append(pBuffer, cal.get(Calendar.MINUTE), 2);
00173         pBuffer.append(':');
00174         append(pBuffer, cal.get(Calendar.SECOND), 2);
00175         return pBuffer;
00176     }
00177 }


rosjava_core
Author(s):
autogenerated on Wed Aug 26 2015 16:06:49