Skip to content
Browse files

now you can match rules with rule manager, replacement is also coming…

… soon
  • Loading branch information...
1 parent e114ab7 commit 25402ea333a9429c454d2d8b74d1ad282f9ace6a @moscartong moscartong committed Feb 6, 2012
View
BIN Rythem.asta
Binary file not shown.
View
7 main.cpp
@@ -9,10 +9,10 @@
#include <QtCore>
#include "qirulemanager.h"
-
#include "qirulemanager2.h"
#include "qirulegroup2.h"
#include "qirule2.h"
+#include "QUrl"
void myMessageHandler(QtMsgType type, const char *msg)
{
@@ -49,6 +49,11 @@ int main(int argc, char *argv[])
QiRuleManager2 manager("/Users/moscartong/Desktop/config.txt");
manager.loadConfig();
+ QiRule2 *rule = manager.getMatchRule("http://abc.com/1.html");
+ if(rule && !rule->isNull()){
+ qDebug() << "match result:" << rule->toJSON();
+ }
+ else qDebug() << "NO MATCH";
QiRuleManager::instance();
View
43 qirule2.cpp
@@ -52,42 +52,27 @@ void QiRule2::update(const QiRule2 &rule){
this->update(rule.name(), rule.type(), rule.pattern(), rule.replacement(), rule.isEnable(), rule.isRemoteRule());
}
-bool QiRule2::match(ConnectionData_ptr conn) const{
+bool QiRule2::match(const QString &) const{
//override this method in different type of rule
- QRegExp rx(pattern(), Qt::CaseInsensitive, QRegExp::Wildcard);
- switch(type()){
- case COMPLEX_ADDRESS_REPLACE:
- //ignore complex address replace first
- return false;
-
- case SIMPLE_ADDRESS_REPLACE:
- return conn->host == pattern();
-
- case REMOTE_CONTENT_REPLACE:
- case LOCAL_FILE_REPLACE:
- case LOCAL_FILES_REPLACE:
- return rx.exactMatch(conn->fullUrl);
-
- case LOCAL_DIR_REPLACE:
- return (conn->fullUrl.indexOf(pattern()) != -1);
-
- default:
- return false;
- }
+ qDebug() << "i am called >__<";
+ return false;
}
-void QiRule2::replace(ConnectionData_ptr conn) const{
+void QiRule2::replace(ConnectionData_ptr) const{
//override this method in different type of rule
}
QString QiRule2::toJSON() const{
- QString result;
- QTextStream(&result) << "{\"name\":\"" << _name << "\","
- << "\"type\":" << _type << ","
- << "\"enable\":" << _isEnable << ","
- << "\"pattern\":\"" << _pattern << "\","
- << "\"replace\":\"" << _replacement << "\"}";
- return result;
+ if(!isNull()){
+ QString result;
+ QTextStream(&result) << "{\"name\":\"" << _name << "\","
+ << "\"type\":" << _type << ","
+ << "\"enable\":" << _isEnable << ","
+ << "\"pattern\":\"" << _pattern << "\","
+ << "\"replace\":\"" << _replacement << "\"}";
+ return result;
+ }
+ else return "{}";
}
bool QiRule2::isNull() const{
View
4 qirule2.h
@@ -26,8 +26,8 @@ class QiRule2 : public QObject
void update(QString name, int type, QString pattern, QString replacement, bool enable = true, bool remote = false);
void update(const QiRule2 &rule);
- bool match(ConnectionData_ptr conn) const;
- void replace(ConnectionData_ptr conn) const;
+ virtual bool match(const QString &url) const;
+ virtual void replace(ConnectionData_ptr conn) const;
QString toJSON() const;
bool isNull() const;
View
5 qirulecomplexaddress.cpp
@@ -3,13 +3,14 @@
QiRuleComplexAddress::QiRuleComplexAddress(QString name, int type, QString pattern, QString replacement, bool enable, bool remote) :
QiRule2(name, type, pattern, replacement, enable, remote)
{
+
}
-bool QiRuleComplexAddress::match(ConnectionData_ptr conn) const{
+bool QiRuleComplexAddress::match(const QString &) const{
//ignore complex address replace for now
return false;
}
-void QiRuleComplexAddress::replace(ConnectionData_ptr conn) const{
+void QiRuleComplexAddress::replace(ConnectionData_ptr) const{
}
View
2 qirulecomplexaddress.h
@@ -10,7 +10,7 @@ class QiRuleComplexAddress : public QiRule2
Q_OBJECT
public:
explicit QiRuleComplexAddress(QString name, int type, QString pattern, QString replacement, bool enable = true, bool remote = false);
- bool match(ConnectionData_ptr conn) const;
+ bool match(const QString &url) const;
void replace(ConnectionData_ptr conn) const;
signals:
View
31 qirulegroup2.cpp
@@ -10,10 +10,16 @@ QiRuleGroup2::QiRuleGroup2() :
QiRuleGroup2::QiRuleGroup2(const QiRuleGroup2 &group) :
QObject(),
+ //copy group info
_groupName(group.groupName()),
_isEnable(group.isEnable()),
_isRemote(group.isRemote())
{
+ //copy rules
+ int i, len = group.length();
+ for(i=0; i<len; i++){
+ addRule(group.getRuleAt(i));
+ }
}
QiRuleGroup2::QiRuleGroup2(QString name, bool enable, bool remote) :
@@ -46,7 +52,7 @@ void QiRuleGroup2::update(const QiRuleGroup2 &group){
update(group.groupName(), group.isEnable(), group.isRemote());
}
-void QiRuleGroup2::addRule(const QiRule2 &value, int index){
+void QiRuleGroup2::addRule(QiRule2 *value, int index){
//remove existed rule with the same name first
int existIndex = _rules.indexOf(value);
if(existIndex != -1) this->removeRuleAt(existIndex);
@@ -63,20 +69,20 @@ int QiRuleGroup2::length() const{
int QiRuleGroup2::getRuleIndex(const QString name) const{
int i, length = _rules.length();
for(i=0; i<length; i++){
- if(_rules.at(i).name() == name){
+ if(_rules.at(i)->name() == name){
return i;
}
}
return -1;
}
-QiRule2 QiRuleGroup2::getRule(const QString name) const{
+QiRule2 *QiRuleGroup2::getRule(const QString name) const{
int index = this->getRuleIndex(name);
return _rules.value(index);
}
-QiRule2 QiRuleGroup2::getRuleAt(const int index) const{
- return _rules.value(index);
+QiRule2 *QiRuleGroup2::getRuleAt(const int index) const{
+ return _rules.at(index);
}
void QiRuleGroup2::updateRule(const QString name, const QiRule2 &newValue){
@@ -87,8 +93,8 @@ void QiRuleGroup2::updateRule(const QString name, const QiRule2 &newValue){
}
void QiRuleGroup2::updateRuleAt(const int index, const QiRule2 &newValue){
- QiRule2 rule = _rules.at(index);
- rule.update(newValue);
+ QiRule2 *rule = _rules.at(index);
+ rule->update(newValue);
emit changed();
}
@@ -106,21 +112,22 @@ void QiRuleGroup2::removeRuleAt(const int index){
}
//判斷一下返回結果的isNull()
-QiRule2 QiRuleGroup2::match(ConnectionData_ptr conn) const{
+QiRule2 *QiRuleGroup2::match(const QString &url) const{
int i, length = _rules.length();
for(i=0; i<length; i++){
- if(_rules.at(i).match(conn)){
- return _rules.at(i);
+ QiRule2 *rule = _rules.at(i);
+ if(rule->match(url)){
+ return rule;
}
}
- return QiRule2();
+ return 0;
}
QString QiRuleGroup2::toJSON() const{
QStringList list;
int i, length = _rules.length();
for(i=0; i<length; i++){
- list << _rules.at(i).toJSON();
+ list << _rules.at(i)->toJSON();
}
QString result;
QTextStream(&result) << "{\"enable\":" << isEnable() << ", \"rules\":[" << list.join(", ") << "]}";
View
11 qirulegroup2.h
@@ -4,6 +4,7 @@
#include <QtCore>
#include "qirule2.h"
#include "qiconnectiondata.h"
+#include "qirulesimpleaddress.h"
class QiRuleGroup2: public QObject
{
@@ -18,16 +19,16 @@ class QiRuleGroup2: public QObject
void update(QString name, bool enable = true, bool remote = false);
void update(const QiRuleGroup2 &group);
- void addRule(const QiRule2 &value, int index = -1);
+ void addRule(QiRule2 *value, int index = -1);
int length() const;
int getRuleIndex(const QString name) const;
- QiRule2 getRule(const QString name) const;
- QiRule2 getRuleAt(const int index) const;
+ QiRule2 *getRule(const QString name) const;
+ QiRule2 *getRuleAt(const int index) const;
void updateRule(const QString name, const QiRule2 &newValue);
void updateRuleAt(const int index, const QiRule2 &newValue);
void removeRule(const QString name);
void removeRuleAt(const int index);
- QiRule2 match(ConnectionData_ptr conn) const;
+ QiRule2 *match(const QString &url) const;
QString toJSON() const;
bool isNull() const;
@@ -42,7 +43,7 @@ class QiRuleGroup2: public QObject
QString _groupName;
bool _isEnable;
bool _isRemote;
- QList<QiRule2> _rules;
+ QList<QiRule2 *> _rules;
};
#endif // QIRULEGROUP2_H
View
6 qirulelocaldir.cpp
@@ -6,10 +6,10 @@ QiRuleLocalDir::QiRuleLocalDir(QString name, int type, QString pattern, QString
}
-bool QiRuleLocalDir::match(ConnectionData_ptr conn) const{
- return (conn->fullUrl.indexOf(pattern()) != -1);
+bool QiRuleLocalDir::match(const QString &url) const{
+ return (url.indexOf(pattern()) != -1);
}
void QiRuleLocalDir::replace(ConnectionData_ptr conn) const{
-
+ qDebug() << "checking local dir rule" << pattern();
}
View
2 qirulelocaldir.h
@@ -10,7 +10,7 @@ class QiRuleLocalDir : public QiRule2
Q_OBJECT
public:
explicit QiRuleLocalDir(QString name, int type, QString pattern, QString replacement, bool enable = true, bool remote = false);
- bool match(ConnectionData_ptr conn) const;
+ bool match(const QString &url) const;
void replace(ConnectionData_ptr conn) const;
signals:
View
6 qirulelocalfile.cpp
@@ -6,11 +6,11 @@ QiRuleLocalFile::QiRuleLocalFile(QString name, int type, QString pattern, QStrin
}
-bool QiRuleLocalFile::match(ConnectionData_ptr conn) const{
+bool QiRuleLocalFile::match(const QString &url) const{
QRegExp rx(pattern(), Qt::CaseInsensitive, QRegExp::Wildcard);
- return rx.exactMatch(conn->fullUrl);
+ return rx.exactMatch(url);
}
void QiRuleLocalFile::replace(ConnectionData_ptr conn) const{
-
+ qDebug() << "checking local file rule" << pattern();
}
View
2 qirulelocalfile.h
@@ -10,7 +10,7 @@ class QiRuleLocalFile : public QiRule2
Q_OBJECT
public:
explicit QiRuleLocalFile(QString name, int type, QString pattern, QString replacement, bool enable = true, bool remote = false);
- bool match(ConnectionData_ptr conn) const;
+ bool match(const QString &url) const;
void replace(ConnectionData_ptr conn) const;
signals:
View
6 qirulelocalfiles.cpp
@@ -6,11 +6,11 @@ QiRuleLocalFiles::QiRuleLocalFiles(QString name, int type, QString pattern, QStr
}
-bool QiRuleLocalFiles::match(ConnectionData_ptr conn) const{
+bool QiRuleLocalFiles::match(const QString &url) const{
QRegExp rx(pattern(), Qt::CaseInsensitive, QRegExp::Wildcard);
- return rx.exactMatch(conn->fullUrl);
+ return rx.exactMatch(url);
}
void QiRuleLocalFiles::replace(ConnectionData_ptr conn) const{
-
+ qDebug() << "checking local files rule" << pattern();
}
View
2 qirulelocalfiles.h
@@ -10,7 +10,7 @@ class QiRuleLocalFiles : public QiRule2
Q_OBJECT
public:
explicit QiRuleLocalFiles(QString name, int type, QString pattern, QString replacement, bool enable = true, bool remote = false);
- bool match(ConnectionData_ptr conn) const;
+ bool match(const QString &url) const;
void replace(ConnectionData_ptr conn) const;
signals:
View
81 qirulemanager2.cpp
@@ -28,40 +28,52 @@ QString QiRuleManager2::remoteConfigURL(){
return "http://" + host + remotePath;
}
-QList<QiRuleGroup2> QiRuleManager2::parseConfigContent(QString json, bool remote){
+QList<QiRuleGroup2 *> *QiRuleManager2::parseConfigContent(QString json, bool remote){
qDebug() << "[RuleManager] parsing config content";
- QList<QiRuleGroup2> groups;
+ QList<QiRuleGroup2 *> *groups = new QList<QiRuleGroup2 *>();
QScriptEngine engine;
QScriptValue value = engine.evaluate("(" + json + ")");
QScriptValueIterator groupsIt(value);
while(groupsIt.hasNext()){
groupsIt.next();
- qDebug() << "[RuleManager] group:" << groupsIt.name();
//constructor the rule group
- QiRuleGroup2 group(groupsIt.name(), groupsIt.value().property("enable").toBool(), remote);
+ QiRuleGroup2 *group = new QiRuleGroup2(groupsIt.name(), groupsIt.value().property("enable").toBool(), remote);
QScriptValueIterator rulesIt(groupsIt.value().property("rules"));
while(rulesIt.hasNext()){
rulesIt.next();
//constructor the rule
+ QiRule2 *rule = 0;
QScriptValue r = rulesIt.value();
+ QString rName = r.property("name").toString();
+ bool rEnable = r.property("enable").toBool();
int rType = r.property("type").toInt32();
+ QString rPattern = r.property("rule").property("pattern").toString();
+ QString rReplace = r.property("rule").property("replace").toString();
switch(rType){
case COMPLEX_ADDRESS_REPLACE:
//ignore complex address replace rule
break;
-
- default:
- QiRule2 rule(
- r.property("name").toString(),
- rType,
- r.property("rule").property("pattern").toString(),
- r.property("rule").property("replace").toString(),
- remote
- );
- group.addRule(rule);
- qDebug() << rule.toJSON();
+ case SIMPLE_ADDRESS_REPLACE:
+ rule = new QiRuleSimpleAddress(rName, rType, rPattern, rReplace, rEnable, remote);
+ break;
+ case REMOTE_CONTENT_REPLACE:
+ rule = new QiRuleRemoteContent(rName, rType, rPattern, rReplace, rEnable, remote);
+ break;
+ case LOCAL_FILE_REPLACE:
+ rule = new QiRuleLocalFile(rName, rType, rPattern, rReplace, rEnable, remote);
+ break;
+ case LOCAL_FILES_REPLACE:
+ rule = new QiRuleLocalFiles(rName, rType, rPattern, rReplace, rEnable, remote);
+ break;
+ case LOCAL_DIR_REPLACE:
+ rule = new QiRuleLocalDir(rName, rType, rPattern, rReplace, rEnable, remote);
+ break;
+ }
+ if(rule){
+ group->addRule(rule);
}
}
+ groups->append(group);
}
return groups;
}
@@ -74,8 +86,8 @@ void QiRuleManager2::loadLocalConfig(){
if(file.open(QIODevice::ReadOnly)){
QTextStream stream(&file);
QString content = stream.readAll();
- QList<QiRuleGroup2> groups = parseConfigContent(content, false);
- localGroups.append(groups);
+ QList<QiRuleGroup2 *> *groups = parseConfigContent(content, false);
+ localGroups.append(*groups);
file.close();
emit localConfigLoaded();
}
@@ -110,7 +122,7 @@ void QiRuleManager2::saveLocalConfigChanges() const{
QStringList groups;
int i, length = localGroups.length();
for(i=0; i<length; i++){
- groups << ("\"" + localGroups.at(i).groupName() + "\":" + localGroups.at(i).toJSON());
+ groups << ("\"" + localGroups.at(i)->groupName() + "\":" + localGroups.at(i)->toJSON());
}
QTextStream stream(&file);
@@ -129,48 +141,49 @@ void QiRuleManager2::saveLocalConfigChanges() const{
}
}
-void QiRuleManager2::addRuleGroup(const QiRuleGroup2 &value, int index){
- QList<QiRuleGroup2> &list = value.isRemote() ? remoteGroups : localGroups;
+void QiRuleManager2::addRuleGroup(QiRuleGroup2 *value, int index){
+ QList<QiRuleGroup2 *> &list = value->isRemote() ? remoteGroups : localGroups;
int existGroup = list.indexOf(value);
if(existGroup != -1) list.removeAt(existGroup);
if(index == -1) list.append(value);
else list.insert(index, value);
}
-QiRule2 QiRuleManager2::findMatchInGroups(ConnectionData_ptr connectionData, const QString &groupName, const QList<QiRuleGroup2> &list) const{
+QiRule2 *QiRuleManager2::findMatchInGroups(const QString &url, const QString &groupName, const QList<QiRuleGroup2 *> &list) const{
int i, len = list.length();
for(i=0; i<len; i++){
- QiRuleGroup2 group = list.at(i);
- if((groupName.length() && groupName == group.groupName()) || !groupName.length()){
- QiRule2 rule = group.match(connectionData);
- if(!rule.isNull()) return rule;
+ QiRuleGroup2 *group = list.at(i);
+ if((groupName.length() && groupName == group->groupName()) || !groupName.length()){
+ QiRule2 *rule = group->match(url);
+ if(rule && !rule->isNull()) return rule;
}
}
- return QiRule2();
+ return 0;
}
-QiRule2 QiRuleManager2::getMatchRule(ConnectionData_ptr connectionData, const QString &groupName) const{
- QiRule2 localMatch = findMatchInGroups(connectionData, groupName, localGroups);
- if(localMatch.isNull()) return findMatchInGroups(connectionData, groupName, remoteGroups);
+QiRule2 *QiRuleManager2::getMatchRule(const QString &url, const QString &groupName) const{
+ qDebug() << "[RuleManager] finding match rule ...";
+ QiRule2 *localMatch = findMatchInGroups(url, groupName, localGroups);
+ if(!localMatch) return findMatchInGroups(url, groupName, remoteGroups);
else return localMatch;
}
void QiRuleManager2::replace(ConnectionData_ptr connectionData) const{
- QiRule2 rule = getMatchRule(connectionData);
+ QiRule2 *rule = getMatchRule(connectionData->fullUrl);
replace(connectionData, rule);
}
-void QiRuleManager2::replace(ConnectionData_ptr connectionData, const QiRule2 &rule) const{
- if(!rule.isNull()){
- rule.replace(connectionData);
+void QiRuleManager2::replace(ConnectionData_ptr connectionData, const QiRule2 *rule) const{
+ if(!rule->isNull()){
+ rule->replace(connectionData);
}
}
void QiRuleManager2::onRemoteConfigLoaded(int id, bool error){
qDebug() << "[RuleManager] remote config loaded" << id << error;
if(!error){
QString content = remoteConfigLoader.readAll();
- remoteGroups.append(parseConfigContent(content, true));
+ remoteGroups.append(*parseConfigContent(content, true));
emit remoteConfigLoaded();
}
}
View
20 qirulemanager2.h
@@ -4,6 +4,12 @@
#include <QtCore>
#include <QHttp>
#include "qirule2.h"
+#include "qirulecomplexaddress.h"
+#include "qirulesimpleaddress.h"
+#include "qiruleremotecontent.h"
+#include "qirulelocalfile.h"
+#include "qirulelocalfiles.h"
+#include "qirulelocaldir.h"
#include "qirulegroup2.h"
#include "qipipe.h"
#include "qiconnectiondata.h"
@@ -22,18 +28,18 @@ class QiRuleManager2 : public QObject
void loadConfig();
void saveLocalConfigChanges() const;
- void addRuleGroup(const QiRuleGroup2 &value, int index = -1);
+ void addRuleGroup(QiRuleGroup2 *value, int index = -1);
- QiRule2 getMatchRule(ConnectionData_ptr connectionData, const QString &groupName = "") const;
+ QiRule2 *getMatchRule(const QString &url, const QString &groupName = "") const;
void replace(ConnectionData_ptr connectionData) const;
- void replace(ConnectionData_ptr connectionData, const QiRule2 &rule) const;
+ void replace(ConnectionData_ptr connectionData, const QiRule2 *rule) const;
QString localConfigFile;
QString remoteHost;
QString remoteAddress;
QString remotePath;
- QList<QiRuleGroup2> localGroups;
- QList<QiRuleGroup2> remoteGroups;
+ QList<QiRuleGroup2 *> localGroups;
+ QList<QiRuleGroup2 *> remoteGroups;
signals:
void localConfigLoaded();
@@ -44,8 +50,8 @@ public slots:
private:
QHttp remoteConfigLoader;
- QList<QiRuleGroup2> parseConfigContent(QString json, bool remote = false);
- QiRule2 findMatchInGroups(ConnectionData_ptr connectionData, const QString &groupName, const QList<QiRuleGroup2> &list) const;
+ QList<QiRuleGroup2 *> *parseConfigContent(QString json, bool remote = false);
+ QiRule2 *findMatchInGroups(const QString &url, const QString &groupName, const QList<QiRuleGroup2 *> &list) const;
};
View
6 qiruleremotecontent.cpp
@@ -6,11 +6,11 @@ QiRuleRemoteContent::QiRuleRemoteContent(QString name, int type, QString pattern
}
-bool QiRuleRemoteContent::match(ConnectionData_ptr conn) const{
+bool QiRuleRemoteContent::match(const QString &url) const{
QRegExp rx(pattern(), Qt::CaseInsensitive, QRegExp::Wildcard);
- return rx.exactMatch(conn->fullUrl);
+ return rx.exactMatch(url);
}
void QiRuleRemoteContent::replace(ConnectionData_ptr conn) const{
-
+ qDebug() << "checking remote content rule" << pattern();
}
View
2 qiruleremotecontent.h
@@ -10,7 +10,7 @@ class QiRuleRemoteContent : public QiRule2
Q_OBJECT
public:
explicit QiRuleRemoteContent(QString name, int type, QString pattern, QString replacement, bool enable = true, bool remote = false);
- bool match(ConnectionData_ptr conn) const;
+ bool match(const QString &url) const;
void replace(ConnectionData_ptr conn) const;
signals:
View
9 qirulesimpleaddress.cpp
@@ -1,14 +1,17 @@
#include "qirulesimpleaddress.h"
+#include <QUrl>
QiRuleSimpleAddress::QiRuleSimpleAddress(QString name, int type, QString pattern, QString replacement, bool enable, bool remote) :
QiRule2(name, type, pattern, replacement, enable, remote)
{
}
-bool QiRuleSimpleAddress::match(ConnectionData_ptr conn) const{
- return conn->host == pattern();
+bool QiRuleSimpleAddress::match(const QString &url) const{
+ QUrl fullUrl(url);
+ QUrl patternUrl(pattern());
+ return fullUrl.host() == patternUrl.host();
}
void QiRuleSimpleAddress::replace(ConnectionData_ptr conn) const{
-
+ qDebug() << "checking simple address rule" << pattern();
}
View
2 qirulesimpleaddress.h
@@ -10,7 +10,7 @@ class QiRuleSimpleAddress : public QiRule2
Q_OBJECT
public:
explicit QiRuleSimpleAddress(QString name, int type, QString pattern, QString replacement, bool enable = true, bool remote = false);
- bool match(ConnectionData_ptr conn) const;
+ bool match(const QString &url) const;
void replace(ConnectionData_ptr conn) const;
signals:

0 comments on commit 25402ea

Please sign in to comment.
Something went wrong with that request. Please try again.