aoqi@0: /* aoqi@0: * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved. aoqi@0: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. aoqi@0: * aoqi@0: * This code is free software; you can redistribute it and/or modify it aoqi@0: * under the terms of the GNU General Public License version 2 only, as aoqi@0: * published by the Free Software Foundation. Oracle designates this aoqi@0: * particular file as subject to the "Classpath" exception as provided aoqi@0: * by Oracle in the LICENSE file that accompanied this code. aoqi@0: * aoqi@0: * This code is distributed in the hope that it will be useful, but WITHOUT aoqi@0: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or aoqi@0: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License aoqi@0: * version 2 for more details (a copy is included in the LICENSE file that aoqi@0: * accompanied this code). aoqi@0: * aoqi@0: * You should have received a copy of the GNU General Public License version aoqi@0: * 2 along with this work; if not, write to the Free Software Foundation, aoqi@0: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. aoqi@0: * aoqi@0: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA aoqi@0: * or visit www.oracle.com if you need additional information or have any aoqi@0: * questions. aoqi@0: * aoqi@0: * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC. aoqi@0: */ aoqi@0: aoqi@0: package com.sun.xml.internal.fastinfoset.algorithm; aoqi@0: aoqi@0: import java.util.ArrayList; aoqi@0: import java.util.List; aoqi@0: import java.nio.CharBuffer; aoqi@0: import com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException; aoqi@0: import com.sun.xml.internal.fastinfoset.CommonResourceBundle; aoqi@0: aoqi@0: public class UUIDEncodingAlgorithm extends LongEncodingAlgorithm { aoqi@0: aoqi@0: public final int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException { aoqi@0: if (octetLength % (LONG_SIZE * 2) != 0) { aoqi@0: throw new EncodingAlgorithmException(CommonResourceBundle.getInstance(). aoqi@0: getString("message.lengthNotMultipleOfUUID",new Object[]{Integer.valueOf(LONG_SIZE * 2)})); aoqi@0: } aoqi@0: aoqi@0: return octetLength / LONG_SIZE; aoqi@0: } aoqi@0: aoqi@0: public final Object convertFromCharacters(char[] ch, int start, int length) { aoqi@0: final CharBuffer cb = CharBuffer.wrap(ch, start, length); aoqi@0: final List longList = new ArrayList(); aoqi@0: aoqi@0: matchWhiteSpaceDelimnatedWords(cb, aoqi@0: new WordListener() { aoqi@0: public void word(int start, int end) { aoqi@0: String uuidValue = cb.subSequence(start, end).toString(); aoqi@0: fromUUIDString(uuidValue); aoqi@0: longList.add(Long.valueOf(_msb)); aoqi@0: longList.add(Long.valueOf(_lsb)); aoqi@0: } aoqi@0: } aoqi@0: ); aoqi@0: aoqi@0: return generateArrayFromList(longList); aoqi@0: } aoqi@0: aoqi@0: public final void convertToCharacters(Object data, StringBuffer s) { aoqi@0: if (!(data instanceof long[])) { aoqi@0: throw new IllegalArgumentException(CommonResourceBundle.getInstance().getString("message.dataNotLongArray")); aoqi@0: } aoqi@0: aoqi@0: final long[] ldata = (long[])data; aoqi@0: aoqi@0: final int end = ldata.length - 2; aoqi@0: for (int i = 0; i <= end; i += 2) { aoqi@0: s.append(toUUIDString(ldata[i], ldata[i + 1])); aoqi@0: if (i != end) { aoqi@0: s.append(' '); aoqi@0: } aoqi@0: } aoqi@0: } aoqi@0: aoqi@0: aoqi@0: private long _msb; aoqi@0: private long _lsb; aoqi@0: aoqi@0: final void fromUUIDString(String name) { aoqi@0: String[] components = name.split("-"); aoqi@0: if (components.length != 5) aoqi@0: throw new IllegalArgumentException(CommonResourceBundle.getInstance(). aoqi@0: getString("message.invalidUUID", new Object[]{name})); aoqi@0: aoqi@0: for (int i=0; i<5; i++) aoqi@0: components[i] = "0x"+components[i]; aoqi@0: aoqi@0: _msb = Long.parseLong(components[0], 16); aoqi@0: _msb <<= 16; aoqi@0: _msb |= Long.parseLong(components[1], 16); aoqi@0: _msb <<= 16; aoqi@0: _msb |= Long.parseLong(components[2], 16); aoqi@0: aoqi@0: _lsb = Long.parseLong(components[3], 16); aoqi@0: _lsb <<= 48; aoqi@0: _lsb |= Long.parseLong(components[4], 16); aoqi@0: } aoqi@0: aoqi@0: final String toUUIDString(long msb, long lsb) { aoqi@0: return (digits(msb >> 32, 8) + "-" + aoqi@0: digits(msb >> 16, 4) + "-" + aoqi@0: digits(msb, 4) + "-" + aoqi@0: digits(lsb >> 48, 4) + "-" + aoqi@0: digits(lsb, 12)); aoqi@0: } aoqi@0: aoqi@0: final String digits(long val, int digits) { aoqi@0: long hi = 1L << (digits * 4); aoqi@0: return Long.toHexString(hi | (val & (hi - 1))).substring(1); aoqi@0: } aoqi@0: aoqi@0: }