KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > cocoon > components > xscript > StringBufferContentHandler


1 /*
2  * Copyright 1999-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 package org.apache.cocoon.components.xscript;
17
18 import org.xml.sax.Attributes JavaDoc;
19 import org.xml.sax.SAXException JavaDoc;
20 import org.xml.sax.helpers.DefaultHandler JavaDoc;
21
22 import java.util.Stack JavaDoc;
23
24
25 /**
26  * A <code>ContentHandler</code> that accumulates the SAX stream into
27  * a <code>StringBuffer</code> object.
28  *
29  * @author <a HREF="mailto:ovidiu@cup.hp.com">Ovidiu Predescu</a>
30  * @author <a HREF="mailto:vgritsenko@apache.org">Vadim Gritsenko</a>
31  * @version CVS $Id: StringBufferContentHandler.java 30932 2004-07-29 17:35:38Z vgritsenko $
32  * @since August 30, 2001
33  */

34 public class StringBufferContentHandler extends DefaultHandler JavaDoc {
35     private static Object JavaDoc marker = new Object JavaDoc();
36
37     private Stack JavaDoc namespaces = new Stack JavaDoc();
38     private StringBuffer JavaDoc stringBuffer;
39
40     public StringBufferContentHandler(StringBuffer JavaDoc stringBuffer) {
41         this.stringBuffer = stringBuffer;
42     }
43
44     public void startPrefixMapping(String JavaDoc prefix, String JavaDoc uri)
45             throws SAXException JavaDoc {
46         namespaces.push(new NPU(prefix, uri));
47     }
48
49     public void endPrefixMapping(String JavaDoc prefix)
50             throws SAXException JavaDoc {
51         namespaces.pop();
52     }
53
54     public void startElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc qName, Attributes JavaDoc a)
55             throws SAXException JavaDoc {
56         int lastNamespaceIndex = 0;
57
58         lastNamespaceIndex = namespaces.size() - 1;
59         while (lastNamespaceIndex >= 0
60                 && namespaces.elementAt(lastNamespaceIndex) != marker) {
61             lastNamespaceIndex--;
62         }
63
64         if (lastNamespaceIndex < 0) {
65             lastNamespaceIndex = 0;
66         } else if (namespaces.elementAt(lastNamespaceIndex) == marker) {
67             lastNamespaceIndex++;
68         }
69
70         namespaces.push(marker);
71         stringBuffer.append("<").append(qName);
72
73         for (int i = 0, len = a.getLength(); i < len; i++) {
74             // Check if the attribute is a namespace declaration. Some
75
// parsers (Xerces) sometimes pass the namespace declaration
76
// as an attribute. We need to catch this case so that we
77
// don't end up generating the namespace declaration twice.
78
String JavaDoc attrName = a.getQName(i);
79             if (attrName.startsWith("xmlns:")) {
80                 // We have a namespace declaration
81
String JavaDoc name = a.getLocalName(i);
82
83                 // Check whether this namespace has been already declared
84
boolean found = false;
85                 for (int j = namespaces.size() - 1;
86                      j >= lastNamespaceIndex;
87                      j--) {
88                     Object JavaDoc obj = namespaces.elementAt(j);
89                     if (obj == marker) {
90                         continue;
91                     }
92                     NPU npu = (NPU) obj;
93
94                     if (name.equals(npu.prefix)) {
95                         found = true;
96                         break;
97                     }
98                 }
99
100                 if (!found) {
101                     namespaces.push(new NPU(name, a.getValue(i)));
102                 }
103             } else {
104                 // Normal attribute
105
stringBuffer.append(" ").append(a.getQName(i)).append("=\"");
106                 escape(stringBuffer, a.getValue(i));
107                 stringBuffer.append("\"");
108             }
109         }
110
111         if (namespaces.size() != 0) {
112             for (int i = namespaces.size() - 1; i >= lastNamespaceIndex; i--) {
113                 Object JavaDoc obj = namespaces.elementAt(i);
114                 if (obj == marker) {
115                     continue;
116                 }
117                 NPU npu = (NPU) obj;
118                 if ("".equals(npu.prefix)) {
119                     // Default namespace
120
stringBuffer.append(" xmlns").append("=\"").append(npu.uri).append("\"");
121                 } else {
122                     stringBuffer.append(" xmlns:").append(npu.prefix).append("=\"").append(npu.uri).append("\"");
123                 }
124             }
125         }
126
127         stringBuffer.append(">");
128     }
129
130     public void endElement(String JavaDoc uri, String JavaDoc loc, String JavaDoc qName)
131             throws SAXException JavaDoc {
132         stringBuffer.append("</").append(qName).append(">");
133
134         Object JavaDoc obj;
135         do {
136             obj = namespaces.pop();
137         } while (obj != marker);
138     }
139
140     public void characters(char ch[], int start, int len)
141             throws SAXException JavaDoc {
142         escape(stringBuffer, ch, start, len);
143     }
144
145
146     /**
147      * Copies string into buffer and
148      * escapes all '<', '&' and '>' chars in the string with
149      * corresponding entities.
150      */

151     private static void escape(StringBuffer JavaDoc buffer, String JavaDoc s) {
152         char[] ch = s.toCharArray();
153         escape(buffer, ch, 0, ch.length);
154     }
155
156     /**
157      * Copies characters from the char array into buffer and
158      * escapes all '<', '&' and '>' chars with corresponding
159      * entities.
160      */

161     private static void escape(StringBuffer JavaDoc buffer, char ch[], int start, int len) {
162         for (int i = start; i < start + len; i++) {
163             switch (ch[i]) {
164                 case '<':
165                     buffer.append("&lt;");
166                     break;
167                 case '&':
168                     buffer.append("&amp;");
169                     break;
170                 case '>':
171                     buffer.append("&gt;");
172                     break;
173                 default:
174                     buffer.append(ch[i]);
175                     break;
176             }
177         }
178     }
179 }
180
181 class NPU {
182     public String JavaDoc prefix;
183     public String JavaDoc uri;
184
185     NPU(String JavaDoc prefix, String JavaDoc uri) {
186         this.prefix = prefix;
187         this.uri = uri;
188     }
189
190     public String JavaDoc toString() {
191         return this.prefix + "=" + this.uri;
192     }
193 }
194
Popular Tags