blob: a3459f918c6119ecd12f7390f655d2c4bde89694 [file] [log] [blame]
malenkova468fa82008-12-22 17:42:49 +03001/*
2 * Copyright 2008 Sun Microsystems, Inc. All Rights Reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
20 * CA 95054 USA or visit www.sun.com if you need additional information or
21 * have any questions.
22 */
23
24/*
25 * @test
26 * @bug 4864117
27 * @summary Tests XMLDecoder within another DefaultHandler for SAX parser
28 * @author Sergey Malenkov
29 */
30
31import java.beans.XMLDecoder;
32import java.beans.ExceptionListener;
33
34import java.io.ByteArrayInputStream;
35import java.io.InputStream;
36import java.io.IOException;
37
38import javax.xml.parsers.ParserConfigurationException;
39import javax.xml.parsers.SAXParserFactory;
40
41import org.xml.sax.Attributes;
42import org.xml.sax.SAXException;
43import org.xml.sax.helpers.DefaultHandler;
44
45public final class Test4864117 extends DefaultHandler implements ExceptionListener {
46 private static final String TEST = "test";
47 private static final String DATA
48 = "<test>\n"
49 + " <void property=\"message\">\n"
50 + " <string>Hello, world!</string>\n"
51 + " </void>\n"
52 + "</test>";
53
54 public static void main(String[] args) {
55 Test4864117 test = new Test4864117();
56 InputStream input = new ByteArrayInputStream(DATA.getBytes());
57 Exception error = null;
58 try {
59 SAXParserFactory.newInstance().newSAXParser().parse(input, test);
60 }
61 catch (ParserConfigurationException exception) {
62 error = exception;
63 }
64 catch (SAXException exception) {
65 error = exception.getException();
66 if (error == null) {
67 error = exception;
68 }
69 }
70 catch (IOException exception) {
71 error = exception;
72 }
73 if (error != null) {
74 throw new Error("unexpected error", error);
75 }
76 test.print('?', test.getMessage());
77 }
78
79 private String message;
80
81 public String getMessage() {
82 if (this.message == null) {
83 throw new Error("owner's method is not called");
84 }
85 return this.message;
86 }
87
88 public void setMessage(String message) {
89 this.message = message;
90 print(':', this.message);
91 }
92
93 // DefaultHandler implementation
94
95 private DefaultHandler handler;
96 private int depth;
97
98 @Override
99 public void startDocument() throws SAXException {
100 this.handler = XMLDecoder.createHandler(this, this, null);
101 this.handler.startDocument();
102 }
103
104 @Override
105 public void endDocument() {
106 this.handler = null;
107 }
108
109 @Override
110 public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
111 print('>', qName);
112 if (this.depth > 0) {
113 this.handler.startElement(uri, localName, qName, attributes);
114 } else if (!TEST.equals(qName)) {
115 throw new SAXException("unexpected element name: " + qName);
116 }
117 this.depth++;
118 }
119
120 @Override
121 public void endElement(String uri, String localName, String qName) throws SAXException {
122 this.depth--;
123 print('<', qName);
124 if (this.depth > 0) {
125 this.handler.endElement(uri, localName, qName);
126 } else if (!TEST.equals(qName)) {
127 throw new SAXException("unexpected element name: " + qName);
128 }
129 }
130
131 @Override
132 public void characters(char[] ch, int start, int length) throws SAXException {
133 this.handler.characters(ch, start, length);
134 }
135
136 public void exceptionThrown(Exception exception) {
137 throw new Error("unexpected exception", exception);
138 }
139
140 private void print(char ch, String name) {
141 StringBuilder sb = new StringBuilder();
142 for (int i = 0; i < this.depth; i++) sb.append(' ');
143 sb.append(ch).append(' ').append(name);
144 System.out.println(sb.toString());
145 }
146}