View Javadoc
1   /*
2    * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
3    * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4    *
5    * This code is free software; you can redistribute it and/or modify it
6    * under the terms of the GNU General Public License version 2 only, as
7    * published by the Free Software Foundation.  Oracle designates this
8    * particular file as subject to the "Classpath" exception as provided
9    * by Oracle in the LICENSE file that accompanied this code.
10   *
11   * This code is distributed in the hope that it will be useful, but WITHOUT
12   * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13   * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
14   * version 2 for more details (a copy is included in the LICENSE file that
15   * accompanied this code).
16   *
17   * You should have received a copy of the GNU General Public License version
18   * 2 along with this work; if not, write to the Free Software Foundation,
19   * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
20   *
21   * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
22   * or visit www.oracle.com if you need additional information or have any
23   * questions.
24   */
25  
26  package com.sun.tools.internal.xjc.reader.internalizer;
27  
28  import com.sun.xml.internal.bind.WhiteSpaceProcessor;
29  
30  import org.xml.sax.Attributes;
31  import org.xml.sax.ContentHandler;
32  import org.xml.sax.EntityResolver;
33  import org.xml.sax.ErrorHandler;
34  import org.xml.sax.SAXException;
35  import org.xml.sax.XMLReader;
36  import org.xml.sax.helpers.XMLFilterImpl;
37  
38  /**
39   * Strips ignorable whitespace from SAX event stream.
40   *
41   * <p>
42   * This filter works only when the event stream doesn't
43   * contain any mixed content.
44   *
45   * @author
46   *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
47   */
48  class WhitespaceStripper extends XMLFilterImpl {
49  
50      private int state = 0;
51  
52      private char[] buf = new char[1024];
53      private int bufLen = 0;
54  
55      private static final int AFTER_START_ELEMENT = 1;
56      private static final int AFTER_END_ELEMENT = 2;
57  
58      public WhitespaceStripper(XMLReader reader) {
59          setParent(reader);
60      }
61  
62      public WhitespaceStripper(ContentHandler handler,ErrorHandler eh,EntityResolver er) {
63          setContentHandler(handler);
64          if(eh!=null)    setErrorHandler(eh);
65          if(er!=null)    setEntityResolver(er);
66      }
67  
68      public void characters(char[] ch, int start, int length) throws SAXException {
69          switch(state) {
70          case AFTER_START_ELEMENT:
71              // we have to store the characters here, even if it consists entirely
72              // of whitespaces. This is because successive characters event might
73              // include non-whitespace char, in which case all the whitespaces in
74              // this event may suddenly become significant.
75              if( bufLen+length>buf.length ) {
76                  // reallocate buffer
77                  char[] newBuf = new char[Math.max(bufLen+length,buf.length*2)];
78                  System.arraycopy(buf,0,newBuf,0,bufLen);
79                  buf = newBuf;
80              }
81              System.arraycopy(ch,start,buf,bufLen,length);
82              bufLen += length;
83              break;
84          case AFTER_END_ELEMENT:
85              // check if this is ignorable.
86              int len = start+length;
87              for( int i=start; i<len; i++ )
88                  if( !WhiteSpaceProcessor.isWhiteSpace(ch[i]) ) {
89                      super.characters(ch, start, length);
90                      return;
91                  }
92              // if it's entirely whitespace, ignore it.
93              break;
94          }
95      }
96  
97      public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
98          processPendingText();
99          super.startElement(uri, localName, qName, atts);
100         state = AFTER_START_ELEMENT;
101         bufLen = 0;
102     }
103 
104     public void endElement(String uri, String localName, String qName) throws SAXException {
105         processPendingText();
106         super.endElement(uri, localName, qName);
107         state = AFTER_END_ELEMENT;
108     }
109 
110     /**
111      * Forwars the buffered characters if it contains any non-whitespace
112      * character.
113      */
114     private void processPendingText() throws SAXException {
115         if(state==AFTER_START_ELEMENT) {
116             for( int i=bufLen-1; i>=0; i-- )
117                 if( !WhiteSpaceProcessor.isWhiteSpace(buf[i]) ) {
118                     super.characters(buf, 0, bufLen);
119                     return;
120                }
121         }
122     }
123 
124     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
125         // ignore completely.
126     }
127 }