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