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.nio.CharBuffer;
042: import java.util.regex.Matcher;
043: import java.util.regex.Pattern;
044: import org.jvnet.fastinfoset.EncodingAlgorithm;
045: import org.jvnet.fastinfoset.EncodingAlgorithmException;
046:
047: public abstract class BuiltInEncodingAlgorithm implements
048: EncodingAlgorithm {
049: protected final static Pattern SPACE_PATTERN = Pattern
050: .compile("\\s");
051:
052: public abstract int getPrimtiveLengthFromOctetLength(int octetLength)
053: throws EncodingAlgorithmException;
054:
055: public abstract int getOctetLengthFromPrimitiveLength(
056: int primitiveLength);
057:
058: public abstract void encodeToBytes(Object array, int astart,
059: int alength, byte[] b, int start);
060:
061: public interface WordListener {
062: public void word(int start, int end);
063: }
064:
065: public void matchWhiteSpaceDelimnatedWords(CharBuffer cb,
066: WordListener wl) {
067: Matcher m = SPACE_PATTERN.matcher(cb);
068: int i = 0;
069: int s = 0;
070: while (m.find()) {
071: s = m.start();
072: if (s != i) {
073: wl.word(i, s);
074: }
075: i = m.end();
076: }
077: if (i != cb.length())
078: wl.word(i, cb.length());
079: }
080:
081: public StringBuffer removeWhitespace(char[] ch, int start,
082: int length) {
083: StringBuffer buf = new StringBuffer();
084: int firstNonWS = 0;
085: int idx = 0;
086: for (; idx < length; ++idx) {
087: if (Character.isWhitespace(ch[idx + start])) {
088: if (firstNonWS < idx) {
089: buf
090: .append(ch, firstNonWS + start, idx
091: - firstNonWS);
092: }
093: firstNonWS = idx + 1;
094: }
095: }
096: if (firstNonWS < idx) {
097: buf.append(ch, firstNonWS + start, idx - firstNonWS);
098: }
099: return buf;
100: }
101:
102: }
|