001/* 002 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 003 * 004 * This software is open source. 005 * See the bottom of this file for the licence. 006 * 007 * $Id: DocumentSource.java,v 1.3 2001/08/08 13:43:24 jstrachan Exp $ 008 */ 009 010package org.dom4j.io; 011 012import javax.xml.transform.Source; 013import javax.xml.transform.sax.SAXSource; 014import javax.xml.transform.TransformerFactory; 015 016import org.dom4j.Document; 017import org.dom4j.Node; 018 019import org.xml.sax.InputSource; 020import org.xml.sax.XMLFilter; 021import org.xml.sax.XMLReader; 022 023/** <p><code>DocumentSource</code> implements a JAXP {@link Source} 024 * for a {@link Document}.</p> 025 * 026 * @author <a href="mailto:james.strachan@metastuff.com">James Strachan</a> 027 * @version $Revision: 1.3 $ 028 */ 029public class DocumentSource extends SAXSource { 030 031 /** If {@link javax.xml.transform.TransformerFactory#getFeature} 032 * returns <code>true</code> when passed this value as an argument 033 * then the Transformer natively supports <i>dom4j</i>. 034 */ 035 public final static String DOM4J_FEATURE = "http://org.dom4j.io.DoucmentSource/feature"; 036 037 /** The XMLReader to use */ 038 private XMLReader xmlReader = new SAXWriter(); 039 040 041 /** Creates a JAXP {@link Source} for the given 042 * {@link Node}. 043 */ 044 public DocumentSource(Node node) { 045 setDocument(node.getDocument()); 046 } 047 048 /** Creates a JAXP {@link Source} for the given 049 * {@link Document}. 050 */ 051 public DocumentSource(Document document) { 052 setDocument(document); 053 } 054 055 056 // Properties 057 //------------------------------------------------------------------------- 058 059 /** @return the document which is being used as the JAXP {@link Source} 060 */ 061 public Document getDocument() { 062 DocumentInputSource documentInputSource 063 = (DocumentInputSource) getInputSource(); 064 return documentInputSource.getDocument(); 065 } 066 067 /** Sets the document used as the JAXP {@link Source} 068 */ 069 public void setDocument(Document document) { 070 super.setInputSource( new DocumentInputSource(document) ); 071 } 072 073 074 // Overloaded methods 075 //------------------------------------------------------------------------- 076 077 /** @return the XMLReader to be used for the JAXP {@link Source}. 078 */ 079 public XMLReader getXMLReader() { 080 return xmlReader; 081 } 082 083 /** This method is not supported as this source is always a 084 * {@link Document} instance. 085 * 086 * @throws UnsupportedOperationException as this method is unsupported 087 */ 088 public void setInputSource(InputSource inputSource) 089 throws UnsupportedOperationException { 090 if ( inputSource instanceof DocumentInputSource ) { 091 super.setInputSource( (DocumentInputSource) inputSource ); 092 } 093 else { 094 throw new UnsupportedOperationException(); 095 } 096 } 097 098 /** Sets the XMLReader used for the JAXP {@link Source}. 099 */ 100 public void setXMLReader(XMLReader reader) 101 throws UnsupportedOperationException { 102 if (reader instanceof SAXWriter) { 103 this.xmlReader = (SAXWriter) reader; 104 } 105 else if (reader instanceof XMLFilter) { 106 XMLFilter filter = (XMLFilter) reader; 107 while (true) { 108 XMLReader parent = filter.getParent(); 109 if ( parent instanceof XMLFilter ) { 110 filter = (XMLFilter) parent; 111 } 112 else { 113 break; 114 } 115 } 116 // install filter in SAXWriter.... 117 filter.setParent(xmlReader); 118 xmlReader = filter; 119 } 120 else { 121 throw new UnsupportedOperationException(); 122 } 123 } 124 125} 126 127 128 129 130 131 132 133/* 134 * Redistribution and use of this software and associated documentation 135 * ("Software"), with or without modification, are permitted provided 136 * that the following conditions are met: 137 * 138 * 1. Redistributions of source code must retain copyright 139 * statements and notices. Redistributions must also contain a 140 * copy of this document. 141 * 142 * 2. Redistributions in binary form must reproduce the 143 * above copyright notice, this list of conditions and the 144 * following disclaimer in the documentation and/or other 145 * materials provided with the distribution. 146 * 147 * 3. The name "DOM4J" must not be used to endorse or promote 148 * products derived from this Software without prior written 149 * permission of MetaStuff, Ltd. For written permission, 150 * please contact dom4j-info@metastuff.com. 151 * 152 * 4. Products derived from this Software may not be called "DOM4J" 153 * nor may "DOM4J" appear in their names without prior written 154 * permission of MetaStuff, Ltd. DOM4J is a registered 155 * trademark of MetaStuff, Ltd. 156 * 157 * 5. Due credit should be given to the DOM4J Project 158 * (http://dom4j.org/). 159 * 160 * THIS SOFTWARE IS PROVIDED BY METASTUFF, LTD. AND CONTRIBUTORS 161 * ``AS IS'' AND ANY EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT 162 * NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND 163 * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL 164 * METASTUFF, LTD. OR ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 165 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 166 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 167 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 168 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 169 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 170 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 171 * OF THE POSSIBILITY OF SUCH DAMAGE. 172 * 173 * Copyright 2001 (C) MetaStuff, Ltd. All Rights Reserved. 174 * 175 * $Id: DocumentSource.java,v 1.3 2001/08/08 13:43:24 jstrachan Exp $ 176 */