Create SVG in UI
[clamp.git] / src / main / java / org / onap / clamp / clds / util / drawing / DocumentBuilder.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP CLAMP
4  * ================================================================================
5  * Copyright (C) 2019 Nokia. All rights
6  *                             reserved.
7  * ================================================================================
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  * ============LICENSE_END============================================
20  * ===================================================================
21  *
22  */
23
24 package org.onap.clamp.clds.util.drawing;
25
26 import org.apache.batik.svggen.SVGGraphics2D;
27 import org.w3c.dom.Document;
28 import org.w3c.dom.Element;
29 import org.w3c.dom.Node;
30
31 public class DocumentBuilder {
32     private final Document groupingDocument;
33     private final Document documentFactory;
34
35     static final String DATA_ELEMENT_ID_ATTRIBUTE = "data-element-id";
36     static final String DATA_ELEMENT_GROUPING_ATTRIBUTE = "data-grouping-id";
37     static final String DATA_FOR_UI_ATTRIBUTE = "data-for-ui";
38
39     DocumentBuilder(Document groupingDocument, Document documentFactory) {
40         this.groupingDocument = groupingDocument;
41         this.documentFactory = documentFactory;
42     }
43
44     void pushChangestoDocument(SVGGraphics2D g2d, String dataElementId) {
45         pushChangestoDocument(g2d, dataElementId,null,null);
46     }
47
48     void pushChangestoDocument(SVGGraphics2D g2d, String dataElementId, String dataGroupingId, String dataForUI) {
49         Element element =
50             this.documentFactory.createElementNS(SVGGraphics2D.SVG_NAMESPACE_URI,
51                 SVGGraphics2D.SVG_G_TAG);
52         element.setAttribute(DATA_ELEMENT_ID_ATTRIBUTE, dataElementId);
53         if (dataGroupingId != null) {
54             element.setAttribute(DATA_ELEMENT_GROUPING_ATTRIBUTE, dataGroupingId);
55         }
56         if (dataForUI != null) {
57             element.setAttribute(DATA_FOR_UI_ATTRIBUTE, dataForUI);
58         }
59         g2d.getRoot(element);
60         Node node = this.groupingDocument.importNode(element, true);
61         this.groupingDocument.getDocumentElement().appendChild(node);
62     }
63
64     Document getGroupingDocument() {
65         return groupingDocument;
66     }
67 }