src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/bindinfo/ForkingFilter.java

changeset 0
373ffda63c9a
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/jaxws_classes/com/sun/tools/internal/xjc/reader/xmlschema/bindinfo/ForkingFilter.java	Wed Apr 27 01:27:09 2016 +0800
     1.3 @@ -0,0 +1,181 @@
     1.4 +/*
     1.5 + * Copyright (c) 1997, 2011, 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.xjc.reader.xmlschema.bindinfo;
    1.30 +
    1.31 +import com.sun.xml.internal.bind.v2.WellKnownNamespace;
    1.32 +import java.util.ArrayList;
    1.33 +
    1.34 +import org.xml.sax.Attributes;
    1.35 +import org.xml.sax.ContentHandler;
    1.36 +import org.xml.sax.Locator;
    1.37 +import org.xml.sax.SAXException;
    1.38 +import org.xml.sax.XMLFilter;
    1.39 +import org.xml.sax.helpers.XMLFilterImpl;
    1.40 +
    1.41 +/**
    1.42 + * {@link XMLFilter} that can fork an event to another {@link ContentHandler}
    1.43 + * in the middle.
    1.44 + *
    1.45 + * <p>
    1.46 + * The side handler receives SAX events before the next handler in the filter chain does.
    1.47 + *
    1.48 + * @author Kohsuke Kawaguchi
    1.49 + */
    1.50 +public class ForkingFilter extends XMLFilterImpl {
    1.51 +
    1.52 +    /**
    1.53 +     * Non-null if we are also forking events to this handler.
    1.54 +     */
    1.55 +    private ContentHandler side;
    1.56 +
    1.57 +    /**
    1.58 +     * The depth of the current element that the {@link #side} handler
    1.59 +     * is seeing.
    1.60 +     */
    1.61 +    private int depth;
    1.62 +
    1.63 +    /**
    1.64 +     * In-scope namespace mapping.
    1.65 +     * namespaces[2n  ] := prefix
    1.66 +     * namespaces[2n+1] := namespace URI
    1.67 +     */
    1.68 +    private final ArrayList<String> namespaces = new ArrayList<String>();
    1.69 +
    1.70 +    private Locator loc;
    1.71 +
    1.72 +    public ForkingFilter() {
    1.73 +    }
    1.74 +
    1.75 +    public ForkingFilter(ContentHandler next) {
    1.76 +        setContentHandler(next);
    1.77 +    }
    1.78 +
    1.79 +    public ContentHandler getSideHandler() {
    1.80 +        return side;
    1.81 +    }
    1.82 +
    1.83 +    @Override
    1.84 +    public void setDocumentLocator(Locator locator) {
    1.85 +        super.setDocumentLocator(locator);
    1.86 +        this.loc = locator;
    1.87 +    }
    1.88 +
    1.89 +    public Locator getDocumentLocator() {
    1.90 +        return loc;
    1.91 +    }
    1.92 +
    1.93 +    @Override
    1.94 +    public void startDocument() throws SAXException {
    1.95 +        reset();
    1.96 +        super.startDocument();
    1.97 +    }
    1.98 +
    1.99 +    private void reset() {
   1.100 +        namespaces.clear();
   1.101 +        side = null;
   1.102 +        depth = 0;
   1.103 +    }
   1.104 +
   1.105 +    @Override
   1.106 +    public void endDocument() throws SAXException {
   1.107 +        loc = null;
   1.108 +        reset();
   1.109 +        super.endDocument();
   1.110 +    }
   1.111 +
   1.112 +    @Override
   1.113 +    public void startPrefixMapping(String prefix, String uri) throws SAXException {
   1.114 +        if (WellKnownNamespace.XML_NAMESPACE_URI.equals(uri)) return; //xml prefix shall not be declared based on jdk api javadoc
   1.115 +        if(side!=null)
   1.116 +            side.startPrefixMapping(prefix,uri);
   1.117 +        namespaces.add(prefix);
   1.118 +        namespaces.add(uri);
   1.119 +        super.startPrefixMapping(prefix,uri);
   1.120 +    }
   1.121 +
   1.122 +    @Override
   1.123 +    public void endPrefixMapping(String prefix) throws SAXException {
   1.124 +        if ("xml".equals(prefix)) return; //xml prefix shall not be declared based on jdk api javadoc
   1.125 +        if(side!=null)
   1.126 +            side.endPrefixMapping(prefix);
   1.127 +        super.endPrefixMapping(prefix);
   1.128 +        namespaces.remove(namespaces.size()-1);
   1.129 +        namespaces.remove(namespaces.size()-1);
   1.130 +    }
   1.131 +
   1.132 +    @Override
   1.133 +    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
   1.134 +        if(side!=null) {
   1.135 +            side.startElement(uri,localName,qName,atts);
   1.136 +            depth++;
   1.137 +        }
   1.138 +        super.startElement(uri, localName, qName, atts);
   1.139 +    }
   1.140 +
   1.141 +    /**
   1.142 +     * Starts the event forking.
   1.143 +     */
   1.144 +    public void startForking(String uri, String localName, String qName, Attributes atts, ContentHandler side) throws SAXException {
   1.145 +        if(this.side!=null)     throw new IllegalStateException();  // can't fork to two handlers
   1.146 +
   1.147 +        this.side = side;
   1.148 +        depth = 1;
   1.149 +        side.setDocumentLocator(loc);
   1.150 +        side.startDocument();
   1.151 +        for( int i=0; i<namespaces.size(); i+=2 )
   1.152 +            side.startPrefixMapping(namespaces.get(i),namespaces.get(i+1));
   1.153 +        side.startElement(uri,localName,qName,atts);
   1.154 +    }
   1.155 +
   1.156 +    @Override
   1.157 +    public void endElement(String uri, String localName, String qName) throws SAXException {
   1.158 +        if(side!=null) {
   1.159 +            side.endElement(uri,localName,qName);
   1.160 +            depth--;
   1.161 +            if(depth==0) {
   1.162 +                for( int i=namespaces.size()-2; i>=0; i-=2 )
   1.163 +                    side.endPrefixMapping(namespaces.get(i));
   1.164 +                side.endDocument();
   1.165 +                side = null;
   1.166 +            }
   1.167 +        }
   1.168 +        super.endElement(uri, localName, qName);
   1.169 +    }
   1.170 +
   1.171 +    @Override
   1.172 +    public void characters(char ch[], int start, int length) throws SAXException {
   1.173 +        if(side!=null)
   1.174 +            side.characters(ch, start, length);
   1.175 +        super.characters(ch, start, length);
   1.176 +    }
   1.177 +
   1.178 +    @Override
   1.179 +    public void ignorableWhitespace(char ch[], int start, int length) throws SAXException {
   1.180 +        if(side!=null)
   1.181 +            side.ignorableWhitespace(ch, start, length);
   1.182 +        super.ignorableWhitespace(ch, start, length);
   1.183 +    }
   1.184 +}

mercurial