AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / xgen / src / main / java / org / onap / aaf / misc / xgen / XGenBuff.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.misc.xgen;
23
24 import java.io.IOException;
25
26 import org.onap.aaf.misc.env.APIException;
27 import org.onap.aaf.misc.env.Env;
28 import org.onap.aaf.misc.env.Trans;
29 import org.onap.aaf.misc.env.util.StringBuilderWriter;
30 import org.onap.aaf.misc.xgen.html.State;
31
32 public class XGenBuff<G extends XGen<G>> {
33         private G xgen;
34         private StringBuilder sb;
35         // private String forward, backward;
36         
37         public XGenBuff(int flags, CacheGen<G> cg) {
38                 sb = new StringBuilder();
39                 xgen = cg.create(flags, new StringBuilderWriter(sb));
40         }
41
42         /**
43          * Normal case of building up Cached HTML without transaction info
44          * 
45          * @param cache
46          * @param code
47          * @throws APIException
48          * @throws IOException
49          */
50         public void run(Cache<G> cache, Code<G> code) throws APIException, IOException {
51                 code.code(cache, xgen);
52         }
53
54         /**
55          * Special Case where code is dynamic, so give access to State and Trans info
56          *  
57          * @param state
58          * @param trans
59          * @param cache
60          * @param code
61          * @throws APIException
62          * @throws IOException
63          */
64         @SuppressWarnings({ "unchecked", "rawtypes" })
65         public void run(State<Env> state, Trans trans, Cache cache, DynamicCode code) throws APIException, IOException {
66                         code.code(state, trans, cache, xgen);
67         }
68         
69         public int getIndent() {
70                 return xgen.getIndent();
71         }
72
73         public void setIndent(int indent) {
74                 xgen.setIndent(indent);
75         }
76
77         public Section<G> newSection() {
78                 Section<G> s = new Section<G>();
79                 s.indent = xgen.getIndent();
80                 s.forward = sb.toString();
81                 sb.setLength(0);
82                 s.backward = sb.toString();
83                 sb.setLength(0);
84                 return s;
85         }
86 }