1 /*
2 * Licensed to the Apache Software Foundation (ASF) under one or more
3 * contributor license agreements. See the NOTICE file distributed with
4 * this work for additional information regarding copyright ownership.
5 * The ASF licenses this file to You under the Apache License, Version 2.0
6 * (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17 package org.apache.commons.betwixt;
18
19 import org.apache.commons.betwixt.io.SAXBeanWriter;
20 import org.xml.sax.Attributes;
21 import org.xml.sax.ContentHandler;
22 import org.xml.sax.Locator;
23 import org.xml.sax.SAXException;
24
25 /**
26 * I would SAX 'start element' event's attributes always expect to have qName
27 * equal to localName for simple, unprefixed XML tags. But that seems not to be
28 * true for betwixt output and breaks my application completely. <br>
29 * For the debugging output to STDOUT I would expect output like:
30 *
31 * <pre>
32 * XML: start document event
33 * XML: start element qName 'test-class', localName 'test-class', URI:
34 * - Attribute qName 'test-prop-1', localName 'test-prop-1' of CDATA: abc
35 * - Attribute qName 'test-prop-2', localName 'test-prop-2' of CDATA: 12
36 * - Attribute qName 'id', localName 'id' of ID: 1
37 * XML: end element 'test-class'
38 * XML: end document event
39 * </pre>
40 *
41 * but I get (the attributes local names differ from the qnames):
42 *
43 * <pre>
44 * XML: start document event
45 * XML: start element qName 'test-class', localName 'test-class', URI:
46 * - Attribute qName 'test-prop-1', localName 'testPropertyOne' of CDATA: abc
47 * </pre>
48 *
49 * got only the first two lines here beacuase assertEquals fails there.
50 *
51 * @author Christoph Gaffga, cgaffga@triplemind.com
52 */
53 public class TestAttributeQNameProblem extends AbstractTestCase {
54
55 public TestAttributeQNameProblem(String testName) {
56 super(testName);
57 }
58
59 public static class StdOutContentHandler implements ContentHandler {
60
61 public void setDocumentLocator(Locator locator) {}
62
63 public void startDocument() throws SAXException {
64 System.out.println("XML: start document event");
65 }
66
67 public void endDocument() throws SAXException {
68 System.out.println("XML: end document event");
69 }
70
71 public void startPrefixMapping(String prefix, String uri) throws SAXException {
72 System.out.println("XML: start prefix '" + prefix + "' mapping, URI: " + uri);
73 }
74
75 public void endPrefixMapping(String prefix) throws SAXException {
76 System.out.println("XML: end prefix '" + prefix + "' mapping");
77 }
78
79 public void startElement(String uri, String localName, String qName, Attributes atts)
80 throws SAXException {
81 System.out.println("XML: start element qName '" + qName + "', localName '" + localName
82 + "', URI:" + uri);
83 for (int i = 0; i < atts.getLength(); i++) {
84 System.out.println(" - Attribute qName '" + atts.getQName(i) + "', localName '"
85 + atts.getLocalName(i) + "' of " + atts.getType(i) + ": "
86 + atts.getValue(i));
87 assertEquals(atts.getQName(i), atts.getLocalName(i));
88 }
89 }
90
91 public void endElement(String uri, String localName, String qName) throws SAXException {
92 System.out.println("XML: end element '" + qName + "'");
93 }
94
95 public void characters(char[] ch, int start, int length) throws SAXException {
96 System.out.println("XML: characters: from " + start + ", length " + length);
97 }
98
99 public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {}
100
101 public void processingInstruction(String target, String data) throws SAXException {
102 System.out.println("XML: processing instruction, target '" + target + "': " + data);
103 }
104
105 public void skippedEntity(String name) throws SAXException {}
106
107 }
108
109 public void testAttributeOutput() {
110 try {
111 SAXBeanWriter beanWriter = new SAXBeanWriter(new StdOutContentHandler());
112 Object bean = new SimpleClass();
113 beanWriter.write(bean);
114 } catch (Exception ex) {
115 ex.printStackTrace();
116 }
117 }
118
119 }