001: /*
002: * Copyright 2001-2004 The Apache Software Foundation.
003: *
004: * Licensed under the Apache License, Version 2.0 (the "License");
005: * you may not use this file except in compliance with the License.
006: * You may obtain a copy of the License at
007: *
008: * http://www.apache.org/licenses/LICENSE-2.0
009: *
010: * Unless required by applicable law or agreed to in writing, software
011: * distributed under the License is distributed on an "AS IS" BASIS,
012: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013: * See the License for the specific language governing permissions and
014: * limitations under the License.
015: */
016: /*
017: * $Id: StringValueHandler.java,v 1.10 2004/02/16 22:55:55 minchau Exp $
018: */
019:
020: package org.apache.xalan.xsltc.runtime;
021:
022: import org.xml.sax.SAXException;
023:
024: import org.apache.xml.serializer.EmptySerializer;
025:
026: /**
027: * @author Jacek Ambroziak
028: * @author Santiago Pericas-Geertsen
029: * @author Morten Jorgensen
030: */
031: public final class StringValueHandler extends EmptySerializer {
032:
033: private StringBuffer _buffer = new StringBuffer();
034: private String _str = null;
035: private static final String EMPTY_STR = "";
036: private boolean m_escaping = false;
037: private int _nestedLevel = 0;
038:
039: public void characters(char[] ch, int off, int len)
040: throws SAXException {
041: if (_nestedLevel > 0)
042: return;
043:
044: if (_str != null) {
045: _buffer.append(_str);
046: _str = null;
047: }
048: _buffer.append(ch, off, len);
049: }
050:
051: public String getValue() {
052: if (_buffer.length() != 0) {
053: String result = _buffer.toString();
054: _buffer.setLength(0);
055: return result;
056: } else {
057: String result = _str;
058: _str = null;
059: return (result != null) ? result : EMPTY_STR;
060: }
061: }
062:
063: public void characters(String characters) throws SAXException {
064: if (_nestedLevel > 0)
065: return;
066:
067: if (_str == null && _buffer.length() == 0) {
068: _str = characters;
069: } else {
070: if (_str != null) {
071: _buffer.append(_str);
072: _str = null;
073: }
074:
075: _buffer.append(characters);
076: }
077: }
078:
079: public void startElement(String qname) throws SAXException {
080: _nestedLevel++;
081: }
082:
083: public void endElement(String qname) throws SAXException {
084: _nestedLevel--;
085: }
086:
087: // Override the setEscaping method just to indicate that this class is
088: // aware that that method might be called.
089: public boolean setEscaping(boolean bool) {
090: boolean oldEscaping = m_escaping;
091: m_escaping = bool;
092:
093: return bool;
094: }
095:
096: /**
097: * The value of a PI must not contain the substring "?>". Should
098: * that substring be present, replace it by "? >".
099: */
100: public String getValueOfPI() {
101: final String value = getValue();
102:
103: if (value.indexOf("?>") > 0) {
104: final int n = value.length();
105: final StringBuffer valueOfPI = new StringBuffer();
106:
107: for (int i = 0; i < n;) {
108: final char ch = value.charAt(i++);
109: if (ch == '?' && value.charAt(i) == '>') {
110: valueOfPI.append("? >");
111: i++;
112: } else {
113: valueOfPI.append(ch);
114: }
115: }
116: return valueOfPI.toString();
117: }
118: return value;
119: }
120: }
|