1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/ws/wsdl/parser/WhitespaceStripper.java Wed Apr 27 01:27:09 2016 +0800 1.3 @@ -0,0 +1,122 @@ 1.4 +/* 1.5 + * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 1.7 + * 1.8 + * This code is free software; you can redistribute it and/or modify it 1.9 + * under the terms of the GNU General Public License version 2 only, as 1.10 + * published by the Free Software Foundation. Oracle designates this 1.11 + * particular file as subject to the "Classpath" exception as provided 1.12 + * by Oracle in the LICENSE file that accompanied this code. 1.13 + * 1.14 + * This code is distributed in the hope that it will be useful, but WITHOUT 1.15 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 1.16 + * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 1.17 + * version 2 for more details (a copy is included in the LICENSE file that 1.18 + * accompanied this code). 1.19 + * 1.20 + * You should have received a copy of the GNU General Public License version 1.21 + * 2 along with this work; if not, write to the Free Software Foundation, 1.22 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 1.23 + * 1.24 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 1.25 + * or visit www.oracle.com if you need additional information or have any 1.26 + * questions. 1.27 + */ 1.28 + 1.29 +package com.sun.tools.internal.ws.wsdl.parser; 1.30 + 1.31 +import com.sun.xml.internal.bind.WhiteSpaceProcessor; 1.32 +import org.xml.sax.*; 1.33 +import org.xml.sax.helpers.XMLFilterImpl; 1.34 + 1.35 +/** 1.36 + * Strips ignorable whitespace from SAX event stream. 1.37 + * 1.38 + * <p> 1.39 + * This filter works only when the event stream doesn't 1.40 + * contain any mixed content. 1.41 + * 1.42 + * @author 1.43 + * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com) 1.44 + * Vivek Pandey 1.45 + */ 1.46 +class WhitespaceStripper extends XMLFilterImpl { 1.47 + 1.48 + private int state = 0; 1.49 + 1.50 + private char[] buf = new char[1024]; 1.51 + private int bufLen = 0; 1.52 + 1.53 + private static final int AFTER_START_ELEMENT = 1; 1.54 + private static final int AFTER_END_ELEMENT = 2; 1.55 + 1.56 + public WhitespaceStripper(XMLReader reader) { 1.57 + setParent(reader); 1.58 + } 1.59 + 1.60 + public WhitespaceStripper(ContentHandler handler, ErrorHandler eh, EntityResolver er) { 1.61 + setContentHandler(handler); 1.62 + if(eh!=null) setErrorHandler(eh); 1.63 + if(er!=null) setEntityResolver(er); 1.64 + } 1.65 + 1.66 + public void characters(char[] ch, int start, int length) throws SAXException { 1.67 + switch(state) { 1.68 + case AFTER_START_ELEMENT: 1.69 + // we have to store the characters here, even if it consists entirely 1.70 + // of whitespaces. This is because successive characters event might 1.71 + // include non-whitespace char, in which case all the whitespaces in 1.72 + // this event may suddenly become significant. 1.73 + if( bufLen+length>buf.length ) { 1.74 + // reallocate buffer 1.75 + char[] newBuf = new char[Math.max(bufLen+length,buf.length*2)]; 1.76 + System.arraycopy(buf,0,newBuf,0,bufLen); 1.77 + buf = newBuf; 1.78 + } 1.79 + System.arraycopy(ch,start,buf,bufLen,length); 1.80 + bufLen += length; 1.81 + break; 1.82 + case AFTER_END_ELEMENT: 1.83 + // check if this is ignorable. 1.84 + int len = start+length; 1.85 + for( int i=start; i<len; i++ ) 1.86 + if( !WhiteSpaceProcessor.isWhiteSpace(ch[i]) ) { 1.87 + super.characters(ch, start, length); 1.88 + return; 1.89 + } 1.90 + // if it's entirely whitespace, ignore it. 1.91 + break; 1.92 + } 1.93 + } 1.94 + 1.95 + public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException { 1.96 + processPendingText(); 1.97 + super.startElement(uri, localName, qName, atts); 1.98 + state = AFTER_START_ELEMENT; 1.99 + bufLen = 0; 1.100 + } 1.101 + 1.102 + public void endElement(String uri, String localName, String qName) throws SAXException { 1.103 + processPendingText(); 1.104 + super.endElement(uri, localName, qName); 1.105 + state = AFTER_END_ELEMENT; 1.106 + } 1.107 + 1.108 + /** 1.109 + * Forwars the buffered characters if it contains any non-whitespace 1.110 + * character. 1.111 + */ 1.112 + private void processPendingText() throws SAXException { 1.113 + if(state==AFTER_START_ELEMENT) { 1.114 + for( int i=bufLen-1; i>=0; i-- ) 1.115 + if( !WhiteSpaceProcessor.isWhiteSpace(buf[i]) ) { 1.116 + super.characters(buf, 0, bufLen); 1.117 + return; 1.118 + } 1.119 + } 1.120 + } 1.121 + 1.122 + public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException { 1.123 + // ignore completely. 1.124 + } 1.125 +}