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
12 * 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=========================================================
20 * ECOMP and OpenECOMP are trademarks
21 * and service marks of AT&T Intellectual Property.
23 package org.openecomp.modelloader.entity.model;
25 import java.io.StringReader;
27 import javax.xml.parsers.DocumentBuilder;
28 import javax.xml.parsers.DocumentBuilderFactory;
30 import org.openecomp.cl.api.Logger;
31 import org.openecomp.cl.eelf.LoggerFactory;
32 import org.openecomp.modelloader.service.ModelLoaderMsgs;
33 import org.w3c.dom.Document;
34 import org.w3c.dom.Element;
35 import org.xml.sax.InputSource;
37 public class ModelParserFactory {
38 private static Logger logger = LoggerFactory.getInstance().getLogger(ModelParserFactory.class.getName());
40 private static String MODEL_ELEMENT = "model";
41 private static String NAMED_QUERY_ELEMENT = "named-query";
43 public static IModelParser createModelParser(byte[] artifactPayload, String artifactName) {
47 String payload = new String(artifactPayload);
48 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
49 DocumentBuilder builder;
50 builder = factory.newDocumentBuilder();
51 InputSource is = new InputSource(new StringReader(payload));
52 doc = builder.parse(is);
53 } catch (Exception e) {
54 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName);
58 if (doc.getDocumentElement().getNodeName().equalsIgnoreCase(NAMED_QUERY_ELEMENT)) {
59 return new NamedQueryArtifactParser();
62 if (!doc.getDocumentElement().getNodeName().equalsIgnoreCase(MODEL_ELEMENT)) {
63 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName
64 + ": Invalid root element: " + doc.getDocumentElement().getNodeName());
68 Element e = doc.getDocumentElement();
69 String ns = e.getAttribute("xmlns");
70 String[] parts = ns.split("/");
72 if (parts.length < 1) {
73 logger.error(ModelLoaderMsgs.ARTIFACT_PARSE_ERROR, "Unable to parse artifact " + artifactName
74 + ": Could not parse namespace version");
78 String modelNamespaceVersion = parts[parts.length-1].trim().replace("v", "");
79 int version = Integer.parseInt(modelNamespaceVersion);
82 return new ModelArtifactParser();
85 return new ModelV8ArtifactParser();