/src/solidstack/query/QueryEncodingWriter.java
Java | 95 lines | 50 code | 12 blank | 33 comment | 5 complexity | 9692850bd88408a5274d1cc4402716a9 MD5 | raw file
Possible License(s): Apache-2.0
1/*-- 2 * Copyright 2006 René M. de Bloois 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17package solidstack.query; 18 19import java.io.IOException; 20import java.util.ArrayList; 21import java.util.BitSet; 22import java.util.List; 23 24import solidstack.template.EncodingWriter; 25 26 27/** 28 * A writer that keeps values separate from the string segments. 29 * 30 * @author René M. de Bloois 31 */ 32public class QueryEncodingWriter implements EncodingWriter 33{ 34 private List< Object > values = new ArrayList< Object >(); 35 private BitSet isValue = new BitSet(); 36 37 //@Override 38 public void write( String s ) 39 { 40 if( s != null && s.length() > 0 ) 41 this.values.add( s ); 42 } 43 44 //@Override 45 public void writeEncoded( Object o ) throws IOException 46 { 47 this.isValue.set( this.values.size() ); 48 this.values.add( o ); 49 } 50 51 public boolean stringsOnly() 52 { 53 return false; 54 } 55 56 /** 57 * Returns the string segments and the values. 58 * 59 * @return An array of string segments (String) and the values (unknown Object). 60 */ 61 public List< Object > getValues() 62 { 63 return this.values; 64 } 65 66 /** 67 * Returns a bitset that indicates which indexes in the {@link #getValues()} list is a value. 68 * 69 * @return A bitset that indicates which indexes in the {@link #getValues()} list is a value. 70 */ 71 public BitSet getIsValue() 72 { 73 return this.isValue; 74 } 75 76 @Override 77 public String toString() 78 { 79 StringBuilder result = new StringBuilder(); 80 int len = this.values.size(); 81 for( int i = 0; i < len; i++ ) 82 { 83 if( this.isValue.get( i ) ) 84 result.append( this.values.get( i ).toString() ); 85 else 86 result.append( (String)this.values.get( i ) ); 87 } 88 return result.toString(); 89 } 90 91 public void flush() throws IOException 92 { 93 // Nothing to flush 94 } 95}