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.reader.internalizer;
038:
039: import javax.xml.XMLConstants;
040:
041: import org.xml.sax.Attributes;
042: import org.xml.sax.SAXException;
043: import org.xml.sax.SAXNotRecognizedException;
044: import org.xml.sax.SAXNotSupportedException;
045: import org.xml.sax.XMLReader;
046: import org.xml.sax.helpers.AttributesImpl;
047: import org.xml.sax.helpers.XMLFilterImpl;
048:
049: /**
050: * {@link XMLReader} filter for supporting
051: * <tt>http://xml.org/sax/features/namespace-prefixes</tt> feature.
052: *
053: * @author Kohsuke Kawaguchi
054: */
055: final class ContentHandlerNamespacePrefixAdapter extends XMLFilterImpl {
056: /**
057: * True if <tt>http://xml.org/sax/features/namespace-prefixes</tt> is set to true.
058: */
059: private boolean namespacePrefixes = false;
060:
061: private String[] nsBinding = new String[8];
062: private int len;
063:
064: public ContentHandlerNamespacePrefixAdapter() {
065: }
066:
067: public ContentHandlerNamespacePrefixAdapter(XMLReader parent) {
068: setParent(parent);
069: }
070:
071: public boolean getFeature(String name)
072: throws SAXNotRecognizedException, SAXNotSupportedException {
073: if (name.equals(PREFIX_FEATURE))
074: return namespacePrefixes;
075: return super .getFeature(name);
076: }
077:
078: public void setFeature(String name, boolean value)
079: throws SAXNotRecognizedException, SAXNotSupportedException {
080: if (name.equals(PREFIX_FEATURE)) {
081: this .namespacePrefixes = value;
082: return;
083: }
084: if (name.equals(NAMESPACE_FEATURE) && value)
085: return;
086: super .setFeature(name, value);
087: }
088:
089: public void startPrefixMapping(String prefix, String uri)
090: throws SAXException {
091: if (len == nsBinding.length) {
092: // reallocate
093: String[] buf = new String[nsBinding.length * 2];
094: System.arraycopy(nsBinding, 0, buf, 0, nsBinding.length);
095: nsBinding = buf;
096: }
097: nsBinding[len++] = prefix;
098: nsBinding[len++] = uri;
099: super .startPrefixMapping(prefix, uri);
100: }
101:
102: public void startElement(String uri, String localName,
103: String qName, Attributes atts) throws SAXException {
104: if (namespacePrefixes) {
105: this .atts.setAttributes(atts);
106: // add namespace bindings back as attributes
107: for (int i = 0; i < len; i += 2) {
108: String prefix = nsBinding[i];
109: if (prefix.length() == 0)
110: this .atts
111: .addAttribute(XMLConstants.XML_NS_URI,
112: "xmlns", "xmlns", "CDATA",
113: nsBinding[i + 1]);
114: else
115: this .atts.addAttribute(XMLConstants.XML_NS_URI,
116: prefix, "xmlns:" + prefix, "CDATA",
117: nsBinding[i + 1]);
118: }
119: atts = this .atts;
120: }
121: len = 0;
122: super .startElement(uri, localName, qName, atts);
123: }
124:
125: private final AttributesImpl atts = new AttributesImpl();
126:
127: private static final String PREFIX_FEATURE = "http://xml.org/sax/features/namespace-prefixes";
128: private static final String NAMESPACE_FEATURE = "http://xml.org/sax/features/namespaces";
129: }
|