1 38 39 40 package com.sun.xml.fastinfoset.algorithm; 41 42 import java.nio.CharBuffer ; 43 import java.util.regex.Matcher ; 44 import java.util.regex.Pattern ; 45 import org.jvnet.fastinfoset.EncodingAlgorithm; 46 import org.jvnet.fastinfoset.EncodingAlgorithmException; 47 48 public abstract class BuiltInEncodingAlgorithm implements EncodingAlgorithm { 49 protected final static Pattern SPACE_PATTERN = Pattern.compile("\\s"); 50 51 public abstract int getPrimtiveLengthFromOctetLength(int octetLength) throws EncodingAlgorithmException; 52 53 public abstract int getOctetLengthFromPrimitiveLength(int primitiveLength); 54 55 public abstract void encodeToBytes(Object array, int astart, int alength, byte[] b, int start); 56 57 public interface WordListener { 58 public void word(int start, int end); 59 } 60 61 public void matchWhiteSpaceDelimnatedWords(CharBuffer cb, WordListener wl) { 62 Matcher m = SPACE_PATTERN.matcher(cb); 63 int i = 0; 64 while(m.find()) { 65 int s = m.start(); 66 if (s != i) { 67 wl.word(i, s); 68 } 69 i = m.end(); 70 } 71 } 72 73 public StringBuffer removeWhitespace(char[] ch, int start, int length) { 74 StringBuffer buf = new StringBuffer (); 75 int firstNonWS = 0; 76 int idx = 0; 77 for (; idx < length; ++idx) { 78 if (Character.isWhitespace(ch[idx])) { 79 if (firstNonWS < idx) { 80 buf.append(ch, firstNonWS, idx - firstNonWS); 81 } 82 firstNonWS = idx + 1; 83 } 84 } 85 if (firstNonWS < idx) { 86 buf.append(ch, firstNonWS, idx - firstNonWS); 87 } 88 return buf; 89 } 90 91 } 92 | Popular Tags |