Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix splitting of XPath expressions into steps #221

Merged
merged 3 commits into from
Oct 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions impexp-client-gui/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ configurations {

dependencies {
api project(':impexp-client-cli')
api 'com.formdev:flatlaf:1.6'
api 'com.formdev:flatlaf-extras:1.6'
api 'com.formdev:flatlaf-swingx:1.6'
api 'com.formdev:flatlaf:1.6.1'
api 'com.formdev:flatlaf-extras:1.6.1'
api 'com.formdev:flatlaf-swingx:1.6.1'
api 'com.fifesoft:rsyntaxtextarea:3.1.3'
api ('org.citydb:swingx-ws:1.1.5') {
transitive = false
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,33 +27,15 @@
*/
package org.citydb.core.database.schema.util;

import org.citydb.core.database.schema.mapping.AbstractAttribute;
import org.citydb.core.database.schema.mapping.AbstractPathElement;
import org.citydb.core.database.schema.mapping.AbstractProperty;
import org.citydb.core.database.schema.mapping.AbstractType;
import org.citydb.core.database.schema.mapping.AbstractTypeProperty;
import org.citydb.core.database.schema.mapping.ComplexAttribute;
import org.citydb.core.database.schema.mapping.ComplexAttributeType;
import org.citydb.core.database.schema.mapping.FeatureType;
import org.citydb.core.database.schema.mapping.PathElementType;
import org.citydb.core.database.schema.mapping.SchemaMapping;
import org.citydb.core.database.schema.mapping.SimpleAttribute;
import org.citydb.core.database.schema.mapping.SimpleType;
import org.citydb.core.database.schema.mapping.*;
import org.citydb.core.database.schema.path.AbstractNodePredicate;
import org.citydb.core.database.schema.path.FeatureTypeNode;
import org.citydb.core.database.schema.path.InvalidSchemaPathException;
import org.citydb.core.database.schema.path.SchemaPath;
import org.citydb.core.database.schema.path.predicate.comparison.EqualToPredicate;
import org.citydb.core.database.schema.path.predicate.logical.BinaryLogicalPredicate;
import org.citydb.core.database.schema.path.predicate.logical.LogicalPredicateName;
import org.citydb.core.query.filter.selection.expression.AbstractLiteral;
import org.citydb.core.query.filter.selection.expression.BooleanLiteral;
import org.citydb.core.query.filter.selection.expression.DateLiteral;
import org.citydb.core.query.filter.selection.expression.DoubleLiteral;
import org.citydb.core.query.filter.selection.expression.LiteralType;
import org.citydb.core.query.filter.selection.expression.LongLiteral;
import org.citydb.core.query.filter.selection.expression.StringLiteral;
import org.citydb.core.query.filter.selection.expression.TimestampLiteral;
import org.citydb.core.query.filter.selection.expression.*;
import org.citydb.core.registry.ObjectRegistry;

import javax.xml.XMLConstants;
Expand All @@ -62,14 +44,16 @@
import javax.xml.datatype.XMLGregorianCalendar;
import javax.xml.namespace.NamespaceContext;
import javax.xml.namespace.QName;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class SimpleXPathParser {
// this parser only supports XPath subset as defined in OGC 09-026r2, Annex D
private final String simplifiedStepExpr_ = "([^\\[]+)(?:\\[(.+)\\]$)?";
private final String simplifiedStepExpr_ = "([^\\[]+)(?:\\[(.+)]$)?";
private final String nameChar_ = "[_a-zA-Z][\\w\\-.]*";
private final String stringLiteral_ = "\"(?:(?:\"\")|[^\"])*?\"|'(?:(?:'')|[^\"])*?'";
private final String stringLiteral_ = "\"(?:\"\"|[^\"])*?\"|'(?:''|[^'])*?'";
private final String qName_ = "(?:(" + nameChar_ + "):)??(" + nameChar_ + ")";
private final String nameTest_ = "@?" + qName_;
private final String kindTest_ = "schema-element\\(" + qName_ + "\\)";
Expand Down Expand Up @@ -105,8 +89,8 @@ public SchemaPath parse(String xpath, FeatureType rootNode, NamespaceContext nam
schemaPath = new SchemaPath();
schemaPath.appendChild(rootNode);

xpath = xpath.trim().replaceAll("^/", "");
String[] stepExprs = xpath.split("/", -1);
xpath = xpath.trim().replaceAll("^/+", "");
List<String> stepExprs = splitXPath(xpath);

for (String stepExpr : stepExprs) {
if (stepExpr.isEmpty())
Expand Down Expand Up @@ -495,4 +479,29 @@ else if ("false".equals(value))
return literal;
}

private List<String> splitXPath(String xpath) {
List<String> tokens = new ArrayList<>();
char beginStringLiteral = 0;
boolean isStringLiteral = false;
int offset = 0;
int next;

for (next = 0; next < xpath.length(); next++) {
char ch = xpath.charAt(next);
if (ch == '\'' || ch == '"') {
if (!isStringLiteral) {
beginStringLiteral = ch;
isStringLiteral = true;
} else if (ch == beginStringLiteral) {
isStringLiteral = false;
}
} else if (ch == '/' && !isStringLiteral) {
tokens.add(xpath.substring(offset, next));
offset = next + 1;
}
}

tokens.add(xpath.substring(offset, next));
return tokens;
}
}