001: /*
002: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
003: *
004: * Copyright 1997-2007 Sun Microsystems, Inc. All rights reserved.
005: *
006: * The contents of this file are subject to the terms of either the GNU
007: * General Public License Version 2 only ("GPL") or the Common Development
008: * and Distribution License("CDDL") (collectively, the "License"). You
009: * may not use this file except in compliance with the License. You can obtain
010: * a copy of the License at https://glassfish.dev.java.net/public/CDDL+GPL.html
011: * or glassfish/bootstrap/legal/LICENSE.txt. See the License for the specific
012: * language governing permissions and limitations under the License.
013: *
014: * When distributing the software, include this License Header Notice in each
015: * file and include the License file at glassfish/bootstrap/legal/LICENSE.txt.
016: * Sun designates this particular file as subject to the "Classpath" exception
017: * as provided by Sun in the GPL Version 2 section of the License file that
018: * accompanied this code. If applicable, add the following below the License
019: * Header, with the fields enclosed by brackets [] replaced by your own
020: * identifying information: "Portions Copyrighted [year]
021: * [name of copyright owner]"
022: *
023: * Contributor(s):
024: *
025: * If you wish your version of this file to be governed by only the CDDL or
026: * only the GPL Version 2, indicate your decision by adding "[Contributor]
027: * elects to include this software in this distribution under the [CDDL or GPL
028: * Version 2] license." If you don't indicate a single choice of license, a
029: * recipient has the option to distribute your version of this file under
030: * either the CDDL, the GPL Version 2 or to extend the choice of license to
031: * its licensees as provided above. However, if you add GPL Version 2 code
032: * and therefore, elected the GPL Version 2 license, then the option applies
033: * only if the new code is made subject to such option by the copyright
034: * holder.
035: */
036:
037: package com.sun.tools.ws.wsdl.parser;
038:
039: import com.sun.xml.bind.WhiteSpaceProcessor;
040: import org.xml.sax.*;
041: import org.xml.sax.helpers.XMLFilterImpl;
042:
043: /**
044: * Strips ignorable whitespace from SAX event stream.
045: *
046: * <p>
047: * This filter works only when the event stream doesn't
048: * contain any mixed content.
049: *
050: * @author
051: * Kohsuke Kawaguchi (kohsuke.kawaguchi@sun.com)
052: * Vivek Pandey
053: */
054: class WhitespaceStripper extends XMLFilterImpl {
055:
056: private int state = 0;
057:
058: private char[] buf = new char[1024];
059: private int bufLen = 0;
060:
061: private static final int AFTER_START_ELEMENT = 1;
062: private static final int AFTER_END_ELEMENT = 2;
063:
064: public WhitespaceStripper(XMLReader reader) {
065: setParent(reader);
066: }
067:
068: public WhitespaceStripper(ContentHandler handler, ErrorHandler eh,
069: EntityResolver er) {
070: setContentHandler(handler);
071: if (eh != null)
072: setErrorHandler(eh);
073: if (er != null)
074: setEntityResolver(er);
075: }
076:
077: public void characters(char[] ch, int start, int length)
078: throws SAXException {
079: switch (state) {
080: case AFTER_START_ELEMENT:
081: // we have to store the characters here, even if it consists entirely
082: // of whitespaces. This is because successive characters event might
083: // include non-whitespace char, in which case all the whitespaces in
084: // this event may suddenly become significant.
085: if (bufLen + length > buf.length) {
086: // reallocate buffer
087: char[] newBuf = new char[Math.max(bufLen + length,
088: buf.length * 2)];
089: System.arraycopy(buf, 0, newBuf, 0, bufLen);
090: buf = newBuf;
091: }
092: System.arraycopy(ch, start, buf, bufLen, length);
093: bufLen += length;
094: break;
095: case AFTER_END_ELEMENT:
096: // check if this is ignorable.
097: int len = start + length;
098: for (int i = start; i < len; i++)
099: if (!WhiteSpaceProcessor.isWhiteSpace(ch[i])) {
100: super .characters(ch, start, length);
101: return;
102: }
103: // if it's entirely whitespace, ignore it.
104: break;
105: }
106: }
107:
108: public void startElement(String uri, String localName,
109: String qName, Attributes atts) throws SAXException {
110: processPendingText();
111: super .startElement(uri, localName, qName, atts);
112: state = AFTER_START_ELEMENT;
113: bufLen = 0;
114: }
115:
116: public void endElement(String uri, String localName, String qName)
117: throws SAXException {
118: processPendingText();
119: super .endElement(uri, localName, qName);
120: state = AFTER_END_ELEMENT;
121: }
122:
123: /**
124: * Forwars the buffered characters if it contains any non-whitespace
125: * character.
126: */
127: private void processPendingText() throws SAXException {
128: if (state == AFTER_START_ELEMENT) {
129: for (int i = bufLen - 1; i >= 0; i--)
130: if (!WhiteSpaceProcessor.isWhiteSpace(buf[i])) {
131: super .characters(buf, 0, bufLen);
132: return;
133: }
134: }
135: }
136:
137: public void ignorableWhitespace(char[] ch, int start, int length)
138: throws SAXException {
139: // ignore completely.
140: }
141: }
|