2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017 AT&T Intellectual Property.
6 * Copyright © 2017 Amdocs
8 * ================================================================================
9 * Licensed under the Apache License, Version 2.0 (the "License");
10 * you may not use this file except in compliance with the License.
11 * You may obtain a copy of the License at
13 * http://www.apache.org/licenses/LICENSE-2.0
15 * Unless required by applicable law or agreed to in writing, software
16 * distributed under the License is distributed on an "AS IS" BASIS,
17 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18 * See the License for the specific language governing permissions and
19 * limitations under the License.
20 * ============LICENSE_END=========================================================
22 * ECOMP is a trademark and service mark of AT&T Intellectual Property.
24 package org.openecomp.modelloader.entity.model;
26 import java.io.StringReader;
28 import javax.xml.parsers.DocumentBuilder;
29 import javax.xml.parsers.DocumentBuilderFactory;
31 import org.openecomp.cl.api.Logger;
32 import org.openecomp.cl.eelf.LoggerFactory;
33 import org.openecomp.modelloader.service.ModelLoaderMsgs;
34 import org.w3c.dom.Document;
35 import org.w3c.dom.Element;
36 import org.xml.sax.InputSource;
38 public class ModelParserFactory {
39 private static Logger logger = LoggerFactory.getInstance().getLogger(ModelParserFactory.class.getName());
41 private static String MODEL_ELEMENT = "model";
42 private static String NAMED_QUERY_ELEMENT = "named-query";
44 public static IModelParser createModelParser(byte[] artifactPayload, String artifactName) {
48 String payload = new String(artifactPayload);
49 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
50 DocumentBuilder builder;
51 builder = factory.newDocumentBuilder();
52 InputSource is = new InputSource(new StringReader(payload));
53 doc = builder.parse(is);
54 } catch (Exception e) {
55 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
59 if (doc.getDocumentElement().getNodeName().equalsIgnoreCase(NAMED_QUERY_ELEMENT)) {
60 return new NamedQueryArtifactParser();
63 if (!doc.getDocumentElement().getNodeName().equalsIgnoreCase(MODEL_ELEMENT)) {
64 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName
65 + ": Invalid root element: " + doc.getDocumentElement().getNodeName());
69 Element e = doc.getDocumentElement();
70 String ns = e.getAttribute("xmlns");
71 String[] parts = ns.split("/");
73 if (parts.length < 1) {
74 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName
75 + ": Could not parse namespace version");
79 String modelNamespaceVersion = parts[parts.length-1].trim().replace("v", "");
80 int version = Integer.parseInt(modelNamespaceVersion);
83 return new ModelArtifactParser();
86 return new ModelV8ArtifactParser();