AT&T 2.0.19 Code drop, stage 1
[aaf/authz.git] / misc / xgen / src / main / java / org / onap / aaf / misc / xgen / CacheGen.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 import java.io.OutputStream;
26 import java.io.OutputStreamWriter;
27 import java.io.Writer;
28 import java.util.ArrayList;
29
30 import org.onap.aaf.misc.env.APIException;
31 import org.onap.aaf.misc.env.Env;
32 import org.onap.aaf.misc.env.Trans;
33 import org.onap.aaf.misc.xgen.html.State;
34 import org.onap.aaf.misc.xgen.html.Thematic;
35
36
37 public abstract class CacheGen<G extends XGen<G>> {
38         public static final int NO_FLAGS = 0x0;
39         public final static int PRETTY  = 0x1;
40         public final static int XML             = 0x2;
41         public final static int HTML4   = 0x4;
42         public final static int HTML5   = 0x8;
43
44         
45         private ArrayList<Section<G>> sections = new ArrayList<Section<G>>();
46         private int flags;
47         private final Thematic thematic;
48
49         public CacheGen(int flags, Code<G> code) throws APIException, IOException {
50                 this.flags = flags;
51                 final XGenBuff<G> buff = new XGenBuff<G>(flags,this);
52                 // Run to gather Strings and Code Class Segments
53                 buff.run(new Cache<G>() {
54                                 @Override
55                                 public void dynamic(G hgen, Code<G> code) {
56                                         sections.add(buff.newSection());
57                                         sections.add(new Dynamic(hgen.getIndent(),code));
58                                 }
59                         },code);
60                 sections.add(buff.newSection());
61         
62                 // If Code implements thematic, set for later
63                 thematic = code instanceof Thematic?(Thematic)code:null;
64
65         }
66         
67         public abstract G create(int htmlStyle, Writer w);
68
69         public void replay(State<Env> state, Trans trans, OutputStream os, String theme) throws IOException, APIException {
70                 replay(state, trans, new OutputStreamWriter(os), theme);
71         }
72         
73         public void replay(State<Env> state, Trans trans,Writer w, String theme) throws IOException, APIException {
74                 if(thematic!=null) {
75                         theme = thematic.themeResolve(theme);
76                 }
77                 /* Theme
78                 trans.setTheme(theme);
79                 int htmlStyle = state.htmlVer(theme);
80                 */
81                 
82                 XGenBuff<G> buff = new XGenBuff<G>(flags,this);
83                 
84                 // forward
85                 int indent = 0;
86                 Section<G> s;
87                 int i=0;
88                 @SuppressWarnings("unchecked")
89                 Section<G>[] reverse = new Section[sections.size()];
90                 for(Section<G> section : sections) {
91                         s = section.use(state, trans, buff); // note, doesn't change cached, only dynamic, which is created for thread
92                         int tempIndent = s.getIndent();
93                         s.setIndent(indent);
94                         s.forward(w);
95                         s.setIndent(tempIndent);
96                         indent = tempIndent;
97                         reverse[i++]=s;
98                 }
99
100                 for(--i;i>=0;--i) {
101                         reverse[i].back(w);
102                 }
103                 w.flush();
104         }
105         
106         private class Dynamic extends Section<G> {
107                 private Code<G> code;
108                 
109                 public Dynamic(int indent, Code<G> code) {
110                         this.code = code;
111                         this.indent = indent;
112                 }
113
114                 @SuppressWarnings("unchecked")
115                 public Section<G> use(State<Env> state, Trans trans, XGenBuff<G> buff) throws APIException, IOException {
116                         // Clone Dynamic to make Thread Safe
117                         Dynamic d = new Dynamic(indent,code);
118                         buff.setIndent(indent);
119                         if(code instanceof DynamicCode) {
120                                 buff.run(state,trans,Cache.Null.singleton(), (DynamicCode<G,?,? extends Trans>)code);
121                         } else {
122                                 buff.run((Cache<G>)Cache.Null.singleton(), code);
123                         }
124                         Section<G> s = buff.newSection();
125                         d.indent = s.indent;
126                         d.forward = s.forward;
127                         d.backward = s.backward;
128                         return d;
129                 }
130         }
131 }