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.xjc.util;
038:
039: import org.xml.sax.helpers.XMLFilterImpl;
040: import org.xml.sax.helpers.DefaultHandler;
041: import org.xml.sax.XMLFilter;
042: import org.xml.sax.ContentHandler;
043: import org.xml.sax.SAXException;
044: import org.xml.sax.Attributes;
045:
046: /**
047: * {@link XMLFilter} that can cut sub-trees.
048: *
049: * @author Kohsuke Kawaguchi
050: */
051: public abstract class SubtreeCutter extends XMLFilterImpl {
052: /**
053: * When we are pruning a sub tree, this field holds the depth of
054: * elements that are being cut. Used to resume event forwarding.
055: *
056: * As long as this value is 0, we will pass through data.
057: */
058: private int cutDepth = 0;
059:
060: /**
061: * This object will receive SAX events while a sub tree is being
062: * pruned.
063: */
064: private static final ContentHandler stub = new DefaultHandler();
065:
066: /**
067: * This field remembers the user-specified ContentHandler.
068: * So that we can restore it once the sub tree is completely pruned.
069: */
070: private ContentHandler next;
071:
072: public void startDocument() throws SAXException {
073: cutDepth = 0;
074: super .startDocument();
075: }
076:
077: public boolean isCutting() {
078: return cutDepth > 0;
079: }
080:
081: /**
082: * Starts cutting a sub-tree. Should be called from within the
083: * {@link #startElement(String, String, String, Attributes)} implementation
084: * before the execution is passed to {@link SubtreeCutter#startElement(String, String, String, Attributes)} .
085: * The current element will be cut.
086: */
087: public void startCutting() {
088: super .setContentHandler(stub);
089: cutDepth = 1;
090: }
091:
092: public void setContentHandler(ContentHandler handler) {
093: next = handler;
094: // changes take effect immediately unless the sub-tree is being pruned
095: if (getContentHandler() != stub)
096: super .setContentHandler(handler);
097: }
098:
099: public void startElement(String uri, String localName,
100: String qName, Attributes atts) throws SAXException {
101: if (cutDepth > 0)
102: cutDepth++;
103: super .startElement(uri, localName, qName, atts);
104: }
105:
106: public void endElement(String namespaceURI, String localName,
107: String qName) throws SAXException {
108: super .endElement(namespaceURI, localName, qName);
109:
110: if (cutDepth != 0) {
111: cutDepth--;
112: if (cutDepth == 1) {
113: // pruning completed. restore the user handler
114: super .setContentHandler(next);
115: cutDepth = 0;
116: }
117: }
118: }
119: }
|