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