Coverage Report - org.apache.commons.betwixt.io.SAXBeanWriter

Classes in this File Line Coverage Branch Coverage Complexity
SAXBeanWriter
87% 
100% 
1.2

 1  
 /*
 2  
  * Copyright 2001-2004 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.io;
 17  
 
 18  
 import org.apache.commons.logging.Log;
 19  
 import org.apache.commons.logging.LogFactory;
 20  
 import org.xml.sax.Attributes;
 21  
 import org.xml.sax.ContentHandler;
 22  
 import org.xml.sax.SAXException;
 23  
 
 24  
 // FIX ME
 25  
 // At the moment, namespaces are NOT supported!
 26  
 
 27  
 /**
 28  
  * The SAXBeanwriter will send events to a ContentHandler
 29  
  * 
 30  
  * @author <a href="mailto:rdonkin@apache.org">Robert Burrell Donkin</a>
 31  
  * @author <a href="mailto:martin@mvdb.net">Martin van den Bemt</a>
 32  
  */
 33  
 public class SAXBeanWriter extends AbstractBeanWriter {
 34  
 
 35  
     /** Where the output goes */
 36  
     private ContentHandler contentHandler;    
 37  
     /** Log used for logging (Doh!) */
 38  65
     private Log log = LogFactory.getLog( SAXBeanWriter.class );
 39  
     /** Should document events (ie. start and end) be called? */
 40  65
     private boolean callDocumentEvents = true;
 41  
     
 42  
     /**
 43  
      * <p> Constructor sets writer used for output.</p>
 44  
      *
 45  
      * @param contentHandler feed events to this content handler
 46  
      */
 47  65
     public SAXBeanWriter(ContentHandler contentHandler) {
 48  65
         this.contentHandler = contentHandler;
 49  65
     }
 50  
 
 51  
     /** 
 52  
      * Should document events (ie start and end) be called?
 53  
      *
 54  
      * @return true if this SAXWriter should call start 
 55  
      * and end of the content handler
 56  
      * @since 0.5
 57  
      */
 58  
     public boolean getCallDocumentEvents() {
 59  0
         return callDocumentEvents;
 60  
     }
 61  
     
 62  
     /**
 63  
      * Sets whether the document events (ie start and end) should be called.
 64  
      *
 65  
      * @param callDocumentEvents should document events be called
 66  
      * @since 0.5
 67  
      */
 68  
     public void setCallDocumentEvents(boolean callDocumentEvents) {
 69  26
         this.callDocumentEvents = callDocumentEvents;
 70  26
     }
 71  
 
 72  
     /**
 73  
      * <p> Set the log implementation used. </p>
 74  
      *
 75  
      * @return <code>Log</code> implementation that this class logs to
 76  
      */ 
 77  
     public Log getLog() {
 78  0
         return log;
 79  
     }
 80  
 
 81  
     /**
 82  
      * <p> Set the log implementation used. </p>
 83  
      *
 84  
      * @param log <code>Log</code> implementation to use
 85  
      */ 
 86  
     public void setLog(Log log) {
 87  0
         this.log = log;
 88  0
     }
 89  
     
 90  
         
 91  
     // Expression methods
 92  
     //-------------------------------------------------------------------------     
 93  
     
 94  
     // Replaced by new API
 95  
 
 96  
     // New API
 97  
     // -------------------------------------------------------------------------
 98  
 
 99  
         
 100  
     /**
 101  
      * Writes the start tag for an element.
 102  
      *
 103  
      * @param uri the element's namespace uri
 104  
      * @param localName the element's local name 
 105  
      * @param qName the element's qualified name
 106  
      * @param attributes the element's attributes
 107  
      * @throws SAXException if an SAX problem occurs during writing 
 108  
      * @since 0.5
 109  
      */
 110  
     protected void startElement(
 111  
                                 WriteContext context,
 112  
                                 String uri, 
 113  
                                 String localName, 
 114  
                                 String qName, 
 115  
                                 Attributes attributes)
 116  
                                     throws
 117  
                                         SAXException {
 118  364
         contentHandler.startElement(
 119  182
                                 uri, 
 120  182
                                 localName, 
 121  182
                                 qName, 
 122  182
                                 attributes);
 123  182
     }
 124  
     
 125  
     /**
 126  
      * Writes the end tag for an element
 127  
      *
 128  
      * @param uri the element's namespace uri
 129  
      * @param localName the element's local name 
 130  
      * @param qName the element's qualified name
 131  
      * @throws SAXException if an SAX problem occurs during writing 
 132  
      * @since 0.5
 133  
      */
 134  
     protected void endElement(
 135  
                                 WriteContext context,
 136  
                                 String uri, 
 137  
                                 String localName, 
 138  
                                 String qName)
 139  
                                     throws
 140  
                                         SAXException {
 141  364
         contentHandler.endElement(
 142  182
                                 uri, 
 143  182
                                 localName, 
 144  182
                                 qName);
 145  182
     }
 146  
 
 147  
     /** 
 148  
      * Express body text 
 149  
      * @param text the element body text 
 150  
      * @throws SAXException if the <code>ContentHandler</code> has a problem
 151  
      * @since 0.5
 152  
      */
 153  
     protected void bodyText(WriteContext context, String text) throws SAXException  {
 154  
             //TODO:
 155  
         // FIX ME
 156  
         // CHECK UNICODE->CHAR CONVERSION!
 157  
         // THIS WILL QUITE POSSIBLY BREAK FOR NON-ROMAN
 158  117
         char[] body = text.toCharArray();
 159  117
         contentHandler.characters(body, 0, body.length);
 160  117
     }  
 161  
     
 162  
     /**
 163  
      * This will announce the start of the document
 164  
      * to the contenthandler.
 165  
      * 
 166  
      * @see org.apache.commons.betwixt.io.AbstractBeanWriter#end()
 167  
      */
 168  
     public void start() throws SAXException {
 169  65
         if ( callDocumentEvents ) {
 170  52
             contentHandler.startDocument();
 171  
         }
 172  65
     }
 173  
 
 174  
     /**
 175  
      * This method will announce the end of the document to
 176  
      * the contenthandler.
 177  
      * 
 178  
      * @see org.apache.commons.betwixt.io.AbstractBeanWriter#start()
 179  
      */
 180  
     public void end() throws SAXException {
 181  65
         if ( callDocumentEvents ) {
 182  52
             contentHandler.endDocument();
 183  
         }
 184  65
     }
 185  
 }