@atomist/sdm-pack-spring
Version:
Atomist software delivery machine extension pack for Spring and Spring Boot applications
109 lines • 4.38 kB
JavaScript
;
/*
* Copyright © 2018 Atomist, 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.
*/
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
return new (P || (P = Promise))(function (resolve, reject) {
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
step((generator = generator.apply(thisArg, _arguments || [])).next());
});
};
Object.defineProperty(exports, "__esModule", { value: true });
const automation_client_1 = require("@atomist/automation-client");
const xmldoc_1 = require("xmldoc");
/**
* FileParser implementation that uses xmldoc library.
* Preserves and exposes positions.
*/
class XmldocFileParser {
constructor() {
this.rootName = "xml";
}
toAst(f) {
return __awaiter(this, void 0, void 0, function* () {
try {
const content = yield f.getContent();
const document = new xmldoc_1.XmlDocument(content);
// console.log("DOC is " + JSON.stringify(document));
return new XmldocTreeNodeImpl(document, undefined, content);
}
catch (err) {
automation_client_1.logger.warn("Could not parse XML document at '%s'", f.path, err);
return undefined;
}
});
}
}
exports.XmldocFileParser = XmldocFileParser;
function isXmldocTreeNode(tn) {
const maybe = tn;
return !!maybe.innerValue;
}
exports.isXmldocTreeNode = isXmldocTreeNode;
class XmldocTreeNodeImpl {
constructor(xd, $parent, rawDoc) {
this.xd = xd;
this.$parent = $parent;
this.rawDoc = rawDoc;
// Add attributes to this
for (const propName of Object.getOwnPropertyNames(this.xd.attr)) {
this[propName] = this.xd.attr[propName];
automation_client_1.logger.debug("Copying property '%s' of '%s': Now have %j", propName, this.xd.attr[propName], this);
}
}
get $children() {
return this.xd.children
.filter(kid => kid.type === "element")
.map(k => new XmldocTreeNodeImpl(k, this, this.rawDoc));
}
get $name() {
return this.xd.name;
}
get $offset() {
return this.xd.startTagPosition - 1;
}
/**
* This is the full element value
* @return {string}
*/
get $value() {
// toString may not be accurate, as per xmldoc readme, but we can work with it
// as the offset will be accurate
const fromXmldocToString = this.xd.toString({ preserveWhitespace: true, compressed: false, trimmed: false });
const fromRawDoc = this.rawDoc.substr(this.$offset, fromXmldocToString.length);
if (fromRawDoc !== fromXmldocToString) {
// In this case, check we have all the non whitespace characters from the toString value
const nonWhitespaceCount = fromXmldocToString.replace(/\s+/g, "").length;
let included = 0;
let str = "";
for (let i = 0; i < fromXmldocToString.length && included < nonWhitespaceCount; i++) {
const c = this.rawDoc.substr(this.$offset).charAt(i);
if (!["\n", " ", "\t", "\r"].includes(c)) {
++included;
}
str += c;
}
return str;
}
return fromXmldocToString;
}
get innerValue() {
return this.xd.val;
}
}
//# sourceMappingURL=XmldocFileParser.js.map