KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xalan > xsltc > runtime > StringValueHandler


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
20 package org.apache.xalan.xsltc.runtime;
21
22 import org.xml.sax.SAXException JavaDoc;
23
24 import org.apache.xml.serializer.EmptySerializer;
25
26 /**
27  * @author Jacek Ambroziak
28  * @author Santiago Pericas-Geertsen
29  * @author Morten Jorgensen
30  */

31 public final class StringValueHandler extends EmptySerializer {
32
33     private StringBuffer JavaDoc _buffer = new StringBuffer JavaDoc();
34     private String JavaDoc _str = null;
35     private static final String JavaDoc EMPTY_STR = "";
36     private boolean m_escaping = false;
37     private int _nestedLevel = 0;
38     
39     public void characters(char[] ch, int off, int len)
40     throws SAXException JavaDoc
41     {
42     if (_nestedLevel > 0)
43         return;
44     
45     if (_str != null) {
46         _buffer.append(_str);
47         _str = null;
48     }
49     _buffer.append(ch, off, len);
50     }
51
52     public String JavaDoc getValue() {
53     if (_buffer.length() != 0) {
54         String JavaDoc result = _buffer.toString();
55         _buffer.setLength(0);
56         return result;
57     }
58     else {
59         String JavaDoc result = _str;
60         _str = null;
61         return (result != null) ? result : EMPTY_STR;
62     }
63     }
64
65     public void characters(String JavaDoc characters) throws SAXException JavaDoc {
66     if (_nestedLevel > 0)
67         return;
68
69     if (_str == null && _buffer.length() == 0) {
70         _str = characters;
71     }
72     else {
73         if (_str != null) {
74             _buffer.append(_str);
75             _str = null;
76         }
77         
78         _buffer.append(characters);
79     }
80     }
81     
82     public void startElement(String JavaDoc qname) throws SAXException JavaDoc {
83         _nestedLevel++;
84     }
85
86     public void endElement(String JavaDoc qname) throws SAXException JavaDoc {
87         _nestedLevel--;
88     }
89
90     // Override the setEscaping method just to indicate that this class is
91
// aware that that method might be called.
92
public boolean setEscaping(boolean bool) {
93         boolean oldEscaping = m_escaping;
94         m_escaping = bool;
95
96         return bool;
97     }
98
99     /**
100      * The value of a PI must not contain the substring "?>". Should
101      * that substring be present, replace it by "? >".
102      */

103     public String JavaDoc getValueOfPI() {
104     final String JavaDoc value = getValue();
105
106     if (value.indexOf("?>") > 0) {
107         final int n = value.length();
108         final StringBuffer JavaDoc valueOfPI = new StringBuffer JavaDoc();
109
110         for (int i = 0; i < n;) {
111         final char ch = value.charAt(i++);
112         if (ch == '?' && value.charAt(i) == '>') {
113             valueOfPI.append("? >"); i++;
114         }
115         else {
116             valueOfPI.append(ch);
117         }
118         }
119         return valueOfPI.toString();
120     }
121     return value;
122     }
123 }
124
Popular Tags