c28749e97052f09388969427adf7df641cdcdc22kais/*
c28749e97052f09388969427adf7df641cdcdc22kais * Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
c28749e97052f09388969427adf7df641cdcdc22kais * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
c28749e97052f09388969427adf7df641cdcdc22kais *
968d6dde54d2efa62910a3cf36438325d0b69031krishna * This code is free software; you can redistribute it and/or modify it
968d6dde54d2efa62910a3cf36438325d0b69031krishna * under the terms of the GNU General Public License version 2 only, as
c28749e97052f09388969427adf7df641cdcdc22kais * published by the Free Software Foundation. Oracle designates this
c28749e97052f09388969427adf7df641cdcdc22kais * particular file as subject to the "Classpath" exception as provided
c28749e97052f09388969427adf7df641cdcdc22kais * by Oracle in the LICENSE file that accompanied this code.
c28749e97052f09388969427adf7df641cdcdc22kais *
c28749e97052f09388969427adf7df641cdcdc22kais * This code is distributed in the hope that it will be useful, but WITHOUT
c28749e97052f09388969427adf7df641cdcdc22kais * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
c28749e97052f09388969427adf7df641cdcdc22kais * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
c28749e97052f09388969427adf7df641cdcdc22kais * version 2 for more details (a copy is included in the LICENSE file that
c28749e97052f09388969427adf7df641cdcdc22kais * accompanied this code).
c28749e97052f09388969427adf7df641cdcdc22kais *
c28749e97052f09388969427adf7df641cdcdc22kais * You should have received a copy of the GNU General Public License version
c28749e97052f09388969427adf7df641cdcdc22kais * 2 along with this work; if not, write to the Free Software Foundation,
c28749e97052f09388969427adf7df641cdcdc22kais * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
c28749e97052f09388969427adf7df641cdcdc22kais *
c28749e97052f09388969427adf7df641cdcdc22kais * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
51144063f3afc862c6cb3f54fd4341724f765075Krishna Yenduri * or visit www.oracle.com if you need additional information or have any
c28749e97052f09388969427adf7df641cdcdc22kais * questions.
c28749e97052f09388969427adf7df641cdcdc22kais *
c28749e97052f09388969427adf7df641cdcdc22kais * THIS FILE WAS MODIFIED BY SUN MICROSYSTEMS, INC.
c28749e97052f09388969427adf7df641cdcdc22kais */
c28749e97052f09388969427adf7df641cdcdc22kais
c28749e97052f09388969427adf7df641cdcdc22kaispackage com.sun.xml.internal.fastinfoset.algorithm;
c28749e97052f09388969427adf7df641cdcdc22kais
c28749e97052f09388969427adf7df641cdcdc22kaisimport java.nio.CharBuffer;
c28749e97052f09388969427adf7df641cdcdc22kaisimport java.util.regex.Matcher;
c28749e97052f09388969427adf7df641cdcdc22kaisimport java.util.regex.Pattern;
c28749e97052f09388969427adf7df641cdcdc22kaisimport com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithm;
c28749e97052f09388969427adf7df641cdcdc22kaisimport com.sun.xml.internal.org.jvnet.fastinfoset.EncodingAlgorithmException;
c28749e97052f09388969427adf7df641cdcdc22kais
c28749e97052f09388969427adf7df641cdcdc22kaispublic abstract class BuiltInEncodingAlgorithm implements EncodingAlgorithm {
c28749e97052f09388969427adf7df641cdcdc22kais protected final static Pattern SPACE_PATTERN = Pattern.compile("\\s");
c28749e97052f09388969427adf7df641cdcdc22kais
c28749e97052f09388969427adf7df641cdcdc22kais public abstract int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException;
c28749e97052f09388969427adf7df641cdcdc22kais
c28749e97052f09388969427adf7df641cdcdc22kais public abstract int getOctetLengthFromPrimitiveLength(int primitiveLength);
c28749e97052f09388969427adf7df641cdcdc22kais
c28749e97052f09388969427adf7df641cdcdc22kais public abstract void encodeToBytes(Object array, int astart, int alength, byte[] b, int start);
c28749e97052f09388969427adf7df641cdcdc22kais
c28749e97052f09388969427adf7df641cdcdc22kais public interface WordListener {
c28749e97052f09388969427adf7df641cdcdc22kais public void word(int start, int end);
c28749e97052f09388969427adf7df641cdcdc22kais }
c28749e97052f09388969427adf7df641cdcdc22kais
c28749e97052f09388969427adf7df641cdcdc22kais public void matchWhiteSpaceDelimnatedWords(CharBuffer cb, WordListener wl) {
c28749e97052f09388969427adf7df641cdcdc22kais Matcher m = SPACE_PATTERN.matcher(cb);
c28749e97052f09388969427adf7df641cdcdc22kais int i = 0;
c28749e97052f09388969427adf7df641cdcdc22kais int s = 0;
c28749e97052f09388969427adf7df641cdcdc22kais while(m.find()) {
c28749e97052f09388969427adf7df641cdcdc22kais s = m.start();
c28749e97052f09388969427adf7df641cdcdc22kais if (s != i) {
c28749e97052f09388969427adf7df641cdcdc22kais wl.word(i, s);
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna }
c28749e97052f09388969427adf7df641cdcdc22kais i = m.end();
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna }
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna if (i != cb.length())
c28749e97052f09388969427adf7df641cdcdc22kais wl.word(i, cb.length());
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna }
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna
c28749e97052f09388969427adf7df641cdcdc22kais public StringBuffer removeWhitespace(char[] ch, int start, int length) {
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna StringBuffer buf = new StringBuffer();
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna int firstNonWS = 0;
c28749e97052f09388969427adf7df641cdcdc22kais int idx = 0;
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna for (; idx < length; ++idx) {
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna if (Character.isWhitespace(ch[idx + start])) {
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna if (firstNonWS < idx) {
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna buf.append(ch, firstNonWS + start, idx - firstNonWS);
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna }
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna firstNonWS = idx + 1;
2bd70d4be73561631df9cb3d9eb4c65fa94fa665krishna }
c28749e97052f09388969427adf7df641cdcdc22kais }
c28749e97052f09388969427adf7df641cdcdc22kais if (firstNonWS < idx) {
c28749e97052f09388969427adf7df641cdcdc22kais buf.append(ch, firstNonWS + start, idx - firstNonWS);
c28749e97052f09388969427adf7df641cdcdc22kais }
c28749e97052f09388969427adf7df641cdcdc22kais return buf;
c28749e97052f09388969427adf7df641cdcdc22kais }
c28749e97052f09388969427adf7df641cdcdc22kais
c28749e97052f09388969427adf7df641cdcdc22kais}
c28749e97052f09388969427adf7df641cdcdc22kais