Fri, 14 Feb 2014 10:53:55 +0100
8025030: Enhance stream handling
Summary: Avoiding caching data initialized via TCCL in static context; fix also reviewed by Iaroslav Savytskyi, Alexander Fomin
Reviewed-by: ahgross, mgrebac, skoivu
ohair@286 | 1 | /* |
mkos@514 | 2 | * Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
ohair@286 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
ohair@286 | 4 | * |
ohair@286 | 5 | * This code is free software; you can redistribute it and/or modify it |
ohair@286 | 6 | * under the terms of the GNU General Public License version 2 only, as |
ohair@286 | 7 | * published by the Free Software Foundation. Oracle designates this |
ohair@286 | 8 | * particular file as subject to the "Classpath" exception as provided |
ohair@286 | 9 | * by Oracle in the LICENSE file that accompanied this code. |
ohair@286 | 10 | * |
ohair@286 | 11 | * This code is distributed in the hope that it will be useful, but WITHOUT |
ohair@286 | 12 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
ohair@286 | 13 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
ohair@286 | 14 | * version 2 for more details (a copy is included in the LICENSE file that |
ohair@286 | 15 | * accompanied this code). |
ohair@286 | 16 | * |
ohair@286 | 17 | * You should have received a copy of the GNU General Public License version |
ohair@286 | 18 | * 2 along with this work; if not, write to the Free Software Foundation, |
ohair@286 | 19 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
ohair@286 | 20 | * |
ohair@286 | 21 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
ohair@286 | 22 | * or visit www.oracle.com if you need additional information or have any |
ohair@286 | 23 | * questions. |
ohair@286 | 24 | */ |
ohair@286 | 25 | |
ohair@286 | 26 | package com.sun.xml.internal.bind; |
ohair@286 | 27 | |
ohair@286 | 28 | import java.math.BigDecimal; |
ohair@286 | 29 | import java.math.BigInteger; |
mkos@514 | 30 | import java.security.AccessController; |
mkos@514 | 31 | import java.security.PrivilegedAction; |
ohair@286 | 32 | import java.util.Calendar; |
mkos@514 | 33 | import java.util.Collections; |
ohair@286 | 34 | import java.util.GregorianCalendar; |
mkos@514 | 35 | import java.util.Map; |
ohair@286 | 36 | import java.util.TimeZone; |
mkos@514 | 37 | import java.util.WeakHashMap; |
ohair@286 | 38 | |
ohair@286 | 39 | import javax.xml.bind.DatatypeConverter; |
ohair@286 | 40 | import javax.xml.bind.DatatypeConverterInterface; |
ohair@286 | 41 | import javax.xml.datatype.DatatypeConfigurationException; |
ohair@286 | 42 | import javax.xml.datatype.DatatypeFactory; |
ohair@286 | 43 | import javax.xml.namespace.NamespaceContext; |
ohair@286 | 44 | import javax.xml.namespace.QName; |
ohair@286 | 45 | import javax.xml.stream.XMLStreamException; |
ohair@286 | 46 | import javax.xml.stream.XMLStreamWriter; |
ohair@286 | 47 | |
ohair@286 | 48 | /** |
ohair@286 | 49 | * This class is the JAXB RI's default implementation of the |
ohair@286 | 50 | * {@link DatatypeConverterInterface}. |
ohair@286 | 51 | * |
ohair@286 | 52 | * <p> |
ohair@286 | 53 | * When client applications specify the use of the static print/parse |
ohair@286 | 54 | * methods in {@link DatatypeConverter}, it will delegate |
ohair@286 | 55 | * to this class. |
ohair@286 | 56 | * |
ohair@286 | 57 | * <p> |
ohair@286 | 58 | * This class is responsible for whitespace normalization. |
ohair@286 | 59 | * |
ohair@286 | 60 | * @author <ul><li>Ryan Shoemaker, Martin Grebac</li></ul> |
ohair@286 | 61 | * @since JAXB1.0 |
ohair@286 | 62 | * @deprecated in JAXB 2.2.4 - use javax.xml.bind.DatatypeConverterImpl instead |
ohair@286 | 63 | * or let us know why you can't |
ohair@286 | 64 | */ |
alanb@368 | 65 | @Deprecated |
alanb@368 | 66 | public final class DatatypeConverterImpl implements DatatypeConverterInterface { |
alanb@368 | 67 | |
alanb@368 | 68 | @Deprecated |
alanb@368 | 69 | public static final DatatypeConverterInterface theInstance = new DatatypeConverterImpl(); |
ohair@286 | 70 | |
ohair@286 | 71 | protected DatatypeConverterImpl() { |
ohair@286 | 72 | // shall not be used |
ohair@286 | 73 | } |
ohair@286 | 74 | |
ohair@286 | 75 | public static BigInteger _parseInteger(CharSequence s) { |
ohair@286 | 76 | return new BigInteger(removeOptionalPlus(WhiteSpaceProcessor.trim(s)).toString()); |
ohair@286 | 77 | } |
ohair@286 | 78 | |
ohair@286 | 79 | public static String _printInteger(BigInteger val) { |
ohair@286 | 80 | return val.toString(); |
ohair@286 | 81 | } |
ohair@286 | 82 | |
ohair@286 | 83 | /** |
ohair@286 | 84 | * Faster but less robust String->int conversion. |
ohair@286 | 85 | * |
ohair@286 | 86 | * Note that: |
ohair@286 | 87 | * <ol> |
ohair@286 | 88 | * <li>XML Schema allows '+', but {@link Integer#valueOf(String)} is not. |
ohair@286 | 89 | * <li>XML Schema allows leading and trailing (but not in-between) whitespaces. |
ohair@286 | 90 | * {@link Integer#valueOf(String)} doesn't allow any. |
ohair@286 | 91 | * </ol> |
ohair@286 | 92 | */ |
ohair@286 | 93 | public static int _parseInt(CharSequence s) { |
ohair@286 | 94 | int len = s.length(); |
ohair@286 | 95 | int sign = 1; |
ohair@286 | 96 | |
ohair@286 | 97 | int r = 0; |
ohair@286 | 98 | |
ohair@286 | 99 | for (int i = 0; i < len; i++) { |
ohair@286 | 100 | char ch = s.charAt(i); |
ohair@286 | 101 | if (WhiteSpaceProcessor.isWhiteSpace(ch)) { |
ohair@286 | 102 | // skip whitespace |
ohair@286 | 103 | } else if ('0' <= ch && ch <= '9') { |
ohair@286 | 104 | r = r * 10 + (ch - '0'); |
ohair@286 | 105 | } else if (ch == '-') { |
ohair@286 | 106 | sign = -1; |
ohair@286 | 107 | } else if (ch == '+') { |
ohair@286 | 108 | // noop |
ohair@286 | 109 | } else { |
ohair@286 | 110 | throw new NumberFormatException("Not a number: " + s); |
ohair@286 | 111 | } |
ohair@286 | 112 | } |
ohair@286 | 113 | |
ohair@286 | 114 | return r * sign; |
ohair@286 | 115 | } |
ohair@286 | 116 | |
ohair@286 | 117 | public static long _parseLong(CharSequence s) { |
ohair@286 | 118 | return Long.valueOf(removeOptionalPlus(WhiteSpaceProcessor.trim(s)).toString()); |
ohair@286 | 119 | } |
ohair@286 | 120 | |
ohair@286 | 121 | public static short _parseShort(CharSequence s) { |
ohair@286 | 122 | return (short) _parseInt(s); |
ohair@286 | 123 | } |
ohair@286 | 124 | |
ohair@286 | 125 | public static String _printShort(short val) { |
ohair@286 | 126 | return String.valueOf(val); |
ohair@286 | 127 | } |
ohair@286 | 128 | |
ohair@286 | 129 | public static BigDecimal _parseDecimal(CharSequence content) { |
ohair@286 | 130 | content = WhiteSpaceProcessor.trim(content); |
ohair@286 | 131 | |
ohair@286 | 132 | if (content.length() <= 0) { |
ohair@286 | 133 | return null; |
ohair@286 | 134 | } |
ohair@286 | 135 | |
ohair@286 | 136 | return new BigDecimal(content.toString()); |
ohair@286 | 137 | |
ohair@286 | 138 | // from purely XML Schema perspective, |
ohair@286 | 139 | // this implementation has a problem, since |
ohair@286 | 140 | // in xs:decimal "1.0" and "1" is equal whereas the above |
ohair@286 | 141 | // code will return different values for those two forms. |
ohair@286 | 142 | // |
ohair@286 | 143 | // the code was originally using com.sun.msv.datatype.xsd.NumberType.load, |
ohair@286 | 144 | // but a profiling showed that the process of normalizing "1.0" into "1" |
ohair@286 | 145 | // could take non-trivial time. |
ohair@286 | 146 | // |
ohair@286 | 147 | // also, from the user's point of view, one might be surprised if |
ohair@286 | 148 | // 1 (not 1.0) is returned from "1.000" |
ohair@286 | 149 | } |
ohair@286 | 150 | |
ohair@286 | 151 | public static float _parseFloat(CharSequence _val) { |
ohair@286 | 152 | String s = WhiteSpaceProcessor.trim(_val).toString(); |
ohair@286 | 153 | /* Incompatibilities of XML Schema's float "xfloat" and Java's float "jfloat" |
ohair@286 | 154 | |
ohair@286 | 155 | * jfloat.valueOf ignores leading and trailing whitespaces, |
ohair@286 | 156 | whereas this is not allowed in xfloat. |
ohair@286 | 157 | * jfloat.valueOf allows "float type suffix" (f, F) to be |
ohair@286 | 158 | appended after float literal (e.g., 1.52e-2f), whereare |
ohair@286 | 159 | this is not the case of xfloat. |
ohair@286 | 160 | |
ohair@286 | 161 | gray zone |
ohair@286 | 162 | --------- |
ohair@286 | 163 | * jfloat allows ".523". And there is no clear statement that mentions |
ohair@286 | 164 | this case in xfloat. Although probably this is allowed. |
ohair@286 | 165 | * |
ohair@286 | 166 | */ |
ohair@286 | 167 | |
ohair@286 | 168 | if (s.equals("NaN")) { |
ohair@286 | 169 | return Float.NaN; |
ohair@286 | 170 | } |
ohair@286 | 171 | if (s.equals("INF")) { |
ohair@286 | 172 | return Float.POSITIVE_INFINITY; |
ohair@286 | 173 | } |
ohair@286 | 174 | if (s.equals("-INF")) { |
ohair@286 | 175 | return Float.NEGATIVE_INFINITY; |
ohair@286 | 176 | } |
ohair@286 | 177 | |
ohair@286 | 178 | if (s.length() == 0 |
ohair@286 | 179 | || !isDigitOrPeriodOrSign(s.charAt(0)) |
ohair@286 | 180 | || !isDigitOrPeriodOrSign(s.charAt(s.length() - 1))) { |
ohair@286 | 181 | throw new NumberFormatException(); |
ohair@286 | 182 | } |
ohair@286 | 183 | |
ohair@286 | 184 | // these screening process is necessary due to the wobble of Float.valueOf method |
ohair@286 | 185 | return Float.parseFloat(s); |
ohair@286 | 186 | } |
ohair@286 | 187 | |
ohair@286 | 188 | public static String _printFloat(float v) { |
ohair@286 | 189 | if (Float.isNaN(v)) { |
ohair@286 | 190 | return "NaN"; |
ohair@286 | 191 | } |
ohair@286 | 192 | if (v == Float.POSITIVE_INFINITY) { |
ohair@286 | 193 | return "INF"; |
ohair@286 | 194 | } |
ohair@286 | 195 | if (v == Float.NEGATIVE_INFINITY) { |
ohair@286 | 196 | return "-INF"; |
ohair@286 | 197 | } |
ohair@286 | 198 | return String.valueOf(v); |
ohair@286 | 199 | } |
ohair@286 | 200 | |
ohair@286 | 201 | public static double _parseDouble(CharSequence _val) { |
ohair@286 | 202 | String val = WhiteSpaceProcessor.trim(_val).toString(); |
ohair@286 | 203 | |
ohair@286 | 204 | if (val.equals("NaN")) { |
ohair@286 | 205 | return Double.NaN; |
ohair@286 | 206 | } |
ohair@286 | 207 | if (val.equals("INF")) { |
ohair@286 | 208 | return Double.POSITIVE_INFINITY; |
ohair@286 | 209 | } |
ohair@286 | 210 | if (val.equals("-INF")) { |
ohair@286 | 211 | return Double.NEGATIVE_INFINITY; |
ohair@286 | 212 | } |
ohair@286 | 213 | |
ohair@286 | 214 | if (val.length() == 0 |
ohair@286 | 215 | || !isDigitOrPeriodOrSign(val.charAt(0)) |
ohair@286 | 216 | || !isDigitOrPeriodOrSign(val.charAt(val.length() - 1))) { |
ohair@286 | 217 | throw new NumberFormatException(val); |
ohair@286 | 218 | } |
ohair@286 | 219 | |
ohair@286 | 220 | |
ohair@286 | 221 | // these screening process is necessary due to the wobble of Float.valueOf method |
ohair@286 | 222 | return Double.parseDouble(val); |
ohair@286 | 223 | } |
ohair@286 | 224 | |
ohair@286 | 225 | public static Boolean _parseBoolean(CharSequence literal) { |
ohair@286 | 226 | if (literal == null) { |
ohair@286 | 227 | return null; |
ohair@286 | 228 | } |
ohair@286 | 229 | |
ohair@286 | 230 | int i = 0; |
ohair@286 | 231 | int len = literal.length(); |
ohair@286 | 232 | char ch; |
ohair@286 | 233 | boolean value = false; |
ohair@286 | 234 | |
ohair@286 | 235 | if (literal.length() <= 0) { |
ohair@286 | 236 | return null; |
ohair@286 | 237 | } |
ohair@286 | 238 | |
ohair@286 | 239 | do { |
ohair@286 | 240 | ch = literal.charAt(i++); |
ohair@286 | 241 | } while (WhiteSpaceProcessor.isWhiteSpace(ch) && i < len); |
ohair@286 | 242 | |
ohair@286 | 243 | int strIndex = 0; |
ohair@286 | 244 | |
ohair@286 | 245 | switch (ch) { |
ohair@286 | 246 | case '1': |
ohair@286 | 247 | value = true; |
ohair@286 | 248 | break; |
ohair@286 | 249 | case '0': |
ohair@286 | 250 | value = false; |
ohair@286 | 251 | break; |
ohair@286 | 252 | case 't': |
ohair@286 | 253 | String strTrue = "rue"; |
ohair@286 | 254 | do { |
ohair@286 | 255 | ch = literal.charAt(i++); |
ohair@286 | 256 | } while ((strTrue.charAt(strIndex++) == ch) && i < len && strIndex < 3); |
ohair@286 | 257 | |
ohair@286 | 258 | if (strIndex == 3) { |
ohair@286 | 259 | value = true; |
ohair@286 | 260 | } else { |
ohair@286 | 261 | return false; |
ohair@286 | 262 | } |
ohair@286 | 263 | // throw new IllegalArgumentException("String \"" + literal + "\" is not valid boolean value."); |
ohair@286 | 264 | |
ohair@286 | 265 | break; |
ohair@286 | 266 | case 'f': |
ohair@286 | 267 | String strFalse = "alse"; |
ohair@286 | 268 | do { |
ohair@286 | 269 | ch = literal.charAt(i++); |
ohair@286 | 270 | } while ((strFalse.charAt(strIndex++) == ch) && i < len && strIndex < 4); |
ohair@286 | 271 | |
ohair@286 | 272 | |
ohair@286 | 273 | if (strIndex == 4) { |
ohair@286 | 274 | value = false; |
ohair@286 | 275 | } else { |
ohair@286 | 276 | return false; |
ohair@286 | 277 | } |
ohair@286 | 278 | // throw new IllegalArgumentException("String \"" + literal + "\" is not valid boolean value."); |
ohair@286 | 279 | |
ohair@286 | 280 | break; |
ohair@286 | 281 | } |
ohair@286 | 282 | |
ohair@286 | 283 | if (i < len) { |
ohair@286 | 284 | do { |
ohair@286 | 285 | ch = literal.charAt(i++); |
ohair@286 | 286 | } while (WhiteSpaceProcessor.isWhiteSpace(ch) && i < len); |
ohair@286 | 287 | } |
ohair@286 | 288 | |
ohair@286 | 289 | if (i == len) { |
ohair@286 | 290 | return value; |
ohair@286 | 291 | } else { |
ohair@286 | 292 | return null; |
ohair@286 | 293 | } |
ohair@286 | 294 | // throw new IllegalArgumentException("String \"" + literal + "\" is not valid boolean value."); |
ohair@286 | 295 | } |
ohair@286 | 296 | |
ohair@286 | 297 | public static String _printBoolean(boolean val) { |
ohair@286 | 298 | return val ? "true" : "false"; |
ohair@286 | 299 | } |
ohair@286 | 300 | |
ohair@286 | 301 | public static byte _parseByte(CharSequence literal) { |
ohair@286 | 302 | return (byte) _parseInt(literal); |
ohair@286 | 303 | } |
ohair@286 | 304 | |
ohair@286 | 305 | public static String _printByte(byte val) { |
ohair@286 | 306 | return String.valueOf(val); |
ohair@286 | 307 | } |
ohair@286 | 308 | |
ohair@286 | 309 | /** |
ohair@286 | 310 | * @return null if fails to convert. |
ohair@286 | 311 | */ |
ohair@286 | 312 | public static QName _parseQName(CharSequence text, NamespaceContext nsc) { |
ohair@286 | 313 | int length = text.length(); |
ohair@286 | 314 | |
ohair@286 | 315 | // trim whitespace |
ohair@286 | 316 | int start = 0; |
ohair@286 | 317 | while (start < length && WhiteSpaceProcessor.isWhiteSpace(text.charAt(start))) { |
ohair@286 | 318 | start++; |
ohair@286 | 319 | } |
ohair@286 | 320 | |
ohair@286 | 321 | int end = length; |
ohair@286 | 322 | while (end > start && WhiteSpaceProcessor.isWhiteSpace(text.charAt(end - 1))) { |
ohair@286 | 323 | end--; |
ohair@286 | 324 | } |
ohair@286 | 325 | |
ohair@286 | 326 | if (end == start) { |
ohair@286 | 327 | throw new IllegalArgumentException("input is empty"); |
ohair@286 | 328 | } |
ohair@286 | 329 | |
ohair@286 | 330 | |
ohair@286 | 331 | String uri; |
ohair@286 | 332 | String localPart; |
ohair@286 | 333 | String prefix; |
ohair@286 | 334 | |
ohair@286 | 335 | // search ':' |
ohair@286 | 336 | int idx = start + 1; // no point in searching the first char. that's not valid. |
ohair@286 | 337 | while (idx < end && text.charAt(idx) != ':') { |
ohair@286 | 338 | idx++; |
ohair@286 | 339 | } |
ohair@286 | 340 | |
ohair@286 | 341 | if (idx == end) { |
ohair@286 | 342 | uri = nsc.getNamespaceURI(""); |
ohair@286 | 343 | localPart = text.subSequence(start, end).toString(); |
ohair@286 | 344 | prefix = ""; |
ohair@286 | 345 | } else { |
ohair@286 | 346 | // Prefix exists, check everything |
ohair@286 | 347 | prefix = text.subSequence(start, idx).toString(); |
ohair@286 | 348 | localPart = text.subSequence(idx + 1, end).toString(); |
ohair@286 | 349 | uri = nsc.getNamespaceURI(prefix); |
ohair@286 | 350 | // uri can never be null according to javadoc, |
ohair@286 | 351 | // but some users reported that there are implementations that return null. |
ohair@286 | 352 | if (uri == null || uri.length() == 0) // crap. the NamespaceContext interface is broken. |
ohair@286 | 353 | // error: unbound prefix |
ohair@286 | 354 | { |
ohair@286 | 355 | throw new IllegalArgumentException("prefix " + prefix + " is not bound to a namespace"); |
ohair@286 | 356 | } |
ohair@286 | 357 | } |
ohair@286 | 358 | |
ohair@286 | 359 | return new QName(uri, localPart, prefix); |
ohair@286 | 360 | } |
ohair@286 | 361 | |
ohair@286 | 362 | public static GregorianCalendar _parseDateTime(CharSequence s) { |
ohair@286 | 363 | String val = WhiteSpaceProcessor.trim(s).toString(); |
mkos@514 | 364 | return getDatatypeFactory().newXMLGregorianCalendar(val).toGregorianCalendar(); |
ohair@286 | 365 | } |
ohair@286 | 366 | |
ohair@286 | 367 | public static String _printDateTime(Calendar val) { |
ohair@286 | 368 | return CalendarFormatter.doFormat("%Y-%M-%DT%h:%m:%s%z", val); |
ohair@286 | 369 | } |
ohair@286 | 370 | |
ohair@286 | 371 | public static String _printDate(Calendar val) { |
ohair@286 | 372 | return CalendarFormatter.doFormat((new StringBuilder("%Y-%M-%D").append("%z")).toString(),val); |
ohair@286 | 373 | } |
ohair@286 | 374 | |
ohair@286 | 375 | public static String _printInt(int val) { |
ohair@286 | 376 | return String.valueOf(val); |
ohair@286 | 377 | } |
ohair@286 | 378 | |
ohair@286 | 379 | public static String _printLong(long val) { |
ohair@286 | 380 | return String.valueOf(val); |
ohair@286 | 381 | } |
ohair@286 | 382 | |
ohair@286 | 383 | public static String _printDecimal(BigDecimal val) { |
ohair@286 | 384 | return val.toPlainString(); |
ohair@286 | 385 | } |
ohair@286 | 386 | |
ohair@286 | 387 | public static String _printDouble(double v) { |
ohair@286 | 388 | if (Double.isNaN(v)) { |
ohair@286 | 389 | return "NaN"; |
ohair@286 | 390 | } |
ohair@286 | 391 | if (v == Double.POSITIVE_INFINITY) { |
ohair@286 | 392 | return "INF"; |
ohair@286 | 393 | } |
ohair@286 | 394 | if (v == Double.NEGATIVE_INFINITY) { |
ohair@286 | 395 | return "-INF"; |
ohair@286 | 396 | } |
ohair@286 | 397 | return String.valueOf(v); |
ohair@286 | 398 | } |
ohair@286 | 399 | |
ohair@286 | 400 | public static String _printQName(QName val, NamespaceContext nsc) { |
ohair@286 | 401 | // Double-check |
ohair@286 | 402 | String qname; |
ohair@286 | 403 | String prefix = nsc.getPrefix(val.getNamespaceURI()); |
ohair@286 | 404 | String localPart = val.getLocalPart(); |
ohair@286 | 405 | |
ohair@286 | 406 | if (prefix == null || prefix.length() == 0) { // be defensive |
ohair@286 | 407 | qname = localPart; |
ohair@286 | 408 | } else { |
ohair@286 | 409 | qname = prefix + ':' + localPart; |
ohair@286 | 410 | } |
ohair@286 | 411 | |
ohair@286 | 412 | return qname; |
ohair@286 | 413 | } |
ohair@286 | 414 | |
ohair@286 | 415 | // base64 decoder |
ohair@286 | 416 | private static final byte[] decodeMap = initDecodeMap(); |
ohair@286 | 417 | private static final byte PADDING = 127; |
ohair@286 | 418 | |
ohair@286 | 419 | private static byte[] initDecodeMap() { |
ohair@286 | 420 | byte[] map = new byte[128]; |
ohair@286 | 421 | int i; |
ohair@286 | 422 | for (i = 0; i < 128; i++) { |
ohair@286 | 423 | map[i] = -1; |
ohair@286 | 424 | } |
ohair@286 | 425 | |
ohair@286 | 426 | for (i = 'A'; i <= 'Z'; i++) { |
ohair@286 | 427 | map[i] = (byte) (i - 'A'); |
ohair@286 | 428 | } |
ohair@286 | 429 | for (i = 'a'; i <= 'z'; i++) { |
ohair@286 | 430 | map[i] = (byte) (i - 'a' + 26); |
ohair@286 | 431 | } |
ohair@286 | 432 | for (i = '0'; i <= '9'; i++) { |
ohair@286 | 433 | map[i] = (byte) (i - '0' + 52); |
ohair@286 | 434 | } |
ohair@286 | 435 | map['+'] = 62; |
ohair@286 | 436 | map['/'] = 63; |
ohair@286 | 437 | map['='] = PADDING; |
ohair@286 | 438 | |
ohair@286 | 439 | return map; |
ohair@286 | 440 | } |
ohair@286 | 441 | |
ohair@286 | 442 | /** |
ohair@286 | 443 | * computes the length of binary data speculatively. |
ohair@286 | 444 | * |
ohair@286 | 445 | * <p> |
ohair@286 | 446 | * Our requirement is to create byte[] of the exact length to store the binary data. |
ohair@286 | 447 | * If we do this in a straight-forward way, it takes two passes over the data. |
ohair@286 | 448 | * Experiments show that this is a non-trivial overhead (35% or so is spent on |
ohair@286 | 449 | * the first pass in calculating the length.) |
ohair@286 | 450 | * |
ohair@286 | 451 | * <p> |
ohair@286 | 452 | * So the approach here is that we compute the length speculatively, without looking |
ohair@286 | 453 | * at the whole contents. The obtained speculative value is never less than the |
ohair@286 | 454 | * actual length of the binary data, but it may be bigger. So if the speculation |
ohair@286 | 455 | * goes wrong, we'll pay the cost of reallocation and buffer copying. |
ohair@286 | 456 | * |
ohair@286 | 457 | * <p> |
ohair@286 | 458 | * If the base64 text is tightly packed with no indentation nor illegal char |
ohair@286 | 459 | * (like what most web services produce), then the speculation of this method |
ohair@286 | 460 | * will be correct, so we get the performance benefit. |
ohair@286 | 461 | */ |
ohair@286 | 462 | private static int guessLength(String text) { |
ohair@286 | 463 | final int len = text.length(); |
ohair@286 | 464 | |
ohair@286 | 465 | // compute the tail '=' chars |
ohair@286 | 466 | int j = len - 1; |
ohair@286 | 467 | for (; j >= 0; j--) { |
ohair@286 | 468 | byte code = decodeMap[text.charAt(j)]; |
ohair@286 | 469 | if (code == PADDING) { |
ohair@286 | 470 | continue; |
ohair@286 | 471 | } |
ohair@286 | 472 | if (code == -1) // most likely this base64 text is indented. go with the upper bound |
ohair@286 | 473 | { |
ohair@286 | 474 | return text.length() / 4 * 3; |
ohair@286 | 475 | } |
ohair@286 | 476 | break; |
ohair@286 | 477 | } |
ohair@286 | 478 | |
ohair@286 | 479 | j++; // text.charAt(j) is now at some base64 char, so +1 to make it the size |
ohair@286 | 480 | int padSize = len - j; |
ohair@286 | 481 | if (padSize > 2) // something is wrong with base64. be safe and go with the upper bound |
ohair@286 | 482 | { |
ohair@286 | 483 | return text.length() / 4 * 3; |
ohair@286 | 484 | } |
ohair@286 | 485 | |
ohair@286 | 486 | // so far this base64 looks like it's unindented tightly packed base64. |
ohair@286 | 487 | // take a chance and create an array with the expected size |
ohair@286 | 488 | return text.length() / 4 * 3 - padSize; |
ohair@286 | 489 | } |
ohair@286 | 490 | |
ohair@286 | 491 | /** |
ohair@286 | 492 | * @param text |
ohair@286 | 493 | * base64Binary data is likely to be long, and decoding requires |
ohair@286 | 494 | * each character to be accessed twice (once for counting length, another |
ohair@286 | 495 | * for decoding.) |
ohair@286 | 496 | * |
ohair@286 | 497 | * A benchmark showed that taking {@link String} is faster, presumably |
ohair@286 | 498 | * because JIT can inline a lot of string access (with data of 1K chars, it was twice as fast) |
ohair@286 | 499 | */ |
ohair@286 | 500 | public static byte[] _parseBase64Binary(String text) { |
ohair@286 | 501 | final int buflen = guessLength(text); |
ohair@286 | 502 | final byte[] out = new byte[buflen]; |
ohair@286 | 503 | int o = 0; |
ohair@286 | 504 | |
ohair@286 | 505 | final int len = text.length(); |
ohair@286 | 506 | int i; |
ohair@286 | 507 | |
ohair@286 | 508 | final byte[] quadruplet = new byte[4]; |
ohair@286 | 509 | int q = 0; |
ohair@286 | 510 | |
ohair@286 | 511 | // convert each quadruplet to three bytes. |
ohair@286 | 512 | for (i = 0; i < len; i++) { |
ohair@286 | 513 | char ch = text.charAt(i); |
ohair@286 | 514 | byte v = decodeMap[ch]; |
ohair@286 | 515 | |
ohair@286 | 516 | if (v != -1) { |
ohair@286 | 517 | quadruplet[q++] = v; |
ohair@286 | 518 | } |
ohair@286 | 519 | |
ohair@286 | 520 | if (q == 4) { |
ohair@286 | 521 | // quadruplet is now filled. |
ohair@286 | 522 | out[o++] = (byte) ((quadruplet[0] << 2) | (quadruplet[1] >> 4)); |
ohair@286 | 523 | if (quadruplet[2] != PADDING) { |
ohair@286 | 524 | out[o++] = (byte) ((quadruplet[1] << 4) | (quadruplet[2] >> 2)); |
ohair@286 | 525 | } |
ohair@286 | 526 | if (quadruplet[3] != PADDING) { |
ohair@286 | 527 | out[o++] = (byte) ((quadruplet[2] << 6) | (quadruplet[3])); |
ohair@286 | 528 | } |
ohair@286 | 529 | q = 0; |
ohair@286 | 530 | } |
ohair@286 | 531 | } |
ohair@286 | 532 | |
ohair@286 | 533 | if (buflen == o) // speculation worked out to be OK |
ohair@286 | 534 | { |
ohair@286 | 535 | return out; |
ohair@286 | 536 | } |
ohair@286 | 537 | |
ohair@286 | 538 | // we overestimated, so need to create a new buffer |
ohair@286 | 539 | byte[] nb = new byte[o]; |
ohair@286 | 540 | System.arraycopy(out, 0, nb, 0, o); |
ohair@286 | 541 | return nb; |
ohair@286 | 542 | } |
ohair@286 | 543 | private static final char[] encodeMap = initEncodeMap(); |
ohair@286 | 544 | |
ohair@286 | 545 | private static char[] initEncodeMap() { |
ohair@286 | 546 | char[] map = new char[64]; |
ohair@286 | 547 | int i; |
ohair@286 | 548 | for (i = 0; i < 26; i++) { |
ohair@286 | 549 | map[i] = (char) ('A' + i); |
ohair@286 | 550 | } |
ohair@286 | 551 | for (i = 26; i < 52; i++) { |
ohair@286 | 552 | map[i] = (char) ('a' + (i - 26)); |
ohair@286 | 553 | } |
ohair@286 | 554 | for (i = 52; i < 62; i++) { |
ohair@286 | 555 | map[i] = (char) ('0' + (i - 52)); |
ohair@286 | 556 | } |
ohair@286 | 557 | map[62] = '+'; |
ohair@286 | 558 | map[63] = '/'; |
ohair@286 | 559 | |
ohair@286 | 560 | return map; |
ohair@286 | 561 | } |
ohair@286 | 562 | |
ohair@286 | 563 | public static char encode(int i) { |
ohair@286 | 564 | return encodeMap[i & 0x3F]; |
ohair@286 | 565 | } |
ohair@286 | 566 | |
ohair@286 | 567 | public static byte encodeByte(int i) { |
ohair@286 | 568 | return (byte) encodeMap[i & 0x3F]; |
ohair@286 | 569 | } |
ohair@286 | 570 | |
ohair@286 | 571 | public static String _printBase64Binary(byte[] input) { |
ohair@286 | 572 | return _printBase64Binary(input, 0, input.length); |
ohair@286 | 573 | } |
ohair@286 | 574 | |
ohair@286 | 575 | public static String _printBase64Binary(byte[] input, int offset, int len) { |
ohair@286 | 576 | char[] buf = new char[((len + 2) / 3) * 4]; |
ohair@286 | 577 | int ptr = _printBase64Binary(input, offset, len, buf, 0); |
ohair@286 | 578 | assert ptr == buf.length; |
ohair@286 | 579 | return new String(buf); |
ohair@286 | 580 | } |
ohair@286 | 581 | |
ohair@286 | 582 | /** |
ohair@286 | 583 | * Encodes a byte array into a char array by doing base64 encoding. |
ohair@286 | 584 | * |
ohair@286 | 585 | * The caller must supply a big enough buffer. |
ohair@286 | 586 | * |
ohair@286 | 587 | * @return |
ohair@286 | 588 | * the value of {@code ptr+((len+2)/3)*4}, which is the new offset |
ohair@286 | 589 | * in the output buffer where the further bytes should be placed. |
ohair@286 | 590 | */ |
ohair@286 | 591 | public static int _printBase64Binary(byte[] input, int offset, int len, char[] buf, int ptr) { |
ohair@286 | 592 | // encode elements until only 1 or 2 elements are left to encode |
ohair@286 | 593 | int remaining = len; |
ohair@286 | 594 | int i; |
ohair@286 | 595 | for (i = offset;remaining >= 3; remaining -= 3, i += 3) { |
ohair@286 | 596 | buf[ptr++] = encode(input[i] >> 2); |
ohair@286 | 597 | buf[ptr++] = encode( |
ohair@286 | 598 | ((input[i] & 0x3) << 4) |
ohair@286 | 599 | | ((input[i + 1] >> 4) & 0xF)); |
ohair@286 | 600 | buf[ptr++] = encode( |
ohair@286 | 601 | ((input[i + 1] & 0xF) << 2) |
ohair@286 | 602 | | ((input[i + 2] >> 6) & 0x3)); |
ohair@286 | 603 | buf[ptr++] = encode(input[i + 2] & 0x3F); |
ohair@286 | 604 | } |
ohair@286 | 605 | // encode when exactly 1 element (left) to encode |
ohair@286 | 606 | if (remaining == 1) { |
ohair@286 | 607 | buf[ptr++] = encode(input[i] >> 2); |
ohair@286 | 608 | buf[ptr++] = encode(((input[i]) & 0x3) << 4); |
ohair@286 | 609 | buf[ptr++] = '='; |
ohair@286 | 610 | buf[ptr++] = '='; |
ohair@286 | 611 | } |
ohair@286 | 612 | // encode when exactly 2 elements (left) to encode |
ohair@286 | 613 | if (remaining == 2) { |
ohair@286 | 614 | buf[ptr++] = encode(input[i] >> 2); |
ohair@286 | 615 | buf[ptr++] = encode(((input[i] & 0x3) << 4) |
ohair@286 | 616 | | ((input[i + 1] >> 4) & 0xF)); |
ohair@286 | 617 | buf[ptr++] = encode((input[i + 1] & 0xF) << 2); |
ohair@286 | 618 | buf[ptr++] = '='; |
ohair@286 | 619 | } |
ohair@286 | 620 | return ptr; |
ohair@286 | 621 | } |
ohair@286 | 622 | |
ohair@286 | 623 | public static void _printBase64Binary(byte[] input, int offset, int len, XMLStreamWriter output) throws XMLStreamException { |
ohair@286 | 624 | int remaining = len; |
ohair@286 | 625 | int i; |
ohair@286 | 626 | char[] buf = new char[4]; |
ohair@286 | 627 | |
ohair@286 | 628 | for (i = offset; remaining >= 3; remaining -= 3, i += 3) { |
ohair@286 | 629 | buf[0] = encode(input[i] >> 2); |
ohair@286 | 630 | buf[1] = encode( |
ohair@286 | 631 | ((input[i] & 0x3) << 4) |
ohair@286 | 632 | | ((input[i + 1] >> 4) & 0xF)); |
ohair@286 | 633 | buf[2] = encode( |
ohair@286 | 634 | ((input[i + 1] & 0xF) << 2) |
ohair@286 | 635 | | ((input[i + 2] >> 6) & 0x3)); |
ohair@286 | 636 | buf[3] = encode(input[i + 2] & 0x3F); |
ohair@286 | 637 | output.writeCharacters(buf, 0, 4); |
ohair@286 | 638 | } |
ohair@286 | 639 | // encode when exactly 1 element (left) to encode |
ohair@286 | 640 | if (remaining == 1) { |
ohair@286 | 641 | buf[0] = encode(input[i] >> 2); |
ohair@286 | 642 | buf[1] = encode(((input[i]) & 0x3) << 4); |
ohair@286 | 643 | buf[2] = '='; |
ohair@286 | 644 | buf[3] = '='; |
ohair@286 | 645 | output.writeCharacters(buf, 0, 4); |
ohair@286 | 646 | } |
ohair@286 | 647 | // encode when exactly 2 elements (left) to encode |
ohair@286 | 648 | if (remaining == 2) { |
ohair@286 | 649 | buf[0] = encode(input[i] >> 2); |
ohair@286 | 650 | buf[1] = encode(((input[i] & 0x3) << 4) |
ohair@286 | 651 | | ((input[i + 1] >> 4) & 0xF)); |
ohair@286 | 652 | buf[2] = encode((input[i + 1] & 0xF) << 2); |
ohair@286 | 653 | buf[3] = '='; |
ohair@286 | 654 | output.writeCharacters(buf, 0, 4); |
ohair@286 | 655 | } |
ohair@286 | 656 | } |
ohair@286 | 657 | |
ohair@286 | 658 | /** |
ohair@286 | 659 | * Encodes a byte array into another byte array by first doing base64 encoding |
ohair@286 | 660 | * then encoding the result in ASCII. |
ohair@286 | 661 | * |
ohair@286 | 662 | * The caller must supply a big enough buffer. |
ohair@286 | 663 | * |
ohair@286 | 664 | * @return |
ohair@286 | 665 | * the value of {@code ptr+((len+2)/3)*4}, which is the new offset |
ohair@286 | 666 | * in the output buffer where the further bytes should be placed. |
ohair@286 | 667 | */ |
ohair@286 | 668 | public static int _printBase64Binary(byte[] input, int offset, int len, byte[] out, int ptr) { |
ohair@286 | 669 | byte[] buf = out; |
ohair@286 | 670 | int remaining = len; |
ohair@286 | 671 | int i; |
ohair@286 | 672 | for (i=offset; remaining >= 3; remaining -= 3, i += 3 ) { |
ohair@286 | 673 | buf[ptr++] = encodeByte(input[i]>>2); |
ohair@286 | 674 | buf[ptr++] = encodeByte( |
ohair@286 | 675 | ((input[i]&0x3)<<4) | |
ohair@286 | 676 | ((input[i+1]>>4)&0xF)); |
ohair@286 | 677 | buf[ptr++] = encodeByte( |
ohair@286 | 678 | ((input[i+1]&0xF)<<2)| |
ohair@286 | 679 | ((input[i+2]>>6)&0x3)); |
ohair@286 | 680 | buf[ptr++] = encodeByte(input[i+2]&0x3F); |
ohair@286 | 681 | } |
ohair@286 | 682 | // encode when exactly 1 element (left) to encode |
ohair@286 | 683 | if (remaining == 1) { |
ohair@286 | 684 | buf[ptr++] = encodeByte(input[i]>>2); |
ohair@286 | 685 | buf[ptr++] = encodeByte(((input[i])&0x3)<<4); |
ohair@286 | 686 | buf[ptr++] = '='; |
ohair@286 | 687 | buf[ptr++] = '='; |
ohair@286 | 688 | } |
ohair@286 | 689 | // encode when exactly 2 elements (left) to encode |
ohair@286 | 690 | if (remaining == 2) { |
ohair@286 | 691 | buf[ptr++] = encodeByte(input[i]>>2); |
ohair@286 | 692 | buf[ptr++] = encodeByte( |
ohair@286 | 693 | ((input[i]&0x3)<<4) | |
ohair@286 | 694 | ((input[i+1]>>4)&0xF)); |
ohair@286 | 695 | buf[ptr++] = encodeByte((input[i+1]&0xF)<<2); |
ohair@286 | 696 | buf[ptr++] = '='; |
ohair@286 | 697 | } |
ohair@286 | 698 | |
ohair@286 | 699 | return ptr; |
ohair@286 | 700 | } |
ohair@286 | 701 | |
ohair@286 | 702 | private static CharSequence removeOptionalPlus(CharSequence s) { |
ohair@286 | 703 | int len = s.length(); |
ohair@286 | 704 | |
ohair@286 | 705 | if (len <= 1 || s.charAt(0) != '+') { |
ohair@286 | 706 | return s; |
ohair@286 | 707 | } |
ohair@286 | 708 | |
ohair@286 | 709 | s = s.subSequence(1, len); |
ohair@286 | 710 | char ch = s.charAt(0); |
ohair@286 | 711 | if ('0' <= ch && ch <= '9') { |
ohair@286 | 712 | return s; |
ohair@286 | 713 | } |
ohair@286 | 714 | if ('.' == ch) { |
ohair@286 | 715 | return s; |
ohair@286 | 716 | } |
ohair@286 | 717 | |
ohair@286 | 718 | throw new NumberFormatException(); |
ohair@286 | 719 | } |
ohair@286 | 720 | |
ohair@286 | 721 | private static boolean isDigitOrPeriodOrSign(char ch) { |
ohair@286 | 722 | if ('0' <= ch && ch <= '9') { |
ohair@286 | 723 | return true; |
ohair@286 | 724 | } |
ohair@286 | 725 | if (ch == '+' || ch == '-' || ch == '.') { |
ohair@286 | 726 | return true; |
ohair@286 | 727 | } |
ohair@286 | 728 | return false; |
ohair@286 | 729 | } |
ohair@286 | 730 | |
mkos@514 | 731 | private static final Map<ClassLoader, DatatypeFactory> DF_CACHE = Collections.synchronizedMap(new WeakHashMap<ClassLoader, DatatypeFactory>()); |
mkos@514 | 732 | |
mkos@514 | 733 | public static DatatypeFactory getDatatypeFactory() { |
mkos@514 | 734 | ClassLoader tccl = AccessController.doPrivileged(new PrivilegedAction<ClassLoader>() { |
mkos@514 | 735 | public ClassLoader run() { |
mkos@514 | 736 | return Thread.currentThread().getContextClassLoader(); |
mkos@514 | 737 | } |
mkos@514 | 738 | }); |
mkos@514 | 739 | DatatypeFactory df = DF_CACHE.get(tccl); |
mkos@514 | 740 | if (df == null) { |
mkos@514 | 741 | synchronized (DatatypeConverterImpl.class) { |
mkos@514 | 742 | df = DF_CACHE.get(tccl); |
mkos@514 | 743 | if (df == null) { // to prevent multiple initialization |
mkos@514 | 744 | try { |
mkos@514 | 745 | df = DatatypeFactory.newInstance(); |
mkos@514 | 746 | } catch (DatatypeConfigurationException e) { |
mkos@514 | 747 | throw new Error(Messages.FAILED_TO_INITIALE_DATATYPE_FACTORY.format(),e); |
mkos@514 | 748 | } |
mkos@514 | 749 | DF_CACHE.put(tccl, df); |
mkos@514 | 750 | } |
mkos@514 | 751 | } |
ohair@286 | 752 | } |
mkos@514 | 753 | return df; |
ohair@286 | 754 | } |
ohair@286 | 755 | |
ohair@286 | 756 | private static final class CalendarFormatter { |
ohair@286 | 757 | |
ohair@286 | 758 | public static String doFormat(String format, Calendar cal) throws IllegalArgumentException { |
ohair@286 | 759 | int fidx = 0; |
ohair@286 | 760 | int flen = format.length(); |
ohair@286 | 761 | StringBuilder buf = new StringBuilder(); |
ohair@286 | 762 | |
ohair@286 | 763 | while (fidx < flen) { |
ohair@286 | 764 | char fch = format.charAt(fidx++); |
ohair@286 | 765 | |
ohair@286 | 766 | if (fch != '%') { // not a meta character |
ohair@286 | 767 | buf.append(fch); |
ohair@286 | 768 | continue; |
ohair@286 | 769 | } |
ohair@286 | 770 | |
ohair@286 | 771 | // seen meta character. we don't do error check against the format |
ohair@286 | 772 | switch (format.charAt(fidx++)) { |
ohair@286 | 773 | case 'Y': // year |
ohair@286 | 774 | formatYear(cal, buf); |
ohair@286 | 775 | break; |
ohair@286 | 776 | |
ohair@286 | 777 | case 'M': // month |
ohair@286 | 778 | formatMonth(cal, buf); |
ohair@286 | 779 | break; |
ohair@286 | 780 | |
ohair@286 | 781 | case 'D': // days |
ohair@286 | 782 | formatDays(cal, buf); |
ohair@286 | 783 | break; |
ohair@286 | 784 | |
ohair@286 | 785 | case 'h': // hours |
ohair@286 | 786 | formatHours(cal, buf); |
ohair@286 | 787 | break; |
ohair@286 | 788 | |
ohair@286 | 789 | case 'm': // minutes |
ohair@286 | 790 | formatMinutes(cal, buf); |
ohair@286 | 791 | break; |
ohair@286 | 792 | |
ohair@286 | 793 | case 's': // parse seconds. |
ohair@286 | 794 | formatSeconds(cal, buf); |
ohair@286 | 795 | break; |
ohair@286 | 796 | |
ohair@286 | 797 | case 'z': // time zone |
ohair@286 | 798 | formatTimeZone(cal, buf); |
ohair@286 | 799 | break; |
ohair@286 | 800 | |
ohair@286 | 801 | default: |
ohair@286 | 802 | // illegal meta character. impossible. |
ohair@286 | 803 | throw new InternalError(); |
ohair@286 | 804 | } |
ohair@286 | 805 | } |
ohair@286 | 806 | |
ohair@286 | 807 | return buf.toString(); |
ohair@286 | 808 | } |
ohair@286 | 809 | |
ohair@286 | 810 | private static void formatYear(Calendar cal, StringBuilder buf) { |
ohair@286 | 811 | int year = cal.get(Calendar.YEAR); |
ohair@286 | 812 | |
ohair@286 | 813 | String s; |
ohair@286 | 814 | if (year <= 0) // negative value |
ohair@286 | 815 | { |
ohair@286 | 816 | s = Integer.toString(1 - year); |
ohair@286 | 817 | } else // positive value |
ohair@286 | 818 | { |
ohair@286 | 819 | s = Integer.toString(year); |
ohair@286 | 820 | } |
ohair@286 | 821 | |
ohair@286 | 822 | while (s.length() < 4) { |
ohair@286 | 823 | s = '0' + s; |
ohair@286 | 824 | } |
ohair@286 | 825 | if (year <= 0) { |
ohair@286 | 826 | s = '-' + s; |
ohair@286 | 827 | } |
ohair@286 | 828 | |
ohair@286 | 829 | buf.append(s); |
ohair@286 | 830 | } |
ohair@286 | 831 | |
ohair@286 | 832 | private static void formatMonth(Calendar cal, StringBuilder buf) { |
ohair@286 | 833 | formatTwoDigits(cal.get(Calendar.MONTH) + 1, buf); |
ohair@286 | 834 | } |
ohair@286 | 835 | |
ohair@286 | 836 | private static void formatDays(Calendar cal, StringBuilder buf) { |
ohair@286 | 837 | formatTwoDigits(cal.get(Calendar.DAY_OF_MONTH), buf); |
ohair@286 | 838 | } |
ohair@286 | 839 | |
ohair@286 | 840 | private static void formatHours(Calendar cal, StringBuilder buf) { |
ohair@286 | 841 | formatTwoDigits(cal.get(Calendar.HOUR_OF_DAY), buf); |
ohair@286 | 842 | } |
ohair@286 | 843 | |
ohair@286 | 844 | private static void formatMinutes(Calendar cal, StringBuilder buf) { |
ohair@286 | 845 | formatTwoDigits(cal.get(Calendar.MINUTE), buf); |
ohair@286 | 846 | } |
ohair@286 | 847 | |
ohair@286 | 848 | private static void formatSeconds(Calendar cal, StringBuilder buf) { |
ohair@286 | 849 | formatTwoDigits(cal.get(Calendar.SECOND), buf); |
ohair@286 | 850 | if (cal.isSet(Calendar.MILLISECOND)) { // milliseconds |
ohair@286 | 851 | int n = cal.get(Calendar.MILLISECOND); |
ohair@286 | 852 | if (n != 0) { |
ohair@286 | 853 | String ms = Integer.toString(n); |
ohair@286 | 854 | while (ms.length() < 3) { |
ohair@286 | 855 | ms = '0' + ms; // left 0 paddings. |
ohair@286 | 856 | } |
ohair@286 | 857 | buf.append('.'); |
ohair@286 | 858 | buf.append(ms); |
ohair@286 | 859 | } |
ohair@286 | 860 | } |
ohair@286 | 861 | } |
ohair@286 | 862 | |
ohair@286 | 863 | /** formats time zone specifier. */ |
ohair@286 | 864 | private static void formatTimeZone(Calendar cal, StringBuilder buf) { |
ohair@286 | 865 | TimeZone tz = cal.getTimeZone(); |
ohair@286 | 866 | |
ohair@286 | 867 | if (tz == null) { |
ohair@286 | 868 | return; |
ohair@286 | 869 | } |
ohair@286 | 870 | |
ohair@286 | 871 | // otherwise print out normally. |
ohair@286 | 872 | int offset = tz.getOffset(cal.getTime().getTime()); |
ohair@286 | 873 | |
ohair@286 | 874 | if (offset == 0) { |
ohair@286 | 875 | buf.append('Z'); |
ohair@286 | 876 | return; |
ohair@286 | 877 | } |
ohair@286 | 878 | |
ohair@286 | 879 | if (offset >= 0) { |
ohair@286 | 880 | buf.append('+'); |
ohair@286 | 881 | } else { |
ohair@286 | 882 | buf.append('-'); |
ohair@286 | 883 | offset *= -1; |
ohair@286 | 884 | } |
ohair@286 | 885 | |
ohair@286 | 886 | offset /= 60 * 1000; // offset is in milli-seconds |
ohair@286 | 887 | |
ohair@286 | 888 | formatTwoDigits(offset / 60, buf); |
ohair@286 | 889 | buf.append(':'); |
ohair@286 | 890 | formatTwoDigits(offset % 60, buf); |
ohair@286 | 891 | } |
ohair@286 | 892 | |
ohair@286 | 893 | /** formats Integer into two-character-wide string. */ |
ohair@286 | 894 | private static void formatTwoDigits(int n, StringBuilder buf) { |
ohair@286 | 895 | // n is always non-negative. |
ohair@286 | 896 | if (n < 10) { |
ohair@286 | 897 | buf.append('0'); |
ohair@286 | 898 | } |
ohair@286 | 899 | buf.append(n); |
ohair@286 | 900 | } |
ohair@286 | 901 | } |
alanb@368 | 902 | |
alanb@368 | 903 | // DEPRECATED METHODS, KEPT FOR JAXB1 GENERATED CLASSES COMPATIBILITY, WILL BE REMOVED IN FUTURE |
alanb@368 | 904 | |
alanb@368 | 905 | @Deprecated |
alanb@368 | 906 | public String parseString(String lexicalXSDString) { |
alanb@368 | 907 | return lexicalXSDString; |
alanb@368 | 908 | } |
alanb@368 | 909 | |
alanb@368 | 910 | @Deprecated |
alanb@368 | 911 | public BigInteger parseInteger(String lexicalXSDInteger) { |
alanb@368 | 912 | return _parseInteger(lexicalXSDInteger); |
alanb@368 | 913 | } |
alanb@368 | 914 | |
alanb@368 | 915 | @Deprecated |
alanb@368 | 916 | public String printInteger(BigInteger val) { |
alanb@368 | 917 | return _printInteger(val); |
alanb@368 | 918 | } |
alanb@368 | 919 | |
alanb@368 | 920 | @Deprecated |
alanb@368 | 921 | public int parseInt(String s) { |
alanb@368 | 922 | return _parseInt(s); |
alanb@368 | 923 | } |
alanb@368 | 924 | |
alanb@368 | 925 | @Deprecated |
alanb@368 | 926 | public long parseLong(String lexicalXSLong) { |
alanb@368 | 927 | return _parseLong(lexicalXSLong); |
alanb@368 | 928 | } |
alanb@368 | 929 | |
alanb@368 | 930 | @Deprecated |
alanb@368 | 931 | public short parseShort(String lexicalXSDShort) { |
alanb@368 | 932 | return _parseShort(lexicalXSDShort); |
alanb@368 | 933 | } |
alanb@368 | 934 | |
alanb@368 | 935 | @Deprecated |
alanb@368 | 936 | public String printShort(short val) { |
alanb@368 | 937 | return _printShort(val); |
alanb@368 | 938 | } |
alanb@368 | 939 | |
alanb@368 | 940 | @Deprecated |
alanb@368 | 941 | public BigDecimal parseDecimal(String content) { |
alanb@368 | 942 | return _parseDecimal(content); |
alanb@368 | 943 | } |
alanb@368 | 944 | |
alanb@368 | 945 | @Deprecated |
alanb@368 | 946 | public float parseFloat(String lexicalXSDFloat) { |
alanb@368 | 947 | return _parseFloat(lexicalXSDFloat); |
alanb@368 | 948 | } |
alanb@368 | 949 | |
alanb@368 | 950 | @Deprecated |
alanb@368 | 951 | public String printFloat(float v) { |
alanb@368 | 952 | return _printFloat(v); |
alanb@368 | 953 | } |
alanb@368 | 954 | |
alanb@368 | 955 | @Deprecated |
alanb@368 | 956 | public double parseDouble(String lexicalXSDDouble) { |
alanb@368 | 957 | return _parseDouble(lexicalXSDDouble); |
alanb@368 | 958 | } |
alanb@368 | 959 | |
alanb@368 | 960 | @Deprecated |
alanb@368 | 961 | public boolean parseBoolean(String lexicalXSDBoolean) { |
alanb@368 | 962 | Boolean b = _parseBoolean(lexicalXSDBoolean); |
alanb@368 | 963 | return (b == null) ? false : b.booleanValue(); |
alanb@368 | 964 | } |
alanb@368 | 965 | |
alanb@368 | 966 | @Deprecated |
alanb@368 | 967 | public String printBoolean(boolean val) { |
alanb@368 | 968 | return val ? "true" : "false"; |
alanb@368 | 969 | } |
alanb@368 | 970 | |
alanb@368 | 971 | @Deprecated |
alanb@368 | 972 | public byte parseByte(String lexicalXSDByte) { |
alanb@368 | 973 | return _parseByte(lexicalXSDByte); |
alanb@368 | 974 | } |
alanb@368 | 975 | |
alanb@368 | 976 | @Deprecated |
alanb@368 | 977 | public String printByte(byte val) { |
alanb@368 | 978 | return _printByte(val); |
alanb@368 | 979 | } |
alanb@368 | 980 | |
alanb@368 | 981 | @Deprecated |
alanb@368 | 982 | public QName parseQName(String lexicalXSDQName, NamespaceContext nsc) { |
alanb@368 | 983 | return _parseQName(lexicalXSDQName, nsc); |
alanb@368 | 984 | } |
alanb@368 | 985 | |
alanb@368 | 986 | @Deprecated |
alanb@368 | 987 | public Calendar parseDateTime(String lexicalXSDDateTime) { |
alanb@368 | 988 | return _parseDateTime(lexicalXSDDateTime); |
alanb@368 | 989 | } |
alanb@368 | 990 | |
alanb@368 | 991 | @Deprecated |
alanb@368 | 992 | public String printDateTime(Calendar val) { |
alanb@368 | 993 | return _printDateTime(val); |
alanb@368 | 994 | } |
alanb@368 | 995 | |
alanb@368 | 996 | @Deprecated |
alanb@368 | 997 | public byte[] parseBase64Binary(String lexicalXSDBase64Binary) { |
alanb@368 | 998 | return _parseBase64Binary(lexicalXSDBase64Binary); |
alanb@368 | 999 | } |
alanb@368 | 1000 | |
alanb@368 | 1001 | @Deprecated |
alanb@368 | 1002 | public byte[] parseHexBinary(String s) { |
alanb@368 | 1003 | final int len = s.length(); |
alanb@368 | 1004 | |
alanb@368 | 1005 | // "111" is not a valid hex encoding. |
alanb@368 | 1006 | if (len % 2 != 0) { |
alanb@368 | 1007 | throw new IllegalArgumentException("hexBinary needs to be even-length: " + s); |
alanb@368 | 1008 | } |
alanb@368 | 1009 | |
alanb@368 | 1010 | byte[] out = new byte[len / 2]; |
alanb@368 | 1011 | |
alanb@368 | 1012 | for (int i = 0; i < len; i += 2) { |
alanb@368 | 1013 | int h = hexToBin(s.charAt(i)); |
alanb@368 | 1014 | int l = hexToBin(s.charAt(i + 1)); |
alanb@368 | 1015 | if (h == -1 || l == -1) { |
alanb@368 | 1016 | throw new IllegalArgumentException("contains illegal character for hexBinary: " + s); |
alanb@368 | 1017 | } |
alanb@368 | 1018 | |
alanb@368 | 1019 | out[i / 2] = (byte) (h * 16 + l); |
alanb@368 | 1020 | } |
alanb@368 | 1021 | |
alanb@368 | 1022 | return out; |
alanb@368 | 1023 | } |
alanb@368 | 1024 | |
alanb@368 | 1025 | @Deprecated |
alanb@368 | 1026 | private static int hexToBin(char ch) { |
alanb@368 | 1027 | if ('0' <= ch && ch <= '9') { |
alanb@368 | 1028 | return ch - '0'; |
alanb@368 | 1029 | } |
alanb@368 | 1030 | if ('A' <= ch && ch <= 'F') { |
alanb@368 | 1031 | return ch - 'A' + 10; |
alanb@368 | 1032 | } |
alanb@368 | 1033 | if ('a' <= ch && ch <= 'f') { |
alanb@368 | 1034 | return ch - 'a' + 10; |
alanb@368 | 1035 | } |
alanb@368 | 1036 | return -1; |
alanb@368 | 1037 | } |
alanb@368 | 1038 | |
alanb@368 | 1039 | @Deprecated |
alanb@368 | 1040 | private static final char[] hexCode = "0123456789ABCDEF".toCharArray(); |
alanb@368 | 1041 | |
alanb@368 | 1042 | @Deprecated |
alanb@368 | 1043 | public String printHexBinary(byte[] data) { |
alanb@368 | 1044 | StringBuilder r = new StringBuilder(data.length * 2); |
alanb@368 | 1045 | for (byte b : data) { |
alanb@368 | 1046 | r.append(hexCode[(b >> 4) & 0xF]); |
alanb@368 | 1047 | r.append(hexCode[(b & 0xF)]); |
alanb@368 | 1048 | } |
alanb@368 | 1049 | return r.toString(); |
alanb@368 | 1050 | } |
alanb@368 | 1051 | |
alanb@368 | 1052 | @Deprecated |
alanb@368 | 1053 | public long parseUnsignedInt(String lexicalXSDUnsignedInt) { |
alanb@368 | 1054 | return _parseLong(lexicalXSDUnsignedInt); |
alanb@368 | 1055 | } |
alanb@368 | 1056 | |
alanb@368 | 1057 | @Deprecated |
alanb@368 | 1058 | public String printUnsignedInt(long val) { |
alanb@368 | 1059 | return _printLong(val); |
alanb@368 | 1060 | } |
alanb@368 | 1061 | |
alanb@368 | 1062 | @Deprecated |
alanb@368 | 1063 | public int parseUnsignedShort(String lexicalXSDUnsignedShort) { |
alanb@368 | 1064 | return _parseInt(lexicalXSDUnsignedShort); |
alanb@368 | 1065 | } |
alanb@368 | 1066 | |
alanb@368 | 1067 | @Deprecated |
alanb@368 | 1068 | public Calendar parseTime(String lexicalXSDTime) { |
mkos@514 | 1069 | return getDatatypeFactory().newXMLGregorianCalendar(lexicalXSDTime).toGregorianCalendar(); |
alanb@368 | 1070 | } |
alanb@368 | 1071 | |
alanb@368 | 1072 | @Deprecated |
alanb@368 | 1073 | public String printTime(Calendar val) { |
alanb@368 | 1074 | return CalendarFormatter.doFormat("%h:%m:%s%z", val); |
alanb@368 | 1075 | } |
alanb@368 | 1076 | |
alanb@368 | 1077 | @Deprecated |
alanb@368 | 1078 | public Calendar parseDate(String lexicalXSDDate) { |
mkos@514 | 1079 | return getDatatypeFactory().newXMLGregorianCalendar(lexicalXSDDate).toGregorianCalendar(); |
alanb@368 | 1080 | } |
alanb@368 | 1081 | |
alanb@368 | 1082 | @Deprecated |
alanb@368 | 1083 | public String printDate(Calendar val) { |
alanb@368 | 1084 | return _printDate(val); |
alanb@368 | 1085 | } |
alanb@368 | 1086 | |
alanb@368 | 1087 | @Deprecated |
alanb@368 | 1088 | public String parseAnySimpleType(String lexicalXSDAnySimpleType) { |
alanb@368 | 1089 | return lexicalXSDAnySimpleType; |
alanb@368 | 1090 | } |
alanb@368 | 1091 | |
alanb@368 | 1092 | @Deprecated |
alanb@368 | 1093 | public String printString(String val) { |
alanb@368 | 1094 | return val; |
alanb@368 | 1095 | } |
alanb@368 | 1096 | |
alanb@368 | 1097 | @Deprecated |
alanb@368 | 1098 | public String printInt(int val) { |
alanb@368 | 1099 | return _printInt(val); |
alanb@368 | 1100 | } |
alanb@368 | 1101 | |
alanb@368 | 1102 | @Deprecated |
alanb@368 | 1103 | public String printLong(long val) { |
alanb@368 | 1104 | return _printLong(val); |
alanb@368 | 1105 | } |
alanb@368 | 1106 | |
alanb@368 | 1107 | @Deprecated |
alanb@368 | 1108 | public String printDecimal(BigDecimal val) { |
alanb@368 | 1109 | return _printDecimal(val); |
alanb@368 | 1110 | } |
alanb@368 | 1111 | |
alanb@368 | 1112 | @Deprecated |
alanb@368 | 1113 | public String printDouble(double v) { |
alanb@368 | 1114 | return _printDouble(v); |
alanb@368 | 1115 | } |
alanb@368 | 1116 | |
alanb@368 | 1117 | @Deprecated |
alanb@368 | 1118 | public String printQName(QName val, NamespaceContext nsc) { |
alanb@368 | 1119 | return _printQName(val, nsc); |
alanb@368 | 1120 | } |
alanb@368 | 1121 | |
alanb@368 | 1122 | @Deprecated |
alanb@368 | 1123 | public String printBase64Binary(byte[] val) { |
alanb@368 | 1124 | return _printBase64Binary(val); |
alanb@368 | 1125 | } |
alanb@368 | 1126 | |
alanb@368 | 1127 | @Deprecated |
alanb@368 | 1128 | public String printUnsignedShort(int val) { |
alanb@368 | 1129 | return String.valueOf(val); |
alanb@368 | 1130 | } |
alanb@368 | 1131 | |
alanb@368 | 1132 | @Deprecated |
alanb@368 | 1133 | public String printAnySimpleType(String val) { |
alanb@368 | 1134 | return val; |
alanb@368 | 1135 | } |
alanb@368 | 1136 | |
ohair@286 | 1137 | } |