001: /*
002: * This file is part of the WfMOpen project.
003: * Copyright (C) 2001-2004 Danet GmbH (www.danet.de), GS-AN.
004: * All rights reserved.
005: *
006: * This program is free software; you can redistribute it and/or modify
007: * it under the terms of the GNU General Public License as published by
008: * the Free Software Foundation; either version 2 of the License, or
009: * (at your option) any later version.
010: *
011: * This program is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
014: * GNU General Public License for more details.
015: *
016: * You should have received a copy of the GNU General Public License
017: * along with this program; if not, write to the Free Software
018: * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
019: *
020: * $Id: NamespaceAttributesAdder.java,v 1.2 2006/12/12 09:58:55 drmlipp Exp $
021: *
022: * $Log: NamespaceAttributesAdder.java,v $
023: * Revision 1.2 2006/12/12 09:58:55 drmlipp
024: * Added.
025: *
026: * Revision 1.1.2.1 2006/12/07 14:32:48 drmlipp
027: * Added.
028: *
029: */
030: package de.danet.an.util.sax;
031:
032: import java.util.ArrayList;
033: import java.util.Iterator;
034: import java.util.List;
035:
036: import org.xml.sax.Attributes;
037: import org.xml.sax.ContentHandler;
038: import org.xml.sax.SAXException;
039: import org.xml.sax.helpers.AttributesImpl;
040: import org.xml.sax.helpers.XMLFilterImpl;
041:
042: import de.danet.an.util.XMLUtil;
043:
044: /**
045: * This class provides a filter that adds namespace declarations
046: * (<code>xmlns:ns='...'</code>) in <code>startElement</code>
047: * events. These attributes are by default not delivered by a SAX
048: * parser but are required by some software packages.
049: *
050: * @author <a href="mailto:lipp@danet.de">Michael Lipp</a>
051: * @version $Revision: 1.2 $
052: */
053:
054: public class NamespaceAttributesAdder extends XMLFilterImpl {
055:
056: private List accumulatedMappings = new ArrayList();
057:
058: /**
059: * Creates an instance of <code>NamespaceAttributesFilter</code>.
060: * @param contentHandler the given content handler.
061: */
062: public NamespaceAttributesAdder(ContentHandler contentHandler) {
063: this .setContentHandler(contentHandler);
064: }
065:
066: /* (non-Javadoc)
067: * @see org.xml.sax.helpers.XMLFilterImpl#startPrefixMapping
068: */
069: public void startPrefixMapping(String prefix, String uri)
070: throws SAXException {
071: super .startPrefixMapping(prefix, uri);
072: accumulatedMappings.add(new String[] { prefix, uri });
073: }
074:
075: /**
076: * Report the start of a new element. Filters the namespace
077: * declarations from the attributes.
078: *
079: * @param nsUri a <code>String</code> value
080: * @param lname a <code>String</code> value
081: * @param qname a <code>String</code> value
082: * @param attributes an <code>Attributes</code> value
083: * @exception SAXException if an error occurs
084: */
085: public void startElement(String nsUri, String lname, String qname,
086: Attributes attributes) throws SAXException {
087: AttributesImpl newAtts = new AttributesImpl(attributes);
088: for (Iterator i = accumulatedMappings.iterator(); i.hasNext();) {
089: String[] mapping = (String[]) i.next();
090: String prefix = mapping[0];
091: String uri = mapping[1];
092: if (attributes.getValue(XMLUtil.XMLNS_NS, prefix) == null) {
093: newAtts.addAttribute(XMLUtil.XMLNS_NS, prefix, "xmlns"
094: + (prefix.length() == 0 ? "" : (":" + prefix)),
095: "CDATA", uri);
096: }
097: }
098: accumulatedMappings.clear();
099: super.startElement(nsUri, lname, qname, newAtts);
100: }
101: }
|