1/* 2 * Copyright (c) 2004, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. Oracle designates this 8 * particular file as subject to the "Classpath" exception as provided 9 * by Oracle in the LICENSE file that accompanied this code. 10 * 11 * This code is distributed in the hope that it will be useful, but WITHOUT 12 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 13 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 14 * version 2 for more details (a copy is included in the LICENSE file that 15 * accompanied this code). 16 * 17 * You should have received a copy of the GNU General Public License version 18 * 2 along with this work; if not, write to the Free Software Foundation, 19 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 20 * 21 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 22 * or visit www.oracle.com if you need additional information or have any 23 * questions. 24 * 25 * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. 26 */ 27 28package com.sun.xml.internal.fastinfoset.algorithm; 29 30import java.util.ArrayList; 31import java.util.List; 32import java.nio.CharBuffer; 33import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException; 34import com.sun.xml.internal.fastinfoset.CommonResourceBundle; 35 36public class UUIDEncodingAlgorithm extends LongEncodingAlgorithm { 37 38 public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException { 39 if (octetLength % (LONG_SIZE * 2) != 0) { 40 throw new EncodingAlgorithmException(CommonResourceBundle.getInstance(). 41 getString("message.lengthNotMultipleOfUUID",new Object[]{Integer.valueOf(LONG_SIZE * 2)})); 42 } 43 44 return octetLength / LONG_SIZE; 45 } 46 47 public final Object convertFromCharacters(char[] ch, int start, int length) { 48 final CharBuffer cb = CharBuffer.wrap(ch, start, length); 49 final List longList = new ArrayList(); 50 51 matchWhiteSpaceDelimnatedWords(cb, 52 new WordListener() { 53 public void word(int start, int end) { 54 String uuidValue = cb.subSequence(start, end).toString(); 55 fromUUIDString(uuidValue); 56 longList.add(Long.valueOf(_msb)); 57 longList.add(Long.valueOf(_lsb)); 58 } 59 } 60 ); 61 62 return generateArrayFromList(longList); 63 } 64 65 public final void convertToCharacters(Object data, StringBuffer s) { 66 if (!(data instanceof long[])) { 67 throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotLongArray")); 68 } 69 70 final long[] ldata = (long[])data; 71 72 final int end = ldata.length - 2; 73 for (int i = 0; i <= end; i += 2) { 74 s.append(toUUIDString(ldata[i], ldata[i + 1])); 75 if (i != end) { 76 s.append(' '); 77 } 78 } 79 } 80 81 82 private long _msb; 83 private long _lsb; 84 85 final void fromUUIDString(String name) { 86 String[] components = name.split("-"); 87 if (components.length != 5) 88 throw new IllegalArgumentException(CommonResourceBundle.getInstance(). 89 getString("message.invalidUUID", new Object[]{name})); 90 91 for (int i=0; i<5; i++) 92 components[i] = "0x"+components[i]; 93 94 _msb = Long.parseLong(components[0], 16); 95 _msb <<= 16; 96 _msb |= Long.parseLong(components[1], 16); 97 _msb <<= 16; 98 _msb |= Long.parseLong(components[2], 16); 99 100 _lsb = Long.parseLong(components[3], 16); 101 _lsb <<= 48; 102 _lsb |= Long.parseLong(components[4], 16); 103 } 104 105 final String toUUIDString(long msb, long lsb) { 106 return (digits(msb >> 32, 8) + "-" + 107 digits(msb >> 16, 4) + "-" + 108 digits(msb, 4) + "-" + 109 digits(lsb >> 48, 4) + "-" + 110 digits(lsb, 12)); 111 } 112 113 final String digits(long val, int digits) { 114 long hi = 1L << (digits * 4); 115 return Long.toHexString(hi | (val & (hi - 1))).substring(1); 116 } 117 118} 119