io.atlasmap.core.AtlasPath Maven / Gradle / Ivy
/**
* Copyright (C) 2017 Red Hat, Inc.
*
* 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.
*/
package io.atlasmap.core;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import io.atlasmap.v2.CollectionType;
public class AtlasPath {
public static final String PATH_SEPARATOR = "/";
public static final char PATH_SEPARATOR_CHAR = '/';
public static final String PATH_SEPARATOR_ESCAPED = "/";
public static final String PATH_ARRAY_START = "[";
public static final String PATH_ARRAY_END = "]";
public static final String PATH_LIST_START = "<";
public static final String PATH_LIST_END = ">";
public static final String PATH_MAP_START = "{";
public static final String PATH_MAP_END = "}";
public static final String PATH_ATTRIBUTE_PREFIX = "@";
public static final String PATH_NAMESPACE_SEPARATOR = ":";
protected List segmentContexts = new ArrayList<>();
private String originalPath = null;
public AtlasPath(String p) {
String path = p;
this.originalPath = path;
if (path != null && !"".equals(path)) {
if (path.startsWith(PATH_SEPARATOR)) {
path = path.replaceFirst(PATH_SEPARATOR, "");
}
if (path.contains(PATH_SEPARATOR)) {
String[] parts = path.split(PATH_SEPARATOR_ESCAPED, 512);
for (String part : parts) {
this.segmentContexts.add(createSegmentContext(part));
}
} else {
this.segmentContexts.add(createSegmentContext(path));
}
}
if (this.segmentContexts.isEmpty() || !this.segmentContexts.get(0).isRoot()) {
// add root segment if there's not
this.segmentContexts.add(0, createSegmentContext(""));
}
}
protected SegmentContext createSegmentContext(String expression) {
return new SegmentContext(expression);
}
private AtlasPath() {
}
public AtlasPath appendField(String fieldExpression) {
this.segmentContexts.add(createSegmentContext(fieldExpression));
return this;
}
public List getSegments(boolean includeRoot) {
if (includeRoot) {
return Collections.unmodifiableList(this.segmentContexts);
}
if (this.segmentContexts.size() > 1) {
return Collections.unmodifiableList(this.segmentContexts.subList(1, this.segmentContexts.size()));
}
return Collections.emptyList();
}
public Boolean isRoot() {
return this.segmentContexts.size() == 1 && this.segmentContexts.get(0).isRoot();
}
public SegmentContext getRootSegment() {
return this.segmentContexts.get(0);
}
public Boolean isCollectionRoot() {
return this.segmentContexts.size() == 1 && this.segmentContexts.get(0).getCollectionType() != CollectionType.NONE;
}
public Boolean hasCollectionRoot() {
return this.segmentContexts.get(0).getCollectionType() != CollectionType.NONE;
}
public SegmentContext getLastSegment() {
return this.segmentContexts.get(this.segmentContexts.size()-1);
}
public SegmentContext getLastSegmentParent() {
if (this.segmentContexts.isEmpty() || this.segmentContexts.size() == 1) {
return null;
}
return this.segmentContexts.get(this.segmentContexts.size() - 2);
}
public AtlasPath getLastSegmentParentPath() {
if (this.segmentContexts.isEmpty() || this.segmentContexts.size() == 1) {
return null;
}
AtlasPath parentPath = new AtlasPath();
for (int i = 0; i < this.segmentContexts.size() - 1; i++) {
parentPath.appendField(this.segmentContexts.get(i).getExpression());
}
return parentPath;
}
public SegmentContext getParentSegmentOf(SegmentContext sc) {
for (int i=0; i 0) {
return Integer.valueOf(index);
}
return null;
}
if (expression.contains(PATH_LIST_START) && expression.endsWith(PATH_LIST_END)) {
int start = expression.indexOf(PATH_LIST_START, 0) + 1;
String index = expression.substring(start, expression.indexOf(PATH_LIST_END, start));
if (index != null && index.length() > 0) {
return Integer.valueOf(index);
}
return null;
}
return null;
}
private String getMapKey(String expression) {
int start = expression.indexOf(PATH_MAP_START, 0) + 1;
String key = expression.substring(start, expression.indexOf(PATH_MAP_END, start));
if (key != null && key.length() > 0) {
return key;
}
return null;
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy