View Javadoc

1   /*
2    * ModeShape (http://www.modeshape.org)
3    * See the COPYRIGHT.txt file distributed with this work for information
4    * regarding copyright ownership.  Some portions may be licensed
5    * to Red Hat, Inc. under one or more contributor license agreements.
6    * See the AUTHORS.txt file in the distribution for a full listing of 
7    * individual contributors. 
8    *
9    * ModeShape is free software. Unless otherwise indicated, all code in ModeShape
10   * is licensed to you under the terms of the GNU Lesser General Public License as
11   * published by the Free Software Foundation; either version 2.1 of
12   * the License, or (at your option) any later version.
13   *
14   * ModeShape is distributed in the hope that it will be useful,
15   * but WITHOUT ANY WARRANTY; without even the implied warranty of
16   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17   * Lesser General Public License for more details.
18   *
19   * You should have received a copy of the GNU Lesser General Public
20   * License along with this software; if not, write to the Free
21   * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
22   * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
23   */
24  package org.modeshape.common.xml;
25  
26  import java.io.IOException;
27  import java.io.OutputStream;
28  import java.util.Collection;
29  import java.util.Collections;
30  import java.util.HashMap;
31  import java.util.Map;
32  import org.modeshape.common.text.TextEncoder;
33  import org.modeshape.common.text.XmlValueEncoder;
34  import org.xml.sax.Attributes;
35  import org.xml.sax.ContentHandler;
36  import org.xml.sax.SAXException;
37  import org.xml.sax.helpers.DefaultHandler;
38  
39  /**
40   * Class that adapts an arbitrary, open {@link OutputStream} to the {@link ContentHandler} interface. SAX events invoked on this
41   * object will be translated into their corresponding XML text and written to the output stream.
42   */
43  public class StreamingContentHandler extends DefaultHandler {
44  
45      /** Debug setting that allows all output to be written to {@link System#out}. */
46      private static final boolean LOG_TO_CONSOLE = false;
47  
48      /**
49       * Encoder to properly escape XML attribute values
50       * 
51       * @see XmlValueEncoder
52       */
53      private final TextEncoder VALUE_ENCODER = new XmlValueEncoder();
54  
55      /**
56       * The list of XML namespaces that are predefined and should not be exported by the content handler.
57       */
58      private final Collection<String> unexportableNamespaces;
59  
60      /**
61       * The output stream to which the XML will be written
62       */
63      private final OutputStream os;
64  
65      /**
66       * The XML namespace prefixes that are currently mapped
67       */
68      private final Map<String, String> mappedPrefixes;
69  
70      public StreamingContentHandler( OutputStream os ) {
71          this(os, Collections.<String>emptyList());
72      }
73  
74      public StreamingContentHandler( OutputStream os,
75                                      Collection<String> unexportableNamespaces ) {
76          this.os = os;
77          this.unexportableNamespaces = unexportableNamespaces;
78          mappedPrefixes = new HashMap<String, String>();
79      }
80  
81      /**
82       * {@inheritDoc}
83       * 
84       * @see org.xml.sax.helpers.DefaultHandler#characters(char[], int, int)
85       */
86      @Override
87      public void characters( char[] ch,
88                              int start,
89                              int length ) throws SAXException {
90          emit(VALUE_ENCODER.encode(new String(ch, start, length)));
91      }
92  
93      /**
94       * {@inheritDoc}
95       * 
96       * @see org.xml.sax.helpers.DefaultHandler#startDocument()
97       */
98      @Override
99      public void startDocument() throws SAXException {
100         emit("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
101     }
102 
103     /**
104      * {@inheritDoc}
105      * 
106      * @see org.xml.sax.helpers.DefaultHandler#startElement(java.lang.String, java.lang.String, java.lang.String,
107      *      org.xml.sax.Attributes)
108      */
109     @Override
110     public void startElement( String uri,
111                               String localName,
112                               String name,
113                               Attributes attributes ) throws SAXException {
114         emit("<");
115         emit(name);
116 
117         for (Map.Entry<String, String> mapping : mappedPrefixes.entrySet()) {
118             emit(" xmlns:");
119             emit(mapping.getKey());
120             emit("=\"");
121             emit(mapping.getValue());
122             emit("\"");
123         }
124 
125         mappedPrefixes.clear();
126 
127         if (attributes != null) {
128             for (int i = 0; i < attributes.getLength(); i++) {
129                 emit(" ");
130                 emit(attributes.getQName(i));
131                 emit("=\"");
132                 emit(VALUE_ENCODER.encode(attributes.getValue(i)));
133                 emit("\"");
134             }
135         }
136 
137         emit(">");
138     }
139 
140     /**
141      * {@inheritDoc}
142      * 
143      * @see org.xml.sax.helpers.DefaultHandler#endElement(java.lang.String, java.lang.String, java.lang.String)
144      */
145     @Override
146     public void endElement( String uri,
147                             String localName,
148                             String name ) throws SAXException {
149         emit("</");
150         emit(name);
151         emit(">");
152         if (LOG_TO_CONSOLE) System.out.println();
153 
154     }
155 
156     /**
157      * {@inheritDoc}
158      * 
159      * @see org.xml.sax.helpers.DefaultHandler#startPrefixMapping(java.lang.String, java.lang.String)
160      */
161     @Override
162     public void startPrefixMapping( String prefix,
163                                     String uri ) {
164         if (!unexportableNamespaces.contains(prefix)) {
165             mappedPrefixes.put(prefix, uri);
166         }
167     }
168 
169     /**
170      * Writes the given text to the output stream for this {@link StreamingContentHandler}.
171      * 
172      * @param text the text to output
173      * @throws SAXException if there is an error writing to the stream
174      * @see StreamingContentHandler#os
175      */
176     private void emit( String text ) throws SAXException {
177 
178         try {
179             if (LOG_TO_CONSOLE) {
180                 System.out.print(text);
181             }
182 
183             os.write(text.getBytes());
184         } catch (IOException ioe) {
185             throw new SAXException(ioe);
186         }
187     }
188 }