| 1 |
/* |
| 2 |
* Copyright (C) 2013-2015, 2019 by the Konclude Developer Team. |
| 3 |
* |
| 4 |
* This file is part of the reasoning system Konclude. |
| 5 |
* For details and support, see <http://konclude.com/>. |
| 6 |
* |
| 7 |
* Konclude is free software: you can redistribute it and/or modify |
| 8 |
* it under the terms of version 3 of the GNU Lesser General Public |
| 9 |
* License (LGPLv3) as published by the Free Software Foundation. |
| 10 |
* |
| 11 |
* Konclude is distributed in the hope that it will be useful, |
| 12 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 13 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 14 |
* GNU (Lesser) General Public License for more details. |
| 15 |
* |
| 16 |
* You should have received a copy of the GNU (Lesser) General Public |
| 17 |
* License along with Konclude. If not, see <http://www.gnu.org/licenses/>. |
| 18 |
* |
| 19 |
*/ |
| 20 |
|
| 21 |
#include "CRDFSimpleTurtleParser.h" |
| 22 |
|
| 23 |
|
| 24 |
namespace Konclude { |
| 25 |
|
| 26 |
namespace Parser { |
| 27 |
|
| 28 |
|
| 29 |
|
| 30 |
CRDFSimpleTurtleParser::CRDFSimpleTurtleParser() { |
| 31 |
|
| 32 |
mPrefixHash.insert("rdf", "http://www.w3.org/1999/02/22-rdf-syntax-ns#"); |
| 33 |
nextAnonymousNodeId = 0; |
| 34 |
|
| 35 |
} |
| 36 |
|
| 37 |
|
| 38 |
CRDFSimpleTurtleParser::~CRDFSimpleTurtleParser() { |
| 39 |
} |
| 40 |
|
| 41 |
|
| 42 |
QString CRDFSimpleTurtleParser::getBlankNode(const QString& blankNodeString) { |
| 43 |
return blankNodeString.mid(2); |
| 44 |
} |
| 45 |
|
| 46 |
|
| 47 |
QString CRDFSimpleTurtleParser::getNextAnonymousNode() { |
| 48 |
return QString("_:konclude_internal_anonymous_node_%1").arg(nextAnonymousNodeId++); |
| 49 |
} |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
QString CRDFSimpleTurtleParser::getIRI(const QString& iriString) { |
| 54 |
if (iriString.startsWith("<") && iriString.endsWith(">")) { |
| 55 |
return iriString.mid(1, iriString.length() - 2); |
| 56 |
} else { |
| 57 |
int colonPos = iriString.indexOf(':'); |
| 58 |
if (colonPos != -1) { |
| 59 |
QString prefix = iriString.mid(0, colonPos); |
| 60 |
QString prefixResolvedString = mPrefixHash.value(prefix); |
| 61 |
if (!prefixResolvedString.isEmpty()) { |
| 62 |
QString remainingIRI = iriString.mid(colonPos+1); |
| 63 |
if (!remainingIRI.isEmpty()) { |
| 64 |
return prefixResolvedString + remainingIRI; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
|
| 69 |
return mBaseIRI + iriString; |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
|
| 74 |
QStringList CRDFSimpleTurtleParser::getBracketContent(QStringList& partList, QChar openingBracket, QChar closingBracket, bool bracketOpened, bool includeOthers) { |
| 75 |
int bracketOpen = 0; |
| 76 |
if (bracketOpened) { |
| 77 |
bracketOpen++; |
| 78 |
} |
| 79 |
QStringList splittedPartList; |
| 80 |
while (!partList.isEmpty()) { |
| 81 |
QString partString = partList.takeFirst(); |
| 82 |
|
| 83 |
|
| 84 |
bool isOpeningBracket = false; |
| 85 |
bool isBracket = false; |
| 86 |
bool isClosingBracket = false; |
| 87 |
|
| 88 |
|
| 89 |
if (partString == openingBracket) { |
| 90 |
bracketOpened = true; |
| 91 |
++bracketOpen; |
| 92 |
isOpeningBracket = true; |
| 93 |
} else if (partString == closingBracket) { |
| 94 |
--bracketOpen; |
| 95 |
isClosingBracket = true; |
| 96 |
} |
| 97 |
|
| 98 |
if (isOpeningBracket || isClosingBracket) { |
| 99 |
isBracket = true; |
| 100 |
} |
| 101 |
|
| 102 |
if (includeOthers || !isBracket && bracketOpened) { |
| 103 |
splittedPartList.append(partString); |
| 104 |
} |
| 105 |
|
| 106 |
|
| 107 |
if (bracketOpen == 0 && bracketOpened) { |
| 108 |
return splittedPartList; |
| 109 |
} |
| 110 |
} |
| 111 |
if (includeOthers) { |
| 112 |
return splittedPartList; |
| 113 |
} else { |
| 114 |
return QStringList(); |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
bool CRDFSimpleTurtleParser::getNextRDFStringTriple(QStringList& partList, CRDFStringTriple& lastNextTriple, QList<CRDFStringTriple>& nextTripleList, int startingSopPos) { |
| 120 |
int sopPos = startingSopPos; |
| 121 |
bool maybeLastParsingTerminator = true; |
| 122 |
while (!partList.isEmpty()) { |
| 123 |
if (partList.first() == "." && !nextTripleList.isEmpty()) { |
| 124 |
lastNextTriple = nextTripleList.takeFirst(); |
| 125 |
return true; |
| 126 |
} |
| 127 |
QString part = partList.takeFirst(); |
| 128 |
bool isTriplePart = false; |
| 129 |
if (part == ".") { |
| 130 |
if (sopPos != 3) { |
| 131 |
if (maybeLastParsingTerminator) { |
| 132 |
maybeLastParsingTerminator = false; |
| 133 |
} else { |
| 134 |
mParsingError = true; |
| 135 |
mLastParsingError = QString("RDF triple terminator '.' is not at correct position"); |
| 136 |
} |
| 137 |
} |
| 138 |
sopPos = 0; |
| 139 |
} else if (part == ";") { |
| 140 |
if (sopPos != 3) { |
| 141 |
if (maybeLastParsingTerminator) { |
| 142 |
maybeLastParsingTerminator = false; |
| 143 |
} else { |
| 144 |
mParsingError = true; |
| 145 |
mLastParsingError = QString("RDF object concatenator ';' is not at correct position"); |
| 146 |
} |
| 147 |
} |
| 148 |
sopPos = 1; |
| 149 |
} else if (part == ",") { |
| 150 |
if (sopPos != 3) { |
| 151 |
if (maybeLastParsingTerminator) { |
| 152 |
maybeLastParsingTerminator = false; |
| 153 |
} else { |
| 154 |
mParsingError = true; |
| 155 |
mLastParsingError = QString("RDF predicate object concatenator ',' is not at correct position"); |
| 156 |
} |
| 157 |
} |
| 158 |
sopPos = 2; |
| 159 |
} else if (part.startsWith("?") || part.startsWith("$")) { |
| 160 |
isTriplePart = true; |
| 161 |
} else if (part == "{") { |
| 162 |
getBracketContent(partList, '{', '}', true); |
| 163 |
sopPos = 0; |
| 164 |
} else if (part == "[") { |
| 165 |
part = getNextAnonymousNode(); |
| 166 |
QStringList anonymousNodeTripleParts = getBracketContent(partList, '[', ']', true); |
| 167 |
QList<CRDFStringTriple> tmpNextTripleList; |
| 168 |
CRDFStringTriple lastTriple; |
| 169 |
lastTriple.string[0] = part; |
| 170 |
while (getNextRDFStringTriple(anonymousNodeTripleParts, lastTriple, tmpNextTripleList, 1)) { |
| 171 |
nextTripleList.append(lastTriple); |
| 172 |
} |
| 173 |
isTriplePart = true; |
| 174 |
} else if (part == "_:") { |
| 175 |
part = getBlankNode(part); |
| 176 |
isTriplePart = true; |
| 177 |
} else { |
| 178 |
part = getIRI(part); |
| 179 |
isTriplePart = true; |
| 180 |
} |
| 181 |
if (isTriplePart) { |
| 182 |
if (sopPos <= 2) { |
| 183 |
lastNextTriple.string[sopPos] = part; |
| 184 |
} else { |
| 185 |
mParsingError = true; |
| 186 |
mLastParsingError = QString("RDF triple not terminated"); |
| 187 |
} |
| 188 |
++sopPos; |
| 189 |
if (sopPos >= 3) { |
| 190 |
return true; |
| 191 |
} |
| 192 |
} |
| 193 |
} |
| 194 |
if (!nextTripleList.isEmpty()) { |
| 195 |
lastNextTriple = nextTripleList.takeFirst(); |
| 196 |
return true; |
| 197 |
} |
| 198 |
return false; |
| 199 |
|
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
QPair<QString, QString> CRDFSimpleTurtleParser::extractDataLiteral(const QString& string) { |
| 205 |
QString dataString = string; |
| 206 |
QString dataLexicalValueString; |
| 207 |
QString datatypeString = mParsingUtilities.DATATYPE_XML_STRING; |
| 208 |
if (dataString.contains("^^")) { |
| 209 |
QStringList splittedParts = dataString.split("^^"); |
| 210 |
dataString = splittedParts.first(); |
| 211 |
datatypeString = getIRI(splittedParts.last()); |
| 212 |
} else { |
| 213 |
int lastLanguageTagSeparator = dataString.lastIndexOf("@"); |
| 214 |
if (lastLanguageTagSeparator >= 0 && lastLanguageTagSeparator < dataString.length() - 2) { |
| 215 |
if (mParsingUtilities.matchesLanguageTag(dataString.mid(lastLanguageTagSeparator + 1))) { |
| 216 |
datatypeString = mParsingUtilities.DATATYPE_RDF_PLAINLITERAL; |
| 217 |
} |
| 218 |
} |
| 219 |
} |
| 220 |
if (dataString.startsWith("\"") && dataString.endsWith("\"") || dataString.startsWith("'") && dataString.endsWith("'")) { |
| 221 |
dataString.remove(dataString.length()-1, 1); |
| 222 |
dataString.remove(0, 1); |
| 223 |
} |
| 224 |
return QPair<QString, QString>(dataString, datatypeString); |
| 225 |
} |
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
QHash<QString, CRDFHahsedSubjectData>* CRDFSimpleTurtleParser::getRDFSubjectHash(QStringList& partList) { |
| 230 |
QHash<QString, CRDFHahsedSubjectData>* subjectHash = new QHash<QString, CRDFHahsedSubjectData>(); |
| 231 |
QList<CRDFStringTriple> nextTripleList; |
| 232 |
|
| 233 |
CRDFStringTriple lastTriple; |
| 234 |
while (getNextRDFStringTriple(partList,lastTriple, nextTripleList)) { |
| 235 |
subjectHash->insertMulti(lastTriple.string[0],CRDFHahsedSubjectData(lastTriple)); |
| 236 |
} |
| 237 |
|
| 238 |
return subjectHash; |
| 239 |
} |
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
QList<CRDFStringTriple>* CRDFSimpleTurtleParser::getRDFStringTriples(QStringList& partList) { |
| 244 |
QList<CRDFStringTriple>* tripleList = new QList<CRDFStringTriple>(); |
| 245 |
QList<CRDFStringTriple> nextTripleList; |
| 246 |
|
| 247 |
CRDFStringTriple lastTriple; |
| 248 |
while (getNextRDFStringTriple(partList, lastTriple, nextTripleList)) { |
| 249 |
tripleList->append(lastTriple); |
| 250 |
} |
| 251 |
|
| 252 |
return tripleList; |
| 253 |
} |
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
QString CRDFSimpleTurtleParser::getNextPart(const QString& text, cint64& pos) { |
| 259 |
cint64 startPos = pos; |
| 260 |
cint64 size = text.size(); |
| 261 |
bool hasNonSeparator = false; |
| 262 |
bool comment = false; |
| 263 |
bool string = false; |
| 264 |
while (pos < size) { |
| 265 |
QChar textChar = text.at(pos); |
| 266 |
bool isSeperator = false; |
| 267 |
bool isBracket = false; |
| 268 |
if (comment && (textChar == '\r' || textChar == '\n')) { |
| 269 |
comment = false; |
| 270 |
startPos = pos; |
| 271 |
} |
| 272 |
if (textChar == '#' && !hasNonSeparator) { |
| 273 |
comment = true; |
| 274 |
} |
| 275 |
if (!comment) { |
| 276 |
if (textChar == "\"") { |
| 277 |
string = !string; |
| 278 |
} |
| 279 |
if (!string) { |
| 280 |
if (textChar == ' ' || textChar == '\t' || textChar == '\r' || textChar == '\n') { |
| 281 |
isSeperator = true; |
| 282 |
} |
| 283 |
if (textChar == '{' || textChar == '}' || textChar == '(' || textChar == ')' || textChar == '[' || textChar == ']') { |
| 284 |
isBracket = true; |
| 285 |
isSeperator = true; |
| 286 |
} |
| 287 |
} |
| 288 |
if (isSeperator) { |
| 289 |
if (hasNonSeparator) { |
| 290 |
QString partString = text.mid(startPos, pos - startPos); |
| 291 |
return partString.trimmed(); |
| 292 |
} |
| 293 |
if (isBracket) { |
| 294 |
++pos; |
| 295 |
return textChar; |
| 296 |
} |
| 297 |
} else { |
| 298 |
if (!hasNonSeparator) { |
| 299 |
hasNonSeparator = true; |
| 300 |
startPos = pos; |
| 301 |
} |
| 302 |
} |
| 303 |
} |
| 304 |
++pos; |
| 305 |
} |
| 306 |
if (!comment) { |
| 307 |
return text.mid(startPos).trimmed(); |
| 308 |
} else { |
| 309 |
return QString(); |
| 310 |
} |
| 311 |
} |
| 312 |
|
| 313 |
|
| 314 |
|
| 315 |
|
| 316 |
}; // end namespace Parser |
| 317 |
|
| 318 |
}; // end namespace Konclude |