1. /*
  2. * Copyright 2001-2004 The Apache Software Foundation.
  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. /*
  17. * $Id: StringValueHandler.java,v 1.10 2004/02/16 22:55:55 minchau Exp $
  18. */
  19. package com.sun.org.apache.xalan.internal.xsltc.runtime;
  20. import org.xml.sax.SAXException;
  21. import com.sun.org.apache.xml.internal.serializer.EmptySerializer;
  22. /**
  23. * @author Jacek Ambroziak
  24. * @author Santiago Pericas-Geertsen
  25. * @author Morten Jorgensen
  26. */
  27. public final class StringValueHandler extends EmptySerializer {
  28. private StringBuffer _buffer = new StringBuffer();
  29. private String _str = null;
  30. private static final String EMPTY_STR = "";
  31. private boolean m_escaping = false;
  32. private int _nestedLevel = 0;
  33. public void characters(char[] ch, int off, int len)
  34. throws SAXException
  35. {
  36. if (_nestedLevel > 0)
  37. return;
  38. if (_str != null) {
  39. _buffer.append(_str);
  40. _str = null;
  41. }
  42. _buffer.append(ch, off, len);
  43. }
  44. public String getValue() {
  45. if (_buffer.length() != 0) {
  46. String result = _buffer.toString();
  47. _buffer.setLength(0);
  48. return result;
  49. }
  50. else {
  51. String result = _str;
  52. _str = null;
  53. return (result != null) ? result : EMPTY_STR;
  54. }
  55. }
  56. public void characters(String characters) throws SAXException {
  57. if (_nestedLevel > 0)
  58. return;
  59. if (_str == null && _buffer.length() == 0) {
  60. _str = characters;
  61. }
  62. else {
  63. if (_str != null) {
  64. _buffer.append(_str);
  65. _str = null;
  66. }
  67. _buffer.append(characters);
  68. }
  69. }
  70. public void startElement(String qname) throws SAXException {
  71. _nestedLevel++;
  72. }
  73. public void endElement(String qname) throws SAXException {
  74. _nestedLevel--;
  75. }
  76. // Override the setEscaping method just to indicate that this class is
  77. // aware that that method might be called.
  78. public boolean setEscaping(boolean bool) {
  79. boolean oldEscaping = m_escaping;
  80. m_escaping = bool;
  81. return bool;
  82. }
  83. /**
  84. * The value of a PI must not contain the substring "?>". Should
  85. * that substring be present, replace it by "? >".
  86. */
  87. public String getValueOfPI() {
  88. final String value = getValue();
  89. if (value.indexOf("?>") > 0) {
  90. final int n = value.length();
  91. final StringBuffer valueOfPI = new StringBuffer();
  92. for (int i = 0; i < n;) {
  93. final char ch = value.charAt(i++);
  94. if (ch == '?' && value.charAt(i) == '>') {
  95. valueOfPI.append("? >"); i++;
  96. }
  97. else {
  98. valueOfPI.append(ch);
  99. }
  100. }
  101. return valueOfPI.toString();
  102. }
  103. return value;
  104. }
  105. }