1   /*
2    * Copyright 2005 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.commons.betwixt;
17  
18  import org.apache.commons.betwixt.io.SAXBeanWriter;
19  import org.xml.sax.Attributes;
20  import org.xml.sax.ContentHandler;
21  import org.xml.sax.Locator;
22  import org.xml.sax.SAXException;
23  
24  /***
25   * I would SAX 'start element' event's attributes always expect to have qName
26   * equal to localName for simple, unprefixed XML tags. But that seems not to be
27   * true for betwixt output and breaks my application completely. <br>
28   * For the debugging output to STDOUT I would expect output like:
29   * 
30   * <pre>
31   *   XML: start document event
32   *   XML: start element qName 'test-class', localName 'test-class', URI:
33   *        - Attribute qName 'test-prop-1', localName 'test-prop-1' of CDATA: abc
34   *        - Attribute qName 'test-prop-2', localName 'test-prop-2' of CDATA: 12
35   *        - Attribute qName 'id', localName 'id' of ID: 1
36   *   XML: end element 'test-class'
37   *   XML: end document event
38   * </pre>
39   * 
40   * but I get (the attributes local names differ from the qnames):
41   * 
42   * <pre>
43   *   XML: start document event
44   *   XML: start element qName 'test-class', localName 'test-class', URI:
45   *        - Attribute qName 'test-prop-1', localName 'testPropertyOne' of CDATA: abc
46   * </pre>
47   * 
48   * got only the first two lines here beacuase assertEquals fails there.
49   * 
50   * @author Christoph Gaffga, cgaffga@triplemind.com
51   */
52  public class TestAttributeQNameProblem extends AbstractTestCase {
53  
54      public TestAttributeQNameProblem(String testName) {
55          super(testName);
56      }
57  
58      public static class StdOutContentHandler implements ContentHandler {
59  
60          public void setDocumentLocator(Locator locator) {}
61  
62          public void startDocument() throws SAXException {
63              System.out.println("XML: start document event");
64          }
65  
66          public void endDocument() throws SAXException {
67              System.out.println("XML: end document event");
68          }
69  
70          public void startPrefixMapping(String prefix, String uri) throws SAXException {
71             System.out.println("XML: start prefix '" + prefix + "' mapping, URI: " + uri);
72          }
73  
74          public void endPrefixMapping(String prefix) throws SAXException {
75              System.out.println("XML: end prefix '" + prefix + "' mapping");
76          }
77  
78          public void startElement(String uri, String localName, String qName, Attributes atts)
79                  throws SAXException {
80              System.out.println("XML: start element qName '" + qName + "', localName '" + localName
81                      + "', URI:" + uri);
82              for (int i = 0; i < atts.getLength(); i++) {
83                  System.out.println("     - Attribute qName '" + atts.getQName(i) + "', localName '"
84                          + atts.getLocalName(i) + "' of " + atts.getType(i) + ": "
85                          + atts.getValue(i));
86                  assertEquals(atts.getQName(i), atts.getLocalName(i));
87              }
88          }
89  
90          public void endElement(String uri, String localName, String qName) throws SAXException {
91              System.out.println("XML: end element '" + qName + "'");
92          }
93  
94          public void characters(char[] ch, int start, int length) throws SAXException {
95              System.out.println("XML: characters: from " + start + ", length " + length);
96          }
97  
98          public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {}
99  
100         public void processingInstruction(String target, String data) throws SAXException {
101             System.out.println("XML: processing instruction, target '" + target + "': " + data);
102         }
103 
104         public void skippedEntity(String name) throws SAXException {}
105 
106     }
107 
108     public void testAttributeOutput() {
109         try {
110             SAXBeanWriter beanWriter = new SAXBeanWriter(new StdOutContentHandler());
111             Object bean = new SimpleClass();
112             beanWriter.write(bean);
113         } catch (Exception ex) {
114             ex.printStackTrace();
115         }
116     }
117 
118 }