001: /*
002: * Copyright 2006 Sun Microsystems, Inc. All Rights Reserved.
003: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
004: *
005: * This code is free software; you can redistribute it and/or modify it
006: * under the terms of the GNU General Public License version 2 only, as
007: * published by the Free Software Foundation. Sun designates this
008: * particular file as subject to the "Classpath" exception as provided
009: * by Sun in the LICENSE file that accompanied this code.
010: *
011: * This code is distributed in the hope that it will be useful, but WITHOUT
012: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
013: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
014: * version 2 for more details (a copy is included in the LICENSE file that
015: * accompanied this code).
016: *
017: * You should have received a copy of the GNU General Public License version
018: * 2 along with this work; if not, write to the Free Software Foundation,
019: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
020: *
021: * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
022: * CA 95054 USA or visit www.sun.com if you need additional information or
023: * have any questions.
024: */
025:
026: package com.sun.tools.internal.xjc.reader.internalizer;
027:
028: import javax.xml.XMLConstants;
029:
030: import org.xml.sax.Attributes;
031: import org.xml.sax.SAXException;
032: import org.xml.sax.SAXNotRecognizedException;
033: import org.xml.sax.SAXNotSupportedException;
034: import org.xml.sax.XMLReader;
035: import org.xml.sax.helpers.AttributesImpl;
036: import org.xml.sax.helpers.XMLFilterImpl;
037:
038: /**
039: * {@link XMLReader} filter for supporting
040: * <tt>http://xml.org/sax/features/namespace-prefixes</tt> feature.
041: *
042: * @author Kohsuke Kawaguchi
043: */
044: final class ContentHandlerNamespacePrefixAdapter extends XMLFilterImpl {
045: /**
046: * True if <tt>http://xml.org/sax/features/namespace-prefixes</tt> is set to true.
047: */
048: private boolean namespacePrefixes = false;
049:
050: private String[] nsBinding = new String[8];
051: private int len;
052:
053: public ContentHandlerNamespacePrefixAdapter() {
054: }
055:
056: public ContentHandlerNamespacePrefixAdapter(XMLReader parent) {
057: setParent(parent);
058: }
059:
060: public boolean getFeature(String name)
061: throws SAXNotRecognizedException, SAXNotSupportedException {
062: if (name.equals(PREFIX_FEATURE))
063: return namespacePrefixes;
064: return super .getFeature(name);
065: }
066:
067: public void setFeature(String name, boolean value)
068: throws SAXNotRecognizedException, SAXNotSupportedException {
069: if (name.equals(PREFIX_FEATURE)) {
070: this .namespacePrefixes = value;
071: return;
072: }
073: if (name.equals(NAMESPACE_FEATURE) && value)
074: return;
075: super .setFeature(name, value);
076: }
077:
078: public void startPrefixMapping(String prefix, String uri)
079: throws SAXException {
080: if (len == nsBinding.length) {
081: // reallocate
082: String[] buf = new String[nsBinding.length * 2];
083: System.arraycopy(nsBinding, 0, buf, 0, nsBinding.length);
084: nsBinding = buf;
085: }
086: nsBinding[len++] = prefix;
087: nsBinding[len++] = uri;
088: super .startPrefixMapping(prefix, uri);
089: }
090:
091: public void startElement(String uri, String localName,
092: String qName, Attributes atts) throws SAXException {
093: if (namespacePrefixes) {
094: this .atts.setAttributes(atts);
095: // add namespace bindings back as attributes
096: for (int i = 0; i < len; i += 2) {
097: String prefix = nsBinding[i];
098: if (prefix.length() == 0)
099: this .atts
100: .addAttribute(XMLConstants.XML_NS_URI,
101: "xmlns", "xmlns", "CDATA",
102: nsBinding[i + 1]);
103: else
104: this .atts.addAttribute(XMLConstants.XML_NS_URI,
105: prefix, "xmlns:" + prefix, "CDATA",
106: nsBinding[i + 1]);
107: }
108: atts = this .atts;
109: }
110: len = 0;
111: super .startElement(uri, localName, qName, atts);
112: }
113:
114: private final AttributesImpl atts = new AttributesImpl();
115:
116: private static final String PREFIX_FEATURE = "http://xml.org/sax/features/namespace-prefixes";
117: private static final String NAMESPACE_FEATURE = "http://xml.org/sax/features/namespaces";
118: }
|