0N/A/*
2362N/A * Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
2362N/A * published by the Free Software Foundation. Oracle designates this
0N/A * particular file as subject to the "Classpath" exception as provided
2362N/A * by Oracle in the LICENSE file that accompanied this code.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
2362N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
2362N/A * or visit www.oracle.com if you need additional information or have any
2362N/A * questions.
0N/A */
0N/A
0N/Apackage com.sun.tools.internal.xjc.util;
0N/A
0N/Aimport org.xml.sax.helpers.XMLFilterImpl;
0N/Aimport org.xml.sax.helpers.DefaultHandler;
0N/Aimport org.xml.sax.XMLFilter;
0N/Aimport org.xml.sax.ContentHandler;
0N/Aimport org.xml.sax.SAXException;
0N/Aimport org.xml.sax.Attributes;
0N/A
0N/A/**
0N/A * {@link XMLFilter} that can cut sub-trees.
0N/A *
0N/A * @author Kohsuke Kawaguchi
0N/A */
0N/Apublic abstract class SubtreeCutter extends XMLFilterImpl {
0N/A /**
0N/A * When we are pruning a sub tree, this field holds the depth of
0N/A * elements that are being cut. Used to resume event forwarding.
0N/A *
0N/A * As long as this value is 0, we will pass through data.
0N/A */
0N/A private int cutDepth=0;
0N/A
0N/A
0N/A /**
0N/A * This object will receive SAX events while a sub tree is being
0N/A * pruned.
0N/A */
0N/A private static final ContentHandler stub = new DefaultHandler();
0N/A
0N/A /**
0N/A * This field remembers the user-specified ContentHandler.
0N/A * So that we can restore it once the sub tree is completely pruned.
0N/A */
0N/A private ContentHandler next;
0N/A
0N/A
0N/A public void startDocument() throws SAXException {
0N/A cutDepth=0;
0N/A super.startDocument();
0N/A }
0N/A
0N/A public boolean isCutting() {
0N/A return cutDepth>0;
0N/A }
0N/A
0N/A /**
0N/A * Starts cutting a sub-tree. Should be called from within the
0N/A * {@link #startElement(String, String, String, Attributes)} implementation
0N/A * before the execution is passed to {@link SubtreeCutter#startElement(String, String, String, Attributes)} .
0N/A * The current element will be cut.
0N/A */
0N/A public void startCutting() {
0N/A super.setContentHandler(stub);
0N/A cutDepth=1;
0N/A }
0N/A
0N/A public void setContentHandler(ContentHandler handler) {
0N/A next = handler;
0N/A // changes take effect immediately unless the sub-tree is being pruned
0N/A if(getContentHandler()!=stub)
0N/A super.setContentHandler(handler);
0N/A }
0N/A
0N/A public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
0N/A if(cutDepth>0)
0N/A cutDepth++;
0N/A super.startElement(uri, localName, qName, atts);
0N/A }
0N/A
0N/A public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
0N/A super.endElement(namespaceURI, localName, qName);
0N/A
0N/A if( cutDepth!=0 ) {
0N/A cutDepth--;
0N/A if( cutDepth == 1 ) {
0N/A // pruning completed. restore the user handler
0N/A super.setContentHandler(next);
0N/A cutDepth=0;
0N/A }
0N/A }
0N/A }
0N/A}
0N/A