UnescapedData.java 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // Copyright (c) 2002, 2003 Per M.A. Bothner.
  2. // This is free software; for terms and warranty disclaimer see ./COPYING.
  3. package gnu.lists;
  4. import java.io.*;
  5. /** Used for text that is supposed to be written out verbatim.
  6. * For example, if the output format is XML, can be used to write
  7. * a literal {@code '<'} as a plain {@code "<"}, instead of being
  8. * escaped as {@code "&lt;"}.
  9. */
  10. public class UnescapedData implements
  11. /* #ifdef use:java.lang.CharSequence */
  12. CharSequence,
  13. /* #endif */
  14. Externalizable
  15. {
  16. String data;
  17. public UnescapedData ()
  18. {
  19. }
  20. public UnescapedData (String data)
  21. {
  22. this.data = data;
  23. }
  24. public final String getData() { return data; }
  25. public final String toString() { return data; }
  26. public final boolean equals(Object other)
  27. {
  28. return other instanceof UnescapedData
  29. && data.equals(other.toString());
  30. }
  31. public final int hashCode() { return data == null ? 0 : data.hashCode(); }
  32. public int length()
  33. {
  34. return data.length();
  35. }
  36. public char charAt(int index)
  37. {
  38. return data.charAt(index);
  39. }
  40. /* #ifdef use:java.lang.CharSequence */
  41. public CharSequence subSequence(int start, int end)
  42. {
  43. return new UnescapedData(data.substring(start, end));
  44. }
  45. /* #endif */
  46. /**
  47. * @serialData Write 'data' (using writeObject).
  48. */
  49. public void writeExternal(ObjectOutput out) throws IOException
  50. {
  51. out.writeObject(data);
  52. }
  53. public void readExternal(ObjectInput in)
  54. throws IOException, ClassNotFoundException
  55. {
  56. data = (String) in.readObject();
  57. }
  58. }