fastman
Version:
快速api测试及文档生成
222 lines • 8.05 kB
JavaScript
;
/**
* @license
* Copyright 2017 Red Hat
*
* 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.
*/
Object.defineProperty(exports, "__esModule", { value: true });
/**
* Represents a canonical path to a node within a OAS document model. The node path
* can be used to identify and locate a single model in the document tree.
*/
var OasNodePath = /** @class */ (function () {
function OasNodePath(path) {
this._segments = [];
if (path && path.indexOf("/") === 0 && path !== "/") {
var currentScanType = "path";
var currentIdx = 1;
while (currentIdx < path.length) {
var segStart = currentIdx;
var segEnd = void 0;
if (currentScanType === "path") {
var nextPathSep = path.indexOf("/", segStart);
var nextBrace = path.indexOf("[", segStart);
if (nextPathSep === -1) {
nextPathSep = path.length;
}
if (nextBrace === -1) {
nextBrace = path.length;
}
if (nextPathSep <= nextBrace) {
segEnd = nextPathSep;
}
else {
segEnd = nextBrace;
}
}
else {
var nextCloseBrace = path.indexOf("]", segStart);
if (nextCloseBrace === -1) {
nextCloseBrace = path.length;
}
segEnd = nextCloseBrace + 1;
}
var seg = path.substring(segStart, segEnd);
var segment = OasNodePathSegment.fromString(seg);
this._segments.push(segment);
// Default next values.
currentScanType = "path";
currentIdx = segEnd + 1;
// Find real next values.
if (path.charAt(segEnd) === '/') {
currentScanType = "path";
currentIdx = segEnd + 1;
}
else if (path.charAt(segEnd) === '[') {
currentScanType = "index";
currentIdx = segEnd;
}
else if (path.charAt(segEnd) === ']') {
if (path.charAt(segEnd + 1) === '[') {
currentScanType = "index";
currentIdx = segEnd + 1;
}
else if (path.charAt(segEnd + 1) === '/') {
currentScanType = "path";
currentIdx = segEnd + 1;
}
}
}
}
}
/**
* Adds a segment to the beginning of the path.
* @param value
* @param index
*/
OasNodePath.prototype.prependSegment = function (value, index) {
this._segments.unshift(new OasNodePathSegment(value, index));
};
/**
* Adds a segment to the end of the path.
* @param value
* @param index
*/
OasNodePath.prototype.appendSegment = function (value, index) {
this._segments.push(new OasNodePathSegment(value, index));
};
/**
* Resolves a path to its target node within the document model. This basically
* walks the tree according to the path segments until it reaches the node being
* referenced. If the path does not point to a valid node, then this method
* returns undefined.
* @param document the document to resolve the path relative to
* @param visitor an optional visitor to invoke for each node in the path
* @return {OasNode}
*/
OasNodePath.prototype.resolve = function (document, visitor) {
var node = document;
if (visitor) {
node.accept(visitor);
}
for (var _i = 0, _a = this._segments; _i < _a.length; _i++) {
var segment = _a[_i];
node = segment.resolve(node);
if (visitor && node && node["accept"]) {
node.accept(visitor);
}
}
return node;
};
/**
* Returns true if this path "contains" the given node. The path is said to contain
* a node if the node is visited while resolving it. In other words, if one of the
* segments of the path represents the node, then this will return true, otherwise it
* will return false.
* @param {OasNode} node
* @return {boolean}
*/
OasNodePath.prototype.contains = function (node) {
var tnode = node.ownerDocument();
// Of course the root document is always a match.
if (tnode === node) {
return true;
}
for (var _i = 0, _a = this._segments; _i < _a.length; _i++) {
var segment = _a[_i];
tnode = segment.resolve(tnode);
if (tnode === node) {
return true;
}
}
return false;
};
/**
* Converts the path to a string.
*/
OasNodePath.prototype.toString = function () {
if (this._segments.length === 0) {
return "/";
}
var rval = "";
for (var _i = 0, _a = this._segments; _i < _a.length; _i++) {
var segment = _a[_i];
if (segment.isIndex()) {
rval += '[' + segment.value() + ']';
}
else {
rval += '/' + segment.value();
}
}
return rval;
};
return OasNodePath;
}());
exports.OasNodePath = OasNodePath;
/**
* Represents a single segment in a model node path.
*/
var OasNodePathSegment = /** @class */ (function () {
function OasNodePathSegment(value, index) {
this._index = false;
this._value = value;
if (index) {
this._index = true;
}
}
OasNodePathSegment.prototype.value = function () {
return this._value;
};
OasNodePathSegment.prototype.isIndex = function () {
return this._index;
};
OasNodePathSegment.prototype.resolve = function (node) {
if (node === null) {
return null;
}
var childNode = null;
if (this.isIndex() && node["__instanceof_IOasIndexedNode"]) {
childNode = node.getItem(this.value());
}
else {
childNode = node[this.value()];
if (childNode === undefined) {
childNode = null;
}
}
return childNode;
};
/**
* Creates a new segment from a string.
* @param segment
* @return {OasNodePathSegment}
*/
OasNodePathSegment.fromString = function (segment) {
if (!segment) {
return new OasNodePathSegment(null);
}
if (segment.indexOf("[") !== 0) {
return new OasNodePathSegment(segment);
}
else {
var bStart = segment.indexOf("[");
var bEnd = segment.indexOf("]", bStart);
var value = segment.substring(bStart + 1, bEnd);
return new OasNodePathSegment(value, true);
}
};
return OasNodePathSegment;
}());
exports.OasNodePathSegment = OasNodePathSegment;
//# sourceMappingURL=node-path.js.map