ohair@286: /* ohair@286: * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved. ohair@286: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. ohair@286: * ohair@286: * This code is free software; you can redistribute it and/or modify it ohair@286: * under the terms of the GNU General Public License version 2 only, as ohair@286: * published by the Free Software Foundation. Oracle designates this ohair@286: * particular file as subject to the "Classpath" exception as provided ohair@286: * by Oracle in the LICENSE file that accompanied this code. ohair@286: * ohair@286: * This code is distributed in the hope that it will be useful, but WITHOUT ohair@286: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or ohair@286: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License ohair@286: * version 2 for more details (a copy is included in the LICENSE file that ohair@286: * accompanied this code). ohair@286: * ohair@286: * You should have received a copy of the GNU General Public License version ohair@286: * 2 along with this work; if not, write to the Free Software Foundation, ohair@286: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. ohair@286: * ohair@286: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA ohair@286: * or visit www.oracle.com if you need additional information or have any ohair@286: * questions. ohair@286: */ ohair@286: ohair@286: package com.sun.tools.internal.ws.wsdl.parser; ohair@286: ohair@286: import com.sun.xml.internal.bind.WhiteSpaceProcessor; ohair@286: import org.xml.sax.*; ohair@286: import org.xml.sax.helpers.XMLFilterImpl; ohair@286: ohair@286: /** ohair@286: * Strips ignorable whitespace from SAX event stream. ohair@286: * ohair@286: *
ohair@286: * This filter works only when the event stream doesn't
ohair@286: * contain any mixed content.
ohair@286: *
ohair@286: * @author
ohair@286: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
ohair@286: * Vivek Pandey
ohair@286: */
ohair@286: class WhitespaceStripper extends XMLFilterImpl {
ohair@286:
ohair@286: private int state = 0;
ohair@286:
ohair@286: private char[] buf = new char[1024];
ohair@286: private int bufLen = 0;
ohair@286:
ohair@286: private static final int AFTER_START_ELEMENT = 1;
ohair@286: private static final int AFTER_END_ELEMENT = 2;
ohair@286:
ohair@286: public WhitespaceStripper(XMLReader reader) {
ohair@286: setParent(reader);
ohair@286: }
ohair@286:
ohair@286: public WhitespaceStripper(ContentHandler handler, ErrorHandler eh, EntityResolver er) {
ohair@286: setContentHandler(handler);
ohair@286: if(eh!=null) setErrorHandler(eh);
ohair@286: if(er!=null) setEntityResolver(er);
ohair@286: }
ohair@286:
ohair@286: public void characters(char[] ch, int start, int length) throws SAXException {
ohair@286: switch(state) {
ohair@286: case AFTER_START_ELEMENT:
ohair@286: // we have to store the characters here, even if it consists entirely
ohair@286: // of whitespaces. This is because successive characters event might
ohair@286: // include non-whitespace char, in which case all the whitespaces in
ohair@286: // this event may suddenly become significant.
ohair@286: if( bufLen+length>buf.length ) {
ohair@286: // reallocate buffer
ohair@286: char[] newBuf = new char[Math.max(bufLen+length,buf.length*2)];
ohair@286: System.arraycopy(buf,0,newBuf,0,bufLen);
ohair@286: buf = newBuf;
ohair@286: }
ohair@286: System.arraycopy(ch,start,buf,bufLen,length);
ohair@286: bufLen += length;
ohair@286: break;
ohair@286: case AFTER_END_ELEMENT:
ohair@286: // check if this is ignorable.
ohair@286: int len = start+length;
ohair@286: for( int i=start; i