--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Schema.h"
+
+using namespace ctx;
+
+Schema::Schema() :
+ retention(0),
+ limit(0)
+{
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_STORE_SCHEMA_H__
+#define __CONTEXT_STORE_SCHEMA_H__
+
+#include <utility>
+#include <string>
+#include <vector>
+#include <ContextStoreTypesPrivate.h>
+
+namespace ctx {
+
+ class Schema {
+ public:
+ enum class AttributeType {
+ UNDEFINED = 0,
+ INTEGER = 1,
+ REAL = 2,
+ TEXT = 3
+ };
+
+ Schema();
+
+ std::string uri;
+ unsigned int retention;
+ unsigned int limit;
+ std::vector<std::string> readPrivileges;
+ std::vector<std::string> writePrivileges;
+ std::vector<std::pair<std::string, AttributeType>> attributes;
+ };
+
+}
+
+#endif
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#include "Schema.h"
+#include "SchemaChecker.h"
+
+#define COL_REGEX "^[A-Za-z]+\\w*$"
+
+using namespace ctx;
+
+SchemaChecker::SchemaChecker() :
+ __uriRegex(URI_REGEX("contextstore"), std::regex::optimize),
+ __privilegeRegex(URI_REGEX("privilege"), std::regex::optimize),
+ __columnNameRegex(COL_REGEX, std::regex::optimize)
+{
+}
+
+bool SchemaChecker::validate(const Schema& schema)
+{
+ if (schema.retention > MAX_RETENTION || schema.limit > MAX_LIMIT)
+ return false;
+
+ if (schema.retention == 0 && schema.limit == 0)
+ return false;
+
+ if (schema.attributes.empty())
+ return false;
+
+ if (!std::regex_match(schema.uri, __uriRegex))
+ return false;
+
+ for (auto& privil : schema.readPrivileges) {
+ if (!std::regex_match(privil, __privilegeRegex))
+ return false;
+ }
+
+ for (auto& privil : schema.writePrivileges) {
+ if (!std::regex_match(privil, __privilegeRegex))
+ return false;
+ }
+
+ for (auto& attr: schema.attributes) {
+ if (!std::regex_match(attr.first, __columnNameRegex))
+ return false;
+ }
+
+ return true;
+}
--- /dev/null
+/*
+ * Copyright (c) 2017 Samsung Electronics Co., Ltd.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef __CONTEXT_STORE_SCHEMA_CHECKER_H__
+#define __CONTEXT_STORE_SCHEMA_CHECKER_H__
+
+#include <regex>
+#include <ContextStoreTypesPrivate.h>
+
+namespace ctx {
+
+ class Schema;
+
+ class SchemaChecker {
+ public:
+ SchemaChecker();
+
+ bool validate(const Schema& schema);
+
+ private:
+ std::regex __uriRegex;
+ std::regex __privilegeRegex;
+ std::regex __columnNameRegex;
+ };
+
+}
+
+#endif
*/
#include <cstdio>
+#include <utility>
#include <fstream>
#include <sstream>
#include <regex>
#include <dirent.h>
#include <PathUtil.h>
#include "DatabaseManager.h"
+#include "Schema.h"
+#include "SchemaChecker.h"
#include "SchemaLoader.h"
/* Schema XML example:
*/
-#define COL_REGEX "^[A-Za-z]+\\w*$"
-
#define CHK_NAME(NODE, NAME) (!xmlStrcmp(NODE->name, (const xmlChar*)(NAME)))
using namespace ctx;
return out;
}
-Schema::Schema() :
- retention(0),
- limit(0)
-{
-}
-
-bool Schema::valid()
-{
- if (retention > MAX_RETENTION || limit > MAX_LIMIT)
- return false;
-
- if (retention == 0 && limit == 0)
- return false;
-
- if (attributes.empty())
- return false;
-
- std::regex uriRegex(URI_REGEX("contextstore"), std::regex::optimize);
- if (!std::regex_match(uri, uriRegex))
- return false;
-
- std::regex privilegeRegex(URI_REGEX("privilege"), std::regex::optimize);
- for (auto& privil : readPrivileges) {
- if (!std::regex_match(privil, privilegeRegex))
- return false;
- }
-
- for (auto& privil : writePrivileges) {
- if (!std::regex_match(privil, privilegeRegex))
- return false;
- }
-
- std::regex columnNameRegex(COL_REGEX, std::regex::optimize);
- for (auto& attr: attributes) {
- if (!std::regex_match(attr.first, columnNameRegex))
- return false;
- }
-
- return true;
-}
-
-SchemaLoader::SchemaLoader()
+SchemaLoader::SchemaLoader() :
+ __schemaChecker(NULL)
{
}
SchemaLoader::~SchemaLoader()
{
+ delete __schemaChecker;
}
bool SchemaLoader::load()
if (!limit.empty())
schema.limit = std::atoi(limit.c_str());
- return schema.valid();
+ SchemaChecker* checker = __getSchemaChecker();
+ IF_FAIL_RETURN(checker, false);
+
+ return checker->validate(schema);
}
void SchemaLoader::__parsePrivilege(xmlNode* node, Schema& schema)
schema.attributes.push_back(std::make_pair(name, Schema::AttributeType::TEXT));
}
+SchemaChecker* SchemaLoader::__getSchemaChecker()
+{
+ if (__schemaChecker)
+ return __schemaChecker;
+
+ try {
+ __schemaChecker = new SchemaChecker;
+ } catch (std::exception& e) {
+ _E("Exception: %s", e.what());
+ return NULL;
+ }
+
+ return __schemaChecker;
+}
+
void SchemaLoader::__insertMetadata(Schema& schema)
{
char* updateSql = NULL;
#ifndef __CONTEXT_STORE_SCHEMA_LOADER_H__
#define __CONTEXT_STORE_SCHEMA_LOADER_H__
-#include <utility>
#include <string>
#include <vector>
#include <libxml/tree.h>
namespace ctx {
- class Schema {
- public:
- enum class AttributeType {
- UNDEFINED = 0,
- INTEGER = 1,
- REAL = 2,
- TEXT = 3
- };
-
- Schema();
- bool valid();
-
- std::string uri;
- unsigned int retention;
- unsigned int limit;
- std::vector<std::string> readPrivileges;
- std::vector<std::string> writePrivileges;
- std::vector<std::pair<std::string, AttributeType>> attributes;
- };
+ class Schema;
+ class SchemaChecker;
class SchemaLoader {
public:
void __parseAttribute(xmlNode* node, Schema& schema);
void __insertMetadata(Schema& schema);
void __createStoreTable(Schema& schema);
+ SchemaChecker* __getSchemaChecker();
virtual std::string __getSchemaDir() = 0;
virtual Database& __getDatabase() = 0;
+ SchemaChecker* __schemaChecker;
+
protected:
SchemaLoader();
};