View Javadoc
1   /*
2    * reserved comment block
3    * DO NOT REMOVE OR ALTER!
4    */
5   /*
6    * Copyright 2001-2004 The Apache Software Foundation.
7    *
8    * Licensed under the Apache License, Version 2.0 (the "License");
9    * you may not use this file except in compliance with the License.
10   * You may obtain a copy of the License at
11   *
12   *     http://www.apache.org/licenses/LICENSE-2.0
13   *
14   * Unless required by applicable law or agreed to in writing, software
15   * distributed under the License is distributed on an "AS IS" BASIS,
16   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17   * See the License for the specific language governing permissions and
18   * limitations under the License.
19   */
20  /*
21   * $Id: StringValueHandler.java,v 1.2.4.1 2005/09/06 11:33:25 pvedula Exp $
22   */
23  
24  package com.sun.org.apache.xalan.internal.xsltc.runtime;
25  
26  import org.xml.sax.SAXException;
27  
28  import com.sun.org.apache.xml.internal.serializer.EmptySerializer;
29  
30  /**
31   * @author Jacek Ambroziak
32   * @author Santiago Pericas-Geertsen
33   * @author Morten Jorgensen
34   */
35  public final class StringValueHandler extends EmptySerializer {
36  
37      private StringBuilder _buffer = new StringBuilder();
38      private String _str = null;
39      private static final String EMPTY_STR = "";
40      private boolean m_escaping = false;
41      private int _nestedLevel = 0;
42  
43      public void characters(char[] ch, int off, int len)
44          throws SAXException
45      {
46          if (_nestedLevel > 0)
47              return;
48  
49          if (_str != null) {
50              _buffer.append(_str);
51              _str = null;
52          }
53          _buffer.append(ch, off, len);
54      }
55  
56      public String getValue() {
57          if (_buffer.length() != 0) {
58              String result = _buffer.toString();
59              _buffer.setLength(0);
60              return result;
61          }
62          else {
63              String result = _str;
64              _str = null;
65              return (result != null) ? result : EMPTY_STR;
66          }
67      }
68  
69      public void characters(String characters) throws SAXException {
70          if (_nestedLevel > 0)
71              return;
72  
73          if (_str == null && _buffer.length() == 0) {
74              _str = characters;
75          }
76          else {
77              if (_str != null) {
78                  _buffer.append(_str);
79                  _str = null;
80              }
81  
82              _buffer.append(characters);
83          }
84      }
85  
86      public void startElement(String qname) throws SAXException {
87          _nestedLevel++;
88      }
89  
90      public void endElement(String qname) throws SAXException {
91          _nestedLevel--;
92      }
93  
94      // Override the setEscaping method just to indicate that this class is
95      // aware that that method might be called.
96      public boolean setEscaping(boolean bool) {
97          boolean oldEscaping = m_escaping;
98          m_escaping = bool;
99  
100         return bool;
101     }
102 
103     /**
104      * The value of a PI must not contain the substring "?>". Should
105      * that substring be present, replace it by "? >".
106      */
107     public String getValueOfPI() {
108         final String value = getValue();
109 
110         if (value.indexOf("?>") > 0) {
111             final int n = value.length();
112             final StringBuilder valueOfPI = new StringBuilder();
113 
114             for (int i = 0; i < n;) {
115                 final char ch = value.charAt(i++);
116                 if (ch == '?' && value.charAt(i) == '>') {
117                     valueOfPI.append("? >"); i++;
118                 }
119                 else {
120                     valueOfPI.append(ch);
121                 }
122             }
123             return valueOfPI.toString();
124         }
125         return value;
126     }
127 }