FileDocCategorySizeDatePackage
Driver.javaAPI DocAndroid 1.5 API18727Wed May 06 22:41:06 BST 2009org.xmlpull.v1.sax2

Driver

public class Driver extends Object implements Locator, Attributes, XMLReader
SAX2 Driver that pulls events from XmlPullParser and comverts them into SAX2 callbacks.
author
Aleksander Slominski

Fields Summary
protected static final String
DECLARATION_HANDLER_PROPERTY
protected static final String
LEXICAL_HANDLER_PROPERTY
protected static final String
NAMESPACES_FEATURE
protected static final String
NAMESPACE_PREFIXES_FEATURE
protected static final String
VALIDATION_FEATURE
protected static final String
APACHE_SCHEMA_VALIDATION_FEATURE
protected static final String
APACHE_DYNAMIC_VALIDATION_FEATURE
protected ContentHandler
contentHandler
protected ErrorHandler
errorHandler
protected String
systemId
protected XmlPullParser
pp
Constructors Summary
public Driver()


    //private final static boolean DEBUG = false;

         
        
        final XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        pp = factory.newPullParser();
    
public Driver(XmlPullParser pp)

        this.pp = pp;
    
Methods Summary
public intgetColumnNumber()

 return pp.getColumnNumber(); 
public org.xml.sax.ContentHandlergetContentHandler()

 return contentHandler; 
public org.xml.sax.DTDHandlergetDTDHandler()

 return null; 
public org.xml.sax.EntityResolvergetEntityResolver()

 return null; 
public org.xml.sax.ErrorHandlergetErrorHandler()

 return errorHandler; 
public booleangetFeature(java.lang.String name)

        if(NAMESPACES_FEATURE.equals(name)) {
            return pp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES);
        } else if(NAMESPACE_PREFIXES_FEATURE.equals(name)) {
            return pp.getFeature(XmlPullParser.FEATURE_REPORT_NAMESPACE_ATTRIBUTES);
        } else if(VALIDATION_FEATURE.equals(name)) {
            return pp.getFeature(XmlPullParser.FEATURE_VALIDATION);
            //        } else if(APACHE_SCHEMA_VALIDATION_FEATURE.equals(name)) {
            //            return false;  //TODO
            //        } else if(APACHE_DYNAMIC_VALIDATION_FEATURE.equals(name)) {
            //            return false; //TODO
        } else {
            return pp.getFeature(name);
            //throw new SAXNotRecognizedException("unrecognized feature "+name);
        }
    
public intgetIndex(java.lang.String qName)

        for (int i = 0; i < pp.getAttributeCount(); i++)
        {
            if(pp.getAttributeName(i).equals(qName))
            {
                return i;
            }

        }
        return -1;
    
public intgetIndex(java.lang.String uri, java.lang.String localName)

        for (int i = 0; i < pp.getAttributeCount(); i++)
        {
            if(pp.getAttributeNamespace(i).equals(uri)
               && pp.getAttributeName(i).equals(localName))
            {
                return i;
            }

        }
        return -1;
    
public intgetLength()

 return pp.getAttributeCount(); 
public intgetLineNumber()

 return pp.getLineNumber(); 
public java.lang.StringgetLocalName(int index)

 return pp.getAttributeName(index); 
public java.lang.ObjectgetProperty(java.lang.String name)

        if(DECLARATION_HANDLER_PROPERTY.equals(name)) {
            return null;
        } else if(LEXICAL_HANDLER_PROPERTY.equals(name)) {
            return null;
        } else {
            return pp.getProperty(name);
            //throw new SAXNotRecognizedException("not recognized get property "+name);
        }
    
public java.lang.StringgetPublicId()

 return null; 
public java.lang.StringgetQName(int index)

        final String prefix = pp.getAttributePrefix(index);
        if(prefix != null) {
            return prefix+':"+pp.getAttributeName(index);
        } else {
            return pp.getAttributeName(index);
        }
    
public java.lang.StringgetSystemId()

 return systemId; 
public java.lang.StringgetType(java.lang.String uri, java.lang.String localName)

        for (int i = 0; i < pp.getAttributeCount(); i++)
        {
            if(pp.getAttributeNamespace(i).equals(uri)
               && pp.getAttributeName(i).equals(localName))
            {
                return pp.getAttributeType(i);
            }

        }
        return null;
    
public java.lang.StringgetType(java.lang.String qName)

        for (int i = 0; i < pp.getAttributeCount(); i++)
        {
            if(pp.getAttributeName(i).equals(qName))
            {
                return pp.getAttributeType(i);
            }

        }
        return null;
    
public java.lang.StringgetType(int index)

 return pp.getAttributeType(index); 
public java.lang.StringgetURI(int index)

 return pp.getAttributeNamespace(index); 
public java.lang.StringgetValue(java.lang.String uri, java.lang.String localName)

        return pp.getAttributeValue(uri, localName);
    
public java.lang.StringgetValue(java.lang.String qName)

        return pp.getAttributeValue(null, qName);
    
public java.lang.StringgetValue(int index)

 return pp.getAttributeValue(index); 
public voidparse(org.xml.sax.InputSource source)


        systemId = source.getSystemId();
        contentHandler.setDocumentLocator(this);

        final Reader reader = source.getCharacterStream();
        try {
            if (reader == null) {
                InputStream stream = source.getByteStream();
                final String encoding = source.getEncoding();

                if (stream == null) {
                    systemId = source.getSystemId();
                    if(systemId == null) {
                        SAXParseException saxException = new SAXParseException(
                            "null source systemId" , this);
                        errorHandler.fatalError(saxException);
                        return;
                    }
                    // NOTE: replace with Connection to run in J2ME environment
                    try {
                        final URL url = new URL(systemId);
                        stream = url.openStream();
                    } catch (MalformedURLException nue) {
                        try {
                            stream = new FileInputStream(systemId);
                        } catch (FileNotFoundException fnfe) {
                            final SAXParseException saxException = new SAXParseException(
                                "could not open file with systemId "+systemId, this, fnfe);
                            errorHandler.fatalError(saxException);
                            return;
                        }
                    }
                }
                pp.setInput(stream, encoding);
            } else {
                pp.setInput(reader);
            }
        } catch (XmlPullParserException ex)  {
            final SAXParseException saxException = new SAXParseException(
                "parsing initialization error: "+ex, this, ex);
            //if(DEBUG) ex.printStackTrace();
            errorHandler.fatalError(saxException);
            return;
        }

        // start parsing - move to first start tag
        try {
            contentHandler.startDocument();
            // get first event
            pp.next();
            // it should be start tag...
            if(pp.getEventType() != XmlPullParser.START_TAG) {
                final SAXParseException saxException = new SAXParseException(
                    "expected start tag not"+pp.getPositionDescription(), this);
                //throw saxException;
                errorHandler.fatalError(saxException);
                return;
            }
        } catch (XmlPullParserException ex)  {
            final SAXParseException saxException = new SAXParseException(
                "parsing initialization error: "+ex, this, ex);
            //ex.printStackTrace();
            errorHandler.fatalError(saxException);
            return;
        }

        // now real parsing can start!

        parseSubTree(pp);

        // and finished ...

        contentHandler.endDocument();
    
public voidparse(java.lang.String systemId)

        parse(new InputSource(systemId));
    
public voidparseSubTree(org.xmlpull.v1.XmlPullParser pp)

        this.pp = pp;
        final boolean namespaceAware = pp.getFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES);
        try {
            if(pp.getEventType() != XmlPullParser.START_TAG) {
                throw new SAXException(
                    "start tag must be read before skiping subtree"+pp.getPositionDescription());
            }
            final int[] holderForStartAndLength = new int[2];
            final StringBuffer rawName = new StringBuffer(16);
            String prefix = null;
            String name = null;
            int level = pp.getDepth() - 1;
            int type = XmlPullParser.START_TAG;

            LOOP:
            do {
                switch(type) {
                    case XmlPullParser.START_TAG:
                        if(namespaceAware) {
                            final int depth = pp.getDepth() - 1;
                            final int countPrev =
                                (level > depth) ? pp.getNamespaceCount(depth) : 0;
                            //int countPrev = pp.getNamespaceCount(pp.getDepth() - 1);
                            final int count = pp.getNamespaceCount(depth + 1);
                            for (int i = countPrev; i < count; i++)
                            {
                                contentHandler.startPrefixMapping(
                                    pp.getNamespacePrefix(i),
                                    pp.getNamespaceUri(i)
                                );
                            }
                            name = pp.getName();
                            prefix = pp.getPrefix();
                            if(prefix != null) {
                                rawName.setLength(0);
                                rawName.append(prefix);
                                rawName.append(':");
                                rawName.append(name);
                            }
                            startElement(pp.getNamespace(),
                                         name,
                                         // TODO Fixed this. Was "not equals".
                                         prefix == null ? name : rawName.toString());
                        } else {
                            startElement(pp.getNamespace(),
                                         pp.getName(),
                                         pp.getName());
                        }
                        //++level;

                        break;
                    case XmlPullParser.TEXT:
                        final char[] chars = pp.getTextCharacters(holderForStartAndLength);
                        contentHandler.characters(chars,
                                                  holderForStartAndLength[0], //start
                                                  holderForStartAndLength[1] //len
                                                 );
                        break;
                    case XmlPullParser.END_TAG:
                        //--level;
                        if(namespaceAware) {
                            name = pp.getName();
                            prefix = pp.getPrefix();
                            if(prefix != null) {
                                rawName.setLength(0);
                                rawName.append(prefix);
                                rawName.append(':");
                                rawName.append(name);
                            }
                            contentHandler.endElement(pp.getNamespace(),
                                                      name,
                                                      prefix != null ? name : rawName.toString()
                                                     );
                            // when entering show prefixes for all levels!!!!
                            final int depth = pp.getDepth();
                            final int countPrev =
                                (level > depth) ? pp.getNamespaceCount(pp.getDepth()) : 0;
                            int count = pp.getNamespaceCount(pp.getDepth() - 1);
                            // undeclare them in reverse order
                            for (int i = count - 1; i >= countPrev; i--)
                            {
                                contentHandler.endPrefixMapping(
                                    pp.getNamespacePrefix(i)
                                );
                            }
                        } else {
                            contentHandler.endElement(pp.getNamespace(),
                                                      pp.getName(),
                                                      pp.getName()
                                                     );

                        }
                        break;
                    case XmlPullParser.END_DOCUMENT:
                        break LOOP;
                }
                type = pp.next();
            } while(pp.getDepth() > level);
        } catch (XmlPullParserException ex)  {
            final SAXParseException saxException = new SAXParseException("parsing error: "+ex, this, ex);
            ex.printStackTrace();
            errorHandler.fatalError(saxException);
        }
    
public voidsetContentHandler(org.xml.sax.ContentHandler handler)

        this.contentHandler = handler;
    
public voidsetDTDHandler(org.xml.sax.DTDHandler handler)

public voidsetEntityResolver(org.xml.sax.EntityResolver resolver)

public voidsetErrorHandler(org.xml.sax.ErrorHandler handler)

        this.errorHandler = handler;
    
public voidsetFeature(java.lang.String name, boolean value)

        try {
            if(NAMESPACES_FEATURE.equals(name)) {
                pp.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, value);
            } else if(NAMESPACE_PREFIXES_FEATURE.equals(name)) {
                if(pp.getFeature(XmlPullParser.FEATURE_REPORT_NAMESPACE_ATTRIBUTES) != value) {
                    pp.setFeature(XmlPullParser.FEATURE_REPORT_NAMESPACE_ATTRIBUTES, value);
                }
            } else if(VALIDATION_FEATURE.equals(name)) {
                pp.setFeature(XmlPullParser.FEATURE_VALIDATION, value);
                //          } else if(APACHE_SCHEMA_VALIDATION_FEATURE.equals(name)) {
                //              // can ignore as validation must be false ...
                //              //              if(true == value) {
                //              //                  throw new SAXNotSupportedException("schema validation is not supported");
                //              //              }
                //          } else if(APACHE_DYNAMIC_VALIDATION_FEATURE.equals(name)) {
                //              if(true == value) {
                //                  throw new SAXNotSupportedException("dynamic validation is not supported");
                //              }
            } else {
                pp.setFeature(name, value);
                //throw new SAXNotRecognizedException("unrecognized feature "+name);
            }
        } catch(XmlPullParserException ex) {
           // throw new SAXNotSupportedException("problem with setting feature "+name+": "+ex);
        }
    
public voidsetProperty(java.lang.String name, java.lang.Object value)

        //
        if(DECLARATION_HANDLER_PROPERTY.equals(name)) {
            throw new SAXNotSupportedException("not supported setting property "+name);//+" to "+value);
        } else if(LEXICAL_HANDLER_PROPERTY.equals(name)) {
            throw new SAXNotSupportedException("not supported setting property "+name);//+" to "+value);
        } else {
            try {
                pp.setProperty(name, value);
            } catch(XmlPullParserException ex) {
                throw new SAXNotSupportedException("not supported set property "+name+": "+ ex);
            }
            //throw new SAXNotRecognizedException("not recognized set property "+name);
        }
    
protected voidstartElement(java.lang.String namespace, java.lang.String localName, java.lang.String qName)
Calls {@link ContentHandler#startElement(String, String, String, Attributes) startElement} on the ContentHandler with this driver object as the {@link Attributes} implementation. In default implementation {@link Attributes} object is valid only during this method call and may not be stored. Sub-classes can overwrite this method to cache attributes.

        contentHandler.startElement(namespace, localName, qName, this);