001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: * See the License for the specific language governing permissions and
015: * limitations under the License.
016: */
017: package org.apache.commons.betwixt;
018:
019: import org.apache.commons.betwixt.io.SAXBeanWriter;
020: import org.xml.sax.Attributes;
021: import org.xml.sax.ContentHandler;
022: import org.xml.sax.Locator;
023: import org.xml.sax.SAXException;
024:
025: /**
026: * I would SAX 'start element' event's attributes always expect to have qName
027: * equal to localName for simple, unprefixed XML tags. But that seems not to be
028: * true for betwixt output and breaks my application completely. <br>
029: * For the debugging output to STDOUT I would expect output like:
030: *
031: * <pre>
032: * XML: start document event
033: * XML: start element qName 'test-class', localName 'test-class', URI:
034: * - Attribute qName 'test-prop-1', localName 'test-prop-1' of CDATA: abc
035: * - Attribute qName 'test-prop-2', localName 'test-prop-2' of CDATA: 12
036: * - Attribute qName 'id', localName 'id' of ID: 1
037: * XML: end element 'test-class'
038: * XML: end document event
039: * </pre>
040: *
041: * but I get (the attributes local names differ from the qnames):
042: *
043: * <pre>
044: * XML: start document event
045: * XML: start element qName 'test-class', localName 'test-class', URI:
046: * - Attribute qName 'test-prop-1', localName 'testPropertyOne' of CDATA: abc
047: * </pre>
048: *
049: * got only the first two lines here beacuase assertEquals fails there.
050: *
051: * @author Christoph Gaffga, cgaffga@triplemind.com
052: */
053: public class TestAttributeQNameProblem extends AbstractTestCase {
054:
055: public TestAttributeQNameProblem(String testName) {
056: super (testName);
057: }
058:
059: public static class StdOutContentHandler implements ContentHandler {
060:
061: public void setDocumentLocator(Locator locator) {
062: }
063:
064: public void startDocument() throws SAXException {
065: System.out.println("XML: start document event");
066: }
067:
068: public void endDocument() throws SAXException {
069: System.out.println("XML: end document event");
070: }
071:
072: public void startPrefixMapping(String prefix, String uri)
073: throws SAXException {
074: System.out.println("XML: start prefix '" + prefix
075: + "' mapping, URI: " + uri);
076: }
077:
078: public void endPrefixMapping(String prefix) throws SAXException {
079: System.out.println("XML: end prefix '" + prefix
080: + "' mapping");
081: }
082:
083: public void startElement(String uri, String localName,
084: String qName, Attributes atts) throws SAXException {
085: System.out.println("XML: start element qName '" + qName
086: + "', localName '" + localName + "', URI:" + uri);
087: for (int i = 0; i < atts.getLength(); i++) {
088: System.out.println(" - Attribute qName '"
089: + atts.getQName(i) + "', localName '"
090: + atts.getLocalName(i) + "' of "
091: + atts.getType(i) + ": " + atts.getValue(i));
092: assertEquals(atts.getQName(i), atts.getLocalName(i));
093: }
094: }
095:
096: public void endElement(String uri, String localName,
097: String qName) throws SAXException {
098: System.out.println("XML: end element '" + qName + "'");
099: }
100:
101: public void characters(char[] ch, int start, int length)
102: throws SAXException {
103: System.out.println("XML: characters: from " + start
104: + ", length " + length);
105: }
106:
107: public void ignorableWhitespace(char[] ch, int start, int length)
108: throws SAXException {
109: }
110:
111: public void processingInstruction(String target, String data)
112: throws SAXException {
113: System.out.println("XML: processing instruction, target '"
114: + target + "': " + data);
115: }
116:
117: public void skippedEntity(String name) throws SAXException {
118: }
119:
120: }
121:
122: public void testAttributeOutput() {
123: try {
124: SAXBeanWriter beanWriter = new SAXBeanWriter(
125: new StdOutContentHandler());
126: Object bean = new SimpleClass();
127: beanWriter.write(bean);
128: } catch (Exception ex) {
129: ex.printStackTrace();
130: }
131: }
132:
133: }
|