2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.onap.aai.schemaservice.nodeschema.validation;
23 import com.google.common.collect.ArrayListMultimap;
24 import com.google.common.collect.Multimap;
26 import java.io.FileInputStream;
27 import java.io.IOException;
28 import java.io.InputStream;
29 import java.util.Collection;
30 import java.util.List;
32 import javax.xml.XMLConstants;
33 import javax.xml.parsers.DocumentBuilder;
34 import javax.xml.parsers.DocumentBuilderFactory;
35 import javax.xml.parsers.ParserConfigurationException;
37 import org.onap.aai.schemaservice.nodeschema.SchemaVersion;
38 import org.w3c.dom.Document;
39 import org.w3c.dom.NodeList;
40 import org.xml.sax.SAXException;
43 * Default duplicate rules for A&AI -
44 * node types may never have a duplicate definition
45 * within the same Version's file set.
47 * Finds all duplicates and what files they're in.
50 public class DefaultDuplicateNodeDefinitionValidationModule
51 implements DuplicateNodeDefinitionValidationModule {
57 * org.onap.aai.nodes.validation.DuplicateNodeDefinitionValidationModule#findDuplicates(java.
61 public String findDuplicates(List<String> files, SchemaVersion v) {
63 final DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
64 docFactory.setFeature(XMLConstants.FEATURE_SECURE_PROCESSING, true);
65 docFactory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true);
66 docFactory.setFeature("http://xml.org/sax/features/external-general-entities", false);
67 docFactory.setFeature("http://xml.org/sax/features/external-parameter-entities", false);
68 docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_DTD, "");
69 docFactory.setAttribute(XMLConstants.ACCESS_EXTERNAL_SCHEMA, "");
70 final DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
72 Multimap<String, String> types = ArrayListMultimap.create();
73 boolean foundDups = false;
74 for (String file : files) {
75 InputStream inputStream = new FileInputStream(file);
76 final Document doc = docBuilder.parse(inputStream);
77 final NodeList list = doc.getElementsByTagName("java-type");
79 for (int i = 0; i < list.getLength(); i++) {
80 String type = list.item(i).getAttributes().getNamedItem("name").getNodeValue();
81 if (types.containsKey(type)) {
84 types.put(type, file);
89 return buildErrorMsg(types, v);
93 } catch (ParserConfigurationException | SAXException | IOException e) {
94 // TODO something useful with this information
95 return e.getMessage();
99 private String buildErrorMsg(Multimap<String, String> types, SchemaVersion v) {
100 StringBuilder errorMsg = new StringBuilder().append("Duplicates found in version ")
101 .append(v.toString()).append(". ");
102 for (String nodeType : types.keySet()) {
103 Collection<String> files = types.get(nodeType);
104 if (files.size() == 1) {
105 continue; // only record the duplicated ones
107 errorMsg.append(nodeType).append(" has definitions in ");
108 for (String file : files) {
109 errorMsg.append(file).append(" ");
112 return errorMsg.toString();