summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/validation/modeldriven/parser/XMLModelParser.java
blob: cf7aa380314cceab1d00810bd0af4575e346e472 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/*
 * ============LICENSE_START===================================================
 * Copyright (c) 2018 Amdocs
 * ============================================================================
 * 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.
 * ============LICENSE_END=====================================================
 */
package org.onap.aai.validation.modeldriven.parser;

import java.io.File;
import java.io.IOException;
import java.nio.charset.Charset;
import java.nio.file.Files;
import java.util.List;
import org.dom4j.DocumentException;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.Node;
import org.onap.aai.validation.exception.ValidationServiceError;
import org.onap.aai.validation.exception.ValidationServiceException;

/**
 * Read models from XML file
 *
 */
public class XMLModelParser {

	public static final String MODEL_ROOT_ELEMENT = "model";
	public static final String MODELS_ROOT_ELEMENT = "models";

	private XMLModelParser() {
		// Do nothing
	}

	/**
	 * Parses an xml file and returns the root Element.
	 *
	 * @param modelFile
	 *            The XML File.
	 * @param validateModel
	 *            If true the model will be validated.
	 * @return The root Element of the document.
	 * @throws DocumentException
	 * @throws ValidationServiceException
	 */
	public static Element parse(File modelFile, boolean validateModel) throws ValidationServiceException {
		try {
			byte[] encoded = Files.readAllBytes(modelFile.toPath());
			String modelString = new String(encoded, Charset.defaultCharset());

			return parse(modelString, validateModel);
		} catch (IOException e) {
			throw new ValidationServiceException(ValidationServiceError.MODEL_PARSE_ERROR, e, modelFile.toString());
		}
	}

	/**
	 * Parses an xml String and returns the root Element.
	 *
	 * @param modelString
	 *            The XML String.
	 * @param validateModel
	 *            If true the model will be validated.
	 * @return The root Element of the document, or null if no valid model can be parsed.
	 * @throws DocumentException
	 */
	public static Element parse(String modelString, boolean validateModel) throws ValidationServiceException {
		// Strip namespace information first.
		String model = removeXmlStringNamespaceAndPreamble(modelString);

		try {
			Element modelElement = DocumentHelper.parseText(model).getRootElement();
			if (validateModel && (modelElement == null || !isValidModel(modelElement))) {
				return null;
			}
			return modelElement;
		} catch (DocumentException e) {
			throw new ValidationServiceException(ValidationServiceError.MODEL_PARSE_ERROR, e, model);
		}
	}

	/**
	 * Returns the result of running the XPath expression on the DOM Node.
	 *
	 * @param currentNode
	 *            The current Node being processed.
	 * @param xPath
	 *            The XPath expression to run on the Node.
	 * @return A List of Nodes representing the result of the XPath expression.
	 */
	@SuppressWarnings("unchecked")
	public static List<Node> getObjectsFromXPath(Node currentNode, String xPath) {
		return currentNode.selectNodes(xPath);
	}

	/**
	 * Returns the child model Element that corresponds to the provided root Element and model ID.
	 *
	 * @param rootElement
	 *            The root Element to search.
	 * @param attributeName
	 *            The name of the attribute that holds the model ID.
	 * @param modelId
	 *            The model ID to search for.
	 * @return The model Element that matches the model ID supplied.
	 */
	public static Element getModelElementWithId(Element rootElement, String attributeName, String modelId) {
		Element modelElement = null;
		List<Node> modelsList = getObjectsFromXPath(rootElement, MODEL_ROOT_ELEMENT + "/" + attributeName);
		for (Node model : modelsList) {
			if (model.getText().equals(modelId)) {
				modelElement = model.getParent();
			}
		}
		return modelElement;
	}

	/**
	 * Determines the validity of the supplied model element by checking if the name of the root element is correct.
	 *
	 * @param modelElement
	 *            The model element to check.
	 * @return True if the root element matches the expected name.
	 */
	private static boolean isValidModel(Element modelElement) {
		return MODEL_ROOT_ELEMENT.equals(modelElement.getName());
	}

	/**
	 * Strips all namespace information from the model XML.
	 *
	 * @param modelXML
	 *            The model XML as a String.
	 * @return The model XML String minus the namespace information.
	 */
	private static String removeXmlStringNamespaceAndPreamble(String xmlString) {
		return xmlString.replaceAll("(<\\?[^<]*\\?>)?", "") /* remove preamble */
				.replaceAll("xmlns.*?(\"|\').*?(\"|\')", "") /* remove xmlns declaration */
				.replaceAll("(<)(\\w+:)(.*?>)", "$1$3") /* remove opening tag prefix */
				.replaceAll("(</)(\\w+:)(.*?>)", "$1$3"); /* remove closing tags prefix */
	}
}