For my bachelor theses I program for android and write a ContentProvider, which has to check URIs against a pattern. For this job I use the uriMatcher Class, but in conflict to the example in the documentation there are some things you have to take care of.
The Constructor
private static final UriMatcher sURIMatcher = new UriMatcher();
you have to specify a int-parameter.
addURI
sURIMatcher.addURI("contacts", "/people", PEOPLE);
sURIMatcher.addURI("contacts", "/people/#", PEOPLE_ID);
sURIMatcher.addURI("contacts", "/people/#/phones", PEOPLE_PHONES);
sURIMatcher.addURI("contacts", "/people/#/phones/#", PEOPLE_PHONES_ID);
- The second parameters String has to start without leading slash.
- The most specific pattern has to be added first.
If you experience the opposite pleas tell me. The leading-slash problem is also documented in the android-issue-tracker.
Nur für diejenigen die sich auch mit Metakit befassen ein weiteres schlechtes Beispiel Code, kein schöner Code aber vielleicht hilft es jemandem. Es hat zumindest meinen Zweck erfüllt eine Akregator Datenbank auszulesen. Geholfen hat mir auch die cplusplus.com Referenz geholfen.
#include <mk4.h> // http://www.equi4.com/metakit/api/mk4_8h.html
#include <mk4io.h> // http://www.equi4.com/metakit/api/mk4io_8h.html
#include <mk4str.h> // http://www.equi4.com/metakit/api/mk4str_8h.html
#include <string>
#include <vector>
#include <iostream>
#include <time.h>
using namespace std;
int main (int argc, char** argv) {
vector<string> toc;
string::iterator tocStringIt;
vector<string>::iterator tocIt;
int index = 0, stack = 0, lastStringIndex = 0;
c4_Storage storage (argv[1], true);
string tocString = string(storage.Description()); // get a Table of Content as String
c4_View db = storage.GetAs(tocString.c_str());
cout << "Read MetaKit Database with " << (long)db.GetSize() << " rows" << endl;
cout << "and the following table of content: " << endl;
cout << tocString << endl;
for (tocStringIt = tocString.begin(); tocStringIt <= tocString.end(); tocStringIt++) {
if(*tocStringIt == ',' || *tocStringIt == '') {
if(stack == 0) {
// split string at this position
// and add substring to the toc (Array)
// maybe http://www.cplusplus.com/reference/string/string/ helps
// It would be cooler to solve this completely with itarators and not with the lastStringIndex int.
cout << "Split Sting" << endl;
toc.push_back(tocString.substr(lastStringIndex,(int)(tocStringIt-tocString.begin())));
lastStringIndex = (int)(tocStringIt-tocString.begin())+1;
}
else if (*tocStringIt == '') {
cout << "Am Ende des Strings ";
if(stack > 1) cout << "liegen"; else cout << "liegt";
cout << "noch" << stack;
if(stack > 1) cout << "Klammern"; else cout << "Klammer";
cout << "auf dem Stack, das ist nicht gut." << endl;
}
}
else if(*tocStringIt == '[') stack++;
else if(*tocStringIt == ']') stack--;
}
vector<c4_View> views;
cout << "the splitted toc" << endl;
for (tocIt=toc.begin(); tocIt < toc.end(); tocIt++) {
cout << " " << *tocIt << endl;
c4_View view = storage.GetAs((*tocIt).c_str());
views.push_back(view);
}
vector<c4_View>::iterator viewIt;
c4_StringProp guid("guid"), title("title"), description("description"), link("link"), commentsLink("commentsLink"), enclosureUrl("enclosureUrl"), enclosureType("enclosureType"), author("author");
c4_IntProp hash("hash"), guidIsHash("guidIsHash"), guidIsPermaLink("guidIsPermaLink"), comments("comments"), status("status"), pubDate("pubDate"), hasEnclosure("hasEnclosure"), enclosureLength("enclosureLength");
for (int article = 0; article < views.at(0).GetSize(); article++) {
time_t time = (time_t)pubDate((views.at(0))[article]);
char* date = ctime(&time);
cout << "<h2>" << title((views.at(0))[article]) << "</h2>" << endl;
cout << "<em>" << date << "</em>" << endl;
cout << description((views.at(0))[article]) << endl;
cout << "<em>" << author((views.at(0))[article]) << "</em>" << endl;
cout << endl;
}
return 0;
}