Minor HTML Gen improvments
[aaf/authz.git] / misc / xgen / src / main / java / org / onap / aaf / misc / xgen / CacheGen.java
1 /**\r
2  * ============LICENSE_START====================================================\r
3  * org.onap.aaf\r
4  * ===========================================================================\r
5  * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.\r
6  * ===========================================================================\r
7  * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * you may not use this file except in compliance with the License.\r
9  * You may obtain a copy of the License at\r
10  * \r
11  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * \r
13  * Unless required by applicable law or agreed to in writing, software\r
14  * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * See the License for the specific language governing permissions and\r
17  * limitations under the License.\r
18  * ============LICENSE_END====================================================\r
19  *\r
20  */\r
21 \r
22 package org.onap.aaf.misc.xgen;\r
23 \r
24 import java.io.IOException;\r
25 import java.io.OutputStream;\r
26 import java.io.OutputStreamWriter;\r
27 import java.io.Writer;\r
28 import java.util.ArrayList;\r
29 \r
30 import org.onap.aaf.misc.env.APIException;\r
31 import org.onap.aaf.misc.env.Env;\r
32 import org.onap.aaf.misc.env.Trans;\r
33 import org.onap.aaf.misc.xgen.html.State;\r
34 import org.onap.aaf.misc.xgen.html.Thematic;\r
35 \r
36 \r
37 public abstract class CacheGen<G extends XGen<G>> {\r
38     public static final int NO_FLAGS = 0x0;\r
39     public final static int PRETTY    = 0x1;\r
40     public final static int XML        = 0x2;\r
41     public final static int HTML4     = 0x4;\r
42     public final static int HTML5     = 0x8;\r
43 \r
44     \r
45     private ArrayList<Section<G>> sections = new ArrayList<>();\r
46     private int flags;\r
47     private final Thematic thematic;\r
48 \r
49     public CacheGen(int flags, Code<G> code) throws APIException, IOException {\r
50         this.flags = flags;\r
51         final XGenBuff<G> buff = new XGenBuff<G>(flags,this);\r
52         // Run to gather Strings and Code Class Segments\r
53         buff.run(new Cache<G>() {\r
54                 @Override\r
55                 public void dynamic(G hgen, Code<G> code) {\r
56                     sections.add(buff.newSection());\r
57                     sections.add(new Dynamic(hgen.getIndent(),code));\r
58                 }\r
59             },code);\r
60         sections.add(buff.newSection());\r
61     \r
62         // If Code implements thematic, set for later\r
63         thematic = code instanceof Thematic?(Thematic)code:null;\r
64 \r
65     }\r
66     \r
67     public abstract G create(int htmlStyle, Writer w);\r
68 \r
69     public void replay(State<Env> state, Trans trans, OutputStream os, String theme) throws IOException, APIException {\r
70         replay(state, trans, new OutputStreamWriter(os), theme);\r
71     }\r
72     \r
73     public void replay(State<Env> state, Trans trans,Writer w, String theme) throws IOException, APIException {\r
74         if (thematic!=null) {\r
75             theme = thematic.themeResolve(theme);\r
76         }\r
77         /* Theme\r
78         trans.setTheme(theme);\r
79         int htmlStyle = state.htmlVer(theme);\r
80         */\r
81         \r
82         XGenBuff<G> buff = new XGenBuff<G>(flags,this);\r
83         \r
84         // forward\r
85         int indent = 0;\r
86         Section<G> s;\r
87         int i=0;\r
88         @SuppressWarnings("unchecked")\r
89         Section<G>[] reverse = new Section[sections.size()];\r
90         for (Section<G> section : sections) {\r
91             s = section.use(state, trans, buff); // note, doesn't change cached, only dynamic, which is created for thread\r
92             int tempIndent = s.getIndent();\r
93             s.setIndent(indent);\r
94             s.forward(w);\r
95             s.setIndent(tempIndent);\r
96             indent = tempIndent;\r
97             reverse[i++]=s;\r
98         }\r
99 \r
100         for (--i;i>=0;--i) {\r
101             reverse[i].back(w);\r
102         }\r
103         w.flush();\r
104     }\r
105     \r
106     private class Dynamic extends Section<G> {\r
107         private Code<G> code;\r
108         \r
109         public Dynamic(int indent, Code<G> code) {\r
110             this.code = code;\r
111             this.indent = indent;\r
112         }\r
113 \r
114         @SuppressWarnings("unchecked")\r
115         public Section<G> use(State<Env> state, Trans trans, XGenBuff<G> buff) throws APIException, IOException {\r
116             // Clone Dynamic to make Thread Safe\r
117             Dynamic d = new Dynamic(indent,code);\r
118             buff.setIndent(indent);\r
119             if (code instanceof DynamicCode) {\r
120                 buff.run(state,trans,Cache.Null.singleton(), (DynamicCode<G,?,? extends Trans>)code);\r
121             } else {\r
122                 buff.run((Cache<G>)Cache.Null.singleton(), code);\r
123             }\r
124             Section<G> s = buff.newSection();\r
125             d.indent = s.indent;\r
126             d.forward = s.forward;\r
127             d.backward = s.backward;\r
128             return d;\r
129         }\r
130     }\r
131 }\r