001 /* 002 * TextConsumer.java 003 * Copyright (C) 1999,2000,2001 The Free Software Foundation 004 * Portions Copyright 2005 Marko Karppinen & Co. LLC 005 * 006 * This file is part of GNU JAXP, a library. 007 * This version has been modified from the original GNU JAXP distribution 008 * on 2005-02-11 by Henri Sivonen working as an employee of Marko 009 * Karppinen & Co. LLC. 010 * 011 * GNU JAXP is free software; you can redistribute it and/or modify 012 * it under the terms of the GNU General Public License as published by 013 * the Free Software Foundation; either version 2 of the License, or 014 * (at your option) any later version. 015 * 016 * GNU JAXP is distributed in the hope that it will be useful, 017 * but WITHOUT ANY WARRANTY; without even the implied warranty of 018 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 019 * GNU General Public License for more details. 020 * 021 * You should have received a copy of the GNU General Public License 022 * along with this program; if not, write to the Free Software 023 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 024 * 025 * Linking this library statically or dynamically with other modules is 026 * making a combined work based on this library. Thus, the terms and 027 * conditions of the GNU General Public License cover the whole 028 * combination. 029 * 030 * As a special exception, the copyright holders of this library give you 031 * permission to link this library with independent modules to produce an 032 * executable, regardless of the license terms of these independent 033 * modules, and to copy and distribute the resulting executable under 034 * terms of your choice, provided that you also meet, for each linked 035 * independent module, the terms and conditions of the license of that 036 * module. An independent module is a module which is not derived from 037 * or based on this library. If you modify this library, you may extend 038 * this exception to your version of the library, but you are not 039 * obliged to do so. If you do not wish to do so, delete this 040 * exception statement from your version. 041 */ 042 043 // Package renamed -- 2005-02-11 hsivonen 044 package fi.karppinen.gnu.xml.pipeline; 045 046 import fi.karppinen.gnu.xml.util.XMLWriter; 047 import gnu.xml.pipeline.EventConsumer; 048 import gnu.xml.pipeline.EventFilter; 049 import gnu.xml.pipeline.NSFilter; 050 import gnu.xml.pipeline.WellFormednessFilter; 051 052 import java.io.IOException; 053 import java.io.OutputStream; 054 import java.io.OutputStreamWriter; 055 import java.io.Writer; 056 057 import org.xml.sax.ContentHandler; 058 import org.xml.sax.DTDHandler; 059 import org.xml.sax.SAXNotRecognizedException; 060 061 /** 062 * Terminates a pipeline, consuming events to print them as well formed XML (or 063 * XHTML) text. 064 * 065 * <p> 066 * Input must be well formed, and must include XML names (e.g. the prefixes and 067 * prefix declarations must be present), or the output of this class is 068 * undefined. 069 * 070 * @see NSFilter 071 * @see WellFormednessFilter 072 * 073 * @author David Brownell 074 * @author Henri Sivonen 075 */ 076 public class TextConsumer extends XMLWriter implements EventConsumer { 077 // edited doc -- 2005-02-11 hsivonen 078 /** 079 * Constructs an event consumer which echoes its input as text, optionally 080 * adhering to some basic XHTML formatting options which increase 081 * interoperability with old (v3) browsers. 082 */ 083 public TextConsumer(Writer w, boolean isXhtml) throws IOException { 084 // Removed encoding-related code -- 2005-02-11 hsivonen 085 super(w); 086 setXhtml(isXhtml); 087 } 088 089 /** 090 * Constructs a consumer that writes its input as XML text. XHTML rules are 091 * not followed. 092 */ 093 public TextConsumer(Writer w) throws IOException { 094 this(w, false); 095 } 096 097 /** 098 * Constructs a consumer that writes its input as XML text, encoded in 099 * UTF-8. XHTML rules are not followed. 100 */ 101 public TextConsumer(OutputStream out) throws IOException { 102 this(new OutputStreamWriter(out, "UTF8"), false); 103 } 104 105 /** <b>EventConsumer </b> Returns the document handler being used. */ 106 public ContentHandler getContentHandler() { 107 return this; 108 } 109 110 /** <b>EventConsumer </b> Returns the dtd handler being used. */ 111 public DTDHandler getDTDHandler() { 112 return this; 113 } 114 115 /** <b>XMLReader </b>Retrieves a property (lexical and decl handlers) */ 116 public Object getProperty(String propertyId) 117 throws SAXNotRecognizedException { 118 if (EventFilter.LEXICAL_HANDLER.equals(propertyId)) 119 return this; 120 if (EventFilter.DECL_HANDLER.equals(propertyId)) 121 return this; 122 throw new SAXNotRecognizedException(propertyId); 123 } 124 }