2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 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====================================================
22 package org.onap.aaf.misc.xgen;
24 import java.io.PrintWriter;
25 import java.io.Writer;
26 import java.util.Stack;
28 import org.onap.aaf.misc.env.util.IndentPrintWriter;
29 import org.onap.aaf.misc.env.util.StringBuilderWriter;
31 public class XGen<RT extends XGen<RT>> {
33 public static int COMMENT_COLUMN = 40;
34 private StringBuilder backSB = new StringBuilder();
35 private Stack<Back> backStack = new Stack<Back>();
37 protected XGen(Writer w) {
38 forward = new IndentPrintWriter(w);
41 public int pushBack(Back b) {
42 int rv = backStack.size();
47 public boolean pretty = false;
48 protected IndentPrintWriter forward;
50 public IndentPrintWriter getWriter() {
54 protected PrintWriter back = new PrintWriter(
55 new StringBuilderWriter(backSB));
57 @SuppressWarnings("unchecked")
63 protected void prettyln(PrintWriter pw) {
64 if(pretty)pw.println();
67 public RT leaf(Mark mark, String tag, String ... args) {
68 mark.spot = backStack.size();
69 return leaf(tag, args);
72 @SuppressWarnings("unchecked")
73 public RT leaf(String tag, String ... attrs) {
81 backStack.push(new Back(backSB.toString(), false, true));
86 public RT incr(String tag, String ... args) {
87 return incr(null, tag, false, args);
90 public RT incr(String tag, boolean oneLine, String ... args) {
91 return incr(null, tag, oneLine, args);
94 public RT incr(Mark mark) {
95 return incr(mark,mark.comment, false, new String[0]);
98 public RT incr(Mark mark, String tag, String ... attrs) {
99 return incr(mark, tag, false, attrs);
102 @SuppressWarnings("unchecked")
103 public RT incr(Mark mark, String tag, boolean oneLine, String ... attrs) {
114 if(mark!=null && mark.comment!=null) {
115 int fi = forward.getIndent()*IndentPrintWriter.INDENT;
116 for(int i = fi+backSB.length();i<=COMMENT_COLUMN;++i) {
119 back.append("<!-- end ");
120 back.append(mark.comment);
123 forward.toCol(COMMENT_COLUMN);
124 forward.append("<!-- begin ");
125 forward.append(mark.comment);
126 forward.append(" -->");
134 if(mark!=null)mark.spot = backStack.size();
135 backStack.push(new Back(backSB.toString(),true, false));
140 @SuppressWarnings("unchecked")
141 public RT tagOnly(String tag, String ... attrs) {
145 forward.append(" />");
152 @SuppressWarnings("unchecked")
153 public RT text(String txt) {
158 @SuppressWarnings("unchecked")
159 public RT xml(String txt) {
160 for(int i=0; i<txt.length();++i) {
161 char c = txt.charAt(i);
164 forward.append("<");
167 forward.append(">");
170 forward.append("&");
180 @SuppressWarnings("unchecked")
181 public RT textCR(int tabs, String txt) {
182 for(int i=0;i<tabs;++i) {
186 if(pretty)forward.println();
190 @SuppressWarnings("unchecked")
192 Mark mark = new Mark();
193 mark.spot = backStack.size()-1;
198 @SuppressWarnings("unchecked")
199 public RT value(String txt) {
201 Mark mark = new Mark();
202 mark.spot = backStack.size()-1;
207 @SuppressWarnings("unchecked")
208 public RT value(String txt, int levels) {
210 Mark mark = new Mark();
211 mark.spot = backStack.size()-levels;
216 @SuppressWarnings("unchecked")
217 public RT end(Mark mark) {
218 int size = backStack.size();
220 boolean println = false;
221 for(int i=mark==null?0:mark.spot;i<size;++i) {
223 if(c.dec)forward.dec();
224 forward.append(c.str);
227 if(pretty && println) {
234 Mark mark = new Mark();
235 mark.spot=backStack.size()-1;
236 if(mark.spot<0)mark.spot=0;
240 public RT end(int i) {
241 Mark mark = new Mark();
242 mark.spot=backStack.size()-i;
243 if(mark.spot<0)mark.spot=0;
247 public void endAll() {
252 protected void addAttrs(String[] attrs) {
254 for(String attr : attrs) {
255 if(attr!=null && attr.length()>0) {
257 String[] split = attr.split("=",2);
258 switch(split.length) {
262 forward.append(split[0]);
263 // forward.append("=\"\"");
266 forward.append(split[0]);
267 forward.append("=\"");
268 forward.append(split[1]);
277 @SuppressWarnings("unchecked")
278 public RT comment(String string) {
280 forward.print("<!-- ");
281 forward.print(string);
282 forward.println(" -->");
287 public void setIndent(int indent) {
288 forward.setIndent(indent);
292 public int getIndent() {
293 return forward.getIndent();