DefaultDataHandlerpublic class DefaultDataHandler extends Object implements ContentInsertHandlerInserts 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> |
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 |
Methods Summary |
---|
public void | characters(char[] ch, int start, int length)
// TODO Auto-generated method stub
| public void | endDocument()
// TODO Auto-generated method stub
| public void | endElement(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 void | endPrefixMapping(java.lang.String prefix)
// TODO Auto-generated method stub
| public void | ignorableWhitespace(char[] ch, int start, int length)
// TODO Auto-generated method stub
| public void | insert(ContentResolver contentResolver, java.io.InputStream in)
mContentResolver = contentResolver;
Xml.parse(in, Xml.Encoding.UTF_8, this);
| public void | insert(ContentResolver contentResolver, java.lang.String in)
mContentResolver = contentResolver;
Xml.parse(in, this);
| private android.net.Uri | insertRow()
Uri u = mContentResolver.insert(mUris.lastElement(), mValues);
mValues = null;
return u;
| private void | parseRow(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 void | processingInstruction(java.lang.String target, java.lang.String data)
// TODO Auto-generated method stub
| public void | setDocumentLocator(org.xml.sax.Locator locator)
// TODO Auto-generated method stub
| public void | skippedEntity(java.lang.String name)
// TODO Auto-generated method stub
| public void | startDocument()
// TODO Auto-generated method stub
| public void | startElement(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 void | startPrefixMapping(java.lang.String prefix, java.lang.String uri)
// TODO Auto-generated method stub
|
|