FileDocCategorySizeDatePackage
DefaultDataHandler.javaAPI DocAndroid 1.5 API8476Wed May 06 22:41:54 BST 2009android.content

DefaultDataHandler

public class DefaultDataHandler extends Object implements ContentInsertHandler
Inserts default data from InputStream, should be in XML format. If the provider syncs data to the server, the imported data will be synced to the server.

Samples:


Insert one row:
<row uri="content://contacts/people">
<Col column = "name" value = "foo feebe "/>
<Col column = "addr" value = "Tx"/>
</row>

Delete, it must be in order of uri, select, and arg:
<del uri="content://contacts/people" select="name=? and addr=?"
arg1 = "foo feebe" arg2 ="Tx"/>

Use first row's uri to insert into another table, content://contacts/people/1/phones:
<row uri="content://contacts/people">
<col column = "name" value = "foo feebe"/>
<col column = "addr" value = "Tx"/>
<row postfix="phones">
<col column="number" value="512-514-6535"/>
</row>
<row postfix="phones">
<col column="cell" value="512-514-6535"/>
</row>
</row>

Insert multiple rows in to same table and same attributes:
<row uri="content://contacts/people" >
<row>
<col column= "name" value = "foo feebe"/>
<col column= "addr" value = "Tx"/>
</row>
<row>
</row>
</row>
hide

Fields Summary
private static final String
ROW
private static final String
COL
private static final String
URI_STR
private static final String
POSTFIX
private static final String
DEL
private static final String
SELECT
private static final String
ARG
private Stack
mUris
private ContentValues
mValues
private ContentResolver
mContentResolver
Constructors Summary
Methods Summary
public voidcharacters(char[] ch, int start, int length)

        // TODO Auto-generated method stub

    
public voidendDocument()

        // TODO Auto-generated method stub

    
public voidendElement(java.lang.String uri, java.lang.String localName, java.lang.String name)

        if (ROW.equals(localName)) {
            if (mUris.empty()) {
                throw new SAXException("uri mismatch"); 
            }
            if (mValues != null) {
                insertRow();
            }
            mUris.pop();                
        } 
    
public voidendPrefixMapping(java.lang.String prefix)

        // TODO Auto-generated method stub

    
public voidignorableWhitespace(char[] ch, int start, int length)

        // TODO Auto-generated method stub

    
public voidinsert(ContentResolver contentResolver, java.io.InputStream in)

   
    
         
               
        mContentResolver = contentResolver;
        Xml.parse(in, Xml.Encoding.UTF_8, this);
    
public voidinsert(ContentResolver contentResolver, java.lang.String in)

        mContentResolver = contentResolver;
        Xml.parse(in, this);
    
private android.net.UriinsertRow()

        Uri u = mContentResolver.insert(mUris.lastElement(), mValues);
        mValues = null;
        return u;
    
private voidparseRow(org.xml.sax.Attributes atts)

        String uriStr = atts.getValue(URI_STR);
        Uri uri;
        if (uriStr != null) {
            // case 1
            uri = Uri.parse(uriStr);
            if (uri == null) {
                throw new SAXException("attribute " +
                        atts.getValue(URI_STR) + " parsing failure"); 
            }
            
        } else if (mUris.size() > 0){
            // case 2
            String postfix = atts.getValue(POSTFIX);
            if (postfix != null) {
                uri = Uri.withAppendedPath(mUris.lastElement(),
                        postfix);
            } else {
                uri = mUris.lastElement();
            } 
        } else {
            throw new SAXException("attribute parsing failure"); 
        }
        
        mUris.push(uri);
        
    
public voidprocessingInstruction(java.lang.String target, java.lang.String data)

        // TODO Auto-generated method stub

    
public voidsetDocumentLocator(org.xml.sax.Locator locator)

        // TODO Auto-generated method stub

    
public voidskippedEntity(java.lang.String name)

        // TODO Auto-generated method stub

    
public voidstartDocument()

        // TODO Auto-generated method stub

    
public voidstartElement(java.lang.String uri, java.lang.String localName, java.lang.String name, org.xml.sax.Attributes atts)

        if (ROW.equals(localName)) {            
            if (mValues != null) {
                // case 2, <Col> before <Row> insert last uri
                if (mUris.empty()) {
                    throw new SAXException("uri is empty");
                }
                Uri nextUri = insertRow();
                if (nextUri == null) {
                    throw new SAXException("insert to uri " + 
                            mUris.lastElement().toString() + " failure");
                } else {
                    // make sure the stack lastElement save uri for more than one row
                    mUris.pop();
                    mUris.push(nextUri);
                    parseRow(atts);
                }
            } else {
                int attrLen = atts.getLength();
                if (attrLen == 0) {
                    // case 3, share same uri as last level
                    mUris.push(mUris.lastElement());
                } else {
                    parseRow(atts);
                }
            }                
        } else if (COL.equals(localName)) {
            int attrLen = atts.getLength();
            if (attrLen != 2) {
                throw new SAXException("illegal attributes number " + attrLen);
            }
            String key = atts.getValue(0);
            String value = atts.getValue(1);
            if (key != null && key.length() > 0 && value != null && value.length() > 0) {
                if (mValues == null) {
                    mValues = new ContentValues();
                }
                mValues.put(key, value);
            } else {
                throw new SAXException("illegal attributes value");
            }            
        } else if (DEL.equals(localName)){
            Uri u = Uri.parse(atts.getValue(URI_STR));
            if (u == null) {
                throw new SAXException("attribute " +
                        atts.getValue(URI_STR) + " parsing failure"); 
            }
            int attrLen = atts.getLength() - 2;
            if (attrLen > 0) {
                String[] selectionArgs = new String[attrLen];
                for (int i = 0; i < attrLen; i++) {
                    selectionArgs[i] = atts.getValue(i+2);
                }
                mContentResolver.delete(u, atts.getValue(1), selectionArgs);
            } else if (attrLen == 0){
                mContentResolver.delete(u, atts.getValue(1), null);
            } else {
                mContentResolver.delete(u, null, null);
            }
            
        } else {
            throw new SAXException("unknown element: " + localName);
        }
    
public voidstartPrefixMapping(java.lang.String prefix, java.lang.String uri)

        // TODO Auto-generated method stub