/src/solidstack/query/QueryEncodingWriter.java

http://solidstack.googlecode.com/ · Java · 95 lines · 50 code · 12 blank · 33 comment · 5 complexity · 9692850bd88408a5274d1cc4402716a9 MD5 · raw file

  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. package solidstack.query;
  17. import java.io.IOException;
  18. import java.util.ArrayList;
  19. import java.util.BitSet;
  20. import java.util.List;
  21. import solidstack.template.EncodingWriter;
  22. /**
  23. * A writer that keeps values separate from the string segments.
  24. *
  25. * @author René M. de Bloois
  26. */
  27. public class QueryEncodingWriter implements EncodingWriter
  28. {
  29. private List< Object > values = new ArrayList< Object >();
  30. private BitSet isValue = new BitSet();
  31. //@Override
  32. public void write( String s )
  33. {
  34. if( s != null && s.length() > 0 )
  35. this.values.add( s );
  36. }
  37. //@Override
  38. public void writeEncoded( Object o ) throws IOException
  39. {
  40. this.isValue.set( this.values.size() );
  41. this.values.add( o );
  42. }
  43. public boolean stringsOnly()
  44. {
  45. return false;
  46. }
  47. /**
  48. * Returns the string segments and the values.
  49. *
  50. * @return An array of string segments (String) and the values (unknown Object).
  51. */
  52. public List< Object > getValues()
  53. {
  54. return this.values;
  55. }
  56. /**
  57. * Returns a bitset that indicates which indexes in the {@link #getValues()} list is a value.
  58. *
  59. * @return A bitset that indicates which indexes in the {@link #getValues()} list is a value.
  60. */
  61. public BitSet getIsValue()
  62. {
  63. return this.isValue;
  64. }
  65. @Override
  66. public String toString()
  67. {
  68. StringBuilder result = new StringBuilder();
  69. int len = this.values.size();
  70. for( int i = 0; i < len; i++ )
  71. {
  72. if( this.isValue.get( i ) )
  73. result.append( this.values.get( i ).toString() );
  74. else
  75. result.append( (String)this.values.get( i ) );
  76. }
  77. return result.toString();
  78. }
  79. public void flush() throws IOException
  80. {
  81. // Nothing to flush
  82. }
  83. }