X-Git-Url: https://gerrit.onap.org/r/gitweb?a=blobdiff_plain;f=misc%2Fxgen%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fmisc%2Fxgen%2FXGen.java;fp=misc%2Fxgen%2Fsrc%2Fmain%2Fjava%2Forg%2Fonap%2Faaf%2Fmisc%2Fxgen%2FXGen.java;h=09c031113cffa2f23231c23e2992497fddd161fc;hb=ac1e1ec76e9125206be91a2f32c7104c9392dc9a;hp=0000000000000000000000000000000000000000;hpb=686d16c26d0435f892de66755ac8bc2383a739d2;p=aaf%2Fauthz.git diff --git a/misc/xgen/src/main/java/org/onap/aaf/misc/xgen/XGen.java b/misc/xgen/src/main/java/org/onap/aaf/misc/xgen/XGen.java new file mode 100644 index 00000000..09c03111 --- /dev/null +++ b/misc/xgen/src/main/java/org/onap/aaf/misc/xgen/XGen.java @@ -0,0 +1,296 @@ +/** + * ============LICENSE_START==================================================== + * org.onap.aaf + * =========================================================================== + * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved. + * =========================================================================== + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * ============LICENSE_END==================================================== + * + */ + +package org.onap.aaf.misc.xgen; + +import java.io.PrintWriter; +import java.io.Writer; +import java.util.Stack; + +import org.onap.aaf.misc.env.util.IndentPrintWriter; +import org.onap.aaf.misc.env.util.StringBuilderWriter; + +public class XGen> { + + public static int COMMENT_COLUMN = 40; + private StringBuilder backSB = new StringBuilder(); + private Stack backStack = new Stack(); + + protected XGen(Writer w) { + forward = new IndentPrintWriter(w); + } + + public int pushBack(Back b) { + int rv = backStack.size(); + backStack.push(b); + return rv; + } + + public boolean pretty = false; + protected IndentPrintWriter forward; + + public IndentPrintWriter getWriter() { + return forward; + } + + protected PrintWriter back = new PrintWriter( + new StringBuilderWriter(backSB)); + + @SuppressWarnings("unchecked") + public RT pretty() { + pretty = true; + return (RT) this; + } + + protected void prettyln(PrintWriter pw) { + if(pretty)pw.println(); + } + + public RT leaf(Mark mark, String tag, String ... args) { + mark.spot = backStack.size(); + return leaf(tag, args); + } + + @SuppressWarnings("unchecked") + public RT leaf(String tag, String ... attrs) { + forward.append('<'); + forward.append(tag); + addAttrs(attrs); + forward.append('>'); + back.append("'); + backStack.push(new Back(backSB.toString(), false, true)); + backSB.setLength(0); + return (RT)this; + } + + public RT incr(String tag, String ... args) { + return incr(null, tag, false, args); + } + + public RT incr(String tag, boolean oneLine, String ... args) { + return incr(null, tag, oneLine, args); + } + + public RT incr(Mark mark) { + return incr(mark,mark.comment, false, new String[0]); + } + + public RT incr(Mark mark, String tag, String ... attrs) { + return incr(mark, tag, false, attrs); + } + + @SuppressWarnings("unchecked") + public RT incr(Mark mark, String tag, boolean oneLine, String ... attrs) { + forward.append('<'); + forward.append(tag); + addAttrs(attrs); + forward.append('>'); + + back.append("'); + + if(pretty) { + if(mark!=null && mark.comment!=null) { + int fi = forward.getIndent()*IndentPrintWriter.INDENT; + for(int i = fi+backSB.length();i<=COMMENT_COLUMN;++i) { + back.append(' '); + } + back.append(""); + + forward.toCol(COMMENT_COLUMN); + forward.append(""); + } + forward.inc(); + if(!oneLine) { + forward.println(); + } + back.println(); + } + if(mark!=null)mark.spot = backStack.size(); + backStack.push(new Back(backSB.toString(),true, false)); + backSB.setLength(0); + return (RT)this; + } + + @SuppressWarnings("unchecked") + public RT tagOnly(String tag, String ... attrs) { + forward.append('<'); + forward.append(tag); + addAttrs(attrs); + forward.append(" />"); + if(pretty) { + forward.println(); + } + return (RT)this; + } + + @SuppressWarnings("unchecked") + public RT text(String txt) { + forward.append(txt); + return (RT)this; + } + + @SuppressWarnings("unchecked") + public RT xml(String txt) { + for(int i=0; i': + forward.append(">"); + break; + case '&': + forward.append("&"); + break; + default: + forward.append(c); + } + } + return (RT)this; + } + + + @SuppressWarnings("unchecked") + public RT textCR(int tabs, String txt) { + for(int i=0;i0) { + forward.append(' '); + String[] split = attr.split("=",2); + switch(split.length) { + case 0: + break; + case 1: + forward.append(split[0]); +// forward.append("=\"\""); + break; + default: + forward.append(split[0]); + forward.append("=\""); + forward.append(split[1]); + forward.append('"'); + break; + } + } + } + } + } + + @SuppressWarnings("unchecked") + public RT comment(String string) { + if(pretty) { + forward.print(""); + } + return (RT)this; + } + + public void setIndent(int indent) { + forward.setIndent(indent); + forward.toIndent(); + } + + public int getIndent() { + return forward.getIndent(); + } + +}