Clover coverage report - dom4j - 1.6.1
Coverage timestamp: ma mei 16 2005 14:23:01 GMT+01:00
file stats: LOC: 945   Methods: 48
NCLOC: 436   Classes: 1
 
 Source file Conditionals Statements Methods TOTAL
SAXWriter.java 65,5% 62,2% 66,7% 63,6%
coverage coverage
 1    /*
 2    * Copyright 2001-2005 (C) MetaStuff, Ltd. All Rights Reserved.
 3    *
 4    * This software is open source.
 5    * See the bottom of this file for the licence.
 6    */
 7   
 8    package org.dom4j.io;
 9   
 10    import java.io.IOException;
 11    import java.util.HashMap;
 12    import java.util.Iterator;
 13    import java.util.List;
 14    import java.util.Map;
 15   
 16    import org.dom4j.Attribute;
 17    import org.dom4j.Branch;
 18    import org.dom4j.CDATA;
 19    import org.dom4j.CharacterData;
 20    import org.dom4j.Comment;
 21    import org.dom4j.Document;
 22    import org.dom4j.DocumentType;
 23    import org.dom4j.Element;
 24    import org.dom4j.Entity;
 25    import org.dom4j.Namespace;
 26    import org.dom4j.Node;
 27    import org.dom4j.ProcessingInstruction;
 28    import org.dom4j.Text;
 29    import org.dom4j.tree.NamespaceStack;
 30   
 31    import org.xml.sax.Attributes;
 32    import org.xml.sax.ContentHandler;
 33    import org.xml.sax.DTDHandler;
 34    import org.xml.sax.EntityResolver;
 35    import org.xml.sax.ErrorHandler;
 36    import org.xml.sax.InputSource;
 37    import org.xml.sax.SAXException;
 38    import org.xml.sax.SAXNotRecognizedException;
 39    import org.xml.sax.SAXNotSupportedException;
 40    import org.xml.sax.XMLReader;
 41    import org.xml.sax.ext.LexicalHandler;
 42    import org.xml.sax.helpers.AttributesImpl;
 43    import org.xml.sax.helpers.LocatorImpl;
 44   
 45    /**
 46    * <p>
 47    * <code>SAXWriter</code> writes a DOM4J tree to a SAX ContentHandler.
 48    * </p>
 49    *
 50    * @author <a href="mailto:james.strachan@metastuff.com">James Strachan </a>
 51    * @version $Revision: 1.24 $
 52    */
 53    public class SAXWriter implements XMLReader {
 54    protected static final String[] LEXICAL_HANDLER_NAMES = {
 55    "http://xml.org/sax/properties/lexical-handler",
 56    "http://xml.org/sax/handlers/LexicalHandler" };
 57   
 58    protected static final String FEATURE_NAMESPACE_PREFIXES
 59    = "http://xml.org/sax/features/namespace-prefixes";
 60   
 61    protected static final String FEATURE_NAMESPACES
 62    = "http://xml.org/sax/features/namespaces";
 63   
 64    /** <code>ContentHandler</code> to which SAX events are raised */
 65    private ContentHandler contentHandler;
 66   
 67    /** <code>DTDHandler</code> fired when a document has a DTD */
 68    private DTDHandler dtdHandler;
 69   
 70    /** <code>EntityResolver</code> fired when a document has a DTD */
 71    private EntityResolver entityResolver;
 72   
 73    private ErrorHandler errorHandler;
 74   
 75    /** <code>LexicalHandler</code> fired on Entity and CDATA sections */
 76    private LexicalHandler lexicalHandler;
 77   
 78    /** <code>AttributesImpl</code> used when generating the Attributes */
 79    private AttributesImpl attributes = new AttributesImpl();
 80   
 81    /** Stores the features */
 82    private Map features = new HashMap();
 83   
 84    /** Stores the properties */
 85    private Map properties = new HashMap();
 86   
 87    /** Whether namespace declarations are exported as attributes or not */
 88    private boolean declareNamespaceAttributes;
 89   
 90  20 public SAXWriter() {
 91  20 properties.put(FEATURE_NAMESPACE_PREFIXES, Boolean.FALSE);
 92  20 properties.put(FEATURE_NAMESPACE_PREFIXES, Boolean.TRUE);
 93    }
 94   
 95  0 public SAXWriter(ContentHandler contentHandler) {
 96  0 this();
 97  0 this.contentHandler = contentHandler;
 98    }
 99   
 100  0 public SAXWriter(ContentHandler contentHandler,
 101    LexicalHandler lexicalHandler) {
 102  0 this();
 103  0 this.contentHandler = contentHandler;
 104  0 this.lexicalHandler = lexicalHandler;
 105    }
 106   
 107  12 public SAXWriter(ContentHandler contentHandler,
 108    LexicalHandler lexicalHandler, EntityResolver entityResolver) {
 109  12 this();
 110  12 this.contentHandler = contentHandler;
 111  12 this.lexicalHandler = lexicalHandler;
 112  12 this.entityResolver = entityResolver;
 113    }
 114   
 115    /**
 116    * A polymorphic method to write any Node to this SAX stream
 117    *
 118    * @param node
 119    * DOCUMENT ME!
 120    *
 121    * @throws SAXException
 122    * DOCUMENT ME!
 123    */
 124  39 public void write(Node node) throws SAXException {
 125  39 int nodeType = node.getNodeType();
 126   
 127  39 switch (nodeType) {
 128  0 case Node.ELEMENT_NODE:
 129  0 write((Element) node);
 130   
 131  0 break;
 132   
 133  0 case Node.ATTRIBUTE_NODE:
 134  0 write((Attribute) node);
 135   
 136  0 break;
 137   
 138  0 case Node.TEXT_NODE:
 139  0 write(node.getText());
 140   
 141  0 break;
 142   
 143  0 case Node.CDATA_SECTION_NODE:
 144  0 write((CDATA) node);
 145   
 146  0 break;
 147   
 148  0 case Node.ENTITY_REFERENCE_NODE:
 149  0 write((Entity) node);
 150   
 151  0 break;
 152   
 153  0 case Node.PROCESSING_INSTRUCTION_NODE:
 154  0 write((ProcessingInstruction) node);
 155   
 156  0 break;
 157   
 158  0 case Node.COMMENT_NODE:
 159  0 write((Comment) node);
 160   
 161  0 break;
 162   
 163  0 case Node.DOCUMENT_NODE:
 164  0 write((Document) node);
 165   
 166  0 break;
 167   
 168  0 case Node.DOCUMENT_TYPE_NODE:
 169  0 write((DocumentType) node);
 170   
 171  0 break;
 172   
 173  39 case Node.NAMESPACE_NODE:
 174   
 175    // Will be output with attributes
 176    // write((Namespace) node);
 177  39 break;
 178   
 179  0 default:
 180  0 throw new SAXException("Invalid node type: " + node);
 181    }
 182    }
 183   
 184    /**
 185    * Generates SAX events for the given Document and all its content
 186    *
 187    * @param document
 188    * is the Document to parse
 189    *
 190    * @throws SAXException
 191    * if there is a SAX error processing the events
 192    */
 193  20 public void write(Document document) throws SAXException {
 194  20 if (document != null) {
 195  20 checkForNullHandlers();
 196   
 197  20 documentLocator(document);
 198  20 startDocument();
 199  20 entityResolver(document);
 200  20 dtdHandler(document);
 201   
 202  20 writeContent(document, new NamespaceStack());
 203  20 endDocument();
 204    }
 205    }
 206   
 207    /**
 208    * Generates SAX events for the given Element and all its content
 209    *
 210    * @param element
 211    * is the Element to parse
 212    *
 213    * @throws SAXException
 214    * if there is a SAX error processing the events
 215    */
 216  0 public void write(Element element) throws SAXException {
 217  0 write(element, new NamespaceStack());
 218    }
 219   
 220    /**
 221    * <p>
 222    * Writes the opening tag of an {@link Element}, including its {@link
 223    * Attribute}s but without its content.
 224    * </p>
 225    *
 226    * @param element
 227    * <code>Element</code> to output.
 228    *
 229    * @throws SAXException
 230    * DOCUMENT ME!
 231    */
 232  0 public void writeOpen(Element element) throws SAXException {
 233  0 startElement(element, null);
 234    }
 235   
 236    /**
 237    * <p>
 238    * Writes the closing tag of an {@link Element}
 239    * </p>
 240    *
 241    * @param element
 242    * <code>Element</code> to output.
 243    *
 244    * @throws SAXException
 245    * DOCUMENT ME!
 246    */
 247  0 public void writeClose(Element element) throws SAXException {
 248  0 endElement(element);
 249    }
 250   
 251    /**
 252    * Generates SAX events for the given text
 253    *
 254    * @param text
 255    * is the text to send to the SAX ContentHandler
 256    *
 257    * @throws SAXException
 258    * if there is a SAX error processing the events
 259    */
 260  12162 public void write(String text) throws SAXException {
 261  12162 if (text != null) {
 262  12162 char[] chars = text.toCharArray();
 263  12162 contentHandler.characters(chars, 0, chars.length);
 264    }
 265    }
 266   
 267    /**
 268    * Generates SAX events for the given CDATA
 269    *
 270    * @param cdata
 271    * is the CDATA to parse
 272    *
 273    * @throws SAXException
 274    * if there is a SAX error processing the events
 275    */
 276  30 public void write(CDATA cdata) throws SAXException {
 277  30 String text = cdata.getText();
 278   
 279  30 if (lexicalHandler != null) {
 280  30 lexicalHandler.startCDATA();
 281  30 write(text);
 282  30 lexicalHandler.endCDATA();
 283    } else {
 284  0 write(text);
 285    }
 286    }
 287   
 288    /**
 289    * Generates SAX events for the given Comment
 290    *
 291    * @param comment
 292    * is the Comment to parse
 293    *
 294    * @throws SAXException
 295    * if there is a SAX error processing the events
 296    */
 297  71 public void write(Comment comment) throws SAXException {
 298  71 if (lexicalHandler != null) {
 299  71 String text = comment.getText();
 300  71 char[] chars = text.toCharArray();
 301  71 lexicalHandler.comment(chars, 0, chars.length);
 302    }
 303    }
 304   
 305    /**
 306    * Generates SAX events for the given Entity
 307    *
 308    * @param entity
 309    * is the Entity to parse
 310    *
 311    * @throws SAXException
 312    * if there is a SAX error processing the events
 313    */
 314  0 public void write(Entity entity) throws SAXException {
 315  0 String text = entity.getText();
 316   
 317  0 if (lexicalHandler != null) {
 318  0 String name = entity.getName();
 319  0 lexicalHandler.startEntity(name);
 320  0 write(text);
 321  0 lexicalHandler.endEntity(name);
 322    } else {
 323  0 write(text);
 324    }
 325    }
 326   
 327    /**
 328    * Generates SAX events for the given ProcessingInstruction
 329    *
 330    * @param pi
 331    * is the ProcessingInstruction to parse
 332    *
 333    * @throws SAXException
 334    * if there is a SAX error processing the events
 335    */
 336  2 public void write(ProcessingInstruction pi) throws SAXException {
 337  2 String target = pi.getTarget();
 338  2 String text = pi.getText();
 339  2 contentHandler.processingInstruction(target, text);
 340    }
 341   
 342    /**
 343    * Should namespace declarations be converted to "xmlns" attributes. This
 344    * property defaults to <code>false</code> as per the SAX specification.
 345    * This property is set via the SAX feature
 346    * "http://xml.org/sax/features/namespace-prefixes"
 347    *
 348    * @return DOCUMENT ME!
 349    */
 350  0 public boolean isDeclareNamespaceAttributes() {
 351  0 return declareNamespaceAttributes;
 352    }
 353   
 354    /**
 355    * Sets whether namespace declarations should be exported as "xmlns"
 356    * attributes or not. This property is set from the SAX feature
 357    * "http://xml.org/sax/features/namespace-prefixes"
 358    *
 359    * @param declareNamespaceAttrs
 360    * DOCUMENT ME!
 361    */
 362  8 public void setDeclareNamespaceAttributes(boolean declareNamespaceAttrs) {
 363  8 this.declareNamespaceAttributes = declareNamespaceAttrs;
 364    }
 365   
 366    // XMLReader methods
 367    // -------------------------------------------------------------------------
 368   
 369    /**
 370    * DOCUMENT ME!
 371    *
 372    * @return the <code>ContentHandler</code> called when SAX events are
 373    * raised
 374    */
 375  0 public ContentHandler getContentHandler() {
 376  0 return contentHandler;
 377    }
 378   
 379    /**
 380    * Sets the <code>ContentHandler</code> called when SAX events are raised
 381    *
 382    * @param contentHandler
 383    * is the <code>ContentHandler</code> called when SAX events
 384    * are raised
 385    */
 386  8 public void setContentHandler(ContentHandler contentHandler) {
 387  8 this.contentHandler = contentHandler;
 388    }
 389   
 390    /**
 391    * DOCUMENT ME!
 392    *
 393    * @return the <code>DTDHandler</code>
 394    */
 395  0 public DTDHandler getDTDHandler() {
 396  0 return dtdHandler;
 397    }
 398   
 399    /**
 400    * Sets the <code>DTDHandler</code>.
 401    *
 402    * @param handler
 403    * DOCUMENT ME!
 404    */
 405  8 public void setDTDHandler(DTDHandler handler) {
 406  8 this.dtdHandler = handler;
 407    }
 408   
 409    /**
 410    * DOCUMENT ME!
 411    *
 412    * @return the <code>ErrorHandler</code>
 413    */
 414  1 public ErrorHandler getErrorHandler() {
 415  1 return errorHandler;
 416    }
 417   
 418    /**
 419    * Sets the <code>ErrorHandler</code>.
 420    *
 421    * @param errorHandler
 422    * DOCUMENT ME!
 423    */
 424  1 public void setErrorHandler(ErrorHandler errorHandler) {
 425  1 this.errorHandler = errorHandler;
 426    }
 427   
 428    /**
 429    * DOCUMENT ME!
 430    *
 431    * @return the <code>EntityResolver</code> used when a Document contains a
 432    * DTD
 433    */
 434  0 public EntityResolver getEntityResolver() {
 435  0 return entityResolver;
 436    }
 437   
 438    /**
 439    * Sets the <code>EntityResolver</code>.
 440    *
 441    * @param entityResolver
 442    * is the <code>EntityResolver</code>
 443    */
 444  0 public void setEntityResolver(EntityResolver entityResolver) {
 445  0 this.entityResolver = entityResolver;
 446    }
 447   
 448    /**
 449    * DOCUMENT ME!
 450    *
 451    * @return the <code>LexicalHandler</code> used when a Document contains a
 452    * DTD
 453    */
 454  0 public LexicalHandler getLexicalHandler() {
 455  0 return lexicalHandler;
 456    }
 457   
 458    /**
 459    * Sets the <code>LexicalHandler</code>.
 460    *
 461    * @param lexicalHandler
 462    * is the <code>LexicalHandler</code>
 463    */
 464  15 public void setLexicalHandler(LexicalHandler lexicalHandler) {
 465  15 this.lexicalHandler = lexicalHandler;
 466    }
 467   
 468    /**
 469    * Sets the <code>XMLReader</code> used to write SAX events to
 470    *
 471    * @param xmlReader
 472    * is the <code>XMLReader</code>
 473    */
 474  0 public void setXMLReader(XMLReader xmlReader) {
 475  0 setContentHandler(xmlReader.getContentHandler());
 476  0 setDTDHandler(xmlReader.getDTDHandler());
 477  0 setEntityResolver(xmlReader.getEntityResolver());
 478  0 setErrorHandler(xmlReader.getErrorHandler());
 479    }
 480   
 481    /**
 482    * Looks up the value of a feature.
 483    *
 484    * @param name
 485    * DOCUMENT ME!
 486    *
 487    * @return DOCUMENT ME!
 488    *
 489    * @throws SAXNotRecognizedException
 490    * DOCUMENT ME!
 491    * @throws SAXNotSupportedException
 492    * DOCUMENT ME!
 493    */
 494