users@jaxb.java.net

Re: Ignoring Namespace URI on Unmarshalling

From: Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM>
Date: Fri, 14 Jan 2005 09:05:58 -0800

Chad Boyd wrote:
> I guess I could, but that seems like extra overhead.

I agree that it could be overhead for developers, because SAX isn't the
easiest API in the world. But in terms of the overhead for processing,
it's probably the best.

You can start from the attached code example. I thought we had something
similar in our samples. Check them out.

> I thought maybe
> there was some property in JAXB to tell it to not validate the
> namespace URI, or something along those lines. Is there something
> similar to this?

No.

-- 
Kohsuke Kawaguchi
Sun Microsystems                   kohsuke.kawaguchi_at_sun.com



import javax.xml.bind.Unmarshaller;
import javax.xml.bind.UnmarshallerHandler;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLFilter;
import org.xml.sax.helpers.XMLFilterImpl;

/**
 * @author Kohsuke Kawaguchi
 */
public class IgnoreNamespaceURIFilter extends XMLFilterImpl implements Attributes {
    
    private Attributes atts;

    public void startElement(String uri, String localName, String qName, Attributes atts) throws SAXException {
        this.atts = atts;
        super.startElement("",localName,qName,this);
        this.atts = null;
    }

    public void endElement(String uri, String localName, String qName) throws SAXException {
        super.endElement("", localName, qName);
    }

    // in real code it shouldn't be here.
    // this is just for illustration
    static Object unmarshal( Unmarshaller unmarshaller, InputSource input ) throws Exception {
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);

        XMLFilter r = new IgnoreNamespaceURIFilter();
        r.setParent(spf.newSAXParser().getXMLReader());

        UnmarshallerHandler handler = unmarshaller.getUnmarshallerHandler();
        r.setContentHandler(handler);

        r.parse(input);

        return handler.getResult();
    }


//
//
// Attributes implementation
//
//
    public int getLength() {
        return atts.getLength();
    }

    public String getURI(int index) {
        return "";
    }

    public String getLocalName(int index) {
        return atts.getLocalName(index);
    }

    public String getQName(int index) {
        return atts.getQName(index);
    }

    public String getType(int index) {
        return atts.getType(index);
    }

    public String getValue(int index) {
        return atts.getValue(index);
    }

    public int getIndex(String uri, String localName) {
        return atts.getIndex(uri, localName);
    }

    public int getIndex(String qName) {
        return atts.getIndex(qName);
    }

    public String getType(String uri, String localName) {
        return atts.getType(uri, localName);
    }

    public String getType(String qName) {
        return atts.getType(qName);
    }

    public String getValue(String uri, String localName) {
        return atts.getValue(uri, localName);
    }

    public String getValue(String qName) {
        return atts.getValue(qName);
    }
}



---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
For additional commands, e-mail: users-help_at_jaxb.dev.java.net