Path: blob/master/test/jdk/java/beans/XMLDecoder/Test4864117.java
41149 views
/*1* Copyright (c) 2008, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*/2223/*24* @test25* @bug 486411726* @summary Tests XMLDecoder within another DefaultHandler for SAX parser27* @author Sergey Malenkov28*/2930import java.beans.XMLDecoder;31import java.beans.ExceptionListener;3233import java.io.ByteArrayInputStream;34import java.io.InputStream;35import java.io.IOException;3637import javax.xml.parsers.ParserConfigurationException;38import javax.xml.parsers.SAXParserFactory;3940import org.xml.sax.Attributes;41import org.xml.sax.SAXException;42import org.xml.sax.helpers.DefaultHandler;4344public final class Test4864117 extends DefaultHandler implements ExceptionListener {45private static final String TEST = "test";46private static final String DATA47= "<test>\n"48+ " <void property=\"message\">\n"49+ " <string>Hello, world!</string>\n"50+ " </void>\n"51+ "</test>";5253public static void main(String[] args) {54Test4864117 test = new Test4864117();55InputStream input = new ByteArrayInputStream(DATA.getBytes());56Exception error = null;57try {58SAXParserFactory.newInstance().newSAXParser().parse(input, test);59}60catch (ParserConfigurationException exception) {61error = exception;62}63catch (SAXException exception) {64error = exception.getException();65if (error == null) {66error = exception;67}68}69catch (IOException exception) {70error = exception;71}72if (error != null) {73throw new Error("unexpected error", error);74}75test.print('?', test.getMessage());76}7778private String message;7980public String getMessage() {81if (this.message == null) {82throw new Error("owner's method is not called");83}84return this.message;85}8687public void setMessage(String message) {88this.message = message;89print(':', this.message);90}9192// DefaultHandler implementation9394private DefaultHandler handler;95private int depth;9697@Override98public void startDocument() throws SAXException {99this.handler = XMLDecoder.createHandler(this, this, null);100this.handler.startDocument();101}102103@Override104public void endDocument() {105this.handler = null;106}107108@Override109public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {110print('>', qName);111if (this.depth > 0) {112this.handler.startElement(uri, localName, qName, attributes);113} else if (!TEST.equals(qName)) {114throw new SAXException("unexpected element name: " + qName);115}116this.depth++;117}118119@Override120public void endElement(String uri, String localName, String qName) throws SAXException {121this.depth--;122print('<', qName);123if (this.depth > 0) {124this.handler.endElement(uri, localName, qName);125} else if (!TEST.equals(qName)) {126throw new SAXException("unexpected element name: " + qName);127}128}129130@Override131public void characters(char[] ch, int start, int length) throws SAXException {132this.handler.characters(ch, start, length);133}134135public void exceptionThrown(Exception exception) {136throw new Error("unexpected exception", exception);137}138139private void print(char ch, String name) {140StringBuilder sb = new StringBuilder();141for (int i = 0; i < this.depth; i++) sb.append(' ');142sb.append(ch).append(' ').append(name);143System.out.println(sb.toString());144}145}146147148