001: /*
002: * Fast Infoset ver. 0.1 software ("Software")
003: *
004: * Copyright, 2004-2005 Sun Microsystems, Inc. All Rights Reserved.
005: *
006: * Software is licensed under the Apache License, Version 2.0 (the "License");
007: * you may not use this file except in compliance with the License. You may
008: * obtain a copy of the License at:
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing, software
013: * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
014: * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
015: * License for the specific language governing permissions and limitations.
016: *
017: * Sun supports and benefits from the global community of open source
018: * developers, and thanks the community for its important contributions and
019: * open standards-based technology, which Sun has adopted into many of its
020: * products.
021: *
022: * Please note that portions of Software may be provided with notices and
023: * open source licenses from such communities and third parties that govern the
024: * use of those portions, and any licenses granted hereunder do not alter any
025: * rights and obligations you may have under such open source licenses,
026: * however, the disclaimer of warranty and limitation of liability provisions
027: * in this License will apply to all Software in this distribution.
028: *
029: * You acknowledge that the Software is not designed, licensed or intended
030: * for use in the design, construction, operation or maintenance of any nuclear
031: * facility.
032: *
033: * Apache License
034: * Version 2.0, January 2004
035: * http://www.apache.org/licenses/
036: *
037: */
038:
039: package com.sun.xml.fastinfoset.algorithm;
040:
041: import java.util.ArrayList;
042: import java.util.List;
043: import java.nio.CharBuffer;
044: import org.jvnet.fastinfoset.EncodingAlgorithmException;
045: import com.sun.xml.fastinfoset.CommonResourceBundle;
046:
047: public class UUIDEncodingAlgorithm extends LongEncodingAlgorithm {
048:
049: public final int getPrimtiveLengthFromOctetLength(int octetLength)
050: throws EncodingAlgorithmException {
051: if (octetLength % (LONG_SIZE * 2) != 0) {
052: throw new EncodingAlgorithmException(CommonResourceBundle
053: .getInstance().getString(
054: "message.lengthNotMultipleOfUUID",
055: new Object[] { Integer
056: .valueOf(LONG_SIZE * 2) }));
057: }
058:
059: return octetLength / LONG_SIZE;
060: }
061:
062: public final Object convertFromCharacters(char[] ch, int start,
063: int length) {
064: final CharBuffer cb = CharBuffer.wrap(ch, start, length);
065: final List longList = new ArrayList();
066:
067: matchWhiteSpaceDelimnatedWords(cb, new WordListener() {
068: public void word(int start, int end) {
069: String uuidValue = cb.subSequence(start, end)
070: .toString();
071: fromUUIDString(uuidValue);
072: longList.add(Long.valueOf(_msb));
073: longList.add(Long.valueOf(_lsb));
074: }
075: });
076:
077: return generateArrayFromList(longList);
078: }
079:
080: public final void convertToCharacters(Object data, StringBuffer s) {
081: if (!(data instanceof long[])) {
082: throw new IllegalArgumentException(CommonResourceBundle
083: .getInstance()
084: .getString("message.dataNotLongArray"));
085: }
086:
087: final long[] ldata = (long[]) data;
088:
089: final int end = ldata.length - 1;
090: for (int i = 0; i <= end; i += 2) {
091: s.append(toUUIDString(ldata[i], ldata[i + 1]));
092: if (i != end) {
093: s.append(' ');
094: }
095: }
096: }
097:
098: private long _msb;
099: private long _lsb;
100:
101: final void fromUUIDString(String name) {
102: String[] components = name.split("-");
103: if (components.length != 5)
104: throw new IllegalArgumentException(CommonResourceBundle
105: .getInstance().getString("message.invalidUUID",
106: new Object[] { name }));
107:
108: for (int i = 0; i < 5; i++)
109: components[i] = "0x" + components[i];
110:
111: _msb = Long.parseLong(components[0], 16);
112: _msb <<= 16;
113: _msb |= Long.parseLong(components[1], 16);
114: _msb <<= 16;
115: _msb |= Long.parseLong(components[2], 16);
116:
117: _lsb = Long.parseLong(components[3], 16);
118: _lsb <<= 48;
119: _lsb |= Long.parseLong(components[4], 16);
120: }
121:
122: final String toUUIDString(long msb, long lsb) {
123: return (digits(msb >> 32, 8) + "-" + digits(msb >> 16, 4) + "-"
124: + digits(msb, 4) + "-" + digits(lsb >> 48, 4) + "-" + digits(
125: lsb, 12));
126: }
127:
128: final String digits(long val, int digits) {
129: long hi = 1L << (digits * 4);
130: return Long.toHexString(hi | (val & (hi - 1))).substring(1);
131: }
132:
133: }
|