1 /*
2  * Copyright (c) 1997, 2012, 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.ws.wsdl.parser;
27 
28 import com.sun.xml.internal.bind.WhiteSpaceProcessor;
29 import org.xml.sax.*;
30 import org.xml.sax.helpers.XMLFilterImpl;
31 
32 /**
33  * Strips ignorable whitespace from SAX event stream.
34  *
35  * <p>
36  * This filter works only when the event stream doesn't
37  * contain any mixed content.
38  *
39  * @author
40  *     Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
41  *     Vivek Pandey
42  */
43 class WhitespaceStripper extends XMLFilterImpl {
44 
45     private int state = 0;
46 
47     private char[] buf = new char[1024];
48     private int bufLen = 0;
49 
50     private static final int AFTER_START_ELEMENT = 1;
51     private static final int AFTER_END_ELEMENT = 2;
52 
WhitespaceStripper(XMLReader reader)53     public WhitespaceStripper(XMLReader reader) {
54         setParent(reader);
55     }
56 
WhitespaceStripper(ContentHandler handler, ErrorHandler eh, EntityResolver er)57     public WhitespaceStripper(ContentHandler handler, ErrorHandler eh, EntityResolver er) {
58         setContentHandler(handler);
59         if(eh!=null)    setErrorHandler(eh);
60         if(er!=null)    setEntityResolver(er);
61     }
62 
characters(char[] ch, int start, int length)63     public void characters(char[] ch, int start, int length) throws SAXException {
64         switch(state) {
65         case AFTER_START_ELEMENT:
66             // we have to store the characters here, even if it consists entirely
67             // of whitespaces. This is because successive characters event might
68             // include non-whitespace char, in which case all the whitespaces in
69             // this event may suddenly become significant.
70             if( bufLen+length>buf.length ) {
71                 // reallocate buffer
72                 char[] newBuf = new char[Math.max(bufLen+length,buf.length*2)];
73                 System.arraycopy(buf,0,newBuf,0,bufLen);
74                 buf = newBuf;
75             }
76             System.arraycopy(ch,start,buf,bufLen,length);
77             bufLen += length;
78             break;
79         case AFTER_END_ELEMENT:
80             // check if this is ignorable.
81             int len = start+length;
82             for( int i=start; i<len; i++ )
83                 if( !WhiteSpaceProcessor.isWhiteSpace(ch[i]) ) {
84                     super.characters(ch, start, length);
85                     return;
86                 }
87             // if it's entirely whitespace, ignore it.
88             break;
89         }
90     }
91 
startElement(String uri, String localName, String qName, Attributes atts)92     public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
93         processPendingText();
94         super.startElement(uri, localName, qName, atts);
95         state = AFTER_START_ELEMENT;
96         bufLen = 0;
97     }
98 
endElement(String uri, String localName, String qName)99     public void endElement(String uri, String localName, String qName) throws SAXException {
100         processPendingText();
101         super.endElement(uri, localName, qName);
102         state = AFTER_END_ELEMENT;
103     }
104 
105     /**
106      * Forwars the buffered characters if it contains any non-whitespace
107      * character.
108      */
processPendingText()109     private void processPendingText() throws SAXException {
110         if(state==AFTER_START_ELEMENT) {
111             for( int i=bufLen-1; i>=0; i-- )
112                 if( !WhiteSpaceProcessor.isWhiteSpace(buf[i]) ) {
113                     super.characters(buf, 0, bufLen);
114                     return;
115                }
116         }
117     }
118 
ignorableWhitespace(char[] ch, int start, int length)119     public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
120         // ignore completely.
121     }
122 }
123